Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8175161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:40:24+00:00 2026-06-06T22:40:24+00:00

The Java interview question is: Without using a temporary buffer, separate the 0’s and

  • 0

The Java interview question is:

Without using a temporary buffer, separate the 0’s and 1’s from an array, placing all 0’s to the left and 1’s to the right. Print the result as a string. For example, given {0,1,1,0,0,1}, the output is “000111”.

And an answer is:

public class ZeroOneSeparator {

    public static void zeroOneSeparator(int[] inputArr){

        // for each index, store number of 1's up to the index
        for (int i = 1; i < inputArr.length; i++) {
            inputArr[i] = inputArr[i-1] + inputArr[i];  
        }

        // This is the "magical math" block I don't understand.
        // Why does this "work"?
        for (int i = inputArr.length - 1; i > 0; i--) {
            if (inputArr[i] > 0) {
                inputArr[i-1] = inputArr[i] - 1;
                inputArr[i] = 1;
            } else {
                inputArr[i-1] = 0;
            }
        }

        for (int i = 0; i < inputArr.length; i++) {
            System.out.print(inputArr[i]);
        }
    }

    public static void main(String[] args) {
        int[] inputArr1 = {1,0,1,0,1,1};
        ZeroOneSeparator.zeroOneSeparator(inputArr1);
        System.out.println();
        int[] inputArr2 = {1,1,1,0,0,0,0,0,0,1};
        ZeroOneSeparator.zeroOneSeparator(inputArr2);
        int[] inputArr3 = {}; // intentionally empty
        System.out.println();
        ZeroOneSeparator.zeroOneSeparator(inputArr3);
        int[] inputArr4 = {0,0,0,0,0,0};
        System.out.println();
        ZeroOneSeparator.zeroOneSeparator(inputArr4);
        int[] inputArr5 = {0,1,0,1,0,1,0,1};
        System.out.println();
        ZeroOneSeparator.zeroOneSeparator(inputArr5);
        int[] inputArr6 = {1,1,1,1,1,1,0,0,0,0,0};
        System.out.println();
        ZeroOneSeparator.zeroOneSeparator(inputArr6);
    }
}

I stepped through this code with a debugger, but I still don’t understand why it works. Can someone please walk me through it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:40:27+00:00Added an answer on June 6, 2026 at 10:40 pm

    Let’s try an example to see what’s going on. Suppose we had the following array:

    0 1 1 0 1 1 0 0
    

    The first loop (as the comment specifies) counts up the total number of 1’s seen so far, where each 1 increases the count, and each 0 leaves it the same. So for our array, we end up with this:

    0 1 2 2 3 4 4 4
    

    Note that the final element of the array is now 4, which is the total number of 1s. We use that fact in the next loop.

    We start at the last element, and check if it’s greater than 0. If it is, then we replace that element with a 1, and then decrement that count by 1 and assign it to the previous element. We keep doing that, filling in 1s as we go, until the count reaches 0. At that point, we set each element we encounter to 0.

    What’s really happening here is that once we know how many 1s there are, we can “count down” from the end of the array, filling in that number of 1s. At that point, we know the rest of the elements must be 0, so we can set the rest of the elements to 0 at that point.

    Visually, it looks like this (with the “current element” surrounded by []’s)

    0 1 2 2 3 4 4 [4] ->
    0 1 2 2 3 4 [3] 1 ->
    0 1 2 2 3 [2] 1 1 ->
    0 1 2 2 [1] 1 1 1 ->
    0 1 2 [0] 1 1 1 1 ->
    0 1 [0] 0 1 1 1 1 ->
    0 [0] 0 0 1 1 1 1 ->
    0 0 0 0 1 1 1 1
    

    Note that the “countdown” seems very obvious when viewed this way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Display numbers from 1 to 100 without loops or conditions Interview question:
Java noob question: Consider the following C array and initializer code: struct { int
this was an interview question posed to me..I vaguely answered it uses Java reflections..but
The Title is self explanatory. This was an interview question. In java, List is
Recently I attended interview in java, the interviewer asked a question like below: I
Recently in a job interview I was asked this following question (for Java): Given:
A java interview question. Is there any way in java programming other then the
This is an interview question (phone screen): write a function (in Java) to find
I recently went for a Java Interview where the interviewer asked me a question
I had a recursion interview question problem in Java,Need your help on this. Write

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.