-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA_Array_Elimination.cpp
More file actions
146 lines (132 loc) · 3.23 KB
/
Copy pathA_Array_Elimination.cpp
File metadata and controls
146 lines (132 loc) · 3.23 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <bits/stdc++.h>
#include <algorithm>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#define nl '\n'
#define sp ' '
#define pi 2 * acos(0.0)
// Types of declarations /////////////////////////////////
#define ui unsigned int
#define us unsigned short
#define ull unsigned long long
#define ll long long
#define ld long double
#define vstr vector<string>
#define vll vector<ll>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
// Odd Even /////////////////////////////////////////////
bool odd(ll num) { return ((num & 1) == 1); }
bool even(ll num) { return ((num & 1) == 0); }
//////////////////////////////////////////////////////// Prime
bool isPrime(int n)
{
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
return true;
}
///////////////////////////////////////////////////////// LCM GCD
long long gcd(long long a, long long b)
{
while (b != 0)
{
long long temp = b;
b = a % b;
a = temp;
}
return a;
}
long long lcm(long long a, long long b)
{
return (a / gcd(a, b)) * b;
}
////////////////////////////////////////////////////////// SQR ROOT
long long sqrt(long long x)
{
long long s = 0, e = 2e9, res = s;
while (s <= e)
{
long long m = (s + e) / 2;
if (m * m <= x)
res = m, s = m + 1;
else
e = m - 1;
}
return res;
}
/*
check all edge cases
check for integer overflow
check for corner cases
check for constraints
check for time complexity
check for array bounds
check for negative values
*/
/*----------------------------------------------------------------------------*/
void solve()
{
// Explanation: Read the size of the array
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
// Explanation: Count how many numbers have each bit (0-30) set
vector<int> bitCounts(31, 0);
for (int i = 0; i < n; i++)
{
for (int bit = 0; bit < 31; bit++)
{
if (a[i] & (1 << bit))
{
bitCounts[bit]++;
}
}
}
// Dry run example:
// Suppose n=4 and a=[4, 4, 4, 4]:
// - Each 4 in binary is 100 (bit 2 set)
// - bitCounts[2] would become 4 (since all have bit 2 set)
// - For k=1: 4 % 1 == 0 (ok). For k=2: 4 % 2 == 0 (ok). For k=3: 4 % 3 != 0 (not ok). For k=4: 4 % 4 == 0 (ok).
// - So valid answers would be k=1,2,4
// Explanation: For each possible k from 1 to n,
// the condition for all elements to become zero is that
// every bitCount must be divisible by k
for (int k = 1; k <= n; k++)
{
bool possible = true;
for (int bit = 0; bit < 31; bit++)
{
if (bitCounts[bit] % k != 0)
{
possible = false;
break;
}
}
if (possible)
{
cout << k << " ";
}
}
cout << "\n";
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
cin >> t;
while (t--)
solve();
}