Skip to content

Commit b634bac

Browse files
committed
🚀 20-Aug-2020
1 parent d0cd247 commit b634bac

File tree

5 files changed

+206
-0
lines changed

5 files changed

+206
-0
lines changed

algorithms/sorting/quick_select.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Input: arr[] = {7, 10, 4, 3, 20, 15}
3+
k = 3
4+
Output: 7
5+
6+
Input: arr[] = {7, 10, 4, 3, 20, 15}
7+
k = 4
8+
Output: 10
9+
*/
10+
11+
12+
13+
14+
15+
16+
17+
#include<bits/stdc++.h>
18+
using namespace std;
19+
20+
int partion(int a[], int low, int high){
21+
int pivot=a[high];
22+
int pIndex=low;
23+
for(int i=low; i< high; i++){
24+
if(a[i] <= pivot){
25+
swap(a[i], a[pIndex]);
26+
pIndex++;
27+
}
28+
}
29+
swap(a[pIndex], a[high]);
30+
return pIndex;
31+
}
32+
33+
int kthSmallest(int a[], int low, int high, int k){
34+
// If k is smaller than number of elements in array
35+
if(k > 0 && k <= high - low + 1){
36+
int pIndex = partion(a, low, high);
37+
if(pIndex-low == k-1) return a[pIndex];
38+
else if(pIndex-low > k-1) kthSmallest(a, low, pIndex-1, k);
39+
else kthSmallest(a, pIndex+1, high, k-pIndex+low-1);
40+
}
41+
// If k is more than number of elements in array
42+
else return INT_MAX;
43+
}
44+
45+
int main(){
46+
int n;
47+
cin>>n;
48+
int a[n];
49+
for(int i=0;i<n;i++) cin>>a[i];
50+
int k;
51+
cin>>k;
52+
53+
cout<<kthSmallest(a, 0, n-1, k); // <array , low, high>
54+
}

algorithms/sorting/quick_sort.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Example 1:
3+
4+
Input: N = 5, arr[] = { 4, 1, 3, 9, 7}
5+
Output: 1 3 4 7 9
6+
Example 2:
7+
8+
Input: N = 10,
9+
arr[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
10+
Output: 1 2 3 4 5 6 7 8 9 10
11+
*/
12+
13+
14+
15+
16+
17+
18+
#include<bits/stdc++.h>
19+
using namespace std;
20+
21+
int partion(int a[], int low, int high){
22+
int pivot=a[high];
23+
int pIndex=low;
24+
for(int i=low; i< high; i++){
25+
if(a[i] <= pivot){
26+
swap(a[i], a[pIndex]);
27+
pIndex++;
28+
}
29+
}
30+
swap(a[pIndex], a[high]);
31+
return pIndex;
32+
}
33+
34+
void quickSort(int a[], int low, int high){
35+
if(low < high){
36+
int pIndex = partion(a, low, high);
37+
quickSort(a, low, pIndex-1);
38+
quickSort(a, pIndex, high);
39+
}
40+
}
41+
42+
int main(){
43+
int n;
44+
cin>>n;
45+
int a[n];
46+
for(int i=0;i<n;i++) cin>>a[i];
47+
48+
quickSort(a, 0, n-1); // <array , low, high>
49+
50+
for(int i=0;i<n;i++) cout<<a[i]<<" ";
51+
}

array/trapping_rain_water.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,31 @@ int main(){
3838

3939
return 0;
4040
}
41+
42+
43+
44+
45+
46+
47+
// TC O(N) SC: O(1)
48+
/*
49+
int trap(vector<int>& height) {
50+
int trapped=0;
51+
int n=height.size();
52+
int l=0, r=n-1;
53+
int maxLeft=0, maxRight=0;
54+
55+
while(l<r){
56+
if(height[l]>height[r]){
57+
if(height[r]>maxRight) maxRight=height[r];
58+
else trapped+=maxRight-height[r];
59+
r--;
60+
} else {
61+
if(height[l]>maxLeft) maxLeft=height[l];
62+
else trapped+=maxLeft-height[l];
63+
l++;
64+
}
65+
}
66+
return trapped;
67+
}
68+
*/

competitive programming/leetcode/29. Divide Two Integers.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,42 @@ class Solution {
3939
return dividend/divisor;
4040
}
4141
};
42+
43+
44+
45+
46+
47+
class Solution {
48+
public:
49+
int divide(int dividend, int divisor) {
50+
long long n=dividend, m=divisor;
51+
if(n <= INT_MIN && m == -1) return INT_MAX;
52+
53+
int sign = (n < 0) ^ (m < 0) ? -1: 1;
54+
55+
n=abs(n);
56+
m=abs(m);
57+
58+
long long q=0, temp=0;
59+
60+
for(int i=31; i>=0; i--){
61+
if(temp + (m << i) <= n){
62+
temp += m << i;
63+
q += 1 << i; // q |= 1 << i;
64+
}
65+
}
66+
return sign * q;
67+
}
68+
};
69+
70+
71+
/*
72+
dividend = quotient * divisor + remainder
73+
dividend - quotient * divisor >= 0
74+
So here we find highest value of quotient
75+
We start with highest value of i ie: (divisor * 2^31) .... (divisor * 2^30)..................(divisor * 2^0)
76+
here (m << i) is equivalent to m * 2^i
77+
we keep adding (1 << i) or in other words 2^i to quotient
78+
*/
79+
80+

competitive programming/leetcode/42. Trapping Rain Water.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,37 @@ class Solution {
4040
return waterTrap;
4141
}
4242
};
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
class Solution {
56+
public:
57+
int trap(vector<int>& height) {
58+
int trapped=0;
59+
int n=height.size();
60+
int l=0, r=n-1;
61+
int maxLeft=0, maxRight=0;
62+
63+
while(l<r){
64+
if(height[l]>height[r]){
65+
if(height[r]>maxRight) maxRight=height[r];
66+
else trapped+=maxRight-height[r];
67+
r--;
68+
} else {
69+
if(height[l]>maxLeft) maxLeft=height[l];
70+
else trapped+=maxLeft-height[l];
71+
l++;
72+
}
73+
}
74+
return trapped;
75+
}
76+
};

0 commit comments

Comments
 (0)