Skip to content

Commit f36b523

Browse files
committed
2 parents b440110 + 8b2bc41 commit f36b523

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2691
-223
lines changed

.github/pull_request_template.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ Happy Contributing!
1919

2020
- [ ] I've read the contribution guidelines.
2121
- [ ] I've checked the issue list before deciding what to submit.
22-
- [ ] I've edited the `README.md` and link to my code.
2322

2423
## Related Issues or Pull Requests
2524

.vscode/settings.json

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**************************************************************************
2+
Given an array arr[] and a number K where K is smaller than size of array,
3+
the task is to find the Kth smallest element in the given array.
4+
Given all Array elements are distinct.
5+
6+
For Example: arr[] = {7, 8, 1, 4, 9, 3}
7+
k = 3 ------> 3rd smallest element in the array
8+
ANS: 4
9+
10+
This can be solved using heap and priority queue.
11+
***************************************************************************/
12+
13+
// SOLUTION (in C++):
14+
15+
#include <bits/stdc++.h>
16+
17+
using namespace std;
18+
19+
int main()
20+
{
21+
int n, k;
22+
cin >> n >> k;
23+
cout << "Enter the array elements" << endl;
24+
int arr[n];
25+
priority_queue<int> maxh;
26+
//A priority queue keeps the elements in the order of their priority, i.e.,
27+
//elements having greater values will be at the top of the queue and elements having smaller values will be kept at the bottom of the queue.
28+
//The element popped out of this queue is the element with the maximum value.
29+
30+
for (int i = 0; i < n; i++)
31+
{
32+
maxh.push(arr[i]);
33+
if (maxh.size() > k)
34+
{
35+
maxh.pop();
36+
}
37+
}
38+
39+
cout << "The " << k << "th smallest element is " << maxh.top();
40+
return 0;
41+
}

Arrays/Max_XOR_of_Two_Array.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
Given an integer array nums, return the maximum result of nums[i] XOR nums[j],
3+
where 0 ≤ i ≤ j < n.
4+
*/
5+
6+
#include<iostream>
7+
#include<vector>
8+
using namespace std;
9+
10+
// Constructing a trie
11+
struct trie {
12+
trie *zero,*one;
13+
};
14+
15+
//Building the trie
16+
void insert(int num,trie *root) {
17+
// Store the head
18+
trie *ptr=root;
19+
for(int i=31;i>=0;i--) {
20+
// Find the i-th bit
21+
int h = (num>>i & 1);
22+
if(h==0) {
23+
// If ptr->zero is NULL
24+
if(ptr->zero==NULL) {
25+
ptr->zero=new trie();
26+
}
27+
// Update ptr to ptr->zero
28+
ptr=ptr->zero;
29+
}
30+
else {
31+
// If ptr->onr is NULL
32+
if(ptr->one==NULL) {
33+
ptr->one=new trie();
34+
}
35+
// Update ptr to ptr->one
36+
ptr=ptr->one;
37+
}
38+
}
39+
}
40+
41+
//Finding the maximum XOR for each element
42+
int comp(int num,trie *root) {
43+
trie *ptr = root;
44+
int sum=0;
45+
for(int i=31;i>=0;i--) {
46+
sum=sum<<1;
47+
// Finding ith bit
48+
int h = (num>>i & 1);
49+
// Check if the bit is 0
50+
if(h==0) {
51+
// If right node exists
52+
if(ptr->one) {
53+
sum++;
54+
ptr=ptr->one;
55+
}
56+
else ptr=ptr->zero;
57+
58+
}
59+
else {
60+
// Check if left node exists
61+
if(ptr->zero) {
62+
sum++;
63+
ptr=ptr->zero;
64+
}
65+
else ptr=ptr->one;
66+
}
67+
}
68+
return sum;
69+
}
70+
71+
int findMaximumXOR(vector<int>& nums) {
72+
// head Node of Trie
73+
trie *root = new trie();
74+
// Insert each element in trie
75+
for(int i=0;i<nums.size();i++) {
76+
insert(nums[i],root);
77+
}
78+
// Stores the maximum XOR value
79+
int maxm=0;
80+
// Traverse the given array
81+
for(int i=0;i<nums.size();i++) {
82+
maxm=max(comp(nums[i],root),maxm);
83+
}
84+
return maxm;
85+
}
86+
87+
//Main Function
88+
int main() {
89+
90+
vector<int>nums;
91+
int sz;
92+
93+
cout<<"Enter the vector size\n";
94+
cin>>sz; // size of the vector
95+
96+
cout<<"Enter the elements in the vector\n";
97+
for(int i=0;i<sz;i++) {
98+
int x;
99+
cin>>x;
100+
nums.push_back(x);
101+
}
102+
103+
int answer = findMaximumXOR(nums);
104+
cout<<"The Maximum XOR of two numbers in the array/vector is : "<<answer<<endl;
105+
106+
return 0;
107+
}
108+
109+
/* Sample Text Case
110+
Enter the vector size
111+
6
112+
Enter the elements in the vector
113+
3 10 5 25 2 8
114+
The Maximum XOR of two numbers in the array/vector is : 28
115+
116+
Time Complexity : O(n) , where n is the number of elements in the vector.
117+
*/

Arrays/Minimum_XOR_Value.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Given an integer array A of N integers, find the pair of integers
3+
in the array which have minimum XOR value. Report the minimum XOR value.
4+
Explanation: According to the problem, we are given the size of the array
5+
and the numbers present in it. So we have to find two such numbers that are
6+
the elements of the array and their xor is the minimum in value in comparison
7+
to all the possible pairs of the array.
8+
*/
9+
10+
#include<bits/stdc++.h>
11+
using namespace std;
12+
13+
void Min_xor(int*A,int n)
14+
{
15+
//ele1 and ele2 will store the value of the elements that gives minimum xor value
16+
int ele1,ele2;
17+
//ans will store the minimum xor value
18+
int ans=INT_MAX;
19+
for(int i=0;i<n-1;i++)
20+
{
21+
//x will store xor value for each possible pair of elements of array
22+
int x=A[i]^A[i+1];
23+
if(x<ans)
24+
{
25+
ans=x;
26+
ele1=A[i];
27+
ele2=A[i+1];
28+
}
29+
}
30+
//printing the output
31+
cout<<"The Minimum xor value is : "<<ans<<endl;
32+
cout<<"The corresponding elements are : "<<ele1<<" and "<<ele2;
33+
}
34+
35+
int main() {
36+
//n will store the size of the array
37+
int n;
38+
cout<<"Enter the size of array : ";
39+
cin>>n;
40+
cout<<"Enter the array : ";
41+
int A[n];
42+
for(int i=0;i<n;i++)
43+
cin>>A[i];
44+
//sort() will sort the array in increasing order
45+
sort(A,A+n);
46+
//calling the function
47+
Min_xor(A,n);
48+
return 0;
49+
}
50+
51+
/*
52+
INPUT:
53+
Enter the size of array : 4
54+
Enter the array : 0 2 5 7
55+
OUTPUT:
56+
The Minimum xor value is : 2
57+
The corresponding elements are : 0 and 2
58+
59+
Time Complexity : O(nlogn)
60+
Space Complexity : O(1)
61+
*/
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Trapping Rain Water
2+
//Approach 1
3+
// O(n^2) O(1)
4+
5+
//i/p: 3 0 1 2 5
6+
//o/p: 6
7+
8+
//Brute force approach
9+
10+
#include<bits/stdc++.h>
11+
using namespace std;
12+
int traprain(int arr[],int n)
13+
{ // To store the maximum water
14+
// that can be stored
15+
int res = 0;
16+
17+
// i/p: 3 0 1 2 5
18+
//o/p: 6
19+
20+
// For every element of the array
21+
for (int i = 1; i < n-1; i++) {
22+
23+
// Find the maximum element on its left from o th to i th value
24+
int left = arr[i];
25+
for (int j=0; j<i; j++) //o(n^2)
26+
left = max(left, arr[j]);
27+
28+
// Find the maximum element on its right from j th th value to end
29+
int right = arr[i];
30+
for (int j=i+1; j<n; j++)
31+
right = max(right, arr[j]);
32+
33+
// Update the maximum water
34+
res = res + (min(left, right) - arr[i]); // 3 + 2 + 1
35+
}
36+
37+
return res;
38+
}
39+
int main()
40+
{
41+
int n;
42+
cout<<"Enter the number of elements in the array\n";
43+
cin>>n;
44+
int arr[n];
45+
cout<<"Enter the elements in the array\n";
46+
for(int i=0;i<n;i++)
47+
cin>>arr[i];
48+
cout<<"Total trapped water is "<<traprain(arr,n);
49+
return 0;
50+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Trapping Rain Water
2+
//Approach 2
3+
// O(n) O(2n)
4+
5+
//i/p: 3 0 1 2 5
6+
//o/p: 6
7+
8+
//Use 2 extra arrays
9+
10+
#include<bits/stdc++.h>
11+
using namespace std;
12+
int getwater(int arr[],int n)
13+
{
14+
int res=0;
15+
int lmax[n],rmax[n];
16+
17+
//left max
18+
lmax[0]=arr[0];
19+
for(int i=1;i<n;i++)
20+
lmax[i]=max(arr[i],lmax[i-1]);
21+
22+
//starts from start to end
23+
//1st element as it is other max will place in lmax
24+
//Array is 5 0 6 2 3
25+
// |/\/\/\/\
26+
//lmax is 5 5 6 6 6
27+
28+
//right max
29+
rmax[n-1]=arr[n-1];
30+
for(int i=n-2;i>=0;i--)
31+
rmax[i]=max(arr[0],rmax[i+1]);
32+
33+
//starts from end to start
34+
//last element as it is other max will place in rmax
35+
//Array is 5 0 6 2 3
36+
// /\/\/\/\|
37+
//rmax is 6 6 6 3 3
38+
39+
//here we get result
40+
for(int i=1;i<n-1;i++)
41+
res+=(min(lmax[i],rmax[i])-arr[i]);
42+
43+
// min(lmax,rmax)-arr[i] min arr[i]
44+
// 5 5 6 6 6 5 5 6 3 3 - 5 0 6 2 3 = 0+5+0+1+0=6
45+
// 6 6 6 3 3
46+
47+
48+
return res;
49+
}
50+
int main()
51+
{
52+
int n;
53+
cout<<"Enter the number of elements in the array\n";
54+
cin>>n;
55+
int arr[n];
56+
cout<<"Enter the elements in the array\n";
57+
for(int i=0;i<n;i++)
58+
cin>>arr[i];
59+
cout<<"Total trapped water is "<<getwater(arr,n);
60+
return 0;
61+
}

0 commit comments

Comments
 (0)