Can i do this question with linear search?

import java.util.*;
public class Main {
public static int linearSearch(int[] arr, int data) {

	for (int i = 0; i < arr.length; i++) {
		if(data==arr[i]) {
			return i;
		}
	}
	return -1;
}

public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int N = sc.nextInt();

	int[] arr = new int[N];

	for (int i = 0; i < arr.length; i++) {
		arr[i] = sc.nextInt();
	}
	int data = sc.nextInt();
	
	
	int index = linearSearch(arr, data);
	if (index < 0) {
		System.out.println(index);
	} else {
		System.out.println(index);
	}
}

}

@nigamshubham1998,
you would get TLE because the constraints are large. Hence do it in O(Log N).

Here is the Algorithm that will let you find the element in O(log n)

  1. Find middle point mid = (l + h)/2
  2. If the key is present at the middle point, return mid.
  3. Else If arr[l…mid] is sorted
  a) If the key to be searched lies in the range from arr[l] to 
     arr[mid], recur for arr[l..mid].
  b) Else recur for arr[mid+1..r]
  1. Else (arr[mid+1…r] must be sorted)
  a) If the key to be searched lies in the range from arr[mid+1]
     to arr[r], recur for arr[mid+1..r].
  b) Else recur for arr[l..mid] 

i submit my question in linear search it fines not TLE coming

@nigamshubham1998,
Okay. But this question can be done in O(Log N) as well. If incase test cases are large, you might get stuck and face TLE.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.