2) BarCode Challenge
Concept In UPC system, The first 610 digits of a 12-digit barcode identify the
company that manufactures. The remaining digits, except for the very last one, are
invented by that company to describe each of its products. When a laser scans for a bar
code, it's actually scanning a series of 95 evenly spaced columns and checking the
amount of light reflected. Columns reflecting more light are considered as one and the
one's reflecting less light are zero.
A new called knights designs a new system for interpreting bar codes(with 95 lines). In
this system, the first bar corresponds to a 1, second to 2 and so on...
Therefore, if the first 5 lines of the code are read by the computer as
0
1
1
0
1
it is translated to 2
3
5
i.e 2,3,5
Task - Obtain n by adding all the digits in the decimal number obtained in knights design
(eg. 00101 becomes 24 becomes 2+4 i.e. 6).
Input
Number of test cases.
A barcode in binary form.
Output Line 1 : Print the number of even n's.
Line 2 : Print the number of odd n's
Note - Print 0 for both even and odd count in case there is no input i.e all
zeros in barcode.
Code #include <iostream>
using namespace std;
int main() {
int arr[95];
int arrno[95];
int i,j;
int even = 0,odd = 0;
for(int i = 0; i < 95; i++){
cin >> arr[i];
}
for(i = 0, j = 0; i < 95; i++){
if(arr[i] == 1){
arrno[j] = i;
j++;
}
}
int size = j;
for(int i = 0; i < size; i++){
if(arrno[i] / 10 == 0){
if(arrno[i] % 2 == 0)
even++;
else
odd++;
}
else
if(((arrno[i]/10) + (arrno[i]%10)) % 2 == 0)
even++;
else
odd++;
}
cout<<even<<endl;
cout<<odd;
return 0;
}
Test Cases 1. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
00000000000000000000000000000000000000
0000000000000000000
2. 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
11111111111111111111111111111111111111
1111111111111111111
3. 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
01010101010101010101010101010101010101
0101010101010101010
4. 1 0 1 0 0 0 1 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 0 0 1 0
10101000101010100010101010001010101000
1010101000101010100
5. 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0
11001100110011001100110011001100110011
0011001100110011001
6. 1 0 1 0 1 1 0 0 0 0 1 0 1 0 1 1 1 1 0 1 0 0 1 0 0 0 0 1 0 1 0 0 0 0 1 1 1 0
00001111101010101010100000101101110100
1110101101100111001
Output 1.
0
0
2.
47
48
3.
22
25
4.
20
18
5.
23
24
6.
21
26