Skip to content

Commit 39135a0

Browse files
committed
Power of Two
1 parent c3b958d commit 39135a0

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
#include<iostream>
2-
#include<queue>
32
#include<vector>
3+
#include<queue>
44
using namespace std;
5-
int findKthLargest(vector<int>& nums, int k){
5+
int findkthLargestElement(vector<int>& nums, int k){
66
priority_queue<int, vector<int>, greater<int>> minheap;
77
for(int num : nums){
88
minheap.push(num);
99
if(minheap.size() > k){
1010
minheap.pop();
1111
}
1212
}
13-
return minheap.top();
13+
minheap.top();
1414
}
1515
int main(){
16-
vector<int> nums = {3,2,1,5,6,4};
17-
int k = 2;
18-
int result = findKthLargest(nums, k);
19-
cout << "The Kth largets element is " << result << endl;
16+
int n;
17+
int k;
18+
cout << "Enter the value of N: " << endl;
19+
cin >> n;
20+
vector<int> nums(n);
21+
cout << "Enter the elements of the array: " << endl;
22+
for(int i = 0; i < n; i++){
23+
cin >> nums[i];
24+
}
25+
cout << "Enter the value of K: " << endl;
26+
cin >> k;
27+
int result = findkthLargestElement(nums, k);
28+
cout << "The " << k << "th Largest element in the array is " << result << endl;
2029
return 0;
2130
}
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1-
bool isPowerOfTwo(int n) {
1+
#include<iostream>
2+
using namespace std;
3+
bool isPowerofTwo(int n){
24
if(n == 1){
35
return true;
46
}
5-
if(n<=0 || n%2!=0){
7+
if( n <= 0 || n % 2 != 0){
68
return false;
79
}
8-
return isPowerOfTwo(n/2);
10+
return isPowerofTwo(n/2);
11+
}
12+
int main(){
13+
int n;
14+
cout << "Enter the value of n: " << endl;
15+
cin >> n;
16+
if(isPowerofTwo(n)){
17+
cout << "True" << endl;
18+
}
19+
else{
20+
cout << "False" << endl;
21+
}
22+
return 0;
923
}

0 commit comments

Comments
 (0)