Skip to content

Commit 6f32040

Browse files
Add files via upload
1 parent 627af46 commit 6f32040

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed

Cumulative Sum & Frequency Array.txt

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
Frequency Array:
2+
-Introduction Problem:
3+
Given n Integers, for each number from 1 to 3 print the number of its occurrences.
4+
ans ----> Three variables.
5+
6+
-Classical Problem:
7+
Given n<=1e5 Integers ranging from 1 to 1e5,for each given number print the number of its occurrences.
8+
ans1 ----> O(n^2) time.
9+
ans2 ----> O(n) time.
10+
11+
int x, freq[N] = {0};
12+
for(int i = 0 ; i < n ; ++i){
13+
scanf("%d", &x);
14+
freq[x]++;
15+
}
16+
for(int i = 0 ; i < N ; ++i){
17+
if(freq[i])
18+
printf("The number %d was found %d times.\n");
19+
}
20+
21+
-What if the numbers are Integers in the range 1 to 1e9?
22+
-What if they weren't Integers (e.g. Chars, doubles or Strings)?
23+
24+
25+
Cumulative (Prefix) sum:
26+
-Classical Problem:
27+
Given n<=1e5 numbers, and q<=1e5 queries asking about sum of elements from l to r.
28+
ans1 ----> O(n*q) time.
29+
ans2 ----> O(q) time.
30+
31+
int n, A[N], pre[N], ans, l, r, q;
32+
void prefixSum(){
33+
scanf("%d", &n);
34+
for(int i = 1 ; i <= n ; ++i){
35+
scanf("%d", &A[i]);
36+
pre[i] = pre[i-1] + A[i];
37+
}
38+
scanf("%d", &q);
39+
while(q--){
40+
scanf("%d%d", &l, &r);
41+
printf("%d\n", pre[r]-pre[l-1]);
42+
}
43+
}
44+
45+
-What about Suffix sum?
46+
-What about 2D .. what about ND?
47+
48+
int n, m, A[N][N], pre[N][N], ans, l1, r1, l2, r2, q;
49+
void prefixSum2D(){
50+
scanf("%d%d", &n, &m);
51+
for(int i = 1 ; i <= n ; ++i){
52+
for(int j = 1 ; j <= m ; ++j){
53+
scanf("%d", &A[i][j]);
54+
pre[i][j] = pre[i][j-1] + A[i][j];
55+
}
56+
for(int j = 1 ; j <= m ; ++j){
57+
pre[i][j] += pre[i-1][j];
58+
}
59+
}
60+
scanf("%d", &q);
61+
while(q--){
62+
scanf("%d%d%d%d", &l1, &r1, &l2, &r2);
63+
printf("%d\n", pre[l2][r2] - pre[l1-1][r2] - pre[l2][r1-1] + pre[l1-1][r1-1]);
64+
}
65+
}
66+
67+
68+
Maximum Intersection:
69+
-Classical Problem:
70+
Given n<=1e5 friend who are free from time l to time r<=1e5
71+
, what's the time in which maximum number of friends are free?
72+
ans1 ----> O(n*r) time ----> TLE.
73+
ans2 ----> O(n) time.
74+
75+
76+
int q, f, t, day[N], maxi = 0, maxday;
77+
void maximumIntersection(){
78+
scanf("%d", &q);
79+
while(q--){
80+
scanf("%d%d", &f, &t);
81+
++day[l], --day[t+1];
82+
}
83+
for(int i = 1 ; i <= N ; ++i){
84+
day[i] += day[i-1];
85+
if(day[i] > maxi){
86+
maxi = day[i];
87+
maxday = i;
88+
}
89+
}
90+
printf("The maximum number of friends is %d in day number %d.\n", maxi, maxday);
91+
}

bitwise operations and bitmasks.txt

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
1- How does numbers are saved?
2+
Binary Representation.
3+
4+
Boolean algebra.
5+
6+
What's bitwise operations?
7+
8+
operation operator
9+
and &
10+
or |
11+
Xor ^
12+
invert ~
13+
left shift <<
14+
right shift >>
15+
16+
- How to get all the possible permutations of k(e.g k = 3) bits?
17+
18+
for(int msk = 0 ; msk < (1<<k) ; ++msk){
19+
// Anything
20+
}
21+
22+
23+
- How to check oddness of a variable x?
24+
25+
if(x&1)
26+
printf("It's Odd\n");
27+
else
28+
printf("It's Even\n");
29+
30+
31+
- How to check the state of the bit at position k of some mask?
32+
33+
if(msk&(1<<k))
34+
printf("It's ON");
35+
else
36+
printf("It's OFF");
37+
38+
39+
- How to ON the bit at position k of some mask?
40+
41+
msk |= (1<<k);
42+
43+
44+
- How to ON the first n bits of some mask?
45+
46+
msk |= ((1<<n)-1);
47+
48+
49+
- How to toggle the bit at position k of some mask?
50+
51+
msk ^= (1<<k);
52+
53+
54+
- How to OFF the bit at position k of some mask?
55+
56+
msk &= ~(1<<k);
57+
58+
59+
- How to count the number of ON bits in some mask of n bits?
60+
61+
int num = 0;
62+
for(int j = 0 ; j < n ; ++j)
63+
if(msk&(1<<j))
64+
++num;

0 commit comments

Comments
 (0)