-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontest10.cpp
More file actions
47 lines (44 loc) · 1007 Bytes
/
Copy pathcontest10.cpp
File metadata and controls
47 lines (44 loc) · 1007 Bytes
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
// 100031. Sum of Values at Indices With K Set Bits
//tc O(nlogn)
//sc O(1)
//code
class Solution {
public:
int countBits(int n){
int count=0;
while(n>0){
count+=n%2;
n/=2;
}
return count;
}
int sumIndicesWithKSetBits(vector<int>& nums, int k) {
int sum =0;
for(int i = 0; i < nums.size(); i++){
if(countBits(i)==k){
sum+=nums[i];
}
}
return sum;
}
};
// 100040. Happy Students
//tc O(n)
//sc O(1)
//code (not accepted)
class Solution {
public:
int countWays(vector<int>& nums) {
int n = nums.size();
int minValue = 0;
int maxValue = n;
for(int i = 0; i<n; i++){
minValue = max(minValue, nums[i]+1);
maxValue = min(maxValue, nums[i]-1);
if( minValue>maxValue) {
return 0;
}
}
return maxValue - minValue + 1;
}
};