├── 1 to 12 Divisibility check ├── Binary search ├── Allocate minimum number of pages │ └── code.cpp ├── Almost sorted array search │ └── almost sorted array.cpp ├── Find Floor of an element │ └── code.cpp ├── Find an element in a rotated sorted array │ └── code.cpp ├── Find ceil of an element │ └── code.cpp ├── Find position of an element in infinite array │ └── code.cpp ├── First and last occurrence │ ├── Last occurrence.cpp │ └── first occurrence.cpp ├── Peak Element │ └── code.cpp ├── binary search │ ├── Reverse sorted binary sorted.cpp │ └── binary search.cpp ├── count of element │ └── count of element.cpp ├── find element in bitonic array │ └── code.cpp ├── minimum difference element │ └── code.cpp ├── next Alphabetical element.cpp │ └── code.cpp ├── num of time a sorted arrray is roted │ └── code.cpp └── search in row and column wise │ └── code.cpp ├── Bit Manipulation ├── AND product.cpp ├── All pair sum xor.cpp ├── And product2.cpp ├── Checking for ith Set Bit.cpp ├── Counting Number Of Set Bits .cpp ├── Minimum Flips to make A OR B equal C LeetCode .cpp ├── Sansa and XOR.cpp ├── all pair sum xor2.cpp ├── bubart left and right shift │ ├── binary left shift.cpp │ └── binary right shift.cpp ├── count max 1 in range.cpp ├── sansa and xor2.cpp ├── single number 2.cpp └── total sum of all pair xor.cpp ├── Combinatorics ├── binomial coefficent │ ├── binomial coefficent dp.cpp │ └── binomial coefficent.cpp └── stars and bars │ ├── Two array │ └── two arrays.cpp │ ├── non negative and not zero solution │ └── non-zero.cpp │ └── non negative solution │ └── non-nagetive.cpp ├── Disjoint set ├── count of friends │ └── counts friends.cpp └── disjoint set │ └── disjoint set.cpp ├── Dynamic programming ├── Scrambled String Recursive │ ├── Scrambled String Memoized.cpp │ └── Scrambled String Recursive.cpp ├── fibonacci │ ├── fibonacci.cpp │ ├── fibonacci2.cpp │ └── fibonacci3.cpp ├── knapsack │ ├── 01knapsack │ │ ├── 01knapsack.cpp │ │ ├── equal sum pritition problem.cpp │ │ ├── memorization.cpp │ │ ├── recursion.cpp │ │ ├── subset-sum.cpp │ │ └── top-down.cpp │ ├── Ulta Knapsack │ │ └── knapsack.cpp │ ├── count of subset sum with given difference │ │ └── count of subset sum with given difference.cpp │ ├── count of subset sum │ │ └── cout subsetsum.cpp │ ├── equal sum patision │ │ └── equal sum patision.cpp │ ├── minimum difference │ │ └── minimun difference.cpp │ ├── subsetsum │ │ ├── subset sum dp.cpp │ │ └── subset sum.cpp │ └── target sum │ │ └── target sum.cpp ├── longest common subsequence │ ├── Longest Palindromic Subsequence │ │ └── Longest Palindromic Subsequence.cpp │ ├── Longest repeating subsequence │ │ └── Longest repeating subsequence.cpp │ ├── Minimum Number of Insertion and Deletion to convert String a to String b │ │ └── minmum itaretion.cpp │ ├── Minimum number of deletion in a string to make it a palindrome │ │ └── mndp.cpp │ ├── Minimum number of insertion in a string to make it a palindrome │ │ └── Minimum number of insertion in a string to make it a palindrome.cpp │ ├── Print shortest common Supersequence │ │ └── Print shortest common Supersequence.cpp │ ├── Sequence Pattern Matching │ │ └── Sequence Pattern Matching.cpp │ ├── lcm │ │ ├── lcm Top-down.cpp │ │ ├── lcm memorization.cpp │ │ └── lcs.cpp │ ├── longest common substring │ │ └── longest common substring.cpp │ ├── printing longest common subsuquence │ │ └── plcs.cpp │ └── sortest common supersequence │ │ └── scs.cpp ├── marge sort │ ├── marge sort.cpp │ └── margesortpractice.cpp ├── matrix chain multiplication │ ├── Egg Dropping Problem Recursive │ │ ├── Egg Dropping Problem Memoization Optimization.cpp │ │ ├── Egg Dropping Problem Memoization.cpp │ │ └── Egg Dropping Problem Recursive.cpp │ ├── Evaluate Expression to True Boolean Parenthesization Recursive │ │ ├── Memoized.cpp │ │ └── recursive.cpp │ ├── Palindrome Partitioning │ │ ├── Palindrome Partitioning Memoization.cpp │ │ ├── Palindrome Partitioning Memoized Optimisation.cpp │ │ └── Palindrome Partitioning Recursive.cpp │ └── mcm │ │ ├── bottom up mcm.cpp │ │ └── matrix chain.cpp └── unbounded knapsack problem │ ├── coin change problem unbounded │ └── coin change.cpp │ ├── min coin change problem │ └── min coin change problem.cpp │ ├── rod cutting problem │ └── rod cutting.cpp │ └── unbounded knapsack │ └── unbounded.cpp ├── Graph ├── Graph part 1 │ ├── 2D GRID │ │ ├── BFS 2d Grid │ │ │ └── BFS │ │ │ │ └── BFS.cpp │ │ └── DFS 2D GRID │ │ │ ├── DFS │ │ │ └── DFS.cpp │ │ │ └── connected component │ │ │ └── connected component.cpp │ ├── BFS │ │ ├── BFS │ │ │ └── bfs.cpp │ │ └── toposort │ │ │ └── topsort.cpp │ ├── DFS │ │ ├── Bipartite graph.cpp │ │ │ └── bipartite.cpp │ │ ├── DFS │ │ │ └── dfs.cpp │ │ ├── connected components │ │ │ └── connected components.cpp │ │ ├── cycel detectition of direct graph.cpp │ │ │ └── cycle.cpp │ │ ├── cyclle detectiton using dfs │ │ │ └── cycle detction.cpp │ │ ├── diameter of tree │ │ │ └── diameter of tree.cpp │ │ ├── finding articulation points.cpp │ │ │ └── articulation point.cpp │ │ ├── finding bridges │ │ │ └── finding bridges.cpp │ │ ├── in and out time │ │ │ └── in and out time.cpp │ │ ├── single source sortet path dfs │ │ │ └── single source sortest path.cpp │ │ └── subtree size │ │ │ └── subtree size.cpp │ ├── minimum movie target night │ │ └── chess knight moves.cpp │ └── strongly connected component │ │ └── kosarajus algorithm.cpp └── Graph part 2 │ ├── Distance between 2 nodes │ └── DB2N.cpp │ ├── Lowest common ancestor │ └── lca.cpp │ ├── Minimum Spanning tree │ └── kruskal algorithm │ │ ├── kruskal algorithm │ │ └── kruskal algorithm.cpp │ └── single source shortest path │ └── Disjsktras algorithm │ └── sssp.cpp ├── Heap algo ├── Connect Ropes to minimise the cost │ └── connect ropes to minimise the cost.cpp ├── Frequency sort │ └── Frequency sort.cpp ├── K Closest points to origin │ └── k closest points to origin.cpp ├── K closest number │ └── k closest numbers.cpp ├── Kth smallest Element │ └── Kth smallest element.cpp ├── Top K frequent number │ └── top k frequent numbers.cpp ├── k largest element │ └── k largest elements.cpp └── sort a K sorted array │ └── sorted a k sorted array.cpp ├── Important solution and question ├── Longest Ascending Subsequence ├── MOS algo ├── MOS algo query sum.cpp ├── MOS algo.cpp ├── MOs algo powerfull array in cf.cpp ├── adjacent max unique value └── mos algo dequery in spoj.cpp ├── Number Theory ├── Euclidean algorithm │ ├── basic algorithm.cpp │ └── extended algorithm.cpp ├── Euler's Totient Function │ ├── basic │ │ └── basic.cpp │ ├── gcd sum │ │ ├── All pair of gcd sum.cpp │ │ ├── gcd sum.cpp │ │ └── gcd sum2.cpp │ ├── nlog(logn) │ │ └── totient function.cpp │ └── sqrt(n) │ │ └── totient function.cpp ├── Linear diophantine equation │ ├── chicken macnugget theorem │ │ └── art of problem solving.cpp │ ├── crucial equation.cpp │ ├── linear diophantintine equation.cpp │ └── nothings.cpp ├── Primality Test │ ├── big number prime │ ├── prime.cpp │ └── sum of k prime num is = n or not ├── Sieve of Eratosthenes │ └── sieve.cpp ├── Total divisor sum │ └── total divisor sum.cpp ├── Total number of divisor │ ├── divisor.cpp │ ├── factor of factorial.cpp │ ├── total divisor.cpp │ └── total divisor2.cpp ├── binary exponentiation │ └── binary exp.cpp ├── binomial coefficient using modulo │ └── binomial coefficient.cpp ├── common divisor in two num │ ├── divisor n and divisible by k │ │ └── code.cpp │ ├── efficient solution │ │ └── common divisor.cpp │ ├── naive solution │ │ └── naive.cpp │ └── queries about numbers solution │ │ └── solution.cpp ├── equation solve │ └── simple way │ │ ├── copy code.cpp │ │ └── equation.cpp ├── extra │ └── sum of all num all digit product int range l to r ├── fibonacci finding │ └── fibonacci.cpp ├── matrix exponentiation │ └── matrix exponentiation.cpp ├── modulo arithmatics │ ├── arphas hard exam.cpp │ ├── finding modulo inverse.cpp │ └── modular gcd.cpp ├── primality test used algorithm │ ├── fermat primality test │ │ └── prime.cpp │ └── miller rabin primality test │ │ └── prime.cpp ├── prime factorization using sieve │ └── prime factor.cpp ├── prime factorzation │ └── prime fact.cpp ├── segmented sieve │ └── segmented sieve.cpp └── sum of consecutive number with neg and pos │ ├── pos and neg.cpp │ └── pos.cpp ├── README.md ├── Recursion ├── Delete Middle Element of a stack │ └── code.cpp ├── Factorial │ └── factorial.cpp ├── Fibonnacci.cpp │ └── code.cpp ├── Generate all balanced parentheses │ └── code.cpp ├── Josephus problem death in a circle │ └── code.cpp ├── KTh Symbol in grammar │ └── code.cpp ├── Letter case permutation │ └── code.cpp ├── Permutation with case change │ └── code.cpp ├── Print 1 to n │ ├── code.cpp │ └── print n to 1.cpp ├── Print N bit binary numbers there num of 1 is grater then 0 │ └── code.cpp ├── Print subsets │ └── code.cpp ├── permutation with spaces │ └── code.cpp ├── reverse Stack │ └── code.cpp ├── sort an array │ └── code.cpp ├── sort an stack │ └── code.cpp └── tower of hanio │ └── code.cpp ├── Sliding Window algo ├── First Negative Number in every window │ └── first negative.cpp ├── LS without reapeating cha │ └── ls without repeating cha.cpp ├── Largest subarray of sum k │ └── largest subarray of sum k.cpp ├── Longest substring with k unique characters │ └── longest substring with k unique characters.cpp ├── Maximum of all subarrays of size k │ └── maximum of all subarrays.cpp ├── Minimum Window Substring ├── count occurrences of anagrams │ └── count occurrences of anagrams.cpp ├── maximum sum subarray of size k │ └── maximum sum.cpp └── pick toys │ └── pick toys.cpp ├── Sort ├── Bubble sort │ └── code.cpp ├── Insersion sort │ └── code.cpp ├── four critaria sort │ └── sort.cpp ├── marge sort │ └── code.cpp └── selection sort │ └── code.cpp ├── Stack algorithm ├── Max Area Rectangle in binary matrix │ └── max area rectangle in bin mat.cpp ├── Maximum area histogram │ └── Maximum area histogram.cpp ├── Minimum element in stack with extra space │ └── minimum element extra space.cpp ├── Nearest Greater to left │ └── code.cpp ├── Nearest Greater to right │ └── code.cpp ├── Nearest Smaller to Right │ └── code.cpp ├── Nearest smaller to left │ └── code.cpp ├── Rain Water Trapping │ └── Rain water trapping.cpp └── Stock Span problem │ ├── code.cpp │ └── finding the medius.cpp ├── String Algorithm ├── Hash algorithm │ ├── Hash algorithm │ │ ├── code.cpp │ │ └── substring hash.cpp │ ├── NHAY - A Needle in the Haystack spoj │ │ └── code.cpp │ └── substring.c ├── KMP Algorithm │ ├── KMP.cpp │ ├── Number of occurences.cpp │ └── another kmp.cpp ├── Rabin-Karp algorithm │ └── code.cpp ├── Sliding windows │ └── Sliding windows1.cpp ├── Substring hashing in O(1) │ └── code.cpp ├── big integer multiplication └── big number sum using string │ └── code.cpp ├── Vule Gele Help Nao ├── All SubArray NON - Zero XOR └── all substring sum or all subarray sum ├── factorial related └── 100factorial.cpp ├── linkedlist ├── doubly linked list │ └── Doubly linked list.cpp └── make linklist │ └── make linklist.cpp ├── mathematics ├── Binary Search.c ├── Bubble sort.c ├── Delete an Element from an Array.c ├── Fibonacci Series using for loop.c ├── Find Minimum Element in an array.c ├── Linear Search of Multiple occurance.c ├── Linear Search.c ├── Maximum Element in an Array.c ├── Read an array and search an num.c ├── Reverse Array.c ├── Selection Sort.c ├── Tower of Hanoi.c ├── insert an Element in an Array.c └── insertion sort.c ├── prefixsum and diffrence array ├── 2 sumarray max sum ├── diffrence array.cpp └── prefix sum.cpp ├── segment tree ├── Lazy propagation │ ├── another new function.cpp │ ├── multiple of 3.cpp │ └── upadate and print sum of range.cpp ├── Range minimum query │ └── code.cpp ├── Reverse range minimum queary │ └── code.cpp ├── marge sort tree │ ├── l to r k greater value.cpp │ └── l to r k smallest value.cpp └── update point and count even or odd │ └── code.cpp └── trie ├── Lexicographic sorting.cpp ├── leetcodesearch.cpp ├── longest common prefix.cpp ├── maximum xor of two numbers in an array.cpp ├── practice.cpp ├── trie.cpp └── wordsearch.cpp /Binary search/Find Floor of an element/code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int floor(int ar[],int l,int r,int x) 21 | { 22 | int ans=0; 23 | while(l<=r) 24 | { 25 | int mid=(l+r)/2; 26 | if(ar[mid]==x) 27 | { 28 | ans=ar[mid]; 29 | break; 30 | } 31 | else if(ar[mid]>n; 44 | int i,ar[n+3]; 45 | for(i=0;i>ar[i]; 46 | int x; 47 | cin>>x; 48 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int cile(int ar[],int l,int r,int x) 21 | { 22 | int ans=-1; 23 | while(l<=r) 24 | { 25 | int mid=(l+r)/2; 26 | if(ar[mid]==x) 27 | { 28 | ans=ar[mid]; 29 | break; 30 | } 31 | else if(ar[mid]>x) 32 | { 33 | ans=ar[mid]; 34 | r=mid-1; 35 | } 36 | else l=mid+1; 37 | } 38 | return ans; 39 | } 40 | int main() 41 | { 42 | int n; 43 | cin>>n; 44 | int i,ar[n+3]; 45 | for(i=0;i>ar[i]; 46 | int q,x; 47 | cin>>q; 48 | while(q--) 49 | { 50 | cin>>x; 51 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int ar[10000]; 21 | int n; 22 | int binsearch(int x) 23 | { 24 | int res=-1; 25 | int l=0,r=n-1,mid; 26 | while(l<=r) 27 | { 28 | mid=(l+r)/2; 29 | if(x==ar[mid]) 30 | { 31 | res=mid; 32 | l=mid+1; 33 | } 34 | else if(x>ar[mid])l=mid+1; 35 | else r=mid-1; 36 | } 37 | return res; 38 | } 39 | int main() 40 | { 41 | cin>>n; 42 | for(int i=0;i>ar[i]; 43 | int q; 44 | cin>>q; 45 | while(q--) 46 | { 47 | int x; 48 | cin>>x; 49 | int ans=binsearch(x); 50 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int ar[10000]; 21 | int n; 22 | int binsearch(int x) 23 | { 24 | int res=-1; 25 | int l=0,r=n-1,mid; 26 | while(l<=r) 27 | { 28 | mid=(l+r)/2; 29 | if(x==ar[mid]) 30 | { 31 | res=mid; 32 | r=mid-1; 33 | } 34 | else if(x>ar[mid])l=mid+1; 35 | else r=mid-1; 36 | } 37 | return res; 38 | } 39 | int main() 40 | { 41 | cin>>n; 42 | for(int i=0;i>ar[i]; 43 | int q; 44 | cin>>q; 45 | while(q--) 46 | { 47 | int x; 48 | cin>>x; 49 | int ans=binsearch(x); 50 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define revsort(ar,s) sort(ar,ar+s,greater()) 16 | #define nl '\n' 17 | #define mod 1000000007 18 | typedef long long int ll; 19 | typedef unsigned long long int llu; 20 | 21 | int ar[10000]; 22 | int n; 23 | 24 | int binsearch(int x) 25 | { 26 | int l=0,r=n-1,mid; 27 | while(l<=r) 28 | { 29 | mid=(l+r)/2; 30 | if(x==ar[mid]) 31 | { 32 | cout<<"YES"<ar[mid])r=mid-1; 37 | else l=mid+1; 38 | } 39 | cout<<"NOT FOUND"<>n; 46 | for(int i=0;i>ar[i]; 47 | int q; 48 | revsort(ar,n); 49 | cin>>q; 50 | while(q--) 51 | { 52 | int x; 53 | cin>>x; 54 | binsearch(x); 55 | } 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Binary search/binary search/binary search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int ar[10000]; 21 | int n; 22 | int binsearch(int x) 23 | { 24 | int l=0,r=n-1,mid; 25 | while(l<=r) 26 | { 27 | mid=(l+r)/2; 28 | if(x==ar[mid]) 29 | { 30 | cout<<"YES"<ar[mid])l=mid+1; 35 | else r=mid-1; 36 | } 37 | cout<<"NOT FOund"<>n; 43 | for(int i=0;i>ar[i]; 44 | int q; 45 | cin>>q; 46 | while(q--) 47 | { 48 | int x; 49 | cin>>x; 50 | binsearch(x); 51 | } 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Binary search/next Alphabetical element.cpp/code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | char nextalpha(string s,int l,int r,char ch) 21 | { 22 | char ans='N'; 23 | while(l<=r) 24 | { 25 | int mid=(l+r)/2; 26 | if(s[mid]<=ch) 27 | { 28 | l=mid+1; 29 | } 30 | else 31 | { 32 | ans=s[mid]; 33 | r=mid-1; 34 | } 35 | } 36 | return ans; 37 | } 38 | int main() 39 | { 40 | string s; 41 | cin>>s; 42 | while(1) 43 | { 44 | char ch; 45 | cin>>ch; 46 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int countrot(int ar[],int l,int r) 21 | { 22 | int res=0; 23 | while(l<=r) 24 | { 25 | int mid=(l+r)/2; 26 | if(midl && ar[mid]ar[mid])r=mid-1; 37 | else l=mid+1; 38 | } 39 | return res; 40 | } 41 | 42 | int main() 43 | { 44 | int n; 45 | cin>>n; 46 | int ar[n+2]; 47 | for(int i=0;i>ar[i]; 50 | } 51 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int n,m; 21 | int ar[1000][1000]; 22 | 23 | void binsearch(int x) 24 | { 25 | int l,r; 26 | l=0,r=m-1; 27 | while(l>=0&&l=0) 28 | { 29 | if(ar[l][r]==x) 30 | { 31 | cout<<"The position is "<x)r--; 35 | else l++; 36 | } 37 | cout<<"The number is not found"<>n>>m; 44 | int i,j; 45 | for(i=0;i>ar[i][j]; 48 | } 49 | int x; 50 | cin>>x; 51 | binsearch(x); 52 | } 53 | -------------------------------------------------------------------------------- /Bit Manipulation/AND product.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | 21 | int main() 22 | { 23 | //problem lik: https://www.hackerrank.com/challenges/and-product/problem 24 | 25 | int n; 26 | cin>>n; 27 | while(n--) 28 | { 29 | ll a,b; 30 | ll i,ans; 31 | cin>>a>>b; 32 | ans=0; 33 | ll f=1; 34 | ll d=b-a; 35 | for(i=0;i<32;i++) 36 | { 37 | if(d>(f< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | 21 | int main() 22 | { 23 | int n; 24 | cin>>n; 25 | int i; 26 | int ans=2; 27 | for(i=2;i<=n;i++) 28 | { 29 | ans=ans^(i*2); 30 | } 31 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | 21 | int main() 22 | { 23 | //problem lik: https://www.hackerrank.com/challenges/and-product/problem 24 | int t; 25 | cin>>t; 26 | while(t--) 27 | { 28 | llu a,b,i,sum=0,f=1; 29 | cin>>a>>b; 30 | llu d=b-a; 31 | for(i=0;i<32;i++) 32 | { 33 | if(d>(f< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | 21 | int main() 22 | { 23 | int t; 24 | cin>>t; 25 | while(t--) 26 | { 27 | int i,n,f=1; 28 | cin>>n>>i; 29 | i--; 30 | f=f< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | 21 | int main() 22 | { 23 | //problem link: https://leetcode.com/problems/counting-bits/ 24 | 25 | int t; 26 | cin>>t; 27 | while(t--) 28 | { 29 | int n,cnt=0; 30 | cin>>n; 31 | while(n>0) 32 | { 33 | cnt++; 34 | n=n&(n-1); 35 | } 36 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | 21 | int main() 22 | { 23 | //problem link https: www.hackerrank.com/challenges/sansa-and-xor/problem 24 | 25 | ll t; 26 | cin>>t; 27 | while(t--) 28 | { 29 | ll n,i,j; 30 | cin>>n; 31 | ll ar[n+1]; 32 | for(i=1;i<=n;i++)cin>>ar[i]; 33 | ll ans=0; 34 | for(i=1;i<=n;i++) 35 | { 36 | ll x=i*(n-i+1); 37 | if(x%2==1)ans^=ar[i]; 38 | } 39 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | 21 | int main() 22 | { 23 | int t; 24 | cin>>t; 25 | while(t--) 26 | { 27 | int n; 28 | cin>>n; 29 | int i; 30 | int ans=0; 31 | for(i=0;i 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | 21 | int main() 22 | { 23 | int t; 24 | cin>>t; 25 | while(t--) 26 | { 27 | int n,i; 28 | cin>>n>>i; 29 | cout<<(n< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | 21 | int main() 22 | { 23 | int t; 24 | cin>>t; 25 | while(t--) 26 | { 27 | int n,i; 28 | cin>>n>>i; 29 | cout<<(n>>i)< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | 21 | int main() 22 | { 23 | //problem link https: www.hackerrank.com/challenges/sansa-and-xor/problem 24 | ll t; 25 | cin>>t; 26 | while(t--) 27 | { 28 | ll n,i,ans=0; 29 | cin>>n; 30 | ll ar[n]; 31 | for(i=0;i>ar[i]; 34 | if(((i+1)*(n-i))%2==1)ans=ans^ar[i]; 35 | } 36 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | 21 | int main() 22 | { 23 | //problem link: https://leetcode.com/problems/single-number-ii/ 24 | int n; 25 | cin>>n; 26 | int ar[n]; 27 | int i; 28 | mapm; 29 | //int i; 30 | for(i=0;i>ar[i]; 33 | m[ar[i]]++; 34 | } 35 | int ans; 36 | for(auto it: m) 37 | { 38 | //cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | 21 | int main() 22 | { 23 | faster 24 | int n; 25 | cin>>n; 26 | int i,ar[n+1]; 27 | for(i=0;i>ar[i]; 30 | } 31 | int j,ans=0; 32 | for(i=0;i<31;i++) 33 | { 34 | int one=0,zero=0; 35 | for(j=0;j 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | ll C(ll n,ll k) 21 | { 22 | ll ar[k+3],i,j; 23 | memset(ar,0,sizeof(ar)); 24 | ar[0]=1; 25 | for(i=1;i<=n;i++) 26 | { 27 | for(j=min(i,k);j>0;j--) 28 | { 29 | ar[j]=(ar[j]+ar[j-1])%mod; 30 | } 31 | } 32 | return ar[k]; 33 | } 34 | 35 | int main() 36 | { 37 | int t; 38 | cin>>t; 39 | //t=1; 40 | while(t--) 41 | { 42 | ll n,k; 43 | cin>>n>>k; 44 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | ll C(ll n,ll k) 21 | { 22 | ll ans=1; 23 | if(k>n-k)k=n-k; 24 | 25 | for(ll i=1;i<=k;i++) 26 | { 27 | ans*=(n-i+1); 28 | ans/=i; 29 | } 30 | return ans; 31 | } 32 | 33 | int main() 34 | { 35 | int t; 36 | cin>>t; 37 | while(t--) 38 | { 39 | ll n,k; 40 | cin>>n>>k; 41 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | ll C(ll n,ll k) 21 | { 22 | ll ar[k+3],i,j; 23 | memset(ar,0,sizeof(ar)); 24 | ar[0]=1; 25 | for(i=1;i<=n;i++) 26 | { 27 | for(j=min(i,k);j>0;j--) 28 | { 29 | ar[j]=(ar[j]+ar[j-1])%mod; 30 | } 31 | } 32 | return ar[k]; 33 | } 34 | 35 | int main() 36 | { 37 | int t; 38 | cin>>t; 39 | while(t--) 40 | { 41 | ll n,k; 42 | cin>>n>>k; 43 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | ll C(ll n,ll k) 21 | { 22 | ll ans=1; 23 | if(k>n-k)k=n-k; 24 | 25 | for(ll i=1;i<=k;i++) 26 | { 27 | ans*=(n-i+1); 28 | ans/=i; 29 | } 30 | return ans; 31 | } 32 | 33 | int main() 34 | { 35 | int t; 36 | cin>>t; 37 | while(t--) 38 | { 39 | ll n,k; 40 | cin>>n>>k; 41 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | ll C(ll n,ll k) 21 | { 22 | ll ans=1; 23 | if(k>n-k)k=n-k; 24 | 25 | for(ll i=1;i<=k;i++) 26 | { 27 | ans*=(n-i+1); 28 | ans/=i; 29 | } 30 | return ans; 31 | } 32 | 33 | int main() 34 | { 35 | int t; 36 | cin>>t; 37 | while(t--) 38 | { 39 | ll n,k; 40 | cin>>n>>k; 41 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int P[100001],R[100001]; 20 | 21 | int finds(int a) 22 | { 23 | if(P[a]==-1)return a; 24 | return P[a]=finds(P[a]); 25 | } 26 | void marge(int a,int b) 27 | { 28 | a=finds(a); 29 | b=finds(b); 30 | if(a==b)return; 31 | 32 | if(R[a]>R[b]) 33 | { 34 | P[b]=a; 35 | R[a]+=R[b]; 36 | } 37 | else{ 38 | P[a]=b; 39 | R[b]+=R[a]; 40 | } 41 | } 42 | int main() 43 | { 44 | int n,m,u,v; 45 | cin>>n>>m; 46 | for(int i=1;i<=n;i++) 47 | { 48 | P[i]=-1; 49 | R[i]=1; 50 | } 51 | while(m--) 52 | { 53 | cin>>u>>v; 54 | marge(u,v); 55 | } 56 | for(int i=1;i<=n;i++) 57 | { 58 | int x=finds(i); 59 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int par[100001]; 20 | int finds(int a) 21 | { 22 | if(par[a]<0)return a; 23 | else{ 24 | return par[a]=finds(par[a]); 25 | } 26 | } 27 | int unions(int a,int b) 28 | { 29 | par[a]=min(par[a],par[b]); 30 | par[b]=a; 31 | } 32 | int main() 33 | { 34 | int n,m,x,y,a,b,i,q; 35 | cin>>n>>m; 36 | for(i=1;i<=n;i++)par[i]=-i; 37 | for(i=1;i<=m;i++) 38 | { 39 | cin>>a>>b; 40 | a=finds(a); 41 | b=finds(b); 42 | if(a!=b)unions(a,b); 43 | } 44 | cin>>q; 45 | while(q--) 46 | { 47 | cin>>x>>y; 48 | a=x,b=y; 49 | a=finds(a); 50 | b=finds(b); 51 | if(a==b)cout<<"TIE"< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int fibonacci(int n) 21 | { 22 | if(n==0)return 0; 23 | if(n==1)return 1; 24 | return fibonacci(n-1)+fibonacci(n-2); 25 | } 26 | 27 | int main() 28 | { 29 | faster 30 | int n; 31 | cin>>n; 32 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int mat[10000]; 20 | int fibonacci(int n) 21 | { 22 | if(n==0)return 0; 23 | if(n==1)return 1; 24 | if(mat[n]!=-1)return mat[n]; 25 | mat[n]=fibonacci(n-1)+fibonacci(n-2); 26 | return mat[n]; 27 | } 28 | int main() 29 | { 30 | faster 31 | int t; 32 | cin>>t; 33 | while(t--) 34 | { 35 | memset(mat,-1,sizeof(mat)); 36 | int n; 37 | cin>>n; 38 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | #define mod 1000000007 20 | ll fibonacci(ll a,ll b,ll n) 21 | { 22 | ll mat[n+1]; 23 | ll i; 24 | mat[0]=a%mod; 25 | mat[1]=b%mod; 26 | for(i=2;i<=n;i++) 27 | { 28 | mat[i]=mat[i-1]%mod+mat[i-2]%mod; 29 | } 30 | return mat[n]%mod; 31 | } 32 | 33 | int main() 34 | { 35 | //faster 36 | ll t; 37 | cin>>t; 38 | while(t--) 39 | { 40 | ll a,b; 41 | ll n; 42 | cin>>a>>b>>n; 43 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int t[100][1000]; 20 | 21 | int knapsack(int w,int wt[],int val[],int n) 22 | { 23 | if(n==0||w==0)return 0; 24 | if(t[n][w]!=-1)return t[n][w]; 25 | if(wt[n]>w) 26 | { 27 | return t[n][w]=knapsack(w,wt,val,n-1); 28 | } 29 | else 30 | { 31 | return t[n][w] = max(val[n]+knapsack(w-wt[n],wt,val,n-1),knapsack(w,wt,val,n-1)); 32 | } 33 | } 34 | int main() 35 | { 36 | faster 37 | int n; 38 | cin>>n; 39 | int wt[n+1],val[n+1],i,j,w; 40 | for(i=0;i>wt[i]; 41 | for(i=0;i>val[i]; 42 | cin>>w; 43 | memset(t,-1,sizeof(t)); 44 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | bool subproblem(int ar[],int n,int sum) 21 | { 22 | bool sub[n+1][sum+1]; 23 | int i,j; 24 | for(i=0;i<=n;i++) 25 | { 26 | for(j=0;j<=sum;j++) 27 | { 28 | if(i==0)sub[i][j]=false; 29 | if(j==0)sub[i][j]=true; 30 | if(ar[i-1]<=j) 31 | { 32 | sub[i][j]=sub[i-1][j] || sub[i-1][j-ar[i-1]]; 33 | } 34 | else sub[i][j]=sub[i-1][j]; 35 | } 36 | } 37 | return sub[n][sum]; 38 | } 39 | 40 | int main() 41 | { 42 | faster; 43 | int n; 44 | cin>>n; 45 | int i,ar[n+1],sum=0; 46 | for(i=0;i>ar[i]; 49 | sum+=ar[i]; 50 | } 51 | if(sum%2==1)cout<<"Not possible"< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int t[100][1000]; 20 | 21 | int napsack(int w,int wt[],int val[],int n) 22 | { 23 | if(n==0||w==0)return 0; 24 | if(t[n][w]!=-1)return t[n][w]; 25 | if(wt[n]>w) 26 | { 27 | return t[n][w]=napsack(w,wt,val,n-1); 28 | } 29 | else{ 30 | return t[n][w]=max(val[n]+napsack(w-wt[n],wt,val,n-1),napsack(w,wt,val,n-1)); 31 | } 32 | } 33 | 34 | int main() 35 | { 36 | faster 37 | int n; 38 | cin>>n; 39 | int i,wt[n+1],val[n+1],w; 40 | for(i=0;i>wt[i]; 41 | for(i=0;i>val[i]; 42 | cin>>w; 43 | memset(t,-1,sizeof(t)); 44 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int napsack(int w,int wt[],int val[],int n) 21 | { 22 | if(n==0||w==0)return 0; 23 | if(wt[n]>w)return napsack(w,wt,val,n-1); 24 | else 25 | { 26 | return max(val[n]+napsack(w-wt[n],wt,val,n-1),napsack(w,wt,val,n-1)); 27 | } 28 | } 29 | 30 | int main() 31 | { 32 | faster 33 | int n; 34 | cin>>n; 35 | int i,j,wt[n+1],val[n+1],w; 36 | for(i=0;i>wt[i]; 37 | for(i=0;i>val[i]; 38 | cin>>w; 39 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | bool subset(int ar[],int n,int sum) 21 | { 22 | bool sub[n+1][sum+1]; 23 | int i,j; 24 | for(i=0;i<=n;i++) 25 | { 26 | for(j=0;j<=n;j++) 27 | { 28 | if(i==0)sub[i][j]=false; 29 | if(j==0)sub[i][j]=true; 30 | } 31 | } 32 | for(i=1;i<=n;i++) 33 | { 34 | for(j=1;j<=sum;j++) 35 | { 36 | if(ar[i-1]<=j) 37 | { 38 | //cout<>n; 54 | int i,ar[n+1]; 55 | for(i=0;i>ar[i]; 56 | int sum; 57 | cin>>sum; 58 | bool x = subset(ar,n,sum); 59 | if(x==true)cout<<"i am here"< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int knapsack(int w,int wt[],int val[],int n) 21 | { 22 | int i,j; 23 | int k[n+1][w+1]; 24 | for(i=0;i<=n;i++)k[i][0]=0; 25 | for(i=1;i<=w;i++)k[0][i]=0; 26 | for(i=1;i<=n;i++) 27 | { 28 | for(j=1;j<=w;j++) 29 | { 30 | if(wt[i-1]<=j) 31 | { 32 | k[i][j]=max((val[i-1]+k[i-1][j-wt[i-1]]),(k[i-1][j])); 33 | } 34 | else{ 35 | k[i][j]=k[i-1][j]; 36 | } 37 | } 38 | } 39 | return k[n][w]; 40 | } 41 | 42 | int main() 43 | { 44 | faster 45 | int n; 46 | cin>>n; 47 | int wt[n+1],val[n+1],i,j,w; 48 | for(i=0;i>wt[i]; 49 | for(i=0;i>val[i]; 50 | cin>>w; 51 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int cntsubset(int ar[],int n,int sum) 21 | { 22 | int i,j,subset[n+1][sum+1]; 23 | for(i=0;i<=n;i++)subset[i][0]=1; 24 | for(i=1;i<=sum;i++)subset[0][i]=0; 25 | for(i=1;i<=n;i++) 26 | { 27 | for(j=1;j<=sum;j++) 28 | { 29 | if(j>n; 43 | int ar[n+1],i,sum=0,dif; 44 | for(i=0;i>ar[i]; 47 | sum+=ar[i]; 48 | } 49 | cin>>dif; 50 | if((dif+sum)%2==1)cout<<0< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int countsubset(int ar[],int n,int sum) 21 | { 22 | int i,j,subset[n+1][sum+1]; 23 | for(i=0;i<=n;i++) 24 | { 25 | subset[i][0]=1; 26 | } 27 | for(i=1;i<=sum;i++) 28 | { 29 | subset[0][i]=0; 30 | } 31 | for(i=1;i<=n;i++) 32 | { 33 | for(j=1;j<=sum;j++) 34 | { 35 | if(j>n; 50 | int ar[n+1],i,sum; 51 | for(i=0;i>ar[i]; 52 | cin>>sum; 53 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | bool subsetsum(int ar[],int n,int sum) 21 | { 22 | int i,j; 23 | bool subset[n+1][sum+1]; 24 | for(i=0;i<=n;i++) 25 | { 26 | subset[i][0]=true; 27 | } 28 | for(i=1;i<=sum;i++)subset[0][i]=false; 29 | for(i=1;i<=n;i++) 30 | { 31 | for(j=1;j<=sum;j++) 32 | { 33 | if(j=ar[i-1])subset[i][j]=subset[i-1][j]||subset[i-1][j-ar[i-1]]; 35 | } 36 | } 37 | return subset[n][sum]; 38 | } 39 | 40 | int main() 41 | { 42 | faster 43 | int n; 44 | cin>>n; 45 | int i,ar[n+1],sum=0; 46 | for(i=0;i>ar[i]; 49 | sum+=ar[i]; 50 | } 51 | //cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | bool subsetsum(int ar[],int n,int sum) 21 | { 22 | bool subset[n+1][sum+1]; 23 | int i,j; 24 | for(i=0;i<=n;i++) 25 | { 26 | subset[i][0]=true; 27 | } 28 | for(i=1;i<=sum;i++) 29 | { 30 | subset[0][i]=false; 31 | } 32 | for(i=1;i<=n;i++) 33 | { 34 | for(j=1;j<=sum;j++) 35 | { 36 | if(j=ar[i-1]){ 41 | subset[i][j]=subset[i-1][j]||subset[i-1][j-ar[i-1]]; 42 | } 43 | cout<>n; 55 | int ar[n+1],i,sum; 56 | for(i=0;i>ar[i]; 57 | cin>>sum; 58 | bool x; 59 | x=subsetsum(ar,n,sum); 60 | if(x==true)cout<<"Yes"< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | bool subsetsum(int ar[],int n,int sum) 21 | { 22 | if(sum==0)return true; 23 | if(n==0)return false; 24 | if(ar[n-1]>sum)return subsetsum(ar,n-1,sum); 25 | else{ 26 | return subsetsum(ar,n-1,sum)||subsetsum(ar,n-1,sum-ar[n-1]); 27 | } 28 | } 29 | 30 | int main() 31 | { 32 | faster 33 | int n; 34 | cin>>n; 35 | int ar[n+1],i,j,sum; 36 | for(i=0;i>ar[i]; 37 | cin>>sum; 38 | bool x; 39 | x=subsetsum(ar,n,sum); 40 | if(x==true)cout<<"Yes"< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int cntsubset(int ar[],int n,int sum) 21 | { 22 | int i,j,subset[n+1][sum+1]; 23 | for(i=0;i<=n;i++)subset[i][0]=1; 24 | for(i=1;i<=sum;i++)subset[0][i]=0; 25 | for(i=1;i<=n;i++) 26 | { 27 | for(j=1;j<=sum;j++) 28 | { 29 | if(j>n; 43 | int ar[n+1],i,sum=0,dif; 44 | for(i=0;i>ar[i]; 47 | sum+=ar[i]; 48 | } 49 | cin>>dif; 50 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int lps(string a,string b,int n) 21 | { 22 | int i,j,dp[n+1][n+1]; 23 | for(i=0;i<=n;i++)dp[i][0]=0; 24 | for(i=1;i<=n;i++)dp[0][i]=0; 25 | for(i=1;i<=n;i++) 26 | { 27 | for(j=1;j<=n;j++) 28 | { 29 | if(a[i-1]==b[j-1])dp[i][j]=1+dp[i-1][j-1]; 30 | else 31 | { 32 | dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 33 | } 34 | } 35 | } 36 | return dp[n][n]; 37 | } 38 | 39 | int main() 40 | { 41 | faster 42 | string a,b; 43 | cin>>a; 44 | b=a; 45 | reverse(b.begin(),b.end()); 46 | int n=a.size(); 47 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int LCM(string a,string b,int n,int m) 21 | { 22 | int i,j; 23 | int T[n+1][m+1]; 24 | for(i=0;i<=n;i++) 25 | { 26 | for(j=0;j<=m;j++) 27 | { 28 | if(i==0||j==0)T[i][j]=0; 29 | } 30 | } 31 | for(i=1;i<=n;i++) 32 | { 33 | for(j=1;j<=m;j++) 34 | { 35 | //if(i==0||j==0)T[i][j]=0; 36 | if(a[i-1]==b[j-1] && i!=j)T[i][j]=1+T[i-1][j-1]; 37 | else{ 38 | T[i][j]=max(T[i-1][j],T[i][j-1]); 39 | } 40 | } 41 | } 42 | return T[n][m]; 43 | 44 | } 45 | 46 | int main() 47 | { 48 | //faster 49 | int t; 50 | cin>>t; 51 | while(t--) 52 | { 53 | string a,b; 54 | cin>>a; 55 | b=a; 56 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int mi(string a,string b,int n,int m) 21 | { 22 | int i,j,dp[n+1][m+1]; 23 | for(i=0;i<=n;i++)dp[i][0]=0; 24 | for(i=1;i<=m;i++)dp[0][i]=0; 25 | for(i=1;i<=n;i++) 26 | { 27 | for(j=1;j<=m;j++) 28 | { 29 | if(a[i-1]==b[j-1])dp[i][j]=1+dp[i-1][j-1]; 30 | else 31 | { 32 | dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 33 | } 34 | } 35 | } 36 | return n-dp[n][m]+m-dp[n][m]; 37 | } 38 | 39 | int main() 40 | { 41 | faster; 42 | string a,b; 43 | int n,m; 44 | cin>>a>>b; 45 | n=a.size(); 46 | m=b.size(); 47 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int lps(string a,string b,int n) 21 | { 22 | int i,j,dp[n+1][n+1]; 23 | for(i=0;i<=n;i++)dp[i][0]=0; 24 | for(i=1;i<=n;i++)dp[0][i]=0; 25 | for(i=1;i<=n;i++) 26 | { 27 | for(j=1;j<=n;j++) 28 | { 29 | if(a[i-1]==b[j-1])dp[i][j]=1+dp[i-1][j-1]; 30 | else 31 | { 32 | dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 33 | } 34 | } 35 | } 36 | return n-dp[n][n]; 37 | } 38 | 39 | int main() 40 | { 41 | faster 42 | string a,b; 43 | cin>>a; 44 | b=a; 45 | reverse(b.begin(),b.end()); 46 | int n=a.size(); 47 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int mni(string a,string b,int n) 21 | { 22 | int i,j,dp[n+1][n+1]; 23 | for(i=0;i<=n;i++) 24 | { 25 | dp[i][0]=0; 26 | } 27 | for(i=1;i<=n;i++)dp[0][i]=0; 28 | for(i=1;i<=n;i++) 29 | { 30 | for(j=1;j<=n;j++) 31 | { 32 | if(a[i-1]==b[j-1])dp[i][j]=1+dp[i-1][j-1]; 33 | else{ 34 | dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 35 | } 36 | } 37 | } 38 | return n-dp[n][n]; 39 | } 40 | 41 | int main() 42 | { 43 | faster 44 | string a,b; 45 | cin>>a; 46 | b=a; 47 | int n; 48 | n=a.size(); 49 | reverse(b.begin(),b.end()); 50 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | bool spm(string a,string b,int n,int m) 21 | { 22 | int i,j,dp[n+1][m+1]; 23 | for(i=0;i<=n;i++) 24 | { 25 | dp[i][0]=0; 26 | } 27 | for(i=1;i<=m;i++)dp[0][i]=0; 28 | for(i=1;i<=n;i++) 29 | { 30 | for(j=1;j<=m;j++) 31 | { 32 | if(a[i-1]==b[j-1])dp[i][j]=1+dp[i-1][j-1]; 33 | else{ 34 | dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 35 | } 36 | } 37 | } 38 | if(dp[n][m]==n)return true; 39 | else return false; 40 | } 41 | 42 | int main() 43 | { 44 | faster 45 | string a,b; 46 | cin>>a>>b; 47 | int x,y; 48 | x=a.size(); 49 | y=b.size(); 50 | bool ans=spm(a,b,x,y); 51 | if(ans==true)cout<<"YES"< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int LCM(string a,string b,int n,int m) 21 | { 22 | int i,j; 23 | int T[n+1][m+1]; 24 | for(i=0;i<=n;i++) 25 | { 26 | for(j=0;j<=m;j++) 27 | { 28 | if(i==0||j==0)T[i][j]=0; 29 | } 30 | } 31 | for(i=1;i<=n;i++) 32 | { 33 | for(j=1;j<=m;j++) 34 | { 35 | //if(i==0||j==0)T[i][j]=0; 36 | if(a[i-1]==b[j-1])T[i][j]=1+T[i-1][j-1]; 37 | else{ 38 | T[i][j]=max(T[i-1][j],T[i][j-1]); 39 | } 40 | } 41 | } 42 | return T[n][m]; 43 | } 44 | 45 | int main() 46 | { 47 | //faster 48 | int t; 49 | cin>>t; 50 | while(t--) 51 | { 52 | string a,b; 53 | cin>>a>>b; 54 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int T[1000][1000]; 20 | 21 | int LCM(string a,string b,int n,int m) 22 | { 23 | if(n==0||m==0)return 0; 24 | if(T[n][m]!=-1)return T[n][m]; 25 | if(a[n-1]==b[m-1]) 26 | { 27 | return T[n][m]=1+LCM(a,b,n-1,m-1); 28 | } 29 | else{ 30 | return T[n][m]=max(LCM(a,b,n-1,m),LCM(a,b,n,m-1)); 31 | } 32 | } 33 | 34 | int main() 35 | { 36 | //faster 37 | int t; 38 | cin>>t; 39 | while(t--) 40 | { 41 | memset(T,-1,sizeof(T)); 42 | string a,b; 43 | cin>>a>>b; 44 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int LCM(string a,string b,int n,int m) 21 | { 22 | if(n==0||m==0)return 0; 23 | if(a[n-1]==b[m-1])return 1+LCM(a,b,n-1,m-1); 24 | else{ 25 | return max(LCM(a,b,n-1,m),LCM(a,b,n,m-1)); 26 | } 27 | } 28 | int main() 29 | { 30 | //faster 31 | int t; 32 | cin>>t; 33 | while(t--) 34 | { 35 | string a,b; 36 | cin>>a>>b; 37 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int lcs(string a,string b,int n,int m) 21 | { 22 | int i,j,dp[n+1][m+1]; 23 | for(i=0;i<=n;i++)dp[i][0]=0; 24 | for(i=0;i<=m;i++)dp[0][i]=0; 25 | int result=0; 26 | for(i=1;i<=n;i++) 27 | { 28 | for(j=1;j<=m;j++) 29 | { 30 | if(a[i-1]==b[j-1]) 31 | { 32 | dp[i][j]=1+dp[i-1][j-1]; 33 | result=max(result,dp[i][j]); 34 | } 35 | else dp[i][j]=0; 36 | } 37 | } 38 | return result; 39 | } 40 | 41 | int main() 42 | { 43 | //faster 44 | int t; 45 | cin>>t; 46 | while(t--) 47 | { 48 | string a,b; 49 | cin>>a>>b; 50 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int scs(string a,string b,int x,int y) 21 | { 22 | int i,j,dp[x+1][y+1]; 23 | for(i=0;i<=x;i++)dp[i][0]=0; 24 | for(i=1;i<=y;i++)dp[0][i]=0; 25 | for(i=1;i<=x;i++) 26 | { 27 | for(j=1;j<=y;j++) 28 | { 29 | if(a[i-1]==b[j-1])dp[i][j]=1+dp[i-1][j-1]; 30 | else 31 | { 32 | dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 33 | } 34 | } 35 | } 36 | cout<>a>>b; 46 | x=a.size(); 47 | y=b.size(); 48 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | int dp[1000][1000]; 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int solve(int e,int f) 21 | { 22 | if(f==0||f==1)return f; 23 | if(e==1)return f; 24 | if(dp[e][f]!=-1)return dp[e][f]; 25 | int mn=INT_MAX; 26 | for(int k=1;k<=f;k++) 27 | { 28 | int a,b; 29 | if(dp[e-1][k-1]!=-1) 30 | { 31 | a=dp[e-1][k-1]; 32 | } 33 | else 34 | { 35 | a=solve(e-1,k-1); 36 | dp[e-1][k-1]=a; 37 | } 38 | if(dp[e][f-k]!=-1) 39 | { 40 | b=dp[e][f-k]; 41 | } 42 | else{ 43 | b=solve(e,f-k); 44 | dp[e][f-k]=b; 45 | } 46 | int temp=1+max(a,b); 47 | mn=min(mn,temp); 48 | } 49 | return dp[e][f]=mn; 50 | } 51 | 52 | int main() 53 | { 54 | int t; 55 | cin>>t; 56 | while(t--) 57 | { 58 | memset(dp,-1,sizeof(dp)); 59 | int egg,floor; 60 | cin>>egg>>floor; 61 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | int dp[1000][1000]; 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int solve(int e,int f) 21 | { 22 | if(f==0||f==1)return f; 23 | if(e==1)return f; 24 | if(dp[e][f]!=-1)return dp[e][f]; 25 | int mn=INT_MAX; 26 | for(int k=1;k<=f;k++) 27 | { 28 | int temp=1+max(solve(e-1,k-1),solve(e,f-k)); 29 | mn=min(mn,temp); 30 | } 31 | return dp[e][f]=mn; 32 | } 33 | 34 | int main() 35 | { 36 | int t; 37 | cin>>t; 38 | while(t--) 39 | { 40 | memset(dp,-1,sizeof(dp)); 41 | int egg,floor; 42 | cin>>egg>>floor; 43 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int solve(int e,int f) 21 | { 22 | if(f==0||f==1)return f; 23 | if(e==1)return f; 24 | int mn=INT_MAX; 25 | for(int k=1;k<=f;k++) 26 | { 27 | int temp=1+max(solve(e-1,k-1),solve(e,f-k)); 28 | mn=min(mn,temp); 29 | } 30 | return mn; 31 | } 32 | 33 | int main() 34 | { 35 | int t; 36 | cin>>t; 37 | while(t--) 38 | { 39 | int egg,floor; 40 | cin>>egg>>floor; 41 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int dp[1000][1000]; 20 | bool ispalindrome(string s,int i, int j) 21 | { 22 | while(i=j)return 0; 36 | if(ispalindrome(s,i,j)==true)return 0; 37 | if(dp[i][j]!=-1)return dp[i][j]; 38 | int k,mn=INT_MAX; 39 | for(k=i;k>a; 52 | int i,j; 53 | i=0,j=a.size()-1; 54 | memset(dp,-1,sizeof(dp)); 55 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int T[1001][1001]; 20 | int mcm(int ar[],int i,int j) 21 | { 22 | if(i>=j)return 0; 23 | if(T[i][j]!=-1)return T[i][j]; 24 | int k; 25 | int mn=INT_MAX; 26 | int cnt; 27 | for(k=i;k>n; 40 | int i,ar[n+1]; 41 | memset(T,-1,sizeof(T)); 42 | for(i=0;i>ar[i]; 43 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | //int mn=INT_MAX; 21 | int matrixchain(int ar[],int i,int j) 22 | { 23 | if(i>=j)return 0; 24 | int k; 25 | int mn=INT_MAX; 26 | int cnt; 27 | for(k=i;k>n; 40 | int ar[n+1],i,j; 41 | for(i=0;i>ar[i]; 42 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int coinchange(int ar[],int n,int sum) 21 | { 22 | int i,j; 23 | int dp[n+1][sum+1]; 24 | for(i=0;i<=n;i++)dp[i][0]=1; 25 | for(i=1;i<=sum;i++)dp[0][i]=0; 26 | for(i=1;i<=n;i++) 27 | { 28 | for(j=1;j<=sum;j++) 29 | { 30 | if(j>n; 44 | int ar[n+1],i,sum; 45 | for(i=0;i>ar[i]; 46 | cin>>sum; 47 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int mincoin(int ar[],int n,int sum) 21 | { 22 | int i,j,dp[n+1][sum+1]; 23 | for(i=0;i<=sum;i++)dp[0][i]=INT_MAX; 24 | for(i=1;i<=n;i++)dp[i][0]=0; 25 | for(i=1;i<=sum;i++) 26 | { 27 | if(i%ar[0]==0)dp[1][i]=i/ar[0]; 28 | else dp[1][i]=INT_MAX-1; 29 | } 30 | for(i=2;i<=n;i++) 31 | { 32 | for(j=1;j<=sum;j++) 33 | { 34 | if(j>=ar[i-1]) 35 | { 36 | dp[i][j]=min(1+dp[i][j-ar[i-1]],dp[i-1][j]); 37 | } 38 | else 39 | { 40 | dp[i][j]=dp[i-1][j]; 41 | } 42 | } 43 | } 44 | return dp[n][sum]; 45 | } 46 | 47 | int main() 48 | { 49 | faster 50 | int n; 51 | cin>>n; 52 | int i,ar[n]; 53 | for(i=0;i>ar[i]; 54 | int sum; 55 | cin>>sum; 56 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int knapsack(int w,int wt[],int val[],int n) 21 | { 22 | int i,j; 23 | int k[n+1][w+1]; 24 | for(i=0;i<=n;i++)k[i][0]=0; 25 | for(i=1;i<=w;i++)k[0][i]=0; 26 | for(i=1;i<=n;i++) 27 | { 28 | for(j=1;j<=w;j++) 29 | { 30 | if(wt[i-1]<=j) 31 | { 32 | k[i][j]=max((val[i-1]+k[i-1][j-wt[i-1]]),(k[i-1][j])); 33 | } 34 | else{ 35 | k[i][j]=k[i-1][j]; 36 | } 37 | } 38 | } 39 | return k[n][w]; 40 | } 41 | 42 | int main() 43 | { 44 | faster 45 | int n; 46 | cin>>n; 47 | int wt[n+1],val[n+1],i,j,w; 48 | for(i=0;i>wt[i]; 49 | for(i=0;i>val[i]; 50 | cin>>w; 51 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int knapsack(int w,int wt[],int val[],int n) 21 | { 22 | int i,j; 23 | int k[n+1][w+1]; 24 | for(i=0;i<=n;i++)k[i][0]=0; 25 | for(i=1;i<=w;i++)k[0][i]=0; 26 | for(i=1;i<=n;i++) 27 | { 28 | for(j=1;j<=w;j++) 29 | { 30 | if(wt[i-1]<=j) 31 | { 32 | k[i][j]=max((val[i-1]+k[i][j-wt[i-1]]),(k[i-1][j])); 33 | } 34 | else{ 35 | k[i][j]=k[i-1][j]; 36 | } 37 | } 38 | } 39 | return k[n][w]; 40 | } 41 | 42 | int main() 43 | { 44 | faster 45 | int n; 46 | cin>>n; 47 | int wt[n+1],val[n+1],i,j,w; 48 | for(i=0;i>wt[i]; 49 | for(i=0;i>val[i]; 50 | cin>>w; 51 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int n,m; 20 | int vis[1001][1001]; 21 | bool isvalid(int x,int y) 22 | { 23 | if(x<1||x>n||y<1||y>m)return false; 24 | if(vis[x][y]==1)return false; 25 | return true; 26 | } 27 | int dx[]={-1,0,1,0}; 28 | int dy[]={0,1,0,-1}; 29 | void dfs(int x,int y) 30 | { 31 | vis[x][y]=1; 32 | cout<>n>>m; 45 | dfs(1,1); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Graph/Graph part 1/2D GRID/DFS 2D GRID/connected component/connected component.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int n,m; 20 | int vis[1001][1001],ar[1001][1001]; 21 | bool isvalid(int x,int y) 22 | { 23 | if(x<1||x>n||y<1||y>m)return false; 24 | if(vis[x][y]==1||ar[x][y]==0)return false; 25 | return true; 26 | } 27 | int dx[]={-1,0,1,0}; 28 | int dy[]={0,1,0,-1}; 29 | void dfs(int x,int y) 30 | { 31 | vis[x][y]=1; 32 | //cout<>n>>m; 45 | int result=0; 46 | for(int i=1;i<=n;i++) 47 | { 48 | for(int j=1;j<=m;j++) 49 | { 50 | cin>>ar[i][j]; 51 | } 52 | } 53 | for(int i=1;i<=n;i++) 54 | { 55 | for(int j=1;j<=m;j++) 56 | { 57 | if(vis[i][j]==0&&ar[i][j]==1) 58 | { 59 | result++; 60 | dfs(i,j); 61 | } 62 | } 63 | } 64 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int vis[10001],dis[10001]; 21 | vectorv[10001]; 22 | void bfs(int node) 23 | { 24 | queueq; 25 | q.push(node); 26 | vis[node]=1; 27 | dis[node]=0; 28 | while(!q.empty()) 29 | { 30 | int a=q.front(); 31 | q.pop(); 32 | for(int child: v[a]) 33 | { 34 | if(vis[child]==0) 35 | { 36 | dis[child]=dis[a]+1; 37 | vis[child]=1; 38 | q.push(child); 39 | } 40 | } 41 | } 42 | } 43 | int main() 44 | { 45 | int n,m,i,x,y; 46 | cin>>n>>m; 47 | while(m--) 48 | { 49 | cin>>x>>y; 50 | v[x].push_back(y); 51 | v[y].push_back(x); 52 | } 53 | bfs(1); 54 | for(i=1;i<=n;i++) 55 | { 56 | cout<<"The level of "< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int mp[10001]; 20 | vectorv[10001]; 21 | vectorans; 22 | void dfs(int n) 23 | { 24 | queueq; 25 | for(int i=1;i<=n;i++) 26 | { 27 | if(mp[i]==0)q.push(i); 28 | } 29 | while(!q.empty()) 30 | { 31 | int cun=q.front(); 32 | q.pop(); 33 | ans.push_back(cun); 34 | for(int child: v[cun]) 35 | { 36 | mp[child]--; 37 | if(mp[child]==0) 38 | { 39 | q.push(child); 40 | } 41 | } 42 | } 43 | cout<<"The topsort is: "<>n>>m; 54 | while(m--) 55 | { 56 | cin>>x>>y; 57 | v[x].push_back(y); 58 | mp[y]++; 59 | } 60 | dfs(n); 61 | } 62 | -------------------------------------------------------------------------------- /Graph/Graph part 1/DFS/Bipartite graph.cpp/bipartite.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int vis[1000],col[1000]; 21 | vectorv[1000]; 22 | 23 | bool dfs(int node,int c) 24 | { 25 | vis[node]=1; 26 | col[node]=c; 27 | for(int child: v[node]) 28 | { 29 | if(vis[child]==0) 30 | { 31 | if(dfs(child,c^1)==false) 32 | { 33 | return false; 34 | } 35 | } 36 | else 37 | { 38 | if(col[node]==col[child]) 39 | { 40 | return false; 41 | } 42 | } 43 | } 44 | return true; 45 | } 46 | 47 | int main() 48 | { 49 | int n,m,x,y,i; 50 | cin>>n>>m; 51 | while(m--) 52 | { 53 | cin>>x>>y; 54 | v[x].push_back(y); 55 | v[y].push_back(x); 56 | } 57 | bool ans=dfs(1,1); 58 | if(ans==true)cout<<"the graph is bipartite"< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int vis[1000]; 20 | vectorv[10000]; 21 | void dfs(int node) 22 | { 23 | vis[node]=1; 24 | cout<"; 25 | for(int child: v[node]) 26 | { 27 | if(vis[child]==0) 28 | { 29 | dfs(child); 30 | } 31 | } 32 | } 33 | int main() 34 | { 35 | int n,m,i; 36 | cin>>n>>m; 37 | int x,y; 38 | while(m--) 39 | { 40 | cin>>x>>y; 41 | v[x].push_back(y); 42 | v[y].push_back(x); 43 | } 44 | for(i=1;i<=n;i++) 45 | { 46 | if(vis[i]==0) 47 | { 48 | dfs(i); 49 | } 50 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int vis[1000]; 20 | vectorv[10000]; 21 | void dfs(int node) 22 | { 23 | vis[node]=1; 24 | //cout<"; 25 | for(int child: v[node]) 26 | { 27 | if(vis[child]==0) 28 | { 29 | dfs(child); 30 | } 31 | } 32 | } 33 | int main() 34 | { 35 | int n,m,i; 36 | cin>>n>>m; 37 | int x,y; 38 | while(m--) 39 | { 40 | cin>>x>>y; 41 | v[x].push_back(y); 42 | v[y].push_back(x); 43 | } 44 | int cnt=0; 45 | for(i=1;i<=n;i++) 46 | { 47 | if(vis[i]==0) 48 | { 49 | cnt++; 50 | dfs(i); 51 | } 52 | //cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int vis[10001]; 20 | vectorv[10001]; 21 | 22 | bool dfs(int node,int par) 23 | { 24 | vis[node]=1; 25 | for(int child: v[node]) 26 | { 27 | if(vis[child]==0) 28 | { 29 | if(dfs(child,node)==true) 30 | { 31 | return true; 32 | } 33 | } 34 | else 35 | { 36 | if(child!=par)return true; 37 | } 38 | } 39 | return false; 40 | } 41 | 42 | int main() 43 | { 44 | int n,m,x,y,i; 45 | cin>>n>>m; 46 | while(m--) 47 | { 48 | cin>>x>>y; 49 | v[x].push_back(y); 50 | v[y].push_back(x); 51 | } 52 | bool ans=dfs(1,-1); 53 | if(ans==true)cout<<"The graph has a cycle"< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int mx=-(INT_MAX),nod; 21 | int vis[10001],dis[10001]; 22 | vectorv[10001]; 23 | void dfs(int node,int d) 24 | { 25 | vis[node]=1; 26 | dis[node]=d; 27 | if(dis[node]>mx) 28 | { 29 | mx=dis[node]; 30 | nod=node; 31 | } 32 | for(int child: v[node]) 33 | { 34 | if(vis[child]==0) 35 | { 36 | dfs(child,dis[node]+1); 37 | } 38 | } 39 | } 40 | int main() 41 | { 42 | int n,m,x,y,i; 43 | cin>>n>>m; 44 | while(m--) 45 | { 46 | cin>>x>>y; 47 | v[x].push_back(y); 48 | v[y].push_back(x); 49 | } 50 | dfs(1,0); 51 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int in[101],low[101],vis[101]; 20 | vectorv[101]; 21 | int timer; 22 | void dfs(int node,int par) 23 | { 24 | vis[node]=1; 25 | in[node]=low[node]=timer++; 26 | for(int child: v[node]) 27 | { 28 | if(child==par)continue; 29 | if(vis[child]==1) 30 | { 31 | low[node]=min(low[node],in[child]); 32 | } 33 | else{ 34 | dfs(child,node); 35 | if(low[child]>in[node]) 36 | { 37 | cout< "<>n>>m; 47 | while(m--) 48 | { 49 | cin>>x>>y; 50 | v[x].push_back(y); 51 | v[y].push_back(x); 52 | } 53 | dfs(1,-1); 54 | } 55 | -------------------------------------------------------------------------------- /Graph/Graph part 1/DFS/in and out time/in and out time.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int vis[10001],in[100001],out[10001]; 21 | int timer=1; 22 | vectorv[10001]; 23 | void dfs(int node) 24 | { 25 | vis[node]=1; 26 | in[node]=timer++; 27 | for(int child: v[node]) 28 | { 29 | if(vis[child]==0) 30 | { 31 | dfs(child); 32 | } 33 | } 34 | out[node]=timer++; 35 | } 36 | int main() 37 | { 38 | int n,m,x,y,i; 39 | cin>>n>>m; 40 | while(m--) 41 | { 42 | cin>>x>>y; 43 | v[x].push_back(y); 44 | v[y].push_back(x); 45 | } 46 | dfs(1); 47 | for(i=1;i<=n;i++) 48 | { 49 | cout<<"The In and Out time of: "< "< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int vis[10001],dis[10001]; 21 | vectorv[1000]; 22 | 23 | void dfs(int node,int d) 24 | { 25 | vis[node]=1; 26 | dis[node]=d; 27 | for(int child: v[node]) 28 | { 29 | if(vis[child]==0) 30 | { 31 | dfs(child,dis[node]+1); 32 | } 33 | } 34 | } 35 | 36 | int main() 37 | { 38 | int n,m; 39 | cin>>n>>m; 40 | int i,x,y; 41 | while(m--) 42 | { 43 | cin>>x>>y; 44 | v[x].push_back(y); 45 | v[y].push_back(x); 46 | } 47 | int a; 48 | cin>>a; 49 | dfs(a,0); 50 | int q; 51 | cin>>q; 52 | while(q--) 53 | { 54 | int b; 55 | cin>>b; 56 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int vis[10001],subsize[10001]; 20 | vectorv[10001]; 21 | int dfs(int node) 22 | { 23 | vis[node]=1; 24 | int cnt=1; 25 | for(int child: v[node]) 26 | { 27 | if(vis[child]==0) 28 | { 29 | cnt+=dfs(child); 30 | } 31 | } 32 | subsize[node]=cnt; 33 | return cnt; 34 | } 35 | int main() 36 | { 37 | int n,m,i,x,y; 38 | cin>>n>>m; 39 | while(m--) 40 | { 41 | cin>>x>>y; 42 | v[x].push_back(y); 43 | v[y].push_back(x); 44 | } 45 | dfs(1); 46 | for(i=1;i<=n;i++) 47 | { 48 | cout<<"The subtree of "< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | struct edge{ 21 | int a,b,w; 22 | }; 23 | edge ar[100006]; 24 | int par[10006]; 25 | bool comp(edge a,edge b) 26 | { 27 | if(a.w>n>>m; 44 | for(int i=1;i<=n;i++)par[i]=-1; 45 | for(int i=0;i>ar[i].a>>ar[i].b>>ar[i].w; 48 | } 49 | sort(ar,ar+m,comp); 50 | int ans=0; 51 | for(int i=0;i 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | vector< pair >v[1001]; 21 | 22 | int main() 23 | { 24 | int n,m,a,b,w,i; 25 | cin>>n>>m; 26 | while(m--) 27 | { 28 | cin>>a>>b>>w; 29 | v[a].push_back({b,w}); 30 | v[b].push_back({a,w}); 31 | } 32 | priority_queue , vector > , greater > >pq; 33 | vectordist(n+1, INT_MAX); 34 | pq.push({0,1}); 35 | dist[1]=0; 36 | while(!pq.empty()) 37 | { 38 | int curr=pq.top().second; 39 | int curr_d=pq.top().first; 40 | pq.pop(); 41 | for(pairedge: v[curr]) 42 | { 43 | if(curr_d+edge.second 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int main() 21 | { 22 | int n; 23 | cin>>n; 24 | int i,x; 25 | priority_queue,greater >pq; 26 | for(i=0;i>x; 29 | pq.push(x); 30 | } 31 | ll cost=0; 32 | while(pq.size()>1) 33 | { 34 | int a=pq.top(); 35 | pq.pop(); 36 | int b=pq.top(); 37 | pq.pop(); 38 | cost+=a+b; 39 | pq.push(a+b); 40 | } 41 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int main() 21 | { 22 | int n; 23 | cin>>n; 24 | int i,ar[n+3]; 25 | mapmp; 26 | for(i=0;i>ar[i]; 29 | mp[ar[i]]++; 30 | } 31 | priority_queue,vector >,greater > >pq; 32 | vectorv; 33 | for(pairp:mp) 34 | { 35 | pq.push({p.second,p.first}); 36 | } 37 | while(!pq.empty()) 38 | { 39 | for(i=1;i<=pq.top().first;i++) 40 | { 41 | v.push_back(pq.top().second); 42 | } 43 | pq.pop(); 44 | } 45 | for(i=v.size()-1;i>=0;i--)cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int main() 21 | { 22 | int n; 23 | cin>>n; 24 | int i,ar[n+3][4]; 25 | for(i=0;i>ar[i][0]>>ar[i][1]; 28 | } 29 | int k; 30 | cin>>k; 31 | priority_queue > >pq; 32 | for(i=0;ik)pq.pop(); 37 | } 38 | while(!pq.empty()) 39 | { 40 | pairp=pq.top().second; 41 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | priority_queue< pair >pq; 21 | 22 | void closestnumber(int ar[],int n,int k,int z) 23 | { 24 | for(int i=0;iz)pq.pop(); 29 | } 30 | } 31 | 32 | void print() 33 | { 34 | while(!pq.empty()) 35 | { 36 | cout<>n; 46 | int i,ar[n+3]; 47 | for(i=0;i>ar[i]; 48 | int k; 49 | cin>>k; 50 | int z; 51 | cin>>z; 52 | closestnumber(ar,n,k,z); 53 | print(); 54 | } 55 | -------------------------------------------------------------------------------- /Heap algo/Kth smallest Element/Kth smallest element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int n,k; 20 | int ar[10000001]; 21 | 22 | void solve(int n,int k) 23 | { 24 | int i; 25 | priority_queueq; 26 | for(i=0;i>ar[i]; 29 | q.push(ar[i]); 30 | if(q.size()>k) 31 | { 32 | cout<>n>>k; 41 | solve(n,k); 42 | } 43 | -------------------------------------------------------------------------------- /Heap algo/Top K frequent number/top k frequent numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int main() 21 | { 22 | int n; 23 | cin>>n; 24 | int i,ar[n+3]; 25 | mapmp; 26 | for(i=0;i>ar[i]; 29 | mp[ar[i]]++; 30 | } 31 | int k; 32 | cin>>k; 33 | priority_queue,vector >,greater > >pq; 34 | for(pairp:mp) 35 | { 36 | pq.push({p.second,p.first}); 37 | if(pq.size()>k)pq.pop(); 38 | } 39 | while(!pq.empty()) 40 | { 41 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | priority_queue,greater >pq; 21 | 22 | void largestelement(int ar[],int n,int k) 23 | { 24 | int i; 25 | for(i=0;ik)pq.pop(); 29 | } 30 | } 31 | 32 | void print() 33 | { 34 | while(!pq.empty()) 35 | { 36 | cout<>n; 46 | int i,ar[n+3]; 47 | for(i=0;i>ar[i]; 48 | int k; 49 | cin>>k; 50 | largestelement(ar,n,k); 51 | print(); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Heap algo/sort a K sorted array/sorted a k sorted array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | priority_queue,greater >pq; 21 | vectorv; 22 | 23 | void largestelement(int ar[],int n,int k) 24 | { 25 | int i; 26 | for(i=0;ik) 30 | { 31 | v.push_back(pq.top()); 32 | pq.pop(); 33 | } 34 | } 35 | while(!pq.empty()) 36 | { 37 | v.push_back(pq.top()); 38 | pq.pop(); 39 | } 40 | } 41 | 42 | void print() 43 | { 44 | for(int i=0;i>n; 52 | int i,ar[n+3]; 53 | for(i=0;i>ar[i]; 54 | int k; 55 | cin>>k; 56 | largestelement(ar,n,k); 57 | print(); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Longest Ascending Subsequence: -------------------------------------------------------------------------------- 1 | int LAS(vector&a) 2 | { 3 | vectorlis; 4 | for(auto &x: a) 5 | { 6 | auto it = upper_bound(lis.begin(),lis.end(),x); 7 | if(it == lis.end())lis.push_back(x); 8 | else *it = x; 9 | } 10 | return lis.size(); 11 | } 12 | -------------------------------------------------------------------------------- /MOS algo/adjacent max unique value: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | // PRIME NUMBER TEST// 21 | bool prime(int n) 22 | { 23 | if(n<2)return false; 24 | if(n<=3)return true; 25 | if(n%2==0)return false; 26 | for(int i=3;i*i<=n;i+=2) 27 | { 28 | if(n%i==0)return false; 29 | } 30 | return true; 31 | } 32 | 33 | int main() 34 | { 35 | int n; 36 | cin>>n; 37 | int ar[n+2]; 38 | for(int i=0;i>ar[i]; 39 | mapmp,mp2; 40 | int i=0; 41 | int mx=0,ans=0; 42 | int x=0; 43 | for(int i=0;i 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int gcd(int a,int b) 21 | { 22 | if(b==0)return a; 23 | return gcd(b,a%b); 24 | } 25 | 26 | int main() 27 | { 28 | int t; 29 | cin>>t; 30 | while(t--) 31 | { 32 | int a,b; 33 | cin>>a>>b; 34 | int ans=gcd(a,b); 35 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int gcd(int a,int b,int *x,int *y) 21 | { 22 | if(a==0) 23 | { 24 | *x=0; 25 | *y=1; 26 | return b; 27 | } 28 | int x1,y1; 29 | int ans=gcd(b%a,a,&x1,&y1); 30 | *x=y1-(b/a)*x1; 31 | *y=x1; 32 | return ans; 33 | } 34 | 35 | int main() 36 | { 37 | int t; 38 | cin>>t; 39 | while(t--) 40 | { 41 | int x,y,a,b; 42 | cin>>a>>b; 43 | int ans=gcd(a,b,&x,&y); 44 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int gcd(int a,int b) 21 | { 22 | if(b==0)return a; 23 | return gcd(b,a%b); 24 | } 25 | int phi(int n) 26 | { 27 | int cnt=1,i; 28 | for(int i=2;i<=n;i++) 29 | { 30 | if(gcd(i,n)==1)cnt++; 31 | } 32 | return cnt; 33 | } 34 | 35 | int main() 36 | { 37 | int t; 38 | cin>>t; 39 | while(t--) 40 | { 41 | int n; 42 | cin>>n; 43 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int phi[1000001]; 20 | void init(int mx) 21 | { 22 | for(int i=1;i<=mx;i++) 23 | { 24 | phi[i]=i; 25 | } 26 | for(int i=2;i<=mx;i++) 27 | { 28 | if(phi[i]==i) 29 | { 30 | for(int j=i;j<=mx;j+=i) 31 | { 32 | phi[j]/=i; 33 | phi[j]*=(i-1); 34 | } 35 | } 36 | } 37 | } 38 | int getcount(int divisor,int n) 39 | { 40 | n/=divisor; 41 | return phi[n]; 42 | } 43 | int main() 44 | { 45 | init(1000000); 46 | int q,n; 47 | cin>>q; 48 | while(q--) 49 | { 50 | cin>>n; 51 | int res=0; 52 | for(int i=1;i*i<=n;i++) 53 | { 54 | if(n%i==0) 55 | { 56 | int d1=i; 57 | int d2=n/i; 58 | res+=d1*getcoutn(d1,n); 59 | if(d1!=d2)res+=d2*getcount(d2,n); 60 | } 61 | } 62 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int phi[1000001]; 20 | void init(int mx) 21 | { 22 | for(int i=1;i<=mx;i++) 23 | { 24 | phi[i]=i; 25 | } 26 | for(int i=2;i<=mx;i++) 27 | { 28 | if(phi[i]==i) 29 | { 30 | for(int j=i;j<=mx;j+=i) 31 | { 32 | phi[j]/=i; 33 | phi[j]*=(i-1); 34 | } 35 | } 36 | } 37 | } 38 | int getcount(int divisor,int n) 39 | { 40 | n/=divisor; 41 | return phi[n]; 42 | } 43 | int main() 44 | { 45 | init(1000000); 46 | int t; 47 | cin>>t; 48 | while(t--) 49 | { 50 | int n; 51 | cin>>n; 52 | int ans=0; 53 | for(int i=1;i*i<=n;i++) 54 | { 55 | if(n%i==0) 56 | { 57 | int d1=i; 58 | int d2=n/i; 59 | //cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int phi[1000001]; 20 | void init(int mx) 21 | { 22 | for(int i=1;i<=mx;i++) 23 | { 24 | phi[i]=i; 25 | } 26 | for(int i=2;i<=mx;i++) 27 | { 28 | if(phi[i]==i) 29 | { 30 | for(int j=i;j<=mx;j+=i) 31 | { 32 | phi[j]/=i; 33 | phi[j]*=(i-1); 34 | } 35 | } 36 | } 37 | } 38 | 39 | int main() 40 | { 41 | init(1000000); 42 | int t,n; 43 | cin>>t; 44 | while(t--) 45 | { 46 | cin>>n; 47 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int phi(int n) 21 | { 22 | int res=n; 23 | for(int i=2;i*i<=n;i++) 24 | { 25 | if(n%i==0) 26 | { 27 | res/=i; 28 | res*=(i-1); 29 | while(n%i==0) 30 | { 31 | n/=i; 32 | } 33 | } 34 | 35 | } 36 | if(n>1) 37 | { 38 | res/=n; 39 | res*=(n-1); 40 | } 41 | return res; 42 | } 43 | 44 | int main() 45 | { 46 | int t; 47 | cin>>t; 48 | while(t--) 49 | { 50 | int n; 51 | cin>>n; 52 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int gcd(int a,int b,int *x,int *y) 20 | { 21 | if(a==0) 22 | { 23 | *x=0; 24 | *y=1; 25 | return b; 26 | } 27 | int x1,y1; 28 | int ans=gcd(b%a,a,&x1,&y1); 29 | *x=y1-(b/a)*x1; 30 | *y=x1; 31 | return ans; 32 | } 33 | 34 | int main() 35 | { 36 | faster 37 | int t; 38 | cin>>t; 39 | while(t--) 40 | { 41 | int a,b; 42 | cin>>a>>b; 43 | int ans=__gcd(a,b); 44 | if(ans>1)cout<<-1< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int a1,b1,c,x,y; 20 | int gcd(int a,int b,int *x,int *y) 21 | { 22 | if(a==0) 23 | { 24 | *x=0; 25 | *y=1; 26 | return b; 27 | } 28 | int x1,y1; 29 | int ans=gcd(b%a,a,&x1,&y1); 30 | *x=y1-(b/a)*x1; 31 | *y=x1; 32 | return ans; 33 | } 34 | 35 | int main() 36 | { 37 | int t; 38 | cin>>t; 39 | for(int i=1;i<=t;i++) 40 | { 41 | cin>>a1>>b1>>c; 42 | int ans=gcd(a1,b1,&x,&y); 43 | cout<<"Case "< 2 | #define REP(i,n) for (int i = 1; i <= n; i++) 3 | #define mod 1000000007 4 | #define pb push_back 5 | #define ff first 6 | #define ss second 7 | #define ii pair 8 | #define vi vector 9 | #define vii vector 10 | #define lli long long int 11 | #define endl '\n' 12 | 13 | using namespace std; 14 | 15 | int gcd(int a, int b, int& x, int& y) { 16 | if (b == 0) { 17 | x = 1; 18 | y = 0; 19 | return a; 20 | } 21 | int x1, y1; 22 | int d = gcd(b, a % b, x1, y1); 23 | x = y1; 24 | y = x1 - y1 * (a / b); 25 | return d; 26 | } 27 | 28 | bool find_any_solution(int a, int b, int c, int &x, int &y) { 29 | 30 | int x0 , y0; 31 | int g = gcd(abs(a), abs(b), x0, y0); 32 | if (c % g) { 33 | return false; 34 | } 35 | 36 | x = x0 * c / g; 37 | y = y0 * c / g; 38 | 39 | if (a < 0) x = -x; 40 | if (b < 0) y = -y; 41 | return true; 42 | } 43 | 44 | int main() 45 | { 46 | int t , a , b , c , x , y; 47 | 48 | cin>>t; 49 | 50 | while(t--) 51 | { 52 | cout<<"a : " , cin>>a; 53 | 54 | cout<<"b : " , cin>>b; 55 | 56 | cout<<"c : " , cin>>c; 57 | 58 | if(find_any_solution(a , b , c , x , y) == false){ 59 | cout<<"No solution exists\n\n"; 60 | } 61 | else 62 | { 63 | cout<<"x = "< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | bool isprime(int n) 21 | { 22 | if(n==1)return false; 23 | for(int i=2;i*i<=n;i++) 24 | { 25 | if(n%i==0)return false; 26 | } 27 | return true; 28 | } 29 | 30 | int main() 31 | { 32 | int t; 33 | cin>>t; 34 | while(t--) 35 | { 36 | int n; 37 | cin>>n; 38 | if(isprime(n))cout<<"Yes"< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int prime[90000001]; 20 | int n=90000000; 21 | vectorv; 22 | void sieve() 23 | { 24 | //int n=100000; 25 | //vectorv; 26 | memset(prime,0,sizeof(prime)); 27 | for(int i=2;i*i<=n;i++) 28 | { 29 | if(prime[i]==0) 30 | { 31 | for(int j=i*i;j<=n;j+=i) 32 | { 33 | prime[j]=1; 34 | } 35 | } 36 | } 37 | for(int i=2;i<=n;i++) 38 | { 39 | if(prime[i]==0)v.push_back(i); 40 | } 41 | } 42 | int main() 43 | { 44 | sieve(); 45 | int t; 46 | cin>>t; 47 | while(t--) 48 | { 49 | int n; 50 | cin>>n; 51 | /* 52 | for(int i=2;i<=n;i++) 53 | { 54 | if(prime[i]==0)cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all (x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll ; 18 | typedef unsigned long long int llu; 19 | 20 | ll countDivisors(ll n) 21 | { 22 | ll cnt = 0; 23 | for (ll i = 1; i <= sqrt(n); i++) { 24 | if (n % i == 0) { 25 | // If divisors are equal, 26 | // count only one 27 | if (n / i == i) 28 | cnt++; 29 | 30 | else // Otherwise count both 31 | cnt = cnt + 2; 32 | } 33 | } 34 | return cnt; 35 | } 36 | 37 | int main() 38 | { 39 | ll t; 40 | cin>>t; 41 | while(t--) 42 | { 43 | ll a,b,ans,div,mx=0; 44 | cin>>a>>b; 45 | for(ll i=a;i<=b;i++) 46 | { 47 | ll x=countDivisors(i); 48 | //cout<mx) 50 | { 51 | ans=i; 52 | //div=x; 53 | mx=max(x,mx); 54 | } 55 | } 56 | cout<<"Between "< 3 | using namespace std; 4 | 5 | #include 6 | #include 7 | using namespace __gnu_pbds; 8 | 9 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 10 | 11 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 12 | 13 | #define sq(x) (x)*(x) 14 | #define PI acos(-1.0) 15 | #define all(x) x.begin(),x.end() 16 | #define nl '\n' 17 | #define mod 1000000007 18 | typedef long long int ll; 19 | typedef unsigned long long int llu; 20 | 21 | ll factor(ll n) 22 | { 23 | mapmp; 24 | for(ll i=2;i<=n;i++) 25 | { 26 | ll num=i; 27 | for(ll j=2;j*j<=num;j++) 28 | { 29 | while(num%j==0) 30 | { 31 | num/=j; 32 | mp[j]++; 33 | } 34 | } 35 | if(num>1) 36 | { 37 | mp[num]++; 38 | } 39 | } 40 | 41 | ll res=1; 42 | for(auto p:mp) 43 | { 44 | res=res*(p.second+1)%mod; 45 | } 46 | return res; 47 | } 48 | 49 | int main() 50 | { 51 | ll n; 52 | cin>>n; 53 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int totaldivisor(int n) 21 | { 22 | bool ar[n+2]; 23 | memset(ar,true,sizeof(ar)); 24 | for(int i=2;i*i<=n;i++) 25 | { 26 | if(ar[i]==true) 27 | { 28 | for(int j=i*2;j<=n;j+=i) 29 | { 30 | ar[j]=false; 31 | } 32 | } 33 | } 34 | int total=1; 35 | for(int i=2;i<=n;i++) 36 | { 37 | if(ar[i]) 38 | { 39 | int cnt=0; 40 | if(n%i==0) 41 | { 42 | while(n%i==0) 43 | { 44 | n/=i; 45 | cnt++; 46 | } 47 | total=total*(cnt+1); 48 | } 49 | } 50 | } 51 | return total; 52 | } 53 | 54 | int main() 55 | { 56 | int t; 57 | cin>>t; 58 | while(t--) 59 | { 60 | int n; 61 | cin>>n; 62 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int totaldivisor(int n) 21 | { 22 | int total=1; 23 | int cnt=0; 24 | while(n%2==0) 25 | { 26 | n/=2; 27 | cnt++; 28 | } 29 | total=total*(cnt+1); 30 | for(int i=3;i<=n;i+=2) 31 | { 32 | if(n%i==0) 33 | { 34 | cnt=0; 35 | while(n%i==0) 36 | { 37 | n/=i; 38 | cnt++; 39 | } 40 | total=total*(cnt+1); 41 | } 42 | } 43 | return total; 44 | } 45 | 46 | int main() 47 | { 48 | int t; 49 | cin>>t; 50 | while(t--) 51 | { 52 | int n; 53 | cin>>n; 54 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | ll power(ll base,ll n) 21 | { 22 | ll result=1; 23 | while(n) 24 | { 25 | if(n%2==1) 26 | { 27 | result*=base; 28 | n--; 29 | } 30 | else { 31 | base*=base; 32 | n/=2; 33 | } 34 | } 35 | return result; 36 | } 37 | 38 | int main() 39 | { 40 | ll t; 41 | cin>>t; 42 | while(t--) 43 | { 44 | ll n,m; 45 | cin>>n>>m; 46 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int f[1000001]; 21 | 22 | int power(int a,int n) 23 | { 24 | int result=1; 25 | while(n) 26 | { 27 | if(n%2==1) 28 | { 29 | result=(result* 1LL * a)%mod; 30 | n--; 31 | } 32 | else{ 33 | a=(a*1LL*a)%mod; 34 | n/=2; 35 | } 36 | } 37 | return result; 38 | } 39 | int C(int n,int k) 40 | { 41 | if(k>n)return 0; 42 | int res=f[n]; 43 | res=(res* 1LL * power(f[k], mod-2))%mod; 44 | res=(res*1LL*power(f[n-k],mod-2))%mod; 45 | return res; 46 | } 47 | int main() 48 | { 49 | f[0]=f[1]=1; 50 | for(int i=2;i<=1000000;i++) 51 | { 52 | f[i]=(f[i-1] * 1LL * i)%mod; 53 | } 54 | int q; 55 | int n,k; 56 | cin>>q; 57 | while(q--) 58 | { 59 | cin>>n>>k; 60 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int divmul(int n,int k) 21 | { 22 | int cnt=0,i; 23 | for(i=1;i*i<=n;i++) 24 | { 25 | if(n%i==0) 26 | { 27 | if(i%k==0)cnt++; 28 | if((n/i)%k==0)cnt++; 29 | } 30 | } 31 | i--; 32 | if((i*i==n)&&(i%k==0))cnt++; 33 | return cnt; 34 | } 35 | int main() 36 | { 37 | int t; 38 | cin>>t; 39 | while(t--) 40 | { 41 | int n,k; 42 | cin>>n>>k; 43 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int gcd(int a,int b) 21 | { 22 | if(b==0)return a; 23 | return gcd(b,a%b); 24 | } 25 | int comdiv(int a,int b) 26 | { 27 | int n=gcd(a,b); 28 | int result=0; 29 | for(int i=1;i*i<=n;i++) 30 | { 31 | if(n%i==0) 32 | { 33 | if(n/i==i)result++; 34 | else result+=2; 35 | } 36 | } 37 | return result; 38 | } 39 | int main() 40 | { 41 | int t; 42 | cin>>t; 43 | while(t--) 44 | { 45 | int a,b; 46 | cin>>a>>b; 47 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | mapmp; 20 | void primefactor(int a) 21 | { 22 | for(int i=2;i*i<=a;i+=2) 23 | { 24 | int cnt=0; 25 | while(a%i==0) 26 | { 27 | cnt++; 28 | a/=i; 29 | } 30 | mp[i]=cnt; 31 | } 32 | if(a>1)mp[a]=1; 33 | } 34 | int comdiv(int a,int b) 35 | { 36 | mp.clear(); 37 | primefactor(a); 38 | int res=1; 39 | for(auto m:mp) 40 | { 41 | int cnt=0; 42 | int x=m.first; 43 | int y=m.second; 44 | while(b%x==0) 45 | { 46 | cnt++; 47 | b/=x; 48 | } 49 | res*=(min(cnt,y)+1); 50 | } 51 | return res; 52 | } 53 | 54 | int main() 55 | { 56 | int t; 57 | cin>>t; 58 | while(t--) 59 | { 60 | int a,b; 61 | cin>>a>>b; 62 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int power(int a,int n,int m) 21 | { 22 | int result=1; 23 | while(n) 24 | { 25 | if(n%2==1) 26 | { 27 | result=(result*a)%m; 28 | n--; 29 | } 30 | else 31 | { 32 | a=(a*a)%m; 33 | n/=2; 34 | } 35 | } 36 | return result; 37 | } 38 | 39 | int main() 40 | { 41 | int n; 42 | cin>>n; 43 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int power(int a,int n,int m) 21 | { 22 | int result=1; 23 | while(n) 24 | { 25 | if(n%2==1) 26 | { 27 | result=(result*a)%m; 28 | n--; 29 | } 30 | else{ 31 | a=(a*a)%m; 32 | n/=2; 33 | } 34 | } 35 | return result; 36 | } 37 | 38 | int main() 39 | { 40 | int t; 41 | cin>>t; 42 | while(t--) 43 | { 44 | int a,m; 45 | cin>>a>>m; 46 | cout<<"a^-1 = "< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | int mx=100000; 20 | int ar[100001]; 21 | void sieve() 22 | { 23 | for(int i=2;i<=mx;i++) 24 | { 25 | if(ar[i]==0) 26 | { 27 | for(int j=i;j<=mx;j+=i) 28 | { 29 | if(ar[j]==0)ar[j]=i; 30 | } 31 | } 32 | } 33 | } 34 | void factor(int n) 35 | { 36 | while(n!=1) 37 | { 38 | cout<>t; 47 | sieve(); 48 | while(t--) 49 | { 50 | int n; 51 | cin>>n; 52 | factor(n); 53 | } 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Number Theory/prime factorzation/prime fact.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | void primefact(int n) 21 | { 22 | for(int i=2;i*i<=n;i++) 23 | { 24 | if(n%i==0) 25 | { 26 | int cnt=0; 27 | while(n%i == 0) 28 | { 29 | cnt++; 30 | n=n/i; 31 | //cout<<"kd"<1)cout<>t; 43 | while(t--) 44 | { 45 | int n; 46 | cin>>n; 47 | primefact(n); 48 | } 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Number Theory/sum of consecutive number with neg and pos/pos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long int countConsecutive(long int N) 5 | { 6 | long int count = 0; 7 | for (long int L =1; L * (L + 1) < 2 * N; L++) 8 | { 9 | float a = (1.0 * N-(L * (L + 1)) / 2) / (L + 1); 10 | if (a-(int)a == 0.0) 11 | count++; 12 | } 13 | return count; 14 | } 15 | 16 | int main() 17 | { 18 | int t; 19 | cin>>t; 20 | while(t--) 21 | { 22 | int N; 23 | cin>>N; 24 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | ll factorial(int n) 21 | { 22 | if(n==1)return 1; 23 | return n*factorial(n-1); 24 | } 25 | 26 | int main() 27 | { 28 | while(1) 29 | { 30 | int n; 31 | cin>>n; 32 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | ll fibonacci(int n) 21 | { 22 | if(n==0||n==1)return n; 23 | return fibonacci(n-1)+fibonacci(n-2); 24 | } 25 | 26 | int main() 27 | { 28 | while(1) 29 | { 30 | int n; 31 | cin>>n; 32 | cout< 3 | using namespace std; 4 | 5 | #include 6 | #include 7 | using namespace __gnu_pbds; 8 | 9 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 10 | 11 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 12 | 13 | #define sq(x) (x)*(x) 14 | #define PI acos(-1.0) 15 | #define all(x) x.begin(),x.end() 16 | #define nl '\n' 17 | #define mod 1000000007 18 | typedef long long int ll; 19 | typedef unsigned long long int llu; 20 | 21 | void solve(int n,int m,string op) 22 | { 23 | if(n==0&&m==0) 24 | { 25 | cout<>n; 58 | int m=n; 59 | string op=""; 60 | solve(n,m,op); 61 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | void solve(vector&v,int ind,int k) 21 | { 22 | if(v.size()==1) 23 | { 24 | cout<>n>>k; 38 | vectorv; 39 | for(int i=1;i<=n;i++) 40 | { 41 | v.push_back(i); 42 | } 43 | int ind=0; 44 | k--; 45 | solve(v,ind,k); 46 | 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Recursion/KTh Symbol in grammar/code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | bool solve(int n,int k) 21 | { 22 | if(n==1&&k==1) 23 | { 24 | return false; 25 | } 26 | 27 | 28 | int mid=(pow(2,n-1))/2; 29 | if(k<=mid) 30 | { 31 | return solve(n-1,k); 32 | } 33 | else 34 | { 35 | return !solve(n-1,k-mid); 36 | } 37 | } 38 | 39 | int main() 40 | { 41 | while(1) 42 | { 43 | int n,k; 44 | cin>>n>>k; 45 | bool ans=solve(n,k); 46 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | void solve(string s,string op) 21 | { 22 | if(s.length()==0) 23 | { 24 | cout<>s; 51 | string op=""; 52 | solve(s,op); 53 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | void solve(string s,string op) 21 | { 22 | if(s.length()==0) 23 | { 24 | cout<>s; 42 | string op; 43 | solve(s,op); 44 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int print(int n) 21 | { 22 | if(n==1)return 1; 23 | print(n-1); 24 | cout<>n; 33 | print(n); 34 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | void print(int n) 21 | { 22 | if(n==1) 23 | { 24 | cout<<1<>n; 37 | print(n); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Recursion/Print N bit binary numbers there num of 1 is grater then 0/code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | void solve(int o,int z,int n,string op) 21 | { 22 | if(n==0) 23 | { 24 | cout<>n; 51 | int z=0,o=0; 52 | string op=""; 53 | solve(o,z,n,op); 54 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | void solve(string s,string op) 21 | { 22 | if(s.size()==0) 23 | { 24 | cout<>s; 41 | string op=""; 42 | solve(s,op); 43 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | void solve(string s,string op) 21 | { 22 | if(s.length()==0) 23 | { 24 | cout<>s; 43 | string op=""; 44 | op.push_back(s[0]); 45 | s.erase(s.begin()+0); 46 | solve(s,op); 47 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | void print(stackv) 21 | { 22 | if(v.empty())return; 23 | int x=v.top(); 24 | v.pop(); 25 | print(v); 26 | cout<>n; 35 | int i,x; 36 | stackst; 37 | for(i=0;i>x; 40 | st.push(x); 41 | } 42 | print(st); 43 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | void inserts(vector&v,int temp) 21 | { 22 | if(v.size()==0||v[v.size()-1]<=temp) 23 | { 24 | v.push_back(temp); 25 | return; 26 | } 27 | int val=v[v.size()-1]; 28 | v.pop_back(); 29 | inserts(v,temp); 30 | v.push_back(val); 31 | cout<&v) 35 | { 36 | if(v.size()==1) 37 | { 38 | return; 39 | } 40 | int temp=v[v.size()-1]; 41 | v.pop_back(); 42 | sorts(v); 43 | inserts(v,temp); 44 | cout<>n; 51 | vectorv; 52 | int x; 53 | for(int i=0;i>x; 56 | v.push_back(x); 57 | } 58 | sorts(v); 59 | for(int i=0;i 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | void inserts(stack&v,int temp) 21 | { 22 | if(v.empty()||v.top()<=temp) 23 | { 24 | v.push(temp); 25 | return; 26 | } 27 | int val=v.top(); 28 | v.pop(); 29 | inserts(v,temp); 30 | v.push(val); 31 | } 32 | 33 | void sorts(stack&v) 34 | { 35 | if(v.size()==1) 36 | { 37 | return; 38 | } 39 | int temp=v.top(); 40 | v.pop(); 41 | sorts(v); 42 | inserts(v,temp); 43 | } 44 | 45 | void print(stackv) 46 | { 47 | if(v.empty())return; 48 | int x=v.top(); 49 | v.pop(); 50 | print(v); 51 | cout<>n; 57 | stackv; 58 | int x; 59 | for(int i=0;i>x; 62 | v.push(x); 63 | } 64 | sorts(v); 65 | print(v); 66 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | ll cnt; 21 | 22 | void towerofhanio(int n,char a,char b,char c) 23 | { 24 | cnt++; 25 | if(n==1) 26 | { 27 | cout<<"Move disk 1 from rod "<>n; 42 | int a,b,c; 43 | towerofhanio(n,'A','B','C'); 44 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | vectorv; 21 | 22 | void solve(int ar[],int n,int sz) 23 | { 24 | queueq; 25 | for(int i=0;i>n; 50 | int i,ar[n+3]; 51 | for(i=0;i>ar[i]; 52 | int sz; 53 | cin>>sz; 54 | solve(ar,n,sz); 55 | for(i=0;i 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | ll solve(string s) 21 | { 22 | ll ans=0; 23 | int i=0,j=0; 24 | mapmp; 25 | while(j>t; 51 | while(t--) 52 | { 53 | string s; 54 | cin>>s; 55 | int k; 56 | ll ans=solve(s); 57 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | vectorv; 21 | void solve(int ar[],int n,int sz) 22 | { 23 | priority_queue >pq; 24 | int i,j; 25 | for(i=0;i>n; 47 | int ar[n+3],i,sz; 48 | for(i=0;i>ar[i]; 49 | cin>>sz; 50 | solve(ar,n,sz); 51 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int cnt=0; 21 | void solve(string s,string a) 22 | { 23 | int sum=0,sum1=0; 24 | int i,j; 25 | for(i=0;i>t; 50 | while(t--) 51 | { 52 | string s,a; 53 | cin>>s>>a; 54 | cnt=0; 55 | solve(s,a); 56 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | ll sliding(int ar[],int n,int sz) 21 | { 22 | ll mx=0,sum=0; 23 | int i; 24 | for(i=1;i<=sz;i++)sum+=ar[i]; 25 | mx=sum; 26 | for(i=sz+1;i<=n;i++) 27 | { 28 | sum+=ar[i]; 29 | sum-=ar[i-sz]; 30 | mx=max(mx,sum); 31 | } 32 | return mx; 33 | } 34 | 35 | int main() 36 | { 37 | int n; 38 | cin>>n; 39 | int i,ar[n+4]; 40 | for(i=1;i<=n;i++)cin>>ar[i]; 41 | int sz; 42 | cin>>sz; 43 | ll ans=sliding(ar,n,sz); 44 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | ll solve(string s,int k) 21 | { 22 | ll ans=0; 23 | int i=0,j=0; 24 | mapmp; 25 | while(jk) 37 | { 38 | mp[s[i]]--; 39 | if(mp[s[i]]==0)mp.erase(s[i]); 40 | i++; 41 | } 42 | j++; 43 | } 44 | } 45 | return ans; 46 | } 47 | 48 | int main() 49 | { 50 | int t; 51 | cin>>t; 52 | while(t--) 53 | { 54 | string s; 55 | cin>>s; 56 | int k; 57 | cin>>k; 58 | ll ans=solve(s,k); 59 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | void swap(int *xp, int *yp) 21 | { 22 | int temp = *xp; 23 | *xp = *yp; 24 | *yp = temp; 25 | } 26 | 27 | void bubblesort(int ar[], int n) 28 | { 29 | int i,j; 30 | for(i=0;iar[j+1]); 35 | { 36 | swap(ar[j],ar[j+1]); 37 | } 38 | } 39 | for(int k=0;k>n; 57 | int i,ar[n+3]; 58 | for(i=0;i>ar[i]; 59 | bubblesort(ar,n); 60 | print(ar,n); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Sort/Insersion sort/code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | void insersionsort(int ar[], int n) 21 | { 22 | int i,j,k; 23 | for(i=1;i=0&&ar[j]>k) 28 | { 29 | ar[j+1]=ar[j]; 30 | j--; 31 | } 32 | ar[j+1]=k; 33 | } 34 | } 35 | 36 | void print(int ar[], int n) 37 | { 38 | for(int i=0;i>n; 49 | int i,ar[n+3]; 50 | for(i=0;i>ar[i]; 51 | insersionsort(ar,n); 52 | print(ar,n); 53 | } 54 | } 55 | 56 | -------------------------------------------------------------------------------- /Sort/selection sort/code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | void selectionsort(int ar[], int n) 21 | { 22 | int i,j,mn; 23 | for(i=0;i>n; 50 | int i,ar[n+3]; 51 | for(i=0;i>ar[i]; 52 | selectionsort(ar,n); 53 | print(ar,n); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Stack algorithm/Minimum element in stack with extra space/minimum element extra space.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | stacks; 21 | stackss; 22 | 23 | int getmin() 24 | { 25 | if(ss.size()==0)return -1; 26 | return ss.top(); 27 | } 28 | void push(int a) 29 | { 30 | s.push(a); 31 | if(ss.size()==0)ss.push(a); 32 | else{ 33 | if(ss.top()>=a)ss.push(a); 34 | } 35 | } 36 | int pop() 37 | { 38 | if(s.size()==0)return -1; 39 | int ans=s.top(); 40 | if(ss.top()==ans)ss.pop(); 41 | return getmin(); 42 | } 43 | 44 | int main() 45 | { 46 | int n; 47 | cin>>n; 48 | while(n--) 49 | { 50 | int x; 51 | cin>>x; 52 | push(x); 53 | } 54 | int q; 55 | cin>>q; 56 | while(q--) 57 | { 58 | ll ans=pop(); 59 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | vectorv; 21 | 22 | void solve(int ar[],int n) 23 | { 24 | int i; 25 | stackst; 26 | for(i=0;i<=n;i++) 27 | { 28 | int ans=-1; 29 | while(!st.empty()) 30 | { 31 | int x=st.top(); 32 | if(x>ar[i]) 33 | { 34 | ans=x; 35 | break; 36 | } 37 | st.pop(); 38 | } 39 | v.push_back(ans); 40 | st.push(ar[i]); 41 | } 42 | } 43 | 44 | void print() 45 | { 46 | int i; 47 | for(i=0;i>n; 55 | int ar[n+2],i; 56 | for(i=0;i>ar[i]; 57 | solve(ar,n-1); 58 | print(); 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Stack algorithm/Nearest Greater to right/code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | vectorv; 21 | 22 | void solve(int ar[],int n) 23 | { 24 | int i; 25 | stackst; 26 | for(i=n;i>=0;i--) 27 | { 28 | int ans=-1; 29 | while(!st.empty()) 30 | { 31 | int x=st.top(); 32 | if(x>ar[i]) 33 | { 34 | ans=x; 35 | break; 36 | } 37 | st.pop(); 38 | } 39 | v.push_back(ans); 40 | st.push(ar[i]); 41 | } 42 | } 43 | 44 | void print() 45 | { 46 | int i; 47 | for(i=v.size()-1;i>=0;i--)cout<>n; 55 | int ar[n+2],i; 56 | for(i=0;i>ar[i]; 57 | solve(ar,n-1); 58 | print(); 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Stack algorithm/Nearest Smaller to Right/code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | vectorv; 21 | 22 | void solve(int ar[],int n) 23 | { 24 | int i; 25 | stackst; 26 | for(i=n;i>=0;i--) 27 | { 28 | int ans=-1; 29 | while(!st.empty()) 30 | { 31 | int x=st.top(); 32 | if(x=0;i--)cout<>n; 55 | int ar[n+2],i; 56 | for(i=0;i>ar[i]; 57 | solve(ar,n-1); 58 | print(); 59 | return 0; 60 | } 61 | 62 | -------------------------------------------------------------------------------- /Stack algorithm/Nearest smaller to left/code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | vectorv; 21 | 22 | void solve(int ar[],int n) 23 | { 24 | int i; 25 | stackst; 26 | for(i=0;i<=n;i++) 27 | { 28 | int ans=-1; 29 | while(!st.empty()) 30 | { 31 | int x=st.top(); 32 | if(x>n; 55 | int ar[n+2],i; 56 | for(i=0;i>ar[i]; 57 | solve(ar,n-1); 58 | print(); 59 | return 0; 60 | } 61 | 62 | -------------------------------------------------------------------------------- /Stack algorithm/Rain Water Trapping/Rain water trapping.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | vectorv; 20 | vectorv1; 21 | 22 | void solve(int ar[],int n) 23 | { 24 | int i,j; 25 | int mx=0; 26 | for(i=0;i=0;i--) 33 | { 34 | mx=max(mx,ar[i]); 35 | v1.push_back(mx); 36 | } 37 | ll ans=0; 38 | for(i=0,j=n-1;i>n; 49 | int i,ar[n+3]; 50 | for(i=0;i>ar[i]; 51 | solve(ar,n); 52 | } 53 | -------------------------------------------------------------------------------- /Stack algorithm/Stock Span problem/code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | vectorv; 21 | 22 | void solve(int ar[],int n) 23 | { 24 | int i; 25 | stack< pair >st; 26 | for(i=0;i<=n;i++) 27 | { 28 | int ans=-1; 29 | while(!st.empty()) 30 | { 31 | int x=st.top().first; 32 | int y=st.top().second; 33 | if(x>ar[i]) 34 | { 35 | ans=y; 36 | break; 37 | } 38 | st.pop(); 39 | } 40 | v.push_back(ans); 41 | st.push({ar[i],i}); 42 | } 43 | } 44 | 45 | void print() 46 | { 47 | int i; 48 | for(i=0;i>n; 56 | int ar[n+2],i; 57 | for(i=0;i>ar[i]; 58 | solve(ar,n-1); 59 | print(); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Stack algorithm/Stock Span problem/finding the medius.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | int main() 21 | { 22 | int n; 23 | cin>>n; 24 | int i,ar[n+3]; 25 | for(i=0;i>ar[i]; 26 | cout<v; 28 | int k; 29 | cin>>k; 30 | for(i=0;i 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | ll gethash(string s) 21 | { 22 | ll ans=0; 23 | ll p=31; 24 | ll power=1; 25 | for(char ch: s) 26 | { 27 | ans=(ans+(ch-'a'+1)*power)%mod; 28 | power=(power*p)%mod; 29 | } 30 | return ans; 31 | } 32 | 33 | int main() 34 | { 35 | while(1) 36 | { 37 | string s; 38 | cin>>s; 39 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | 18 | typedef long long int ll; 19 | typedef unsigned long long int llu; 20 | 21 | int main() 22 | { 23 | string s,t; 24 | cin>>s>>t; 25 | ll n=s.size(); 26 | ll m=t.size(); 27 | 28 | ll ar[n+4]; 29 | ar[0]=0; 30 | ll val=0; 31 | ll prime=7; 32 | for(ll i=0;i 2 | using namespace std; 3 | 4 | vector prefix(200000,0); 5 | 6 | void formPrefix(string search) { 7 | 8 | for(int i=1;i0 && search[i]!=search[j]) 12 | { 13 | j = prefix[j-1]; 14 | } 15 | if(search[i]==search[j])j++; 16 | prefix[i] = j; 17 | } 18 | return ; 19 | } 20 | 21 | int main() { 22 | string p,t; 23 | cin >> p >> t; 24 | string search = p + "#" + t; 25 | formPrefix(search); 26 | int occurences = 0; 27 | for(auto num:prefix) { 28 | if(num==p.size()) 29 | occurences++; 30 | } 31 | cout << occurences << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /String Algorithm/KMP Algorithm/another kmp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | string ne,hay; 7 | int j=0,count=0,n,i,counter; 8 | 9 | while(cin>>n>>ne>>hay){ 10 | if(ne.length()>hay.length()){ 11 | cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | string s,x; 21 | bool match(int i,int n) 22 | { 23 | string p; 24 | for(int j=i;j>t; 35 | cin.ignore(); 36 | while(t--) 37 | { 38 | //string s; 39 | getline(cin,s); 40 | //string x; 41 | cin>>x; 42 | //cout< 2 | #include 3 | 4 | int main() 5 | { 6 | int t; 7 | scanf("%d",&t); 8 | while(t--) 9 | { 10 | int a[10005],b[10005]; 11 | int ans[200005]= {0}; 12 | int i,j,tmp; 13 | 14 | char s1[10051],s2[10051]; 15 | 16 | scanf(" %s",s1); 17 | scanf(" %s",s2); 18 | 19 | int l1 = strlen(s1); 20 | int l2 = strlen(s2); 21 | for(i = l1-1,j=0; i>=0; i--,j++) 22 | { 23 | a[j] = s1[i]-'0'; 24 | } 25 | 26 | for(i = l2-1,j=0; i>=0; i--,j++) 27 | { 28 | b[j] = s2[i]-'0'; 29 | } 30 | 31 | for(i = 0; i < l2; i++) 32 | { 33 | for(j = 0; j < l1; j++) 34 | { 35 | ans[i+j]+=b[i]*a[j]; 36 | } 37 | } 38 | for(i=0; i= 0; i--) 46 | { 47 | if(ans[i]>0) 48 | break; 49 | } 50 | 51 | for(; i>= 0; i--) 52 | { 53 | printf("%d",ans[i]); 54 | } 55 | printf("\n"); 56 | } 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Vule Gele Help Nao/All SubArray NON - Zero XOR: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | // PRIME NUMBER TEST// 21 | bool prime(int n) 22 | { 23 | if(n<2)return false; 24 | if(n<=3)return true; 25 | if(n%2==0)return false; 26 | for(int i=3;i*i<=n;i+=2) 27 | { 28 | if(n%i==0)return false; 29 | } 30 | return true; 31 | } 32 | 33 | // Graph Grid // 34 | int dx[]={-1,0,1,0}; 35 | int dy[]={0,1,0,-1}; 36 | 37 | vectorv; 38 | 39 | void solve() 40 | { 41 | vectorx; 42 | x.push_back(1); 43 | int y = 1; 44 | v.push_back(1); 45 | while(v.size()<1e5) 46 | { 47 | int pw = pow(2,y); 48 | v.push_back(pw); 49 | for(int i=0;i>t; 59 | solve(); 60 | while(t--) 61 | { 62 | int n; 63 | cin>>n; 64 | for(int i=0;i 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | #define mx 500 21 | int multiply(int x,int res[],int sz); 22 | 23 | void factorial(int n) 24 | { 25 | int res[mx]; 26 | res[0]=1; 27 | int sz=1; 28 | for(int i=2;i<=n;i++) 29 | { 30 | sz=multiply(i,res,sz); 31 | } 32 | for(int i=sz-1;i>=0;i--) 33 | { 34 | cout<>t; 59 | while(t--) 60 | { 61 | int n; 62 | cin>>n; 63 | factorial(n); 64 | } 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /mathematics/Binary Search.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n; 5 | printf("Enter the Number of Element in an Array: "); 6 | scanf("%d", &n); 7 | 8 | int array[n]; 9 | for(int i=0;inum)r=mid-1; 28 | else l=mid+1; 29 | } 30 | if(ans==-1)printf("The num is not Found\n"); 31 | else{ 32 | printf("%d is Found at position %d\n", num,ans); 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /mathematics/Bubble sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n,swap; 5 | printf("Enter the number of Element in an Array: "); 6 | scanf("%d", &n); 7 | 8 | int array[n]; 9 | for(int i=0;iarray[j+1]) 19 | { 20 | swap = array[j]; 21 | array[j]=array[j+1]; 22 | array[j+1]=swap; 23 | } 24 | } 25 | } 26 | 27 | printf("The Sorted Array is: \n"); 28 | for(int i=0;i 2 | int main() 3 | { 4 | int n,array[100]; 5 | printf("Enter the number of Element in an array: "); 6 | scanf("%d", &n); 7 | 8 | for(int i=0;i=n) 17 | { 18 | printf("The Deletion is not Possible\n"); 19 | return 0; 20 | } 21 | 22 | for(int i=pos-1;i 2 | int main() 3 | { 4 | int n; 5 | printf("Enter the number of Terms: "); 6 | scanf("%d", &n); 7 | 8 | int f=0,s=1,next; 9 | printf("%d\n%d\n", f,s); 10 | 11 | for(int i=2;i 2 | int main() 3 | { 4 | int n; 5 | printf("Enter the number of Element in an Array: "); 6 | scanf("%d", &n); 7 | 8 | int array[n]; 9 | for(int i=0;i 2 | int main() 3 | { 4 | int n; 5 | printf("Enter the Number of Element in an Array: "); 6 | scanf("%d", &n); 7 | 8 | int array[n]; 9 | for(int i=0;i 2 | int main() 3 | { 4 | int n; 5 | printf("Enter the Number of Element in an Array: "); 6 | scanf("%d", &n); 7 | 8 | int array[n]; 9 | for(int i=0;i 2 | int main() 3 | { 4 | int n; 5 | printf("Enter the number of Element in an Array: "); 6 | scanf("%d", &n); 7 | 8 | int array[n]; 9 | for(int i=0;imax)max=array[i]; 17 | } 18 | 19 | printf("The Maximum Element of This Array is: %d\n", max); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /mathematics/Read an array and search an num.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n; 5 | printf("Enter the Number of Element in an Array: "); 6 | scanf("%d", &n); 7 | 8 | int array[n]; 9 | for(int i=0;inum)r=mid-1; 28 | else l=mid+1; 29 | } 30 | if(ans==-1)printf("UNSUCCESSFUL SEARCH\n"); 31 | else{ 32 | printf("SUCCESSFUL SEARCH\n"); 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /mathematics/Reverse Array.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n; 5 | printf("Enter the number of Element in an Array: "); 6 | scanf("%d", &n); 7 | 8 | int a[n],b[n]; 9 | for(int i=0;i=0;i--) 16 | { 17 | b[j]=a[i]; 18 | j++; 19 | } 20 | 21 | printf("Print the Reverse Array is: \n"); 22 | 23 | for(int i=0;i 2 | int main() 3 | { 4 | int n,swap; 5 | printf("Enter the number of Element in an Array: "); 6 | scanf("%d", &n); 7 | 8 | int array[n]; 9 | for(int i=0;iarray[j]) 20 | { 21 | pos=j; 22 | } 23 | } 24 | if(pos!=i) 25 | { 26 | swap = array[i]; 27 | array[i]=array[pos]; 28 | array[pos]=swap; 29 | } 30 | } 31 | printf("The Sorted Array is: \n"); 32 | for(int i=0;i 2 | 3 | void TOH(int n,char a,char b,char c) 4 | { 5 | if(n>0) 6 | { 7 | TOH(n-1,a,c,b); 8 | printf("\n%c->%c",a,b); 9 | TOH(n-1,c,b,a); 10 | } 11 | } 12 | int main() 13 | { 14 | int n; 15 | printf("How many plates?: "); 16 | scanf("%d", &n); 17 | 18 | TOH(n,'A','B','C'); 19 | } 20 | -------------------------------------------------------------------------------- /mathematics/insert an Element in an Array.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n,array[100]; 5 | printf("Enter the number of Element in an array: "); 6 | scanf("%d", &n); 7 | 8 | for(int i=0;i=pos-1;i--) 20 | { 21 | array[i+1]=array[i]; 22 | } 23 | array[pos-1]=value; 24 | 25 | printf("Resulting Array is: "); 26 | for(int i=0;i<=n;i++) 27 | { 28 | printf("%d ", array[i]); 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /mathematics/insertion sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n,swap; 5 | printf("Enter the number of Element in an Array: "); 6 | scanf("%d", &n); 7 | 8 | int array[n]; 9 | for(int i=0;i0 && array[d-1]>array[d]) 18 | { 19 | swap = array[d]; 20 | array[d]=array[d-1]; 21 | array[d-1]=swap; 22 | d--; 23 | } 24 | } 25 | printf("The Sorted Array is: \n"); 26 | for(int i=0;i 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | 20 | 21 | int main() 22 | { 23 | int t; 24 | cin>>t; 25 | while(t--) 26 | { 27 | int n; 28 | cin>>n; 29 | int ar[n+3],i,pre[n+5]; 30 | pre[0]=0; 31 | for(i=1;i<=n;i++) 32 | { 33 | cin>>ar[i]; 34 | pre[i]=pre[i-1]+ar[i]; 35 | } 36 | for(i=1;i<=n;i++)cout< 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | 8 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 9 | 10 | #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 11 | 12 | #define sq(x) (x)*(x) 13 | #define PI acos(-1.0) 14 | #define all(x) x.begin(),x.end() 15 | #define nl '\n' 16 | #define mod 1000000007 17 | typedef long long int ll; 18 | typedef unsigned long long int llu; 19 | ll phi[2000010]; 20 | ll dp[2000003]; 21 | void init(ll mx) 22 | { 23 | for(ll i=1;i<=mx;i++) 24 | { 25 | phi[i]=i; 26 | } 27 | for(ll i=2;i<=mx;i++) 28 | { 29 | if(phi[i]==i) 30 | { 31 | for(ll j=i;j<=mx;j+=i) 32 | { 33 | phi[j]/=i; 34 | phi[j]*=(i-1); 35 | } 36 | } 37 | } 38 | } 39 | 40 | ll solve(ll n) 41 | { 42 | if(n==1) 43 | { 44 | return 0; 45 | } 46 | if(dp[n]!=0) 47 | { 48 | return dp[n]; 49 | } 50 | return dp[n]=solve(phi[n])+1; 51 | } 52 | 53 | int main() 54 | { 55 | init(2000000); 56 | ll t,n,m; 57 | cin>>t; 58 | while(t--) 59 | { 60 | cin>>m>>n; 61 | ll sum=0; 62 | for(ll i=m;i<=n;i++) 63 | { 64 | sum+=solve(i); 65 | } 66 | cout< 2 | #define REP(i,n) for (int i = 1; i <= n; i++) 3 | #define mod 1000000007 4 | #define pb push_back 5 | #define ff first 6 | #define ss second 7 | #define ii pair 8 | #define vi vector 9 | #define vii vector 10 | #define lli long long int 11 | #define INF 1000000000 12 | #define endl '\n' 13 | const double PI = 3.141592653589793238460; 14 | typedef std::complex Complex; 15 | typedef std::valarray CArray; 16 | 17 | using namespace std; 18 | int ar[100001] , st[400004]; 19 | 20 | void build(int si , int ss , int se) 21 | { 22 | if(ss == se) 23 | { 24 | st[si] = ar[ss]; 25 | return ; 26 | } 27 | 28 | int mid = (ss + se) / 2; 29 | build(2*si , ss , mid); 30 | build(2*si+1 ,mid+1 , se); 31 | 32 | st[si] = min(st[2*si] , st[2*si+1]); 33 | } 34 | 35 | int query(int si , int ss , int se , int qs , int qe) 36 | { 37 | if(ss > qe || se < qs) return INT_MAX; 38 | 39 | if(ss >= qs && se <= qe) return st[si]; 40 | 41 | int mid = (ss + se) / 2; 42 | 43 | return min(query(2*si , ss , mid , qs , qe) , query(2*si+1 , mid+1 , se , qs , qe)); 44 | } 45 | 46 | int main() 47 | { 48 | int n , q , l , r; 49 | cin>>n; 50 | for(int i=1;i<=n;i++) cin>>ar[i]; 51 | 52 | build(1 , 1 , n); 53 | cin>>q; 54 | 55 | while(q--) 56 | { 57 | cin>>l>>r; 58 | cout<