Wipro NLTH Coding Sample Question- 3

PrepSter Wipro NLTH Coding Question – 3

Ques:  Given an n-by-n matrix of 0’s and 1’s where all 1’s in each row come before all 0’s, find the most efficient way to return the row with the maximum number of 0’s.

{1,1,1,1},
{1,1,0,0},
{1,0,0,0},
{1,1,0,0},

NOTE:- Please comment down the code in other languages as well below –

#include <stdio.h>
#include<math.h>
int main()
{
int r,c,a,m=2147483647,ans=-1;
scanf("%d %d",&r,&c);
for(int i=0;i<r;i++)
{
int sum=0;
for(int j=0;j<c;j++)
{
scanf("%d",&a);
sum+=a;
}
if(m>sum)
{
m=sum;
ans=i+1;
}
}
printf("%d",ans);
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
int r,c,a,m=INT_MAX,ans=-1;
cin>>r>>c;
for(int i=0;i<r;i++)
{
int sum=0;
for(int j=0;j<c;j++) {cin>>a; sum+=a;}
if(m>sum)
{
m=sum;
ans=i+1;
}
}
cout<<ans;
}
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int row=sc.nextInt();
int col=sc.nextInt();
int a,ans=-1;

int sum=0,max=Integer.MAX_VALUE;
for(int i=0;i<row;i++)
{
sum=0;
for(int j=0;j<col;j++)
{
a=sc.nextInt();
sum=sum+a;
}
if(max>sum)
{
max=sum;
ans=i+1;
}
}
System.out.println(ans);
}
}
Code is not available yet, comment down the code below the page in the comment
section using the same logic or different approach you want to.