├── Introductory_Problems ├── Weird_Algorthim │ ├── shreyanshvinit.cpp │ ├── TheAnshul756.cpp │ └── kshitij18.py ├── Missing_Number │ ├── Ishan4041.py │ └── Ary4.cpp ├── Number_Spiral │ ├── shreyanshvinit.cpp │ └── loud-mouth.cpp ├── Two_Knights │ └── patwari26.cpp ├── Coin_Piles │ └── patwari26.cpp ├── Increasing array │ └── shreyanshyad.cpp ├── Permutations │ ├── loud-mouth.cpp │ └── kristi.java ├── Repetitions │ └── shreyanshyad.cpp ├── Bit_Strings │ └── patwari26.cpp ├── Creating_Strings_I │ └── loud-mouth.cpp ├── Trailing_Zeros │ └── AnonySharma.cpp ├── Apple_Divison │ └── loud-mouth.cpp ├── Palindrome_Reorder │ └── loud-mouth.cpp ├── Two_Sets │ └── patwari26.cpp └── Chessboard_And_Queens │ └── loud-mouth.cpp ├── Dynamic_Programming ├── Removing_Digits │ ├── vmgmreddy.cpp │ └── Ary4.cpp ├── Minimizing_Coins │ └── vmgmreddy.cpp ├── Coin_Combinations_I │ └── shreyanshyad.cpp ├── Book_Shop │ └── Ary4.cpp ├── Edit_Distance │ └── Ary4.cpp ├── Dice_Combinations │ └── jindalji-hitesh.cpp ├── Removal_Game │ └── kristi.cpp ├── Coin_Combinations_II │ └── kristi.cpp ├── Money_Sums │ └── CoderAnshu.cpp ├── Increasing_Subsequence │ └── jindalji-hitesh.cpp ├── Rectangle_Cutting │ └── CoderAnshu.cpp ├── Grid_Paths │ └── AnonySharma.cpp ├── Array_Description │ └── CoderAnshu.cpp ├── Longest_Increasing_Subsequence │ └── dhruvgupta1999.cpp ├── Projects │ └── CoderAnshu.cpp └── Two_Sets_II │ └── jindalji-hitesh.cpp ├── Sorting_and_Searching ├── Distinct_Numbers │ ├── shreyanshyad.cpp │ └── dhruvgupta1999.cpp ├── Maximum_Subarray_Sum │ └── sleepingsamurai.cpp ├── Subarray_Divisibility │ └── anshgupta551.cpp ├── Restaurant_Customers │ └── piyushmaurya.cpp ├── Stick_Lengths │ └── sleepingsamurai.cpp ├── Towers │ └── sleepingsamurai.cpp ├── Ferris_Wheel │ └── pilgrim2308.cpp ├── Array_Division │ └── anshgupta551.cpp ├── Movie_Festival │ └── pilgrim2308.cpp ├── Apartments │ └── pilgrim2308.cpp ├── Concert_Tickets │ └── pilgrim2308.cpp ├── Playlist │ └── sleepingsamurai.cpp ├── Sum_Of_Two_Values │ └── pilgrim2308.cpp ├── Traffic_Lights │ └── sleepingsamurai.cpp ├── Subarray_Sums_I │ └── lone_wolf_delena.cpp ├── Reading_books │ └── lone_wolf_delena.cpp ├── Sum_of_three_values │ └── anshgupta551.cpp ├── Factory_Machines │ └── lone_wolf_delena.cpp ├── Nearest_Smaller_Values │ └── lone_wolf_delena.cpp └── Sum_of_Four_values │ └── anshgupta551.cpp ├── Range_Queries ├── Range_Sum_1 │ └── CoderAnshu.cpp ├── Range_Xor_Queries │ └── CoderAnshu.cpp ├── Range_sum_2 │ └── CoderAnshu.cpp ├── Range_Minimum_Queries_2 │ ├── piyushmaurya.cpp │ └── CoderAnshu.cpp ├── Range_Minimum_Queries_1 │ └── CoderAnshu.cpp ├── Subarray_Sum_Queries │ └── King-01.cpp └── Distinct_Values_Queries │ └── King-01.cpp ├── Mathematics ├── Nim_Game_1 │ └── CoderAnshu.cpp ├── Counting_Divisors │ └── anuraj1183.cpp ├── Sum_of_Two_Values │ └── AnonySharma.cpp ├── Exponentiation │ └── anuraj1183.cpp ├── Exponentiation_II │ └── kristi.cpp ├── Candy_Lottery │ └── CoderAnshu.cpp ├── Christmas_Party │ └── CoderAnshu.cpp ├── Common_Divisors │ └── CoderAnshu.cpp ├── Creating_Strings_2 │ └── CoderAnshu.cpp ├── Dice_Probability │ └── CoderAnshu.cpp ├── Binomial_Coefficients │ └── anuraj1183.cpp ├── Inversion_Probability │ └── CoderAnshu.cpp ├── Distributing_Apples │ └── AnonySharma.cpp ├── Fibonacci_Numbers │ └── CoderAnshu.cpp ├── Stick_Game │ └── CoderAnshu.cpp ├── Graph_Paths_1 │ └── CoderAnshu.cpp ├── Graph_Paths_2 │ └── CoderAnshu.cpp └── Throwing_Dice │ └── CoderAnshu.cpp ├── Additional_Problems ├── Permutations_II │ └── mrsac7.cpp ├── Stick_Divisions │ └── mrsac7.cpp ├── Bit_Problem │ └── mrsac7.cpp └── Tree_Isomorphism_II │ └── mrsac7.cpp ├── String_Algorithms └── String_Matching │ └── Ary4.cpp ├── Tree_Algorithms ├── Subordinates │ └── CoderAnshu.cpp ├── Tree_Diameter │ ├── piyushmaurya.cpp │ └── anuraj1183.cpp ├── Salary_Queries │ └── CoderAnshu.cpp ├── Tree_Distances_1 │ └── CoderAnshu.cpp ├── Subtree_Queries │ └── CoderAnshu.cpp ├── Company_Queries_2 │ └── CoderAnshu.cpp ├── Company_Queries_1 │ └── CoderAnshu.cpp ├── Path_Queries │ └── CoderAnshu.cpp └── Distance_Queries │ └── CoderAnshu.cpp ├── Graph_Algorithms ├── Game_Routes │ └── TheAnshul756.cpp ├── Course_Schedule │ └── TheAnshul756.cpp ├── Investigation │ └── ritik11g.cpp ├── Shortest_Routes_II │ └── jindalji-hitesh.cpp ├── Labyrinth │ └── AnonySharma.cpp ├── Building_Roads │ └── King-01.cpp ├── Counting_Rooms │ └── King-01.cpp ├── Round_Trip_II │ └── TheAnshul756.cpp ├── Shortest_Routes_I │ └── kira-1008.cpp ├── Message_Routes │ └── King-01.cpp └── Monsters │ └── king-01.cpp ├── CONTRIBUTING.md └── README.md /Introductory_Problems/Weird_Algorthim/shreyanshvinit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | long long a; 8 | cin>>a; 9 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector dp(n+1,1e9); 8 | dp[0] = 0; 9 | for (auto i = 0; i <= n; ++i) { 10 | for (char ch : to_string(i)) { 11 | dp[i] = min(dp[i], dp[i-(ch-'0')]+1); 12 | } 13 | } 14 | 15 | cout << dp[n] << endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Sorting_and_Searching/Distinct_Numbers/shreyanshyad.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /*logic 5 | add all to set anc counts it's size 6 | */ 7 | 8 | int main() 9 | { 10 | int n; 11 | cin >> n; 12 | set st; 13 | for (int i = 0; i < n; i++) 14 | { 15 | int t; 16 | cin >> t; 17 | st.insert(t); 18 | } 19 | cout << st.size(); 20 | } -------------------------------------------------------------------------------- /Introductory_Problems/Weird_Algorthim/TheAnshul756.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define N 100005 4 | 5 | int main() 6 | { 7 | long long n; 8 | cin >> n; 9 | cout << n << " "; 10 | while(n != 1) { 11 | if(n&1) { 12 | // n is odd n:= 3 * n + 1 13 | n = 3 * n + 1; 14 | } else { 15 | // n is even n:= n / 2 16 | n/=2; 17 | } 18 | cout << n << " "; 19 | } 20 | cout << endl; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Introductory_Problems/Number_Spiral/shreyanshvinit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | int main() 6 | { 7 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 8 | ll t; 9 | cin>>t; 10 | while(t-->0) 11 | { 12 | ll y, x; 13 | cin>>y>>x; 14 | y>x ? y%2==1 ? cout<<(y-1)*(y-1)+x : cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define lli long long int 7 | #define li long int 8 | #define ld long double 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | ios_base::sync_with_stdio(false); 14 | cin.tie(NULL); 15 | lli k; 16 | cin >> k; 17 | for (lli n = 1; n <= k; n++) 18 | { 19 | cout << n * n * (n * n - 1) / 2 - 4 * (n - 1) * (n - 2) << endl; 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Introductory_Problems/Coin_Piles/patwari26.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | long long int t, a, b; 7 | cin >> t; 8 | while (t--) 9 | { 10 | cin >> a >> b; 11 | // we have to remove 3 coins in every step so (a+b)%3 should be zero 12 | // the pile with more coins should not contain greater coins than double of first pile 13 | // else it can never be removed 14 | cout << ((a + b) % 3 == 0 && (min(a, b) * 2 >= max(a, b)) ? "YES" : "NO") << endl; 15 | } 16 | } -------------------------------------------------------------------------------- /Introductory_Problems/Weird_Algorthim/kshitij18.py: -------------------------------------------------------------------------------- 1 | # recursive function to solve the problem 2 | def solve(number): 3 | # base condition when the number hits 1 4 | if number == 1: 5 | print(number, end=' ') 6 | return 7 | 8 | # printing the current number 9 | print(number, end=' ') 10 | 11 | # if the number is even 12 | if(number % 2 == 0): 13 | solve(number//2) 14 | 15 | # if the number is odd 16 | elif(number % 2 != 0): 17 | solve(number*3+1) 18 | 19 | 20 | # driver code 21 | n = int(input()) 22 | solve(n) 23 | -------------------------------------------------------------------------------- /Dynamic_Programming/Minimizing_Coins/vmgmreddy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, trg; 6 | cin >> n >> trg; 7 | vector cn(n); 8 | 9 | for (int&v : cn) { 10 | cin >> v; 11 | } 12 | 13 | vector dp(trg+1,1e9); 14 | dp[0] = 0; 15 | 16 | for (auto i = 1; i <= trg; ++i) { 17 | for (auto j = 0; j < n; ++j) { 18 | if (i-cn[j] >= 0) { 19 | dp[i] = min(dp[i], dp[i-cn[j]]+1); 20 | } 21 | } 22 | } 23 | 24 | cout << (dp[trg] == 1e9 ? -1 : dp[trg]) << endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Introductory_Problems/Increasing array/shreyanshyad.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /*logic 5 | if the next element is less than the current one, 6 | increase the current upto next and continue further 7 | */ 8 | 9 | int main() 10 | { 11 | int n; 12 | cin >> n; 13 | vector v(n); 14 | for (int i = 0; i < n; i++) 15 | cin >> v[i]; 16 | long long ans = 0; 17 | for (int i = 1; i < n; i++) 18 | { 19 | if (v[i] < v[i - 1]) 20 | ans += v[i - 1] - v[i], v[i] = v[i - 1]; 21 | } 22 | cout << ans; 23 | } -------------------------------------------------------------------------------- /Introductory_Problems/Permutations/loud-mouth.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | int main() 6 | { 7 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 8 | ll n; 9 | cin>>n; 10 | if(n==1) //corner case n==1 11 | cout<<1<<" "; 12 | if(n<=3) //corner cases n==2 or n==3 13 | { 14 | cout<<"NO SOLUTION"; 15 | } 16 | else 17 | { 18 | //just print all even numbers then all odd numbers 19 | for(ll j : {2, 1}){ 20 | for(ll i=j; i<=n; i+=2) 21 | { 22 | cout< 2 | using namespace std; 3 | 4 | /*logic 5 | while the previous char matches increase count 6 | else reset 7 | */ 8 | 9 | int main() 10 | { 11 | string s; 12 | cin >> s; 13 | int mx = 1; 14 | int cur = 1; 15 | for (int i = 0; i < (int)s.length(); i++) 16 | { 17 | if (s[i] == s[i - 1]) 18 | { 19 | cur++; 20 | mx = max(mx, cur); 21 | } 22 | else 23 | { 24 | cur = 1; 25 | } 26 | } 27 | cout << mx; 28 | } -------------------------------------------------------------------------------- /Dynamic_Programming/Coin_Combinations_I/shreyanshyad.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MOD = 1e9 + 7; 5 | 6 | int main() 7 | { 8 | int n, sum; 9 | cin >> n >> sum; 10 | vector v(n); 11 | for (int i = 0; i < n; i++) 12 | cin >> v[i]; 13 | vector dp(sum + 1); 14 | dp[0] = 1; 15 | for (int i = 1; i <= sum; i++) 16 | { 17 | for (int j = 0; j < n; j++) 18 | { 19 | if (i - v[j] >= 0) 20 | (dp[i] += dp[i - v[j]]) %= MOD; 21 | } 22 | } 23 | cout << dp[sum]; 24 | } -------------------------------------------------------------------------------- /Dynamic_Programming/Book_Shop/Ary4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(0); cout.tie(0); 7 | int n,x; 8 | cin>>n>>x; 9 | int price[n],pages[n]; 10 | for(int i=0;i>price[i]; 12 | for(int i=0;i>pages[i]; 14 | int dp[x+1]={0}; 15 | for(int i=0;i=prc;j--) 19 | { 20 | dp[j]=max(dp[j],dp[j-prc]+pages[i]); 21 | } 22 | 23 | } 24 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(0); cout.tie(0); 7 | int n; 8 | cin>>n; 9 | int val[n]; 10 | set ans; 11 | for(int i=0;i>val[i]; 13 | vector dp(n+1,n); 14 | dp[0]=0; 15 | for(int i=1;i<=n;i++) 16 | { 17 | for(int j=i;j!=0;j=j/10) 18 | { 19 | dp[i]=min(dp[i],dp[i-j%10]+1); //checking for every current digits of i 20 | } 21 | } 22 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(0); cout.tie(0); 7 | string s,t; 8 | cin>>s>>t; 9 | int n=s.length(), m=t.length(); 10 | int dp[n+1][m+1]; 11 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | const int N = 200005; 6 | 7 | long long prefix_sum[N]; 8 | int a[N]; 9 | 10 | int main() 11 | { 12 | int n,Q; 13 | scanf("%d %d",&n,&Q); 14 | 15 | for(int i=1;i<=n;++i) 16 | scanf("%d",a+i); 17 | 18 | // for answering range_sums we can just maintain the prefix_sums and then the answer for range (l,r) 19 | // would pe prefix_sum[r] - prefix_sum[l]; 20 | 21 | for(int i=1;i<=n;++i) 22 | prefix_sum[i] = prefix_sum[i-1] + a[i]; 23 | 24 | while(Q--) 25 | { 26 | int l,r; 27 | scanf("%d %d",&l,&r); 28 | 29 | printf("%lld\n",prefix_sum[r] - prefix_sum[l-1]); 30 | } 31 | } -------------------------------------------------------------------------------- /Introductory_Problems/Permutations/kristi.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | public class Permutations{ 4 | public static void main(String args[])throws IOException 5 | { 6 | InputStreamReader r=new InputStreamReader(System.in); 7 | Scanner sc=new Scanner(System.in); 8 | BufferedReader br=new BufferedReader(r); 9 | long n=Long.parseLong(br.readLine()); 10 | if(n==1) 11 | System.out.println(n); 12 | else if(n<4) 13 | System.out.println("NO SOLUTION"); 14 | 15 | else 16 | { 17 | for(int i=2; i<=n; i+=2) 18 | System.out.println(i+" "); 19 | for(int i=1; i<=n; i+=2) 20 | System.out.println(i+" "); 21 | 22 | } 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /Introductory_Problems/Bit_Strings/patwari26.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define lli long long int 7 | #define li long int 8 | #define ld long double 9 | using namespace std; 10 | const lli mod = 1e9 + 7; 11 | 12 | lli power(lli x, lli y, lli p) 13 | { 14 | // (x^y) mod p 15 | lli res = 1; 16 | x %= p; 17 | while (y) 18 | { 19 | if (y & 1) 20 | res = (res * x) % p; 21 | y >>= 1; 22 | x = (x * x) % p; 23 | } 24 | return res; 25 | } 26 | 27 | int main() 28 | { 29 | ios_base::sync_with_stdio(false); 30 | cin.tie(NULL); 31 | lli n; 32 | cin >> n; 33 | cout << power(2, n, mod); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Mathematics/Nim_Game_1/CoderAnshu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | /* This is a pretty standard game => Nim 5 | If the Xor of All The values is 0, only then it is a losing state else if the Xor of All The Numbers 6 | is non-zero then it is a winning state . 7 | For complete proofs and theory , 8 | Refer : https://cp-algorithms.com/game_theory/sprague-grundy-nim.html 9 | 10 | */ 11 | 12 | int main() 13 | { 14 | int n,TESTS; 15 | cin >> TESTS; 16 | while(TESTS --) 17 | { 18 | int Xor = 0; 19 | cin>>n; 20 | while(n--) 21 | { 22 | int a; 23 | cin>>a; 24 | Xor^=a; 25 | } 26 | cout<<(Xor?"first\n":"second\n"); 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Dynamic_Programming/Dice_Combinations/jindalji-hitesh.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | #define hell 1000000007 4 | using namespace std; 5 | int main(){ 6 | ios_base::sync_with_stdio(0); 7 | cin.tie(0); 8 | cout.tie(0); 9 | 10 | ll n; 11 | cin>>n; 12 | ll a[1000001]; 13 | //assign values from 1 to 6 14 | for(ll i=1;i<=6;i++) 15 | a[i]=pow(2,i-1); 16 | 17 | //as each value on dice ranges from 1 to 6 the answer of i will be sum of all cases in the range i-6 to i-1 18 | for(ll i=7;i<=n;i++){ // iterating from 7 to n 19 | ll sum=0; 20 | for(ll j=1;j<=6;j++) // for iterating from i-1 to i-6 21 | sum=(sum+a[i-j])%hell; 22 | a[i]=sum; 23 | } 24 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(0); cout.tie(0); 7 | int n,xr=0,sm=0,tmp; 8 | cin>>n; 9 | //algo1 10 | // for(int i=0;i>tmp; 13 | // sm+=tmp; //given all numbers except n,take sum of all the remaining numbers concept.easy but care overflow 14 | // } 15 | //algo 2 16 | for(int i=0;i>tmp; 19 | xr^=tmp; 20 | } 21 | for(int i=0;i 2 | using namespace std ; 3 | 4 | const int N=200005; 5 | int a[N]; 6 | int b[N]; 7 | 8 | /* Xor function also satisfies all properties of prefix sum type functions therefore we build 9 | 10 | b[i] = a[1]^a[2]^a[3]^......a[i] 11 | i.e Prefix-Xor upto i. 12 | 13 | Then Answer for query(l,r) i.e Xor of Subarray(l,r) = Prefix-Xor[r] ^ Prefix-Xor[l-1] 14 | */ 15 | 16 | int main() 17 | { 18 | ios_base::sync_with_stdio(0); 19 | 20 | int n,q; 21 | cin>>n>>q; 22 | for(int i=1;i<=n;++i) 23 | cin>>a[i]; 24 | for(int i=1;i<=n;++i) 25 | b[i]=b[i-1]^a[i]; 26 | while(q--) 27 | { 28 | int l,r; 29 | cin>>l>>r; 30 | cout<<(b[r]^b[l-1])<<'\n'; 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Additional_Problems/Permutations_II/mrsac7.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Problem Name: Permutations II 3 | Problem Link: https://cses.fi/problemset/task/1075 4 | Author: Sachin Srivastava (mrsac7) 5 | */ 6 | #include 7 | using namespace std; 8 | 9 | #define int long long 10 | #define endl '\n' 11 | 12 | const int md = 1e9+7; 13 | signed main(){ 14 | ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); 15 | #ifdef LOCAL 16 | freopen("input.txt", "r" , stdin); 17 | freopen("output.txt", "w", stdout); 18 | #endif 19 | 20 | //A002464 - OEIS 21 | int n; cin>>n; 22 | int dp[n+1] = {0}; 23 | dp[0] = 1, dp[1] = 1; 24 | for (int i = 4; i <= n; i++) { 25 | dp[i] = (i+1)*dp[i-1] - (i-2)*dp[i-2] 26 | - (i-5)*dp[i-3] + (i-3)*dp[i-4]; 27 | dp[i]%=md; 28 | dp[i] = (dp[i] + md)%md; 29 | } 30 | cout< 2 | #define ll long long 3 | #define s 5005 4 | using namespace std; 5 | 6 | ll dp[s][s],a[s]; 7 | 8 | ll fun(ll i,ll j) 9 | { 10 | 11 | //Base case 12 | if(i > j) 13 | return 0; 14 | 15 | //Lookup case 16 | if(dp[i][j] != -1) 17 | return dp[i][j]; 18 | 19 | //Recursion case 20 | ll op1 = a[i] + min(fun(i+2,j), fun(i+1,j-1) ); 21 | ll op2 = a[j] + min(fun(i+1,j-1), fun(i,j-2) ); 22 | 23 | return dp[i][j] = max(op1,op2); 24 | 25 | } 26 | 27 | int main() 28 | { 29 | ios_base::sync_with_stdio(false); 30 | cin.tie(NULL); 31 | cout.tie(NULL); 32 | 33 | ll n; 34 | cin>>n; 35 | 36 | for(ll i=0;i>a[i]; 38 | 39 | memset(dp,-1,sizeof(dp)); 40 | cout< 2 | #define N 2000006 3 | #define hell 10000000 4 | #define ll long long 5 | using namespace std; 6 | 7 | void solve(long long cno) 8 | { 9 | 10 | 11 | 12 | int i,n,m,k; 13 | int x; 14 | int j; 15 | cin>>n; 16 | int maxN=1e6+1; 17 | int seive[maxN]; 18 | for(i=0;i>l; 35 | cout<>TESTS; 49 | long long i=1; 50 | while(TESTS--) 51 | { 52 | solve(i); 53 | i++; 54 | } 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Additional_Problems/Stick_Divisions/mrsac7.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Problem Name: Stick Divisions 3 | Problem Link: https://cses.fi/problemset/task/1161 4 | Author: Sachin Srivastava (mrsac7) 5 | */ 6 | #include 7 | using namespace std; 8 | 9 | #define int long long 10 | #define endl '\n' 11 | 12 | signed main(){ 13 | ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); 14 | #ifdef LOCAL 15 | freopen("input.txt", "r" , stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | 19 | int k,n; cin>>k>>n; 20 | int ans = 0; 21 | multiset s; 22 | for (int i = 0; i < n; i++) { 23 | int x; cin>>x; 24 | s.insert(x); 25 | } 26 | while(s.size() > 1) { 27 | auto x = s.begin(); 28 | s.erase(x); 29 | auto y = s.begin(); 30 | s.erase(y); 31 | ans += *x+*y; 32 | s.insert(*x+*y); 33 | } 34 | cout< 2 | using namespace std; 3 | 4 | //DEFINE and TYPEDEF-------------------------------------------------------------- 5 | #define SPEEDUP ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); 6 | typedef long long int lld; 7 | //-------------------------------------------------------------------------------- 8 | 9 | int main() 10 | { 11 | SPEEDUP; 12 | lld n,x,a; 13 | cin>>n>>x; 14 | 15 | map m; // m[x] represents the index of x in the inputted array 16 | for(lld i=0;i>a; 19 | if(m.count(x-a)) // If x-a was already in the array, then (x-a)+a will result into x 20 | { 21 | cout< 2 | using namespace std; 3 | #define ll long long 4 | 5 | set se; 6 | string a; 7 | 8 | void permute(int l, int r) 9 | { 10 | // Base case 11 | if (l == r) 12 | se.insert(a); 13 | else 14 | { 15 | // Permutations made 16 | for (int i = l; i <= r; i++) 17 | { 18 | 19 | // Swapping done 20 | swap(a[l], a[i]); 21 | 22 | // Recursion called 23 | permute(l+1, r); 24 | 25 | //backtrack 26 | swap(a[l], a[i]); 27 | } 28 | } 29 | } 30 | 31 | signed main() 32 | { 33 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 34 | //string s; 35 | cin>>a; 36 | permute(0, (int)a.size()-1); 37 | cout<<(int)se.size()<<"\n"; 38 | for(string s : se) 39 | cout< 2 | using namespace std; 3 | 4 | //DEFINE and TYPEDEF-------------------------------------------------------------- 5 | #define SPEEDUP ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); 6 | #define st first 7 | #define nd second 8 | 9 | typedef long long int lld; 10 | //-------------------------------------------------------------------------------- 11 | 12 | int main() 13 | { 14 | SPEEDUP; 15 | lld n,ans=0; 16 | cin>>n; 17 | 18 | /*In high school maths, we have learned this technique, which is used to 19 | calculate trailing zeros in a factorial of a given number. 20 | I just coded that same algorithm. 21 | 22 | The algo is: 23 | Divide n by the powers of 5 and store the sum of floor of the quotients we get. 24 | */ 25 | while(n) 26 | { 27 | ans+=n/5; // i.e. same as floor(n/5) 28 | n/=5; 29 | } 30 | 31 | cout< 2 | #define N 2000006 3 | #define hell 10000000 4 | #define ll long long 5 | using namespace std; 6 | ll mod; 7 | ll power(ll n,ll x) 8 | { 9 | if(x==0) 10 | return 1; 11 | ll res=power(n,x/2); 12 | res=(res*res)%mod; 13 | if(x%2==0) 14 | return res; 15 | return (res*n)%mod; 16 | } 17 | void solve(long long cno) 18 | { 19 | 20 | 21 | 22 | int i,n,m,k; 23 | int x; 24 | cin>>n>>m; 25 | mod=1e9+7; 26 | 27 | cout<>TESTS; 62 | long long i=1; 63 | while(TESTS--) 64 | { 65 | solve(i); 66 | i++; 67 | } 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /Introductory_Problems/Number_Spiral/loud-mouth.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | int main() 6 | { 7 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 8 | ll t; 9 | cin>>t; 10 | while(t-->0) 11 | { 12 | ll y, x; 13 | cin>>y>>x; 14 | if(y > x) 15 | { 16 | if(y%2) 17 | { 18 | //square of side (y-1) has been filled 19 | //hence we just have to fill the xth row 20 | cout<<(y-1)*(y-1)+x; 21 | } 22 | else 23 | { 24 | //we fill a square of y side length 25 | //and move (x-1) steps back 26 | cout< 2 | #define ll long long 3 | #define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | fast_io; 9 | 10 | ll n,x; 11 | ll mod = 1e9+7; 12 | cin>>n>>x; 13 | 14 | vector coin(n); 15 | for(ll&i : coin) 16 | cin>>i; 17 | 18 | vector dp(x+1,0); 19 | dp[0] = 1; // ways to achieve sum of 0,using the array = 1, i.e = taking a null subset. 20 | 21 | for(ll i=0;i= 0) 26 | { 27 | dp[j] += dp[j-coin[i]]; 28 | dp[j] %= mod; 29 | } 30 | } 31 | } 32 | cout< 2 | #define ll long long 3 | #define ull unsigned long long 4 | #define endl '\n' 5 | #define pb push_back 6 | #define mp make_pair 7 | #define sp(x) fixed< 0) 19 | { 20 | if(b&1) 21 | res = (res*a)%mod; 22 | 23 | a = (a*a) % mod; 24 | b = b>>1; //remove last bit 25 | } 26 | return res; 27 | } 28 | 29 | int main() 30 | { 31 | fast_io; 32 | 33 | cin>>t; 34 | while(t--) 35 | { 36 | ll a,b,c; 37 | cin>>a>>b>>c; 38 | ll k =fun(b,c,MOD-1); // Fermat's theorem 39 | cout< 2 | using namespace std; 3 | #define int long long 4 | vector a; 5 | int n, sum=0LL; 6 | signed main() 7 | { 8 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 9 | cin>>n; 10 | for(int i=0; i>b; 14 | a.push_back(b); 15 | sum+=b; 16 | } 17 | int min=1<<30; 18 | 19 | for(int mask=1; mask<(1<> j)&1)) 27 | { 28 | tp+=a[j]; 29 | } 30 | } 31 | int cmp = abs(tp - (sum-tp)); //set difference in this case 32 | if(cmp< min){ 33 | min=cmp; 34 | } 35 | } 36 | cout< 2 | using namespace std; 3 | #define ll long long int 4 | #define pb push_back 5 | #define pi 3.141592653589 6 | #define F first 7 | #define S second 8 | 9 | ll power(ll b,ll e,ll m) 10 | { 11 | if(e==0) return 1; 12 | if(e&1) return b*power(b*b%m,e/2,m)%m; 13 | return power(b*b%m,e/2,m); 14 | } 15 | ll power(ll b,ll e) 16 | { 17 | if(e==0) return 1; 18 | if(e&1) return b*power(b*b,e/2); 19 | return power(b*b,e/2); 20 | } 21 | int main() 22 | { 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll n; 26 | cin>>n; 27 | ll arr[n]; 28 | for(ll i=0;i>arr[i]; 30 | ll maxall=arr[0],maxthis=arr[0]; // maxall give the overall maximum sum, maxthis give the maxsum till this point 31 | for(ll i=1;imaxall) 35 | maxall=maxthis; 36 | } 37 | cout< 2 | using namespace std; 3 | 4 | #define rep(i, a, b) for(int i = a; i < (b); ++i) 5 | #define trav(a, x) for(auto& a : x) 6 | #define all(x) begin(x), end(x) 7 | #define sz(x) (int)(x).size() 8 | typedef long long ll; 9 | typedef pair pii; 10 | typedef vector vi; 11 | //Applying KMP algo,refer web to learn more. divided into 2 chunks of code. 12 | vi pi(const string& s) { 13 | vi p(sz(s)); 14 | rep(i,1,sz(s)) { 15 | int g = p[i-1]; 16 | while (g && s[i] != s[g]) g = p[g-1]; 17 | p[i] = g + (s[i] == s[g]); 18 | } 19 | return p; 20 | } 21 | 22 | vi match(const string& s, const string& pat) { 23 | vi p = pi(pat + '\0' + s), res; 24 | rep(i,sz(p)-sz(s),sz(p)) 25 | if (p[i] == sz(pat)) res.push_back(i - 2 * sz(pat)); 26 | return res; 27 | } 28 | 29 | int main() { 30 | cin.sync_with_stdio(0); cin.tie(0); 31 | cin.exceptions(cin.failbit); 32 | 33 | string s, t; 34 | cin >> s >> t; 35 | cout << sz(match(s, t)) << '\n'; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Dynamic_Programming/Money_Sums/CoderAnshu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | const int N=100005; 5 | 6 | bool dp[N]; // if the sum is possible 7 | int a[101]; 8 | 9 | /* This is the most common problem of dynamic programming which is also called knapsack problem. 10 | For more details refer to : 11 | https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/ 12 | */ 13 | 14 | int main() 15 | { 16 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 17 | 18 | int n; 19 | cin>>n; 20 | for(int i=1;i<=n;++i) 21 | cin>>a[i]; 22 | 23 | // dp[i] = if sum i can be obtained or not 24 | 25 | // base condition 26 | dp[0]=true; 27 | 28 | for(int i=1;i<=n;++i) 29 | { 30 | for(int j=1e5-a[i];j>=0;--j) 31 | { 32 | dp[j+a[i]] = dp[j+a[i]] || dp[j]; 33 | } 34 | } 35 | int ans=0; 36 | for(int i=1;i 2 | using namespace std ; 3 | 4 | #define ld long double 5 | 6 | int main() 7 | { 8 | ios_base::sync_with_stdio(0); 9 | 10 | int n,k; 11 | cin>>n>>k; 12 | 13 | vector dp(k+1); 14 | 15 | // dp[i] stores the probability that each student gets <=i candies 16 | 17 | dp[0] = 0.0; 18 | 19 | for(int i=1;i<=k;++i) 20 | { 21 | // n students and probability of getting <=i candies is i/k 22 | dp[i] = pow(i*1.0/k,n); 23 | } 24 | 25 | ld ans=0; 26 | // expected value of maximum number of candies = 27 | /* 28 | E = P(max = 1)*1 + P(max = 2)*2 + ............ P(max = k)*k 29 | Here 30 | P(max = i) = dp[i] - dp[i-1] because if we subtract those cases from dp[i] where we get <= i-1 31 | candies then there is atleast one student who gets = i candies and max is therefore = i; 32 | 33 | */ 34 | 35 | for(int i=1;i<=k;++i) 36 | ans += i*(dp[i]-dp[i-1]); 37 | 38 | // seting up precision value to 6 decimal places 39 | cout< 2 | using namespace std ; 3 | 4 | #define ll long long 5 | 6 | const int NC=1000005, MOD=1e9+7; 7 | 8 | ll inv[NC],fac_inv[NC],fac[NC]; 9 | 10 | // function for precomputing the factorials and their inverse factorials modulo MOD; 11 | void precompute() 12 | { 13 | ll i; 14 | inv[1]=1; 15 | for(i=2;i<=NC-2;i++) 16 | inv[i]=(MOD-MOD/i)*inv[MOD%i]%MOD; 17 | fac[0]=fac[1]=1; 18 | for(i=2;i<=NC-2;i++) 19 | fac[i]=i*fac[i-1]%MOD; 20 | fac_inv[0]=fac_inv[1]=1; 21 | for(i=2;i<=NC-2;i++) 22 | fac_inv[i]=inv[i]*fac_inv[i-1]%MOD; 23 | } 24 | 25 | /* This is just like deranging the n objects and the formula is : 26 | 27 | D(n) = n! (1 - 1/1! + 1/2! - 1/3! .....(-1)^n/n!) 28 | */ 29 | 30 | int main() 31 | { 32 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 33 | 34 | precompute(); 35 | 36 | ll n; 37 | cin>>n; 38 | ll ans=0; 39 | for(int i=2;i<=n;++i) 40 | { 41 | ans += ((i&1)?MOD-1:1)*1LL*fac_inv[i]; 42 | ans %= MOD; 43 | } 44 | cout<<(ans*fac[n]%MOD); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Introductory_Problems/Palindrome_Reorder/loud-mouth.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | 6 | signed main() 7 | { 8 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 9 | string s; 10 | cin>>s; 11 | int cnt[26]={}; //to store count of all letters 12 | int n=(int)s.length(); 13 | for(auto ch : s) 14 | { 15 | cnt[ch-'A']++; 16 | } 17 | 18 | string ans=""; 19 | //we can have atmost 1 leter with odd frequency 20 | bool x=false; //check whether we found that odd frequency letter 21 | char ch='.'; 22 | for(int i=0; i<26; i++) 23 | { 24 | if(cnt[i]%2) 25 | { 26 | if(!x) 27 | { 28 | x=true; 29 | ch=(char)('A'+i); //store that odd character 30 | for(int j=0; j1 letter with odd frequency 36 | cout<<"NO SOLUTION"; 37 | return 0; 38 | } 39 | 40 | } 41 | else 42 | { 43 | for(int j=0; j 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define lli long long int 7 | #define li long int 8 | #define ld long double 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | ios_base::sync_with_stdio(false); 14 | cin.tie(NULL); 15 | lli n; 16 | cin >> n; 17 | lli total = n * (n + 1) / 2; 18 | if (total & 1) 19 | { 20 | cout << "NO"; 21 | } 22 | else 23 | { 24 | cout << "YES" << endl; 25 | vector arr1, arr2; 26 | total /= 2LL; 27 | while (n) 28 | { 29 | if (total - n >= 0) 30 | { 31 | arr1.push_back(n); 32 | total -= n; 33 | } 34 | else 35 | { 36 | arr2.push_back(n); 37 | } 38 | n--; 39 | } 40 | cout << arr1.size() << endl; 41 | for (li ele : arr1) 42 | cout << ele << " "; 43 | cout << endl; 44 | cout << arr2.size() << endl; 45 | for (li ele : arr2) 46 | cout << ele << " "; 47 | } 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Tree_Algorithms/Subordinates/CoderAnshu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int N = 200005; 5 | 6 | vector v[N]; 7 | 8 | int sub[N],ans[N]; 9 | 10 | /* 11 | This is a basic subtree dp 12 | 13 | Suppose we have to calculate the subtree size of a node X and we have calculated the subtree sizes of 14 | its descendents . 15 | Then our answer would be the sum of subtree_sizes of the children of the current node X 16 | 17 | sub[X] = sum_over_all_children(sub[j]) + 1(node itself); 18 | 19 | */ 20 | 21 | 22 | void dfs(int s,int p) 23 | { 24 | // include the node itself in the subtree size 25 | sub[s] = 1; 26 | 27 | for(int &j:v[s]) 28 | { 29 | if(j!=p) 30 | { 31 | dfs(j,s); 32 | sub[s] += sub[j]; 33 | } 34 | } 35 | } 36 | 37 | int main() 38 | { 39 | int n; 40 | cin>>n; 41 | for(int i=2;i<=n;++i) 42 | { 43 | int p; 44 | cin>>p; 45 | v[p].push_back(i); 46 | } 47 | dfs(1,0); 48 | 49 | for(int i=1;i<=n;++i) 50 | { 51 | // node itself should not be included 52 | cout << sub[i] -1 << " "; 53 | } 54 | cout << "\n"; 55 | } 56 | -------------------------------------------------------------------------------- /Mathematics/Common_Divisors/CoderAnshu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | const int N=1000005; 5 | 6 | int cnt[N]; 7 | 8 | // gcd of a number n with any number m is a divisor of both n and m for sure 9 | 10 | /* Here we store the count of multiples of each number which are in the array 11 | Basically , we will iterate on the divisors of the a[i] and increment their count . 12 | 13 | If any pair of elements has gcd = g then both of them should be the multiples of g . 14 | We will iterate in reverse order and print the very first number whose count > 2 because that 15 | must be the gcd of at least one pair in the array . 16 | 17 | */ 18 | 19 | int main() 20 | { 21 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 22 | int n; 23 | cin>>n; 24 | while(n--) 25 | { 26 | int a; 27 | cin>>a; 28 | for(int i=1;i*i<=a;++i) 29 | { 30 | if(a%i==0) 31 | { 32 | ++cnt[i]; 33 | if(i!=a/i) 34 | ++cnt[a/i]; 35 | } 36 | } 37 | } 38 | for(int i=1e6;i>=1;--i) 39 | { 40 | if(cnt[i]>1) 41 | { 42 | cout< 2 | 3 | #define pb push_back 4 | using namespace std; 5 | 6 | vector > g; 7 | int n; 8 | 9 | pair bfs(int root){ 10 | vector dist(n+1, 0); 11 | dist[root] = 1; 12 | queue q; 13 | q.push(root); 14 | while(!q.empty()){ 15 | int curr = q.front(); 16 | for(auto it : g[curr]){ 17 | if(dist[it] != 0){ 18 | continue; 19 | } 20 | q.push(it); 21 | dist[it] = dist[curr] + 1; 22 | } 23 | q.pop(); 24 | } 25 | int mx = INT_MIN, vertex = -1; 26 | for(int i=1 ; i<=n ; i++){ 27 | if(mx < dist[i]){ 28 | mx = dist[i]; 29 | vertex = i; 30 | } 31 | } 32 | return make_pair(mx, vertex); 33 | } 34 | 35 | int diameter(int root){ 36 | pair p1 = bfs(root), p2; 37 | p2 = bfs(p1.second); 38 | return p2.first - 1; 39 | } 40 | 41 | int main(){ 42 | cin >> n; 43 | g.resize(n+1); 44 | for(int i=0, u, v ; i> u >> v; 46 | g[u].pb(v); 47 | g[v].pb(u); 48 | } 49 | cout << diameter(1) ; 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Tree_Algorithms/Salary_Queries/CoderAnshu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | using namespace __gnu_pbds; 6 | using namespace std ; 7 | 8 | template 9 | using ordered_set = tree ,rb_tree_tag ,tree_order_statistics_node_update>; 10 | ordered_set> s; 11 | 12 | const int N=200005; 13 | 14 | int salary[N],n,q; 15 | 16 | /* This is a very basic use of policy based data structures. 17 | For more details you can refer here : 18 | https://www.geeksforgeeks.org/ordered-set-gnu-c-pbds/ 19 | */ 20 | 21 | int main() 22 | { 23 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 24 | 25 | cin>>n>>q; 26 | for(int i=1;i<=n;++i) 27 | { 28 | cin>>salary[i]; 29 | s.insert({salary[i],i}); 30 | } 31 | while(q--) 32 | { 33 | char cc; 34 | int a,b; 35 | cin>>cc>>a>>b; 36 | if(cc=='?') 37 | { 38 | cout< 2 | #define F first 3 | #define S second 4 | #define endl "\n" 5 | #define Endl "\n" 6 | #define ll long long 7 | using namespace std; 8 | int main(){ 9 | ios_base::sync_with_stdio(0); 10 | cin.tie(NULL); 11 | cout.tie(NULL); 12 | /* 13 | In this approach we maintain a set of numbers 14 | if a number is the greatest till now it will be simply added to set 15 | else 16 | it will be added and next greater element will be removed from the set 17 | set would not contain the actual subsequence but its size will properly give its length 18 | case1 can be easily proved for its correctness 19 | in case 2 our previous lis would be still there as size of set does not change but if actual lis is not the previous lis 20 | the further elements will automatically eleminate the elements of previous lis 21 | */ 22 | ll n,i; 23 | cin>>n; 24 | ll a[n]; 25 | set s; 26 | for(i=0;i>a[i]; 28 | if(s.insert(a[i]).S){ //check if the element was not present before 29 | auto k=s.find(a[i]); 30 | k++; 31 | if(k!=s.end()) 32 | s.erase(k); //erase the element in the set which is just greater than a[i] 33 | } 34 | } 35 | cout< 2 | using namespace std; 3 | #define N 100005 4 | 5 | int dp[N]; 6 | vector a[N]; 7 | bool vis[N]; 8 | stack st; 9 | 10 | 11 | void dfs(int node) { 12 | vis[node] = 1; 13 | for(auto i : a[node]) { 14 | if(!vis[i]) { 15 | dfs(i); 16 | } 17 | } 18 | st.push(node); 19 | } 20 | 21 | void solve() 22 | { 23 | /* 24 | Basic idea is to Topological sort the whole graph. Then apply dynamic programming to calcualte the number of 25 | ways where dp[i] denotes number of ways to go to n from i. This dp[n] would be initailsed 1. And 26 | dp[node] = sum(dp[i]) for all childerns of node. 27 | */ 28 | int n,m,x,y; 29 | cin >> n >> m; 30 | for(int i = 0; i < m; i++) { 31 | cin >> x >> y; 32 | a[x].push_back(y); 33 | } 34 | for(int i = 1; i <= n; i++) { 35 | if(!vis[i]) { 36 | dfs(i); 37 | } 38 | } 39 | 40 | dp[n] = 1; 41 | vector tmp; 42 | 43 | while(!st.empty()) { 44 | tmp.push_back(st.top()); 45 | st.pop(); 46 | } 47 | 48 | for(int z = tmp.size() - 1; z >= 0; z--) { 49 | int node = tmp[z]; 50 | for(auto i:a[node]) { 51 | dp[node] += dp[i]; 52 | dp[node] %= 1000000007; 53 | } 54 | } 55 | 56 | cout<< dp[1] << endl; 57 | return; 58 | } 59 | int main() 60 | { 61 | solve(); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /Mathematics/Creating_Strings_2/CoderAnshu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | #define ll long long 5 | 6 | const int NC = 1000005; 7 | const int MOD = 1e9+7; 8 | 9 | // precomputing factorials and their inverse factorials 10 | 11 | ll inv[NC],fac_inv[NC],fac[NC]; 12 | void initialize() 13 | { 14 | ll i; 15 | inv[1]=1; 16 | for(i=2;i<=NC-2;i++) 17 | inv[i]=(MOD-MOD/i)*inv[MOD%i]%MOD; 18 | fac[0]=fac[1]=1; 19 | for(i=2;i<=NC-2;i++) 20 | fac[i]=i*fac[i-1]%MOD; 21 | fac_inv[0]=fac_inv[1]=1; 22 | for(i=2;i<=NC-2;i++) 23 | fac_inv[i]=inv[i]*fac_inv[i-1]%MOD; 24 | } 25 | 26 | /* This is a basic question of permutations 27 | Number of ways to arrange n objects where a1,a2 .. objects are of one kind = n! / (a1! * a2! * a3!...) 28 | 29 | */ 30 | 31 | 32 | int main() 33 | { 34 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 35 | 36 | initialize(); 37 | 38 | ll ans=1; 39 | string s; 40 | cin>>s; 41 | 42 | // count of each character 43 | vector cnt(26,0); 44 | for(auto j:s) 45 | cnt[j-'a']++; 46 | 47 | ans=fac[s.size()]; // factorial(n); 48 | 49 | for(int i=0;i<26;++i) 50 | ans*=fac_inv[cnt[i]],ans%=MOD; 51 | 52 | cout< 2 | using namespace std ; 3 | #define ll long long 4 | #define pb push_back 5 | #define in(x) ll x; cin>>x; 6 | #define ln(x) long long x; cin>>x; 7 | #define memf(a) memset(a,false,sizeof(a)) 8 | #define F first 9 | #define S second 10 | #define endl "\n" 11 | #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 12 | ll power(ll b,ll e,ll m) 13 | { 14 | if(e==0) return 1; 15 | if(e&1) return b*power(b*b%m,e/2,m)%m; 16 | return power(b*b%m,e/2,m); 17 | } 18 | ll power( ll b, ll e) 19 | { 20 | if(e==0) return 1; 21 | if(e&1) return b*power(b*b,e/2); 22 | return power(b*b,e/2); 23 | } 24 | int main() 25 | { 26 | fast 27 | ll n; 28 | cin>>n; 29 | ll a[n]; 30 | for(ll i=0;i>a[i]; 32 | map m; // creating a map to count frequency of remainders 33 | ll presum[n+1]; // to store prefix sums 34 | presum[0]=0; 35 | for(ll i=1;i<=n;i++) 36 | { 37 | presum[i]=presum[i-1]+a[i-1]; 38 | m[((presum[i]%n)+n)%n]++; 39 | } 40 | ll ans=0; 41 | for(ll i=0;i 2 | using namespace std; 3 | 4 | const int N = 502; 5 | 6 | int dp[N][N]; 7 | 8 | /* Here dp[i][j] => minimum number of moves to cut a rectangle of dimension ixj into squares 9 | 10 | We will iterate on the horizontal cut and similarly on the vertical cut and sum up the DP's of the 11 | two obtained rectangles and take minimum over all possible cuts. 12 | 13 | For example : if rectangle is 3 x 4 and we make a cut to reduce it into 1 x 4 and 2 x 4 14 | then we would say dp[3][4] = min(dp[3][4],dp[1][4] + dp[2][4] + 1); 15 | */ 16 | 17 | int main() 18 | { 19 | int a,b; 20 | cin>>a>>b; 21 | 22 | for(int i=1;i<=a;++i) 23 | { 24 | for(int j=1;j<=b;++j) 25 | { 26 | dp[i][j] = 1e9; // initial value is infinity 27 | 28 | if(i==j) // if it is already a square 29 | dp[i][j] = 0; 30 | 31 | // horizontal cut 32 | for(int k=1;k<=i-1;++k) 33 | dp[i][j] = min(dp[i][j], dp[k][j] + dp[i-k][j] + 1); 34 | 35 | // vertical cut 36 | for(int k=1;k<=j-1;++k) 37 | dp[i][j] = min(dp[i][j] , dp[i][k] + dp[i][j-k] + 1); 38 | 39 | } 40 | } 41 | 42 | cout << dp[a][b] << "\n"; 43 | } 44 | -------------------------------------------------------------------------------- /Sorting_and_Searching/Restaurant_Customers/piyushmaurya.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ios \ 4 | ios_base::sync_with_stdio(false); \ 5 | cin.tie(0); \ 6 | cout.tie(0); 7 | #define ll long long 8 | #define endl '\n' 9 | #define pii pair 10 | #define all(a) (a).begin(), (a).end() 11 | #define F first 12 | #define S second 13 | #define pb push_back 14 | 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | ios 20 | ll n; 21 | cin >> n; 22 | // This vector of pairs will store entry and exit times 23 | // with a flag representing the type of timestamp 24 | // 0 represents entry and 1 represents exit 25 | vector> v; 26 | for (ll i = 0; i < n; i++) 27 | { 28 | ll x, y; 29 | cin >> x >> y; 30 | v.pb({x, 0}); 31 | v.pb({y, 1}); 32 | } 33 | // All the combined timestamps should be sorted for a sweep 34 | sort(all(v)); 35 | ll cur = 0, ans = 0; 36 | // Doing a sweep on entry and exit times keeping track of 37 | // maximum number of customers 38 | for (auto x : v) 39 | { 40 | if (x.S == 0) 41 | cur++; 42 | else 43 | cur--; 44 | ans = max(ans, cur); 45 | } 46 | cout << ans; 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Mathematics/Dice_Probability/CoderAnshu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | #define ld long double 5 | #define ll long long 6 | 7 | // Basic Dynamic Programming Problem 8 | 9 | int main() 10 | { 11 | ios_base::sync_with_stdio(0); 12 | 13 | int n,a,b; 14 | cin>>n>>a>>b; 15 | vector> dp(n+1,vector(6*n+1,0)); 16 | dp[0][0]=1; 17 | 18 | /* dp[i][j] = Probability that the sum obtained is j after throwing the dice i times */ 19 | // Transitions are quite simple 20 | 21 | for(int i=1;i<=n;++i) 22 | { 23 | // i-> represents the the current number of turn 24 | for(int j=1;j<=6;++j) 25 | { 26 | // j-> number obtained in the current turn 27 | for(int k=0;k<=6*n;++k) 28 | { 29 | // k-> sum obtained uptill now 30 | if(k+j<=6*n) 31 | { 32 | dp[i][k+j]+=1.0/6*dp[i-1][k]; // probability of obtaining each number if 1/6; 33 | } 34 | } 35 | } 36 | } 37 | ld ans=0; 38 | // need to calculate sum of probabilities where total_sum is between A and B 39 | for(int i=a;i<=b;++i) 40 | ans+=dp[n][i]; 41 | 42 | // setting up the precision to 6 decimal places 43 | cout< 2 | using namespace std; 3 | #define ll long long int 4 | #define pb push_back 5 | #define pi 3.141592653589 6 | #define F first 7 | #define S second 8 | 9 | ll power(ll b,ll e,ll m) 10 | { 11 | if(e==0) return 1; 12 | if(e&1) return b*power(b*b%m,e/2,m)%m; 13 | return power(b*b%m,e/2,m); 14 | } 15 | ll power(ll b,ll e) 16 | { 17 | if(e==0) return 1; 18 | if(e&1) return b*power(b*b,e/2); 19 | return power(b*b,e/2); 20 | } 21 | int main() 22 | { 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll n; 26 | cin>>n; 27 | ll arr[n]; 28 | for(ll i=0;i>arr[i]; 30 | sort(arr,arr+n); //sorting to find median 31 | if(n%2==0) 32 | { 33 | ll mid1=arr[n/2]; 34 | ll mid2=arr[(n/2)-1]; //2 medians if n is even 35 | ll val1=0,val2=0; 36 | for(ll i=0;i 6 | #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 7 | #define endl "\n" 8 | #define ll long long int 9 | #define vll vector 10 | #define pll pair 11 | #define mod 1000000007 12 | #define inf 9000000000000000001 13 | #define rep(i,a,b) for(ll i=a;i=a;i--) 15 | #define all(c) c.begin(),c.end() 16 | #define mp(x,y) make_pair(x,y) 17 | #define mem(a,val) memset(a,val,sizeof(a)) 18 | #define eb emplace_back 19 | #define f first 20 | #define s second 21 | #define sz(x) ((ll)x.size()) 22 | #define DEBUG 23 | #ifdef DEBUG 24 | #define dbg(x) cout << (#x) << " is " << (x) << endl 25 | #else 26 | #define dbg(x) 27 | #endif 28 | #define N 200002 29 | 30 | using namespace std; 31 | int main() 32 | { 33 | fastio 34 | ll T=1,n,m;string str; 35 | // cin>>T; 36 | // cin.ignore(); put after cin, just b4 a getline(cin, s) to avoid getline() directly getting \n 37 | while(T--) 38 | { 39 | ll p=0,q=0,tmp=0,sum=0,cnt=0; //tmp variable 40 | int flag=0; 41 | 42 | cin>>n; 43 | ll a[n]; 44 | set s; 45 | for(ll i=0;i < n;i++) 46 | { 47 | cin>>a[i]; 48 | s.insert(a[i]); 49 | } 50 | 51 | cout << s.size() << endl; 52 | 53 | 54 | 55 | 56 | } 57 | return 0; 58 | } -------------------------------------------------------------------------------- /Mathematics/Binomial_Coefficients/anuraj1183.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 2000006 3 | #define hell 1000000007 4 | #define maxN 1000000+10 5 | #define ll long long 6 | #define mod (ll)1e9+7 7 | using namespace std; 8 | 9 | 10 | ll f[maxN]; 11 | 12 | ll power(ll n,ll x) 13 | { 14 | if(x==0) 15 | return 1; 16 | if(x==1) 17 | return n%hell; 18 | ll res=power(n,x/2)%hell; 19 | res=(res*res)%hell; 20 | if(x%2==0) 21 | return res; 22 | return (res*n)%hell; 23 | } 24 | ll ncr(ll n, ll r) 25 | { 26 | if(r==0 || r==n) 27 | return 1; 28 | if(r==1) 29 | { 30 | return (n)%hell; 31 | } 32 | 33 | ll ans=(f[n]*power(f[r],hell-2))%hell; // (1/a) % b = power(a,b-2) % b if gcd(a,b)==1 34 | ans=(ans*power(f[n-r],hell-2))%hell; 35 | return ans; 36 | } 37 | void solve(long long cno) 38 | { 39 | 40 | 41 | 42 | long long n,i,j,k,l; 43 | long long ans; 44 | 45 | 46 | f[0]=f[1]=1; 47 | for(i=2;i>n; 53 | while(n--) 54 | { 55 | ll a,b; 56 | cin>>a>>b; 57 | cout<>TESTS; 70 | long long i=1; 71 | while(TESTS--) 72 | { 73 | solve(i); 74 | i++; 75 | } 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /Mathematics/Inversion_Probability/CoderAnshu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | #define ll long long 5 | 6 | int main() 7 | { 8 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 9 | 10 | 11 | int n; 12 | cin>>n; 13 | vector r(n+1,0); 14 | for(int i=1;i<=n;++i) 15 | cin>>r[i]; 16 | long double ans=0; 17 | 18 | /* Number of inversions depends only on the pair of indexes and therefore we iterate over all pairs 19 | and add to answer the probability of current pair to be the inversion and since only one inversion 20 | is counted in every case we simply add P(is_inversion)*1 21 | */ 22 | for(int i=1;i<=n;++i) 23 | { 24 | for(int j=i+1;j<=n;++j) 25 | { 26 | //(i,j) pair of indices 27 | for(int k=1;k<=r[i];++k) 28 | { 29 | // range of a[i] 30 | for(int l=1;l<=r[j];++l) 31 | { 32 | // range of a[j] 33 | if(k>l) 34 | { 35 | // if is a inversion 36 | ans+=1.0/r[i]/r[j]; 37 | } 38 | } 39 | } 40 | } 41 | } 42 | 43 | // setting up the precision to 6 decimal places 44 | cout< 2 | using namespace std; 3 | #define ll long long int 4 | #define pb push_back 5 | #define pi 3.141592653589 6 | #define F first 7 | #define S second 8 | 9 | ll power(ll b,ll e,ll m) 10 | { 11 | if(e==0) return 1; 12 | if(e&1) return b*power(b*b%m,e/2,m)%m; 13 | return power(b*b%m,e/2,m); 14 | } 15 | ll power(ll b,ll e) 16 | { 17 | if(e==0) return 1; 18 | if(e&1) return b*power(b*b,e/2); 19 | return power(b*b,e/2); 20 | } 21 | int main() 22 | { 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll n; 26 | cin>>n; 27 | ll arr[n]; 28 | for(ll i=0;i>arr[i]; 30 | ll c=1; 31 | multiset s; //multiset s store the number of towers 32 | s.insert(arr[0]); 33 | for(ll i=1;i 2 | #define N 2000006 3 | #define hell 1000000007 4 | #define maxN 10000000+10 5 | #define ll long long 6 | #define mod (ll)1e9+7 7 | using namespace std; 8 | vector adj[maxN]; 9 | ll depth[maxN]={0}; 10 | ll n,m; 11 | 12 | /* first make dfs call from any node and get the farthest node from that root node*/ 13 | /* then take this farthest node as root node and then do another dfs and get farthest node*/ 14 | /*distance between these two node will be the diameter of the tree*/ 15 | void dfs(ll root,ll par=-1) 16 | { 17 | if(par==-1) 18 | depth[root]=0; 19 | else 20 | depth[root]=depth[par]+1; 21 | for(auto x:adj[root]) 22 | { 23 | if(x!=par) 24 | { 25 | dfs(x,root); 26 | } 27 | } 28 | } 29 | void solve(long long cno) 30 | { 31 | 32 | cin>>n; 33 | for(ll i=0;i>j>>k; 37 | j--;k--; 38 | adj[j].push_back(k); 39 | adj[k].push_back(j); 40 | } 41 | 42 | dfs(0); 43 | 44 | int root=-1; 45 | int mx=0; 46 | for(auto i=0;imx) 49 | { 50 | mx=depth[i]; 51 | root=i; 52 | } 53 | } 54 | dfs(root); 55 | 56 | cout<<*max_element(depth,depth+n); 57 | 58 | } 59 | 60 | 61 | 62 | int main() 63 | { 64 | ios_base::sync_with_stdio(false); 65 | cin.tie(NULL); 66 | cout.tie(NULL); 67 | long long TESTS=1; 68 | // cin>>TESTS; 69 | long long i=1; 70 | while(TESTS--) 71 | { 72 | solve(i); 73 | i++; 74 | } 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /Dynamic_Programming/Grid_Paths/AnonySharma.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //DEFINE and TYPEDEF-------------------------------------------------------------- 5 | #define SPEEDUP ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); 6 | #define st first 7 | #define nd second 8 | 9 | typedef long long int lld; 10 | #define mod 1000000007 11 | //-------------------------------------------------------------------------------- 12 | 13 | int main() 14 | { 15 | SPEEDUP; 16 | lld n; 17 | cin>>n; 18 | 19 | string s[n]; 20 | for(lld i=0;i>s[i]; 22 | 23 | lld dp[n+2][n+2]; //dp[i][j] is the number of ways to visit s[i][j] from the upper left square 24 | memset(dp,0,sizeof(dp)); 25 | dp[0][0]=1; //We are already at upper left square 26 | 27 | for(lld i=0;i 2 | using namespace std ; 3 | 4 | const int mod = 1e9+7; 5 | const int N=100005; 6 | 7 | int dp[N][101]; 8 | int a[N]; 9 | 10 | // Pretty standard dynamic programming 11 | 12 | int main() 13 | { 14 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 15 | 16 | int n,m; 17 | cin>>n>>m; 18 | for(int i=1;i<=n;++i) 19 | cin>>a[i]; 20 | 21 | /* Here dp[i][j] = number of ways to fill the first i elements such that the last element is j */ 22 | 23 | /* If the last element is j then the second last element can be only j , j-1 , j+1 obviously 24 | => dp[i][j] = dp[i-1][j-1] + dp[i-1][j] + dp[i-1][j+1]; 25 | */ 26 | 27 | /* Base conditon */ 28 | if(a[1]==0) 29 | { 30 | // every integer from 1 to n is possible 31 | for(int i=1;i<=m;++i) 32 | dp[1][i]=1; 33 | } 34 | else 35 | { 36 | dp[1][a[1]]=1; 37 | } 38 | 39 | for(int i=2;i<=n;++i) 40 | { 41 | for(int j=1;j<=m;++j) 42 | { 43 | // check if a[i]==0 || a[i]==j i.e j is feasible as last element or not 44 | if(a[i]==0 || a[i]==j) 45 | { 46 | dp[i][j] = ((dp[i-1][j-1]+dp[i-1][j+1])%mod+dp[i-1][j])%mod; 47 | } 48 | } 49 | } 50 | int ans=0; 51 | for(int i=1;i<=m;++i) 52 | { 53 | ans += dp[n][i]; 54 | ans %= mod; 55 | } 56 | cout< 7 | using namespace std; 8 | 9 | #define int long long 10 | #define endl '\n' 11 | 12 | const int mxN = 1<<21; 13 | int freq[mxN]; 14 | int dp1[mxN][21], dp2[mxN][21]; 15 | signed main(){ 16 | ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); 17 | #ifdef LOCAL 18 | freopen("input.txt", "r" , stdin); 19 | freopen("output.txt", "w", stdout); 20 | #endif 21 | 22 | int n; cin>>n; 23 | int a[n]; 24 | for (int i = 0; i < n; i++) { 25 | int x; cin>>x; 26 | a[i] = x; 27 | freq[x]++; 28 | } 29 | for (int i = 1; i < mxN>>1; i++) { 30 | dp1[i][0] = freq[i]; 31 | if (i&1) 32 | dp1[i][0] += freq[i^1]; 33 | for (int k = 1; k < 21; k++) { 34 | dp1[i][k] = dp1[i][k-1]; 35 | if (i>>k&1) 36 | dp1[i][k] += dp1[i^1<>1; i > 0; i--) { 40 | dp2[i][0] = freq[i]; 41 | if (i&1^1) 42 | dp2[i][0] += freq[i^1]; 43 | for (int k = 1; k < 21; k++) { 44 | dp2[i][k] = dp2[i][k-1]; 45 | if (i>>k&1^1) 46 | dp2[i][k] += dp2[i^1<>1) - 1)][20]< 2 | 3 | using namespace std; 4 | class LongestIncreasingSubsequence { 5 | public: 6 | 7 | int binSearch(int dp[],int start,int end, int val ) 8 | { 9 | int lo = start,hi = end; 10 | int mid; 11 | while(loval) 15 | { 16 | hi = mid; 17 | } 18 | else if(dp[mid]& nums) { 29 | 30 | int n = nums.size(); 31 | if(!n) return 0; 32 | int dp[n]; 33 | // memset(dp,-1,sizeof(dp)); 34 | int mx = 1; 35 | dp[0] = nums[0]; 36 | for(int i=1;i> n; 55 | int tmp; 56 | vector nums; 57 | for(int i=0;i> tmp; nums.push_back(tmp); 60 | } 61 | cout << lengthofLIS(nums) << endl; 62 | } 63 | }; 64 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | 2 | # Contribution Guidelines 3 | Thanks for taking the time to contribute to this repo. Following are some of the rules that should be followed before making a PR:- 4 | 5 | ### Coding Rules to Follow:- 6 | 7 | * Proper Indentation is must. 8 | * Include comments whereever required so as to make the code easily understandable. 9 | * Place your code in right directory. 10 | * **Name your code file with your username. For ex: TheAnshul756.cpp** 11 | * Proper naming of variables and functions is must throughout the code. 12 | 13 | ### Editorial of Problems:- 14 | 15 | * Create a markdown file for editorial. 16 | * Try to write a clear and consice solution. 17 | * Write proper Proof and Justificatioion if required. 18 | * Place your editorial in right directory. 19 | * **Name your editorial file with your username. For ex: TheAnshul756.md** 20 | * Add link to your implimentation if required. 21 | * You can also add more to an existing editorial. 22 | 23 | ### How to Contribute? 24 | 25 | - Fork this Repo. 26 | - Clone the repo to your local machine. 27 | - Create a new directory(if it doesn't exist) in the relevant folder using the problem name as the directory name. 28 | For example, 29 | 30 | >"# Weird_Algorithm". 31 | 32 | - Add your code file in this folder only. 33 | - Push your changes to your Github repo. 34 | - Create a Pull Request(PR). 35 | - One PR should be created for only one problem. 36 | - If your PR is valid, it'll be merged to the master branch. 37 | 38 | 39 | _Please make sure, all code files should be categorized in the same order as mentioned in the above sheet._ 40 | -------------------------------------------------------------------------------- /Range_Queries/Range_sum_2/CoderAnshu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int N = 200005; 5 | 6 | // BIT -> Binary Indexed Tree 7 | // can support point update and range_sum queries 8 | template 9 | struct fenwick { 10 | T a[N],bit[N]; 11 | int n; 12 | fenwick() 13 | { 14 | for(int i=1;i<=N-2;++i) 15 | a[i] = T(0),bit[i] = T(0); 16 | } 17 | void build(int n_) 18 | { 19 | n = n_; 20 | for(int i=1;i<=n;++i) 21 | for(int j=i;j<=n;j+=j&-j) 22 | bit[j] += a[i]; 23 | } 24 | void update(int j,T val) 25 | { 26 | // adds val to a[j] -> a[j] += val; 27 | for(;j<=n;j+=j&-j) 28 | bit[j] += val; 29 | } 30 | T get(int r) 31 | { 32 | T u = 0; 33 | for(;r;r-=r&-r) 34 | u += bit[r]; 35 | return u; 36 | } 37 | T query(int l,int r) 38 | { 39 | return get(r)-get(l-1); 40 | } 41 | }; 42 | 43 | fenwick<> fenw; 44 | // call fenw.build(n); 45 | 46 | int main() 47 | { 48 | int n,Q; 49 | cin>>n>>Q; 50 | for(int i=1;i<=n;++i) 51 | { 52 | // initializing the fenwick tree 53 | cin>>fenw.a[i]; 54 | } 55 | fenw.build(n); 56 | while(Q--) 57 | { 58 | int tt,l,r; 59 | cin>>tt>>l>>r; 60 | if(tt==1) 61 | { 62 | // point update in BIT 63 | fenw.update(l,r-fenw.a[l]); 64 | fenw.a[l] = r; 65 | } 66 | else 67 | cout< 2 | //#include 3 | #include 4 | using namespace std; 5 | 6 | #define ll long long 7 | typedef vector vi; 8 | typedef vector vvi; 9 | #define F first 10 | #define S second 11 | #define all(v) v.begin(),v.end() 12 | #define rep(i,n) for(ll i=0;i=0;i--) 16 | #define mp make_pair 17 | #define pb push_back 18 | typedef pair > pii; 19 | #define inf 1e10 20 | 21 | bool prime[100005]={false}; 22 | void sieve(){ 23 | prime[0]=true; 24 | prime[1]=true; 25 | for(ll i=2;i<100005;i++){ 26 | if(prime[i]==false){ 27 | for(ll j=i*i;j<100005;j+=i) 28 | prime[j]=true; 29 | } 30 | } 31 | 32 | } 33 | 34 | 35 | void solve(){ 36 | ll n,x,cnt=0; 37 | cin>>n>>x; 38 | ll p[n]; 39 | rep(i,n) 40 | cin>>p[i]; 41 | sort(p,p+n); //sorting kids by increasing weight 42 | for(ll i=0,j=n-1;i<=j;){ 43 | if(p[i]+p[j]<=x){ //taking one light kid 44 | cnt++; //and heavy kid 45 | i++; 46 | j--; 47 | } 48 | else{ //if sum of weights is greater than x 49 | cnt++; //take a kid just lighter than j 50 | j--; 51 | } 52 | } 53 | cout<>T; 60 | while(T--){ 61 | solve();} 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Graph_Algorithms/Course_Schedule/TheAnshul756.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define N 100005 4 | 5 | vector req[N]; 6 | int visited[N]; 7 | bool isPossible; 8 | stack topoOrder; 9 | 10 | void dfs(int node) { 11 | visited[node] = 1; 12 | 13 | for(auto i : req[node]) { 14 | if(!visited[i]) { 15 | dfs(i); 16 | } else if(visited[i] == 1) { 17 | isPossible = 0; 18 | } 19 | } 20 | 21 | visited[node] = 2; 22 | } 23 | 24 | void topoSort(int node) { 25 | visited[node] = 1; 26 | for(auto i : req[node]) { 27 | if(!visited[i]) { 28 | topoSort(i); 29 | } 30 | } 31 | topoOrder.push(node); 32 | } 33 | 34 | // Soultion Start here 35 | void solve() 36 | { 37 | /* 38 | All courses and there requirements will form a directed graph. 39 | We first check if there exist a cycle in this directed graph. If yes, then its impossible to assign subjects. 40 | else we will topological sort the graph and print it. 41 | */ 42 | int n,m; 43 | cin >> n >> m; 44 | 45 | int a,b; 46 | for(int i = 0; i < m; i++) { 47 | cin >> a >> b; 48 | req[a].push_back(b); 49 | } 50 | 51 | isPossible = 1; 52 | 53 | for(int i = 1; i <= n; i++) { 54 | if(!visited[i]) { 55 | dfs(i); 56 | } 57 | } 58 | 59 | if(!isPossible) { 60 | cout << "IMPOSSIBLE\n"; 61 | return; 62 | } 63 | 64 | memset(visited,0,sizeof(int)*(n+1)); 65 | for(int i = 1; i <= n; i++) { 66 | if(!visited[i]) { 67 | topoSort(i); 68 | } 69 | } 70 | 71 | while(!topoOrder.empty()) { 72 | cout << topoOrder.top() << " "; 73 | topoOrder.pop(); 74 | } 75 | cout << endl; 76 | 77 | return; 78 | } 79 | 80 | int main() 81 | { 82 | solve(); 83 | return 0; 84 | } -------------------------------------------------------------------------------- /Dynamic_Programming/Projects/CoderAnshu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int N = 200005; 5 | 6 | long long dp[N]; 7 | 8 | struct node 9 | { 10 | int l,r,p; 11 | }; 12 | // denoting a project 13 | 14 | int main() 15 | { 16 | int n; 17 | cin>>n; 18 | 19 | vector v(n+1); 20 | 21 | for(int i=1;i<=n;++i) 22 | cin>>v[i].l>>v[i].r>>v[i].p; 23 | 24 | // sorting according to endpoints 25 | sort(v.begin(),v.end(),[&](node a,node b) 26 | { 27 | return a.r maximum amount of reward we can earn upto day v[i].r (i.e) using only projects from 1 to i. 31 | 32 | /* Transitions : 33 | dp[i] = dp[i-1] => if current project is skipped 34 | dp[i] = v[i].p ( reward for current project ) + (now we cant do any projects whose r > v[i].l)dp[j] 35 | where j is the last index where v[j].r < v[i].l 36 | Because we can't take any project from v[i].l to v[i].r and the previous ones are maintained in 37 | DP. 38 | For index j , we will binary search as v[i].r is monotonic . 39 | */ 40 | 41 | dp[0] = 0; 42 | for(int i=1;i<=n;++i) 43 | { 44 | dp[i] = dp[i-1]; 45 | // binary search for j 46 | int lo = 1,hi = i-1,j = 0; 47 | while(lo<=hi) 48 | { 49 | int mid = (lo+hi)/2; 50 | if(v[mid].r 2 | using namespace std ; 3 | #define ll long long 4 | #define pb push_back 5 | #define in(x) ll x; cin>>x; 6 | #define ln(x) long long x; cin>>x; 7 | #define memf(a) memset(a,false,sizeof(a)) 8 | #define F first 9 | #define S second 10 | #define endl "\n" 11 | #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 12 | ll power(ll b,ll e,ll m) 13 | { 14 | if(e==0) return 1; 15 | if(e&1) return b*power(b*b%m,e/2,m)%m; 16 | return power(b*b%m,e/2,m); 17 | } 18 | ll power( ll b, ll e) 19 | { 20 | if(e==0) return 1; 21 | if(e&1) return b*power(b*b,e/2); 22 | return power(b*b,e/2); 23 | } 24 | ll n,k; 25 | ll a[200005]; 26 | bool possible(ll x) // whether that particlar max value is feasible within k divisions or not 27 | { 28 | ll i=0; 29 | ll z=0; 30 | ll xx=1; 31 | while(i>n; 55 | cin>>k; 56 | ll mx=0; 57 | for(ll i=0;i>a[i];mx=max(mx,a[i]);} 59 | ll l=mx,r=1e15; //the range in which our answer lies 60 | ll mid; 61 | while(l<=r) //applying binary search to find our answer 62 | { 63 | mid=(l+r)/2; 64 | if(l==r) 65 | break; 66 | if(l+1==r) 67 | { 68 | if(possible(l)) 69 | { 70 | mid=l; 71 | break; 72 | } 73 | else if(possible(r)) 74 | { 75 | mid=r; 76 | break; 77 | } 78 | } 79 | if(possible(mid)) 80 | { 81 | r=mid; 82 | } 83 | else l=mid; 84 | } 85 | cout< 2 | //#include 3 | #include 4 | using namespace std; 5 | 6 | #define ll long long 7 | typedef vector vi; 8 | typedef vector vvi; 9 | #define F first 10 | #define S second 11 | #define all(v) v.begin(),v.end() 12 | #define rep(i,n) for(ll i=0;i=0;i--) 16 | #define mp make_pair 17 | #define pb push_back 18 | typedef pair > pii; 19 | #define inf 1e10 20 | 21 | bool prime[100005]={false}; 22 | void sieve(){ 23 | prime[0]=true; 24 | prime[1]=true; 25 | for(ll i=2;i<100005;i++){ 26 | if(prime[i]==false){ 27 | for(ll j=i*i;j<100005;j+=i) 28 | prime[j]=true; 29 | } 30 | } 31 | 32 | } 33 | 34 | 35 | void solve(){ 36 | ll n,a,b,ls=0,cnt=0; 37 | vi p; 38 | vector > q; 39 | cin>>n; 40 | rep(i,n){ 41 | cin>>a>>b; 42 | p.pb(a); //p contains starting times of all movies 43 | q.pb(mp(b,i)); //q contains pairs of ending time and movie number 44 | } 45 | sort(q.begin(),q.end()); 46 | for(auto u:q){ 47 | if(p[u.S]>=ls){ //if starting time of (u.S)th movie is greater than 48 | cnt++; //ending time of last watched movie, 49 | ls=u.F; //then increase count of movies and 50 | } //update ending time of last watched movie 51 | } 52 | cout<>T; 59 | while(T--){ 60 | solve();} 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Introductory_Problems/Chessboard_And_Queens/loud-mouth.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define int long long 4 | vector a; 5 | char ch[8][8]; 6 | int ans=0; 7 | void permute(int l, int r) 8 | { 9 | // Base case 10 | if (l == r) 11 | { 12 | //ans+=1; 13 | bool rc[16]={0}, lc[16]={0}; 14 | for(int i=0; i<8; ++i) 15 | { 16 | //ans+=1; 17 | int j = a[i]; 18 | if(ch[i][j] == '*') 19 | { 20 | return; 21 | } 22 | if(rc[i+j]==false) 23 | rc[i+j]=true; 24 | else return; 25 | if(lc[i-j+7]==false) 26 | lc[i-j+7]=true; 27 | else return; 28 | } 29 | ans+=1; 30 | } 31 | else 32 | { 33 | // Permutations made 34 | for (int i = l; i <= r; i++) 35 | { 36 | 37 | // Swapping done 38 | swap(a[l], a[i]); 39 | 40 | // Recursion called 41 | permute(l+1, r); 42 | 43 | //backtrack 44 | swap(a[l], a[i]); 45 | } 46 | } 47 | } 48 | 49 | signed main() 50 | { 51 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 52 | for(int i=0; i<8; ++i) 53 | { 54 | a.push_back(i); 55 | for(int j=0; j<8; ++j) 56 | { 57 | cin>>ch[i][j]; 58 | } 59 | } 60 | //for every permutation of 0, 1, ... 7 say arr 61 | //we can get every possible way of filling the board 62 | // with position (0, arr[0]), (1, arr[1]), ..., (7, arr[7]) 63 | //since no 2 queens are in the same row 64 | 65 | 66 | //we can try and fit every single one of these 8! permutations 67 | permute(0, 7); 68 | 69 | cout< 2 | //#include 3 | #include 4 | using namespace std; 5 | 6 | #define ll long long 7 | typedef vector vi; 8 | typedef vector vvi; 9 | #define F first 10 | #define S second 11 | #define all(v) v.begin(),v.end() 12 | #define rep(i,n) for(ll i=0;i=0;i--) 16 | #define mp make_pair 17 | #define pb push_back 18 | typedef pair > pii; 19 | #define inf 1e10 20 | 21 | bool prime[100005]={false}; 22 | void sieve(){ 23 | prime[0]=true; 24 | prime[1]=true; 25 | for(ll i=2;i<100005;i++){ 26 | if(prime[i]==false){ 27 | for(ll j=i*i;j<100005;j+=i) 28 | prime[j]=true; 29 | } 30 | } 31 | 32 | } 33 | 34 | 35 | void solve(){ 36 | ll n,m,k,ans=0; 37 | cin>>n>>m>>k; 38 | ll a[n],b[m]; 39 | rep(i,n) 40 | cin>>a[i]; 41 | rep(i,m) 42 | cin>>b[i]; 43 | sort(a,a+n); 44 | sort(b,b+m); 45 | for(ll i=0,j=0;i=b[j]){ //succesfully matched apartment to applicant 47 | ans++; 48 | i++; 49 | j++; 50 | } 51 | if(a[i]-k>b[j]) //apartment too small for applicant, 52 | j++; //moving to next apartment 53 | 54 | if(a[i]+k>T; 64 | while(T--){ 65 | solve();} 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Sorting_and_Searching/Concert_Tickets/pilgrim2308.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | //#include 3 | #include 4 | using namespace std; 5 | 6 | #define ll long long 7 | typedef vector vi; 8 | typedef vector vvi; 9 | #define F first 10 | #define S second 11 | #define all(v) v.begin(),v.end() 12 | #define rep(i,n) for(ll i=0;i=0;i--) 16 | #define mp make_pair 17 | #define pb push_back 18 | typedef pair > pii; 19 | #define inf 1e10 20 | 21 | bool prime[100005]={false}; 22 | void sieve(){ 23 | prime[0]=true; 24 | prime[1]=true; 25 | for(ll i=2;i<100005;i++){ 26 | if(prime[i]==false){ 27 | for(ll j=i*i;j<100005;j+=i) 28 | prime[j]=true; 29 | } 30 | } 31 | 32 | } 33 | 34 | 35 | void solve(){ 36 | ll n,m,x; 37 | cin>>n>>m; 38 | multiset v; 39 | rep(i,n){ 40 | cin>>x; 41 | v.insert(x); 42 | } 43 | rep(i,m){ 44 | cin>>x; 45 | if(v.empty()){ //if no tickets are available 46 | cout<<"-1\n"; 47 | continue; 48 | } 49 | auto it=v.upper_bound(x); 50 | if(it==v.begin()) //if cheapest ticket is expensive than 51 | { //what customer wants to pay 52 | cout<<-1<<"\n"; 53 | continue; 54 | } 55 | it--; 56 | cout<<*it<<"\n"; //customer buys the ticket, 57 | v.erase(it); //that ticket is removed from set 58 | } 59 | } 60 | 61 | int main(){ 62 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 63 | ll T=1; 64 | //cin>>T; 65 | while(T--){ 66 | solve();} 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /Sorting_and_Searching/Playlist/sleepingsamurai.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long int 4 | #define pb push_back 5 | #define pi 3.141592653589 6 | #define F first 7 | #define S second 8 | 9 | ll power(ll b,ll e,ll m) 10 | { 11 | if(e==0) return 1; 12 | if(e&1) return b*power(b*b%m,e/2,m)%m; 13 | return power(b*b%m,e/2,m); 14 | } 15 | ll power(ll b,ll e) 16 | { 17 | if(e==0) return 1; 18 | if(e&1) return b*power(b*b,e/2); 19 | return power(b*b,e/2); 20 | } 21 | int main() 22 | { 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll n; 26 | cin>>n; 27 | ll arr[n]; 28 | for(ll i=0;i>arr[i]; 30 | ll cont[n]; //array cont will store the length of continuous series till that point in the array 31 | for(ll i=0;i m,m1; //map m stores the occurrence of a track and m1 stores the index of that track; 34 | ll c=0,k=0; 35 | for(ll i=0;i 2 | using namespace std; 3 | 4 | const int N = 200005; 5 | 6 | vector v[N]; 7 | 8 | int dep[N],ans[N]; 9 | 10 | /* 11 | The maximum distance of a node to any other node always occur at one of the endpoints of the diameter. 12 | 13 | Here , our approach would be to find two endpoints of diameter and record the maximum of the two distances 14 | from then for every node in the graph. 15 | 16 | There is a general algorithm for finding the diameter of the tree using only two depth first searches 17 | 18 | For more details refer to : 19 | https://www.quora.com/How-do-I-find-the-diameter-of-a-tree 20 | */ 21 | 22 | 23 | void dfs(int s,int p) 24 | { 25 | for(int &j:v[s]) 26 | { 27 | if(j!=p) 28 | { 29 | dep[j] = dep[s] + 1; 30 | dfs(j,s); 31 | } 32 | } 33 | } 34 | 35 | int main() 36 | { 37 | int n; 38 | cin>>n; 39 | for(int i=1;i<=n-1;++i) 40 | { 41 | int x,y; 42 | cin>>x>>y; 43 | v[x].push_back(y),v[y].push_back(x); 44 | } 45 | dfs(1,0); 46 | 47 | // finding the first endpoint 48 | int u = -1, dis = -1; 49 | for(int i=1;i<=n;++i) 50 | { 51 | if(dep[i]>dis) 52 | dis = dep[i] , u = i; 53 | } 54 | 55 | // call dfs on one endpoint 56 | dep[u] = 0; 57 | dfs(u,0); 58 | 59 | int v = -1; // second endpoint 60 | dis = -1; 61 | // for distances from one endpoint of diameter 62 | for(int i=1;i<=n;++i) 63 | { 64 | if(dep[i] > dis) 65 | dis = dep[i] , v = i; 66 | 67 | ans[i] = max(ans[i] , dep[i]); 68 | } 69 | 70 | dep[v] = 0; 71 | dfs(v,0); 72 | 73 | // record for second endpoint 74 | for(int i=1;i<=n;++i) 75 | { 76 | ans[i] = max(ans[i] , dep[i]); 77 | cout << ans[i] << " "; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /Range_Queries/Range_Minimum_Queries_2/piyushmaurya.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); 4 | #define ll long long 5 | #define endl '\n' 6 | #define pii pair 7 | #define all(a) (a).begin(), (a).end() 8 | #define F first 9 | #define S second 10 | #define pb push_back 11 | #define mp make_pair 12 | #define repi(i, n) for(ll i=0; i &v, vector &tree, ll i, ll j, ll root){ 20 | if(i==j) {tree[root] = v[i]; return;} 21 | ll m = (i+j)/2; 22 | build(v, tree, i, m, 2*root+1); build(v, tree, m+1, j, 2*root+2); 23 | tree[root] = tree[2*root+1] + tree[2*root+2]; 24 | } 25 | 26 | void update(vector &tree, ll i, ll j, ll root, ll k, ll u) { 27 | if(i == j) {tree[root] = u; return;} 28 | ll m = (i+j)/2; 29 | if(k <= m) update(tree, i, m, 2*root+1, k, u); 30 | else update(tree, m+1, j, 2*root+2, k, u); 31 | tree[root] = tree[2*root+1] + tree[2*root+2]; 32 | } 33 | 34 | ll query(vector &tree, ll i, ll j, ll root, ll l, ll r){ 35 | if(r < i || l > j) return 0; 36 | if(l <= i && j <= r) return tree[root]; 37 | ll m = (i+j)/2; 38 | ll left = query(tree, i, m, 2*root+1, l, r); 39 | ll right = query(tree, m+1, j, 2*root+2, l, r); 40 | return left + right; 41 | } 42 | 43 | int main() { 44 | ios 45 | ll n, q; 46 | cin>>n>>q; 47 | vector v(n); 48 | repi(i, n) cin>>v[i]; 49 | vector tree(4*n+5); 50 | build(v, tree, 0, n-1, 0); 51 | while(q--){ 52 | ll t; cin>>t; 53 | if(t==1){ 54 | ll k, u; 55 | cin>>k>>u; 56 | k--; 57 | update(tree, 0, n-1, 0, k, u); 58 | } 59 | else{ 60 | ll a, b; 61 | cin>>a>>b; 62 | a--; b--; 63 | cout< 2 | using namespace std; 3 | #define ll long long int 4 | #define pb push_back 5 | #define pi 3.141592653589 6 | #define F first 7 | #define S second 8 | 9 | ll power(ll b,ll e,ll m) 10 | { 11 | if(e==0) return 1; 12 | if(e&1) return b*power(b*b%m,e/2,m)%m; 13 | return power(b*b%m,e/2,m); 14 | } 15 | ll power(ll b,ll e) 16 | { 17 | if(e==0) return 1; 18 | if(e&1) return b*power(b*b,e/2); 19 | return power(b*b,e/2); 20 | } 21 | int main() 22 | { 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | int n,x; 26 | cin>>n>>x; 27 | pair p[n]; // storing numbers in array of pairs 28 | for(int i=0;i>p[i].F; 31 | p[i].S=i+1; 32 | } 33 | sort(p,p+n); // sorting the array of pairs 34 | map m1; // using map to store values less then x/2; 35 | int mark1,mark2,f=0,no; 36 | for(int i=0;ix/2) 41 | { 42 | if(m1[x-p[i].F]!=0) 43 | { 44 | no=x-p[i].F; 45 | f=1; 46 | mark2=p[i].S; 47 | break; 48 | } 49 | } 50 | } 51 | if(f==1) 52 | { 53 | for(int i=0;i v; 66 | for(int i=0;i=2) 74 | { 75 | cout< 2 | using namespace std; 3 | #define ll long long int 4 | #define pb push_back 5 | #define pi 3.141592653589 6 | #define F first 7 | #define S second 8 | 9 | ll power(ll b,ll e,ll m) 10 | { 11 | if(e==0) return 1; 12 | if(e&1) return b*power(b*b%m,e/2,m)%m; 13 | return power(b*b%m,e/2,m); 14 | } 15 | ll power(ll b,ll e) 16 | { 17 | if(e==0) return 1; 18 | if(e&1) return b*power(b*b,e/2); 19 | return power(b*b,e/2); 20 | } 21 | int main() 22 | { 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll x,n; 26 | cin>>x>>n; 27 | ll arr[n]; 28 | multiset s,s1; //set s stores the inserted traffic light and s1 stores the difference at a point in the series 29 | cin>>arr[0]; // 1st insertion of traffic light 30 | s.insert(arr[0]); 31 | s1.insert(arr[0]); 32 | s1.insert(x-arr[0]); 33 | auto itr=s1.end(); 34 | itr--; 35 | cout<<*itr<<" "; 36 | s.insert(-1); 37 | s.insert(x+1); //setting the borders to -1 and x+1 38 | for(ll i=1;i>arr[i]; 41 | auto itr1=s.lower_bound(arr[i]); 42 | itr1--; 43 | auto itr2=s.upper_bound(arr[i]); 44 | ll diff=abs(*itr1-*itr2); 45 | if(*itr1==-1 || *itr2==x+1) 46 | diff--; 47 | if(s1.count(diff)); // deleting the difference which got destroyed after inserton of traffic light 48 | s1.erase(s1.find(diff)); 49 | ll max1=abs(*itr1-arr[i]); 50 | ll max2=abs(*itr2-arr[i]); 51 | if(*itr1==-1) 52 | max1--; 53 | if(*itr2==x+1) 54 | max2--; 55 | s1.insert(max1); // adding new differences after inserion 56 | s1.insert(max2); 57 | s.insert(arr[i]); // inserting the traffic light 58 | auto itr=s1.end(); // max difference is answer 59 | itr--; 60 | cout<<*itr<<" "; 61 | } 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Dynamic_Programming/Two_Sets_II/jindalji-hitesh.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define F first 3 | #define S second 4 | #define endl "\n" 5 | #define Endl "\n" 6 | #define ll long long 7 | #define vl vector 8 | #define pll pair 9 | #define sl set 10 | #define uset unordered_set 11 | #define umap unordered_map 12 | #define prq priority_queue 13 | #define pqll priority_queue 14 | #define hell 1000000007 15 | #define mod 998244353 16 | #define pb push_back 17 | #define ppb pop_back 18 | #define mem0(a) memset(a,0,sizeof(a)) 19 | #define mem1(a) memset(a,-1,sizeof(a)) 20 | #define all(v) (v).begin(),(v).end() 21 | using namespace std; 22 | ll power(ll x, ll y, ll p) 23 | { 24 | ll res = 1; 25 | x = x % p; 26 | while (y > 0) 27 | { 28 | if (y & 1) 29 | res = (res*x) % p; 30 | y = y>>1; // y = y/2 31 | x = (x*x) % p; 32 | } 33 | return res; 34 | } 35 | 36 | // Returns n^(-1) mod p 37 | ll modInverse(ll n, ll p=hell) 38 | { 39 | return power(n, p-2, p); 40 | } 41 | 42 | ll dp[501][100000]; 43 | ll solve(ll i,ll n,ll sum){ 44 | if(i>n||sum<=0||i>sum) 45 | return 0; 46 | if(dp[i][sum]!=-1) 47 | return dp[i][sum]; 48 | if(sum==i) 49 | return 1; 50 | return dp[i][sum]=(solve(i+1,n,sum)+solve(i+1,n,sum-i))%hell; 51 | } 52 | int main(){ 53 | ios_base::sync_with_stdio(0); 54 | cin.tie(NULL); 55 | cout.tie(NULL); 56 | // the problem can simplified to number of subsets of set {1,2,....n} whose sum is (n*(n+1))/4 57 | // the solution of this is simple recursive approach where at each number two cases are explored to include that number or exclude that number 58 | // memoization is used to avoid overlapping subproblems 59 | ll n; 60 | cin>>n; 61 | mem1(dp); 62 | if(n%4==1||n%4==2){ //check if sum of numbers is odd , if it is so numbers cannot be divided into two sets of equal sum 63 | cout<<0; 64 | return 0; 65 | } 66 | ll sum=(n*(n+1))/4; 67 | cout<<(solve(1,n,sum)*modInverse(2))%hell; 68 | 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /Graph_Algorithms/Investigation/ritik11g.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define int long long 6 | #define ff first 7 | #define ss second 8 | #define pii pair 9 | #define fast ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0); 10 | 11 | const int inf = 1e15; 12 | const int nax = 1e5+10; 13 | const int mod = 1e9+7; 14 | vector g[nax] , dp(nax) ; 15 | vector vis(nax,0) , dp_mx(nax) , dp_mn(nax) ; 16 | int n,m; 17 | 18 | signed main() 19 | { 20 | fast; 21 | cin >> n >> m; 22 | for(int i=0;i>u>>v>>w; 26 | g[v].push_back({u,w}); 27 | } 28 | set< pii > pq; 29 | for(int i=1;i<=n;i++) 30 | { 31 | dp_mx[i] = 0; 32 | dp_mn[i] = n+1; 33 | dp[i].ss = 0; 34 | dp[i].ff = inf; 35 | if( i==n ) 36 | { 37 | dp[i].ff = 0; 38 | dp[i].ss = 1; 39 | } 40 | pq.insert( {dp[i].ff , i } ); 41 | } 42 | dp_mx[n] = dp_mn[n] = 0; 43 | while( !pq.empty() ) 44 | { 45 | pii p = *pq.begin(); 46 | // cout << p.ff << " " << p.ss << "\n"; 47 | pq.erase(pq.begin()); 48 | int i = p.ss; 49 | vis[ i ] = 1; 50 | for( auto &j : g[i] ) 51 | { 52 | if( vis[ j.ff ] ) continue; 53 | if( dp[i].ff + j.ss < dp[j.ff].ff ) 54 | { 55 | pq.erase(pq.find( {dp[j.ff].ff , j.ff} ) ); 56 | dp[j.ff].ff = dp[i].ff + j.ss; 57 | pq.insert( {dp[j.ff].ff , j.ff } ); 58 | dp[j.ff].ss = dp[i].ss ; 59 | dp_mx[j.ff] = dp_mx[i]+1 ; 60 | dp_mn[j.ff] = dp_mn[i]+1 ; 61 | } 62 | else if( dp[i].ff + j.ss == dp[j.ff].ff ) 63 | { 64 | dp[j.ff].ss = (dp[i].ss + dp[j.ff].ss ) % mod ; 65 | dp_mx[j.ff] = max( dp_mx[i]+1 , dp_mx[j.ff] ); 66 | dp_mn[j.ff] = min( dp_mn[i]+1 , dp_mn[j.ff] ); 67 | } 68 | } 69 | } 70 | 71 | cout << dp[1].ff << " " << dp[1].ss << " " << dp_mn[1] << " " << dp_mx[1]; 72 | } 73 | -------------------------------------------------------------------------------- /Mathematics/Distributing_Apples/AnonySharma.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //DEFINE and TYPEDEF-------------------------------------------------------------- 5 | #define SPEEDUP ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); 6 | #define st first 7 | #define nd second 8 | 9 | typedef long long int lld; 10 | #define mod 1000000007 11 | //-------------------------------------------------------------------------------- 12 | 13 | lld power(lld b, lld e, lld m) // This calculates (b^e)%m 14 | { 15 | assert(e>=0); 16 | if(e==0) return 1; 17 | if(e%2==1) return b*power(((b%m)*(b%m))%m,(e-1)/2,m)%m; 18 | else return power(((b%m)*(b%m))%m,e/2,m)%m; 19 | } 20 | 21 | lld modInv(lld x, lld m) 22 | { 23 | // This calculates modular multiplicative inverse. 24 | // For reference, check method 3 on https://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/ 25 | return power(x,m-2,m); 26 | } 27 | 28 | lld fact[2000007]; 29 | lld nCr(lld n, lld r) 30 | { 31 | fact[0]=1; 32 | 33 | for(lld i=1;i<=n;i++) 34 | fact[i]=i*fact[i-1]%mod; 35 | 36 | return (((fact[n]*modInv(fact[r],mod))%mod)*modInv(fact[n-r],mod))%mod; // This just calculates (n!)/((n-r)!*r!) % mod 37 | } 38 | 39 | int main() 40 | { 41 | SPEEDUP; 42 | lld a,b; 43 | cin>>a>>b; 44 | 45 | // This is solved using the circles and lines method we used in Permutation and combination, 46 | // Let's consider a case of distributing 2 apples to 3 students 47 | // 0|0| -> 1st gets 1 apple, 2nd gets 1 apple, and 3rd gets 0 apple 48 | // 00|| -> 1st gets 2 apples, 2nd gets 0 apple, and 3rd gets 0 apple 49 | 50 | // Its pretty clear that, The total possibilities is all the possible arrangements of these circles and lines 51 | // So, Total arrangements = fact(Number of zeros+Numbers of lines)/(fact(Number of zeros)*fact(Numbers of lines)) 52 | // Number of circles = number of apples, and Number of lines = Number of students - 1 53 | // And, it is same as nCr(a+b-1,a-1); 54 | 55 | // And for calculating nCr mod m, I used Fermat Little Theorem. 56 | // For reference: https://www.geeksforgeeks.org/compute-ncr-p-set-3-using-fermat-little-theorem/ 57 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define ld long double 5 | #define mem0(a) memset(a,0,sizeof(a)) 6 | #define mem1(a) memset(a,-1,sizeof(a)) 7 | #define memf(a) memset(a,false,sizeof(a)) 8 | #define all(v) (v).begin(),(v).end() 9 | #define lb lower_bound 10 | #define ub upper_bound 11 | #define pll pair 12 | #define mll map 13 | #define endl "\n" 14 | #define pb push_back 15 | #define mp make_pair 16 | #define F first 17 | #define S second 18 | #define DEC(x) fixed<void amax(T &a,T1 b){if(b>a)a=b;} 28 | templatevoid amin(T &a,T1 b){if(b>n>>x; 53 | ll a[n]; 54 | multisets; 55 | // traversing through the array and taking the sum and finding the count of sum-x from the set and adding it to the answer. 56 | for(ll i=0;i>a[i]; 58 | for(ll i=0;i>TESTS; 81 | #ifdef _NCR_ 82 | initialvalues(); 83 | #endif 84 | while(TESTS--) 85 | TLE_TERROR(); 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /Sorting_and_Searching/Reading_books/lone_wolf_delena.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define ld long double 5 | #define mem0(a) memset(a,0,sizeof(a)) 6 | #define mem1(a) memset(a,-1,sizeof(a)) 7 | #define memf(a) memset(a,false,sizeof(a)) 8 | #define all(v) (v).begin(),(v).end() 9 | #define lb lower_bound 10 | #define ub upper_bound 11 | #define pll pair 12 | #define mll map 13 | #define endl "\n" 14 | #define pb push_back 15 | #define mp make_pair 16 | #define F first 17 | #define S second 18 | #define DEC(x) fixed<void amax(T &a,T1 b){if(b>a)a=b;} 28 | templatevoid amin(T &a,T1 b){if(b>n; 53 | ll a[n]; 54 | // let the first boy read all the books and the second boy read all the books except the one which takes the maximum time. 55 | // so if the difference between the time is positive then we will add it to the time taken by one boy to read all the books. 56 | for(ll i=0;i>a[i]; 59 | sum+=a[i]; 60 | } 61 | sort(a,a+n); 62 | ll summ=sum-a[n-1]; 63 | ll z=a[n-1]-summ; 64 | if(z>0) 65 | sum+=z; 66 | cout<>TESTS; 77 | #ifdef _NCR_ 78 | initialvalues(); 79 | #endif 80 | while(TESTS--) 81 | TLE_TERROR(); 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /Mathematics/Fibonacci_Numbers/CoderAnshu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | #define ll long long 5 | 6 | 7 | /* This is a standard problem of matrix exponentiation . 8 | We can speed up solving of recurrences to O(k^3logn) where k is the size of the matrix . 9 | For general details and matrices for fibonaaci refer : 10 | https://www.geeksforgeeks.org/matrix-exponentiation/ 11 | */ 12 | const int M =1e9+7; 13 | // Matrix template 14 | struct Matrix 15 | { 16 | vector> val; 17 | ll n_; 18 | Matrix (ll n) : n_(n) 19 | { 20 | val.resize(n_); 21 | for(ll i=0;i>n; 87 | if(n==0) 88 | { 89 | cout<<'0';return 0; 90 | } 91 | dp=power(dp,n-1); 92 | cout< 2 | #define ll long long 3 | #define vll vector 4 | #define vpp vector,ll>> 5 | #define vp vector> 6 | #define mk make_pair 7 | #define pb push_back 8 | #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) 9 | using namespace std; 10 | 11 | /* 12 | using djikstra's algo for each i , 1<=i<=n we will find d[i][j] (d[i][j] denotes shortest path from i to j) 1<=j<=n 13 | now queries can easily be answered online 14 | */ 15 | ll n,m; 16 | ll v[501][501]; 17 | ll INF = 1e18; 18 | ll d[502][502]; 19 | int main() 20 | { 21 | 22 | IOS; 23 | ll q,i,j; 24 | cin>>n>>m>>q; 25 | ll a,b,c; 26 | for(i=0;i<=500;i++){ 27 | for(j=0;j<=500;j++){ 28 | v[i][j]=INF; 29 | } 30 | } 31 | for( ll i=0; i>a>>b>>c; 34 | v[a][b]=min(v[a][b],c); //if there are two or morre edges between one pair we will keep the one with shortest length 35 | v[b][a]=v[a][b]; 36 | } 37 | ll s; 38 | for(j=1; j<=n; j++){ 39 | s=j; 40 | for(i=0; i<=n; i++) 41 | d[s][i] = INF; // intialize al distances to infinity 42 | 43 | d[s][s] = 0; 44 | using pii = pair; 45 | priority_queue, greater> q; 46 | q.push(mk(0,s)); 47 | // djiskstra's algo starts here 48 | while( !q.empty()) 49 | { 50 | ll ii = q.top().second; 51 | ll jj = q.top().first; 52 | q.pop(); 53 | 54 | if( jj != d[s][ii]) 55 | continue; 56 | 57 | for(i=1;i<=n;i++) 58 | { 59 | if(v[ii][i]==INF) 60 | continue; 61 | ll to = i; 62 | ll c1 = v[ii][i]; 63 | 64 | 65 | if( d[s][ii]+c1 < d[s][to]) 66 | { 67 | d[s][to] = d[s][ii]+c1; 68 | q.push(mk(d[s][to],to)); 69 | } 70 | } 71 | } 72 | } 73 | for( ll i=1; i<=q; i++) 74 | { 75 | cin>>a>>b; 76 | if( d[a][b] != INF) 77 | cout< 7 | using namespace std; 8 | 9 | #define int long long 10 | #define endl '\n' 11 | 12 | vector center(int n, vector adj[]) { 13 | int deg[n+1] = {0}; 14 | vector v; 15 | for (int i = 1; i <= n; i++) { 16 | deg[i] = adj[i].size(); 17 | if (deg[i] == 1) 18 | v.push_back(i), deg[i]--; 19 | } 20 | int m = v.size(); 21 | while(m < n) { 22 | vector vv; 23 | for (auto i: v) { 24 | for (auto j: adj[i]) { 25 | deg[j]--; 26 | if (deg[j] == 1) 27 | vv.push_back(j); 28 | } 29 | } 30 | m += vv.size(); 31 | v = vv; 32 | } 33 | return v; 34 | } 35 | map, int> mp; 36 | int idx = 0; 37 | 38 | int dfs(int s, int p, vector adj[]) { 39 | vector v; 40 | for (auto i: adj[s]) { 41 | if (i != p) 42 | v.push_back(dfs(i, s, adj)); 43 | } 44 | sort(v.begin(), v.end()); 45 | if (!mp.count(v)) mp[v] = idx++; 46 | return mp[v]; 47 | } 48 | signed main(){ 49 | ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); 50 | #ifdef LOCAL 51 | freopen("input.txt", "r" , stdin); 52 | freopen("output.txt", "w", stdout); 53 | #endif 54 | 55 | int t; cin>>t; 56 | while(t--) { 57 | int n; cin>>n; 58 | vector adj1[n+1], adj2[n+1]; 59 | for (int i = 1; i < n; i++) { 60 | int x,y; cin>>x>>y; 61 | adj1[x].push_back(y); 62 | adj1[y].push_back(x); 63 | } 64 | for (int i = 1; i < n; i++) { 65 | int x,y; cin>>x>>y; 66 | adj2[x].push_back(y); 67 | adj2[y].push_back(x); 68 | } 69 | vector v1 = center(n, adj1), v2 = center(n, adj2); 70 | int s1 = dfs(v1[0], -1, adj1); 71 | int f = 0; 72 | for (auto s: v2) { 73 | int s2 = dfs(s, -1, adj2); 74 | if (s1 == s2) 75 | f = 1; 76 | } 77 | if (f) cout<<"YES"; 78 | else cout<<"NO"; 79 | cout< 2 | using namespace std ; 3 | #define ll long long 4 | #define pb push_back 5 | #define in(x) ll x; cin>>x; 6 | #define ln(x) long long x; cin>>x; 7 | #define memf(a) memset(a,false,sizeof(a)) 8 | #define F first 9 | #define S second 10 | #define endl "\n" 11 | #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 12 | ll power(ll b,ll e,ll m) 13 | { 14 | if(e==0) return 1; 15 | if(e&1) return b*power(b*b%m,e/2,m)%m; 16 | return power(b*b%m,e/2,m); 17 | } 18 | ll power( ll b, ll e) 19 | { 20 | if(e==0) return 1; 21 | if(e&1) return b*power(b*b,e/2); 22 | return power(b*b,e/2); 23 | } 24 | int main() 25 | { 26 | ll n,sum; 27 | cin>>n>>sum; 28 | ll a[n]; 29 | map> m; 30 | for(ll i=0;i>a[i]; 33 | m[a[i]].insert(i); 34 | } 35 | ll l, r; 36 | 37 | 38 | sort(a, a+n); // Sort the elements 39 | 40 | /* Now fix the first element one by one and find the 41 | other two elements */ 42 | for (ll i = 0; i < n - 2; i++) 43 | { 44 | // To find the other two elements, start two index 45 | // variables from two corners of the array and move 46 | // them toward each other 47 | l = i + 1; // index of the first element in the 48 | // remaining elements 49 | 50 | r = n - 1; // index of the last element 51 | while (l < r) 52 | { 53 | if (a[i] + a[l] + a[r] == sum) 54 | { 55 | ll x=*m[a[i]].begin(); 56 | cout< sum 76 | r--; 77 | } 78 | } 79 | cout<<"IMPOSSIBLE"; 80 | return 0; 81 | } 82 | 83 | -------------------------------------------------------------------------------- /Mathematics/Stick_Game/CoderAnshu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | const int N = 1e6+5; 5 | 6 | // Basic dynamic programming problem 7 | bool dp[N]; 8 | 9 | int main() 10 | { 11 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 12 | 13 | 14 | int n,k; 15 | cin>>n>>k; 16 | string ans; 17 | vector v(k); // possible moves 18 | for(int i=0;i>v[i]; 20 | 21 | /* 0 denotes the losing position and 1 denotes the winning position */ 22 | 23 | dp[0] = false ; // because if no sticks are remaining and its ur move then u are sure to lose 24 | 25 | for(int i=1;i<=n;++i) 26 | { 27 | dp[i] = false; // Setting the current position to be losing position 28 | 29 | /* 30 | This question is based on basics of game theory . 31 | Now dp[i] is winning state if and only if you can convert it into any of the possible 32 | losing states by performing a valid move . And if you convert the game into a losing state 33 | for the opponent then you are sure to win by playing optimally. 34 | 35 | Losing state here means a state where you can never win and irrespective of your moves your 36 | opponent can always win by a definite strategy. 37 | 38 | Winning state here means that if by performing any valid move you make your opponent enter into 39 | a losing state then you are sure to win. 40 | 41 | A state is losing if by performing any valid move you always end up in a winning state therby 42 | losing tha game. 43 | 44 | And a state is winning if u have any of the reachable losing states forcing your opponent to that 45 | state by performing the corresponding move. 46 | */ 47 | 48 | for(int j=0;j=0) 52 | { 53 | // move is valid 54 | if(!dp[i-v[j]]) 55 | { 56 | // the state after this move is losing and therefore u can force ur oppenent to 57 | // that losing state 58 | dp[i] = true; 59 | } 60 | } 61 | } 62 | ans += dp[i]?"W":"L"; 63 | } 64 | 65 | cout< 2 | using namespace std ; 3 | 4 | #define M 1000000007 5 | #define ll long long 6 | 7 | // Matrix template 8 | struct Matrix 9 | { 10 | vector> val; 11 | ll n_; 12 | Matrix (ll n) : n_(n) 13 | { 14 | val.resize(n_); 15 | for(ll i=0;i>n>>m>>k; 86 | Matrix dp(n); 87 | dp.set(0); 88 | while(m--) 89 | { 90 | int x,y; 91 | cin>>x>>y; 92 | --x,--y; 93 | ++dp.val[x][y]; 94 | } 95 | dp=power(dp,k); 96 | cout< 2 | using namespace std ; 3 | 4 | #define M 1000000007 5 | #define ll long long 6 | 7 | // Matrix Template 8 | 9 | struct Matrix 10 | { 11 | vector> val; 12 | ll n_; 13 | Matrix (ll n) : n_(n) 14 | { 15 | val.resize(n_); 16 | for(ll i=0;i>n>>m>>k; 86 | Matrix dp(n); 87 | dp.set(4e18); 88 | while(m--) 89 | { 90 | int x,y; 91 | ll w; 92 | cin>>x>>y>>w; 93 | --x,--y; 94 | dp.val[x][y] = min(dp.val[x][y] , w); 95 | } 96 | dp=power(dp,k); 97 | ll x=dp.val[0][n-1]; 98 | cout<<(x==4e18?-1:x)<<'\n'; 99 | 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /Tree_Algorithms/Subtree_Queries/CoderAnshu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define ll long long 6 | 7 | const int N = 200005; 8 | 9 | vector v[N]; // adj list 10 | 11 | ll val[N]; 12 | 13 | int in[N],out[N]; 14 | int cur = 1;// cur _size_of_array 15 | 16 | // for_storing_the_dfs_order(flattening the tree) 17 | 18 | // BIT - > Binary Indexed Tree 19 | template 20 | struct fenwick { 21 | T a[N],bit[N]; 22 | int n; 23 | fenwick() 24 | { 25 | for(int i=1;i<=N-2;++i) 26 | a[i] = T(0),bit[i] = T(0); 27 | } 28 | void build(int n_) 29 | { 30 | n = n_; 31 | for(int i=1;i<=n;++i) 32 | for(int j=i;j<=n;j+=j&-j) 33 | bit[j] += a[i]; 34 | } 35 | void update(int j,T val) 36 | { 37 | for(;j<=n;j+=j&-j) 38 | bit[j] += val; 39 | } 40 | T get(int r) 41 | { 42 | T u = 0; 43 | for(;r;r-=r&-r) 44 | u += bit[r]; 45 | return u; 46 | } 47 | T query(int l,int r) 48 | { 49 | return get(r)-get(l-1); 50 | } 51 | }; 52 | // call fenw.build(n); 53 | fenwick fenw; 54 | 55 | void dfs(int s,int p) 56 | { 57 | // s -> source p-> parent 58 | in[s] = cur ++ ; 59 | for(int &j:v[s]) 60 | if(j!=p) 61 | dfs(j,s); 62 | out[s] = cur-1; 63 | } 64 | 65 | int main() 66 | { 67 | int n,Q; 68 | cin>>n>>Q; 69 | for(int i=1;i<=n;++i) 70 | cin>>val[i]; 71 | for(int i=0;i>x>>y; 75 | v[x].push_back(y),v[y].push_back(x); 76 | } 77 | dfs(1,0); 78 | for(int i=1;i<=n;++i) 79 | { 80 | // intializing the values 81 | fenw.a[in[i]] = val[i]; 82 | } 83 | fenw.build(n); 84 | for(int i=0;i>tt>>s; 88 | if(tt==1) 89 | { 90 | cin>>x; 91 | // update in the fenwick tree 92 | fenw.update(in[s],x-val[s]); 93 | val[s] = x; 94 | } 95 | else 96 | cout< 2 | using namespace std ; 3 | 4 | #define ll long long 5 | 6 | const int N=200005; 7 | const int LOGN=20; // approximate logn for 2e5 8 | 9 | // adj list 10 | vector v[N]; 11 | int dep[N],par[N][LOGN]; 12 | 13 | 14 | /* The technique here is same as in the previous question Company Queries 1 . 15 | 16 | Here we just need to find the first(deepest) node which is the ancestor of both u and v given two 17 | vertices u and v. 18 | 19 | */ 20 | 21 | void dfs(int s,int p) 22 | { 23 | for(auto j:v[s]) 24 | { 25 | if(j!=p) 26 | { 27 | dep[j]=dep[s]+1,par[j][0]=s; 28 | dfs(j,s); 29 | } 30 | } 31 | } 32 | 33 | // lca -> lowest common ancestor 34 | int lca(int u,int v) 35 | { 36 | if(dep[u]>dep[v]) 37 | swap(u,v); 38 | 39 | // df -> difference 40 | int df=dep[v]-dep[u]; 41 | // first we raise the vertex v to the same level as of vertex u and this is exactly 42 | // same as lifting it to its (dep[v] - dep[u])th parent, which we did earlier. 43 | 44 | for(int i=0;i=0; i--) 60 | { 61 | if(par[u][i]!=par[v][i]) 62 | { 63 | u=par[u][i]; 64 | v=par[v][i]; 65 | } 66 | } 67 | return par[u][0]; 68 | } 69 | 70 | int main() 71 | { 72 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 73 | 74 | int n,q; 75 | cin>>n>>q; 76 | for(int i=2;i<=n;++i) 77 | { 78 | int p; 79 | cin>>p; 80 | v[p].push_back(i),v[i].push_back(p); 81 | } 82 | dfs(1,0); 83 | for(int i=1;i>u>>v; 96 | cout< 2 | using namespace std ; 3 | 4 | #define ll long long 5 | 6 | const int N=200005; 7 | const int LOGN=20; // approximate logn for 2e5 8 | 9 | // adj list 10 | vector v[N]; 11 | int dep[N],par[N][LOGN]; 12 | 13 | 14 | /* The main technique used in this question is binary lifting . 15 | 16 | Example : We want to find the kth parent of some node . 17 | We can not do so by jumping over the parents as that would be order n**2 and extremely slow for chain 18 | like trees . 19 | 20 | Here we store (2^i) parent for each node for each i from 0 to LOGN . This requires O(nlogn) memory. 21 | 22 | Now the basic trick is that we have stored all 2^i th parents for each node (where 2^0 = 1 is the 23 | immediate parent of that node and rest can be computed in DP fashion). 24 | Like for (2^i) th parent of current node we take (2^(i-1)) parent of the (2^(i-1)) th parent of 25 | current node . ( 2^(i-1) + 2^(i-1) = 2^i) 26 | 27 | Now suppose we want to find the kth parent of a node , suppose k = 13 28 | 13 = 1101 (binary) 29 | then we jump over the set bits of k 30 | like first we go to 2^0 th parent of current node and then 2^2 parent of current node then 2^3 31 | parent of current node and we are done as we have traversed (2^0 + 2^2 + 2^3 = 13) nodes and 32 | now we are at the required parent node . 33 | 34 | */ 35 | 36 | void dfs(int s,int p) 37 | { 38 | for(auto j:v[s]) 39 | { 40 | if(j!=p) 41 | { 42 | dep[j]=dep[s]+1,par[j][0]=s; 43 | dfs(j,s); 44 | } 45 | } 46 | } 47 | 48 | int lift(int u,int k) 49 | { 50 | for(int i=0;i>n>>q; 69 | for(int i=2;i<=n;++i) 70 | { 71 | int p; 72 | cin>>p; 73 | v[p].push_back(i),v[i].push_back(p); 74 | } 75 | dfs(1,0); 76 | for(int i=1;i>u>>v; 89 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define ld long double 5 | #define mem0(a) memset(a,0,sizeof(a)) 6 | #define mem1(a) memset(a,-1,sizeof(a)) 7 | #define memf(a) memset(a,false,sizeof(a)) 8 | #define all(v) (v).begin(),(v).end() 9 | #define lb lower_bound 10 | #define ub upper_bound 11 | #define pll pair 12 | #define mll map 13 | #define endl "\n" 14 | #define pb push_back 15 | #define mp make_pair 16 | #define F first 17 | #define S second 18 | #define DEC(x) fixed<void amax(T &a,T1 b){if(b>a)a=b;} 28 | templatevoid amin(T &a,T1 b){if(b>n>>k; 53 | ll a[n]; 54 | for(ll i=0;i>a[i]; 56 | ll low=0,high=2e18; 57 | // APPLYTING BINARY SEARCH TO CHECK WHICH TIME IS POSSIBLE AND FINDING THE MINIMUM TIME OUT OF IT. 58 | // to check, first consider a time which is to be founded by binary search.Then add the quotient of division of that time from each array element to find the total products 59 | // that can be made.If they are greater than the required products,then take its minimum. 60 | while(low<(high-1)) 61 | { 62 | ll m=(low+high)/2; 63 | ll ttl=0,f=0; 64 | for(ll i=0;i=k) 68 | { 69 | ans=min(ans,m); 70 | high=m; 71 | f=1; 72 | break; 73 | } 74 | } 75 | if(f==0) 76 | low=m; 77 | 78 | } 79 | cout<>TESTS; 90 | #ifdef _NCR_ 91 | initialvalues(); 92 | #endif 93 | while(TESTS--) 94 | TLE_TERROR(); 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /Range_Queries/Range_Minimum_Queries_1/CoderAnshu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | 5 | 6 | // segment tree template 7 | 8 | /* Segment Tree is a data structure that supports point updates and range updates along with 9 | a large number of range queries 10 | To learn in detail refer to : https://cp-algorithms.com/data_structures/segment_tree.html 11 | */ 12 | 13 | const int N=200005; 14 | struct node 15 | { 16 | int sum; 17 | }fake={int(2e9)}; 18 | 19 | struct SEGTREE 20 | { 21 | int a[N]; 22 | SEGTREE(){ } 23 | node tree[4*N]; 24 | 25 | // combine two child nodes to a new node 26 | inline node combine(node a,node b) 27 | { 28 | node ret; 29 | ret.sum=min(a.sum,b.sum); 30 | return ret; 31 | } 32 | 33 | // make a new node 34 | inline node make_node(int val) 35 | { 36 | node ret; 37 | ret.sum=val; 38 | return ret; 39 | } 40 | 41 | // build the segTree Bottom - Up 42 | void buildTree(int v,int st,int en) 43 | { 44 | if(st==en) 45 | { 46 | tree[v]=make_node(a[st]); 47 | return ; 48 | } 49 | int mid=(st+en)>>1; 50 | buildTree(v<<1,st,mid); 51 | buildTree(v<<1 | 1,mid+1,en); 52 | tree[v]=combine(tree[v<<1],tree[v<<1 | 1]); 53 | } 54 | 55 | // Point - Update 56 | void update(int v,int st,int en,int in,int val) 57 | { 58 | int mid=(st+en)>>1; 59 | if(st==en) 60 | { 61 | tree[v]=make_node(val); 62 | a[st]=val; 63 | return ; 64 | } 65 | if(in<=mid) 66 | update(v<<1,st,mid,in,val); 67 | else 68 | update(v<<1 | 1,mid+1,en,in,val); 69 | tree[v]=combine(tree[v<<1],tree[v<<1 | 1]); 70 | } 71 | 72 | // Range Query 73 | node query(int v,int st,int en,int l,int r) 74 | { 75 | if(enr) 76 | return fake; 77 | if(st>=l && en<=r) 78 | return tree[v]; 79 | int mid=(st+en)>>1; 80 | return combine(query(v<<1,st,mid,l,r),query(v<<1 | 1,mid+1,en,l,r)); 81 | } 82 | }seg; 83 | 84 | 85 | int main() 86 | { 87 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 88 | 89 | int n,q; 90 | cin>>n>>q; 91 | 92 | // initialize the segment tree with initial values of the array 93 | for(int i=0;i>seg.a[i]; 95 | 96 | // build the segTree 97 | seg.buildTree(1,0,n-1); 98 | 99 | while(q--) 100 | { 101 | int a,b; 102 | cin>>a>>b; 103 | 104 | cout< 2 | using namespace std ; 3 | 4 | #define ll long long 5 | 6 | const int M = 1e9+7; 7 | 8 | // Matrix template 9 | struct Matrix 10 | { 11 | vector> val; 12 | ll n_; 13 | Matrix (ll n) : n_(n) 14 | { 15 | val.resize(n_); 16 | for(ll i=0;i>n; 96 | Matrix dp(6); 97 | dp.set(0); 98 | for(int i=0;i<6;++i) 99 | dp.val[0][i]=1; 100 | for(int i=1;i<6;++i) 101 | dp.val[i][i-1]=1; 102 | ll ans[6]={1,1,2,4,8,16}; 103 | if(n<6) 104 | { 105 | cout< 2 | using namespace std ; 3 | 4 | 5 | 6 | // segment tree template 7 | 8 | /* Segment Tree is a data structure that supports point updates and range updates along with 9 | a large number of range queries 10 | To learn in detail refer to : https://cp-algorithms.com/data_structures/segment_tree.html 11 | */ 12 | 13 | const int N=200005; 14 | struct node 15 | { 16 | int sum; 17 | }fake={int(2e9)}; 18 | 19 | struct SEGTREE 20 | { 21 | int a[N]; 22 | SEGTREE(){ } 23 | node tree[4*N]; 24 | 25 | // combine two child nodes to a new node 26 | inline node combine(node a,node b) 27 | { 28 | node ret; 29 | ret.sum=min(a.sum,b.sum); 30 | return ret; 31 | } 32 | 33 | // make a new node 34 | inline node make_node(int val) 35 | { 36 | node ret; 37 | ret.sum=val; 38 | return ret; 39 | } 40 | 41 | // build the segTree Bottom - Up 42 | void buildTree(int v,int st,int en) 43 | { 44 | if(st==en) 45 | { 46 | tree[v]=make_node(a[st]); 47 | return ; 48 | } 49 | int mid=(st+en)>>1; 50 | buildTree(v<<1,st,mid); 51 | buildTree(v<<1 | 1,mid+1,en); 52 | tree[v]=combine(tree[v<<1],tree[v<<1 | 1]); 53 | } 54 | 55 | // Point - Update 56 | void update(int v,int st,int en,int in,int val) 57 | { 58 | int mid=(st+en)>>1; 59 | if(st==en) 60 | { 61 | tree[v]=make_node(val); 62 | a[st]=val; 63 | return ; 64 | } 65 | if(in<=mid) 66 | update(v<<1,st,mid,in,val); 67 | else 68 | update(v<<1 | 1,mid+1,en,in,val); 69 | tree[v]=combine(tree[v<<1],tree[v<<1 | 1]); 70 | } 71 | 72 | // Range Query 73 | node query(int v,int st,int en,int l,int r) 74 | { 75 | if(enr) 76 | return fake; 77 | if(st>=l && en<=r) 78 | return tree[v]; 79 | int mid=(st+en)>>1; 80 | return combine(query(v<<1,st,mid,l,r),query(v<<1 | 1,mid+1,en,l,r)); 81 | } 82 | }seg; 83 | 84 | 85 | int main() 86 | { 87 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 88 | 89 | int n,q; 90 | cin>>n>>q; 91 | 92 | // initialize the segment tree with initial values of the array 93 | for(int i=0;i>seg.a[i]; 95 | 96 | // build the segTree 97 | seg.buildTree(1,0,n-1); 98 | 99 | while(q--) 100 | { 101 | int tt; 102 | cin>>tt; 103 | int a,b; 104 | cin>>a>>b; 105 | if(tt==1) 106 | seg.update(1,0,n-1,a-1,b); 107 | else 108 | cout< 2 | using namespace std ; 3 | 4 | #define ll long long 5 | 6 | const int N=200005; 7 | 8 | template 9 | struct fenwick { 10 | T a[N],bit[N]; 11 | int n; 12 | fenwick() 13 | { 14 | for(int i=1;i<=N-2;++i) 15 | a[i] = T(0),bit[i] = T(0); 16 | } 17 | void build(int n_) 18 | { 19 | n = n_; 20 | for(int i=1;i<=n;++i) 21 | for(int j=i;j<=n;j+=j&-j) 22 | bit[j] += a[i]; 23 | } 24 | void update(int j,T val) 25 | { 26 | for(;j<=n;j+=j&-j) 27 | bit[j] += val; 28 | } 29 | T get(int r) 30 | { 31 | T u = 0; 32 | for(;r;r-=r&-r) 33 | u += bit[r]; 34 | return u; 35 | } 36 | T query(int l,int r) 37 | { 38 | return get(r)-get(l-1); 39 | } 40 | }; 41 | // call fenw.build(n); 42 | fenwick fenw; 43 | 44 | vector v[N]; 45 | 46 | // entry time and exit time 47 | int cur=1,in[N],out[N]; 48 | ll val[N]; 49 | ll intial_sum_to_root[N]; 50 | 51 | // flatten the tree using dfs_order 52 | void dfs(int s,int p,ll u=0) 53 | { 54 | in[s]=cur++; 55 | u += val[s]; 56 | intial_sum_to_root[s] = u; 57 | 58 | for(auto j:v[s]) 59 | { 60 | if(j!=p) 61 | { 62 | dfs(j,s,u); 63 | } 64 | } 65 | 66 | out[s]=cur-1; 67 | } 68 | 69 | /* The logic here is that if you update a node then only the nodes in the subtree of that node 70 | will get affected i.e if you add some val to node X then the answer for the nodes in the subtree 71 | of that node will also be +=val since the node X is in the root to node path for every such node . 72 | 73 | Here we need to add a value to a range which we can do using fenwick trees 74 | 75 | Suppose we need to value = x in the range (l,r) 76 | then we a[l] += x and a[r+1] -= x; 77 | and whenever we need to get the value added at a particular index we just query for the prefix sum. 78 | */ 79 | 80 | int main() 81 | { 82 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 83 | 84 | int n,q; 85 | cin>>n>>q; 86 | for(int i=1;i<=n;++i) 87 | cin>>val[i]; 88 | for(int i=0;i>x>>y; 92 | v[x].push_back(y),v[y].push_back(x); 93 | } 94 | dfs(1,0); 95 | 96 | fenw.build(n); 97 | 98 | while(q--) 99 | { 100 | int t,u; 101 | ll x; 102 | cin>>t>>u; 103 | if(t==1) 104 | { 105 | cin>>x; 106 | fenw.update(in[u],x-val[u]); 107 | fenw.update(out[u] + 1,val[u] - x); 108 | val[u]=x; 109 | } 110 | else 111 | { 112 | cout< 2 | using namespace std ; 3 | 4 | #define ll long long 5 | 6 | const int N=200005; 7 | const int LOGN=20; // approximate logn for 2e5 8 | 9 | // adj list 10 | vector v[N]; 11 | int dep[N],par[N][LOGN]; 12 | 13 | 14 | /* We only need to calculate lca of two vertices. 15 | 16 | For detailed explanation on how to calculate LCA efficiently , refer to Questions -> Comapny Queries 1 and 2 17 | 18 | */ 19 | 20 | void dfs(int s,int p) 21 | { 22 | for(auto j:v[s]) 23 | { 24 | if(j!=p) 25 | { 26 | dep[j]=dep[s]+1,par[j][0]=s; 27 | dfs(j,s); 28 | } 29 | } 30 | } 31 | 32 | // lca -> lowest common ancestor 33 | int lca(int u,int v) 34 | { 35 | if(dep[u]>dep[v]) 36 | swap(u,v); 37 | 38 | // df -> difference 39 | int df=dep[v]-dep[u]; 40 | // first we raise the vertex v to the same level as of vertex u and this is exactly 41 | // same as lifting it to its (dep[v] - dep[u])th parent, which we did earlier. 42 | 43 | for(int i=0;i=0; i--) 59 | { 60 | if(par[u][i]!=par[v][i]) 61 | { 62 | u=par[u][i]; 63 | v=par[v][i]; 64 | } 65 | } 66 | return par[u][0]; 67 | } 68 | 69 | int main() 70 | { 71 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 72 | 73 | int n,q; 74 | cin>>n>>q; 75 | for(int i=0;i>x>>y; 79 | v[x].push_back(y),v[y].push_back(x); 80 | } 81 | dfs(1,0); 82 | for(int i=1;i>u>>v; 95 | int l = lca(u,v); 96 | 97 | /* For length of path between u and v : 98 | Any path between two vertices in a tree can be decomposed into two paths of 99 | u -> lca and lca -> v and there is always one unique path in a tree between any two vertices 100 | Now , out answer is just (dep[u] + dep[v] - 2*dep[lca]) since lca is ancestor of both u and v 101 | and their path can be simply computed using depth of the nodes . 102 | */ 103 | cout << dep[u] + dep[v] - 2*dep[l] << "\n"; 104 | } 105 | 106 | return 0; 107 | } 108 | -------------------------------------------------------------------------------- /Sorting_and_Searching/Nearest_Smaller_Values/lone_wolf_delena.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define ld long double 5 | #define mem0(a) memset(a,0,sizeof(a)) 6 | #define mem1(a) memset(a,-1,sizeof(a)) 7 | #define memf(a) memset(a,false,sizeof(a)) 8 | #define all(v) (v).begin(),(v).end() 9 | #define lb lower_bound 10 | #define ub upper_bound 11 | #define pll pair 12 | #define mll map 13 | #define endl "\n" 14 | #define pb push_back 15 | #define mp make_pair 16 | #define F first 17 | #define S second 18 | #define DEC(x) fixed<void amax(T &a,T1 b){if(b>a)a=b;} 28 | templatevoid amin(T &a,T1 b){if(b>n; 53 | ll a[n]; 54 | stacks; 55 | for(ll i=0;i>a[i]; 57 | ll ans[n]; 58 | for(ll i=0;i=0;i--) 69 | { 70 | if(s.empty()) 71 | { 72 | s.push(i); 73 | continue; 74 | } 75 | while(!s.empty()&&a[s.top()]>a[i]) 76 | { 77 | ans[s.top()]=i+1; 78 | s.pop(); 79 | } 80 | s.push(i); 81 | } 82 | 83 | for(ll i=0;i>TESTS; 99 | #ifdef _NCR_ 100 | initialvalues(); 101 | #endif 102 | while(TESTS--) 103 | TLE_TERROR(); 104 | return 0; 105 | } 106 | -------------------------------------------------------------------------------- /Graph_Algorithms/Labyrinth/AnonySharma.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //DEFINE and TYPEDEF-------------------------------------------------------------- 5 | #define SPEEDUP ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); 6 | #define st first 7 | #define nd second 8 | 9 | typedef long long int lld; 10 | //-------------------------------------------------------------------------------- 11 | 12 | lld n,m; 13 | char arr[1002][1002]; 14 | queue> q; //Queue for BFS 15 | bool visited[1002][1002]; 16 | map,pair> parent; //if parent[{a,b}]={c,d} then (a,b) was visited just after visiting (c,d) 17 | 18 | void bfs(lld x, lld y) 19 | { 20 | q.push({x,y}); 21 | visited[x][y]=1; 22 | 23 | while(q.size()) // Normal BFS 24 | { 25 | pair temp=q.front(); 26 | q.pop(); 27 | 28 | x=temp.st; 29 | y=temp.nd; 30 | 31 | if(arr[x][y]=='B') // If found B then break 32 | break; 33 | 34 | if(x) //If going left is possible, add it to the queue 35 | { 36 | if(!visited[x-1][y] and arr[x-1][y]!='#') 37 | { 38 | parent[{x-1,y}]={x,y}; 39 | q.push({x-1,y}); 40 | } 41 | 42 | visited[x-1][y]=1; //Mark it visited 43 | } 44 | 45 | if(y) //If going up is possible, add it to the queue 46 | { 47 | if(!visited[x][y-1] and arr[x][y-1]!='#') 48 | { 49 | parent[{x,y-1}]={x,y}; 50 | q.push({x,y-1}); 51 | } 52 | 53 | visited[x][y-1]=1; //Mark it visited 54 | } 55 | 56 | if(x>n>>m; 85 | 86 | for(lld i=0;i>arr[i][j]; 90 | if(arr[i][j]=='A') 91 | x=i,y=j; //Storing the location of 'A' 92 | else if(arr[i][j]=='B') 93 | a=i,b=j; //Storing the location of 'B' 94 | } 95 | 96 | bfs(x,y); 97 | cout<<(visited[a][b]?"YES\n":"NO"); 98 | 99 | if(visited[a][b]) 100 | { 101 | vector> path; 102 | while(1) 103 | { 104 | path.push_back({a,b}); 105 | if(x==a and y==b) // If reached the location of 'B' then break 106 | break; 107 | 108 | lld ta=a,tb=b; 109 | a=parent[{ta,tb}].st; 110 | b=parent[{ta,tb}].nd; 111 | } 112 | 113 | reverse(path.begin(), path.end()); //Since we have to give the path from A to B, we need to reverse it 114 | string s; 115 | for(lld i=0;ipath[i+1].nd) 122 | s+='L'; 123 | if(path[i].stpath[i+1].st and path[i].nd==path[i+1].nd) 126 | s+='U'; 127 | } 128 | } 129 | 130 | cout< 3 | // *****************************Template Begins ************************** // 4 | #define pb push_back 5 | #define s second 6 | #define mii map 7 | #define dbg(x) { cerr<<#x<<": "< 11 | #define ppb pop_back 12 | #define f first 13 | #define vi vector 14 | #define vii vector> 15 | #define si set 16 | #define all(a) (a).begin(),(a).end() 17 | #define sz(x) (int )x.size() 18 | #define hell 1000000007 19 | #define mp make_pair 20 | #define yes cout << "YES" << endl; 21 | #define no cout << "NO" << endl; 22 | #define inf 1e18L+1 23 | #define endl '\n' 24 | using namespace std; 25 | #define N 300005 26 | // *****************************Template ends **************************** // 27 | /* 28 | Solution - 29 | This question can also be solved by DSU but I've used DFS, logic behind this question is that since we're given undirected graph, we will have several connected components(or maybe 1), now minimum number of roads required to connect them is size of connected component - 1, you can feel it like, if we have 3 connected components containing several vertices, let's assume, Component 1 has a vertex 1, 2 has vertex 2 and 3 has vertex 3, since the graph is undirected, optimal strategy to merge these 3 components will be to build a road between 1 and 2, 1 and 3. Now you can look that for 3 components we created 2 components, since we merge all components into 1. 30 | Since the logic behind the solution is pretty simple, let's walk through the solution - 31 | 1. Taking input, list of global variables defined along with their work - 32 | i. n, m is number of vertices and edges respectively. 33 | ii. Vector edge, is the adjaceny list for the graph. 34 | iii. vis is the array which is true, if that vertex is visited, false otherwise. 35 | 2. Since the graph is undirected, we walk through the edges from 1 to n and if that vertex is unvisited, it means that it is in different connected component, so we run dfs from this vertex and push this vertex in a vector, which marks that this vertex is in different connected component for further steps. 36 | 3. Now we output minimum number of edges required to connect all the components as size of head of components(v) - 1 and merge all the components from the first entry in v. 37 | I've further marked each step in the code also, you'int se something like step 1 begins, step 1 ends etc. 38 | End of the solution. 39 | */ 40 | int n, m; 41 | vi edge[N]; 42 | bool vis[N]; 43 | void dfs(int node) 44 | { 45 | vis[node]=1; 46 | for(auto i:edge[node]) 47 | if(!vis[i]) 48 | dfs(i); 49 | } 50 | int main() 51 | { 52 | ios::sync_with_stdio(false); 53 | cin.tie(0); 54 | cout.tie(0); 55 | int tests = 1; 56 | // cin >> tests; 57 | while(tests--) 58 | { 59 | // step 1 begins 60 | vi v; 61 | cin >> n >> m; 62 | rep(i, 0, m) 63 | { 64 | int a, b; 65 | cin >> a >> b; 66 | edge[a].pb(b); 67 | edge[b].pb(a); 68 | } 69 | // step 1 ends 70 | // step 2 begins 71 | rep(i, 1, n+1) 72 | { 73 | if(!vis[i]) 74 | { 75 | v.pb(i); 76 | dfs(i); 77 | } 78 | } 79 | // step 2 ends 80 | // step 3 begins 81 | cout << sz(v) - 1 << endl; 82 | rep(i, 1, sz(v)) 83 | { 84 | cout << v[0] << " " << v[i] << endl; 85 | } 86 | // step 3 ends 87 | } 88 | return 0; 89 | } -------------------------------------------------------------------------------- /Graph_Algorithms/Counting_Rooms/King-01.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link = https://cses.fi/problemset/task/1192 2 | #include 3 | // *****************************Template Begins ************************** // 4 | #define pb push_back 5 | #define s second 6 | #define mii map 7 | #define dbg(x) { cerr<<#x<<": "< 11 | #define ppb pop_back 12 | #define f first 13 | #define vi vector 14 | #define vii vector> 15 | #define si set 16 | #define all(a) (a).begin(),(a).end() 17 | #define sz(x) (int )x.size() 18 | #define hell 1000000007 19 | #define mp make_pair 20 | #define yes cout << "YES" << endl; 21 | #define no cout << "NO" << endl; 22 | #define inf 1e18L+1 23 | #define endl '\n' 24 | using namespace std; 25 | #define N 300005 26 | // *****************************Template ends **************************** // 27 | /* 28 | Solution - 29 | So here we use the fact that each room will be reachable from any point inside it to any other point inside it, now since wall cannot be room, we can solve this question either by BFS or DFS, but I've used DFS here. 30 | Since the logic behind the solution is pretty simple, let's walk through the solution - 31 | 1. Taking input, I've defined a global variable grid to store the map of the building, n for number of rows and m for nummber of columns, vis array to mark a floor ('.') cell visited if vis is true otherwise unvisited. 32 | 2. initializing vis array to false by default, and counter to 0 where counter denotes number of rooms in our grid. 33 | 3. we walk through the grid, if we find a cell that is a floor('.') and unvisited, we start dfs from that cell. 34 | 4. In dfs of each cell, we first mark it visited, since we can move in each of the 4 directions namely up, down, left and right, we check whether it's valid to move in that cell by checking that the cell is inside boundry, is floor and unvisited, if so we recurse till we reach all the reachable cells in this dfs and mark them visited. 35 | 5. Since each dfs call in our iteration increased a room, thus counter stores the final count of room and is thus our required output. 36 | I've further marked each step in the code also, you'll se something like step 1 begins, step 1 ends etc. 37 | End of the solution. 38 | */ 39 | int n, m; 40 | string grid[1001]; 41 | bool vis[1001][1001]; 42 | // Step 4 begins 43 | void dfs(int i, int j) 44 | { 45 | vis[i][j] = 1; 46 | if(i && grid[i-1][j] == '.' && !vis[i-1][j])dfs(i-1, j); 47 | if(i < n && grid[i+1][j] == '.' && !vis[i+1][j])dfs(i+1, j); 48 | if(j < m && grid[i][j+1] == '.' && !vis[i][j+1])dfs(i, j+1); 49 | if(j && grid[i][j-1] == '.' && !vis[i][j-1])dfs(i, j-1); 50 | } 51 | // Step 4 ends 52 | int main() 53 | { 54 | ios::sync_with_stdio(false); 55 | cin.tie(0); 56 | cout.tie(0); 57 | int tests = 1; 58 | // cin >> tests; 59 | while(tests--) 60 | { 61 | // Step 1 begins 62 | cin >> n >> m; 63 | rep(i,0,n) 64 | { 65 | cin >> grid[i]; 66 | } 67 | int counter = 0; 68 | // Step 1 ends 69 | // Step 2 begins 70 | rep(i,0,n) 71 | { 72 | rep(j,0,m) 73 | { 74 | vis[i][j] = false; 75 | } 76 | } 77 | // Step 2 ends 78 | // Step 3 begins 79 | rep(i,0,n) 80 | { 81 | rep(j,0,m) 82 | { 83 | if(grid[i][j] == '.' && !vis[i][j]){ 84 | dfs(i, j); 85 | counter++; 86 | } 87 | } 88 | } 89 | // Step 3 ends 90 | // Step 5 begins 91 | cout << counter << endl; 92 | // Step 5 ends 93 | } 94 | return 0; 95 | } -------------------------------------------------------------------------------- /Graph_Algorithms/Round_Trip_II/TheAnshul756.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define N 100005 4 | 5 | vector stronglyConnectedNode; // vector to store strongly connected component 6 | vector cycle; // vector to store cycle 7 | vector conn[N], reverseConn[N]; 8 | stack topoOrder; // stack for topological sorting 9 | bool visited[N],visited2[N]; 10 | bool cycleFound; // global variable to end recursion when cycle found 11 | 12 | void findCycle(int node) { 13 | visited[node] = 1; 14 | visited2[node] = 1; 15 | cycle.push_back(node); 16 | 17 | for(auto i : conn[node]) { 18 | if(!visited[i]) { 19 | findCycle(i); 20 | } else if(visited2[i]) { 21 | cycle.push_back(i); 22 | cycleFound = true; 23 | return; 24 | } 25 | if(cycleFound) { 26 | return; 27 | } 28 | } 29 | if(cycleFound) { 30 | return; 31 | } 32 | 33 | cycle.pop_back(); 34 | } 35 | 36 | void topoSort(int node) { 37 | visited[node] = true; 38 | for(auto i : conn[node]) { 39 | if(!visited[i]) { 40 | topoSort(i); 41 | } 42 | } 43 | topoOrder.push(node); 44 | } 45 | 46 | void dfs(int node) { 47 | visited[node] = 1; 48 | stronglyConnectedNode.push_back(node); 49 | 50 | for(auto i: reverseConn[node]) { 51 | if(!visited[i]) { 52 | dfs(i); 53 | } 54 | } 55 | } 56 | 57 | // Solution starts here 58 | void solve() { 59 | /* 60 | So our task is to find a cycle in directed graph. Which is similar to find a Strongly Connected Component. 61 | But its not exactly same as strongly connected component have many cycles. So we are goinf to use Kosaraju's Algorithm 62 | to find strongly connected compopnent.(If you don't know this then read about it first.) Then once we have found a SCC 63 | (strongly connected compomnent) of size greater than 1 length then we need to find a cycle in it. We can use cycle 64 | detection algorithm here to detect cycle and while doing so we will also store the depth till that in a vector to print 65 | that cycle. That vector will look somewhat like this: 66 | n6 -> n7 -> n1 -> n2 -> n2 -> n3 -> n4 -> n1 67 | here lenght of cycle is 4. Once we find a cycle we will use a global variable to end our recursion function and insure 68 | not to delete from out cycle vector. Then we will iterate from backwads and store that cycle in a different vector. 69 | Note : because we are iterating backwards that cycle will be in reverse order so make sure to reverse it before printing. 70 | */ 71 | 72 | int n,m; 73 | cin >> n >> m; 74 | 75 | int a,b; 76 | for(int i = 0; i < m; i++) { 77 | cin >> a >> b; 78 | conn[a].push_back(b); // Original Graph 79 | reverseConn[b].push_back(a); // Transpose of Graph 80 | } 81 | 82 | 83 | // topological sort 84 | for(int i = 1; i <= n; i++) { 85 | if(!visited[i]) { 86 | topoSort(i); 87 | } 88 | } 89 | 90 | memset(visited,false,sizeof(bool)*(n+1)); 91 | 92 | // searching for SCC in transpose graph 93 | while(!topoOrder.empty()) { 94 | int node = topoOrder. top(); 95 | topoOrder.pop(); 96 | 97 | if(!visited[node]) { 98 | 99 | stronglyConnectedNode.clear(); 100 | 101 | dfs(node); 102 | 103 | if(stronglyConnectedNode.size() > 1) { 104 | // SCC found 105 | break; 106 | } 107 | 108 | } 109 | } 110 | if(stronglyConnectedNode.size() <= 1) { 111 | // No SCC thus no cycle. 112 | cout << "IMPOSSIBLE\n"; 113 | return; 114 | } 115 | 116 | // Cycle detection algorithm 117 | cycleFound = false; 118 | // rest all nodes which are not in SCC are marked visited so that we do not traverse through them 119 | memset(visited,true,sizeof(bool)*(n+1)); 120 | for(auto node: stronglyConnectedNode) { 121 | visited[node] = 0; 122 | } 123 | findCycle(stronglyConnectedNode[0]); 124 | 125 | 126 | vector out; 127 | int node = cycle.back(); 128 | cycle.pop_back(); 129 | 130 | // getting node of cylce from backwards 131 | out.push_back(node); 132 | while(cycle.back() != node) { 133 | out.push_back(cycle.back()); 134 | cycle.pop_back(); 135 | } 136 | out.push_back(node); 137 | 138 | // reversing cycle 139 | reverse(out.begin(),out.end()); 140 | cout << out.size() << endl; 141 | for(auto i : out) { 142 | cout << i << " "; 143 | } 144 | cout << endl; 145 | } 146 | 147 | int main() 148 | { 149 | // solution is in solve function 150 | solve(); 151 | return 0; 152 | } 153 | -------------------------------------------------------------------------------- /Graph_Algorithms/Shortest_Routes_I/kira-1008.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define tezi ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 4 | #define pb push_back 5 | #define loop(i,a,b) for(int i=a;i 23 | #define vi vector 24 | #define vll vector 25 | #define mem0(v) memset(v,0,sizeof(v)) 26 | #define memf(v) memset(v,false,sizeof(v)) 27 | #define memt(v) memset(v,true,sizeof(v)) 28 | #define mem(v,a) memset(v,a,sizeof(v)) 29 | #define npos string::npos 30 | #define inf 1e18 31 | #define MAX 1000000 32 | #define MOD 1000000007 33 | /**************SIEVE****************/ 34 | 35 | /*bool prime[MAX+1]; 36 | vector v; 37 | void sieve(){ 38 | // Create a boolean array "prime[0..n]" and initialize 39 | // all entries it as true. A value in prime[i] will 40 | // finally be false if i is Not a prime, else true. 41 | 42 | memset(prime, true, sizeof(prime)); 43 | 44 | for (long p=2; p*p<=MAX; p++) 45 | { 46 | // If prime[p] is not changed, then it is a prime 47 | if (prime[p] == true) 48 | { 49 | // Update all multiples of p greater than or 50 | // equal to the square of it 51 | // numbers which are multiple of p and are 52 | // less than p^2 are already been marked. 53 | for (long i=p*p; i<=MAX; i += p) 54 | prime[i] = false; 55 | } 56 | } 57 | for(int i=0;i<=MAX;i++)if(prime[i])v.pb(i); 58 | 59 | 60 | 61 | 62 | } 63 | 64 | /**************gcd****************/ 65 | 66 | ll gcd(ll a,ll b) 67 | { if(a>b)swap(a,b); 68 | if (a == 0) 69 | return b; 70 | return gcd(b%a, a); 71 | } 72 | 73 | 74 | 75 | /**************POWER****************/ 76 | ull binpow(ll a, ll b) { 77 | if (b == 0) 78 | return 1; 79 | ll res = binpow(a, b / 2)%MOD; 80 | if (b % 2) 81 | return ((ull)res * (ull)res * (ull)a)%MOD; 82 | else 83 | return ((ull)res * (ull)res)%MOD; 84 | } 85 | 86 | 87 | 88 | 89 | 90 | 91 | /**************MAIn****************/ 92 | 93 | const int N=1e5+5; 94 | vector> adj[N]; 95 | ll d[N]; 96 | bool vis[N]; 97 | 98 | int main() 99 | { 100 | #ifndef ONLINE_JUDGE 101 | freopen("input.txt","r",stdin); 102 | freopen("put.txt","w",stdout); 103 | #endif 104 | int T=1; 105 | //cin>>T; 106 | while(T--) 107 | { 108 | 109 | int n,m; 110 | cin>>n>>m; 111 | for(int i=0;i>a>>b>>c; 115 | a--;b--; 116 | adj[a].pb({b,c}); 117 | 118 | } 119 | for(int i=0;i> q; 126 | q.insert({0,0}); 127 | while(!q.empty()) 128 | {int v=q.begin()->second; 129 | vis[v]=1; 130 | q.erase(q.begin()); 131 | for(auto edge:adj[v]) 132 | {int child=edge.f; 133 | ll len=edge.s; 134 | if(d[v]+len 2 | // using namespace std; 3 | 4 | // // The following structure is needed 5 | // // to store pair sums in aux[] 6 | // class pairSum { 7 | // public: 8 | // // index (ll A[]) of first element in pair 9 | // ll first; 10 | 11 | // // index of second element in pair 12 | // ll sec; 13 | 14 | // // sum of the pair 15 | // ll sum; 16 | // }; 17 | 18 | // // Following function is needed 19 | // // for library function qsort() 20 | // ll compare(const void* a, const void* b) 21 | // { 22 | // return ((*(pairSum*)a).sum - (*(pairSum*)b).sum); 23 | // } 24 | 25 | // // Function to check if two given pairs 26 | // // have any common element or not 27 | // bool noCommon(pairSum a, pairSum b) 28 | // { 29 | // if (a.first == b.first || a.first == b.sec 30 | // || a.sec == b.first || a.sec == b.sec) 31 | // return false; 32 | // return true; 33 | // } 34 | 35 | // // The function finds four 36 | // // elements with given sum X 37 | // void findFourElements(ll arr[], ll n, ll X) 38 | // { 39 | // ll i, j; 40 | 41 | // // Create an auxiliary array 42 | // // to store all pair sums 43 | // ll size = (n * (n - 1)) / 2; 44 | // pairSum aux[size]; 45 | 46 | // // Generate all possible pairs 47 | // // from A[] and store sums 48 | // // of all possible pairs in aux[] 49 | // ll k = 0; 50 | // for (i = 0; i < n - 1; i++) { 51 | // for (j = i + 1; j < n; j++) { 52 | // aux[k].sum = arr[i] + arr[j]; 53 | // aux[k].first = i; 54 | // aux[k].sec = j; 55 | // k++; 56 | // } 57 | // } 58 | 59 | // // Sort the aux[] array using 60 | // // library function for sorting 61 | // qsort(aux, size, sizeof(aux[0]), compare); 62 | 63 | // // Now start two index variables 64 | // // from two corners of array 65 | // // and move them toward each other. 66 | // i = 0; 67 | // j = size - 1; 68 | // while (i < size && j >= 0) { 69 | // if ((aux[i].sum + aux[j].sum == X) 70 | // && noCommon(aux[i], aux[j])) { 71 | // cout << arr[aux[i].first] << ", " 72 | // << arr[aux[i].sec] << ", " 73 | // << arr[aux[j].first] << ", " 74 | // << arr[aux[j].sec] << endl; 75 | // return; 76 | // } 77 | // else if (aux[i].sum + aux[j].sum < X) 78 | // i++; 79 | // else 80 | // j--; 81 | // } 82 | // } 83 | 84 | // // Driver code 85 | // ll main() 86 | // { 87 | // ll arr[] = { 10, 20, 30, 40, 1, 2 }; 88 | // ll n = sizeof(arr) / sizeof(arr[0]); 89 | // ll X = 91; 90 | 91 | // // Function Call 92 | // findFourElements(arr, n, X); 93 | // return 0; 94 | // } 95 | 96 | // // This is code is contributed by rathbhupendra 97 | 98 | 99 | #include 100 | using namespace std ; 101 | #define ll long long 102 | #define pb push_back 103 | #define in(x) ll x; cin>>x; 104 | #define ln(x) long long x; cin>>x; 105 | #define memf(a) memset(a,false,sizeof(a)) 106 | #define F first 107 | #define S second 108 | #define endl "\n" 109 | #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 110 | ll power(ll b,ll e,ll m) 111 | { 112 | if(e==0) return 1; 113 | if(e&1) return b*power(b*b%m,e/2,m)%m; 114 | return power(b*b%m,e/2,m); 115 | } 116 | ll power( ll b, ll e) 117 | { 118 | if(e==0) return 1; 119 | if(e&1) return b*power(b*b,e/2); 120 | return power(b*b,e/2); 121 | } 122 | /*Function to check if two given pairs 123 | have any common element or not*/ 124 | bool noCommon(pair A, pair B) 125 | { 126 | if(A.F==B.F || A.F==B.S || A.S==B.F || A.S==B.S) 127 | return false; 128 | else 129 | return true; 130 | } 131 | int main() 132 | { 133 | fast 134 | ll n,x; 135 | cin>>n>>x; 136 | ll a[n]; 137 | for(ll i=0;i>a[i]; 140 | } 141 | ll k=n*(n-1)/2; 142 | pair> p[k]; 143 | ll y=0; 144 | /*Generate all possible pairs from a[] and store sums of all possible pairs in p along with indexes*/ 145 | for(ll i=0;i 3 | // *****************************Template Begins ************************** // 4 | #define int long long int 5 | #define pb push_back 6 | #define s second 7 | #define mii map 8 | #define dbg(x) { cerr<<#x<<": "< 12 | #define ppb pop_back 13 | #define f first 14 | #define vi vector 15 | #define vii vector> 16 | #define si set 17 | #define all(a) (a).begin(),(a).end() 18 | #define sz(x) (int )x.size() 19 | #define hell 1000000007 20 | #define mp make_pair 21 | #define yes cout << "YES" << endl; 22 | #define no cout << "NO" << endl; 23 | #define inf 1e18L+1 24 | #define endl '\n' 25 | using namespace std; 26 | #define N 300005 27 | // *****************************Template ends **************************** // 28 | /* 29 | Solution - 30 | So in this question we have to find shortest path in an undirected graph for a destination from a source. To do these kind of problems, implementing BFS is pretty standard, and i've also done the same. 31 | Since the logic behind the solution is pretty simple, let's walk through the solution - 32 | 1. Taking input and initializing variables, list of global variables defined along with their work - 33 | i. n, m is number of vertices and edges respectively. 34 | ii. Vector edge, is the adjaceny list for the graph. 35 | iii. vis is the array which is true, if that vertex is visited, false otherwise. 36 | iv. dist is the array which stores smallest distance of a vertex v from source s, and is equal to a large value(inf) if it is unreachable from source. 37 | 2. Now we apply BFS, we have implemented standard bfs traversal using queue, if you are unaware about its implementation, google it. 38 | 3. If the destination vertex is unreachable, we output IMPOSSIBLE, otherwise, we backtrack from our destination vertex to the source. 39 | I've further marked each step in the code also, you'int se something like step 1 begins, step 1 ends etc. 40 | Time Complexity Analysis - O(V + E) (V = Vertices and E = Edges) (since we're implementing BFS, each entry in adjacency list will be visited atmost once, and there are O(E) such entries and V vertices will be visited by BFS in worst case, for the backtracking part, O(E) entries will be used to reach source in the worst case and also O(V) vertex will be backtracked). 41 | End of the solution. 42 | */ 43 | int n, m, dist[N], vis[N]; 44 | vi edge[N]; 45 | int32_t main() 46 | { 47 | ios::sync_with_stdio(false); 48 | cin.tie(0); 49 | cout.tie(0); 50 | int tests = 1; 51 | // cin >> tests; 52 | while(tests--) 53 | { 54 | // step 1 begins. 55 | cin >> n >> m; 56 | rep(i, 0, m) 57 | { 58 | int a,b; 59 | cin >> a >> b; 60 | edge[a].pb(b); 61 | edge[b].pb(a); 62 | } 63 | rep(i, 1, n+1)dist[i] = inf; 64 | rep(i, 1, n+1)vis[i] = 0; 65 | // step 1 ends. 66 | // step 2 begins. 67 | queue q; 68 | q.push(1); 69 | dist[1] = 1; 70 | while(!q.empty()) 71 | { 72 | // cout << 1; 73 | int k = q.front(); 74 | q.pop(); 75 | if(vis[k])continue; 76 | vis[k] = 1; 77 | for(auto itr:edge[k]) 78 | { 79 | if(!vis[itr]) 80 | q.push(itr); 81 | dist[itr] = min(dist[itr], dist[k]+1); 82 | } 83 | } 84 | // step 2 ends. 85 | // step 3 begins. 86 | if(dist[n] == inf) 87 | { 88 | cout << "IMPOSSIBLE"; 89 | return 0; 90 | } 91 | int node = n; 92 | vi ans ; 93 | cout << dist[n] << endl; 94 | while(dist[node] > 1) 95 | { 96 | ans.pb(node); 97 | for(auto itr:edge[node]) 98 | { 99 | if(dist[itr] == dist[node] - 1) 100 | { 101 | node = itr; 102 | break; 103 | } 104 | } 105 | } 106 | ans.pb(node); 107 | reverse(all(ans)); 108 | for(auto itr:ans)cout << itr << " "; 109 | // step 3 ends. 110 | } 111 | return 0; 112 | } -------------------------------------------------------------------------------- /Graph_Algorithms/Monsters/king-01.cpp: -------------------------------------------------------------------------------- 1 | // problem link - https://cses.fi/problemset/task/1194 2 | #include 3 | #include 4 | #include 5 | // #pragma GCC optimize("-O3") 6 | // #pragma GCC optimize("Ofast") 7 | // #pragma GCC target("avx,avx2,fma") 8 | using namespace std; 9 | using namespace __gnu_pbds; 10 | #ifdef ON_LINUX 11 | #include 12 | #define meminc rlimit rlim;if (getrlimit(RLIMIT_STACK, &rlim)) return 1;rlim.rlim_cur = 26843556;if (setrlimit(RLIMIT_STACK, &rlim)) return 2; 13 | #else 14 | #define meminc 15 | #endif 16 | #ifdef LOCAL 17 | #define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator _it(_ss); err(_it, args); } 18 | #else 19 | #define dbg(args...) 20 | #endif 21 | struct custom_hash { 22 | static uint64_t splitmix64(uint64_t x) { 23 | // http://xorshift.di.unimi.it/splitmix64.c 24 | x += 0x9e3779b97f4a7c15; 25 | x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; 26 | x = (x ^ (x >> 27)) * 0x94d049bb133111eb; 27 | return x ^ (x >> 31); 28 | } 29 | 30 | size_t operator()(uint64_t x) const { 31 | static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); 32 | return splitmix64(x + FIXED_RANDOM); 33 | } 34 | }; 35 | void err(istream_iterator it) {} 36 | template 37 | void err(istream_iterator it, T a, Args... args) { 38 | cerr << *it << " = " << a << endl; 39 | err(++it, args...); 40 | } 41 | template std::ostream& operator<<(std::ostream& out, std::pair a) { 42 | out<< a.first << " " << a.second; 43 | return out; 44 | } 45 | 46 | template std::istream& operator>>(std::istream& in, std::pair &a) { 47 | in >> a.first >> a.second; 48 | return in; 49 | } 50 | #define int long long int 51 | #define ll int 52 | #define x first 53 | #define y second 54 | #define pii pair 55 | #define vii vector 56 | #define vi vector 57 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 58 | // -------------------Standard Traversal Moves--------------------- 59 | vi movement_in_x = {1 ,-1 ,0, 0}, movement_in_y = {0, 0, -1, 1}; 60 | // vi movement_in_x = {2, -2, 2, -2, 1, -1, 1, -1}, movement_in_y = {1, 1, -1, -1, 2, 2, -2, -2}; 61 | // vi movement_in_x = {1, 1, 1, -1, -1 , -1, 0, 0}, movement_in_y = {1, -1, 0, 1, -1, 0, 1, -1}; 62 | // ---------------------------------------------------------------- 63 | 64 | #define rep(i, a, b) for(int i=a;i -1 and y > -1 and x < n and y < m); 84 | } 85 | void bfs(int starting_x, int starting_y) 86 | { 87 | priority_queue> pq; 88 | rep(i, 0, n) 89 | { 90 | rep(j ,0, m) 91 | { 92 | if(labyrinth[i][j] == 'M') 93 | { 94 | pq.push({{0, 2}, {i, j}}); 95 | is_visited[i][j] = 1; 96 | } 97 | } 98 | } 99 | pq.push({{0, 1}, {starting_x, starting_y}}); 100 | is_visited[starting_x][starting_y] = 1; 101 | while(!pq.empty()) 102 | { 103 | auto p = pq.top(); 104 | pq.pop(); 105 | int time = p.first.first, type = p.first.second, current_x = p.second.first, current_y = p.second.second; 106 | if(type == 1 and (current_x == 0 or current_y == 0 or current_x == n - 1 or current_y == m - 1)) 107 | { 108 | key = 1; 109 | while(current_x != starting_x or current_y != starting_y) 110 | { 111 | path += previous_move[current_x][current_y]; 112 | if(previous_move[current_x][current_y] == 'R') 113 | { 114 | current_y--; 115 | } 116 | else if(previous_move[current_x][current_y] == 'L') 117 | { 118 | current_y++; 119 | } 120 | else if(previous_move[current_x][current_y] == 'D')current_x--; 121 | else current_x++; 122 | } 123 | reverse(all(path)); 124 | return ; 125 | } 126 | rep(i, 0, 4) 127 | { 128 | int new_x = current_x + movement_in_x[i], new_y = current_y + movement_in_y[i]; 129 | if(!check(new_x, new_y) or is_visited[new_x][new_y] or labyrinth[new_x][new_y] == '#')continue; 130 | pq.push({{time - 1, type}, {new_x, new_y}}); 131 | is_visited[new_x][new_y] = 1; 132 | if(movement_in_x[i] == 1) 133 | { 134 | previous_move[new_x][new_y] = 'D'; 135 | } 136 | else if(movement_in_x[i] == -1)previous_move[new_x][new_y] = 'U'; 137 | else if(movement_in_y[i] == -1)previous_move[new_x][new_y] = 'L'; 138 | else previous_move[new_x][new_y] = 'R'; 139 | } 140 | } 141 | 142 | } 143 | int32_t main() 144 | { 145 | // meminc; 146 | ios::sync_with_stdio(false); 147 | cin.tie(nullptr); 148 | cerr.precision(10); 149 | cout.precision(25); 150 | cout << fixed; 151 | #ifdef ON_LINUX 152 | // for getting input from input.txt 153 | freopen("input.txt", "r", stdin); 154 | // for writing output to output.txt 155 | freopen("output.txt", "w", stdout); 156 | #endif 157 | int tests = 1; 158 | // cin >> tests; 159 | rep(test, 1, tests+1) 160 | { 161 | cin >> n; 162 | cin >> m; 163 | rep(i, 0, n)cin >> labyrinth[i]; 164 | memset(is_visited ,0, sizeof is_visited); 165 | int starting_x, starting_y; 166 | rep(i, 0, n) 167 | { 168 | rep(j, 0 ,m) 169 | { 170 | if(labyrinth[i][j] == 'A') 171 | { 172 | starting_x = i; 173 | starting_y = j; 174 | bfs(starting_x, starting_y); 175 | if(!key)no; 176 | else 177 | { 178 | yes; 179 | cout << sz(path) << endl; 180 | cout << path << endl; 181 | } 182 | return 0; 183 | } 184 | } 185 | } 186 | } 187 | #ifdef LOCAL 188 | // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " labyrinth.\n"; 189 | #endif 190 | return 0; 191 | } 192 | -------------------------------------------------------------------------------- /Range_Queries/Subarray_Sum_Queries/King-01.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://cses.fi/problemset/task/1190/ 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | using namespace __gnu_pbds; 7 | //******************************************Template Begins*************************************************************// 8 | #ifdef LOCAL 9 | #define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator _it(_ss); err(_it, args); } 10 | #else 11 | #define dbg(args...) 12 | #endif 13 | struct custom_hash { 14 | static uint64_t splitmix64(uint64_t x) { 15 | // http://xorshift.di.unimi.it/splitmix64.c 16 | x += 0x9e3779b97f4a7c15; 17 | x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; 18 | x = (x ^ (x >> 27)) * 0x94d049bb133111eb; 19 | return x ^ (x >> 31); 20 | } 21 | 22 | size_t operator()(uint64_t x) const { 23 | static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); 24 | return splitmix64(x + FIXED_RANDOM); 25 | } 26 | }; 27 | void err(istream_iterator it) {} 28 | template 29 | void err(istream_iterator it, T a, Args... args) { 30 | cerr << *it << " = " << a << endl; 31 | err(++it, args...); 32 | } 33 | #define ll int 34 | #define vi vector 35 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 36 | // -------------------Standard Traversal Moves--------------------- 37 | // vi fx = {1 ,-1 ,0, 0}, fy = {0, 0, -1, 1}; 38 | // vi fx = {2, -2, 2, -2, 1, -1, 1, -1}, fy = {1, 1, -1, -1, 2, 2, -2, -2}; 39 | // vi fx = {1, 1, 1, -1, -1 , -1, 0, 0}, fy = {1, -1, 0, 1, -1, 0, 1, -1}; 40 | // ---------------------------------------------------------------- 41 | 42 | #define rep(i, a, b) for(int i=a;i= 0 63 | Now if we have single element, we can simply create it like i've done on line 69 64 | To perform merge operation that i've implemented on line 79 - 65 | Sum of parent will simple be sum of left child and right child 66 | Prefix of parent will be 0 or prefix of left child or sum of left child + prefix of right child 67 | Suffix of parent will be 0 or Suffix of right child or sum of right child + Suffix of left child 68 | Ans of parent will either be 0 or answer of left child or answer of right child or suffix of left child + prefix of right child. 69 | */ 70 | // standard item class to mange the queries 71 | struct item{ 72 | long long int ans, sum, pref, suf; 73 | item(){ 74 | ans = sum = pref = suf = 0; 75 | } 76 | item(long long int ok) 77 | { 78 | ans = max(0LL, ok); 79 | sum = ok; 80 | pref = max(0LL, ok); 81 | suf = max(0LL, ok); 82 | } 83 | // Overloading + operator so that we can perform merge operation in our segment tree without changing anything inside segtree class. 84 | // For more clarity about need to overload it you can watch out lines numbered 156, 169. 85 | item operator+(const item &op1) 86 | { 87 | item ret; 88 | ret.sum = sum + op1.sum; 89 | ret.pref = max({ 90 | 0LL, pref, sum + op1.pref 91 | }); 92 | ret.suf = max({ 93 | 0LL, op1.suf, op1.sum + suf 94 | }); 95 | ret.ans = max({ 96 | ans, op1.ans, suf + op1.pref 97 | }); 98 | return ret; 99 | } 100 | }; 101 | //O(N) preprocessing time and O(logN) query time amortized. 102 | // take care this implementation is 0 indexed! 103 | // segtree st; 104 | // st.init(n); 105 | // st.build(v); 106 | // ************************Basic Segment Tree Template Begins********************// 107 | template 108 | struct segtree{ 109 | int sz; 110 | vector values; 111 | itemType NEUTRAL_ELEMENT; 112 | itemType merge(itemType fi, itemType se) 113 | { 114 | return {fi.val + se.val}; 115 | } 116 | 117 | itemType single(B val) 118 | { 119 | // if(val > 0)return {val, val, val, val}; 120 | // return {0, 0 , 0, val}; 121 | return {val}; 122 | } 123 | inline int ri(int _gg){return _gg * 2 + 2;} 124 | inline int le(int _gg){return _gg * 2 + 1;} 125 | void init(int new_size) 126 | { 127 | sz = 1; 128 | while(sz < new_size)sz <<= 1; 129 | values.resize(sz << 1); 130 | } 131 | void build(vector &v, int x, int lx, int rx) 132 | { 133 | if(rx - lx == 1) 134 | { 135 | if(lx < sz(v)) 136 | { 137 | values[x] = v[lx]; 138 | } 139 | return ; 140 | } 141 | int mx = (lx + rx) >> 1; 142 | build(v, le(x), lx, mx); 143 | build(v, ri(x), mx, rx); 144 | 145 | values[x] = values[le(x)] + values[ri(x)]; 146 | } 147 | void build(vector &v) 148 | { 149 | /****************************************************************************/ 150 | // REMEMBER TO CHANGE IT IF OUR FIRST ARGUMENT IS NOT ITEM WHICH IS RARE THO 151 | // initializing neutral value 152 | NEUTRAL_ELEMENT = item(); 153 | /*****************************************************************************/ 154 | int _n = v.size(); 155 | init(_n); 156 | build(v, 0, 0, sz); 157 | } 158 | void set(int i, B val, int x, int lx, int rx) 159 | { 160 | if(rx - lx == 1) 161 | { 162 | values[x] = val; 163 | return ; 164 | } 165 | int mx = (lx + rx) >> 1; 166 | if(i < mx) 167 | { 168 | set(i, val, le(x), lx, mx); 169 | } 170 | else 171 | set(i, val, ri(x), mx, rx); 172 | values[x] = values[le(x)] + values[ri(x)]; 173 | } 174 | void set(int i, B val) 175 | { 176 | set(i, val, 0, 0, sz); 177 | } 178 | itemType calc(int l, int r, int x, int lx, int rx) 179 | { 180 | if(lx >= r or l >= rx)return NEUTRAL_ELEMENT; 181 | if(lx >= l and rx <= r)return values[x]; 182 | int mx = (lx + rx) >> 1; 183 | itemType f1 = calc(l ,r ,le(x), lx, mx); 184 | itemType f2 = calc(l ,r ,ri(x), mx, rx); 185 | return f1 + f2; 186 | } 187 | itemType calc(int l, int r) 188 | { 189 | return calc(l, r, 0, 0, sz); 190 | } 191 | }; 192 | // ************************Basic Segment Tree Template Ends********************// 193 | 194 | 195 | int32_t main() 196 | { 197 | /* Code for Fast I/O */ 198 | ios::sync_with_stdio(false); 199 | cin.tie(nullptr); 200 | cerr.precision(10); 201 | cout.precision(25); 202 | cout << fixed; 203 | #ifdef LOCAL 204 | // for getting input from input.txt 205 | freopen("input.txt", "r", stdin); 206 | // for writing output to output.txt 207 | freopen("output.txt", "w", stdout); 208 | #endif 209 | int tests = 1; 210 | // cin >> tests; 211 | rep(test, 1, tests+1) 212 | { 213 | cin >> n; 214 | cin >> m; 215 | // initializing vector to initialize segment tree 216 | vector to_create(n); 217 | // Creating an object of segment tree class defined above 218 | segtree st; 219 | // reading initial values 220 | rep(i, 0, n)cin >> to_create[i]; 221 | // Calling build function for segment tree 222 | st.build(to_create); 223 | // Processing the queries. 224 | while(m--) 225 | { 226 | int u, v; 227 | cin >> u >> v; 228 | u--; 229 | st.set(u, v); 230 | cout << st.calc(0, n).ans << endl; 231 | } 232 | } 233 | #ifdef LOCAL 234 | // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; 235 | #endif 236 | return 0; 237 | } -------------------------------------------------------------------------------- /Range_Queries/Distinct_Values_Queries/King-01.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link - https://cses.fi/problemset/task/1734 2 | #include 3 | #include 4 | #include 5 | // #pragma GCC optimize("-O3") 6 | // #pragma GCC optimize("Ofast") 7 | // #pragma GCC target("avx,avx2,fma") 8 | //*****************************************Base Template Begins**********************************************// 9 | using namespace std; 10 | using namespace __gnu_pbds; 11 | #ifdef LOCAL 12 | #define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator _it(_ss); err(_it, args); } 13 | #else 14 | #define dbg(args...) 15 | #endif 16 | struct custom_hash { 17 | static uint64_t splitmix64(uint64_t x) { 18 | // http://xorshift.di.unimi.it/splitmix64.c 19 | x += 0x9e3779b97f4a7c15; 20 | x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; 21 | x = (x ^ (x >> 27)) * 0x94d049bb133111eb; 22 | return x ^ (x >> 31); 23 | } 24 | 25 | size_t operator()(uint64_t x) const { 26 | static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); 27 | return splitmix64(x + FIXED_RANDOM); 28 | } 29 | }; 30 | void err(istream_iterator it) {} 31 | template 32 | void err(istream_iterator it, T a, Args... args) { 33 | cerr << *it << " = " << a << endl; 34 | err(++it, args...); 35 | } 36 | #define ll int 37 | #define vi vector 38 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 39 | // -------------------Standard Traversal Moves--------------------- 40 | // vi fx = {1 ,-1 ,0, 0}, fy = {0, 0, -1, 1}; 41 | // vi fx = {2, -2, 2, -2, 1, -1, 1, -1}, fy = {1, 1, -1, -1, 2, 2, -2, -2}; 42 | // vi fx = {1, 1, 1, -1, -1 , -1, 0, 0}, fy = {1, -1, 0, 1, -1, 0, 1, -1}; 43 | // ---------------------------------------------------------------- 44 | 45 | #define rep(i, a, aux) for(int i=a;i 107 | struct segtree{ 108 | int sz; 109 | vector values; 110 | itemType NEUTRAL_ELEMENT; 111 | itemType merge(itemType fi, itemType se) 112 | { 113 | return {fi.val + se.val}; 114 | } 115 | 116 | itemType single(aux val) 117 | { 118 | // if(val > 0)return {val, val, val, val}; 119 | // return {0, 0 , 0, val}; 120 | return {val}; 121 | } 122 | inline int ri(int _gg){return _gg * 2 + 2;} 123 | inline int le(int _gg){return _gg * 2 + 1;} 124 | void init(int new_size) 125 | { 126 | sz = 1; 127 | while(sz < new_size)sz <<= 1; 128 | values.resize(sz << 1); 129 | } 130 | void build(vector &v, int x, int lx, int rx) 131 | { 132 | if(rx - lx == 1) 133 | { 134 | if(lx < sz(v)) 135 | { 136 | values[x] = v[lx]; 137 | } 138 | return ; 139 | } 140 | int mx = (lx + rx) >> 1; 141 | build(v, le(x), lx, mx); 142 | build(v, ri(x), mx, rx); 143 | 144 | values[x] = values[le(x)] + values[ri(x)]; 145 | } 146 | void build(vector &v) 147 | { 148 | /****************************************************************************/ 149 | // REMEMBER TO CHANGE IT IF OUR FIRST ARGUMENT IS NOT ITEM WHICH IS RARE THO 150 | // initializing neutral value 151 | NEUTRAL_ELEMENT = item(); 152 | /*****************************************************************************/ 153 | int _n = v.size(); 154 | init(_n); 155 | build(v, 0, 0, sz); 156 | } 157 | void set(int i, aux val, int x, int lx, int rx) 158 | { 159 | if(rx - lx == 1) 160 | { 161 | values[x] = val; 162 | return ; 163 | } 164 | int mx = (lx + rx) >> 1; 165 | if(i < mx) 166 | { 167 | set(i, val, le(x), lx, mx); 168 | } 169 | else 170 | set(i, val, ri(x), mx, rx); 171 | values[x] = values[le(x)] + values[ri(x)]; 172 | } 173 | void set(int i, aux val) 174 | { 175 | set(i, val, 0, 0, sz); 176 | } 177 | itemType calc(int l, int r, int x, int lx, int rx) 178 | { 179 | if(lx >= r or l >= rx)return NEUTRAL_ELEMENT; 180 | if(lx >= l and rx <= r)return values[x]; 181 | int mx = (lx + rx) >> 1; 182 | itemType f1 = calc(l ,r ,le(x), lx, mx); 183 | itemType f2 = calc(l ,r ,ri(x), mx, rx); 184 | return f1 + f2; 185 | } 186 | itemType calc(int l, int r) 187 | { 188 | return calc(l, r, 0, 0, sz); 189 | } 190 | }; 191 | // ************************Basic Segment Tree Template Ends********************// 192 | 193 | 194 | int32_t main() 195 | { 196 | ios::sync_with_stdio(false); 197 | cin.tie(nullptr); 198 | cerr.precision(10); 199 | cout.precision(25); 200 | cout << fixed; 201 | #ifdef LOCAL 202 | // for getting input from input.txt 203 | freopen("input.txt", "r", stdin); 204 | // for writing output to output.txt 205 | freopen("output.txt", "w", stdout); 206 | #endif 207 | int tests = 1; 208 | // cin >> tests; 209 | rep(test, 1, tests+1) 210 | { 211 | // step 1 begins. 212 | cin >> n; 213 | cin >> m; 214 | segtree st; 215 | vector to_create(n + 1, 0); 216 | map next_greater_index; 217 | rep(i, 0, n)cin >> a[i]; 218 | // step 1 ends. 219 | // step 2 begins. 220 | int aux[n]; 221 | for(int i = n - 1; i > -1; i--) 222 | { 223 | if(next_greater_index.find(a[i]) == next_greater_index.end()) 224 | { 225 | aux[i] = n; 226 | } 227 | else 228 | { 229 | aux[i] = next_greater_index[a[i]]; 230 | } 231 | next_greater_index[a[i]] = i; 232 | } 233 | // step 2 ends. 234 | // step 3 begins. 235 | rep(i, 0, n) 236 | { 237 | if(next_greater_index[a[i]] == i)to_create[i] = 1; 238 | } 239 | // step 3 ends. 240 | // step 4 begins. 241 | st.build(to_create); 242 | vector, int>> queries(m); 243 | rep(i, 0, m) 244 | { 245 | cin >> queries[i].first.first >> queries[i].first.second; 246 | queries[i].second = i; 247 | } 248 | sort(all(queries)); 249 | // step 4 ends. 250 | int i = 0, ans[n]; 251 | // step 5 begins. 252 | rep(j, 0, m) 253 | { 254 | int lidx = queries[j].first.first, ridx = queries[j].first.second; 255 | lidx--; 256 | while(i < lidx) 257 | { 258 | st.set(aux[i], 1); 259 | i++; 260 | } 261 | ans[queries[j].second] = st.calc(lidx, ridx).val; 262 | } 263 | rep(j, 0, m)cout << ans[j] << endl; 264 | // step 5 ends. 265 | } 266 | #ifdef LOCAL 267 | // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; 268 | #endif 269 | return 0; 270 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CSES-Solutions 2 | 3 | Solutions and Editorials to problems of [CSES](https://cses.fi/problemset/). 4 | 5 | ## Current Status 6 |
Introductory Problems 7 |

8 | 9 | - [x] - [Weird Algorithm](https://cses.fi/problemset/task/1068) 10 | - [x] - [Missing Number](https://cses.fi/problemset/task/1083) 11 | - [x] - [Repetitions](https://cses.fi/problemset/task/1069) 12 | - [x] - [Increasing Array](https://cses.fi/problemset/task/1094) 13 | - [x] - [Permutations](https://cses.fi/problemset/task/1070) 14 | - [x] - [Number Spiral](https://cses.fi/problemset/task/1071) 15 | - [x] - [Two Knights](https://cses.fi/problemset/task/1072) 16 | - [x] - [Two Sets](https://cses.fi/problemset/task/1092) 17 | - [x] - [Bit Strings](https://cses.fi/problemset/task/1617) 18 | - [x] - [Trailing Zeros](https://cses.fi/problemset/task/1618) 19 | - [x] - [Coin Piles](https://cses.fi/problemset/task/1754) 20 | - [x] - [Palindrome Reorder](https://cses.fi/problemset/task/1755) 21 | - [x] - [Creating Strings I](https://cses.fi/problemset/task/1622) 22 | - [x] - [Apple Division](https://cses.fi/problemset/task/1623) 23 | - [x] - [Chessboard and Queens](https://cses.fi/problemset/task/1624) 24 | - [ ] - [Grid Paths](https://cses.fi/problemset/task/1625) 25 | 26 |

27 |
28 |
Sorting and Searching 29 |

30 | 31 | - [ ] - [Distinct Numbers](https://cses.fi/problemset/task/1621) 32 | - [ ] - [Apartments](https://cses.fi/problemset/task/1084) 33 | - [ ] - [Ferris Wheel](https://cses.fi/problemset/task/1090) 34 | - [ ] - [Concert Tickets](https://cses.fi/problemset/task/1091) 35 | - [ ] - [Restaurant Customers](https://cses.fi/problemset/task/1619) 36 | - [ ] - [Movie Festival](https://cses.fi/problemset/task/1629) 37 | - [ ] - [Sum of Two Values](https://cses.fi/problemset/task/1640) 38 | - [ ] - [Maximum Subarray Sum](https://cses.fi/problemset/task/1643) 39 | - [ ] - [Stick Lengths](https://cses.fi/problemset/task/1074) 40 | - [ ] - [Playlist](https://cses.fi/problemset/task/1141) 41 | - [ ] - [Towers](https://cses.fi/problemset/task/1073) 42 | - [ ] - [Traffic Lights](https://cses.fi/problemset/task/1163) 43 | - [ ] - [Room Allocation](https://cses.fi/problemset/task/1164) 44 | - [ ] - [Factory Machines](https://cses.fi/problemset/task/1620) 45 | - [ ] - [Tasks and Deadlines](https://cses.fi/problemset/task/1630) 46 | - [ ] - [Reading Books](https://cses.fi/problemset/task/1631) 47 | - [ ] - [Sum of Three Values](https://cses.fi/problemset/task/1641) 48 | - [ ] - [Sum of Four Values](https://cses.fi/problemset/task/1642) 49 | - [ ] - [Nearest Smaller Values](https://cses.fi/problemset/task/1645) 50 | - [ ] - [Subarray Sums I](https://cses.fi/problemset/task/1660) 51 | - [ ] - [Subarray Sums II](https://cses.fi/problemset/task/1661) 52 | - [ ] - [Subarray Divisibility](https://cses.fi/problemset/task/1662) 53 | - [ ] - [Array Division](https://cses.fi/problemset/task/1085) 54 | - [ ] - [Sliding Median](https://cses.fi/problemset/task/1076) 55 | - [ ] - [Sliding Cost](https://cses.fi/problemset/task/1077) 56 | - [ ] - [Movie Festival II](https://cses.fi/problemset/task/1632) 57 | - [ ] - [Maximum Subarray Sum II](https://cses.fi/problemset/task/1644) 58 |

59 |
60 |
Dynamic Programming 61 |

62 | 63 | - [ ] - [Dice Combinations](https://cses.fi/problemset/task/1633) 64 | - [ ] - [Minimizing Coins](https://cses.fi/problemset/task/1634) 65 | - [ ] - [Coin Combinations I](https://cses.fi/problemset/task/1635) 66 | - [ ] - [Coin Combinations II](https://cses.fi/problemset/task/1636) 67 | - [ ] - [Removing Digits](https://cses.fi/problemset/task/1637) 68 | - [ ] - [Grid Paths](https://cses.fi/problemset/task/1638) 69 | - [ ] - [Book Shop](https://cses.fi/problemset/task/1158) 70 | - [ ] - [Array Description](https://cses.fi/problemset/task/1746) 71 | - [ ] - [Edit Distance](https://cses.fi/problemset/task/1639) 72 | - [ ] - [Rectangle Cutting](https://cses.fi/problemset/task/1744) 73 | - [ ] - [Money Sums](https://cses.fi/problemset/task/1745) 74 | - [ ] - [Removal Game](https://cses.fi/problemset/task/1097) 75 | - [ ] - [Two Sets II](https://cses.fi/problemset/task/1093) 76 | - [ ] - [Increasing Subsequence](https://cses.fi/problemset/task/1145) 77 | - [ ] - [Projects](https://cses.fi/problemset/task/1140) 78 |

79 |
80 |
Graph Algorithms 81 |

82 | 83 | - [x] - [Counting Rooms](https://cses.fi/problemset/task/1192) 84 | - [x] - [Labyrinth](https://cses.fi/problemset/task/1193) 85 | - [x] - [Building Roads](https://cses.fi/problemset/task/1666) 86 | - [x] - [Message Route](https://cses.fi/problemset/task/1667) 87 | - [ ] - [Building Teams](https://cses.fi/problemset/task/1668) 88 | - [ ] - [Round Trip](https://cses.fi/problemset/task/1669) 89 | - [x] - [Monsters](https://cses.fi/problemset/task/1194) 90 | - [x] - [Shortest Routes I](https://cses.fi/problemset/task/1671) 91 | - [x] - [Shortest Routes II](https://cses.fi/problemset/task/1672) 92 | - [ ] - [High Score](https://cses.fi/problemset/task/1673) 93 | - [ ] - [Flight Discount](https://cses.fi/problemset/task/1195) 94 | - [ ] - [Cycle Finding](https://cses.fi/problemset/task/1197) 95 | - [ ] - [Flight Routes](https://cses.fi/problemset/task/1196) 96 | - [x] - [Round Trip II](https://cses.fi/problemset/task/1678) 97 | - [x] - [Course Schedule](https://cses.fi/problemset/task/1679) 98 | - [ ] - [Longest Flight Route](https://cses.fi/problemset/task/1680) 99 | - [x] - [Game Routes](https://cses.fi/problemset/task/1681) 100 | - [ ] - [Investigation](https://cses.fi/problemset/task/1202) 101 | - [ ] - [Planets Queries I](https://cses.fi/problemset/task/1750) 102 | - [ ] - [Planets Queries II](https://cses.fi/problemset/task/1160) 103 | - [ ] - [Planets Cycles](https://cses.fi/problemset/task/1751) 104 | - [ ] - [Road Reparation](https://cses.fi/problemset/task/1675) 105 | - [ ] - [Road Construction](https://cses.fi/problemset/task/1676) 106 | - [ ] - [Flight Routes Check](https://cses.fi/problemset/task/1682) 107 | - [ ] - [Planets and Kingdoms](https://cses.fi/problemset/task/1683) 108 | - [ ] - [Giant Pizza](https://cses.fi/problemset/task/1684) 109 | - [ ] - [Coin Collector](https://cses.fi/problemset/task/1686) 110 | - [ ] - [Mail Delivery](https://cses.fi/problemset/task/1691) 111 | - [ ] - [De Bruijn Sequence](https://cses.fi/problemset/task/1692) 112 | - [ ] - [Teleporters Path](https://cses.fi/problemset/task/1693) 113 | - [ ] - [Hamiltonian Flights](https://cses.fi/problemset/task/1690) 114 | - [ ] - [Knight's Tour](https://cses.fi/problemset/task/1689) 115 | - [ ] - [Download Speed](https://cses.fi/problemset/task/1694) 116 | - [ ] - [Police Chase](https://cses.fi/problemset/task/1695) 117 | - [ ] - [School Dance](https://cses.fi/problemset/task/1696) 118 | - [ ] - [Distinct Routes](https://cses.fi/problemset/task/1711) 119 |

120 |
121 |
Range Queries 122 |

123 | 124 | - [ ] - [Range Sum Queries I](https://cses.fi/problemset/task/1646) 125 | - [x] - [Range Minimum Queries I](https://cses.fi/problemset/task/1647) 126 | - [ ] - [Range Sum Queries II](https://cses.fi/problemset/task/1648) 127 | - [x] - [Range Minimum Queries II](https://cses.fi/problemset/task/1649) 128 | - [x] - [Range Xor Queries](https://cses.fi/problemset/task/1650) 129 | - [ ] - [Range Update Queries](https://cses.fi/problemset/task/1651) 130 | - [ ] - [Forest Queries](https://cses.fi/problemset/task/1652) 131 | - [ ] - [Hotel Queries](https://cses.fi/problemset/task/1143) 132 | - [ ] - [List Removals](https://cses.fi/problemset/task/1749) 133 | - [ ] - [Salary Queries](https://cses.fi/problemset/task/1144) 134 | - [x] - [Subarray Sum Queries](https://cses.fi/problemset/task/1190) 135 | - [x] - [Distinct Values Queries](https://cses.fi/problemset/task/1734) 136 | - [ ] - [Forest Queries II](https://cses.fi/problemset/task/1739) 137 | - [ ] - [Range Updates and Sums](https://cses.fi/problemset/task/1735) 138 | - [ ] - [Polynomial Queries](https://cses.fi/problemset/task/1736) 139 | - [ ] - [Range Queries and Copies](https://cses.fi/problemset/task/1737) 140 |

141 |
142 |
Tree Algorithms 143 |

144 | 145 | - [x] - [Subordinates](https://cses.fi/problemset/task/1674) 146 | - [ ] - [Tree Matching](https://cses.fi/problemset/task/1130) 147 | - [x] - [Tree Diameter](https://cses.fi/problemset/task/1131) 148 | - [x] - [Tree Distances I](https://cses.fi/problemset/task/1132) 149 | - [ ] - [Tree Distances II](https://cses.fi/problemset/task/1133) 150 | - [x] - [Company Queries I](https://cses.fi/problemset/task/1687) 151 | - [x] - [Company Queries II](https://cses.fi/problemset/task/1688) 152 | - [x] - [Distance Queries](https://cses.fi/problemset/task/1135) 153 | - [ ] - [Counting Paths](https://cses.fi/problemset/task/1136) 154 | - [x] - [Subtree Queries](https://cses.fi/problemset/task/1137) 155 | - [x] - [Path Queries](https://cses.fi/problemset/task/1138) 156 | - [ ] - [Distinct Colors](https://cses.fi/problemset/task/1139) 157 |

158 |
159 |
Mathematics 160 |

161 | 162 | - [ ] - [Exponentiation](https://cses.fi/problemset/task/1095) 163 | - [ ] - [Exponentiation II](https://cses.fi/problemset/task/1712) 164 | - [ ] - [Counting Divisors](https://cses.fi/problemset/task/1713) 165 | - [ ] - [Common Divisors](https://cses.fi/problemset/task/1081) 166 | - [ ] - [Sum of Divisors](https://cses.fi/problemset/task/1082) 167 | - [ ] - [Binomial Coefficients](https://cses.fi/problemset/task/1079) 168 | - [ ] - [Creating Strings II](https://cses.fi/problemset/task/1715) 169 | - [ ] - [Distributing Apples](https://cses.fi/problemset/task/1716) 170 | - [ ] - [Christmas Party](https://cses.fi/problemset/task/1717) 171 | - [ ] - [Fibonacci Numbers](https://cses.fi/problemset/task/1722) 172 | - [ ] - [Throwing Dice](https://cses.fi/problemset/task/1096) 173 | - [ ] - [Graph Paths I](https://cses.fi/problemset/task/1723) 174 | - [ ] - [Graph Paths II](https://cses.fi/problemset/task/1724) 175 | - [ ] - [Dice Probability](https://cses.fi/problemset/task/1725) 176 | - [ ] - [Moving Robots](https://cses.fi/problemset/task/1726) 177 | - [ ] - [Candy Lottery](https://cses.fi/problemset/task/1727) 178 | - [ ] - [Inversion Probability](https://cses.fi/problemset/task/1728) 179 | - [ ] - [Stick Game](https://cses.fi/problemset/task/1729) 180 | - [ ] - [Nim Game I](https://cses.fi/problemset/task/1730) 181 | - [ ] - [Nim Game II](https://cses.fi/problemset/task/1098) 182 | - [ ] - [Stair Game](https://cses.fi/problemset/task/1099) 183 |

184 |
185 |
String Algorithms 186 |

187 | 188 | - [ ] - [Word Combinations](https://cses.fi/problemset/task/1731) 189 | - [ ] - [String Matching](https://cses.fi/problemset/task/1753) 190 | - [ ] - [Finding Borders](https://cses.fi/problemset/task/1732) 191 | - [ ] - [Finding Periods](https://cses.fi/problemset/task/1733) 192 | - [ ] - [Minimal Rotation](https://cses.fi/problemset/task/1110) 193 | - [ ] - [Longest Palindrome](https://cses.fi/problemset/task/1111) 194 | - [ ] - [Required Substring](https://cses.fi/problemset/task/1112) 195 |

196 |
197 |
Additional Problems 198 |

199 | 200 | - [ ] - [Shortest Subsequence](https://cses.fi/problemset/task/1087) 201 | - [ ] - [Counting Bits](https://cses.fi/problemset/task/1146) 202 | - [ ] - [Swap Game](https://cses.fi/problemset/task/1670) 203 | - [ ] - [Meet in the Middle](https://cses.fi/problemset/task/1628) 204 | - [ ] - [Prüfer Code](https://cses.fi/problemset/task/1134) 205 | - [ ] - [Edge Directions](https://cses.fi/problemset/task/1756) 206 | - [ ] - [Advertisement](https://cses.fi/problemset/task/1142) 207 | - [ ] - [Elevator Rides](https://cses.fi/problemset/task/1653) 208 | - [ ] - [Maximum Xor Subarray](https://cses.fi/problemset/task/1655) 209 | - [ ] - [Movie Festival Queries](https://cses.fi/problemset/task/1664) 210 | - [ ] - [Chess Tournament](https://cses.fi/problemset/task/1697) 211 | - [ ] - [Tree Traversals](https://cses.fi/problemset/task/1702) 212 | - [ ] - [Network Renovation](https://cses.fi/problemset/task/1704) 213 | - [ ] - [Graph Girth](https://cses.fi/problemset/task/1707) 214 | - [ ] - [Intersection Points](https://cses.fi/problemset/task/1740) 215 | - [ ] - [String Reorder](https://cses.fi/problemset/task/1743) 216 | - [ ] - [Pyramid Array](https://cses.fi/problemset/task/1747) 217 | - [ ] - [Increasing Subsequence II](https://cses.fi/problemset/task/1748) 218 | - [ ] - [String Removals](https://cses.fi/problemset/task/1149) 219 | - [ ] - [Bit Inversions](https://cses.fi/problemset/task/1188) 220 | - [ ] - [Writing Numbers](https://cses.fi/problemset/task/1086) 221 | - [ ] - [String Transform](https://cses.fi/problemset/task/1113) 222 | - [ ] - [Maximum Building I](https://cses.fi/problemset/task/1147) 223 | - [ ] - [Sorting Methods](https://cses.fi/problemset/task/1162) 224 | - [ ] - [Cyclic Array](https://cses.fi/problemset/task/1191) 225 | - [ ] - [Food Division](https://cses.fi/problemset/task/1189) 226 | - [ ] - [Bit Problem](https://cses.fi/problemset/task/1654) 227 | - [ ] - [Swap Round Sorting](https://cses.fi/problemset/task/1698) 228 | - [ ] - [Tree Isomorphism I](https://cses.fi/problemset/task/1700) 229 | - [ ] - [Critical Cities](https://cses.fi/problemset/task/1703) 230 | - [ ] - [School Excursion](https://cses.fi/problemset/task/1706) 231 | - [ ] - [Coin Grid](https://cses.fi/problemset/task/1709) 232 | - [ ] - [Robot Path](https://cses.fi/problemset/task/1742) 233 | - [ ] - [Course Schedule II](https://cses.fi/problemset/task/1757) 234 | - [ ] - [Empty String](https://cses.fi/problemset/task/1080) 235 | - [ ] - [Grid Paths](https://cses.fi/problemset/task/1078) 236 | - [ ] - [Book Shop II](https://cses.fi/problemset/task/1159) 237 | - [ ] - [Network Breakdown](https://cses.fi/problemset/task/1677) 238 | - [ ] - [Visiting Cities](https://cses.fi/problemset/task/1203) 239 | - [ ] - [Number Grid](https://cses.fi/problemset/task/1157) 240 | - [ ] - [Maximum Building II](https://cses.fi/problemset/task/1148) 241 | - [ ] - [Stick Divisions](https://cses.fi/problemset/task/1161) 242 | - [ ] - [Coding Company](https://cses.fi/problemset/task/1665) 243 | - [ ] - [Flight Route Requests](https://cses.fi/problemset/task/1699) 244 | - [ ] - [Tree Isomorphism II](https://cses.fi/problemset/task/1701) 245 | - [ ] - [Forbidden Cities](https://cses.fi/problemset/task/1705) 246 | - [ ] - [Area of Rectangles](https://cses.fi/problemset/task/1741) 247 | - [ ] - [Creating Offices](https://cses.fi/problemset/task/1752) 248 | - [ ] - [Permutations II](https://cses.fi/problemset/task/1075) 249 | - [ ] - [New Flight Routes](https://cses.fi/problemset/task/1685) 250 |

251 |
252 | 253 | ## Important Note 254 | 255 | Everyone is requested to either comment their code properly or use appropriate variable and function names throughout the code. 256 | 257 | ### What's this repo about? 258 | 259 | This repo is created to house the solutions and editorials of problems of CSES at one place. CSES contains 200 problems from beginner to advanced levels. 260 | 261 | ### What is CSES? 262 | The CSES Problem Set contains a collection of competitive programming practice problems. You can access the problems [here](https://cses.fi/problemset/). 263 | 264 | ### How to contribute? 265 | 266 | Read the **Contributing.md** file for this. All the contributions should follow the guidelines. 267 | 268 | ### Language for contribution 269 | 270 | Contributions are accepted only in the following languages: 271 | * Assembly 272 | * C++ 273 | * Haskell 274 | * Java 275 | * Node.js 276 | * Pascal 277 | * Python2 278 | * Python3 279 | * Ruby 280 | * Rust 281 | * Scala 282 | 283 | ### Learning Resources 284 | 285 | If you are new to Open source or github. You can follow this [wonderful guide](https://www.digitalocean.com/community/tutorial_series/an-introduction-to-open-source) to learn more about it. 286 | 287 | _Keep Learning with Open Source_ 288 | --------------------------------------------------------------------------------