├── 0.All Past contest.md ├── 1.How -to-start-cp[lecture 1].md ├── 1.time Compexity[lecture 2].md ├── 2.Arrays.md ├── 3.Number_Theory.md ├── 4.STL.md ├── 5.bitmasking.md ├── README.md ├── SPOJ_LIST ├── ABSP1.cpp ├── DOL.cpp ├── ESYRCRTN.cpp ├── GIRLSNBS.cpp ├── MOHIB.cpp ├── PHT.cpp ├── QUADAREA.cpp ├── SPCQ.cpp ├── SPOJQUESTIONSLIST.pdf ├── acpc11b.cpp ├── army.cpp ├── codchess.cpp ├── crds.cpp ├── ec_conb.cpp ├── fashion.cpp ├── fctrl.cpp ├── fctrl2(c++17).cpp ├── fctrl2(using int array).cpp ├── ieeeeebgam.cpp └── infix2postfix.cpp ├── graphs.cpp └── toh.cpp /0.All Past contest.md: -------------------------------------------------------------------------------- 1 | # All past Contest 2 | 3 | 4 | Contest 1 based on arrays 5 | 6 | link to contest : https://codeforces.com/contestInvitation/4377116e5bd9466d83d1f6e6cf2411299e088592 7 | 8 | Link to Editorial : https://youtu.be/hEKLwM9qXPc 9 | 10 | Contest 2 based on Number Theory 11 | 12 | Link to contest : https://codeforces.com/contestInvitation/57cb7c90836ae129ab29d3bda0c850f8fcaa23d8 13 | 14 | Link to editorial : https://youtu.be/J7N-2a4wv-0 15 | 16 | Contest 3 based on Strings 17 | 18 | Link to contest : https://codeforces.com/contestInvitation/c1e985a9cc2bdcc0ae980029870384526ade7035 19 | 20 | Link to editorial : https://youtu.be/0S6EvRbMkKo 21 | 22 | Contest 4 based on STL 23 | 24 | Link to contest : https://codeforces.com/contestInvitation/6134b80446ec8fe60c7a579401850487b4dd60f4 25 | 26 | Link to editorial : https://youtu.be/qwdpu09wF3U 27 | 28 | Contest 5 based on Bit-masking 29 | 30 | Link to contest : https://codeforces.com/contestInvitation/2f1acb4a733c9ee47ac3974001e2eb815aa792b4 31 | 32 | Link to editorial : https://youtu.be/o_D7x2cwnOo 33 | 34 | Contest 6 based on Binary-Search 35 | 36 | Link to contest : https://codeforces.com/contestInvitation/d35aa6eeecc2893eb6e44b66b106205f67c03d28 37 | 38 | Link to editorial : https://youtu.be/OjEqOminJew 39 | 40 | Contest 7 prized contest mix problems 41 | 42 | Link to contest : https://codeforces.com/contestInvitation/731604873168614ef132ac74e851305f79fc9d87 43 | 44 | Link to editorial of A : https://youtu.be/lebxb3adlhY 45 | 46 | 47 | Link to editorial of B : https://youtu.be/CwKhKuYEvZo 48 | 49 | 50 | Link to editorial of C : https://youtu.be/Hqn8XEW3EQI 51 | 52 | 53 | Link to editorial of D : https://youtu.be/Di7R4ic6YIs 54 | 55 | 56 | Link to editorial of E : https://youtu.be/XuQnCByZHAQ 57 | -------------------------------------------------------------------------------- /1.How -to-start-cp[lecture 1].md: -------------------------------------------------------------------------------- 1 | #header 2 | + 3 | ##header2 4 | 5 | 6 | 7 | Important resources for practicing questions from basic level Topic-wise or level-wise. 8 | 9 | Codedigger.tech 10 | 11 | http://codedigger.tech/ 12 | Solve at least 5 question per day from ladder A. 13 | 14 | Leetcode 15 | 16 | https://leetcode.com/ 17 | Select the topic you want to practice do 10-20 easy question, 10 medium and 5 hard question per topic. 18 | 19 | 20 | For basic of data structures you can visit : https://youtu.be/92S4zgXN17o 21 | 22 | For basic of programming language C visit : https://youtu.be/AWliApDc61w 23 | 24 | For beginners best book for data structures and algorithms is : https://drive.google.com/file/d/1r8RhR3lBVptlGOvoQ2i9hYZ2BcmX9zAN/view?usp=sharing 25 | 26 | For advanced people in cp best book for practice is :https://drive.google.com/open?id=1DEK28TI8fKmy9JY4E7tB07jn2VrgLO1m 27 | 28 | For interview preparation :https://drive.google.com/file/d/14X5jJpfWwdfzqEr9kNxlhFJEmW2s6Z6A/view?usp=sharing 29 | 30 | // Happy Coding // 31 | -------------------------------------------------------------------------------- /1.time Compexity[lecture 2].md: -------------------------------------------------------------------------------- 1 | # Time Complexity 2 | 3 | 4 | 5 | 6 | Resources To learn the basics of time Complexity 7 | - https://www.interviewbit.com/courses/programming/topics/time-complexity/#problems 8 | 9 | - My lecture video : https://youtu.be/M_eRoHLMEJ8 10 | 11 | Question: Why we find it ? 12 | 13 | To calculate how much time our program will take in seconds for the worst case scenario and see if it's within the constraints or not. 14 | 15 | Constraints are probably the most important part of the problem. 16 | 17 | 1. They can give you exact time complexity that is needed to pass all test cases . 18 | 2. They can tell you the space complexity that you will be needed. 19 | 3. They can tell you about data type that you should be using. 20 | 21 | Part 1. Time Complexity 22 | 23 | Most of the platforms uses 1 sec to perform 10^8 operations 24 | 25 | Suppose you perform x operations 26 | 27 | 10^8 operations = 1 sec 28 | x operation will take = x/10^8 secs 29 | 30 | Generally you will have the time limit of 1-2 secs to pass all the test cases 31 | 32 | So your number of operation must not exceed 10^8 operations. 33 | 34 | How to calculate number of operations ? 35 | 36 | Suppose constraints of n is 1<=n<=10^7 37 | 38 | You have calculated that time complexity of your program is N*logN 39 | If you want to calculate the number of operations then replace N with maximum value of N because if your maximum operation can be performed within the time limit, than less number 40 | of operations can be performed easily. 41 | 42 | So number of operations are= 10^7* log(10^7) = 7 * 10^7 assuming log base 10. 43 | Now if you want time in seconds then that will be = 10^7 * 7 / 10^8 = 7* 10^-1 = 10 ms . 44 | 45 | So its easy to observe that we just need to limit our number of operations to 10^8 for passing all test cases. 46 | 47 | How you can easily find the limit of your program's complexity according to given Constraints ? 48 | 49 | Remember these constraints and the maximum time limit you can have for these constraints . 50 | 51 | Constraints Time Complexity 52 | 53 | 1<=N<=10^18 O( log N ) 54 | 55 | 1<=N<=10^8 O( N ) 56 | 57 | 1<=N<=10^7 O( N logN ) 58 | 59 | 1<=N<=10^4 O( N^2 ) 60 | 61 | 1<=N<=500 O( N^3 ) 62 | 63 | 1<=N<=90 O( N^4 ) 64 | 65 | 1<=N<=20 O( 2^N ) 66 | 67 | 1<=N<=10 O( N! ) 68 | 69 | 70 | Some Commonly used For loops and their time Complexity 71 | 72 | For loops Their Corresponding time Complexity 73 | 74 | 1. for( i=0;i fact(int N) 32 | 33 | { 34 | vector factor; 35 | for(int i=1; i<= sqrt(N); i++) 36 | { 37 | if(N % i == 0) 38 | { 39 | factor.push_back(i); 40 | if(i != N/i) 41 | factor.push_back(N/i); 42 | } 43 | } 44 | return factor; 45 | } 46 | ``` 47 | 48 | 49 | - Prime (Sieve algo) 50 | 51 | ```c++ 52 | vector isprime(1e6+1,true); 53 | 54 | vector primes; 55 | 56 | void sieve() 57 | 58 | { 59 | 60 | ll int i,j; 61 | isprime[0] = false; 62 | isprime[1] = false; 63 | for(i=2; i<=1e6 ;i++) 64 | { 65 | 66 | if(isprime[i]) 67 | { 68 | 69 | primes.push_back(i); 70 | for(j=i*i; j<=1e6; j+=i) 71 | 72 | { 73 | 74 | isprime[j]=false; 75 | 76 | } 77 | 78 | } 79 | 80 | } 81 | 82 | } 83 | ``` 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | Number Theory (part-2) tutorial (1 Qustion on sieve, Prime-Factors, Totient Function ) 92 | 93 | 94 | - Link : https://youtu.be/m6uROtdC-q4 95 | 96 | - Link to pdf : https://drive.google.com/file/d/1sT7BFhU_SfeWEtQf81qQ1eNbiK7suSIZ/view?usp=sharing 97 | 98 | - Question Link : https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=720&page=show_problem&problem=1081 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | - How to find Prime-factors 107 | 108 | ```c++ 109 | vector isprime(1e6+1,true); 110 | 111 | vector primes; 112 | 113 | void sieve() 114 | 115 | { 116 | 117 | ll int i,j; 118 | isprime[0] = false; 119 | isprime[1] = false; 120 | 121 | for(i=2; i<=1e6 ;i++) 122 | { 123 | 124 | if(isprime[i]) 125 | { 126 | 127 | primes.push_back(i); 128 | 129 | for(j=i*i; j<=1e6; j+=i) 130 | { 131 | isprime[j]=false; 132 | } 133 | 134 | } 135 | 136 | } 137 | } 138 | 139 | ``` 140 | 141 | ```c++ 142 | vector primefactors(ll int N) 143 | 144 | { 145 | 146 | vector factors; 147 | ll pf_idx = 0, pf=primes[pf_idx]; 148 | 149 | while(pf*pf<=N) 150 | { 151 | while(N % pf == 0) 152 | { 153 | N/=pf; 154 | factors.push_back(pf); 155 | } 156 | pf = primes[++pf_idx]; 157 | } 158 | 159 | if(N != 1) factors.push_back(N); 160 | return factors; 161 | 162 | } 163 | 164 | ``` 165 | 166 | 167 | 168 | 169 | 170 | Important functions to do 171 | 172 | 1. Count number of prime factors. 173 | 2. Count number of distinct prime factors. 174 | 3. Sum of all prime factors. 175 | 4. Count number of divisors of N. 176 | 5. Sum of all divisors of N. 177 | 178 | If N can be represented as N = a^i x b^j x ....... x c^k 179 | where a, b, c are prime factors of N then -: 180 | 181 | No of divisors = (i+1)x(j+1)x......x(k+1) 182 | 183 | Sum of divisors = ((a^i+1) - 1 )/(a-1) x ((b^j+1)-1)/(b-1) x ..... x ((c^k+1)-1)/(c-1) 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | Totient Function 192 | 193 | Formula = Eulerphi(N) = N x ((product of all terms)(1 - 1/Prime factor i )) 194 | eg N = 36 = 2^2 x 3^2 195 | 196 | Eulerphi(N) = N x (1 - 1/2) x (1 - 1/3) = 12 197 | 198 | ```c++ 199 | ll int eulerPhi( ll int N) 200 | { 201 | 202 | ll pf_idx = 0, pf=primes[pf_idx]; 203 | 204 | ll ans=N; 205 | 206 | while(pf*pf<=N) 207 | { 208 | if(N % pf == 0) 209 | { 210 | ans -= ans / pf; 211 | } 212 | while(N % pf == 0) N /= pf; 213 | 214 | pf = primes[++pf_idx]; 215 | } 216 | if(N != 1) ans -= ans / N; 217 | return ans; 218 | } 219 | 220 | ``` 221 | 222 | - Link to Contest 2 based on Number theory : https://codeforces.com/contestInvitation/57cb7c90836ae129ab29d3bda0c850f8fcaa23d8 223 | 224 | - Link to editorial : https://youtu.be/J7N-2a4wv-0 225 | 226 | 227 | Number Theory (part-3) tutorial (Factorial and cpp_int data type, Modified sieve, Modulo Arithmetic) 228 | 229 | - Link : https://youtu.be/RY8JzpnX9PU 230 | 231 | - Link to pdf : https://drive.google.com/file/d/11-ZpFL1D5VNtSqSH_K3oT-gNf_rgG9fw/view?usp=sharing 232 | 233 | 234 | Factorial 235 | 236 | ```c++ 237 | vector factorial(n, 1); 238 | 239 | long long int fact(int n) 240 | 241 | { 242 | for(int i=1; i<=n; i++) 243 | { 244 | factorial[i] = factorial[i - 1] * i; 245 | } 246 | return factorial[n]; 247 | } 248 | 249 | ``` 250 | 251 | 252 | 253 | 254 | Modified sieve to count number of different prime factors of a number directly 255 | 256 | ```c++ 257 | vector< int > divisor(1e7+1, 0) ; 258 | 259 | void sieve() 260 | { 261 | long long int j; 262 | for(int i=2; i<=1e7; i++) 263 | { 264 | if(!divisor[i]) // i.e prime number 265 | { 266 | for(j= i+i; j<=1e7; j+=i) 267 | { 268 | divisor[j]++; // after this algo you can find the number of diff P.F 269 | in O(1) just by divisor[n]. 270 | } 271 | } 272 | } 273 | 274 | } 275 | 276 | ``` 277 | 278 | Modulo Arithmatics 279 | 280 | 1. (a + b) % m = ( a % m + b % m ) % m 281 | 282 | 2. (a - b) % m = ( a % m - b % m ) % m 283 | 284 | 3. (a * b) % m = ( a % m * b % m ) % m 285 | 286 | 4. (a^b) % m = (( a % m )^b ) % m 287 | 288 | 289 | 290 | Number Theory (part-4) tutorial (Extended Euclid Algo, Diophantine EQ., Inverse Modulo) 291 | 292 | - Link : https://youtu.be/IQQKrcDkfvE 293 | 294 | - Link to pdf : https://drive.google.com/file/d/1r_EqMLX3MfiiI_tuxEQGZJ8z4H3uyXD8/view?usp=sharing 295 | 296 | 297 | Extended Euclid 298 | 299 | 300 | ```c++ 301 | int x, y, d; 302 | 303 | void extended_euclid(int a, int b) 304 | 305 | { 306 | if(b == 0) {x = 1, y=0, d = a, return; } 307 | extended_euclid( b, a%b); // same as gcd 308 | int x1 = y; 309 | int y1 = x - (a / b) * y; 310 | x = x1; 311 | y = y1; 312 | } 313 | 314 | ``` 315 | 316 | Inverse Modulo 317 | 318 | 1. Use Extend euclid to calculate inverse multiplicative modulo of a with modulo m by calling above 319 | function as extended_euclid(a, m) and the value of x is our inverse. 320 | 321 | 2. (a / b) % m = ( a % m * (b^-1) % m ) % m 322 | = ( a % m * (inv) ) % m // inv = value of x from above 323 | 324 | 325 | 326 | 327 | 328 | // Happy Coding // 329 | -------------------------------------------------------------------------------- /4.STL.md: -------------------------------------------------------------------------------- 1 | # Stl 2 | 3 | 4 | 5 | ##### Best website to learn all the stls in cpp is :- 6 | 7 | - [cplusplus](http://www.cplusplus.com/) 8 | 9 | 10 | ##### Best website to practice topic-wise each stl is :- 11 | 12 | - [codedigger](http://codedigger.tech/) 13 | 14 | ##### Video tutorial for STL 15 | 16 | Link to playlist : https://youtu.be/PCahjLFtlbY 17 | 18 | Do study about the below mentioned STL data structures and inbuilt function 19 | 20 | Some of the important STL containers are :- 21 | 22 | 1. Vectors :- 23 | 24 | Vectors are sequence containers representing arrays that can change in size. Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container. 25 | 26 | vector < type > variable_name; 27 | 28 | 2. Maps :- 29 | 30 | Maps are associative containers that store elements formed by a combination of a key value and a mapped value. In a map, the key values are generally used to sort and uniquely identify the elements, while the mapped values store the content associated to this key. The types of key and mapped value may differ. Map containers are generally slower than unordered_map containers to access individual elements by their key, but they allow the direct iteration on subsets based on their order. 31 | 32 | map < type_1, typr_2 > variable_name; 33 | 34 | 3. Set :- 35 | 36 | Sets are containers that store unique elements following a specific order. In a set, the value of an element also identifies it (the value is itself the key, of type T), and each value must be unique. The value of the elements in a set cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container. 37 | 38 | set < type > variable_name; 39 | 40 | 3. List :- 41 | 42 | Lists are sequence containers that allow constant time insert and erase operations anywhere within the sequence, and iteration in both directions. List containers are implemented as doubly-linked lists; Doubly linked lists can store each of the elements they contain in different and unrelated storage locations. The ordering is kept internally by the association to each element of a link to the element preceding it and a link to the element following it. 43 | They are very similar to forward_list: The main difference being that forward_list objects are single-linked lists, and thus they can only be iterated forwards, in exchange for being somewhat smaller and more efficient. The main drawback of lists and forward_lists compared to these other sequence containers is that they lack direct access to the elements by their position 44 | 45 | list < type > variable_name; 46 | 47 | 4. Stack :- 48 | 49 | Stacks are a type of container adaptor, specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from one end of the container. 50 | 51 | stack < type > variable_name; 52 | 53 | 5. Queue :- 54 | 55 | Queues are a type of container adaptor, specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other. 56 | 57 | queue < type > variable_name; 58 | 59 | 6. Deque :- 60 | 61 | Deque (usually pronounced like "deck") is an irregular acronym of double-ended queue. Double-ended queues are sequence containers with dynamic sizes that can be expanded or contracted on both ends (either its front or its back). They provide a functionality similar to vectors, but with efficient insertion and deletion of elements also at the beginning of the sequence, and not only at its end. But, unlike vectors, deques are not guaranteed to store all its elements in contiguous storage locations: accessing elements in a deque by offsetting a pointer to another element causes undefined behavior. 62 | 63 | deque < type > variable_name; 64 | 65 | 7. Priority Queue :- 66 | 67 | Priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains, according to some strict weak ordering criterion. This context is similar to a heap, where elements can be inserted at any moment, and only the max heap element can be retrieved (the one at the top in the priority queue). 68 | If you want min_priority_queue you can either use priority_queue >, or in numerical case pass the negative of all numbers as greater number negative is samller and samller's negative is greater so it will always give smaller elements first. 69 | 70 | priority_queue < type > variable_name; //for max_heap 71 | priority_queue , greater< type > > variable_name; // for min_heap 72 | 73 | 8. Bitset :- 74 | 75 | A bitset stores bits (elements with only two possible values: 0 or 1, true or false, ...). The class emulates an array of bool elements, but optimized for space allocation: generally, each element occupies only one bit (which, on most systems, is eight times less than the smallest elemental type: char). Bitsets have the feature of being able to be constructed from and converted to both integer values and binary strings (see its constructor and members to_ulong and to_string). They can also be directly inserted and extracted from streams in binary format. The size of a bitset is fixed at compile-time. 76 | 77 | bitset < size > variable_name; 78 | 79 | 9. Pairs :- 80 | 81 | This class couples together a pair of values, which may be of different types (T1 and T2). The individual values can be accessed through its public members first and second. Pairs are a particular case of tuple. 82 | 83 | pair < type_1, type_2 > variable_name; 84 | 85 | 10. Tuple :- 86 | 87 | A tuple is an object capable to hold a collection of elements. Each element can be of a different type. 88 | 89 | tuple < type_1, type2, type_3, ....., type_n > varibale_name; 90 | 91 | 92 | #### Some of the inbuilt usefull STL functions are :- 93 | 94 | 1. find() :- 95 | Searches the container for an element equivalent to val and returns an iterator to it if found, otherwise it returns an iterator to container::end. O(logn) for set, map. 96 | 97 | 2. count() :- 98 | Searches the container for elements equivalent to val and returns the number of matches. O(log n) complexity in map, set. 99 | 100 | 3. erase() :- 101 | Removes from the container either a single element or a range of elements. O(1) for erase(pos), O(log n) for erase(val) and linear for range. 102 | 103 | 4. insert() :- 104 | Extends the container by inserting new elements, effectively increasing the container size by the number of elements inserted. O(log n) for set, map. 105 | 106 | 5. begin() :- 107 | Returns an iterator referring to the first element in the container. O(1). 108 | 109 | 6. end() :- 110 | Returns an iterator referring to the past-the-end element in the set container. O(1). 111 | 112 | 7. rbegin() :- 113 | Returns a reverse iterator pointing to the last element in the container (i.e., its reverse beginning). Reverse iterators iterate backwards: increasing them moves them towards the beginning of the container. O(1). 114 | 115 | 8. rend() :- 116 | Returns a reverse iterator pointing to the theoretical element right before the first element in the container (which is considered its reverse end). O(1). 117 | 118 | 9. push() :- 119 | Inserts a new element at the top of the stack,front of queue, priority_queue. The content of this new element is initialized to a copy of val. O(1). 120 | 121 | 10. push_back() :- 122 | Adds a new element at the end of the container, after its current last element. The content of val is copied (or moved) to the new element. O(1). 123 | 124 | 11. push_front():- 125 | Inserts a new element at the beginning of the deque container, right before its current first element. The content of val is copied (or moved) to the inserted element. O(1). 126 | 127 | 12. pop() :- 128 | Removes the element on top of the stack,queue, peiority_queue effectively reducing its size by one. O(1). 129 | 130 | 13. pop_back() :- 131 | Removes the last element in the deque container, effectively reducing the container size by one. O(1). 132 | 133 | 14. pop_front() :- 134 | Removes the first element in the deque container, effectively reducing its size by one. O(1). 135 | 136 | 15. top() :- 137 | Returns a reference to the top element in the stack, max element in priority_queue. O(1). 138 | 139 | 16. front() :- 140 | Returns a reference to the first element in the container. O(1). 141 | 142 | 17. back() :- 143 | Returns a reference to the last element in the container. O(1). 144 | 145 | 19. sort() :- 146 | Sorts the elements in the range [first,last) into ascending order. O(n log n). 147 | 148 | 20. reverse() :- 149 | Reverses the order of the elements in the range [first,last). O(linear). 150 | 151 | 21. stoi() :- 152 | Parses str interpreting its content as an integral number of the specified base, which is returned as an int value. O(linear). 153 | 154 | 22. to_string() :- 155 | Returns a string with the representation of val. O(linear). 156 | 157 | 23. __gcd() :- 158 | Calculate gcd of two numbers. O(log n). 159 | 160 | 24. clear() :- 161 | Removes all elements from the set container (which are destroyed), leaving the container with a size of 0. - O(n). 162 | 163 | 25. empty() :- 164 | Returns whether the container is empty (i.e. whether its size is 0). O(1). 165 | 166 | 26. equal_range() :- 167 | Returns the bounds of a range that includes all the elements in the container that are equivalent to val. O(log n) in set, map. 168 | 169 | 27. lower_bound() :- 170 | Returns an iterator pointing to the first element in the container which is not considered to go before val (i.e., either it is equivalent or goes after). O(log n) 171 | 172 | 28. upper_bound() :- 173 | Returns an iterator pointing to the first element in the container which is considered to go after val. O(log n). 174 | 175 | 29. size() :- 176 | Returns the number of elements in the set container. O(1). 177 | 178 | 30. swap() :- 179 | Swaps the two arguments. O(1) . 180 | 181 | 31. binary_search() :- 182 | Search an element in an sorted array (i.e., keep dividing the array in half (divide and conquer) until the element is found). 183 | 184 | -------------------------------------------------------------------------------- /5.bitmasking.md: -------------------------------------------------------------------------------- 1 | # Bitmasking 2 | 3 | 4 | ##### Best website to practice Bit-masking(i.e difficulty wise sorted question ) is :- 5 | 6 | - [codedigger](http://codedigger.tech/) 7 | 8 | ##### Book to go deep in bit_masking is:- 9 | 10 | - [Hacker's Delight](https://drive.google.com/file/d/1Afqi5s8o0l5NBvC_OARdzlNFIO4Nrzd3/view?usp=sharing) 11 | 12 | ##### Video Tutorials for Bit-Masking 13 | 14 | - [Bitmasking Part-1](https://youtu.be/5upadP-hWXQ) 15 | - [Bitmasking Part-2](https://youtu.be/KD9EfL-hedA) 16 | - [Bitmasking Part-3](https://youtu.be/o_D7x2cwnOo) 17 | 18 | 19 | ##### Useful Templates to be used for bit-masking are : - 20 | 21 | #define isOn(S, j) (S & (1 << j)) 22 | 23 | #define setBit(S, j) (S |= (1 << j)) 24 | 25 | #define clearBit(S, j) (S &= ~(1 << j)) 26 | 27 | #define toggleBit(S, j) (S ^= (1 << j)) 28 | 29 | #define lowBit(S) (S & (-S)) 30 | 31 | #define setAll(S, n) (S = (1 << n) - 1) 32 | 33 | #define modulo(S, N) ((S) & (N - 1)) // returns S % N, where N is a power of 2 34 | 35 | #define isPowerOfTwo(S) (!(S & (S - 1))) 36 | 37 | #define nearestPowerOfTwo(S) ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5))) 38 | 39 | #define turnOffLastBit(S) ((S) & (S - 1)) 40 | 41 | #define turnOnLastZero(S) ((S) | (S + 1)) 42 | 43 | #define turnOffLastConsecutiveBits(S) ((S) & (S + 1)) 44 | 45 | #define turnOnLastConsecutiveZeroes(S) ((S) | (S - 1)) 46 | 47 | 48 | ##### Some very useful inbuilt functions :- 49 | 50 | 1. _builtin_popcount(x): This function is used to count the number of one’s(set bits) in an integer. 51 | 52 | 2. _builtin_parity(x): This function is used to check the parity of a number. This function returns true(1) if the number has odd parity else it returns false(0) for even parity. 53 | 54 | 3. __builtin_clz(x): This function is used to count the leading zeros of the integer. Note : clz = count leading zero’s 55 | 56 | 4. __builtin_ctz(x): This function is used to count the trailing zeros of the given integer. Note : ctz = count trailing zeros. 57 | 58 | 59 | ##### A very useful STL for bitmasing is bitset<> 60 | 61 | A bitset is an array of bool but each Boolean value is not stored separately instead bitset optimizes the space such that each bool takes 1 bit space only, so space taken by bitset bs is less than that of bool bs[N] and vector bs(N). However, a limitation of bitset is, N must be known at compile time, i.e., a constant. 62 | 63 | ###### Useful inbuilt functions with bitset<> are :- 64 | 65 | 1. set() 66 | 67 | 2. flip() 68 | 69 | 3. reset() 70 | 71 | 4. any() 72 | 73 | 5. none() 74 | 75 | 6. all() 76 | 77 | 7. test() 78 | 79 | 8. count() 80 | 81 | 82 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Competitive-Coding-From-scratch 2 | ALL the topics of competitive-programming will be avialable here with the resourses from where you can learn those topic basics and Question related to that topic along with the solution will be provided here. 3 | 4 | //Happy Coding// 5 | -------------------------------------------------------------------------------- /SPOJ_LIST/ABSP1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | while(t--) 20 | { 21 | ll n; 22 | cin>>n; 23 | ll a[n]; 24 | fo(i,n) 25 | cin>>a[i]; 26 | ll sum=0; 27 | for(ll i=0;i 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | for(int i=0;i>n; 23 | while(n%2==0) 24 | { 25 | n/=2; 26 | } 27 | cout<<"Case "<<(i+1)<<": "< 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | while(t--) 20 | { 21 | ll n; 22 | cin>>n; 23 | 24 | ll a[]={1,4,6,5,2,0}; 25 | cout<<(a[(n-1)%6])<<"\n"; 26 | 27 | } 28 | 29 | 30 | return 0; 31 | } 32 | /******************************************************* 33 | WRONG QUESTION(ERROR) 34 | **********************************************************/ -------------------------------------------------------------------------------- /SPOJ_LIST/GIRLSNBS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>g>>b && g!=-1) 19 | { 20 | ll ans=0; 21 | ll k=g/(b+1); 22 | ll l=b/(g+1); 23 | if(g==b) 24 | { 25 | if(g==0 && b==0) 26 | ans=0; 27 | else 28 | ans=1; 29 | } 30 | else if(g>b) 31 | { 32 | if(g%(b+1)!=0) 33 | ans=1+k; 34 | else 35 | ans=k; 36 | } 37 | else 38 | { 39 | if(b%(g+1)!=0) 40 | ans=1+l; 41 | else 42 | ans=l; 43 | } 44 | cout< 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | while(t--) 20 | { 21 | ll x,a; 22 | cin>>x>>a; 23 | ll n=a-x; 24 | ll sum=n*(a+1); 25 | sum=sum-n*(n-1)/2; 26 | cout< 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | for(int i=0;i>n; 23 | 24 | if(n<3) 25 | ans=0; 26 | else 27 | ans=sqrt(1+n)-1; 28 | cout<<"Case "<<(i+1)<<": "< 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | while(t--) 20 | { 21 | double a,b,c,d; 22 | cin>>a>>b>>c>>d; 23 | 24 | double sp=(a+b+c+d)/2; 25 | double area=sqrt((sp-a)*(sp-b)*(sp-c)*(sp-d)); 26 | 27 | cout< 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i0) 15 | { 16 | digsum+=n%10; 17 | n/=10; 18 | } 19 | return digsum; 20 | } 21 | 22 | int main() { 23 | #ifndef ONLINE_JUDGE 24 | freopen("input.txt", "r", stdin); 25 | freopen("output.txt", "w", stdout); 26 | #endif 27 | fast_io 28 | 29 | ll t; 30 | cin>>t; 31 | while(t--) 32 | { 33 | ll n; 34 | cin>>n; 35 | while((n%sum(n))!=0) 36 | { 37 | n++; 38 | } 39 | cout< 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | while(t--) 20 | { 21 | ll n,m,diff=0,mini=INT_MAX; 22 | cin>>n; 23 | ll a[n]; 24 | fo(i,n) 25 | cin>>a[i]; 26 | cin>>m; 27 | ll b[m]; 28 | fo(i,m) 29 | cin>>b[i]; 30 | 31 | sort(a,a+n); 32 | sort(b,b+m); 33 | 34 | ll c=0,d=0; 35 | ll res=INT_MAX; 36 | 37 | while(c 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | while(t--) 20 | { 21 | ll n,m,maxa,maxb,ans; 22 | cin>>n>>m; 23 | vector a(n); 24 | vector b(m); 25 | 26 | fo(i,n) 27 | cin>>a[i]; 28 | fo(i,m) 29 | cin>>b[i]; 30 | maxa=*max_element(a.begin(),a.end()); 31 | maxb=*max_element(b.begin(),b.end()); 32 | 33 | // ans=max(maxa,maxb); 34 | if(maxa>=maxb) 35 | cout<<"Godzilla"<<"\n"; 36 | else 37 | cout<<"MechaGodzilla"<<"\n"; 38 | 39 | } 40 | 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /SPOJ_LIST/codchess.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | while(t--) 20 | { 21 | ll n;cin>>n; 22 | 23 | if(n%2) 24 | cout<<0<<"\n"; 25 | else 26 | cout<<1<<"\n"; 27 | 28 | } 29 | 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /SPOJ_LIST/crds.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | while(t--) 20 | { 21 | ll n; 22 | cin>>n; 23 | 24 | ll MOD=1000007; 25 | 26 | //ll ans=((((n%MOD)*(n%MOD)*(n%MOD))%MOD + ((n%MOD)*(n%MOD))%MOD -(2*(n%MOD))%MOD))/2%MOD; 27 | ll ans=(((3*(n%MOD)*(n%MOD))+(n%MOD))/2)%MOD; 28 | 29 | cout< 2 | #define ll long long int 3 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 4 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 5 | #define MOD 1000000007 6 | #define pb push_back 7 | #define fo(i,n) for(ll i=0;i>t; 16 | while(t--) 17 | { 18 | tem = 0;; 19 | cin >> x; 20 | if (x % 2 != 0) 21 | { 22 | cout << x << endl; 23 | continue; 24 | } 25 | 26 | while (x != 0) 27 | { 28 | tem = tem << 1; 29 | tem = tem + (x & 1); 30 | x = x >> 1; 31 | 32 | } 33 | cout << tem << endl; 34 | } 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /SPOJ_LIST/fashion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | while(t--) 20 | { 21 | ll n,hb=0,sum=0; 22 | cin>>n; 23 | ll a[n]; 24 | ll b[n]; 25 | fo(i,n) 26 | cin>>a[i]; 27 | fo(i,n) 28 | cin>>b[i]; 29 | 30 | sort(a,a+n); 31 | sort(b,b+n); 32 | 33 | fo(i,n) 34 | { 35 | hb=a[i]*b[i]; 36 | sum+=hb; 37 | } 38 | cout< 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | while(t--) 20 | { 21 | ll n,ans=0; 22 | cin>>n; 23 | for(ll i=5;n/i>=1;i*=5) 24 | { 25 | ans+=n/i; 26 | } 27 | cout< 2 | #include 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i1; --i) 16 | fact *= i; 17 | return fact; 18 | } 19 | 20 | 21 | int main() { 22 | #ifndef ONLINE_JUDGE 23 | freopen("input.txt", "r", stdin); 24 | freopen("output.txt", "w", stdout); 25 | #endif 26 | fast_io 27 | 28 | ll t; 29 | cin>>t; 30 | while(t--) 31 | { 32 | ll n; 33 | cin>>n; 34 | cout< 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | while(t--) 20 | { 21 | 22 | } 23 | 24 | 25 | return 0; 26 | } 27 | 28 | 29 | // TO DO -------------------------------------------------------------------------------- /SPOJ_LIST/ieeeeebgam.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i>t; 19 | while(t--) 20 | { 21 | int n; 22 | cin>>n; 23 | 24 | double m=n; 25 | double ans=m/(m+1); 26 | cout< 2 | 3 | #define ll long long int 4 | #define watch(x) cout << (#x) << " is " << (x) << endl //watch function print variable and value for debugging 5 | #define count_ones __builtin_popcountll // count_ones(9) is equal to 2 valid for ll also 6 | #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); 7 | #define fo(i,n) for(ll i=0;i st; 25 | st.push('N'); 26 | int l=s.length(); 27 | string ns; 28 | 29 | for(int i=0;i='a' && s[i]<='z') || (s[i]>='A' && s[i]<='Z')) 32 | ns+=s[i]; 33 | else if(s[i]=='(') 34 | st.push('('); 35 | else if(s[i]==')') 36 | { 37 | while(st.top()!='N' && st.top()!='(') 38 | { 39 | char c=st.top(); 40 | st.pop(); 41 | ns+=c; 42 | } 43 | if(st.top()=='(') 44 | { 45 | char c=st.top(); 46 | st.pop(); 47 | } 48 | } 49 | else 50 | { 51 | while(st.top()!='N' && prec(s[i])<=prec(st.top())) 52 | { 53 | char c=st.top(); 54 | st.pop(); 55 | ns+=c; 56 | } 57 | st.push(s[i]); 58 | } 59 | } 60 | //pop whatever remains 61 | while(st.top()!='N') 62 | { 63 | char c=st.top(); 64 | st.pop(); 65 | ns+=c; 66 | } 67 | cout<>n; 78 | while(n--) 79 | { 80 | string exp=""; 81 | cin>>exp; 82 | infix2postfix(exp); 83 | } 84 | return 0; 85 | } -------------------------------------------------------------------------------- /graphs.cpp: -------------------------------------------------------------------------------- 1 | class graph 2 | { 3 | public: 4 | int n_v, n_e; 5 | vector> adj_list; 6 | bool isDirected; 7 | vector vis; 8 | vector indeg; //Stores indegree of all Nodes 9 | vector topo; //Stores lexicographically smallest toposort 10 | 11 | graph(int n) 12 | { 13 | n_v = n; 14 | adj_list.resize(n); 15 | vis.assign(n, false); 16 | } 17 | 18 | graph() {} 19 | 20 | void take_input(bool = false); 21 | 22 | void show_adj_list(); 23 | 24 | void addEdge(int , int , bool = false); 25 | 26 | void dfs(int ); 27 | 28 | void bfs(int ); 29 | 30 | bool toposort(); 31 | 32 | }; 33 | 34 | void graph::take_input(bool isDir) 35 | { 36 | cin >> n_v >> n_e; 37 | 38 | adj_list.resize(n_v); 39 | 40 | vis.assign(n_v, false); 41 | 42 | int to, fro; 43 | 44 | for (int i = 0; i < n_e; ++i) 45 | { 46 | cin >> to >> fro; 47 | addEdge(to - 1, fro - 1, isDir); 48 | } 49 | } 50 | 51 | void graph::show_adj_list() 52 | { 53 | 54 | cout << "No. of Vertices = " << n_v << endl; 55 | 56 | cout << "No. of Edges = " << n_e << endl; 57 | 58 | for (int i = 0 ; i < n_v; i++) 59 | { 60 | cout << i << "-> "; 61 | 62 | for (int j = 0; j < adj_list[i].size(); j++) 63 | cout << adj_list[i][j] << " "; 64 | 65 | cout << endl; 66 | } 67 | } 68 | 69 | void graph::addEdge(int to, int fro, bool isDir) 70 | { 71 | adj_list[to].push_back(fro); 72 | 73 | if (!isDir) 74 | adj_list[fro].push_back(to); 75 | } 76 | 77 | void graph::dfs(int source) 78 | { 79 | vis[source] = true; 80 | 81 | //cout< q; 98 | 99 | q.push(source); 100 | 101 | vis[source] = true; 102 | 103 | while (!q.empty()) 104 | { 105 | int parent = q.front(); 106 | 107 | cout << parent + 1 << " "; 108 | 109 | q.pop(); 110 | 111 | 112 | 113 | for (int child = 0; child < adj_list[parent].size(); child++) 114 | { 115 | int v = adj_list[parent][child]; 116 | if (!vis[v]) 117 | { 118 | q.push(v); 119 | vis[v] = true; 120 | } 121 | } 122 | } 123 | } 124 | 125 | /*Use Case 126 | graph g; 127 | g.take_input(); 128 | g.show_adj_list(); 129 | */ -------------------------------------------------------------------------------- /toh.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void towerofhanoi(int n,char src,char dest,char helper) 4 | { if(n==0) 5 | { return; 6 | } 7 | 8 | towerofhanoi(n-1,src,helper,dest); 9 | cout<<"Move"<>n; 17 | towerofhanoi(n,'A','C','B'); 18 | return 0; 19 | } 20 | --------------------------------------------------------------------------------