Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Find First and Last Position of Element in Sorted Array

class Solution {
    public int[] searchRange(int[] array, int target){
      int[] index = {-1, -1};
      int start = 0;
      int end = array.length-1;
      while (start <= end) {
          int mid = start + (end - start) / 2;
          if (array[mid] >= target) end = mid - 1;
          else start = mid + 1;
          if (array[mid] == target) index[0] = mid;
      }

      // resetting the start and end
      start = 0;
      end = array.length-1;
      while (start <= end){
          int mid = start  + (end-start) / 2;
          if (array[mid] <= target) start = mid+1;
          else end = mid-1;
          if (array[mid] == target) index[1] = mid;
      }
      return index;
    }
}
Comment

Find First and Last Position of Element in Sorted Array

class Solution {
    public int[] searchRange(int[] nums, int target){
        int[] ans = new int[2];
        ans[0] = searchElementIndex(nums, target, true);
        ans[1] = searchElementIndex(nums, target, false);
        return ans;
    }

    public int searchElementIndex(int[] nums, int target, boolean findFirstIndex){
        int index = -1;
        int start = 0;
        int end = nums.length-1;

        while (start <= end) {
            int mid = start + (end-start) / 2;

            if (findFirstIndex){
                if (nums[mid] >= target) end = mid-1;
                else start = mid+1;
            }else {
                if (nums[mid] <= target) start = mid+1;
                else end = mid-1;
            }
            if (nums[mid]==target) index = mid;
        }
        return index;
    }
}
Comment

Find First and Last Position of Element in Sorted Array

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        
    }
};
Comment

Find First and Last Position of Element in Sorted Array

class Solution {
    public int[] searchRange(int[] nums, int target) {
        
    }
}
Comment

Find First and Last Position of Element in Sorted Array



/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* searchRange(int* nums, int numsSize, int target, int* returnSize){

}
Comment

Find First and Last Position of Element in Sorted Array

public class Solution {
    public int[] SearchRange(int[] nums, int target) {
        
    }
}
Comment

Find First and Last Position of Element in Sorted Array

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var searchRange = function(nums, target) {
    
};
Comment

Find First and Last Position of Element in Sorted Array

# @param {Integer[]} nums
# @param {Integer} target
# @return {Integer[]}
def search_range(nums, target)
    
end
Comment

Find First and Last Position of Element in Sorted Array

class Solution {
    func searchRange(_ nums: [Int], _ target: Int) -> [Int] {
        
    }
}
Comment

Find First and Last Position of Element in Sorted Array

class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $target
     * @return Integer[]
     */
    function searchRange($nums, $target) {
        
    }
}
Comment

Find First and Last Position of Element in Sorted Array

function searchRange(nums: number[], target: number): number[] {

};
Comment

first and last position of a sorted array

class Solution {
   public int[] searchRange(int[] nums, int target) {
    int si=0;
    int ei=nums.length-1;
    int[] ans=new int[]{-1,-1};
while(si<=ei){
    int mid=si+(ei-si)/2;
    if(nums[mid]==target){
        si=mid-1;ei=mid+1;
		// search for first index with while loop in -ve  direction
        while(si>=0&&nums[si]==target){si--;}
        ans[0]=si+1;
		// search for ending index in +ve direction
        while(ei<nums.length&&nums[ei]==target){ei++;}
        ans[1]=ei-1;
        return ans;
    }
    else if(target>nums[mid]){
        si=mid+1;
    }
   
    else ei=mid-1;
}
    return ans;
    
}
}
Comment

PREVIOUS NEXT
Code Example
Java :: observer design pattern 
Java :: java complex numbers 
Java :: java integer object to char 
Java :: mongorepository spring boot custom query 
Java :: java list change element position 
Java :: limit decimals java 
Java :: java boolean data type 
Java :: string into ascii in java 
Java :: search in rotated sorted array leetcode 
Java :: get ocurrences in array java 
Java :: Java Creating a HashSet 
Java :: java float data type 
Java :: single level inheritance in java 
Java :: How to Initialize Arrays in Java? 
Java :: navigation graph setup android 
Java :: java draw image 
Java :: Java Exception handling using try...catch 
Java :: how to divide a double in java 
Java :: calculating the percentile in java 
Java :: abstract class java 
Java :: java firebase syncrhonous data read 
Java :: android activity set action bar options 
Java :: java cant use string functions after changing an integer into string 
Java :: first none duplicated char 
Java :: how to repeat string in java 
Java :: input method manager hide keyboard 
Java :: int in double umwandeln java 
Java :: java 8 lambda delete from list 
Java :: Set value of input in android webview 
Java :: java nom de la methode actuel 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =