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

  • Home
  • SEARCH
  • 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 8043839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:02:38+00:00 2026-06-05T05:02:38+00:00

With n=5 and k=3 the following loop will do it List<String> l=new ArrayList<String>(); l.add(A);l.add(B);l.add(C);l.add(D);l.add(E);

  • 0

With n=5 and k=3 the following loop will do it

List<String> l=new ArrayList<String>();
l.add("A");l.add("B");l.add("C");l.add("D");l.add("E");
int broadcastSize = (int) Math.pow(2, l.size());
for (int i = 1; i < broadcastSize; i++) {
    StringBuffer buffer = new StringBuffer(50);
    int mask = i;
    int j = 0;   
    int size=0;
    System.out.println();
    while (mask > 0) {
        if ((mask & 1) == 1) {
            System.out.println(".. "+mask);
            buffer.append(l.get(j));
            if (++size>3){
                buffer = new StringBuffer(50);
                break;
            }
        }
        System.out.println(" "+mask);
        mask >>= 1;
        j++;
    }
    if (buffer.length()>0)
        System.out.println(buffer.toString());

}

but it’s not efficient I would like to do it with Banker’s sequence and thus explore first singletons, then pairs, then 3-tuple and stop.

I did not find a way do that, but at least this loop should be more efficient:

List<String> l=new ArrayList<String>();
l.add("A");l.add("B");l.add("C");l.add("D");l.add("E");
int broadcastSize = (int) Math.pow(2, l.size());
for (int i = 1; i < broadcastSize; i++) {
    StringBuffer buffer = new StringBuffer(50);
    int mask = i;
    int j = 0;   

    if (StringUtils.countMatches(Integer.toBinaryString(i), "1") < 4){
        while (mask > 0) {
            if ((mask & 1) == 1) {
                buffer.append(l.get(j));

            }
            mask >>= 1;
            j++;
        }
        if (buffer.length()>0)
            System.out.println(buffer.toString());
    }


}

there is also: but k embedded loops looks ugly

//singleton
for (int i = 0; i < l.size(); i++) {
    System.out.println(l.get(i));
}

//pairs
for (int i = 0; i < l.size(); i++) {
    for (int j = i+1; j < l.size(); j++) {
        System.out.println(l.get(i)+l.get(j));
    }
}

//3-tuple
for (int i = 0; i < l.size(); i++) {
    for (int j = i+1; j < l.size(); j++) {
        for (int k = j+1; k < l.size(); k++) {
            System.out.println(l.get(i)+l.get(j)+l.get(k));
        }
    }
}
//...
// k-tuple
  • 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-05T05:02:39+00:00Added an answer on June 5, 2026 at 5:02 am

    this should be the most efficient way, even if k embedded loops looks ugly

    //singleton
    for (int i = 0; i < l.size(); i++) {
        System.out.println(l.get(i));
    }
    
    //pairs
    for (int i = 0; i < l.size(); i++) {
        for (int j = i+1; j < l.size(); j++) {
            System.out.println(l.get(i)+l.get(j));
        }
    }
    
    //3-tuple
    for (int i = 0; i < l.size(); i++) {
        for (int j = i+1; j < l.size(); j++) {
            for (int k = j+1; k < l.size(); k++) {
                System.out.println(l.get(i)+l.get(j)+l.get(k));
            }
        }
    }
    // ...
    //k-tuple
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code Ruby code will iterate the source string and produce a list
I have a known list of strings like the following: List<string> groupNames = new
I have the following loop in my viewDidLoad: for(int i=1; i<[eventsArray count]; i++) {
I am using the following loop to add items to an an array. I
I have the following code class Program { static void Main(string[] args) { List<A>
i have a class like class CXmlData { String Data1=; String Data2=; List<String> lst=new
Say I have the following loop: i = 0 l = [0, 1, 2,
I have the following loop inside an function init(); which is executed onload, I
I have the following loop... for ($i = 1; $i <= 10; $i++) {
I am using the following loop to send/attach multiple attachments to my email message.

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.