Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Binary Matrix problem solution

YASH PAL, 31 July 2024
In this HackerEarth Binary Matrix problem solution N numbers were converted into their respective Binary form of M length and arranged in an NxM matrix.
Your task is to find the index of row (1 based indexing) which contains the binary number with maximum value. If multiple rows have maximum value then print the row with minimum index.
HackerEarth Binary Matrix problem solution

HackerEarth Binary Matrix problem solution.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define M 1000000007
#define N 1000016
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
#define f first
#define s second
#define inf 9000000000000000000
char a[1010][1010];

int main()
{
freopen("inp0.txt","rt",stdin);
freopen("out0.txt","wt",stdout);
ll n,i,j,m;
cin>>n>>m;
unordered_set<ll> v,v2;
for(i=1; i<=n; i++)
{
v.insert(i);
for(j=1; j<=m; j++)
cin>>a[i][j];
}

for(j=1; j<=m; j++)
{
for(auto ind: v)
{
if(a[ind][j]=='1')
v2.insert(ind);
}
if(!v2.empty())
v = v2;
v2.clear();
}
ll mini = inf;
for(auto ind: v)
mini = min(mini, ind);

cout<<mini<<"n";



return 0;
}

Second solution

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;

public class Main {
public static void main(String[] args) {
new Thread(null, new Runnable() {
public void run() {
new Main().solve();
}
}, "1", 1 << 26).start();
}

void solve() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
ScanReader in = new ScanReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
BinaRYMATRIX solver = new BinaRYMATRIX();
solver.solve(1, in, out);
out.close();
}

static class BinaRYMATRIX {
public void solve(int testNumber, ScanReader in, PrintWriter out) {
int n = in.scanInt();
int m = in.scanInt();
String[] s = new String[n];
for (int i = 0; i < n; i++) s[i] = in.scanString();
String max = "";
int index = -1;
for (int i = 0; i < n; i++) {
if (max.compareTo(s[i]) < 0) {
max = s[i];
index = i;
}
}
out.println(index + 1);
}

}

static class ScanReader {
private byte[] buf = new byte[4 * 1024];
private int index;
private BufferedInputStream in;
private int total;

public ScanReader(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}

private int scan() {
if (index >= total) {
index = 0;
try {
total = in.read(buf);
} catch (Exception e) {
e.printStackTrace();
}
if (total <= 0) return -1;
}
return buf[index++];
}

public int scanInt() {
int integer = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}

public String scanString() {
int c = scan();
if (c == -1) return null;
while (isWhiteSpace(c)) c = scan();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = scan();
} while (!isWhiteSpace(c));
return res.toString();
}

private boolean isWhiteSpace(int n) {
if (n == ' ' || n == 'n' || n == 'r' || n == 't' || n == -1) return true;
else return false;
}

}
}
coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes