Wipro NTH Coding Sample Question- 3
Wipro NTH 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 –
C
C++
JAVA
Python
C
Run
#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); }
C++
Run
#include <iostream> #include <cmath> using namespace std; int main() { int r, c, a, m = 2147483647, ans = -1; cin >> r >> c; for (int i = 0; i < r; i++) { int sum = 0; for (int j = 0; j < c; j++) { std::cin >> a; sum += a; } if (m > sum) { m = sum; ans = i + 1; } } cout << ans << std::endl; return 0; }
JAVA
Run
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int r = scanner.nextInt(); int c = scanner.nextInt(); int a, m = Integer.MAX_VALUE, ans = -1; for (int i = 0; i < r; i++) { int sum = 0; for (int j = 0; j < c; j++) { a = scanner.nextInt(); sum += a; } if (m > sum) { m = sum; ans = i + 1; } } System.out.println(ans); } }
Python
Run
r, c = map(int, input().split()) m = float('inf') ans = -1 for i in range(r): row_sum = sum(map(int, input().split())) if row_sum < m: m = row_sum ans = i + 1 print(ans)
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others