0

I do have a problem, i have numbers from 1-49, now the question here is how do i get the maximum number of random sets of six from the given sample. like

int[] a1 = { 1, 2, 3 ,5,6,7 ... 49};

how many unique combination of numbers or arrays can i get from that one big array from 1 to 49 like below

1,2,3,4,5,6
2,1,4,5,8,9
2,1,0,2,4,5
................

What am trying to get is the maximum output or number of possible unique arrays with a length of six i could get . to be honest i have tried writing a loop that reads through the array, but how to capture six random digits is where am stuck i can go any further than

      for(int x=0;<a1.length;x++)
      {
          // here i believe i must turn the captured information 
         // into a muti dimentional array to cpature like '1,2,3,4,5,6' but how. am stuck
       }
Bels
  • 85
  • 8
  • 3
    I might be misunderstanding your question, but isn't it just a combination and binomial coefficient problem? Here is an explanation of a solution: https://en.wikipedia.org/wiki/Combination#Enumerating_k-combinations – RafazZ Aug 31 '16 at 09:47
  • 2
    Also, why don't you try something and show some code that you have tried already, and we will try to point out what you are doing wrong/right? – RafazZ Aug 31 '16 at 09:47
  • you can have a combination of repeated sets but the entire set must not have all numbers similar to the other. for example 1,2,3,4,5,6 23,3,1,4,2,5 – Bels Aug 31 '16 at 09:50

2 Answers2

2

If I understand your question correctly, what you need is the binomial coefficient n! / k! (n - k)!, which in this case would be 49! / (6! * (49 - 6)!) = 13983816. No need to write code if the only thing you want to know is the number of possible combinations.

If you really wanted to list all of them, you'd need a little patience. One way to achieve this is via a recursive approach:

public class NOverK {

    private static final int[] numbers = new int[6];
    private static final int MAX = 49;

    private static void output() {
        System.out.println();
        for (int n : numbers) {
            System.out.print(n + " ");
        }
    }

    private static void allCombinations(int x, int start) {
        if (x > 0) {
            for (int i = start; i <= MAX; i++) {
                numbers[numbers.length - x] = i;
                allCombinations(x - 1, i + 1);
            }
        } else {
            output();
        }
    }

    public static void main(String[] args) {
        allCombinations(6, 1);
    }
}
apophis
  • 374
  • 3
  • 11
1

This question has been asked at Stack Overflow a few times in the past. For example, look at this answer:

Click here: Algorithm to return all combinations of k elements from n

The answers to this question contain numerous solutions of your question in different programming languages (Java, Python, C, C# etc.), too. Check out or adjust a solution that meets your requirements.

You can search for other questions/answers in Stack Overflow (search field in upper right corner) with keywords

[algorithm] [combinations]

A Google search would lead to numerous solutions of your question, too. Try with keywords as follows:

java algorithm combinations without repetition

or

c# algorithm combinations without repetition

Community
  • 1
  • 1
user319785
  • 31
  • 1
  • 4