Row with max 1s | Practice | GeeksforGeeks

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.
Example 1:
Input: 
N = 4 , M = 4
Arr[][] = {{0, 1, 1, 1},
           {0, 0, 1, 1},
           {1, 1, 1, 1},
           {0, 0, 0, 0}}
Output: 2
Explanation: Row 2 contains 4 1's (0-based
indexing).


Example 2:
Input: 
N = 2, M = 2
Arr[][] = {{0, 0}, {1, 1}}
Output: 1
Explanation: Row 1 contains 2 1's (0-based
indexing).


Your Task:  
You don't need to read input or print anything. Your task is to complete the function rowWithMax1s() which takes the array of booleans arr[][], n and m as input parameters and returns the 0-based index of the first row that has the most number of 1s. If no such row exists, return -1.
 
Expected Time Complexity: O(N+M)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N, M ≤ 103
0 ≤ Arr[i][j] ≤ 1 
 
+1
krunalpabari2012in 9 hours
Time Complexity O(N).
Space Complexity O(1).
 
class Solution {   int rowWithMax1s(int arr[][], int n, int m) {           int row=0;           int counter=0;           int idx=-1;           int max=0;           for(int i=m-1;i>=0 && row<n;i--)           {               if(arr[row][i]==1) {                   counter++;                   idx=row;               }               else{                   row++;                   i++;               }                              }           return idx;   }}
0
aman hamid20 hours ago
USING BINARY SEARCH
 
int rowWithMax1s(vector<vector<int> > arr, int n, int m) {    // code here    int i;    int ans = -1;    int max = INT_MIN;    for(i=0;i<n;i++) {        int start=0,end=m-1,index=-1,count=0;        while(start<=end) {            int mid = (start+end)/2;            if(arr[i][mid]==1) {                index = mid;                end = mid-1;            } else {                start = mid+1;            }        }        if(index!=-1) {            count = (m-index);            if(count>max) {                max = count;                ans = i;            }        }    }    return ans;}
0
priyansh7089021 hours ago
Time Complexity O(n*m)
Token → 0.21/1.74
Simple Solution
 
     int max = 0;    int count = 0;    int ans = -1;    for(int i =0;i<n;i++)    {        count  = 0;        for(int j=0;j<m;j++)        {            if(arr[i][j] == 1)            {                count++;            }        }        if(count>max)        {            max = count;            ans = i;        }    }    return ans;;} 
 
0
riyaraip084 days ago
C++ soln
Time takes = 0.22s  
int rowWithMax1s(vector<vector<int> > arr, int n, int m) {    // code here    for(int i=0;i<m;i++)    {        for(int j=0;j<n;j++)        {            if(arr[j][i]==1){                return j;            }        }    }    return -1;}
+1
mannukhurana103975 days ago
Java
int rowWithMax1s(int arr[][], int n, int m) {
        // space : O(1)
        // time: O(n*m) || 0.74/2.2
        
        int max = 0;
        int index=-1;
        
        for(int i=0;i<n;i++){
            int sum=0;
            for(int ii: arr[i])
                sum+=ii;
            if(sum>max){
                max = sum;
                index=i;
            }
            
        }
        
        
        // -------------------------------------------------------------------
        
        // space : O(1)
        // time: O(n+m) || 1.25/2.2
        
        int index=-1;
        int x=0;
        
        for(int i=m-1;i>=0;i--){
            if(arr[x][i]==1){
                index=x;
            }
            else{
                if(x<n-1){
                i++;x++;}
            }
        }
        
        return index;
    }
0
aniket6518gadhe6 days ago
JAVA SOLUTION
Total Time Taken:0.82/2.2
 
int rowWithMax1s(int arr[][], int n, int m) {       boolean isEmpty= true;       for(int i=0;i<n;i++){           if(arr[i][m-1]==1){               isEmpty=false;               break;}       }       if(isEmpty==false){      for(int i=0;i<m;i++){           for(int j=0;j<n;j++){               if(arr[j][i]==1)                   return j;           }      }}      return -1;   }
0
raghavendra7776 days ago
Python 0.54 S
a=0 p=-1 g=0 l=[] for i  in range(0,len(arr)):     k=arr[i].count(1)     g=i     if k>a:         a=k         p=g return p
+1
avinav26116 days ago
Easy C++ Solution ( 0.2 / 1.74 sec)
 
+1
kshitij14101 week ago
 int rowWithMax1s(vector<vector<int> > arr, int n, int m) {   int ans=-1;   int r=0;   int top=m-1;      while(top>=0 and r<n)   {       if(arr[r][top]==1)       {           ans=r;           top--;       }       else       r++;   }      return ans;}
+1
vcreateforme91 week ago
class Solution{public:int rowWithMax1s(vector<vector<int> > arr, int n, int m) {    // code here    for(int i=0;i<m;i++)    {        for(int j=0;j<n;j++)        {            if(arr[j][i]==1)            return j;        }    }    return -1;}
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.