You are on page 1of 3

public static int divideAndRule(int[] input1)

{
int r=-1;
int l = input1.length;
if(l%2==0 || l<=0){
return r;
}
for(int i =0; i<l; i++){
int[] q = remove_int(input1, i);
int l1= q.length;
int l2 = l1/2;
int combin=factorial(l1)/(factorial(l2)* factorial(l1-l2));
System.out.println(" "+ combin);
s= new String[combin];
saveCombination(q, l1,l2);
int m= aretwosame(s);
if(m!=1){
r=-1;
return r;
}
else{
r=m;
}

}
return r;
//Write code here
}
public static int getvalue(String x){
int i=0;
x=x+" ";
int l= x.length();String g="";
for(int j=0; j<l; j++){
char c=x.charAt(i);
if(c==' '){
i=i+Integer.parseInt(g);
g="";
}
else{
g=g+c;
}
}
return i;
}
public static int aretwosame(String[] d){
int l= d.length;
int[] e= new int[l];
for(int i=0; i<l; i++){
e[i] = getvalue(d[i]);
}
int f= twointsame(e);
return f;

}
public static int twointsame(int[] d){
int l = d.length;
int c=-1;
for(int i=0; i<l; i++){
for(int j=0; j<l && i!=j; j++){
if(d[i]==d[j]){
c=1;
}
}
}
return c;
}
public static int[] remove_int(int[] a, int p){
int l = a.length;
int k=l-1;
int[] r = new int[k];int j=0;
for(int i=0; i<l;i++){
if(i!=p){
r[j]=a[i];
j++;
}
}
return r;
}
public static int factorial(int n){
int i=1;
while( n>0){
i=i*n;
n--;
}
return i;
}
static String[] s; static int z=0;
static void combinationUtil(int arr[], int data[], int start,
int end, int index, int r)
{

if (index == r)
{
for (int j=0; j<r; j++){
Syste
s[z] = s[z]+" "+data[j];
}

z++;
return;
}

// replacing index with all possible elements. The condition


// "end-i+1 >= r-index" makes sure that including one element
// at index will make a combination with remaining elements
// at remaining positions
for (int i=start; i<=end && end-i+1 >= r-index; i++)
{
data[index] = arr[i];
combinationUtil(arr, data, i+1, end, index+1, r);
}
}

// The main function that saves all combinations of size r


// in arr[] of size n. This function mainly uses combinationUtil()
static void saveCombination(int arr[], int n, int r)
{
// A temporary array to store all combination one by one
int data[]=new int[r];

// save all combination using temprary array 'data[]'


combinationUtil(arr, data, 0, n-1, 0, r);
}

You might also like