java – 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.

Thanks in Advance!!!

class FindIndex {
public static void main (String() args) {
    Scanner scanner = new Scanner(System.in);
    int noOfTestCase = scanner.nextInt();

    while (noOfTestCase-- > 0) {
        int n = scanner.nextInt();
        int() array = new int(n);
        int index = -1;
        for (int i = 0 ; i < n ; i++) {
            array(i) = scanner.nextInt();
            if (array(i) == 1 && index == -1) {
                index = i;
            }
        }
        System.out.println(index);
    }
}

}

https://practice.geeksforgeeks.org/problems/index-of-first-1-in-a-sorted-array-of-0s-and-1s/0