We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I don't know how it works but i observed the following pattern:
1, 2, 4, 8 = 120
1, 2, 4, 8, 6(4+2) = 240
1, 2, 4, 10(8+2), 8 = 240
1, 2, 4, 10, 6, 8, 3 = 960 = 120 * BigInteger.Pow(2, arr.count - 4(1 2 4 8 )
//you should use the above examples to solve it rather than look at the code :)
//it took me a lot of time and i changed my code several times because i didn't use BigInteger and i was right from the beginning.
public static int xoringNinja(List arr) //C#
{
List<int> list = new List<int>();
int max = 0;
int i = 1;
foreach(int x in arr)
max = (max | x);
if(max == 0)
return 0;
while(max > i)
{
i = i << 1;
if(i == (1 << 30))
break;
}
for(i = i; i != 0; i = i >> 1)
{
if((max - i) >= 0)
{
list.Add(i);
max -= i;
}
}
max = 0;
foreach(int x in list)
max += x;
BigInteger b = BigInteger.Multiply(max, BigInteger.Pow(2, list.Count-1));
if((arr.Count - list.Count) < 1) //0
{
for(i = list.Count - arr.Count;i != 0; i--)
b = b/2;
}
else
{
b = BigInteger.Multiply(b, BigInteger.Pow(2, arr.Count - list.Count));
b = b % 1000000007;
}
return (int)b;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Xoring Ninja
You are viewing a single comment's thread. Return to all comments →
I don't know how it works but i observed the following pattern: 1, 2, 4, 8 = 120 1, 2, 4, 8, 6(4+2) = 240 1, 2, 4, 10(8+2), 8 = 240 1, 2, 4, 10, 6, 8, 3 = 960 = 120 * BigInteger.Pow(2, arr.count - 4(1 2 4 8 ) //you should use the above examples to solve it rather than look at the code :) //it took me a lot of time and i changed my code several times because i didn't use BigInteger and i was right from the beginning. public static int xoringNinja(List arr) //C# {