-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThePromotion.java
More file actions
105 lines (93 loc) · 2.41 KB
/
ThePromotion.java
File metadata and controls
105 lines (93 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.util.Scanner;
import java.lang.Math;
public class ThePromotion
{
public static int solution(String s,int n)
{
String numbers[]=s.split(" ");
Integer[] values=new Integer[n];
for(int i=0;i<n;i++)
{
values[i]=Integer.parseInt(numbers[i]);
}
String[] groupSet=new String[n*(n-1)/2];
for(int i=0,k=0;i<n-1;i++)
{
for (int j = i + 1; j < n; j++)
{
groupSet[k]=values[i]+" "+values[j];
k++;
}
}
int sum=0;
for(int i=0;i<groupSet.length;i++)
{
sum+=and(groupSet[i]);
}
return sum%101;
}
static int factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
public static Integer[] decimalToBinary(int n)
{
Integer[] binRev=new Integer[8];
for(int i=0;i<8;i++)
binRev[i]=0;
Integer[] bin=new Integer[8];
for(int i=0;i<8;i++)
bin[i]=0;
int i=0;
while(n > 0)
{
int a=n%2;
binRev[i]=a;
i++;
n = n / 2;
}
for(i=0;i<8;i++)
bin[i]=binRev[7-i];
return bin;
}
public static double binaryToDecimal(Integer[] a)
{
double decimal=0;
for(int i=7;i>=0;i--)
{
decimal+=a[i]*Math.pow(2,7-i);
}
return decimal;
}
public static int and(String s)
{
String[] n= s.split(" ");
Integer[] a=decimalToBinary(Integer.parseInt(n[0]));
Integer[] b=decimalToBinary(Integer.parseInt(n[1]));
Integer[] and=new Integer[8];
for(int i=0;i<8;i++)
{
if(a[i]==1&&b[i]==1)
{
and[i]=1;
}
else and[i]=0;
}
return (int)binaryToDecimal(and);
}
public static void main(String args[])
{
System.out.println("Enter Input: ");
Scanner s=new Scanner(System.in);
String[] input = new String[2];
for (int i = 0; i < 2; i++)
{
input[i] = s.nextLine();
}
int count=Integer.parseInt(input[0]);
System.out.println( solution(input[1],count));
}
}