Skip to content

Commit 701f4db

Browse files
take pull of master
1 parent 02491b0 commit 701f4db

26 files changed

+164
-9
lines changed

Untitled-1.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
const int MAX_N = 3e5 + 6;
4+
5+
struct Node {
6+
Node* child[2];
7+
Node() {
8+
this -> child[0] = this -> child[1] = NULL;
9+
}
10+
};
11+
void insert(Node* root, int x) {
12+
Node* cur = root;
13+
for(int bit = 21; bit >= 0; bit--) {
14+
int p = (x & (1 << bit)) ? 1 : 0;
15+
if(!cur -> child[p]) {
16+
cur -> child[p] = new Node();
17+
}
18+
cur = cur -> child[p];
19+
}
20+
}
21+
int xor_max_query(Node* root, int x) {
22+
int ans = 0;
23+
Node* cur = root;
24+
for(int bit = 21; bit >= 0; bit--) {
25+
int p = (x & (1 << bit)) ? 1 : 0;
26+
if(cur -> child[1^p]) {
27+
ans += (1 << bit);
28+
cur = cur -> child[1^p];
29+
} else if(cur -> child[p]) {
30+
cur = cur -> child[p];
31+
} else {
32+
break;
33+
}
34+
}
35+
return ans;
36+
}
37+
38+
// vector<Node*> tree;
39+
Node* tree[4 * MAX_N];
40+
void merge(Node* &root, Node* root1, Node* root2) {
41+
if(!root1 && !root2)
42+
return;
43+
root = new Node();
44+
if(!root1) {
45+
root = root2;
46+
return;
47+
}
48+
if(!root2) {
49+
root = root1;
50+
return;
51+
}
52+
merge(root -> child[0], root1 -> child[0], root2 -> child[0]);
53+
merge(root -> child[1], root1 -> child[1], root2 -> child[1]);
54+
}
55+
void build(int id, int l, int r, vector<int>& a) {
56+
if(l == r) {
57+
tree[id] = new Node();
58+
insert(tree[id], a[l]);
59+
return;
60+
}
61+
int mid = (l + r) / 2;
62+
build(2 * id, l, mid, a);
63+
build(2 * id + 1, mid + 1, r, a);
64+
merge(tree[id], tree[2 * id], tree[2 * id + 1]);
65+
}
66+
int query(int id, int l, int r, int lq, int rq, int x) {
67+
if(lq > r || l > rq)
68+
return 0;
69+
if(lq <= l && r <= rq)
70+
return xor_max_query(tree[id], x);
71+
int mid = (l + r) / 2;
72+
int on_left = query(2 * id, l, mid, lq, rq, x);
73+
int on_right = query(2 * id + 1, mid + 1, r, lq, rq, x);
74+
return max(on_left, on_right);
75+
}
76+
int main() {
77+
ios::sync_with_stdio(false);
78+
cin.tie(0);
79+
int n;
80+
cin >> n;
81+
vector<int> a(n);
82+
for(int i = 0; i < n; i++)
83+
cin >> a[i];
84+
build(1, 0, n - 1, a);
85+
int Q;
86+
cin >> Q;
87+
for(int q = 0; q < Q; q++) {
88+
int l, r, x;
89+
cin >> l >> r >> x;
90+
l -= 1;
91+
r -= 1;
92+
cout << query(1, 0, n - 1, l, r, x) << '\n';
93+
}
94+
return 0;
95+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"Local: increaseDecrease","url":"/Users/sunilkumarmaurya/Desktop/solution-leedcode-codechef-codeforces-gfg-interviewbits-hackerEarth-HackerRank/interviewbits solution/increaseDecrease.cpp","tests":[{"id":1659161446896,"input":"3\n1\n1\n1 4 6","output":""},{"id":1659161460246,"input":"3\n5\n1\n1 4 6","output":""},{"id":1659161463012,"input":"3\n1\n10\n1 4 6","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"/Users/sunilkumarmaurya/Desktop/solution-leedcode-codechef-codeforces-gfg-interviewbits-hackerEarth-HackerRank/interviewbits solution/increaseDecrease.cpp","group":"local","local":true}
41.1 KB
Binary file not shown.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
7+
int n,k,d;
8+
cin >> n>>k>>d;
9+
int arr[n];
10+
for(int i=0;i<n;i++){
11+
cin>>arr[i];
12+
}
13+
sort(arr,arr+n);
14+
int count=0;
15+
int i=0,j=n-1;
16+
while(i<j){
17+
int diff = (arr[j]-arr[i])-d;
18+
if(diff>0){
19+
count = count+ (diff/(2*k));
20+
if(diff%(2*k)){
21+
count++;
22+
}
23+
}
24+
i++;
25+
j--;
26+
}
27+
cout<<count<<"\n";
28+
29+
30+
return 0;
31+
}

question_bank/Array.docx

75.7 KB
Binary file not shown.

question_bank/Bit magic.docx

26.6 KB
Binary file not shown.
32.1 KB
Binary file not shown.
27.1 KB
Binary file not shown.

question_bank/Graph.docx

58.1 KB
Binary file not shown.

question_bank/Greedy.docx

42.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)