Tuesday, February 7, 2017

Java Program to Generate the all Non repeated sequence of the Array values


package p1;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/**
 *
 * @author sujith
 */
class ticketGenerator {

    static void ticketGenerator(int arr[], int n, int r, int index,
                                int data[], int i,ArrayList ar)
    {
       
        if (index == r)
        {
            int arr1[]= new int[r];
            String st="";
            for (int j=0; j<r; j++){
                arr1[j]=data[j];
                st=st+data[j]+" ";
            }
            pHelper(arr1,0,ar);
            ar.add(st);
        return;
        }
        if (i >= n)
            return;
        data[index] = arr[i];
        ticketGenerator(arr, n, r, index+1, data, i+1,ar);
        ticketGenerator(arr, n, r, index, data, i+1,ar);
    }

    static void printTicket(int arr[], int n, int r,ArrayList ar)
    {
        int data[]=new int[r];
        ticketGenerator(arr, n, r, 0, data, 0,ar);
    }
   
   
private static void pHelper(int[] arr, int index,ArrayList mylist){
    if(index >= arr.length - 1){
        String st="";
        for(int i = 0; i < arr.length - 1; i++){
            st=st+arr[i]+" ";
        }
        if(arr.length > 0) {
            st=st+arr[arr.length - 1];
        }
        mylist.add(st);
        return;
    }

    for(int i = index; i < arr.length; i++){
        int t = arr[index];
        arr[index] = arr[i];
        arr[i] = t;
        pHelper(arr, index+1,mylist);
        t = arr[index];
        arr[index] = arr[i];
        arr[i] = t;
    }
}
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        ArrayList<String> ar= new ArrayList<>();
        System.out.println("Enter the number of Tickets");
        int n=sc.nextInt();
        int arr[]= new int[n];
        System.out.println("Enter the Tickets");
        for (int i = 0; i < n; i++) {
            arr[i]=sc.nextInt();
        }
        System.out.println("Enter the  generation sequence limit");
        int r =sc.nextInt();
        printTicket(arr, n, r,ar);
        Collections.shuffle(ar);
        System.out.println("The generated Keys");
        for (String temp : ar) {
            System.out.println(temp);
        }
    }
}