My 5 lines Java solution

  • 0
    public int searchInsert(int[] nums, int target) {
            int count =0;
            for(int i=0;i<nums.length;i++){
                if(nums[i] == target) return i;
                if(target>nums[i]) count++;
            return count;

    Here, we will return the index if the element is present in the array.
    In the other case, we compare if the target is greater than the current value. If yes, we simply increment the target's could be position and return the count.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.