1

Given an array of integers which is sorted in ascending order, and an integer target, write a function to search target in the array. If the target exists, then return its index. Otherwise, return -1.

You must write an algorithm with O(log n) runtime complexity.

I solved the above question using the following code:

int search(int* nums, int numsSize, int target){

    int l = 0;
    int r = numsSize -1;
    int result = -1;
    
    while(l <= r)
    {
        int mid = (l + (r-l))/2;
        
        if(nums[mid] == target)
            return result = nums[mid];
        if(nums[mid] < target)
            return l = mid + 1;
        else
            return r = mid - 1;
    }
    return result;
}

But I am still not getting the right answer. Please help.

3
  • 2
    Under what conditions are you getting the wrong answer? What is an array you are searching? for what target value? What result are you expecting, and what result are you getting? Commented Oct 11, 2021 at 13:55
  • If you have only 1 element in the array (l == r) you automatically return -1 even if that element matches the target. Commented Oct 11, 2021 at 14:25
  • @500-InternalServerError The value returned should be mid as pointed out in Corina's answer below. Commented Oct 11, 2021 at 16:33

4 Answers 4

3

You are returning the newly calculated l and r value.

if(nums[mid] < target)
    return l = mid + 1;
else
    return r = mid - 1;

You just need to update them and keep searching the element.

if(nums[mid] < target)
    l = mid + 1;
else
    r = mid - 1;
Sign up to request clarification or add additional context in comments.

1 Comment

After making these changes, I am getting TIME LIMIT EXCEEDED as result
3

Corrected code:

int search(int* nums, int numsSize, int target){

    int l = 0;
    int r = numsSize -1;
    int result = -1;
    printf("%d %d %d\n", numsSize, target, nums[0]);

    while(l <= r)
    {
        int mid = l + (r - l) / 2;
        
        if(nums[mid] == target)
            return result = mid;
        if(nums[mid] < target)
            l = mid + 1;
        else
            r = mid - 1;
    }
    return result;
}

The errors:

  1. You computed wrongly the mid, it's not:

    int mid = (l + (r-l))/2;

Correct version is:

int mid = l + (r - l) / 2;
  1. You should not use return here because you break the loop.
if(nums[mid] < target)
        return l = mid + 1;
    else
        return r = mid - 1;
    if(nums[mid] == target)
          return result = nums[mid];

Here you should return the mid, the position of the target value, not the value itself.

1 Comment

You don't need the assignment in return result = mid; (simply return mid;), and then you can eliminate result altogether and end with return -1;.
-1

Try this: It is Java though

 public int search(int[] nums, int target) {
    int l = 0;
    int r = nums.length - 1;
    while(l<=r){
        
        int mid = (l+r)/2;
        if(target == nums[l]){
            return l;
        } else if (target == nums[r]){
            return r;
        } else if(target == nums[mid]){
            return mid;
        }
        else if(target > nums[mid]){
            l = mid;
            r = r - 1;
        }
        else {
            l = l + 1;
            r = mid;
        }
    }
    return -1;
}

1 Comment

This is a C question, providing java code with no explanation doesn't really answer the question.
-1

solved using python

def search(self, nums, target):
    
    lowerIndex = 0
    higherIndex = len(nums) -1

    while lowerIndex < higherIndex:
        middle = (lowerIndex + higherIndex) // 2
        mid_number = nums[middle]
    
        if mid_number == target:
            return middle
    
        elif mid_number > target:
            lowerIndex = middle + 1
        elif mid_number < target:
            higherIndex = middle -1
    return -1

1 Comment

It is written in the question that the author wants the code in C only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.