Find first index of 1 Given a sorted array consisting 0’s and 1’s. The task is to find the index of first ‘1’ in the given array. I submitted the below code in geeks for geeks and the execution time is 5.77. Need help in optimizing the below code.
class FindIndex {
    public static void main (String[] args) {
        Scanner scanner = new Scanner(System.in);
        int noOfTestCase = scanner.nextInt();

        while (noOfTestCase-- &gt; 0) {
            int n = scanner.nextInt();
            int[] array = new int[n];
            int index = -1;
            for (int i = 0 ; i &lt; n ; i++) {
                array[i] = scanner.nextInt();
                if (array[i] == 1 &amp;&amp; index == -1) {
                    index = i;
                }
            }
            System.out.println(index);
        }
    }
}

Source
Trying to outsmart the challenge defeats the purpose
While there is value in learning how to do the bare minimum needed (as you try to detect the number as soon as it is inputted instead of performing a search), there is only so much you can learn by trying to outsmart the challenge and not practice the knowledge this challenge is supposed to help you practice (which in this case is binary search).
Input reading
You can read the input faster using BufferedReader, see this answer's &quot;Reading Data&quot; section for example. You can read a whole line at once and then perform a search.
Binary search
Binary search is a very fast way to find an item in a sorted array where by looking at any element you know if what you're looking for is on the left or right of the current position.
You can perform the search on the string, converting the string to an array of numbers is unnecessary and therefore a waste of time.