Take a Boolean 2D array of 10X 10, where each row is sorted from the user, Find the row with the
maximum number of 1s.
#include<iostream>
using namespace std;
int row,col;
int index(bool a[],int left,int right){
if(left<=right){
int mid=(left+right)/2;
if((a[mid-1]==0||mid==0)&& a[mid]==1){
return mid;
}else{
if(a[mid]==0){
return index(a,mid+1,right);
}else{
return (a,left,mid-1);
}
}
return -1;
}
int Row_With_Max_is(bool a[row][col]){
int max_1=0;
int max_row_index=0;
for(int i=0;i<row;i++){
int idx=index(a[i],0,col-1);
if(idx==-1)continue;
int count_1=col-idx;
if(count_1>max_1){
max_1=count_1;
max_row_index=i;
}
}
}
Comments
Leave a comment