3

I am working on a project involving "Dynamic Programming" and am struck on this trivial thing, please help.

Suppose I take 4 as an input, I want to display something like: 0000 to 1111

But, if I input 5, I want to display like: 00000 to 11111 and so on.

Thanks in advance,

EDIT: Please don't post asking me for the code. This is not a homework problem and I don't need any code, just tell me the logic for it and I would be happy.

EDIT2: WTH is happening with Stackoverflow, did I ask any of you to write code for me? I want the person who downvoted to upvote it. What is a point of this forum if I can't for help?

Share the logic with me. We can discuss and I do not require the code for this.

EDIT3: Here I am posting the code which I tried. I hope this "SATISFIES" all the people who were thinking I have not tried anything.

import java.util.ArrayList;

public class RegularInvestigator {

public ArrayList createCombinations(ArrayList listOfFlightNumbers) {

ArrayList<String> result = new ArrayList<String>();

for(int i = 1; i < listOfFlightNumbers.size(); i++) {

  String binaryEqvivalent = Integer.toBinaryString(i);System.out.println(binaryEqvivalent);
  String element = "";

  for(int j = 0; j < binaryEqvivalent.length(); j++)
    if(binaryEqvivalent.charAt(j) == '1')
      element += listOfFlightNumbers + " ";

  result.add(element.substring(0, element.length() - 1));
}

return result;

}

private String getContent(ArrayList<String> flight) {
String temp = "";

for(int i = 0; i < flight.size() - 1; i++)  temp += flight.get(i) + " ";

temp += flight.get(flight.size() - 1);

return temp;

}

private ArrayList removeElementAtIndex(ArrayList flight, int position) {

ArrayList<String> res = new ArrayList<String>();

for(int i = 0; i < flight.size(); i++) {
  if(i != position) res.add(flight.get(i));
}

return res;

} }

EDIT4: Thank you phoxis, PengOne, Jerry Coffin and oliholz for your valuable answers :)

10
  • do you need to display all combinations from 0000 to 1111 or jsut 0000 and 1111 Commented Jun 24, 2011 at 4:29
  • 3
    share the code as well to check it Commented Jun 24, 2011 at 4:29
  • @harigm, all combinations from 0000 to 1111 for input as 4 Commented Jun 24, 2011 at 4:30
  • I am working on a problem which involves this logic, so I cant share the entire code with you. I am working on a Dynamic Programming problem and it involves listing all valid combinations of a string, so if I can know the logic of this question, I can apply this logic in my project and can see the performance of it Commented Jun 24, 2011 at 4:32
  • 1
    @Shankar: "What is a point of this forum if I can't for help" this is not a forum actually, its a Q&A site. :) Commented Jun 24, 2011 at 4:37

6 Answers 6

8
  • Get input n
  • Count from i=0 to (2^n) - 1
  • for each value of i bitmask each bit of i and display.
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, but I need the number of bits in the binary stream to be the value of n
and by following the above you will get that. just mask and print upto n bits starting from LSB
using the & operator ? mask = 0x01 and i=the count , and shift mask left on each iteration and do mask & i if result is true then print 1 else print 0
@Shankar: did you delete your comment "how to bitmask in java?"
yeah I actually figured it out :)
8
public void outBinary(int value){
   for (int i = 0; i < Math.pow(2, value); i++) {
       System.out.println(Integer.toBinaryString(i));
   }
}

with leading zeros something like that

    for (int i = 0; i < Math.pow(2, value); i++) {
        StringBuilder binary = new StringBuilder(Integer.toBinaryString(i));
        for(int j = binary.length(); j < value; j++) {
            binary.insert( 0, '0' );
        }
        System.out.println(binary);
    }

2 Comments

Would the size of the Integer.toBinaryString(i) be of "value" bits?
+1 for succinctness. This will print 0 for 0 and not 0000. @asker should add logic to pad zeros.
4

Either use phoxis's very nice solution, or just iterate them lexicographically (this is really the same solution!): Given a binary string of a given length, get the next lexicographic string by finding the rightmost zero entry, change it to a 1, and change everything to the right of it back to a 0, e.g.

0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

Comments

1

I'm a bit lost as to how you'd apply dynamic programming to this. It's just a matter of counting from 0 to one less than the specified maximum value (where the maximum value is 1 shifted left the specified number of bits).

Edit: I should add that there are other possibilities (e.g., gray codes) but absent some reason to do otherwise, simple binary counting is probably the simplest to implement.

3 Comments

the asker says "I am working on a problem which involves this logic", note "involves"
@phoxis The question has the dynamic-programming tag. If dynamic programming is irrelevant to the question, adding the tag and mentioning it just confuses matters.
the question tags are invalid, i agree, and is definitely confusing.
0
int x = 5;

for(int i = 0; i < (1 << x); i++){ 
 System.out.println(Integer.toBinaryString(i)); 
}

1 Comment

You should include an explanation of your answer.
0

here is the code is to find the combination

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package rotateimage;

/**
 *
 * @author ANGEL
 */
public class BinaryPermutaion {

    public static void main(String[] args) {
        //object creation
        BinaryPermutaion binaryDigit=new BinaryPermutaion();
        //Recursive call of the function to print the binary string combinations
        binaryDigit.printBinary("", 4);
    }

    /**
     * 
     * @param soFar String to be printed
     * @param iterations number of combinations
     */
    public void printBinary(String soFar, int iterations) {
    if(iterations == 0) {
        System.out.println(soFar);
    }
    else {
        printBinary(soFar + "0", iterations - 1);
        printBinary(soFar + "1", iterations - 1);
    }
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.