This is the Angry Delta Editorial Codechef.
One day, Delta, the dog, got very angry. He has N
items with different values, and he decided to destroy a few of them. However, Delta loves his hooman as well. So he only destroyed those items whose Least Significant Bit in binary representation is 0. Can you help Delta to find the total damage he did so he could make an equally sorry face?
Table of Contents
Input:
- First line will contain T
, number of testcases. Then the testcases follow. First line of Each test case a single integer N. Next line contains N
- integers denoting values of items.
Output:
For each testcase, output in a single line the total damage caused by Delta.
Constraints
- 1≤T≤103
1≤N≤103 1≤value≤103
Sample Input:
1
5
1 2 3 4 5
Sample Output:
6
EXPLANATION:
Total Damage: 2+4=6.
Prerequisite
Checking Least Significant Bit
Explanation:
This is the straight forward, You need to find the Numbers whose last bit is not set and then simply add them.
Code
import java.io.*;
import java.util.*;
class GeeksToCode {
public static int check(int n)
{
if( (n&1) == 0)
return 0;
else
return 1;
}
public static void main (String[] args) {
int t;
Scanner s = new Scanner(System.in);
t = s.nextInt();
while(t-->0)
{
int size;
size = s.nextInt();
List<Integer> list = new ArrayList<>();
while(size --> 0)
list.add(s.nextInt());
ListIterator<Integer> it = list.listIterator();
int ans = 0;
while(it.hasNext())
{
int z= it.next();
if(GeeksToCode.check(z) == 0)
ans +=z;
}
System.out.println(ans);
}
}
}
We use scanner class to take input and then stored the number in array list and called the check() for checking the last bit.
This is the Angry Delta Editorial CodeChef.
You can view the submission here