Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Hackers with Bits problem solution

YASH PAL, 31 July 202413 February 2026
In this HackerEarth Hackers with Bits problem solution Hackers love bits, so does Alex. Alex has just started his career as a hacker and found a special binary array A (containing 0s and 1s).
 
In one operation, he can choose any two positions in the array and can swap their values. He has to perform exactly one operation and maximize the length of the subarray containing only 1s.
 
 
HackerEarth Hackers with Bits problem solution

 

 

HackerEarth Hackers with Bits problem solution.

#include <cstdio>
#include <algorithm>

int a[123];

int n;

int count() {
int ones = 0;
int ret = 0;
for (int i = 0; i < n; i++) {
if (a[i] == 1) {
++ones;
} else ones = 0;
ret = std::max(ret, ones);
}
return ret;
}

int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", a + i);
int ans = count();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
std::swap(a[i], a[j]);
ans = std::max(ans, count());
std::swap(a[i], a[j]);
}
}
printf("%dn", ans);
}
 
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes