Skip to content

Commit

Permalink
Contest-10, Day 152
Browse files Browse the repository at this point in the history
  • Loading branch information
shivamkasaudhan committed Sep 17, 2023
1 parent 3b48de7 commit bdea152
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions contest10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;

}
};

0 comments on commit bdea152

Please sign in to comment.