/*
Online Java - IDE, Code Editor, Compiler

Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/

// This has been created to ensure I can utilize any random functions more efficiently.
// It is a creation of the NcR    combinations calculator.
// It has used techniques I learnt including recursion and also memoization to speed up execution.
// I will incorporate this into Java applications I created previously..

import java.math.*;
import java.util.*;


class largestNumber
{
    long combinations;
    int count;
    String temp;
    
    Set <String> s = new HashSet <>();   // this will store the  combinations 
    
    List <Integer> lst = new ArrayList<>(Arrays.asList(10,7,76,415));  // this is list of numbers. In future, it will be chosen to use other data
    
    List <Integer> copy = new ArrayList<>(lst);  //keeps a copy    // this list keeps a copy since during program execution the top list is modified
    
    
    public largestNumber(long combinations)
    {
        
        this.combinations=combinations;
        int randomNumber;    // random  number generated
        int numberArray;     // value at index of randomNumber in the set
        
        Random rand = new Random();  // generates random number
        
        System.out.println("Combinations: " + combinations +"\n");   //number combinations without replacement
       
        
        do
        {
            
            lst = new ArrayList<>(copy);     // each time it starts again, it has to get the original ArrayList back
            
            temp="";   // this is used concatenation of value before it is stored in the set....
            
            do
            {
                System.out.println("Size of list: " + lst.size());         //size list
                
                randomNumber = rand.nextInt(lst.size());                  //random number between  0 - (size list-1)
                System.out.println("random number: " + (randomNumber));
                
                numberArray = lst.get(randomNumber);   //gets number from the list
                System.out.println("This is number in list: " + numberArray);    //corresponding value in list
                
                temp =  temp + Integer.toString(numberArray);    // concatenating the values
                
                lst.remove(randomNumber);     // this is important step.. it removes value at this index from list... enforce combination without replacment
                
                //System.out.println("value of i: " + i + "\n");
                
            } while (!lst.isEmpty());            // it will keep processing while this is true....   while list is not empty
            
            
            s.add(temp);    // adding the value to the set.
            System.out.println("set size: " + s.size() +"\n");   //once this has incremented, the set has grown...
            
           
        count++;   // this is indication of a cycle.. It is aiming to be close to combinations...
        
            
        } while (s.size()<combinations);    // this is 1 less due to 0 indexing of the set. 
        
        
        System.out.println("Number cycles: " + count);
        
        System.out.println("Original list: " + copy.toString());  //original list outputted to the screen
        System.out.println("highest is: " + checkMaximum());     // function call to check for maximum
        
    }
    
    public int checkMaximum()
    {
        int highest=0;
        
        for (String m: s)    // checks each string in the set
        {
            if (Integer.valueOf(m)>highest)   // greater than initial 0
            {
                highest=Integer.valueOf(m);      // it will store value
            }
            
        }
        
        return highest;
    }
}


public class Combination
{
    public static void main(String[] args) {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        int originalNumber=4;
        int n=originalNumber;
        int r =4;
        Map <Integer, Long> m = new HashMap<>();
        System.out.println("***COMBINATIONS***  (WITHOUT REPLACEMENT)");
        System.out.println("P(" + n+","+r+") = " + n+"!" + " / " + "("+r+"!"+"("+n+"-"+r+")!)");
        
        largestNumber ln = new largestNumber(Combinations (n,r,originalNumber, m));
        
    }
    
    public static long Combinations (int n,  int r, int originalNumber, Map factorialResults)
    {
        
        // n are objects
        // r is sample
       
       /*
       ***CALCULATION***
        P(n,r) =   n! /  (r!(n−r)!)
        
        */
        
        long result=0;
        int denominator1;
        int denominator2;
        
        if (n>=1)
        {
            // EXAMPLE
            
            // P (5,6)   =   6* 5* 4 * 3 * 2 * 1   /   6! (6-5)!     =    720 / (5! * 1!) =  120 / 5*4*3*2*1  * 1  = 720 / 120 = 6
        
        result = (n* (Combinations (n-1, r,originalNumber, factorialResults)));   // this completes factorial for numerator
        
        factorialResults.put(n,result);   //result stored in the Map
        //System.out.println("getting result back out: " + factorialResults.get(n));
        
        
        if (n==originalNumber)   // this will occur once
        {
            
            denominator1 = originalNumber-r;        // originalNumber required since n has reduced as part of the recursive calls
           
            denominator2 = r;                       // r sample size has not changed
           
            
        // this is using the Java Memoization technique to ensure the factorial outcome is not calculated again, to save program cycles.
        // since the returns are done in reverse order.... n = 1 is processed first and n=6 last... Hence in practice
        // there will be entry in Map for all factorials, ready for the denominator..
        
        
            if (factorialResults.containsKey(denominator1) && factorialResults.containsKey(denominator2))
            
            {
                //System.out.println("here");
                //System.out.println("This is exact value of factorial  " + (denominator1) + " : " +  factorialResults.get(denominator1));
                //System.out.println("This is exact value of factorial  " + (denominator2) + " : " +  factorialResults.get(denominator2));
               
                return result / ((long) factorialResults.get(denominator1) * (long)factorialResults.get(denominator2));
            }
           
        }
           
            return result;
            
        }
          return 1;    // it should not reach here  
        }
        
    }