Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • 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
Programmingoneonone
Programmingoneonone

HackerEarth Maximum bit shifts problem solution

YASH PAL, 31 July 2024
In this HackerEarth Maximum bit shifts, problem-solution you are given a sequence of N numbers. You can perform the following operation:
Select a number from the sequence and swap any pair of bits in its binary representation (Binary representation does not have any leading zeroes).
Output the lexicographic maximum sequence possible after performing 0 or more of the above operations.
HackerEarth Maximum bit shifts problem solution

HackerEarth Maximum bit shifts problem solution.

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

public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(1, in, out);
out.close();
}

static class Task {
PrintWriter out;
InputReader in;

public void solve(int testNumber, InputReader in, PrintWriter out) {
this.out = out;
this.in = in;
int n = ni();
long[] arr = new long[n];
long[] ans = new long[n];
int i = 0;
for (i = 0; i < n; i++) {
arr[i] = nl();
String st = Long.toBinaryString(arr[i]);
int c = 0;
for (int j = 0; j < st.length(); j++)
c += st.charAt(j) - '0';
String mx = "";
for (int j = 0; j < c; j++)
mx += '1';
for (int j = 0; j < st.length() - c; j++)
mx += '0';
ans[i] = Long.parseLong(mx, 2);
}
for (i = 0; i < n; i++)
p(ans[i] + " ");
pn("");
}

int ni() {
return in.nextInt();
}

long nl() {
return in.nextLong();
}

void pn(String zx) {
out.println(zx);
}

void p(Object o) {
out.print(o);
}

}

static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;

public InputReader(InputStream stream) {
this.stream = stream;
}

public int read() {
if (numChars == -1) {
throw new UnknownError();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new UnknownError();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}

public int nextInt() {
return Integer.parseInt(next());
}

public long nextLong() {
return Long.parseLong(next());
}

public String next() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));

return res.toString();
}

private boolean isSpaceChar(int c) {
return c == ' ' || c == 'n' || c == 'r' || c == 't' || c == -1;
}

}
}
coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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