Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- class GFG
- {
- static int outcomes;
- static void printArray(int p[], int n, int teamSize)
- {
- int max = Integer.MIN_VALUE;
- int maxCount = 0;
- boolean badVal = false;
- if (n <= teamSize && !(p[0] == n - 1 && n == teamSize))
- {
- for (int i = 0; i < n; i++)
- {
- System.out.print(p[i]+" ");
- if (p[i] == teamSize - 1)
- {
- maxCount++;
- }
- else if (p[i] >= teamSize)
- {
- badVal = true;
- }
- max = Math.max(Integer.MIN_VALUE, p[i]);
- }
- if (badVal == false && n <= teamSize && maxCount <= 1)
- {
- outcomes++;
- System.out.println("<- Added outcome");
- }
- else
- {
- System.out.println();
- }
- }
- }
- // Function to generate all unique partitions of an integer
- static void printAllUniqueParts(int n, int teamSize)
- {
- n = n * (n - 1) / 2;
- int[] p = new int[n]; // An array to store a partition
- int k = 0; // Index of last element in a partition
- p[k] = n; // Initialize first partition as number itself
- // This loop first prints current partition, then generates next
- // partition. The loop stops when the current partition has all 1s
- while (true)
- {
- // print current partition
- printArray(p, k+1, teamSize);
- // Generate next partition
- // Find the rightmost non-one value in p[]. Also, update the
- // rem_val so that we know how much value can be accommodated
- int rem_val = 0;
- while (k >= 0 && p[k] == 1)
- {
- rem_val += p[k];
- k--;
- }
- // if k < 0, all the values are 1 so there are no more partitions
- if (k < 0) return;
- // Decrease the p[k] found above and adjust the rem_val
- p[k]--;
- rem_val++;
- // If rem_val is more, then the sorted order is violeted. Divide
- // rem_val in differnt values of size p[k] and copy these values at
- // different positions after p[k]
- while (rem_val > p[k])
- {
- p[k+1] = p[k];
- rem_val = rem_val - p[k];
- k++;
- }
- // Copy rem_val to next position and increment position
- p[k+1] = rem_val;
- k++;
- }
- }
- public static void main (String[] args)
- {
- outcomes = 0;
- printAllUniqueParts(5, 5);
- System.out.println("Number of unique outcomes:" + outcomes);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement