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 6075379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:29:17+00:00 2026-05-23T10:29:17+00:00

I’ve just been looking at the following piece of code package test; import java.util.ArrayList;

  • 0

I’ve just been looking at the following piece of code

package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

    public static void main(final String[] args) {

        final int sizeA = 3;
        final int sizeB = 5;

        final List<int[]> combos = getAllCombinations(sizeA-1, sizeB);

        int counter = 1;
        for(final int[] combo : combos) {
            System.out.println("Combination " + counter);
            System.out.println("--------------");
            for(final int value : combo) {
                System.out.print(value + " ");
            }
            System.out.println();
            System.out.println();
            ++counter;
        }

    }

    private static List<int[]> getAllCombinations(final int maxIndex, final int size) {

        if(maxIndex >= size)
            throw new IllegalArgumentException("The maximum index must be smaller than the array size.");

        final List<int[]> result = new ArrayList<int[]>();

        if(maxIndex == 0) {
            final int[] array = new int[size];
            Arrays.fill(array, maxIndex);
            result.add(array);
            return result;
        }

        //We'll create one array for every time the maxIndex can occur while allowing
        //every other index to appear, then create every variation on that array
        //by having every possible head generated recursively
        for(int i = 1; i < size - maxIndex + 1; ++i) {

            //Generating every possible head for the array
            final List<int[]> heads = getAllCombinations(maxIndex - 1, size - i);

            //Combining every head with the tail
            for(final int[] head : heads) {
                final int[] array = new int[size];
                System.arraycopy(head, 0, array, 0, head.length);
                //Filling the tail of the array with i maxIndex values
                for(int j = 1; j <= i; ++j)
                    array[size - j] = maxIndex;
                result.add(array);
            }

        }

        return result;

    }

}

I’m wondering, how do I eliminate recursion from this, so that it returns a single random combination, rather than a list of all possible combinations?

Thanks

  • 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-05-23T10:29:17+00:00Added an answer on May 23, 2026 at 10:29 am

    If I understand your code correctly your task is as follows: give a random combination of numbers '0' .. 'sizeA-1' of length sizeB where

    1. the combination is sorted
    2. each number occurs at least once

    i.e. in your example e.g. [0,0,1,2,2].

    If you want to have a single combination only I’d suggest another algorithm (pseudo-code):

    • Randomly choose the step-up positions (e.g. for sequence [0,0,1,1,2] it would be steps (1->2) & (3->4)) – we need sizeA-1 steps randomly chosen at sizeB-1 positions.
    • Calculate your target combination out of this vector

    A quick-and-dirty implementation in java looks like follows

    // Generate list 0,1,2,...,sizeB-2 of possible step-positions 
    List<Integer> steps = new ArrayList<Integer>();
    for (int h = 0; h < sizeB-1; h++) {
        steps.add(h);
    }
    
    // Randomly choose sizeA-1 elements
    Collections.shuffle(steps);
    steps = steps.subList(0, sizeA - 1);
    Collections.sort(steps);
    
    // Build result array
    int[] result = new int[sizeB];
    for (int h = 0, o = 0; h < sizeB; h++) {
        result[h] = o;
        if (o < steps.size() && steps.get(o) == h) {
            o++;
        }
    }
    

    Note: this can be optimized further – the first step generates a random permutation and later strips this down to desired size. Therefore it is just for demonstration purpose that the algorithm itself works as desired.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
For some reason, after submitting a string like this Jack’s Spindle from a text
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.