-
Notifications
You must be signed in to change notification settings - Fork 0
/
intresting array.txt
76 lines (45 loc) · 1.47 KB
/
intresting array.txt
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
Problem Description
You have an array A with N elements. We have 2 types of operation available on this array :
We can split a element B into 2 elements C and D such that B = C + D.
We can merge 2 elements P and Q as one element R such that R = P^Q i.e XOR of P and Q.
You have to determine whether it is possible to make array A containing only 1 element i.e. 0 after several splits and/or merge?
Problem Constraints
1 <= N <= 100000
1 <= A[i] <= 106
Input Format
The first argument is an integer array A of size N.
Output Format
Return "Yes" if it is possible otherwise return "No".
Example Input
Input 1:
A = [9, 17]
Input 2:
A = [1]
Example Output
Output 1:
Yes
Output 2:
No
Example Explanation
Explanation 1:
Following is one possible sequence of operations -
1) Merge i.e 9 XOR 17 = 24
2) Split 24 into two parts each of size 12
3) Merge i.e 12 XOR 12 = 0
As there is only 1 element i.e 0. So it is possible.
Explanation 2:
There is no possible way to make it 0.
..........................Algorithm..........................................
if we have an even number then we can divide it into two equall parts(a^a=0) if we have odd numbers even time then their xor are even again
we split into two eqall parts(a^a=0);
Time complexity:O(n)
space Complexity:O(1)
string Solution::solve(vector<int> &A) {
int n=A.size();
int even=0,odd=0;
for(int i=0;i<n;i++){
if(A[i]&1)
odd ++;
}
return (odd&1)?"No":"Yes";
}