|
| 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 | + } |
0 commit comments