Skip to content

Commit 58adeab

Browse files
Mohit JainMohit Jain
Mohit Jain
authored and
Mohit Jain
committed
mid-update
1 parent aa2b3bd commit 58adeab

Some content is hidden

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

57 files changed

+1530
-8
lines changed

.DS_Store

6 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.

CountSort

39.5 KB
Binary file not shown.

CountSort.cpp

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
4+
using namespace std;
5+
6+
7+
void countSort(int *a, int n,int *result) {
8+
9+
10+
int mx = *max_element(a,a+n);
11+
12+
int *count = new int[mx];
13+
14+
memset(count, 0, mx);
15+
16+
for(int i = 0; i < n ; i ++) {
17+
count[a[i]]++;
18+
}
19+
int res=0,lop=0;
20+
21+
while(lop<= mx) {
22+
if(count[lop]> 0) {
23+
// cout<<count[a[lop]]<<"dd"<<a[lop];
24+
result[res] = lop;
25+
res++;
26+
count[lop]--;
27+
28+
} else {
29+
lop++;
30+
}
31+
32+
}
33+
34+
35+
}
36+
37+
38+
39+
40+
int main() {
41+
42+
43+
#ifndef ONLINE_JUDGE
44+
// for getting input from input.txt
45+
freopen("input.txt", "r", stdin);
46+
// for writing output to output.txt
47+
freopen("output.txt", "w", stdout);
48+
#endif
49+
50+
int n;
51+
52+
cin>>n;
53+
54+
int *a = new int[n];
55+
56+
57+
for(int i=0; i < n ; i ++) {
58+
59+
cin>>a[i];
60+
61+
}
62+
63+
int *result = new int[n];
64+
65+
countSort(a, n, result);
66+
67+
for(int i=0; i < n ; i ++) {
68+
69+
cout<<result[i]<<" ";
70+
71+
}
72+
73+
74+
return 0;
75+
}

DecimaalToBinary.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include<iostream>
2+
3+
using namespace std;
4+
5+
6+
int decimalToBinary(int n) {
7+
8+
int ans =0 ;
9+
10+
int p =1;
11+
12+
while(n > 0) {
13+
int last_bit = (n & 1);
14+
15+
ans+ = p* last_bit;
16+
17+
p = p*10;
18+
19+
n = n >>1;
20+
}
21+
22+
return ans;
23+
24+
25+
26+
27+
}

LinearSort012

34.8 KB
Binary file not shown.

LinearSort012.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
4+
using namespace std;
5+
6+
7+
8+
void sortLinear(int *a, int n) {
9+
10+
int low = 0;
11+
12+
int mid = 0;
13+
14+
int high = n-1;
15+
16+
17+
while ( mid <= high ) {
18+
19+
if(a[mid] ==0) {
20+
swap(a[low],a[mid]);
21+
low++;mid++;
22+
} else if (a[mid] == 1) {
23+
24+
mid++;
25+
} else {
26+
27+
swap(a[mid],a[high]);
28+
high--;
29+
}
30+
}
31+
32+
33+
}
34+
35+
36+
int main() {
37+
38+
#ifndef ONLINE_JUDGE
39+
// for getting input from input.txt
40+
freopen("input.txt", "r", stdin);
41+
// for writing output to output.txt
42+
freopen("output.txt", "w", stdout);
43+
#endif
44+
45+
int n;
46+
47+
cin>>n;
48+
49+
int *a = new int[n];
50+
51+
for (int i =0; i< n ; i ++) {
52+
53+
cin>>a[i];
54+
}
55+
56+
sortLinear(a,n);
57+
for (int j =0; j < n; j ++) {
58+
59+
cout<<a[j]<<endl;
60+
}
61+
62+
return 0;
63+
64+
}

MaxXor

51 KB
Binary file not shown.

MaxXor.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// {5 6} 0110, 0101 tries // for fun // bit
5+
//manipulation for fun
6+
// { 5^6, 5^5, 6^6} = 3
7+
int max_xor(int arr[] , int n) {
8+
9+
int mask = 0;
10+
11+
int ans = 0;
12+
13+
int i,j;
14+
15+
for(i = 31; i>=0; i--) {
16+
17+
mask = mask | (1<<i); // eg 0010
18+
set<int> s;
19+
20+
for(j =0; j<n; j++)
21+
{
22+
s.insert((arr[j] & mask));
23+
24+
// either 100000 or 00000
25+
// 0000 0100 0010
26+
}
27+
int candidate = ans | (1<<i); // 0100 | 0010
28+
// 0110
29+
set<int> ::iterator it;
30+
for(it = s.begin(); it!=s.end();it++){
31+
if(s.find(*it^candidate) != s.end() ){
32+
// 0100
33+
ans = candidate; // 0110
34+
break;
35+
}
36+
}
37+
}
38+
39+
return ans;
40+
41+
}
42+
43+
int main() {
44+
45+
#ifndef ONLINE_JUDGE
46+
// for getting input from input.txt
47+
freopen("input.txt", "r", stdin);
48+
// for writing output to output.txt
49+
freopen("output.txt", "w", stdout);
50+
#endif
51+
52+
int x,y;
53+
54+
cin>>x>>y;
55+
56+
57+
58+
int n = (y-x) + 1;
59+
int *a = new int[n];
60+
int j =0;
61+
for(int i=x; i <= y; i++) {
62+
63+
a[j++] = i;
64+
}
65+
66+
cout<<max_xor(a,n)<<endl;
67+
68+
return 0;
69+
}

Prime_Construction

42.8 KB
Binary file not shown.

Prime_Construction.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
using namespace std;
5+
6+
7+
int main() {
8+
#ifndef ONLINE_JUDGE
9+
// for getting input from input.txt
10+
freopen("input.txt", "r", stdin);
11+
// for writing output to output.txt
12+
freopen("output.txt", "w", stdout);
13+
#endif
14+
15+
long long int value;
16+
vector<long long int> inputs;
17+
while(cin>>value){
18+
cout<<value;
19+
inputs.push_back(value);
20+
}
21+
int q = inputs[inputs.size()-1];
22+
23+
long long int ans = 1;
24+
25+
for(int i=0; i<inputs.size()-1;i++){
26+
ans = ans*inputs[i];
27+
}
28+
auto loopEnd = max_element(inputs.begin(), inputs.end()-1);
29+
for(int i=0; i <loopEnd ; )
30+
31+
32+
return 0;
33+
}

ProblemReplaceBitsNandM

34.5 KB
Binary file not shown.

ProblemReplaceBitsNandM.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
6+
// Replace bits in N by M
7+
8+
// You are given two 32- bit numbers, N and M two bit positions, i and j.
9+
// Write a method to set all bits between i and j in N equal to M
10+
// (e.g., M becomes a substring of N located at i and starting at j).
11+
12+
// EXAMPLE:
13+
// Input: N = 10000000000,
14+
// M = 10101, i = 2, j = 6
15+
16+
// Output: N = 10001010100
17+
18+
// for this we have to first cleard the range of bits from range i to j
19+
// and then or with m left shift i.
20+
21+
int clearRangeItoJ(int n, int i, int j) {
22+
23+
int ones = (~0);
24+
int a = ones<<(j+1);
25+
int b = (1<<i) - 1;
26+
27+
int mask = a|b;
28+
29+
int ans = n&mask;
30+
31+
return ans;
32+
}
33+
34+
int replaceBits(int n, int m, int i, int j) {
35+
int n_ = clearRangeItoJ(n,i,j);
36+
37+
int ans = n_ | (m << i);
38+
39+
return ans;
40+
}
41+
42+
int main() {
43+
44+
#ifndef ONLINE_JUDGE
45+
// for getting input from input.txt
46+
freopen("input.txt", "r", stdin);
47+
// for writing output to output.txt
48+
freopen("output.txt", "w", stdout);
49+
#endif
50+
51+
int n = 15;
52+
53+
int i = 1, j = 4;
54+
55+
int m = 5;
56+
57+
int ans = replaceBits(n,m,i,j);
58+
59+
cout<<ans<<endl;
60+
61+
return 0;
62+
}

0 commit comments

Comments
 (0)