1

I have an array in ascending order, consisting of positive integers and with condition duplicates are not allowed.In the array i must find m such that array[m]=m using binary search I have used below code:

public class Main {

public static void main(String[] args) {
    Scanner a = new Scanner(System.in);
    int n = a.nextInt();
    int arr[] = new int[n];
    for (int i = 0; i < n; i++) {
        arr[i] = a.nextInt();
    }
    int result = binarysearch(arr,n);
    if(result!=-1)
        System.out.println(result);
    else
        System.out.println("NOT_FOUND");
}

public static  int binarysearch(int[] array,int n){
int start=1,end=n;
while(start<=end){
    int mid = (start+end)/2;
    if(mid==array[mid-1])
        return mid;
    else  if(mid<array[mid-1])
        end=mid-1;
    else
        start=mid+1;
}
return -1;
}

Please note that the array is 1-indexed and not 0-indexed The problem I am facing is when i try the following case : array={1,2,3,4,5} the expected output is 1 but due to binary search the method returns 3 as Output. Is there any workaround to bypass this type of cases?

3
  • 2
    This problem trivial if the array only has positive numbers without duplicates in ascending order. You only need to check the first number, if it is 1 then return it. Otherwise, the rest of the array would never be equal to its index. Commented Mar 11, 2020 at 19:25
  • An array is always 0-indexed. Commented Mar 11, 2020 at 19:31
  • 1
    @ArvindKumarAvinash Apparently, the OP is viewing the array as 1-indexed Commented Mar 11, 2020 at 19:32

2 Answers 2

2

Looking at the conditions: "I have an array in ascending order, consisting of positive integers and with condition duplicates are not allowed"

  1. ascending order
  2. positive integers
  3. no duplicates

This problem has a trivial solution.

public static int binarysearch(int[] array,int n){
    If(a[0] == 1) return 1;
    else return -1;
}

Because if the first number in the array is not 1, the rest of the array would not have any value that is equal to its index.

Explanation:

Suppose the array has terms a1, a2, a3 ... an, where non of the terms are equal and aj < ak if j < k

The problem is essentially finding a term ai where ai = i

If a1 is not 1, it has to be greater than 1 according to condition #2.

By condition 1 and 3, a2 has to be greater than 2. Then, a3 has to be greater than 3, and so on. Term an has to be greater than n. Yielding no solution.

On the other hand, if a1 is 1, you have the term and you can return it.

Sign up to request clarification or add additional context in comments.

Comments

1

Your code looks mostly correct, but you should not assume the array is already sorted when the user inputs it.

I think the real problem lies in understanding how binary search works. Binary search always starts in the middle of an array. Thus, if you provide the input {1, 2, 3, 4, 5}, you will always get 3 because the algorithm starts by searching in the middle and notices that the number 3 equals its index.

If your goal is to find the smallest integer m such that a[m] is equal to m, then you should not use binary search. Instead, you should start from the beginning of the array (since we assume it is sorted in ascending order) and use linear search:

for (int i = 0; i < n; ++i) {
  if (a[i] == i) {
    return i;
  }
}
return -1;

Something like the above should give the intended result. (Note that the above uses 0-based index logic, but you should be able to tweak it as needed. Also, arrays in Java are always 0-indexed, so you may want to double-check that you understand the problem correctly.)

Comments

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.