Skip to content

Instantly share code, notes, and snippets.

@KeshariPiyush24
Created September 21, 2024 10:06
Show Gist options
  • Save KeshariPiyush24/3aca453ad2f61d2b8d480477a48a6711 to your computer and use it in GitHub Desktop.
Save KeshariPiyush24/3aca453ad2f61d2b8d480477a48a6711 to your computer and use it in GitHub Desktop.
Row with max 1s

Question: Row with max 1s

Intution: Not exactly binary search but we can utilize the fact that it the row is sorted. We have to use 2 pointers approach.

Time Complexity: $$O(n + m)$$

Space Complexity: $$O(1)$$

Solution:

class Solution {
    public int rowWithMax1s(int arr[][]) {
        int n = arr.length;
        int m = arr[0].length;
        int i = 0;
        int j = m - 1;
        int ans = -1;
        while (i < n && j >= 0) {
            if (arr[i][j] == 1) {
                ans = i;
                j--;
            } else {
                i++;
            }
        }
        return ans;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment