Given a boolean 2D array of n x m dimensions where each row is sorted. Find the 0-based index of the first row that has the maximum number of 1's.
Below is the code I have written
int rowWithMax1s(int arr[][], int n, int m) {
int ans = -1;
int row = 0;
int col = m-1;
while(row<n && col>=0){
if(arr[row][col] == 1){
col--;
ans = row;
}
else{
row++;
}
}
return ans;
}
I believe time complexity for the above is O(M+N). yet its giving me time limit exceeded error.
This is the link for the question https://practice.geeksforgeeks.org/problems/row-with-max-1s0023/1#
Please help me what else I can do to optimize my code.