├── C Programs ├── C Programs - 1 │ ├── Ashu and Prime Factors.cpp │ ├── Can You Guess.cpp │ ├── Choosing_the_Judges.c │ ├── Closest_Gift.c │ ├── Dhoni_and_Holiday.c │ ├── Dorsplen.cpp │ ├── Help Oz.cpp │ ├── In Love With Primes Alternate Solutions.cpp │ ├── In Love with Primes.cpp │ ├── Lonely_Integer.c │ ├── Love Triangle.c │ ├── Micro and Prime Prime.cpp │ ├── Mogu_Loves_Numbers.c │ ├── Monk at the Graph Factory.cpp │ ├── Monk in the Real Estate.cpp │ ├── Monk_Takes_a_Walk.c │ ├── My Girlfriend and Her Love for Cats.cpp │ ├── Mystery.cpp │ ├── Prime Interval.cpp │ ├── Roy and Shopping.cpp │ ├── Ryotsu Loves Primes.cpp │ ├── Sum of Primes.cpp │ ├── The_Football_Fest.c │ ├── Wacky_Workout.c │ └── Xsquare_and_Coin_Collection.c ├── C Programs - 2 │ ├── ASCII_value.c │ ├── Alice and Bob Play.cpp │ ├── Calculate_the_Power.c │ ├── Confused_Monk.c │ ├── Fredo and Array Update.cpp │ ├── Gas_Stations.c │ ├── Intelligent_Girl.c │ ├── Jarvis_and_Lone_Integer.c │ ├── Kiljil and Sorted Array.cpp │ ├── Let's Begin.cpp │ ├── Little Shino and Common Factors.cpp │ ├── Magical Words.cpp │ ├── Maximise GCD.cpp │ ├── Maximise Sum.cpp │ ├── Monk_Box_Cookies.c │ ├── Monk_and_his_Friend.c │ ├── Once Upon a Time in Time-Land.cpp │ ├── Palindrome Count.cpp │ ├── Panda and Chain Reaction.cpp │ ├── Saraff's New Shop.cpp │ ├── Sherlock_and_XOR.c │ ├── The Prime Cells.cpp │ ├── Tic Tac Toe.cpp │ ├── Tree of Many Leaves.cpp │ └── Xsquare And Two Arrays.cpp ├── C Programs - 3 │ ├── 2 vs 3.cpp │ ├── Archery.cpp │ ├── Binary Queries.cpp │ ├── Bricks and Building.cpp │ ├── Calculate Sum.cpp │ ├── Can you answer these queries.cpp │ ├── Coin Game.cpp │ ├── Contribution from Divisors.cpp │ ├── Counting Number of Ways.cpp │ ├── Fredo and Sums.cpp │ ├── Game of Coins.cpp │ ├── Kya Ye wala Ho Payega.cpp │ ├── Largest Sub String.cpp │ ├── Mancunian and Factorisation Game.cpp │ ├── Micro and Array Update.cpp │ ├── Naruto and his New Jutsu.cpp │ ├── Prime Numbers Again.cpp │ ├── Reduce the Fraction.cpp │ ├── Rhezo and Big Powers.cpp │ ├── Riya's Birthday Party.cpp │ ├── Roy and Ropes.cpp │ ├── Selection of Cities.cpp │ ├── Smith Numbers.cpp │ ├── Sumit's Sub Array and Sub Sequence.cpp │ └── Supreme Subset.cpp └── C Programs - 4 │ ├── Balanced Partition.cpp │ ├── Cheapest Subarray.cpp │ ├── Help Mona.cpp │ ├── Holi and Colorful Houses.cpp │ ├── Kingdom of Dreams.cpp │ ├── Modified Subarrays.cpp │ ├── Operate Once.cpp │ ├── Pandya Swag.cpp │ ├── Phineas and Ferb.cpp │ ├── Points on a Rectangle.cpp │ ├── Remove Friends.cpp │ ├── Shuffle or Not.cpp │ └── Simple Queries.cpp ├── Contests ├── DSA Coding Contest │ └── 2023 October │ │ └── Programs │ │ └── Tree Operations.cpp ├── Data Structures and Algorithm Coding Challenge 2 │ ├── Explanations │ │ └── Divisorless Groups Explanation.txt │ └── Programs │ │ └── Divisorless Groups.cpp ├── Data Structures and Algorithms Coding Challenge 4 │ ├── Explanations │ │ ├── Knapsacks Explanation.txt │ │ └── Number of Overtakes Explanation.txt │ └── Programs │ │ ├── Knapsacks.cpp │ │ └── Number of Overtakes.cpp ├── Data Structures and Algorithms Coding Contest 3 │ ├── Explanations │ │ └── Count of Integers Explanation.txt │ ├── Programs │ │ └── Count of Integers.cpp │ └── Rough Notes Link ├── December Easy 2019 │ ├── Explanation │ │ ├── Destination Cost Explanation.txt │ │ ├── Digit Strings Explanation.txt │ │ ├── Fitting Circles Explanation.txt │ │ ├── Paint Walls Explanation.txt │ │ └── Trees in a Restaurant Explanation.txt │ └── Programs │ │ ├── Destination Cost.cpp │ │ ├── Digit Strings.cpp │ │ ├── Fitting Circles.cpp │ │ ├── Paint Walls.cpp │ │ └── Treat in a Restaurant.cpp ├── December Easy 2020 │ ├── Explanations │ │ ├── Exponential Subsets Explanation.txt │ │ └── Non Great Equations Explanation.txt │ └── Programs │ │ ├── Exponential Subsets.cpp │ │ └── Non Great Equations.cpp ├── December Easy 2021 │ ├── Explanations │ │ ├── Bob's Confusion Explanation.txt │ │ ├── Manhattan Distance Explanation.txt │ │ ├── Maximum MOD Explanation.txt │ │ ├── Rooted Tree Explanation.txt │ │ ├── Sum as Frequency Explanation.txt │ │ └── Three Rectangles Explanation.txt │ └── Programs │ │ ├── Bob's Confusion.cpp │ │ ├── Manhattan Distance.cpp │ │ ├── Maximum MOD.cpp │ │ ├── Rooted Tree.cpp │ │ ├── Sum as per Frequency.cpp │ │ └── Three Rectangles.cpp ├── December Easy 2022 │ ├── Explanations │ │ ├── ASME to SAME Explanation.txt │ │ ├── Fruit Smash Explanation.txt │ │ ├── Product Game Explanation.txt │ │ ├── Shift String Explanation.txt │ │ └── Travelling Alex Explanation.txt │ └── Programs │ │ ├── ASME to SAME.cpp │ │ ├── Fruit Smash.cpp │ │ ├── Product Game.cpp │ │ ├── Shift String.cpp │ │ └── Travelling Alex.cpp ├── Easy Series │ └── 2024 │ │ └── 01 January Easy 2024 │ │ └── Programs │ │ ├── Hamming Sort.java │ │ └── Xorzilla.cpp ├── February Easy 2019 │ ├── Explanations │ │ ├── Array Game Explanation.txt │ │ ├── Colourful Tree Explanation.txt │ │ ├── Development Cost Explanation.txt │ │ ├── Flipping Brackets Explanation.txt │ │ ├── Good Subsequences Explanation.txt │ │ └── Pile of Coins Explanation.txt │ └── Programs │ │ ├── Array Game.cpp │ │ ├── Colourful Tree.cpp │ │ ├── Development Cost.cpp │ │ ├── Flipping Brackets.cpp │ │ ├── Good Subsequences.cpp │ │ └── Pile of Coins.cpp ├── January Easy 2018 │ ├── Explanations │ │ ├── Shubham and Subarray XOR Explanation.txt │ │ └── Shubham and XOR Explanation.txt │ └── Programs │ │ ├── Shubham and Subarray XOR.cpp │ │ └── Shubham and XOR .cpp ├── January Easy 2020 │ └── Programs │ │ └── Maximum Bit Shifts.cpp ├── July Easy 2019 │ └── Rough Notes Link ├── July Easy 2023 │ └── Programs │ │ └── Tom and Jerry.cpp ├── June Easy 2018 │ ├── Explanation │ │ ├── Balanced Partition Explanation.txt │ │ ├── Cheapest Subarray Explanation.txt │ │ ├── Heat Transfer Explanation.txt │ │ └── Mosquito Mesh Explanation.txt │ └── Programs │ │ ├── Balanced Partition.cpp │ │ ├── Cheapest Subarray.cpp │ │ ├── Heat Transfer.cpp │ │ └── Mosquito Mesh.cpp ├── June Easy 2019 │ ├── Explanations │ │ ├── Cost of Balloons Explanation.txt │ │ ├── Median Game Explanation.txt │ │ └── Minimum Cost Explanation.txt │ ├── Programs │ │ ├── Cost of Balloons.cpp │ │ ├── Median Game.cpp │ │ └── Minimum Cost.cpp │ └── Rough Notes Link ├── June Easy 2021 │ ├── Explanations │ │ ├── A Sports Fest Explanation.txt │ │ ├── A Website Explanation.txt │ │ ├── Cheapest Items Explanation.txt │ │ ├── Multiple of 3 Explanation.txt │ │ └── XOR Subsequences Explanation.txt │ └── Programs │ │ ├── A Sports Fest.cpp │ │ ├── A Website.cpp │ │ ├── Cheapest Items.cpp │ │ ├── Multiple of 3.cpp │ │ └── XOR Subsequences.cpp ├── June Easy 2023 │ ├── Explanations │ │ ├── Alice and Apple Explanation.txt │ │ ├── Alice and GCD Explanation.txt │ │ ├── Good Sum Explanation.txt │ │ ├── Largest Number Explanation.txt │ │ ├── Reach the Queen Explanation.txt │ │ └── Too Chocolatey Explanation.txt │ └── Programs │ │ ├── Alice and Apple.cpp │ │ ├── Alice and GCD.cpp │ │ ├── Good Sum.cpp │ │ ├── Largest Number.cpp │ │ ├── Reach the Queen.cpp │ │ └── Too Chocolatey.cpp ├── March Easy 2019 │ ├── Explanations │ │ ├── Lost Numbers Explanation.txt │ │ ├── Operations on an Array Explanation.txt │ │ └── Palindromes Everywhere Explanation.txt │ └── Programs │ │ ├── Lost Numbers.cpp │ │ ├── Operations on an Array.cpp │ │ └── Palindromes Everywhere.cpp ├── March Easy 2023 │ ├── Explanations │ │ ├── Bob and Cakes Explanation.txt │ │ ├── Potential Tree Explanation.txt │ │ ├── Same AND OR Explanation.txt │ │ └── Segment Tree Explanation.txt │ └── Programs │ │ ├── Bob and Cakes.cpp │ │ ├── Potential.cpp │ │ ├── Same AND OR.cpp │ │ └── Segment Tree.cpp ├── May Circuits 2023 │ ├── Explanations │ │ ├── AND Subsequence Explanation.txt │ │ ├── Compatibility Queries Explanation.txt │ │ ├── Count Tuples Explanation.txt │ │ ├── Operation X Explanation.txt │ │ ├── Prime Array Explanation.txt │ │ └── Station Pairs Explanation.txt │ └── Programs │ │ ├── AND Subsequence.cpp │ │ ├── Compatibility Queries.cpp │ │ ├── Count Tuples.cpp │ │ ├── Operation X.cbp │ │ ├── Prime Array.cpp │ │ └── Station Pairs.cpp ├── May Easy 2019 │ ├── Explanations │ │ ├── Close Subsequences Explanation.txt │ │ ├── Good Numbers Explanation.txt │ │ ├── Minimum Flip Explanation.txt │ │ └── XOR and Insert Explanation.txt │ └── Programs │ │ ├── Close Subsequences.cpp │ │ ├── Good Numbers.cpp │ │ ├── Minimum Flip.cpp │ │ └── XOR and Insert.cpp ├── May Easy 2023 │ ├── Explanations │ │ ├── Birthday Gift Explanation.txt │ │ ├── Edge Letters Explanation.txt │ │ ├── Favourite Singer Explanation.txt │ │ ├── Kth Prime Sum Explanation.txt │ │ ├── Swapping Pairs Explanation.txt │ │ └── Twin String Explanation.txt │ └── Programs │ │ ├── Birthday Gift.cpp │ │ ├── Edge Letters.cpp │ │ ├── Favourite Singer.cpp │ │ ├── Kth Prime Sum.cpp │ │ ├── Swapping Pairs.cpp │ │ └── Twin Strings.cpp ├── November Circuits 2022 │ ├── Explanations │ │ ├── Distribute the Numbers Explanation.txt │ │ ├── Four Distinct Factors Explanation.txt │ │ └── Max And Explanation.txt │ └── Programs │ │ ├── Distribute the Numbers.cpp │ │ ├── Fill Grid.cpp │ │ ├── Four Distinct Factors.cpp │ │ └── MAX AND.cpp ├── November Easy 2019 │ ├── Explanations │ │ ├── Determining Numbers Explanation.txt │ │ ├── Determining Numbers XOR Solution.txt │ │ ├── Exchanging Money Explanation.txt │ │ ├── Numbers in Range Explanation.txt │ │ ├── One and Only Flow Explanation.txt │ │ └── Zero Path Operations Explanation.txt │ └── Programs │ │ ├── Determining Numbers XOR Solution.cpp │ │ ├── Determining Numbers.cpp │ │ ├── Exchanging Money.cpp │ │ ├── Numbers in Range.cpp │ │ ├── One and Only Flow.cpp │ │ └── Zero Path Operations.cpp ├── October Easy 2019 │ ├── Explanations │ │ └── Finite or Infinite Explanation.txt │ └── Programs │ │ └── Finite or Infinite.cpp ├── September Easy 2018 │ └── Programs │ │ └── Pair Recovery.cpp ├── Sprint Challenge 2 - NIT Jalandhar │ ├── Explanation │ │ ├── Calculate Sum Explanation.txt │ │ ├── Kingdom of Dreams Explanation.txt │ │ ├── Largest Sub String Explanation.txt │ │ ├── Modified Subarrays Explanation.txt │ │ ├── Operate Once Explanation.txt │ │ ├── Points on a Rectangle Explanation.txt │ │ ├── Reduce the Fraction Explanation.txt │ │ └── Simple Queries Explanation.txt │ └── Programs │ │ ├── Calculate Sum.cpp │ │ ├── Kingdom of Dreams.cpp │ │ ├── Largest Sub String.cpp │ │ ├── Modified Subarrays.cpp │ │ ├── Operate Once.cpp │ │ ├── Points on a Rectangle.cpp │ │ ├── Reduce the Fraction.cpp │ │ └── Simple Queries.cpp └── TPC v2.0 Online Round │ ├── Explanations │ ├── Bee Explanation.txt │ ├── Corona Virus Explanation.txt │ ├── Dr Alice and Ants Explanation.txt │ ├── Minimum Table Size Explanation.txt │ ├── Sum of Powers of Three Explanation.txt │ └── Two Arrays and K Elements Explanation.txt │ └── Programs │ ├── Bee.cpp │ ├── Corona Virus.cpp │ ├── Dr Alice and Ants.cpp │ ├── Minimum Table Size.cpp │ ├── Sum of Powers of Three.cpp │ └── Two Arrays and K Elements.cpp ├── Explanations ├── Explanations - 1 │ ├── Ashu and Prime Factors Explanation.txt │ ├── Can You Guess Explanation.txt │ ├── Choosing the Judges - Explanation.txt │ ├── Closest Gift - Explanation.txt │ ├── Dhoni and Holiday - Explanation.txt │ ├── Dorsplen - Explanation.txt │ ├── Help Oz Explanation.txt │ ├── In Love with Primes Alternate Solutions Explanation.txt │ ├── In Love with Primes Explanation.txt │ ├── Lonely Integer - Explanation.txt │ ├── Love Triangle Explanation.txt │ ├── Micro and Prime Prime Explanation.txt │ ├── Mogu Loves Numbers - Explanation.txt │ ├── Monk Takes a Walk - Explanation.txt │ ├── Monk at the Graph Factory - Explanation.txt │ ├── Monk in the Real Estate - Explanation.txt │ ├── My Girlfriend and Her Love for Cats Explanation.txt │ ├── Mystery Explanation.txt │ ├── Prime Interval Explanation.txt │ ├── Roy and Shopping Explanation.txt │ ├── Ryotsu Loves Primes Explanation.txt │ ├── Sum of Primes Explanation.txt │ ├── The Football Fest - Explanation.txt │ ├── Wacky Workout - Explanation.txt │ └── Xsquare and Coin Collection - Explanation.txt ├── Explanations - 2 │ ├── ASCII value - Explanation.txt │ ├── Alice and Bob Play Explanation.txt │ ├── Calculate the Power - Explanation.txt │ ├── Confused Monk - Explanation.txt │ ├── Fredo and Array Update Explanation.txt │ ├── Gas Stations - Explanation.txt │ ├── Intelligent Girl - Explanation.txt │ ├── Jarvis and Lone Integer - Explanation.txt │ ├── Kiljil and Sorted Array Explanation.txt │ ├── Let's Begin Explanation.txt │ ├── Little Shino and Common Factors Explanation.txt │ ├── Maximise GCD Explanation.txt │ ├── Maximise Sum Explanation.txt │ ├── Monk and Box of Cookies - Explanation.txt │ ├── Monk and his Friend - Explanation.txt │ ├── Once Upon a Time in Time-Land Explanation.txt │ ├── Palindrome Count Explanation.txt │ ├── Panda and Chain Reaction Explanation.txt │ ├── Saraff's New Shop Explanation.txt │ ├── Sherlock and XOR - Explanation.txt │ ├── The Prime Cells Explanation.txt │ ├── Tic Tac Toe Explanation.txt │ ├── Tree of Many Leaves Explanation.txt │ └── Xsquare and Two Arrays Explanation.txt ├── Explanations - 3 │ ├── 2 vs 3 Explanation.txt │ ├── Archery Explanation.txt │ ├── Binary Queries Explanation.txt │ ├── Bricks and Building Explanation.txt │ ├── Calculate Sum Explanation.txt │ ├── Can you answer these queries Explanation.txt │ ├── Coin Game Explanation.txt │ ├── Contribution from Divisors Explanation.txt │ ├── Counting Number of Ways Explanation.txt │ ├── Fredo and Sums.txt │ ├── Game of Coins Explanation.txt │ ├── Kya Ye Wala Ho Payega Explanation.txt │ ├── Largest Sub String Explanation.txt │ ├── Mancunian and Factorisation Game Explanation.txt │ ├── Micro and Array Update Explanation.txt │ ├── Naruto and his New Jutsu Explanation.txt │ ├── Prime Numbers Again Explanation.txt │ ├── Reduce the Fraction Explanation.txt │ ├── Rhezo and Big Powers Explanation.txt │ ├── Riya's Birthday Party Explanation.txt │ ├── Roy and Ropes Explanation.txt │ ├── Selection of Cities Explanation.txt │ ├── Smith Numbers Explanation.txt │ ├── Sumit's Sub Array and Sub Sequence Explanation.txt │ └── Supreme Subset Explanation.txt └── Explanations - 4 │ ├── Balanced Partition Explanation.txt │ ├── Cheapest Subarry Explanation.txt │ ├── Help Mona Explanation.txt │ ├── Holi and Colourful Houses Explanation.txt │ ├── Kingdom of Dreams Explanation.txt │ ├── Modified Subarrays Explanation.txt │ ├── Operate Once Explanation.txt │ ├── Pandya Swag Explanation.txt │ ├── Phineas and Ferb Explanation.txt │ ├── Points on a Rectangle Explanation.txt │ ├── Remove Friends Explanation.txt │ ├── Shuffle or Not Explanation.txt │ └── Simple Queries Explanation.txt └── README.md /C Programs/C Programs - 1/Ashu and Prime Factors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define UNMARKED 0 5 | 6 | using namespace std; 7 | 8 | vector no_of_numbers_with_least_prime_factor(1e6 + 1, 0); 9 | 10 | void precompute_prime_factors() 11 | { 12 | vector least_prime_factor(1e6 + 1, UNMARKED); 13 | 14 | for(int i = 2; i <= 1e6; i++) 15 | { 16 | if(least_prime_factor[i] == UNMARKED) 17 | { 18 | 19 | for(int j = i ; j <= 1e6; j += i) 20 | { 21 | if(least_prime_factor[j] == UNMARKED) 22 | { 23 | least_prime_factor[j] = i; 24 | no_of_numbers_with_least_prime_factor[i]++; 25 | } 26 | } 27 | } 28 | } 29 | 30 | } 31 | 32 | int main() 33 | { 34 | int no_of_test_cases; 35 | scanf("%d", &no_of_test_cases); 36 | 37 | precompute_prime_factors(); 38 | 39 | while(no_of_test_cases--) 40 | { 41 | int query; 42 | scanf("%d", &query); 43 | 44 | printf("%d\n", no_of_numbers_with_least_prime_factor[query]); 45 | } 46 | 47 | return 0; 48 | } 49 | 50 | -------------------------------------------------------------------------------- /C Programs/C Programs - 1/Can You Guess.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void solve() 5 | { 6 | int number, sum = 1; 7 | cin >> number; 8 | 9 | if(number == 1) 10 | sum = 0; 11 | 12 | for(int divisor = 2; divisor*divisor <= number; divisor++) 13 | if(number%divisor == 0) 14 | sum += (divisor*divisor == number ? divisor : divisor + number/divisor); 15 | 16 | cout << sum << "\n"; 17 | } 18 | int main() 19 | { 20 | int no_of_test_cases; 21 | cin >> no_of_test_cases; 22 | 23 | while(no_of_test_cases--) 24 | solve(); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /C Programs/C Programs - 1/Closest_Gift.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define true 1 4 | #define false 0 5 | 6 | int isPrime(int n) 7 | { 8 | int i; 9 | 10 | if(n < 2) 11 | return false; 12 | 13 | for(i = 2; i*i <= n; i++) 14 | if(n%i == 0) 15 | return false; 16 | 17 | return true; 18 | } 19 | 20 | int main() 21 | { 22 | int number, distance; 23 | scanf("%d", &number); 24 | 25 | for(distance = 0; ; distance++) 26 | { 27 | if(isPrime(number + distance)) 28 | break; 29 | } 30 | 31 | printf("%d\n",distance); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /C Programs/C Programs - 1/Dorsplen.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | long long red, green, blue, max_colour, min_colour, mid_colour, no_of_artifacts; 8 | cin >> red >> green >> blue; 9 | 10 | min_colour = min(red, min(green, blue)); 11 | 12 | red -= min_colour; 13 | green -= min_colour; 14 | blue -= min_colour; 15 | 16 | no_of_artifacts = min_colour + (red + green + +blue)/2 + (red + green + +blue)%2; 17 | 18 | cout << no_of_artifacts << endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /C Programs/C Programs - 1/In Love With Primes Alternate Solutions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int no_of_cakes; 6 | scanf("%d", &no_of_cakes); 7 | 8 | while(no_of_cakes--) 9 | { 10 | int weight; 11 | scanf("%d", &weight); 12 | 13 | printf(weight == 2 ? "Arjit\n": "Deepa\n"); 14 | } 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /C Programs/C Programs - 1/In Love with Primes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | vector is_prime(1e5 + 1, true); 7 | vector prime; 8 | 9 | void precompute_primes() 10 | { 11 | is_prime[0] = is_prime[1] = false; 12 | 13 | for(int i = 2; i < 1e5; i++) 14 | { 15 | if(is_prime[i]) 16 | { 17 | for(int multiple = 2*i; multiple < 1e5; multiple +=i) 18 | { 19 | is_prime[multiple] = false; 20 | } 21 | prime.push_back(i); 22 | } 23 | } 24 | } 25 | 26 | void solve() 27 | { 28 | bool two_prime_sum_possible = false; 29 | int weight; 30 | scanf("%d", &weight); 31 | 32 | for(int i = 0; prime[i] < weight; i++) 33 | { 34 | if(is_prime[weight - prime[i]]) 35 | { 36 | two_prime_sum_possible = true; 37 | break; 38 | } 39 | } 40 | printf(two_prime_sum_possible ? "Deepa\n" : "Arjit\n"); 41 | } 42 | 43 | int main() 44 | { 45 | int no_of_cakes; 46 | scanf("%d", &no_of_cakes); 47 | 48 | precompute_primes(); 49 | 50 | while(no_of_cakes--) 51 | solve(); 52 | 53 | return 0; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /C Programs/C Programs - 1/Lonely_Integer.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int i, current_number, bitwise_xor_all_numbers = 0, no_of_elements; 6 | scanf("%d",&no_of_elements); 7 | 8 | for(i = 1; i <= no_of_elements; i++) 9 | { 10 | scanf("%d",¤t_number); 11 | bitwise_xor_all_numbers = bitwise_xor_all_numbers^current_number; 12 | } 13 | 14 | printf("%d\n",bitwise_xor_all_numbers); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /C Programs/C Programs - 1/Love Triangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long base_9(long long num) 4 | { 5 | if(num < 9) 6 | return num; 7 | else 8 | return num%9 + 10*base_9(num/9);//Multiply by 10 to shift significant digits 9 | } 10 | 11 | int main() 12 | { 13 | long long number; 14 | 15 | while(scanf("%lld", &number) != EOF) 16 | { 17 | printf("%lld\n", base_9(number)); 18 | } 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /C Programs/C Programs - 1/Micro and Prime Prime.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | vector no_of_prime_primes_till(1e6 + 1, 0); 7 | 8 | void precompute_primes() 9 | { 10 | vector is_prime(1e6 + 1, true); 11 | vector no_of_primes_till(1e6 + 1, 0); 12 | 13 | is_prime[0] = is_prime[1] = false; 14 | for(int i = 2; i <= 1e6; i++) 15 | { 16 | if(is_prime[i]) 17 | { 18 | for(int multiple = 2*i; multiple <= 1e6; multiple +=i) 19 | { 20 | is_prime[multiple] = false; 21 | } 22 | } 23 | } 24 | 25 | for(int i = 2; i <= 1e6; i++) 26 | { 27 | no_of_primes_till[i] = no_of_primes_till[i - 1] + (is_prime[i] == true); 28 | 29 | no_of_prime_primes_till[i] = no_of_prime_primes_till[i - 1] + (is_prime[no_of_primes_till[i] ] == true); 30 | 31 | } 32 | } 33 | 34 | int main() 35 | { 36 | int no_of_test_cases; 37 | scanf("%d", &no_of_test_cases); 38 | 39 | precompute_primes(); 40 | 41 | while(no_of_test_cases--) 42 | { 43 | int left, right; 44 | scanf("%d %d", &left, &right); 45 | printf("%d\n", (no_of_prime_primes_till[right] - no_of_prime_primes_till[left - 1]) ); 46 | } 47 | 48 | return 0; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /C Programs/C Programs - 1/Mogu_Loves_Numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define swap(a, b) {a = a^b; b = b^a; a = a^b;} 4 | #define SIZE 1000000 5 | int is_Prime[SIZE + 1], sum_of_primes_till[SIZE + 1]; 6 | 7 | int main() 8 | { 9 | int no_of_queries, left, right, i, multiple; 10 | 11 | for(i = 0; i <= SIZE; is_Prime[i++] = 1); 12 | 13 | //Sieve of Erathosthenes 14 | is_Prime[0] = is_Prime[1] = 0; 15 | for(i = 2; i*i <= SIZE; i++) 16 | if(is_Prime[i] == 1) 17 | for(multiple = 2; i*multiple <= SIZE; multiple++) 18 | is_Prime[i*multiple] = 0; 19 | 20 | 21 | sum_of_primes_till[0] = 0; 22 | for(i = 1; i <= SIZE; i++) 23 | sum_of_primes_till[i] = is_Prime[i] + sum_of_primes_till[i-1]; 24 | 25 | scanf("%d", &no_of_queries); 26 | 27 | while(no_of_queries-- != 0) 28 | { 29 | scanf("%d %d",&left, &right); 30 | 31 | if(left > right) 32 | swap(left, right); 33 | 34 | printf("%d\n", sum_of_primes_till[right] - sum_of_primes_till[left - 1]); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /C Programs/C Programs - 1/Monk at the Graph Factory.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int no_of_vertices, no_of_edges = 0; 7 | cin >> no_of_vertices; 8 | 9 | for(int i = 1; i <= no_of_vertices; i++) 10 | { 11 | int degree_i; 12 | cin >> degree_i; 13 | 14 | no_of_edges += degree_i; 15 | } 16 | 17 | cout << (no_of_edges == 2*(no_of_vertices - 1) ? "Yes\n" : "No\n"); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /C Programs/C Programs - 1/Monk in the Real Estate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX 1e4 + 1 5 | using namespace std; 6 | 7 | void solve() 8 | { 9 | int no_of_roads, city_x, city_y, no_of_cities = 0; 10 | vector visited(MAX, false); 11 | scanf("%d", &no_of_roads); 12 | 13 | for(int i = 1; i <= no_of_roads; i++) 14 | { 15 | scanf("%d %d", &city_x, &city_y); 16 | visited[city_x] = visited[city_y] = true; 17 | } 18 | 19 | for(int i = 1; i < MAX; i++) 20 | if(visited[i] == true) 21 | no_of_cities++; 22 | 23 | printf("%d\n", no_of_cities); 24 | } 25 | 26 | int main() 27 | { 28 | int no_of_test_cases; 29 | scanf("%d", &no_of_test_cases); 30 | 31 | while(no_of_test_cases--) 32 | solve(); 33 | 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /C Programs/C Programs - 1/Monk_Takes_a_Walk.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX_LENGTH 100000 3 | #define true 1 4 | #define false 0 5 | 6 | int isVowel(char ch) 7 | { 8 | switch(ch) 9 | { 10 | case 'A' : case 'a' : 11 | case 'E' : case 'e' : 12 | case 'I' : case 'i' : 13 | case 'O' : case 'o' : 14 | case 'U' : case 'u' : return true; 15 | } 16 | return false; 17 | } 18 | 19 | void solve() 20 | { 21 | int bad_tree_count = 0, i; 22 | char garden[MAX_LENGTH + 1]; 23 | scanf("%s", garden); 24 | 25 | for(i = 0; garden[i] != '\0'; i++) 26 | { 27 | if(isVowel(garden[i])) 28 | { 29 | bad_tree_count++; 30 | } 31 | } 32 | 33 | printf("%d\n", bad_tree_count); 34 | } 35 | int main() 36 | { 37 | int no_of_test_cases; 38 | scanf("%d", &no_of_test_cases); 39 | 40 | while(no_of_test_cases-- != 0) 41 | { 42 | solve(); 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /C Programs/C Programs - 1/My Girlfriend and Her Love for Cats.cpp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /C Programs/C Programs - 1/Mystery.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void solve() 6 | { 7 | int number, number_of_divisors = 0; 8 | cin >> number; 9 | 10 | for(int divisor = 1; divisor*divisor <= number; divisor++) 11 | if(number%divisor == 0) 12 | number_of_divisors += (divisor*divisor == number ? 1 : 2); 13 | 14 | cout << number_of_divisors << "\n"; 15 | } 16 | 17 | int main() 18 | { 19 | int no_of_test_cases; 20 | cin >> no_of_test_cases; 21 | 22 | while(no_of_test_cases--) 23 | solve(); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /C Programs/C Programs - 1/Prime Interval.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | vector prime; 7 | 8 | void precompute_primes() 9 | { 10 | vector is_prime(1e4 + 11, true); 11 | 12 | for(int i = 2; i <= 1e4 + 10; i++) 13 | { 14 | if(is_prime[i]) 15 | { 16 | for(int multiple = 2*i; multiple <= 1e4 + 10; multiple +=i) 17 | { 18 | is_prime[multiple] = false; 19 | } 20 | prime.push_back(i); 21 | } 22 | 23 | } 24 | } 25 | 26 | int main() 27 | { 28 | int left, right; 29 | scanf("%d %d", &left, &right); 30 | 31 | precompute_primes(); 32 | 33 | int i = 0; 34 | while(prime[i] < left) 35 | i++; 36 | 37 | for(; prime[i] <= right; i++) 38 | { 39 | printf("%d ",prime[i]); 40 | } 41 | return 0; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /C Programs/C Programs - 1/Roy and Shopping.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define UNMARKED 0 5 | using namespace std; 6 | 7 | vector least_prime_factor(1000000 + 1, UNMARKED); 8 | 9 | void precompute_prime_factors() 10 | { 11 | for(int i = 2; i <= 1e6; i++) 12 | { 13 | if(least_prime_factor[i] == UNMARKED) //It is unmarked 14 | { 15 | for(int j = i; j <= 1e6; j += i) 16 | { 17 | if(least_prime_factor[j] == UNMARKED) 18 | least_prime_factor[j] = i; 19 | } 20 | } 21 | } 22 | } 23 | 24 | int main() 25 | { 26 | int no_of_products; 27 | cin >> no_of_products; 28 | 29 | precompute_prime_factors(); 30 | 31 | while(no_of_products--) 32 | { 33 | int price; 34 | cin >> price; 35 | 36 | cout << price - least_prime_factor[price] << "\n"; 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /C Programs/C Programs - 1/Ryotsu Loves Primes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | vector is_prime(1000000 + 1, true); 7 | 8 | void precompute_primes() 9 | { 10 | for(int i = 2; i <= 1000000; i++) 11 | { 12 | if(is_prime[i]) 13 | { 14 | for(int composite = 2*i; composite <= 1000000; composite += i) 15 | is_prime[composite] = false; 16 | } 17 | } 18 | } 19 | 20 | void solve() 21 | { 22 | int prime_sum = 0, no_of_elements, k, prime_count = 0; 23 | cin >> no_of_elements >> k; 24 | 25 | for(int i = 1 ; i <= no_of_elements; i++) 26 | { 27 | int element_i; 28 | cin >> element_i; 29 | 30 | if(is_prime[element_i] && element_i >= 2) 31 | prime_count++; 32 | 33 | 34 | if(prime_count == k) 35 | { 36 | prime_sum += element_i; 37 | prime_count = 0; 38 | } 39 | 40 | } 41 | 42 | cout << prime_sum << "\n"; 43 | } 44 | 45 | int main() 46 | { 47 | int no_of_test_cases; 48 | cin >> no_of_test_cases; 49 | 50 | precompute_primes(); 51 | 52 | while(no_of_test_cases--) 53 | solve(); 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /C Programs/C Programs - 1/Sum of Primes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | vector prime_sum(1e6 + 1, 0); 7 | 8 | void precompute_prime_sum() 9 | { 10 | vector is_prime(1e6 + 1, true); 11 | is_prime[0] = is_prime[1] = false; 12 | 13 | for(int i = 2; i <= 1e6; i++) 14 | { 15 | if(is_prime[i]) 16 | { 17 | for(int j = 2*i ; j <= 1e6; j += i) 18 | { 19 | is_prime[j] = false; 20 | 21 | } 22 | } 23 | } 24 | 25 | for(int i = 2; i <= 1e6; i++) 26 | { 27 | prime_sum[i] = prime_sum[i - 1] + (is_prime[i] ? i : 0); 28 | } 29 | 30 | } 31 | 32 | int main() 33 | { 34 | int no_of_test_cases; 35 | scanf("%d", &no_of_test_cases); 36 | 37 | precompute_prime_sum(); 38 | 39 | while(no_of_test_cases--) 40 | { 41 | int left, right; 42 | scanf("%d %d", &left, &right); 43 | 44 | printf("%lld\n", prime_sum[right] - prime_sum[left - 1]); 45 | } 46 | 47 | return 0; 48 | } 49 | 50 | -------------------------------------------------------------------------------- /C Programs/C Programs - 1/The_Football_Fest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define swap(a,b)({a = a + b; b = a - b; a = a - b;}) 5 | unsigned int find_final_possesser(unsigned int, unsigned int); 6 | int main() 7 | { 8 | short no_of_test_cases, i; 9 | unsigned int no_of_passes, final_possesser, first_possesser; 10 | 11 | scanf("%hu",&no_of_test_cases); 12 | for(i = 1; i <= no_of_test_cases; i++) 13 | { 14 | scanf("%u %u",&no_of_passes, &first_possesser); 15 | final_possesser = find_final_possesser(no_of_passes, first_possesser); 16 | printf("Player %u\n",final_possesser); 17 | } 18 | return 0; 19 | } 20 | 21 | unsigned int find_final_possesser(unsigned int no_of_passes, unsigned int first_possesser) 22 | { 23 | char current_action; 24 | unsigned int previous_possesser, current_possesser; //This is a stack 25 | unsigned int pass_count; 26 | 27 | current_possesser = first_possesser; 28 | 29 | for(pass_count = 0; pass_count < no_of_passes; pass_count++) 30 | { 31 | scanf(" %c",¤t_action); 32 | 33 | if(current_action == 'P') 34 | { 35 | previous_possesser = current_possesser; 36 | scanf("%u",¤t_possesser); //Pushing an element onto the stack 37 | } 38 | else//Pass it back 39 | { 40 | swap(current_possesser, previous_possesser); 41 | } 42 | } 43 | 44 | return current_possesser; 45 | } 46 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/ASCII_value.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char ch; 6 | scanf("%c", &ch); 7 | printf("%d\n",ch); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/Alice and Bob Play.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void solve() 4 | { 5 | int no_of_rows, no_of_columns; 6 | scanf("%d %d", &no_of_rows, &no_of_columns); 7 | 8 | printf( no_of_rows%2 == 0 || no_of_columns%2 == 0 || (no_of_columns == 1 && no_of_rows > 1) ? "Bob\n" : "Alice\n"); 9 | } 10 | 11 | int main() 12 | { 13 | int no_of_test_cases; 14 | scanf("%d", &no_of_test_cases); 15 | 16 | while(no_of_test_cases--) 17 | solve(); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/Calculate_the_Power.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | long answer = 1L, base, exponent; 6 | const int MOD = 1e9 + 7; 7 | 8 | scanf("%ld %ld",&base, &exponent); 9 | 10 | for( ;exponent > 0; exponent = exponent >> 1) 11 | { 12 | if(exponent%2 == 1) 13 | answer = (answer*base)%MOD; 14 | 15 | base = (base*base)%MOD; 16 | } 17 | 18 | printf("%ld\n",answer); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/Fredo and Array Update.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int no_of_elements, element_i; 6 | scanf("%d", &no_of_elements); 7 | 8 | int sum = 0; 9 | for(int i = 1; i <= no_of_elements; i++) 10 | { 11 | scanf("%d", &element_i); 12 | sum += element_i; 13 | } 14 | 15 | int x = sum/no_of_elements + 1 ; 16 | 17 | printf("%d\n", x); 18 | 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/Gas_Stations.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int fuel_available, petrol_at_current_milestone, furthest_milestone_travelled, no_of_milestones, i; 6 | scanf("%d %d", &no_of_milestones , &fuel_available); 7 | 8 | furthest_milestone_travelled = no_of_milestones; 9 | for(i = 1; i <= no_of_milestones; i++) 10 | { 11 | scanf("%d", &petrol_at_current_milestone); 12 | fuel_available = fuel_available - petrol_at_current_milestone; 13 | if(fuel_available < 0) 14 | { 15 | furthest_milestone_travelled = i; 16 | break; 17 | } 18 | } 19 | 20 | printf("%d\n",furthest_milestone_travelled); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/Jarvis_and_Lone_Integer.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void solve() 4 | { 5 | int i, no_of_elements; 6 | long current_number, bitwise_xor_all_numbers = 0, answer; 7 | scanf("%d",&no_of_elements); 8 | 9 | for(i = 1; i <= no_of_elements; i++) 10 | { 11 | scanf("%ld",¤t_number); 12 | bitwise_xor_all_numbers = bitwise_xor_all_numbers^current_number; 13 | } 14 | 15 | answer = (bitwise_xor_all_numbers > 0 ? bitwise_xor_all_numbers : -1); 16 | printf("%ld\n",answer); 17 | } 18 | 19 | int main() 20 | { 21 | int no_of_test_cases; 22 | 23 | scanf("%d",&no_of_test_cases); 24 | 25 | while(no_of_test_cases-- != 0) 26 | { 27 | solve(); 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/Kiljil and Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int no_of_numbers, number_i; 8 | scanf("%d", &no_of_numbers); 9 | 10 | int max_so_far = 0; 11 | long long added_amount = 0; 12 | for(int i = 1; i <= no_of_numbers; i++) 13 | { 14 | scanf("%d", &number_i); 15 | 16 | if(number_i <= max_so_far) 17 | { 18 | added_amount += (max_so_far + 1) - number_i; 19 | } 20 | 21 | max_so_far = max(max_so_far+1, number_i); 22 | } 23 | printf("%lld\n", added_amount); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/Let's Begin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define min_4(a, b, c, d) min( (min(a, b)), (min(c, d)) ) 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | vector minimum_number_of_summands(1e6 + 1, 0); 10 | minimum_number_of_summands[0] = 0, minimum_number_of_summands[1] = -1; 11 | //Primes 12 | minimum_number_of_summands[2] = minimum_number_of_summands[3] = 1; 13 | minimum_number_of_summands[5] = minimum_number_of_summands[7] = 1; 14 | 15 | minimum_number_of_summands[6] = minimum_number_of_summands[4] = 2; 16 | minimum_number_of_summands[8] = 2; 17 | 18 | for(int i = 9; i <= 1e6; i++) 19 | { 20 | minimum_number_of_summands[i] =1 + min_4(minimum_number_of_summands[i - 2], minimum_number_of_summands[i - 3], 21 | minimum_number_of_summands[i - 5], minimum_number_of_summands[i -7]); 22 | } 23 | 24 | int no_of_test_cases; 25 | scanf("%d", &no_of_test_cases); 26 | 27 | while(no_of_test_cases--) 28 | { 29 | int number; 30 | scanf("%d", &number); 31 | 32 | printf("%d\n", minimum_number_of_summands[number]); 33 | } 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/Little Shino and Common Factors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | long long gcd(long long a, long long b) 6 | { 7 | if(b == 0) 8 | return a; 9 | else 10 | return gcd(a, a%b); 11 | } 12 | 13 | long long no_of_factors(long long n) 14 | { 15 | int no_of_factors = 0; 16 | for(long long i = 1; i*i <= n; i++) 17 | { 18 | if(n%i == 0) 19 | { 20 | no_of_factors += (i*i == n ? 1 : 2); 21 | } 22 | } 23 | return no_of_factors; 24 | } 25 | 26 | int main() 27 | { 28 | long long a, b; 29 | scanf("%lld %lld", &a, &b); 30 | 31 | printf("%lld\n", no_of_factors(gcd(max(a, b), min(a, b) ) ) ); 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/Magical Words.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define MAX_LENGTH 1000 + 2 6 | using namespace std; 7 | 8 | void solve() 9 | { 10 | char string[MAX_LENGTH]; 11 | scanf("%s", string); 12 | 13 | int string_length = strlen(string); 14 | typedef vector v_int; 15 | vector is_palindrome(string_length, v_int(string_length, false)); 16 | 17 | long long strength = 0; 18 | for(int i = 0; string[i] != '\0'; i++) 19 | { 20 | is_palindrome[i][i] = true; 21 | strength += 1*1; 22 | } 23 | 24 | for(int length = 2; length <= string_length; length++) 25 | { 26 | for(int start = 0, end = start + (length - 1); end < string_length; start++, end++) 27 | { 28 | if(string[start] == string[end]) 29 | { 30 | if(start + 1 == end || is_palindrome[start + 1][end - 1]) 31 | { 32 | is_palindrome[start][end] = true; 33 | strength += length*length; 34 | } 35 | } 36 | } 37 | } 38 | 39 | printf("%lld\n", strength); 40 | } 41 | 42 | int main() 43 | { 44 | int no_of_test_cases; 45 | scanf("%d", &no_of_test_cases); 46 | 47 | while(no_of_test_cases--) 48 | solve(); 49 | 50 | return 0; 51 | } 52 | 53 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/Maximise GCD.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int gcd(int a, int b) 6 | { 7 | if(a == 0 || b == 0) 8 | return (a + b); 9 | else 10 | return gcd(min(a, b), max(a, b)%min(a, b)); 11 | } 12 | 13 | int main() 14 | { 15 | int no_of_numbers; 16 | scanf("%d", &no_of_numbers); 17 | 18 | vector number(no_of_numbers + 1, 0); 19 | for(int i = 1; i <= no_of_numbers; i++) 20 | scanf("%d", &number[i]); 21 | 22 | int suffix_gcd = number[no_of_numbers], removable_prefix_length = 0; 23 | for(int i = no_of_numbers - 1; i >= 1; i--) 24 | { 25 | suffix_gcd = gcd(suffix_gcd, number[i]); 26 | 27 | if(suffix_gcd != number[no_of_numbers]) 28 | { 29 | removable_prefix_length = i; 30 | break; 31 | } 32 | } 33 | 34 | printf("%d\n", removable_prefix_length); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/Maximise Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int no_of_elements; 8 | scanf("%d", &no_of_elements); 9 | 10 | vector element(no_of_elements + 1); 11 | for(int i = 1; i <= no_of_elements; i++) 12 | scanf("%d", &element[i]); 13 | 14 | vector max_sum_till(no_of_elements + 1, 0); 15 | for(int i = 1; i <= no_of_elements; i++) 16 | { 17 | if(i == 1) 18 | max_sum_till[i] = element[i]; 19 | else if(i == 2) 20 | max_sum_till[i] = element[i]*1LL*element[i - 1]; 21 | else 22 | max_sum_till[i] = max(element[i] + max_sum_till[i - 1], element[i]*1LL*element[i - 1] + max_sum_till[i - 2]); 23 | } 24 | 25 | printf("%lld\n", max_sum_till[no_of_elements]); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/Monk_Box_Cookies.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void solve() 4 | { 5 | int no_of_balls, number, i; 6 | int no_of_bits[32] = {0}, current_bit_position, most_set_bit_position = 0; 7 | 8 | scanf("%d", &no_of_balls); 9 | 10 | for(i = 1; i <= no_of_balls; i++) 11 | { 12 | scanf("%d", &number); 13 | 14 | for(current_bit_position = 0; number != 0; current_bit_position++) 15 | { 16 | no_of_bits[current_bit_position] += (number & 1) ; 17 | number = number >> 1; 18 | } 19 | } 20 | 21 | for(i = 0; i < 32; i++) 22 | { 23 | most_set_bit_position = (no_of_bits[i] > no_of_bits[most_set_bit_position] ? i : most_set_bit_position); 24 | } 25 | 26 | printf("%d\n",most_set_bit_position); 27 | } 28 | 29 | int main() 30 | { 31 | int no_of_test_cases; 32 | 33 | 34 | scanf("%d", &no_of_test_cases); 35 | 36 | while(no_of_test_cases-- != 0) 37 | { 38 | solve(); 39 | } 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/Monk_and_his_Friend.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MASK_1 0x5555555555555555 3 | #define MASK_2 0x3333333333333333 4 | #define MASK_3 0x0F0F0F0F0F0F0F0F 5 | #define MASK_4 0x00FF00FF00FF00FF 6 | #define MASK_5 0x0000FFFF0000FFFF 7 | #define MASK_6 0x00000000FFFFFFFF 8 | 9 | int population_count(unsigned long long n) 10 | { 11 | n = (n&MASK_1) + ( (n >> 1) & MASK_1); 12 | n = (n&MASK_2) + ( (n >> 2) & MASK_2); 13 | n = (n&MASK_3) + ( (n >> 4) & MASK_3); 14 | n = (n&MASK_4) + ( (n >> 8) & MASK_4); 15 | n = (n&MASK_5) + ( (n >> 16) & MASK_5); 16 | n = (n&MASK_6) + ( (n >> 32) & MASK_6); 17 | 18 | return n; 19 | } 20 | 21 | void solve() 22 | { 23 | long long number_1, number_2; 24 | int answer; 25 | scanf("%I64u %I64u", &number_1, &number_2); 26 | 27 | answer = population_count(number_1^number_2); 28 | printf("%d\n",answer); 29 | } 30 | 31 | int main() 32 | { 33 | int no_of_test_cases; 34 | scanf("%d", &no_of_test_cases); 35 | 36 | while(no_of_test_cases-- != 0) 37 | { 38 | solve(); 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/Palindrome Count.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define MAX_LENGTH 50 + 2 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | char string[MAX_LENGTH]; 11 | scanf("%s", string); 12 | 13 | typedef vector v_int; 14 | vector is_palindrome(MAX_LENGTH, v_int(MAX_LENGTH, false)); 15 | 16 | int no_of_palindromic_substrings = 0; 17 | 18 | for(int i = 0; string[i] != '\0'; i++) 19 | { 20 | is_palindrome[i][i] = true; 21 | no_of_palindromic_substrings++; 22 | } 23 | 24 | int string_length = strlen(string); 25 | for(int length = 2; length <= string_length; length++) 26 | { 27 | int start = 0, end = start + (length - 1); 28 | for(;end < string_length; start++, end++) 29 | { 30 | if(string[start] == string[end]) 31 | { 32 | if(start + 1 == end || is_palindrome[start + 1][end - 1]) 33 | { 34 | is_palindrome[start][end] = true; 35 | no_of_palindromic_substrings++; 36 | } 37 | } 38 | } 39 | } 40 | 41 | printf("%d\n", no_of_palindromic_substrings); 42 | return 0; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/Panda and Chain Reaction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void precompute_factorial(vector &factorial) 7 | { 8 | const int MOD = 1e6 + 3; 9 | for(int i = 1; i < MOD; i++) 10 | factorial[i] = (factorial[i - 1]*1LL*i)%MOD; 11 | } 12 | 13 | void solve(vector &factorial) 14 | { 15 | long long no_of_chambers, particles_in_chamber_zero; 16 | scanf("%lld %lld", &no_of_chambers, &particles_in_chamber_zero); 17 | 18 | const int MOD = 1e6 + 3; 19 | long long answer; 20 | 21 | if(no_of_chambers >= MOD) 22 | { 23 | answer = 0; 24 | } 25 | else 26 | { 27 | answer = (particles_in_chamber_zero*factorial[no_of_chambers])%MOD; 28 | } 29 | 30 | printf("%lld\n", answer); 31 | } 32 | 33 | int main() 34 | { 35 | vector factorial(1e6 + 3 + 1, 1); 36 | precompute_factorial(factorial); 37 | 38 | int no_of_test_cases; 39 | scanf("%d", &no_of_test_cases); 40 | 41 | while(no_of_test_cases--) 42 | solve(factorial); 43 | 44 | return 0; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/Saraff's New Shop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void solve() 4 | { 5 | long long number; 6 | int k; 7 | scanf("%d %lld", &k, &number); 8 | 9 | int no_of_digits_n_in_base_k_plus_1 = 0; 10 | 11 | while(number > 0) 12 | { 13 | number = number/(k + 1); 14 | no_of_digits_n_in_base_k_plus_1++; 15 | } 16 | printf("%d\n", no_of_digits_n_in_base_k_plus_1); 17 | } 18 | 19 | int main() 20 | { 21 | int no_of_test_cases; 22 | scanf("%d", &no_of_test_cases); 23 | 24 | while(no_of_test_cases--) 25 | solve(); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/Sherlock_and_XOR.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void solve() 4 | { 5 | int no_of_numbers, no_of_odd_numbers = 0, no_of_even_numbers, i, current_number; 6 | unsigned int no_of_pairs; 7 | 8 | scanf("%d", &no_of_numbers); 9 | for(i = 0; i < no_of_numbers; i++) 10 | { 11 | scanf("%d",¤t_number); 12 | no_of_odd_numbers += (current_number % 2); 13 | } 14 | no_of_even_numbers = no_of_numbers - no_of_odd_numbers; 15 | 16 | no_of_pairs = (no_of_odd_numbers*no_of_even_numbers); //(p, q) gives an odd XOR iff p and q have different parity. We count the pairs up to order 17 | printf("%u\n",no_of_pairs); 18 | } 19 | int main() 20 | { 21 | int no_of_test_cases; 22 | scanf("%d",&no_of_test_cases); 23 | 24 | while(no_of_test_cases-- != 0) 25 | { 26 | solve(); 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/The Prime Cells.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | vector is_prime(500, true); 6 | 7 | void precompute_primes() 8 | { 9 | is_prime[0] = is_prime[1] = false; 10 | 11 | for(int i = 2; i <= 500; i++) 12 | { 13 | if(is_prime[i]) 14 | { 15 | for(int multiple = i*i; multiple <= 500; multiple += i) 16 | { 17 | is_prime[multiple] = false; 18 | } 19 | } 20 | } 21 | } 22 | 23 | int main() 24 | { 25 | precompute_primes(); 26 | 27 | int order_of_matrix; 28 | scanf("%d", &order_of_matrix); 29 | 30 | typedef vector v_int; 31 | vector grid(order_of_matrix + 2, v_int(order_of_matrix + 2, 0)); 32 | 33 | for(int row = 1; row <= order_of_matrix; row++) 34 | for(int column = 1; column <= order_of_matrix; column++) 35 | scanf("%d", &grid[row][column]); 36 | 37 | int cells_surrounded_by_primes = 0; 38 | for(int row = 1; row <= order_of_matrix; row++) 39 | for(int column = 1; column <= order_of_matrix; column++) 40 | cells_surrounded_by_primes += is_prime[grid[row][column - 1] + grid[row][column + 1] + 41 | grid[row - 1][column] + grid[row + 1][column]]; 42 | 43 | printf("%d\n", cells_surrounded_by_primes); 44 | return 0; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/Tic Tac Toe.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long power(int x, int n, int MOD) 5 | { 6 | long long result = 1LL; 7 | 8 | while(n > 0) 9 | { 10 | if(n%2 == 1) 11 | result = (result*1LL*x)%MOD; 12 | 13 | x = (x*1LL*x)%MOD; 14 | n = n/2; 15 | } 16 | return (result%MOD); 17 | } 18 | 19 | void solve() 20 | { 21 | const int MOD = 1e9 + 7; 22 | long long maximum, minimum; 23 | int no_of_players; 24 | scanf("%d", &no_of_players); 25 | 26 | //Max = sum of first n-1 squares ... Write it in a form to avoid overflow 27 | long long six_inverse = power(6, MOD - 2, MOD); 28 | maximum = (no_of_players - 1)*1LL*(no_of_players) %MOD; 29 | maximum = ( maximum*(2LL*no_of_players - 1) ) %MOD; 30 | maximum = ( maximum*six_inverse) %MOD; 31 | 32 | 33 | int no_of_matches = no_of_players - 1; //Play everyone but yourself. 34 | int wins = no_of_matches/2; //Everyone wins half their matches 35 | long long wins_squared = (wins*1LL*wins) % MOD; 36 | 37 | minimum = (no_of_players*wins_squared) % MOD; 38 | 39 | printf("%lld %lld\n",minimum, maximum); 40 | } 41 | 42 | int main() 43 | { 44 | int no_of_test_cases; 45 | scanf("%d", &no_of_test_cases); 46 | 47 | while(no_of_test_cases--) 48 | solve(); 49 | 50 | return 0; 51 | } 52 | 53 | -------------------------------------------------------------------------------- /C Programs/C Programs - 2/Tree of Many Leaves.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void solve() 4 | { 5 | int no_of_leaves, leaf_i; 6 | scanf("%d", &no_of_leaves); 7 | 8 | int root = 0; 9 | for(int i = 1; i <= no_of_leaves; i++) 10 | { 11 | scanf("%d", &leaf_i); 12 | root = root^leaf_i; 13 | } 14 | 15 | printf("%d\n",root); 16 | } 17 | 18 | int main() 19 | { 20 | int no_of_test_cases; 21 | scanf("%d", &no_of_test_cases); 22 | 23 | while(no_of_test_cases--) 24 | solve(); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Binary Queries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int no_of_bits, no_of_queries; 9 | scanf("%d %d", &no_of_bits, &no_of_queries); 10 | 11 | vector number(no_of_bits + 1); 12 | for(int i = 1; i <= no_of_bits; i++) 13 | scanf("%d", &number[i]); 14 | 15 | while(no_of_queries--) 16 | { 17 | const int PARITY_CHECK = 0, FLIP = 1; 18 | 19 | int query_type; 20 | scanf("%d", &query_type); 21 | 22 | if(query_type == PARITY_CHECK) 23 | { 24 | int left, right; 25 | scanf("%d %d", &left, &right); 26 | 27 | printf(number[right] == 1 ? "ODD\n" : "EVEN\n"); 28 | } 29 | else if(query_type == FLIP) 30 | { 31 | int position; 32 | scanf("%d", &position); 33 | 34 | number[position] ^= 1; 35 | } 36 | } 37 | 38 | return 0; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Bricks and Building.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void get_multiples(int n, vector &no_of_multiples) 6 | { 7 | for(int i = 1; i*i <= n; i++) 8 | { 9 | if(n%i == 0) 10 | { 11 | if(i*i == n) 12 | no_of_multiples[i]++; 13 | else 14 | { 15 | no_of_multiples[i]++; 16 | no_of_multiples[n/i]++; 17 | } 18 | } 19 | } 20 | } 21 | 22 | int main() 23 | { 24 | const int LIMIT = 1e5; 25 | vector no_of_multiples(LIMIT + 1, 0); 26 | 27 | int no_of_elements; 28 | scanf("%d", &no_of_elements); 29 | 30 | for(int i = 1; i <= no_of_elements; i++) 31 | { 32 | int element_i; 33 | scanf("%d", &element_i); 34 | 35 | get_multiples(element_i, no_of_multiples); 36 | } 37 | 38 | int no_of_queries; 39 | scanf("%d", &no_of_queries); 40 | 41 | while(no_of_queries--) 42 | { 43 | int n; 44 | scanf("%d", &n); 45 | printf("%d\n", no_of_multiples[n]); 46 | } 47 | 48 | return 0; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Calculate Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int MOD = 1e9 + 7; 4 | 5 | long long power(long long x, long long power) 6 | { 7 | long long result = 1; 8 | 9 | while(power) 10 | { 11 | if(power%2 == 1) 12 | result = (result*x)%MOD; 13 | 14 | x = (x*x)%MOD; 15 | power = power >> 1; 16 | } 17 | 18 | return result; 19 | } 20 | 21 | long long inverse(long long x) 22 | { 23 | return power(x, MOD - 2); 24 | } 25 | 26 | long long cube_sum(long long n) 27 | { 28 | long long sum = ((n*(n + 1)%MOD)*inverse(2))%MOD; 29 | 30 | return (sum*sum)%MOD; 31 | } 32 | 33 | long long square_sum(long long n) 34 | { 35 | return ( ( (n*(n + 1)%MOD )* ( (2*n + 1)%MOD )%MOD )*inverse(6) )%MOD; 36 | } 37 | 38 | void solve() 39 | { 40 | long long n; 41 | scanf("%lld", &n); 42 | 43 | n %= MOD; 44 | 45 | long long answer = square_sum(n)*inverse(2) + cube_sum(n)*inverse(2); 46 | answer %= MOD; 47 | 48 | printf("%lld\n", answer); 49 | } 50 | 51 | int main() 52 | { 53 | int no_of_test_cases; 54 | scanf("%d", &no_of_test_cases); 55 | 56 | while(no_of_test_cases--) 57 | solve(); 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Can you answer these queries.cpp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Coin Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int rightmost_zero_bits(int n) 4 | { 5 | int rightmost_zeroes = 0; 6 | 7 | while(n%2 == 0) 8 | { 9 | rightmost_zeroes++; 10 | n = n >> 1; 11 | } 12 | 13 | return rightmost_zeroes; 14 | } 15 | 16 | void solve() 17 | { 18 | int no_of_cards; 19 | scanf("%d", &no_of_cards); 20 | 21 | int no_of_moves = 0; 22 | for(int i = 1; i <= no_of_cards; i++) 23 | { 24 | int card_i; 25 | scanf("%d", &card_i); 26 | 27 | no_of_moves += rightmost_zero_bits(card_i); 28 | } 29 | 30 | printf(no_of_moves%2 == 0 ? "Alan\n" : "Charlie\n"); 31 | } 32 | 33 | int main() 34 | { 35 | int no_of_test_cases; 36 | scanf("%d", &no_of_test_cases); 37 | 38 | while(no_of_test_cases--) 39 | solve(); 40 | 41 | return 0; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Contribution from Divisors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int get_odd_digit_sum(int n) 7 | { 8 | int sum = 0; 9 | 10 | while(n > 0) 11 | { 12 | int digit = n%10; 13 | sum += digit*(digit%2); 14 | 15 | n = n/10; 16 | } 17 | 18 | return sum; 19 | } 20 | 21 | void precompute(vector &answer, int LIMIT) 22 | { 23 | for(int i = 1; i <= LIMIT; i++) 24 | { 25 | int odd_digit_sum = get_odd_digit_sum(i); 26 | 27 | for(int multiple = i; multiple <= LIMIT; multiple += i) 28 | { 29 | answer[multiple] += odd_digit_sum; 30 | } 31 | } 32 | } 33 | 34 | int main() 35 | { 36 | const int LIMIT = 1e6; 37 | vector answer(LIMIT + 1, 0); 38 | precompute(answer, LIMIT); 39 | 40 | int no_of_queries; 41 | scanf("%d", &no_of_queries); 42 | 43 | while(no_of_queries--) 44 | { 45 | int n; 46 | scanf("%d", &n); 47 | 48 | printf("%d\n", answer[n]); 49 | } 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Counting Number of Ways.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int MAX_X = 1e4, MAX_K = 100; 4 | int no_of_ways[MAX_X + 1][MAX_K + 1]; 5 | 6 | void precompute() 7 | { 8 | const int MOD = 1e9 + 7; 9 | 10 | for(int k = 1; k <= MAX_K; k++) 11 | { 12 | no_of_ways[0][k] = no_of_ways[1][k] = 1; 13 | 14 | for(int x = 2; x <= MAX_X; x++) 15 | { 16 | if(x <= k) 17 | no_of_ways[x][k] = (2LL*no_of_ways[x - 1][k])%MOD; 18 | else 19 | no_of_ways[x][k] = (2LL*no_of_ways[x - 1][k] + MOD - no_of_ways[x - k - 1][k])%MOD; 20 | } 21 | } 22 | } 23 | 24 | int main() 25 | { 26 | precompute(); 27 | 28 | int no_of_test_cases; 29 | scanf("%d", &no_of_test_cases); 30 | 31 | while(no_of_test_cases--) 32 | { 33 | int distance, k; 34 | scanf("%d %d", &distance, &k); 35 | printf("%d\n", no_of_ways[distance][k]); 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Fredo and Sums.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define all(v) (v).begin(), (v).end() 6 | using namespace std; 7 | 8 | void solve() 9 | { 10 | int no_of_elements; 11 | scanf("%d", &no_of_elements); 12 | 13 | vector element(no_of_elements); 14 | for(int i = 0; i < no_of_elements; i++) 15 | scanf("%d", &element[i]); 16 | 17 | sort(all(element)); 18 | 19 | long long minimum_s = 0, maximum_s = 0; 20 | for(int i = 0; i < no_of_elements; i+= 2) 21 | minimum_s += element[i + 1] - element[i]; 22 | 23 | for(int front_i = 0, back_i = no_of_elements - 1; front_i < back_i; front_i++, back_i--) 24 | maximum_s += element[back_i] - element[front_i]; 25 | 26 | printf("%lld %lld\n", minimum_s, maximum_s); 27 | } 28 | 29 | int main() 30 | { 31 | int no_of_test_cases; 32 | scanf("%d", &no_of_test_cases); 33 | 34 | while(no_of_test_cases--) 35 | solve(); 36 | 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Game of Coins.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int no_of_test_cases; 6 | scanf("%d", &no_of_test_cases); 7 | 8 | while(no_of_test_cases--) 9 | { 10 | int no_of_coins; 11 | scanf("%d", &no_of_coins); 12 | 13 | printf("Alice\n"); 14 | } 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Kya Ye wala Ho Payega.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int power_of_2_that_divides(long long n) 6 | { 7 | int power = 0; 8 | 9 | while(n%2 == 0) 10 | { 11 | n = n/2; 12 | power++; 13 | } 14 | return power; 15 | } 16 | 17 | int main() 18 | { 19 | int no_of_elements; 20 | scanf("%d", &no_of_elements); 21 | 22 | vector sum_of_power_of_2_till(no_of_elements + 1, 0); 23 | for(int i = 1; i <= no_of_elements; i++) 24 | { 25 | long long element_i; 26 | scanf("%lld", &element_i); 27 | 28 | sum_of_power_of_2_till[i] = sum_of_power_of_2_till[i - 1] + power_of_2_that_divides(element_i); 29 | } 30 | 31 | int no_of_queries; 32 | scanf("%d", &no_of_queries); 33 | 34 | while(no_of_queries--) 35 | { 36 | int left, right; 37 | scanf("%d %d", &left, &right); 38 | 39 | printf("%d\n", sum_of_power_of_2_till[right] - sum_of_power_of_2_till[left - 1]); 40 | } 41 | return 0; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Micro and Array Update.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define min(a, b) (a < b ? a : b) 4 | #define max(a, b) (a > b ? a : b) 5 | 6 | void solve() 7 | { 8 | int no_of_elements, k; 9 | scanf("%d %d", &no_of_elements, &k); 10 | 11 | const int oo = 1e9; 12 | int minimum = oo; 13 | 14 | while(no_of_elements--) 15 | { 16 | int element; 17 | scanf("%d", &element); 18 | 19 | minimum = min(minimum, element); 20 | } 21 | 22 | int answer = max(0, k - minimum); 23 | printf("%d\n", answer); 24 | } 25 | 26 | int main() 27 | { 28 | int no_of_test_cases; 29 | scanf("%d", &no_of_test_cases); 30 | 31 | while(no_of_test_cases--) 32 | solve(); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Naruto and his New Jutsu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void solve() 6 | { 7 | int n; 8 | scanf("%d", &n); 9 | 10 | int sum_of_odd_divisors = 0; 11 | 12 | while(n%2 == 0) 13 | n = n/2; 14 | 15 | for(int i = 1; i*i <= n; i++) 16 | { 17 | if(n%i == 0) 18 | { 19 | sum_of_odd_divisors += (i*i == n ? i : i + n/i); 20 | } 21 | } 22 | 23 | printf("%d\n", sum_of_odd_divisors); 24 | } 25 | 26 | int main() 27 | { 28 | int no_of_test_cases; 29 | scanf("%d", &no_of_test_cases); 30 | 31 | while(no_of_test_cases--) 32 | solve(); 33 | 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Prime Numbers Again.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define min(a, b) (a < b ? a : b) 5 | 6 | using namespace std; 7 | 8 | void precompute(vector &minimum_summands, int N) 9 | { 10 | vector is_prime(N + 1, true); 11 | is_prime[0] = is_prime[1] = false; 12 | 13 | for(int i = 2; i <= N; i++) 14 | { 15 | if(is_prime[i]) 16 | for(int multiple = 2*i; multiple <= N; multiple += i) 17 | is_prime[multiple] = false; 18 | } 19 | 20 | vector primatic; 21 | for(int i = 2; i <= N; i++) 22 | if(is_prime[i] || i == 4 || i == 27 || i == 3125) 23 | primatic.push_back(i); 24 | 25 | minimum_summands[0] = 0; 26 | 27 | for(int i = 2; i <= N; i++) 28 | for(int j = 0; primatic[j] <= i; j++) 29 | minimum_summands[i] = min(minimum_summands[i],1 + minimum_summands[i - primatic[j]]); 30 | } 31 | 32 | int main() 33 | { 34 | const int N = 1e4, oo = 1e9; 35 | vector minimum_summands(N + 1, oo); 36 | precompute(minimum_summands, N); 37 | 38 | int no_of_tests; 39 | scanf("%d", &no_of_tests); 40 | 41 | while(no_of_tests--) 42 | { 43 | int n; 44 | scanf("%d", &n); 45 | printf("%d\n", minimum_summands[n]); 46 | } 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Reduce the Fraction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define min(a, b) (a < b ? a : b) 4 | #define max(a, b) (a > b ? a : b) 5 | 6 | int gcd(int a, int b) 7 | { 8 | if(a == 0 || b == 0) 9 | return (a + b); 10 | 11 | return gcd(min(a, b), max(a, b)%min(a, b)); 12 | } 13 | 14 | void solve() 15 | { 16 | int A, B; 17 | scanf("%d %d", &A, &B); 18 | 19 | printf("%d/%d\n", A/gcd(A, B), B/gcd(A, B)); 20 | } 21 | 22 | int main() 23 | { 24 | int no_of_test_cases; 25 | scanf("%d", &no_of_test_cases); 26 | 27 | while(no_of_test_cases--) 28 | solve(); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Rhezo and Big Powers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX_SIZE 100000 + 3 4 | using namespace std; 5 | 6 | long long power_mod(int x, int power, int mod) 7 | { 8 | long long result = 1LL, y = x; 9 | 10 | while(power > 0) 11 | { 12 | if(power%2 == 1) 13 | result = (result*y)%mod; 14 | 15 | y = (y*y)%mod; 16 | power = power >> 1; 17 | } 18 | 19 | return result; 20 | } 21 | 22 | int get_remainder(char exponent[], int mod) 23 | { 24 | int remainder = 0; 25 | for(int i = 0; exponent[i] != '\0'; i++) 26 | { 27 | remainder = (remainder*10LL + exponent[i] - '0')%mod; 28 | } 29 | return remainder; 30 | } 31 | 32 | int main() 33 | { 34 | int a; 35 | char exponent[MAX_SIZE]; 36 | scanf("%d %s", &a, exponent); 37 | 38 | const int MOD = 1e9 + 7; 39 | int net_exponent = get_remainder(exponent, MOD - 1); 40 | 41 | long long answer = power_mod(a, net_exponent, MOD); 42 | printf("%lld\n", answer); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Riya's Birthday Party.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void solve() 5 | { 6 | long long n; 7 | const int mod = 1e9 + 7; 8 | 9 | scanf("%lld", &n); 10 | n %= mod; 11 | 12 | long long answer = n*(2*n - 1); 13 | answer %= mod; 14 | 15 | printf("%lld\n", answer); 16 | } 17 | 18 | int main() 19 | { 20 | int no_of_test_cases; 21 | scanf("%d", &no_of_test_cases); 22 | 23 | while(no_of_test_cases--) 24 | solve(); 25 | 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Roy and Ropes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define max(a, b) (a > b ? a : b) 5 | using namespace std; 6 | 7 | void solve() 8 | { 9 | int length; 10 | scanf("%d", &length); 11 | 12 | vector upper(length); 13 | for(int i = 1; i < length; i++) 14 | scanf("%d", &upper[i]); 15 | 16 | vector lower(length + 1); 17 | for(int i = 1; i < length; i++) 18 | scanf("%d", &lower[i]); 19 | 20 | for(int i = length - 1; i >= 1; i--) 21 | { 22 | int remaining_length = length - i; 23 | upper[i] = max(0, upper[i] - remaining_length); 24 | lower[i] = max(0, lower[i] - remaining_length); 25 | } 26 | 27 | int extra_time = 0; 28 | for(int i = 1; i < length; i++) 29 | { 30 | extra_time = max(extra_time, upper[i]); 31 | extra_time= max(extra_time, lower[i]); 32 | } 33 | 34 | int ans = length + extra_time; 35 | printf("%d\n", ans); 36 | } 37 | 38 | int main() 39 | { 40 | int no_of_test_cases; 41 | scanf("%d", &no_of_test_cases); 42 | 43 | while(no_of_test_cases--) 44 | solve(); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Selection of Cities.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long power_mod(int x, long long power, int mod) 5 | { 6 | long long result = 1, y = x; 7 | 8 | while(power > 0) 9 | { 10 | if(power%2 == 1) 11 | result = (result*y)%mod; 12 | 13 | y = (y*y)%mod; 14 | power = power >> 1; 15 | } 16 | 17 | return result; 18 | } 19 | 20 | void solve() 21 | { 22 | long long no_of_cities; 23 | scanf("%lld", &no_of_cities); 24 | 25 | const int mod = 1e9 + 7; 26 | long long no_of_choices = power_mod(2, no_of_cities, mod) + (mod - 1); //Adding -1 27 | no_of_choices %= mod; 28 | 29 | printf("%lld\n", no_of_choices); 30 | } 31 | 32 | int main() 33 | { 34 | int no_of_test_cases; 35 | scanf("%d", &no_of_test_cases); 36 | 37 | while(no_of_test_cases--) 38 | solve(); 39 | 40 | return 0; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Sumit's Sub Array and Sub Sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void solve() 8 | { 9 | int no_of_elements; 10 | scanf("%d", &no_of_elements); 11 | 12 | vector A(no_of_elements + 1, 0); 13 | for(int i = 1; i <= no_of_elements; i++) 14 | scanf("%d", &A[i]); 15 | 16 | int positive_sum = 0, smallest_negative_number = -1e5; 17 | for(int i = 1; i <= no_of_elements; i++) 18 | { 19 | if(A[i] > 0) 20 | positive_sum += A[i]; 21 | else 22 | smallest_negative_number = max(smallest_negative_number, A[i]); 23 | } 24 | int largest_sum = (positive_sum == 0 ? smallest_negative_number : positive_sum); 25 | 26 | int largest_sum_ending_here = 0, largest_subarray_sum = A[1]; 27 | for(int i = 1; i <= no_of_elements; i++) 28 | { 29 | largest_sum_ending_here = max(A[i], A[i] + largest_sum_ending_here); 30 | largest_subarray_sum = max(largest_sum_ending_here, largest_subarray_sum); 31 | } 32 | 33 | printf("%d %d\n", largest_subarray_sum, largest_sum); 34 | } 35 | 36 | int main() 37 | { 38 | int no_of_test_cases; 39 | scanf("%d", &no_of_test_cases); 40 | 41 | while(no_of_test_cases--) 42 | solve(); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /C Programs/C Programs - 3/Supreme Subset.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | #define all(v) (v).begin(), (v).end() 9 | 10 | int main() 11 | { 12 | int no_of_elements, m; 13 | scanf("%d %d", &no_of_elements, &m); 14 | 15 | vector element(no_of_elements + 1); 16 | map frequency; 17 | for(int i = 1; i <= no_of_elements; i++) 18 | { 19 | scanf("%d", &element[i]); 20 | 21 | frequency[element[i]%m]++; 22 | } 23 | 24 | int max_frequency = 0, answer_modulus; 25 | 26 | sort(all(element)); 27 | for(int i = 1; i <= no_of_elements; i++) 28 | { 29 | if(frequency[element[i]%m] > max_frequency) 30 | { 31 | max_frequency = frequency[element[i]%m]; 32 | answer_modulus = element[i]%m; 33 | } 34 | } 35 | 36 | vector answer; 37 | for(int i = 1; i <= no_of_elements; i++) 38 | if(element[i]%m == answer_modulus) 39 | answer.push_back(element[i]); 40 | 41 | 42 | printf("%u\n", answer.size()); 43 | for(int i = 0; i < answer.size(); i++) 44 | printf("%d ", answer[i]); 45 | 46 | return 0; 47 | } 48 | 49 | -------------------------------------------------------------------------------- /C Programs/C Programs - 4/Cheapest Subarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void solve() 7 | { 8 | int n; 9 | scanf("%d", &n); 10 | 11 | vector A(n); 12 | for(int i = 0; i < n; i++) scanf("%d", &A[i]); 13 | 14 | const int oo = 2e9; 15 | int answer = oo; 16 | 17 | for(int i = 0; i + 1 < n; i++) 18 | { 19 | answer = min(answer, A[i] + A[i + 1]); 20 | } 21 | 22 | printf("%d\n", answer); 23 | } 24 | 25 | int main() 26 | { 27 | int no_of_test_cases; 28 | scanf("%d", &no_of_test_cases); 29 | 30 | while(no_of_test_cases--) 31 | solve(); 32 | 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /C Programs/C Programs - 4/Holi and Colorful Houses.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void solve() 8 | { 9 | int length, no_of_queries; 10 | cin >> length >> no_of_queries; 11 | 12 | char house[length + 1]; cin >> (house + 1); 13 | 14 | vector no_of_changes_till(length + 1, 0); 15 | for(int i = 2; i <= length; i++) 16 | { 17 | no_of_changes_till[i] = no_of_changes_till[i - 1] + (house[i] != house[i - 1]); 18 | } 19 | 20 | while(no_of_queries--) 21 | { 22 | int left, right; 23 | cin >> left >> right; 24 | 25 | if(left > right) swap(left, right); 26 | 27 | int clockwise_answer = no_of_changes_till[right] - no_of_changes_till[left]; 28 | 29 | int anti_clockwise_answer = (no_of_changes_till[length] - no_of_changes_till[right]) 30 | + (no_of_changes_till[left] - no_of_changes_till[1]) 31 | + (house[length] != house[1]); 32 | 33 | int answer = min(clockwise_answer, anti_clockwise_answer); 34 | cout << answer << "\n"; 35 | } 36 | } 37 | 38 | int main() 39 | { 40 | int no_of_test_cases; 41 | cin >> no_of_test_cases; 42 | 43 | while(no_of_test_cases--) 44 | solve(); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /C Programs/C Programs - 4/Kingdom of Dreams.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define all(v) (v).begin(), (v).end() 4 | 5 | using namespace std; 6 | 7 | void solve() 8 | { 9 | int no_of_people; 10 | scanf("%d", &no_of_people); 11 | 12 | vector cost(no_of_people + 1, 0); 13 | for(int i = 0; i < no_of_people; i++) 14 | scanf("%d", &cost[i]); 15 | 16 | sort(all(cost)); 17 | 18 | long long total_cost = 0; 19 | 20 | while(no_of_people >= 4) 21 | { 22 | total_cost += min(cost[2] + cost[1] + cost[no_of_people] + cost[2], 23 | cost[no_of_people] + cost[1] + cost[no_of_people - 1] + cost[1]); 24 | 25 | no_of_people -= 2; 26 | } 27 | 28 | if(no_of_people == 3) total_cost += cost[3] + cost[1] + cost[2]; 29 | 30 | if(no_of_people == 2) total_cost += cost[2]; 31 | 32 | if(no_of_people == 1) total_cost += cost[1]; 33 | 34 | printf("%lld\n", total_cost); 35 | } 36 | 37 | int main() 38 | { 39 | int no_of_test_cases; 40 | scanf("%d", &no_of_test_cases); 41 | 42 | while(no_of_test_cases--) 43 | solve(); 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /C Programs/C Programs - 4/Phineas and Ferb.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int no_of_piles; 8 | scanf("%d", &no_of_piles); 9 | 10 | vector xor_of_all_piles_till(no_of_piles + 1, 0); 11 | for(int i = 1; i <= no_of_piles; i++) 12 | { 13 | int stones_i; 14 | scanf("%d", &stones_i); 15 | 16 | xor_of_all_piles_till[i] = xor_of_all_piles_till[i - 1]^stones_i; 17 | } 18 | 19 | int no_of_queries; 20 | scanf("%d", &no_of_queries); 21 | 22 | for(int i = 1; i <= no_of_queries; i++) 23 | { 24 | int left, right; 25 | scanf("%d %d", &left, &right); 26 | 27 | int bitwise_xor_piles = xor_of_all_piles_till[right]^ xor_of_all_piles_till[left - 1]; 28 | 29 | printf(bitwise_xor_piles == 0 ? "Ferb\n" : "Phineas\n"); 30 | } 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /C Programs/C Programs - 4/Points on a Rectangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define min(a, b) (a < b ? a : b) 4 | #define max(a, b) (a > b ? a : b) 5 | 6 | void solve() 7 | { 8 | int x1, y1, x2, y2, no_of_points, no_of_internal_points = 0; 9 | scanf("%d %d %d %d", &x1, &y1, &x2, &y2); 10 | 11 | scanf("%d", &no_of_points); 12 | 13 | while(no_of_points--) 14 | { 15 | int x, y; 16 | scanf("%d %d", &x, &y); 17 | 18 | no_of_internal_points += (x > min(x1, x2) && x < max(x1, x2) && y > min(y1, y2) && y> no_of_horses; 5 | 6 | vector H(no_of_horses + 1); 7 | for(int i = 1; i <= no_of_horses; i++) 8 | { 9 | cin >> H[i].velocity; 10 | } 11 | 12 | for(int i = 1; i <= no_of_horses; i++) 13 | { 14 | cin >> H[i].position; 15 | } 16 | 17 | sort(all(H)); 18 | 19 | memset(sum_tree, 0, sizeof(sum_tree)); 20 | 21 | long long inversions = 0; 22 | for(int i = 1; i <= no_of_horses;) 23 | { 24 | int j; 25 | 26 | for(j = i; j <= no_of_horses && H[j].velocity == H[i].velocity; j++) 27 | { 28 | inversions += get_sum(1, 1, no_of_horses, 1, H[j].position - 1); 29 | } 30 | 31 | for(j = i; j <= no_of_horses && H[j].velocity == H[i].velocity; j++) 32 | { 33 | update(1, 1, no_of_horses, H[j].position, 1); 34 | } 35 | 36 | i = j; 37 | 38 | } 39 | 40 | cout << inversions << "\n"; 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Contests/Data Structures and Algorithms Coding Contest 3/Rough Notes Link: -------------------------------------------------------------------------------- 1 | https://1drv.ms/b/s!AoiWcyvzkQXViRkIA1Dv9HUsI1Wm?e=7Exz3c 2 | -------------------------------------------------------------------------------- /Contests/December Easy 2019/Explanation/Digit Strings Explanation.txt: -------------------------------------------------------------------------------- 1 | Let f(i) be the number of ways of making partitions till i. 2 | 3 | We will iterate over the last j (at most 18) characters and check the value of the last segment 4 | 5 | If the segment [i - j, i] < k, then we will add f(i - j) to f(i) 6 | 7 | ----- 8 | 9 | void solve() 10 | { 11 | const int MOD = 1e9 + 7, MAX_N = 1e5 + 5; 12 | 13 | int length; 14 | char S[MAX_N]; 15 | long long K; 16 | cin >> length >> K; 17 | scanf("%s", S + 1); 18 | 19 | vector no_of_ways(length + 1, 0); 20 | no_of_ways[0] = 1; 21 | 22 | for(int i = 1; i <= length; i++) 23 | { 24 | for(int j = 0; j < 18 && i - j >= 1; j++) 25 | { 26 | long long this_segment = 0; 27 | for(int k = i - j; k <= i; k++) 28 | { 29 | this_segment = this_segment*10 + (S[k] - '0'); 30 | } 31 | 32 | if(this_segment < K) 33 | { 34 | no_of_ways[i] += no_of_ways[i - j - 1]; 35 | 36 | no_of_ways[i] %= MOD; 37 | } 38 | else 39 | { 40 | break; 41 | } 42 | } 43 | } 44 | 45 | cout << no_of_ways[length] << "\n"; 46 | } 47 | 48 | int main() 49 | { 50 | int no_of_test_cases; 51 | cin >> no_of_test_cases; 52 | 53 | while(no_of_test_cases--) 54 | solve(); 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Contests/December Easy 2019/Explanation/Fitting Circles Explanation.txt: -------------------------------------------------------------------------------- 1 | The circle diameter is = min(length, width). 2 | We will stack as many circles as possible. 3 | 4 | This is = max(l, w)/min(l, w) 5 | 6 | ----- 7 | 8 | #include 9 | 10 | using namespace std; 11 | 12 | void solve() 13 | { 14 | long long length, width; 15 | cin >> length >> width; 16 | 17 | if(width > length) 18 | { 19 | swap(width, length); 20 | } 21 | 22 | cout << length/width << "\n"; 23 | } 24 | 25 | int main() 26 | { 27 | int no_of_test_cases; 28 | cin >> no_of_test_cases; 29 | 30 | while(no_of_test_cases--) 31 | solve(); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Contests/December Easy 2019/Programs/Digit Strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | void solve() 9 | { 10 | const int MOD = 1e9 + 7, MAX_N = 1e5 + 5; 11 | 12 | int length; 13 | char S[MAX_N]; 14 | long long K; 15 | cin >> length >> K; 16 | scanf("%s", S + 1); 17 | 18 | vector no_of_ways(length + 1, 0); 19 | no_of_ways[0] = 1; 20 | 21 | for(int i = 1; i <= length; i++) 22 | { 23 | for(int j = 0; j < 18 && i - j >= 1; j++) 24 | { 25 | long long this_segment = 0; 26 | for(int k = i - j; k <= i; k++) 27 | { 28 | this_segment = this_segment*10 + (S[k] - '0'); 29 | } 30 | 31 | if(this_segment < K) 32 | { 33 | no_of_ways[i] += no_of_ways[i - j - 1]; 34 | 35 | no_of_ways[i] %= MOD; 36 | } 37 | else 38 | { 39 | break; 40 | } 41 | } 42 | } 43 | 44 | cout << no_of_ways[length] << "\n"; 45 | } 46 | 47 | int main() 48 | { 49 | int no_of_test_cases; 50 | cin >> no_of_test_cases; 51 | 52 | while(no_of_test_cases--) 53 | solve(); 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Contests/December Easy 2019/Programs/Fitting Circles.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void solve() 6 | { 7 | long long length, width; 8 | cin >> length >> width; 9 | 10 | if(width > length) 11 | { 12 | swap(width, length); 13 | } 14 | 15 | cout << length/width << "\n"; 16 | } 17 | 18 | int main() 19 | { 20 | int no_of_test_cases; 21 | cin >> no_of_test_cases; 22 | 23 | while(no_of_test_cases--) 24 | solve(); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Contests/December Easy 2020/Explanations/Non Great Equations Explanation.txt: -------------------------------------------------------------------------------- 1 | Let us rearrange the equation to take advantage of the symmetry. 2 | 3 | a^2 + b^3 = c^2 4 | 5 | b^3 = c^2 - a^2 = (c + a)(c - a) 6 | 7 | Suppose, we have two factors of b^3 = pq, 8 | 9 | Then, c + a = p, c - a = q 10 | 11 | But b^3 is too big to fit into 64 bits or be factorised. 12 | 13 | But, b and b^2 are both divisors of b^3 ! 14 | 15 | c + a = b^2 16 | c - a = b 17 | 18 | c = (b^2 + b)/2, a = (b^2 - b)/2 19 | 20 | ----- 21 | 22 | #include 23 | 24 | using namespace std; 25 | 26 | int main() 27 | { 28 | long long a, b, c; 29 | cin >> b; 30 | 31 | c = (b*b + b)/2; 32 | a = (b*b - b)/2; 33 | 34 | cout << a << " " << c << "\n"; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Contests/December Easy 2020/Programs/Non Great Equations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | long long a, b, c; 8 | cin >> b; 9 | 10 | c = (b*b + b)/2; 11 | a = (b*b - b)/2; 12 | 13 | cout << a << " " << c << "\n"; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Contests/December Easy 2021/Explanations/Bob's Confusion Explanation.txt: -------------------------------------------------------------------------------- 1 | It is very similar to this problem - https://atcoder.jp/contests/abc088/tasks/abc088_c 2 | 3 | Notice that if (A1, A2, A3, B1, B2, B3) is a solution to this problem, even 4 | 5 | (A1 + x, A2 + x, A3 + x, B1 - x, B2 - x, B3 - x) is also a solution to this problem. 6 | 7 | If one solution exists, it implies there are an infinite number of solutions. 8 | 9 | So, we can choose x appropriately by setting A1 = 0 10 | 11 | https://mathprogrammer.quora.com/AtCoder-ABC088_C-Takahashis-Information?share=50561b47&srid=F7Hz 12 | 13 | ----- 14 | 15 | void solve() 16 | { 17 | const int N = 3; 18 | vector > grid(N + 1, vector (N + 1)); 19 | for(int i = 1; i <= N; i++) 20 | { 21 | for(int j = 1; j <= N; j++) 22 | { 23 | cin >> grid[i][j]; 24 | } 25 | } 26 | 27 | vector A(N + 1), B(N + 1); 28 | A[1] = 0; 29 | B[1] = grid[1][1] - A[1]; 30 | A[2] = grid[2][1] - B[1]; 31 | B[2] = grid[2][2] - A[2]; 32 | A[3] = grid[3][2] - B[2]; 33 | B[3] = grid[3][3] - A[3]; 34 | 35 | int possible = true; 36 | for(int i = 1; i <= N; i++) 37 | { 38 | for(int j = 1; j <= N; j++) 39 | { 40 | if(A[i] + B[j] != grid[i][j]) 41 | { 42 | possible = false; 43 | } 44 | } 45 | } 46 | 47 | cout << (possible ? "YES" : "NO") << "\n"; 48 | } 49 | -------------------------------------------------------------------------------- /Contests/December Easy 2021/Explanations/Manhattan Distance Explanation.txt: -------------------------------------------------------------------------------- 1 | First of all, let us notice that X and Y are independent. 2 | So, let us discuss the problem of finding the sum of Manhattan distances for one dimension. 3 | 4 | There are N^2 pairs but only N terms so let us count the contribution of each X_i 5 | 6 | Suppose X_1 < X_2 < ... < X_i < ... < X_n 7 | 8 | X_i will be added when it is paired with any of the first (i - 1) terms 9 | X_i will be subtracted when it is paired with any of the later (n - i) terms 10 | 11 | So X_i is counted totally [(i - 1) - (n - i)] times. 12 | 13 | Answer = sum[ (i - 1) - (n - i) ] X_i 14 | 15 | ----- 16 | 17 | long long calculate(vector &A, int no_of_elements) 18 | { 19 | sort(A.begin(), A.end()); 20 | 21 | long long sum = 0; 22 | for(int i = 1; i <= no_of_elements; i++) 23 | { 24 | long long before_contribution = (i - 1)*A[i]; 25 | long long after_contribution = (no_of_elements - i)*A[i]; 26 | 27 | sum += (before_contribution - after_contribution); 28 | } 29 | 30 | return sum; 31 | } 32 | 33 | void solve() 34 | { 35 | int no_of_points; 36 | cin >> no_of_points; 37 | 38 | vector X(no_of_points + 1), Y(no_of_points + 1); 39 | for(int i = 1; i <= no_of_points; i++) 40 | { 41 | cin >> X[i] >> Y[i]; 42 | } 43 | 44 | long long x_answer = calculate(X, no_of_points), y_answer = calculate(Y, no_of_points); 45 | long long answer = x_answer + y_answer; 46 | 47 | cout << answer << "\n"; 48 | } 49 | -------------------------------------------------------------------------------- /Contests/December Easy 2021/Explanations/Maximum MOD Explanation.txt: -------------------------------------------------------------------------------- 1 | Let X = (A_1 A_2 ... A_n) - 1 2 | 3 | x = -1 (mod A_i) 4 | 5 | So we can get the maximum mod (-1) with all of the integers. The answer is sum of (A_i - 1) 6 | 7 | ----- 8 | 9 | void solve() 10 | { 11 | int no_of_elements; 12 | cin >> no_of_elements; 13 | 14 | long long sum = 0; 15 | for(int i = 1; i <= no_of_elements; i++) 16 | { 17 | int x; 18 | cin >> x; 19 | sum += (x - 1); 20 | } 21 | 22 | cout << sum << "\n"; 23 | } 24 | -------------------------------------------------------------------------------- /Contests/December Easy 2021/Explanations/Sum as Frequency Explanation.txt: -------------------------------------------------------------------------------- 1 | Let us maintain an array Frequency_sum 2 | 3 | Where Frequency_sum[i] holds the sum of all elements who's frequency = i 4 | 5 | We can maintain a prefix sum array over this array in order to answer each query in O(1) time 6 | 7 | One thing to be careful of is each element should be counted multiple times if it occurs multiple times in the array. 8 | 9 | ------ 10 | 11 | int main() 12 | { 13 | ios_base::sync_with_stdio(false); 14 | cin.tie(NULL); 15 | 16 | int no_of_elements; 17 | cin >> no_of_elements; 18 | 19 | map frequency; 20 | vector A(no_of_elements + 1); 21 | for(int i = 1; i <= no_of_elements; i++) 22 | { 23 | cin >> A[i]; 24 | 25 | frequency[A[i]]++; 26 | } 27 | 28 | vector frequency_sum(no_of_elements + 1); 29 | for(auto it = frequency.begin(); it != frequency.end(); it++) 30 | { 31 | int element = it->first; 32 | 33 | frequency_sum[frequency[element]] += element*1LL*frequency[element]; 34 | } 35 | 36 | vector frequency_prefix_sum(no_of_elements + 1); 37 | for(int i = 1; i <= no_of_elements; i++) 38 | { 39 | frequency_prefix_sum[i] = frequency_prefix_sum[i - 1] + frequency_sum[i]; 40 | } 41 | 42 | int no_of_queries; 43 | cin >> no_of_queries; 44 | 45 | while(no_of_queries--) 46 | { 47 | int left, right; 48 | cin >> left >> right; 49 | 50 | cout << frequency_prefix_sum[right] - frequency_prefix_sum[left - 1] << "\n"; 51 | } 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /Contests/December Easy 2021/Programs/Bob's Confusion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void solve() 7 | { 8 | const int N = 3; 9 | vector > grid(N + 1, vector (N + 1)); 10 | for(int i = 1; i <= N; i++) 11 | { 12 | for(int j = 1; j <= N; j++) 13 | { 14 | cin >> grid[i][j]; 15 | } 16 | } 17 | 18 | vector A(N + 1), B(N + 1); 19 | A[1] = 0; 20 | B[1] = grid[1][1] - A[1]; 21 | A[2] = grid[2][1] - B[1]; 22 | B[2] = grid[2][2] - A[2]; 23 | A[3] = grid[3][2] - B[2]; 24 | B[3] = grid[3][3] - A[3]; 25 | 26 | int possible = true; 27 | for(int i = 1; i <= N; i++) 28 | { 29 | for(int j = 1; j <= N; j++) 30 | { 31 | if(A[i] + B[j] != grid[i][j]) 32 | { 33 | possible = false; 34 | } 35 | } 36 | } 37 | 38 | cout << (possible ? "YES" : "NO") << "\n"; 39 | } 40 | 41 | int main() 42 | { 43 | int no_of_test_cases; 44 | cin >> no_of_test_cases; 45 | 46 | while(no_of_test_cases--) 47 | solve(); 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Contests/December Easy 2021/Programs/Manhattan Distance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define all(v) (v).begin, (v).end() 6 | using namespace std; 7 | 8 | long long calculate(vector &A, int no_of_elements) 9 | { 10 | sort(A.begin(), A.end()); 11 | 12 | long long sum = 0; 13 | for(int i = 1; i <= no_of_elements; i++) 14 | { 15 | long long before_contribution = (i - 1)*A[i]; 16 | long long after_contribution = (no_of_elements - i)*A[i]; 17 | 18 | sum += (before_contribution - after_contribution); 19 | } 20 | 21 | return sum; 22 | } 23 | 24 | void solve() 25 | { 26 | int no_of_points; 27 | cin >> no_of_points; 28 | 29 | vector X(no_of_points + 1), Y(no_of_points + 1); 30 | for(int i = 1; i <= no_of_points; i++) 31 | { 32 | cin >> X[i] >> Y[i]; 33 | } 34 | 35 | long long x_answer = calculate(X, no_of_points), y_answer = calculate(Y, no_of_points); 36 | long long answer = x_answer + y_answer; 37 | 38 | cout << answer << "\n"; 39 | } 40 | 41 | int main() 42 | { 43 | int no_of_test_cases; 44 | cin >> no_of_test_cases; 45 | 46 | while(no_of_test_cases--) 47 | solve(); 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Contests/December Easy 2021/Programs/Maximum MOD.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void solve() 6 | { 7 | int no_of_elements; 8 | cin >> no_of_elements; 9 | 10 | long long sum = 0; 11 | for(int i = 1; i <= no_of_elements; i++) 12 | { 13 | int x; 14 | cin >> x; 15 | sum += (x - 1); 16 | } 17 | 18 | cout << sum << "\n"; 19 | } 20 | 21 | int main() 22 | { 23 | int no_of_test_cases; 24 | cin >> no_of_test_cases; 25 | 26 | while(no_of_test_cases--) 27 | solve(); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Contests/December Easy 2021/Programs/Sum as per Frequency.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | ios_base::sync_with_stdio(false); 10 | cin.tie(NULL); 11 | 12 | int no_of_elements; 13 | cin >> no_of_elements; 14 | 15 | map frequency; 16 | vector A(no_of_elements + 1); 17 | for(int i = 1; i <= no_of_elements; i++) 18 | { 19 | cin >> A[i]; 20 | 21 | frequency[A[i]]++; 22 | } 23 | 24 | vector frequency_sum(no_of_elements + 1); 25 | for(auto it = frequency.begin(); it != frequency.end(); it++) 26 | { 27 | int element = it->first; 28 | 29 | frequency_sum[frequency[element]] += element*1LL*frequency[element]; 30 | } 31 | 32 | vector frequency_prefix_sum(no_of_elements + 1); 33 | for(int i = 1; i <= no_of_elements; i++) 34 | { 35 | frequency_prefix_sum[i] = frequency_prefix_sum[i - 1] + frequency_sum[i]; 36 | } 37 | 38 | int no_of_queries; 39 | cin >> no_of_queries; 40 | 41 | while(no_of_queries--) 42 | { 43 | int left, right; 44 | cin >> left >> right; 45 | 46 | cout << frequency_prefix_sum[right] - frequency_prefix_sum[left - 1] << "\n"; 47 | } 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Contests/December Easy 2022/Programs/ASME to SAME.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void count(string &S, vector &F) 5 | { 6 | for(int i = 0; i < S.size(); i++) 7 | { 8 | if('a' <= S[i] && S[i] <= 'z') 9 | { 10 | F[S[i] - 'a']++; 11 | } 12 | } 13 | } 14 | 15 | void solve() 16 | { 17 | int length; 18 | string S, T; 19 | cin >> length >> S >> T; 20 | 21 | const int NO_OF_ALPHABETS = 26; 22 | vector frequency_S(NO_OF_ALPHABETS, 0); 23 | count(S, frequency_S); 24 | 25 | vector frequency_T(NO_OF_ALPHABETS, 0); 26 | count(T, frequency_T); 27 | 28 | int question_marks = 0; 29 | for(int i = 0; i < S.size(); i++) 30 | { 31 | question_marks += (S[i] == '?'); 32 | } 33 | 34 | int possible = true, needed_question_marks = 0; 35 | for(int alpha = 0; alpha < NO_OF_ALPHABETS; alpha++) 36 | { 37 | if(frequency_S[alpha] > frequency_T[alpha]) 38 | { 39 | possible = false; 40 | } 41 | 42 | if(frequency_T[alpha] > frequency_S[alpha]) 43 | { 44 | needed_question_marks += frequency_T[alpha] - frequency_S[alpha]; 45 | } 46 | } 47 | cout << (possible && needed_question_marks == question_marks ? "Yes" : "No") << "\n"; 48 | } 49 | 50 | int main() 51 | { 52 | int no_of_test_cases; 53 | cin >> no_of_test_cases; 54 | 55 | while(no_of_test_cases--) 56 | solve(); 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Contests/December Easy 2022/Programs/Fruit Smash.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void solve() 5 | { 6 | int no_of_elements; 7 | cin >> no_of_elements; 8 | 9 | vector A(no_of_elements + 1); 10 | for(int i = 1; i <= no_of_elements; i++) 11 | { 12 | cin >> A[i]; 13 | } 14 | 15 | set LIS; 16 | for(int i = no_of_elements; i >= 1; i--) 17 | { 18 | if(LIS.size() == 0 || A[i] > *(LIS.rbegin()) ) 19 | { 20 | LIS.insert(A[i]); 21 | } 22 | else 23 | { 24 | auto first_greater_it = LIS.lower_bound(A[i]); 25 | 26 | LIS.erase(first_greater_it); 27 | 28 | LIS.insert(A[i]); 29 | } 30 | } 31 | 32 | cout << LIS.size() << "\n"; 33 | } 34 | 35 | int main() 36 | { 37 | int no_of_test_cases; 38 | cin >> no_of_test_cases; 39 | 40 | while(no_of_test_cases--) 41 | solve(); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Contests/February Easy 2019/Explanations/Good Subsequences Explanation.txt: -------------------------------------------------------------------------------- 1 | The length of the longest distinct subsequence is simply the number of distinct letters in the string. We will choose one of each and concatenate them. 2 | 3 | Now, we must realise that in any distinct subsequence of this length, each of the distinct letters must appear exactly once. 4 | 5 | The number of ways of choosing alphabet x, is frequency[x] in the string. 6 | 7 | The number of distinct subsequences is the product of the frequencies of all the alphabets. 8 | 9 | ----- 10 | 11 | void solve() 12 | { 13 | string S; 14 | cin >> S; 15 | 16 | const int NO_OF_ALPHABETS = 26; 17 | vector frequency(NO_OF_ALPHABETS, 0); 18 | for(int i = 0; i < S.size(); i++) 19 | frequency[S[i] - 'a']++; 20 | 21 | const int MOD = 1e9 + 7; 22 | long long no_of_subsequences = 1; 23 | 24 | for(int alpha = 0; alpha < NO_OF_ALPHABETS; alpha++) 25 | if(frequency[alpha] != 0) 26 | no_of_subsequences = (no_of_subsequences*frequency[alpha])%MOD; 27 | 28 | cout << no_of_subsequences << endl; 29 | } -------------------------------------------------------------------------------- /Contests/February Easy 2019/Explanations/Pile of Coins Explanation.txt: -------------------------------------------------------------------------------- 1 | The first player wins if there is at least one even number and loses otherwise. 2 | 3 | Proof - 4 | 5 | If there is one even number, the first player can write it as the sum of two odd integers. 6 | 7 | The second player must break one of the odd numbers as the sum of an odd and an even number. 8 | 9 | The first player will again choose the even number. 10 | 11 | The final configuration of (1, 1) - two odd numbers, can only occur after the first player's move. 12 | 13 | ---- 14 | 15 | If there are only odd numbers in the beginning, then the first player is in the situation the second player was in above and loses. 16 | 17 | --- 18 | 19 | void solve() 20 | { 21 | int pile_1, pile_2; 22 | cin >> pile_1 >> pile_2; 23 | 24 | cout << (pile_1%2 == 1 && pile_2%2 == 1 ? "Jeel\n" : "Ashish\n"); 25 | } -------------------------------------------------------------------------------- /Contests/February Easy 2019/Programs/Array Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void solve() 8 | { 9 | int no_of_elements; 10 | cin >> no_of_elements; 11 | 12 | vector A(no_of_elements + 1); 13 | for(int i = 1; i <= no_of_elements; i++) 14 | cin >> A[i]; 15 | 16 | vector max_till(no_of_elements + 1, 0); 17 | for(int i = 1; i <= no_of_elements; i++) 18 | max_till[i] = max(max_till[i - 1], A[i]); 19 | 20 | vector min_from(no_of_elements + 1); 21 | min_from[no_of_elements] = A[no_of_elements]; 22 | for(int i = no_of_elements - 1; i >= 1; i--) 23 | min_from[i] = min(min_from[i + 1], A[i]); 24 | 25 | int no_of_break_points = 0; 26 | for(int i = 1; i < no_of_elements; i++) 27 | { 28 | no_of_break_points += (max_till[i] < min_from[i + 1]); 29 | } 30 | 31 | cout << (no_of_break_points%2 == 0 ? "Ashish\n" : "Jeel\n"); 32 | } 33 | 34 | int main() 35 | { 36 | int no_of_test_cases; 37 | cin >> no_of_test_cases; 38 | 39 | while(no_of_test_cases--) 40 | solve(); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Contests/February Easy 2019/Programs/Good Subsequences.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void solve() 8 | { 9 | string S; 10 | cin >> S; 11 | 12 | const int NO_OF_ALPHABETS = 26; 13 | vector frequency(NO_OF_ALPHABETS, 0); 14 | for(int i = 0; i < S.size(); i++) 15 | frequency[S[i] - 'a']++; 16 | 17 | const int MOD = 1e9 + 7; 18 | long long no_of_subsequences = 1; 19 | 20 | for(int alpha = 0; alpha < NO_OF_ALPHABETS; alpha++) 21 | if(frequency[alpha] != 0) 22 | no_of_subsequences = (no_of_subsequences*frequency[alpha])%MOD; 23 | 24 | cout << no_of_subsequences << endl; 25 | } 26 | 27 | int main() 28 | { 29 | int no_of_test_cases; 30 | cin >> no_of_test_cases; 31 | 32 | while(no_of_test_cases--) 33 | solve(); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Contests/February Easy 2019/Programs/Pile of Coins.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void solve() 8 | { 9 | int pile_1, pile_2; 10 | cin >> pile_1 >> pile_2; 11 | 12 | cout << (pile_1%2 == 1 && pile_2%2 == 1 ? "Jeel\n" : "Ashish\n"); 13 | } 14 | 15 | int main() 16 | { 17 | int no_of_test_cases; 18 | cin >> no_of_test_cases; 19 | 20 | while(no_of_test_cases--) 21 | solve(); 22 | 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Contests/January Easy 2018/Explanations/Shubham and XOR Explanation.txt: -------------------------------------------------------------------------------- 1 | A^B = 0 => A = B 2 | 3 | We just need to count the number of pairs (A, B) such that A = B 4 | 5 | --- 6 | 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | int no_of_elements; 15 | scanf("%d", &no_of_elements); 16 | 17 | map frequency; 18 | 19 | while(no_of_elements--) 20 | { 21 | int element; 22 | scanf("%d", &element); 23 | 24 | frequency[element]++; 25 | } 26 | 27 | long long no_of_pairs = 0; 28 | 29 | for(map :: iterator i = frequency.begin(); i != frequency.end(); i++) 30 | { 31 | long long frequency_i = i->second; 32 | 33 | no_of_pairs += (frequency_i*(frequency_i - 1))/2; 34 | } 35 | 36 | printf("%lld\n", no_of_pairs); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Contests/January Easy 2018/Programs/Shubham and XOR .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int no_of_elements; 9 | scanf("%d", &no_of_elements); 10 | 11 | map frequency; 12 | 13 | while(no_of_elements--) 14 | { 15 | int element; 16 | scanf("%d", &element); 17 | 18 | frequency[element]++; 19 | } 20 | 21 | long long no_of_pairs = 0; 22 | 23 | for(map :: iterator i = frequency.begin(); i != frequency.end(); i++) 24 | { 25 | long long frequency_i = i->second; 26 | 27 | no_of_pairs += (frequency_i*(frequency_i - 1))/2; 28 | } 29 | 30 | printf("%lld\n", no_of_pairs); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Contests/January Easy 2020/Programs/Maximum Bit Shifts.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int is_bit_set(long long n, int bit) 7 | { 8 | return ( (n & (1LL << bit)) != 0 ); 9 | } 10 | 11 | int count_ones(long long n) 12 | { 13 | int ones = 0; 14 | 15 | while(n) 16 | { 17 | ones += n%2; 18 | 19 | n /= 2; 20 | } 21 | 22 | return ones; 23 | } 24 | 25 | int count_bits(long long n) 26 | { 27 | int bits = 0; 28 | 29 | while(n) 30 | { 31 | bits++; 32 | 33 | n /= 2; 34 | } 35 | 36 | return bits; 37 | } 38 | 39 | int main() 40 | { 41 | int no_of_elements; 42 | cin >> no_of_elements; 43 | 44 | vector A(no_of_elements + 1); 45 | for(int i = 1; i <= no_of_elements; i++) 46 | { 47 | cin >> A[i]; 48 | } 49 | 50 | vector B(no_of_elements + 1); 51 | for(int i = 1; i <= no_of_elements; i++) 52 | { 53 | int no_of_ones = count_ones(A[i]), total_bits = count_bits(A[i]); 54 | 55 | for(int bit = total_bits - 1; bit >= 0; bit--) 56 | { 57 | if(no_of_ones > 0) 58 | { 59 | B[i] |= (1LL << bit); 60 | 61 | no_of_ones--; 62 | } 63 | } 64 | } 65 | 66 | for(int i = 1; i <= no_of_elements; i++) 67 | { 68 | cout << B[i] << " "; 69 | } 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /Contests/July Easy 2019/Rough Notes Link: -------------------------------------------------------------------------------- 1 | https://1drv.ms/b/s!AoiWcyvzkQXVh2ulJNEYQpeZbn5k?e=LoqzZr 2 | -------------------------------------------------------------------------------- /Contests/June Easy 2018/Explanation/Cheapest Subarray Explanation.txt: -------------------------------------------------------------------------------- 1 | Blog Link - http://qr.ae/TUppyf 2 | 3 | Basically, the optimum answer always consists of consecutive elements. 4 | 5 | If we have A[L, ... , R] 6 | 7 | Then A[i, i + 1] will always have a smaller sum, where i is the index of the minimum element. 8 | 9 | ---------------------------- 10 | 11 | void solve() 12 | { 13 | int n; 14 | scanf("%d", &n); 15 | 16 | vector A(n); 17 | for(int i = 0; i < n; i++) scanf("%d", &A[i]); 18 | 19 | const int oo = 2e9; 20 | int answer = oo; 21 | 22 | for(int i = 0; i + 1 < n; i++) 23 | { 24 | answer = min(answer, A[i] + A[i + 1]); 25 | } 26 | 27 | printf("%d\n", answer); 28 | } 29 | -------------------------------------------------------------------------------- /Contests/June Easy 2018/Explanation/Mosquito Mesh Explanation.txt: -------------------------------------------------------------------------------- 1 | Blog Link - http://qr.ae/TUp9V6 2 | 3 | int main() 4 | { 5 | precompute(); 6 | 7 | int n, no_of_holes; 8 | scanf("%d %d", &n, &no_of_holes); 9 | 10 | vector is_free(n + 1, true); 11 | for(int i = 1; i <= no_of_holes; i++) 12 | { 13 | int top, size; 14 | scanf("%d %d", &top, &size); 15 | 16 | for(int j = top + 1; j < top + size ; j++) 17 | is_free[j] = false; 18 | } 19 | 20 | long long no_of_paths = 0; 21 | 22 | for(int i = 0; i <= n; i++) 23 | { 24 | if(is_free[i])//Square (i, i) is not in a mesh. 25 | { 26 | no_of_paths += square(choose(n, i)); 27 | no_of_paths %= MOD; 28 | } 29 | } 30 | 31 | printf("%lld\n", no_of_paths); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Contests/June Easy 2018/Programs/Cheapest Subarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void solve() 7 | { 8 | int n; 9 | scanf("%d", &n); 10 | 11 | vector A(n); 12 | for(int i = 0; i < n; i++) scanf("%d", &A[i]); 13 | 14 | const int oo = 2e9; 15 | int answer = oo; 16 | 17 | for(int i = 0; i + 1 < n; i++) 18 | { 19 | answer = min(answer, A[i] + A[i + 1]); 20 | } 21 | 22 | printf("%d\n", answer); 23 | } 24 | 25 | int main() 26 | { 27 | int no_of_test_cases; 28 | scanf("%d", &no_of_test_cases); 29 | 30 | while(no_of_test_cases--) 31 | solve(); 32 | 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Contests/June Easy 2019/Explanations/Cost of Balloons Explanation.txt: -------------------------------------------------------------------------------- 1 | We will find out the price if green is associated with answer 1 and purple with answer 2 and vice versa. 2 | 3 | --- 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | void solve() 10 | { 11 | int green, purple, no_of_participants; 12 | cin >> green >> purple >> no_of_participants; 13 | 14 | long long first_solves = 0, second_solves = 0; 15 | for(int i = 1; i <= no_of_participants; i++) 16 | { 17 | int answer_1, answer_2; 18 | cin >> answer_1 >> answer_2; 19 | 20 | first_solves += answer_1; 21 | second_solves += answer_2; 22 | } 23 | 24 | long long price = min(green*first_solves + purple*second_solves, 25 | purple*first_solves + green*second_solves); 26 | cout << price << "\n"; 27 | } 28 | 29 | int main() 30 | { 31 | int no_of_tests; 32 | cin >> no_of_tests; 33 | 34 | while(no_of_tests--) 35 | solve(); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Contests/June Easy 2019/Explanations/Median Game Explanation.txt: -------------------------------------------------------------------------------- 1 | The minimum and maximum element of the array are never the Median of any subarray. Hence, they are the final two elements remaining. 2 | 3 | --- 4 | 5 | void solve() 6 | { 7 | int no_of_elements; 8 | cin >> no_of_elements; 9 | 10 | const int oo = 1e9 + 9; 11 | int maximum = -oo, minimum = oo; 12 | 13 | while(no_of_elements--) 14 | { 15 | int element; 16 | cin >> element; 17 | 18 | maximum = max(maximum, element); 19 | minimum = min(minimum, element); 20 | } 21 | 22 | cout << maximum + minimum << "\n"; 23 | } -------------------------------------------------------------------------------- /Contests/June Easy 2019/Explanations/Minimum Cost Explanation.txt: -------------------------------------------------------------------------------- 1 | We can use a queue and perform BFS. 2 | 3 | At each step, we can either go to P(i) or go to (i + 1) or (i - 1) with the respective cost of {0, 1, 1}. 4 | 5 | The answer is the cost to reach square N. 6 | 7 | ----- 8 | 9 | void solve() 10 | { 11 | int no_of_elements; 12 | cin >> no_of_elements; 13 | 14 | vector permutation(no_of_elements + 1); 15 | for(int i = 1; i <= no_of_elements; i++) 16 | cin >> permutation[i]; 17 | 18 | const int oo = 1e9 + 9; 19 | vector answer(no_of_elements + 1, oo); 20 | 21 | deque Q; 22 | Q.push_back(1); 23 | answer[1] = 0; 24 | 25 | while(Q.size()) 26 | { 27 | int x = Q.front(); 28 | Q.pop_front(); 29 | 30 | if(answer[permutation[x]] > answer[x]) 31 | { 32 | answer[permutation[x]] = answer[x]; 33 | Q.push_front(permutation[x]); 34 | } 35 | if(x - 1 > 0 && answer[x - 1] > answer[x] + 1) 36 | { 37 | answer[x - 1] = answer[x] + 1; 38 | Q.push_back(x - 1); 39 | } 40 | if(x + 1 <= no_of_elements && answer[x + 1] > answer[x] + 1) 41 | { 42 | answer[x + 1] = answer[x] + 1; 43 | Q.push_back(x + 1); 44 | } 45 | } 46 | 47 | cout << answer[no_of_elements] << "\n"; 48 | } -------------------------------------------------------------------------------- /Contests/June Easy 2019/Programs/Cost of Balloons.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void solve() 6 | { 7 | int green, purple, no_of_participants; 8 | cin >> green >> purple >> no_of_participants; 9 | 10 | long long first_solves = 0, second_solves = 0; 11 | for(int i = 1; i <= no_of_participants; i++) 12 | { 13 | int answer_1, answer_2; 14 | cin >> answer_1 >> answer_2; 15 | 16 | first_solves += answer_1; 17 | second_solves += answer_2; 18 | } 19 | 20 | long long price = min(green*first_solves + purple*second_solves, 21 | purple*first_solves + green*second_solves); 22 | cout << price << "\n"; 23 | } 24 | 25 | int main() 26 | { 27 | int no_of_tests; 28 | cin >> no_of_tests; 29 | 30 | while(no_of_tests--) 31 | solve(); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Contests/June Easy 2019/Programs/Median Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void solve() 6 | { 7 | int no_of_elements; 8 | cin >> no_of_elements; 9 | 10 | const int oo = 1e9 + 9; 11 | int maximum = -oo, minimum = oo; 12 | 13 | while(no_of_elements--) 14 | { 15 | int element; 16 | cin >> element; 17 | 18 | maximum = max(maximum, element); 19 | minimum = min(minimum, element); 20 | } 21 | 22 | cout << maximum + minimum << "\n"; 23 | } 24 | 25 | int main() 26 | { 27 | int no_of_tests; 28 | cin >> no_of_tests; 29 | 30 | while(no_of_tests--) 31 | solve(); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Contests/June Easy 2019/Programs/Minimum Cost.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void solve() 8 | { 9 | int no_of_elements; 10 | cin >> no_of_elements; 11 | 12 | vector permutation(no_of_elements + 1); 13 | for(int i = 1; i <= no_of_elements; i++) 14 | cin >> permutation[i]; 15 | 16 | const int oo = 1e9 + 9; 17 | vector answer(no_of_elements + 1, oo); 18 | 19 | deque Q; 20 | Q.push_back(1); 21 | answer[1] = 0; 22 | 23 | while(Q.size()) 24 | { 25 | int x = Q.front(); 26 | Q.pop_front(); 27 | 28 | if(answer[permutation[x]] > answer[x]) 29 | { 30 | answer[permutation[x]] = answer[x]; 31 | Q.push_front(permutation[x]); 32 | } 33 | if(x - 1 > 0 && answer[x - 1] > answer[x] + 1) 34 | { 35 | answer[x - 1] = answer[x] + 1; 36 | Q.push_back(x - 1); 37 | } 38 | if(x + 1 <= no_of_elements && answer[x + 1] > answer[x] + 1) 39 | { 40 | answer[x + 1] = answer[x] + 1; 41 | Q.push_back(x + 1); 42 | } 43 | } 44 | 45 | cout << answer[no_of_elements] << "\n"; 46 | } 47 | 48 | int main() 49 | { 50 | int no_of_tests; 51 | cin >> no_of_tests; 52 | 53 | while(no_of_tests--) 54 | solve(); 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Contests/June Easy 2019/Rough Notes Link: -------------------------------------------------------------------------------- 1 | https://1drv.ms/b/s!AoiWcyvzkQXVhz01xNbPW4LkbV-n 2 | -------------------------------------------------------------------------------- /Contests/June Easy 2021/Explanations/Multiple of 3 Explanation.txt: -------------------------------------------------------------------------------- 1 | An integer is a multiple of 3 if it's sum of digits = 0 (mod 3) 2 | We have to add 0, 1, 2 to make an integer a multiple of 3 3 | 4 | If n = 0 (mod 3), we will do nothing. 5 | Otherwise, we will insert either 1 or 2 in the first position where n[i] > {1, 2} from the left. 6 | This is the lexicographically smallest string. 7 | 8 | ----- 9 | 10 | void solve() 11 | { 12 | int n; 13 | cin >> n; 14 | 15 | if(n%3 == 0) 16 | { 17 | cout << n << "\n"; 18 | 19 | return; 20 | } 21 | 22 | int extra_digit = (3 - n%3); 23 | 24 | vector digits; 25 | while(n > 0) 26 | { 27 | digits.push_back(n%10); 28 | 29 | n /= 10; 30 | } 31 | reverse(digits.begin(), digits.end()); 32 | 33 | int position = digits.size(); 34 | for(int i = 0; i < digits.size(); i++) 35 | { 36 | if(digits[i] > extra_digit) 37 | { 38 | position = i; 39 | break; 40 | } 41 | } 42 | 43 | for(int i = 0; i < position; i++) 44 | { 45 | cout << digits[i]; 46 | } 47 | cout << extra_digit; 48 | for(int i = position; i < digits.size(); i++) 49 | { 50 | cout << digits[i]; 51 | } 52 | 53 | cout << "\n"; 54 | } 55 | -------------------------------------------------------------------------------- /Contests/June Easy 2021/Explanations/XOR Subsequences Explanation.txt: -------------------------------------------------------------------------------- 1 | If XOR(X, Y) = 0 => X = Y 2 | 3 | We have to look for the largest sequence of distinct elements 4 | 5 | ----- 6 | 7 | void solve() 8 | { 9 | int no_of_elements; 10 | cin >> no_of_elements; 11 | 12 | vector A(no_of_elements + 1); 13 | for(int i = 1; i <= no_of_elements; i++) 14 | { 15 | cin >> A[i]; 16 | } 17 | 18 | map taken; 19 | vector answer; 20 | for(int i = 1; i <= no_of_elements; i++) 21 | { 22 | if(!taken[A[i]]) 23 | { 24 | answer.push_back(i); 25 | taken[A[i]] = true; 26 | } 27 | } 28 | 29 | cout << answer.size() << "\n"; 30 | for(int index : answer) 31 | { 32 | cout << index << " "; 33 | } 34 | cout << "\n"; 35 | } 36 | -------------------------------------------------------------------------------- /Contests/June Easy 2021/Programs/A Sports Fest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | struct sport 8 | { 9 | int a, b, sum; 10 | 11 | sport(){} 12 | 13 | sport(int A, int B) 14 | { 15 | a = A; b = B; 16 | sum = (A + B); 17 | } 18 | 19 | const int operator <(const sport &S) 20 | { 21 | if(sum == S.sum) 22 | { 23 | return (a < S.a); 24 | } 25 | 26 | return (sum < S.sum); 27 | } 28 | }; 29 | 30 | void solve() 31 | { 32 | int no_of_elements; 33 | cin >> no_of_elements; 34 | 35 | vector A(no_of_elements + 1), B(no_of_elements + 1); 36 | for(int i = 1; i <= no_of_elements; i++) 37 | { 38 | cin >> A[i]; 39 | } 40 | 41 | for(int i = 1; i <= no_of_elements; i++) 42 | { 43 | cin >> B[i]; 44 | } 45 | 46 | vector S(no_of_elements + 1); 47 | for(int i = 1; i <= no_of_elements; i++) 48 | { 49 | S[i] = sport(A[i], B[i]); 50 | } 51 | sort(S.begin() + 1, S.end()); 52 | 53 | long long max_difference = 0; 54 | for(int turn = 1, i = no_of_elements; i >= 1; i--, turn++) 55 | { 56 | max_difference += (turn%2 == 1 ? S[i].a : -S[i].b); 57 | } 58 | 59 | cout << max_difference << "\n"; 60 | } 61 | 62 | int main() 63 | { 64 | int no_of_test_cases; 65 | cin >> no_of_test_cases; 66 | 67 | while(no_of_test_cases--) 68 | solve(); 69 | 70 | return 0; 71 | } 72 | 73 | -------------------------------------------------------------------------------- /Contests/June Easy 2021/Programs/Cheapest Items.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void solve() 8 | { 9 | int no_of_elements; 10 | cin >> no_of_elements; 11 | 12 | vector value(no_of_elements + 1), price(no_of_elements + 1); 13 | for(int i = 1; i <= no_of_elements; i++) 14 | { 15 | cin >> value[i] >> price[i]; 16 | value[i]++; 17 | } 18 | 19 | const long long oo = 1e18; 20 | vector < vector > minimum_price(no_of_elements + 1, vector (no_of_elements + 1, oo)); 21 | minimum_price[0][0] = 0; 22 | for(int i = 1; i <= no_of_elements; i++) 23 | { 24 | for(int v = 0; v <= no_of_elements; v++) 25 | { 26 | minimum_price[i][v] = minimum_price[i - 1][v]; 27 | } 28 | 29 | for(int previous_value = 0; previous_value <= no_of_elements; previous_value++) 30 | { 31 | int value_here = min(previous_value + value[i], no_of_elements); 32 | 33 | minimum_price[i][value_here] = min(minimum_price[i][value_here], 34 | price[i] + minimum_price[i - 1][previous_value]); 35 | } 36 | } 37 | 38 | cout << minimum_price[no_of_elements][no_of_elements] << "\n"; 39 | } 40 | 41 | int main() 42 | { 43 | int no_of_test_cases; 44 | cin >> no_of_test_cases; 45 | 46 | while(no_of_test_cases--) 47 | solve(); 48 | 49 | return 0; 50 | } 51 | 52 | -------------------------------------------------------------------------------- /Contests/June Easy 2021/Programs/Multiple of 3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void solve() 8 | { 9 | int n; 10 | cin >> n; 11 | 12 | if(n%3 == 0) 13 | { 14 | cout << n << "\n"; 15 | 16 | return; 17 | } 18 | 19 | int extra_digit = (3 - n%3); 20 | 21 | vector digits; 22 | while(n > 0) 23 | { 24 | digits.push_back(n%10); 25 | 26 | n /= 10; 27 | } 28 | reverse(digits.begin(), digits.end()); 29 | 30 | int position = digits.size(); 31 | for(int i = 0; i < digits.size(); i++) 32 | { 33 | if(digits[i] > extra_digit) 34 | { 35 | position = i; 36 | break; 37 | } 38 | } 39 | 40 | for(int i = 0; i < position; i++) 41 | { 42 | cout << digits[i]; 43 | } 44 | cout << extra_digit; 45 | for(int i = position; i < digits.size(); i++) 46 | { 47 | cout << digits[i]; 48 | } 49 | 50 | cout << "\n"; 51 | } 52 | 53 | int main() 54 | { 55 | int no_of_test_cases; 56 | cin >> no_of_test_cases; 57 | 58 | while(no_of_test_cases--) 59 | solve(); 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Contests/June Easy 2021/Programs/XOR Subsequences.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void solve() 8 | { 9 | int no_of_elements; 10 | cin >> no_of_elements; 11 | 12 | vector A(no_of_elements + 1); 13 | for(int i = 1; i <= no_of_elements; i++) 14 | { 15 | cin >> A[i]; 16 | } 17 | 18 | map taken; 19 | vector answer; 20 | for(int i = 1; i <= no_of_elements; i++) 21 | { 22 | if(!taken[A[i]]) 23 | { 24 | answer.push_back(i); 25 | taken[A[i]] = true; 26 | } 27 | } 28 | 29 | cout << answer.size() << "\n"; 30 | for(int index : answer) 31 | { 32 | cout << index << " "; 33 | } 34 | cout << "\n"; 35 | } 36 | 37 | int main() 38 | { 39 | int no_of_test_cases; 40 | cin >> no_of_test_cases; 41 | 42 | while(no_of_test_cases--) 43 | solve(); 44 | 45 | return 0; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /Contests/June Easy 2023/Explanations/Alice and Apple Explanation.txt: -------------------------------------------------------------------------------- 1 | Let us look at the prime factorisation of A[i] 2 | 3 | A[i] = 2^a 3^b 5^c ... 4 | 5 | Notice that our operations can only change the exponents of a and b and the remaining is invariant. 6 | 7 | Since we can always reduce the exponent of 2 and 3, let us reduce it till both a = b = 0 8 | 9 | Let us call the 'normal' value of A[i], the value of A[i] after it is no longer divisible by 2 and 3. 10 | 11 | It is possible to make all elements equal if and only if all elements have the same normal value. 12 | 13 | ------ 14 | 15 | #include 16 | #include 17 | #include 18 | using namespace std; 19 | 20 | int normalise(int n) 21 | { 22 | if(n == 0) 23 | { 24 | return n; 25 | } 26 | 27 | while(n%2 == 0) 28 | { 29 | n /= 2; 30 | } 31 | 32 | while(n%3 == 0) 33 | { 34 | n /= 3; 35 | } 36 | 37 | return n; 38 | } 39 | 40 | void solve() 41 | { 42 | int no_of_elements; 43 | cin >> no_of_elements; 44 | 45 | set normalised_value; 46 | vector A(no_of_elements + 1); 47 | for(int i = 1; i <= no_of_elements; i++) 48 | { 49 | cin >> A[i]; 50 | 51 | normalised_value.insert(normalise(A[i])); 52 | } 53 | 54 | cout << (normalised_value.size() == 1 ? "Yes" : "No") << "\n"; 55 | } 56 | 57 | int main() 58 | { 59 | int no_of_test_cases; 60 | cin >> no_of_test_cases; 61 | 62 | while(no_of_test_cases--) 63 | solve(); 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Contests/June Easy 2023/Explanations/Largest Number Explanation.txt: -------------------------------------------------------------------------------- 1 | We can actually do it in O(K x log N) time by iterating over the deletions and deleting one character each time. 2 | 3 | I did in a brute force way where I iterated over every possible digit with (N - K) digits. 4 | 5 | I did that with the help of bitmasks 6 | 7 | ------ 8 | 9 | #include 10 | using namespace std; 11 | 12 | int is_bit_set(int n, int bit) 13 | { 14 | return ( (n&(1 << bit)) != 0 ); 15 | } 16 | 17 | int main() 18 | { 19 | string N; 20 | int no_of_deletions; 21 | cin >> N >> no_of_deletions; 22 | 23 | long long answer = 0; 24 | const int MAX_MASK = (1 << N.size()); 25 | for(int m = 0; m < MAX_MASK; m++) 26 | { 27 | if(__builtin_popcount(m) != N.size() - no_of_deletions) 28 | { 29 | continue; 30 | } 31 | 32 | long long n_here = 0; 33 | for(int bit = 0; bit < N.size(); bit++) 34 | { 35 | if(is_bit_set(m, bit)) 36 | { 37 | n_here = (n_here*10) + (N[bit] - '0'); 38 | } 39 | } 40 | 41 | answer = max(answer, n_here); 42 | } 43 | 44 | cout << answer << "\n"; 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Contests/June Easy 2023/Programs/Alice and Apple.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int normalise(int n) 7 | { 8 | if(n == 0) 9 | { 10 | return n; 11 | } 12 | 13 | while(n%2 == 0) 14 | { 15 | n /= 2; 16 | } 17 | 18 | while(n%3 == 0) 19 | { 20 | n /= 3; 21 | } 22 | 23 | return n; 24 | } 25 | 26 | void solve() 27 | { 28 | int no_of_elements; 29 | cin >> no_of_elements; 30 | 31 | set normalised_value; 32 | vector A(no_of_elements + 1); 33 | for(int i = 1; i <= no_of_elements; i++) 34 | { 35 | cin >> A[i]; 36 | 37 | normalised_value.insert(normalise(A[i])); 38 | } 39 | 40 | cout << (normalised_value.size() == 1 ? "Yes" : "No") << "\n"; 41 | } 42 | 43 | int main() 44 | { 45 | int no_of_test_cases; 46 | cin >> no_of_test_cases; 47 | 48 | while(no_of_test_cases--) 49 | solve(); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Contests/June Easy 2023/Programs/Alice and GCD.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int is_bit_set(int n, int bit) 8 | { 9 | return ( (n&(1 << bit)) != 0 ); 10 | } 11 | 12 | int main() 13 | { 14 | int no_of_elements; 15 | cin >> no_of_elements; 16 | 17 | vector A(no_of_elements + 1); 18 | for(int i = 0; i < no_of_elements; i++) 19 | { 20 | cin >> A[i]; 21 | } 22 | 23 | int max_mask = (1 << no_of_elements), no_of_rounds = no_of_elements/2; 24 | vector > max_score(max_mask, vector (no_of_rounds + 1)); 25 | for(int m = 0; m < max_mask; m++) 26 | { 27 | if(__builtin_popcount(m)%2 == 1) 28 | { 29 | continue; 30 | } 31 | 32 | int round_no = __builtin_popcount(m)/2; 33 | 34 | for(int bit_1 = 0; bit_1 < no_of_elements; bit_1++) 35 | { 36 | if(!is_bit_set(m, bit_1)) 37 | { 38 | continue; 39 | } 40 | 41 | for(int bit_2 = bit_1 + 1; bit_2 < no_of_elements; bit_2++) 42 | { 43 | if(!is_bit_set(m, bit_2)) 44 | { 45 | continue; 46 | } 47 | 48 | int previous_m = m^(1 << bit_1)^(1 << bit_2); 49 | 50 | int pair_score = round_no*(__gcd(A[bit_1], A[bit_2])); 51 | 52 | max_score[m][round_no] = max(max_score[m][round_no], 53 | pair_score + max_score[previous_m][round_no - 1]); 54 | } 55 | } 56 | } 57 | 58 | cout << max_score[max_mask - 1][no_of_rounds] << "\n"; 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Contests/June Easy 2023/Programs/Good Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | int main() 6 | { 7 | int no_of_elements; 8 | cin >> no_of_elements; 9 | 10 | vector A(no_of_elements + 1); 11 | for(int i = 1; i <= no_of_elements; i++) 12 | { 13 | cin >> A[i]; 14 | 15 | A[i] %= 2; 16 | } 17 | 18 | const int MOD = 1e9 + 7; 19 | vector > no_of_sequences(no_of_elements + 1, vector (2, 0)); 20 | for(int i = 1; i <= no_of_elements; i++) 21 | { 22 | no_of_sequences[i][A[i]] = 1; 23 | 24 | for(int m = 0; m < 2; m++) 25 | { 26 | no_of_sequences[i][m] += no_of_sequences[i - 1][m]; 27 | no_of_sequences[i][m] %= MOD; 28 | 29 | int new_m = (A[i] + m)%2; 30 | no_of_sequences[i][new_m] += no_of_sequences[i - 1][m]; 31 | no_of_sequences[i][new_m] %= MOD; 32 | } 33 | } 34 | 35 | cout << no_of_sequences[no_of_elements][0] << "\n"; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Contests/June Easy 2023/Programs/Largest Number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int is_bit_set(int n, int bit) 5 | { 6 | return ( (n&(1 << bit)) != 0 ); 7 | } 8 | 9 | int main() 10 | { 11 | string N; 12 | int no_of_deletions; 13 | cin >> N >> no_of_deletions; 14 | 15 | long long answer = 0; 16 | const int MAX_MASK = (1 << N.size()); 17 | for(int m = 0; m < MAX_MASK; m++) 18 | { 19 | if(__builtin_popcount(m) != N.size() - no_of_deletions) 20 | { 21 | continue; 22 | } 23 | 24 | long long n_here = 0; 25 | for(int bit = 0; bit < N.size(); bit++) 26 | { 27 | if(is_bit_set(m, bit)) 28 | { 29 | n_here = (n_here*10) + (N[bit] - '0'); 30 | } 31 | } 32 | 33 | answer = max(answer, n_here); 34 | } 35 | 36 | cout << answer << "\n"; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Contests/June Easy 2023/Programs/Reach the Queen.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | int main() 7 | { 8 | int no_of_vertices, no_of_edges; 9 | cin >> no_of_vertices >> no_of_edges; 10 | 11 | vector indegree(no_of_vertices + 1); 12 | vector > graph(no_of_vertices + 1); 13 | for(int i = 1; i <= no_of_edges; i++) 14 | { 15 | int u, v; 16 | cin >> u >> v; 17 | 18 | graph[u].push_back(v); 19 | indegree[v]++; 20 | } 21 | 22 | queue Q; 23 | for(int i = 1; i <= no_of_vertices; i++) 24 | { 25 | if(indegree[i] == 0) 26 | { 27 | Q.push(i); 28 | } 29 | } 30 | 31 | const int MOD = 1e9 + 7; 32 | vector no_of_ways(no_of_vertices + 1); 33 | no_of_ways[1] = 1; 34 | while(!Q.empty()) 35 | { 36 | int v = Q.front(); 37 | Q.pop(); 38 | 39 | for(int child_v : graph[v]) 40 | { 41 | no_of_ways[child_v] += no_of_ways[v]; 42 | no_of_ways[child_v] %= MOD; 43 | 44 | indegree[child_v]--; 45 | if(indegree[child_v] == 0) 46 | { 47 | Q.push(child_v); 48 | } 49 | } 50 | } 51 | 52 | cout << no_of_ways[no_of_vertices] << "\n"; 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Contests/June Easy 2023/Programs/Too Chocolatey.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void solve() 7 | { 8 | int no_of_elements; 9 | cin >> no_of_elements; 10 | 11 | const int MAX_A = 1e3 + 5; 12 | vector frequency(MAX_A); 13 | vector A(no_of_elements + 1); 14 | for(int i = 1; i <= no_of_elements; i++) 15 | { 16 | cin >> A[i]; 17 | 18 | frequency[A[i]]++; 19 | } 20 | 21 | vector final_A; 22 | final_A.push_back(0); 23 | const int ALICE = 0, BOB = 1; 24 | vector > difference(MAX_A, vector (2, 0)); 25 | for(int i = 1; i < MAX_A; i++) 26 | { 27 | if(frequency[i] == 1) 28 | { 29 | difference[i][ALICE] = difference[i - 1][BOB] + i; 30 | difference[i][BOB] = difference[i - 1][ALICE] - i; 31 | } 32 | else 33 | { 34 | difference[i][ALICE] = difference[i - 1][ALICE]; 35 | difference[i][BOB] = difference[i - 1][BOB]; 36 | } 37 | } 38 | 39 | cout << (difference[MAX_A - 1][ALICE] > 0 ? "Alex" : "Bob") << "\n"; 40 | } 41 | 42 | int main() 43 | { 44 | int no_of_test_cases; 45 | cin >> no_of_test_cases; 46 | 47 | while(no_of_test_cases--) 48 | solve(); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Contests/March Easy 2019/Explanations/Operations on an Array Explanation.txt: -------------------------------------------------------------------------------- 1 | Let us see the cost of making the entire array equal to X. 2 | 3 | Each element will either be reduced to 0 or made to be equal to x. 4 | 5 | Sum(Min{A[i], abs(X - A[i])}) 6 | 7 | -------- 8 | 9 | We notice that it is always better to make the final element some element from the array. 10 | 11 | ------ 12 | 13 | We will check if the last element is each element from the array. 14 | 15 | ------- 16 | 17 | long long moves_if_equal_to(vector &A, int x) 18 | { 19 | long long moves = 0; 20 | 21 | for(int i = 0; i < A.size(); i++) 22 | { 23 | long long moves_here = min(A[i], abs(x - A[i])); 24 | 25 | moves += moves_here; 26 | } 27 | 28 | return moves; 29 | } 30 | 31 | int main() 32 | { 33 | int n; 34 | cin >> n; 35 | 36 | vector A(n); 37 | for(int i = 0; i < n; i++) 38 | cin >> A[i]; 39 | 40 | const long long oo = 1e15; 41 | long long minimum_moves = oo; 42 | for(int i = 0; i < n; i++) 43 | minimum_moves = min(minimum_moves, moves_if_equal_to(A, A[i])); 44 | 45 | cout << minimum_moves; 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Contests/March Easy 2019/Explanations/Palindromes Everywhere Explanation.txt: -------------------------------------------------------------------------------- 1 | The answer will either be a palindrome only in A, only in B, or starting in A and ending in B. 2 | 3 | --------- 4 | 5 | Here's the missing insight that makes this problem easy ... 6 | 7 | We will look for a string S, that occurs in A, such that Reverse(S) occurs in B. 8 | 9 | Suppose S has length L and it starts at i in A and ends in j at B. 10 | 11 | Then in A, S is from A[i, i + 1, ... , i + L - 1] 12 | 13 | In B, S is from B[j, j - 1, j - L + 1] 14 | 15 | Then, let P be a palindrome only in B that ends at position (j - 1). 16 | 17 | Let Q be a palindrome only in A that starts at position (i - 1). 18 | 19 | So the answer will either be - 20 | 21 | 1. (S + P + Reverse(S)) 22 | 2. (S + Q + Reverse(S)) 23 | 24 | Whichever is greater. 25 | 26 | ---------- 27 | 28 | So, we need to know the length of the largest palindrome starting at every position in A and ending at every position in B first. We do this with a 2d DP. 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /Contests/March Easy 2019/Programs/Operations on an Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | long long moves_if_equal_to(vector &A, int x) 7 | { 8 | long long moves = 0; 9 | 10 | for(int i = 0; i < A.size(); i++) 11 | { 12 | long long moves_here = min(A[i], abs(x - A[i])); 13 | 14 | moves += moves_here; 15 | } 16 | 17 | return moves; 18 | } 19 | 20 | int main() 21 | { 22 | int n; 23 | cin >> n; 24 | 25 | vector A(n); 26 | for(int i = 0; i < n; i++) 27 | cin >> A[i]; 28 | 29 | const long long oo = 1e15; 30 | long long minimum_moves = oo; 31 | for(int i = 0; i < n; i++) 32 | minimum_moves = min(minimum_moves, moves_if_equal_to(A, A[i])); 33 | 34 | cout << minimum_moves; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Contests/March Easy 2023/Explanations/Bob and Cakes Explanation.txt: -------------------------------------------------------------------------------- 1 | Calculate the number of good segments ending at each i and then add all of them. 2 | 3 | How many segments end at A[i] ? 4 | 5 | Let L be the position of the last non-positive integer. The number of segments ending at i are [i - L] 6 | 7 | [L + 1, i], [L + 2, i], .... , [i, i] 8 | 9 | ----- 10 | 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | long long segments(long long n) 16 | { 17 | return (n*(n + 1))/2; 18 | } 19 | 20 | void solve() 21 | { 22 | int no_of_elements; 23 | cin >> no_of_elements; 24 | 25 | vector A(no_of_elements + 1); 26 | for(int i = 1; i <= no_of_elements; i++) 27 | { 28 | cin >> A[i]; 29 | } 30 | 31 | long long total_segments = 0; 32 | int left = 0, right = 0; 33 | for(int i = 1; i <= no_of_elements; i++) 34 | { 35 | if(A[i] <= 0) 36 | { 37 | right = i - 1; 38 | total_segments += segments(right - left); 39 | left = i; 40 | } 41 | else if(i == no_of_elements) 42 | { 43 | right = i; 44 | total_segments += segments(right - left); 45 | } 46 | } 47 | 48 | cout << total_segments << "\n"; 49 | } 50 | 51 | int main() 52 | { 53 | int no_of_test_cases; 54 | cin >> no_of_test_cases; 55 | 56 | while(no_of_test_cases--) 57 | solve(); 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Contests/March Easy 2023/Explanations/Same AND OR Explanation.txt: -------------------------------------------------------------------------------- 1 | And and OR of a segment is only equal if every bit is equal. In other words, the integers have to be equal. 2 | We have to count the number of segments where every integer is equal. 3 | 4 | We can do this by maintaining the last unequal element to the left of every integer 5 | 6 | Last_unequal[i] = (A[i] == A[i - 1] ? last_unequal[i - 1] ? i - 1) 7 | 8 | ----- 9 | 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | int is_bit_set(int n, int bit) 15 | { 16 | return ( (n & (1 << bit)) != 0 ); 17 | } 18 | 19 | void solve() 20 | { 21 | int no_of_elements; 22 | cin >> no_of_elements; 23 | 24 | vector A(no_of_elements + 1); 25 | for(int i = 1; i <= no_of_elements; i++) 26 | { 27 | cin >> A[i]; 28 | } 29 | 30 | long long total_segments = 0; 31 | vector last_unequal(no_of_elements + 1); 32 | for(int i = 1; i <= no_of_elements; i++) 33 | { 34 | last_unequal[i] = (A[i] != A[i - 1] ? i - 1 : last_unequal[i - 1]); 35 | 36 | total_segments += (i - last_unequal[i]); 37 | } 38 | 39 | total_segments -= no_of_elements; 40 | cout << total_segments << "\n"; 41 | } 42 | 43 | int main() 44 | { 45 | int no_of_test_cases; 46 | cin >> no_of_test_cases; 47 | 48 | while(no_of_test_cases--) 49 | solve(); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Contests/March Easy 2023/Explanations/Segment Tree Explanation.txt: -------------------------------------------------------------------------------- 1 | We will modify the operation slightly. 2 | Instead of subtracting s from the array other than A[i], we will subtract the whole array by s. 3 | We will add A[i] by (x + s) 4 | 5 | ----- 6 | 7 | Keep track of the total amount subtracted so far. 8 | 9 | Update A[i] = A[i] + x + S 10 | The answer to the second query is A[y] - S 11 | 12 | ----- 13 | 14 | #include 15 | #include 16 | 17 | using namespace std; 18 | int main() 19 | { 20 | int no_of_elements; 21 | cin >> no_of_elements; 22 | 23 | vector A(no_of_elements + 1); 24 | for(int i = 0; i < no_of_elements; i++) 25 | { 26 | cin >> A[i]; 27 | } 28 | 29 | int no_of_queries; 30 | cin >> no_of_queries; 31 | 32 | long long total_subtracted = 0; 33 | for(int i = 1; i <= no_of_queries; i++) 34 | { 35 | const int UPDATE = 1, QUERY = 2; 36 | int query_type; 37 | cin >> query_type; 38 | 39 | switch(query_type) 40 | { 41 | case UPDATE: 42 | { 43 | int index, add, subtract; 44 | cin >> index >> add >> subtract; 45 | 46 | total_subtracted += subtract; 47 | add += subtract; 48 | 49 | A[index] += add; 50 | break; 51 | } 52 | case QUERY: 53 | { 54 | int index; 55 | cin >> index; 56 | 57 | cout << A[index] - total_subtracted << "\n"; 58 | break; 59 | } 60 | } 61 | } 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Contests/March Easy 2023/Programs/Bob and Cakes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | long long segments(long long n) 6 | { 7 | return (n*(n + 1))/2; 8 | } 9 | 10 | void solve() 11 | { 12 | int no_of_elements; 13 | cin >> no_of_elements; 14 | 15 | vector A(no_of_elements + 1); 16 | for(int i = 1; i <= no_of_elements; i++) 17 | { 18 | cin >> A[i]; 19 | } 20 | 21 | long long total_segments = 0; 22 | int left = 0, right = 0; 23 | for(int i = 1; i <= no_of_elements; i++) 24 | { 25 | if(A[i] <= 0) 26 | { 27 | right = i - 1; 28 | total_segments += segments(right - left); 29 | left = i; 30 | } 31 | else if(i == no_of_elements) 32 | { 33 | right = i; 34 | total_segments += segments(right - left); 35 | } 36 | } 37 | 38 | cout << total_segments << "\n"; 39 | } 40 | 41 | int main() 42 | { 43 | int no_of_test_cases; 44 | cin >> no_of_test_cases; 45 | 46 | while(no_of_test_cases--) 47 | solve(); 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Contests/March Easy 2023/Programs/Same AND OR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int is_bit_set(int n, int bit) 6 | { 7 | return ( (n & (1 << bit)) != 0 ); 8 | } 9 | 10 | void solve() 11 | { 12 | int no_of_elements; 13 | cin >> no_of_elements; 14 | 15 | vector A(no_of_elements + 1); 16 | for(int i = 1; i <= no_of_elements; i++) 17 | { 18 | cin >> A[i]; 19 | } 20 | 21 | long long total_segments = 0; 22 | vector last_unequal(no_of_elements + 1); 23 | for(int i = 1; i <= no_of_elements; i++) 24 | { 25 | last_unequal[i] = (A[i] != A[i - 1] ? i - 1 : last_unequal[i - 1]); 26 | 27 | total_segments += (i - last_unequal[i]); 28 | } 29 | 30 | total_segments -= no_of_elements; 31 | cout << total_segments << "\n"; 32 | } 33 | 34 | int main() 35 | { 36 | int no_of_test_cases; 37 | cin >> no_of_test_cases; 38 | 39 | while(no_of_test_cases--) 40 | solve(); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Contests/March Easy 2023/Programs/Segment Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | int main() 6 | { 7 | int no_of_elements; 8 | cin >> no_of_elements; 9 | 10 | vector A(no_of_elements + 1); 11 | for(int i = 0; i < no_of_elements; i++) 12 | { 13 | cin >> A[i]; 14 | } 15 | 16 | int no_of_queries; 17 | cin >> no_of_queries; 18 | 19 | long long total_subtracted = 0; 20 | for(int i = 1; i <= no_of_queries; i++) 21 | { 22 | const int UPDATE = 1, QUERY = 2; 23 | int query_type; 24 | cin >> query_type; 25 | 26 | switch(query_type) 27 | { 28 | case UPDATE: 29 | { 30 | int index, add, subtract; 31 | cin >> index >> add >> subtract; 32 | 33 | total_subtracted += subtract; 34 | add += subtract; 35 | 36 | A[index] += add; 37 | break; 38 | } 39 | case QUERY: 40 | { 41 | int index; 42 | cin >> index; 43 | 44 | cout << A[index] - total_subtracted << "\n"; 45 | break; 46 | } 47 | } 48 | } 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Contests/May Circuits 2023/Explanations/Operation X Explanation.txt: -------------------------------------------------------------------------------- 1 | Let us sort the array. 2 | 3 | It is clear that some prefix of the array will have X added and some suffix will have X subtracted. 4 | 5 | Let us iterate over this prefix end. 6 | 7 | There are two candidates for the maximum and the minimum. 8 | 9 | Minimum - A[1] + X, A[i + 1] - X 10 | Maximum - A[i] + X, A[n] - X 11 | 12 | Calculate the maximum, minimum and difference for every possible prefix end. 13 | 14 | ------ 15 | 16 | int main() 17 | { 18 | int no_of_elements, x; 19 | cin >> no_of_elements >> x; 20 | 21 | vector A(no_of_elements + 1); 22 | for(int i = 1; i <= no_of_elements; i++) 23 | { 24 | cin >> A[i]; 25 | } 26 | sort(A.begin() + 1, A.end()); 27 | 28 | int answer = A[no_of_elements] - A[1]; 29 | for(int i = 1; i < no_of_elements; i++) 30 | { 31 | int prefix_end = i; 32 | int minimum = min(A[1] + x, A[prefix_end + 1] - x); 33 | int maximum = max(A[prefix_end] + x, A[no_of_elements] - x); 34 | 35 | answer = min(answer, maximum - minimum); 36 | } 37 | 38 | cout << answer << "\n"; 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Contests/May Circuits 2023/Explanations/Prime Array Explanation.txt: -------------------------------------------------------------------------------- 1 | If A[i] x A[j] x A[k] is prime, then one of them is prime while two of them are 1 2 | 3 | Let us iterate over every prime number and then count the number of triplets it is a part of. 4 | 5 | ----- 6 | 7 | void solve() 8 | { 9 | int no_of_elements; 10 | cin >> no_of_elements; 11 | 12 | vector A(no_of_elements + 1); 13 | for(int i = 1; i <= no_of_elements; i++) 14 | { 15 | cin >> A[i]; 16 | } 17 | 18 | vector prefix_ones(no_of_elements + 1); 19 | for(int i = 1; i <= no_of_elements; i++) 20 | { 21 | prefix_ones[i] = prefix_ones[i - 1] + (A[i] == 1); 22 | } 23 | 24 | vector suffix_ones(no_of_elements + 2); 25 | for(int i = no_of_elements; i >= 1; i--) 26 | { 27 | suffix_ones[i] = suffix_ones[i + 1] + (A[i] == 1); 28 | } 29 | 30 | long long answer = 0; 31 | for(int i = 1; i <= no_of_elements; i++) 32 | { 33 | if(is_prime[A[i]]) 34 | { 35 | long long starting_here = choose_2(suffix_ones[i]); 36 | long long middle_here = prefix_ones[i]*suffix_ones[i]; 37 | long long ending_here = choose_2(prefix_ones[i]); 38 | 39 | answer += starting_here + middle_here + ending_here; 40 | } 41 | } 42 | 43 | cout << answer << "\n"; 44 | } -------------------------------------------------------------------------------- /Contests/May Circuits 2023/Programs/AND Subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int is_bit_set(int n, int bit) 7 | { 8 | return ( (n&(1 << bit)) != 0 ); 9 | } 10 | 11 | int get_submask_subsequence(vector &A, int m) 12 | { 13 | int count = 0; 14 | for(int i = 1; i < A.size(); i++) 15 | { 16 | if((m&A[i]) == m) 17 | { 18 | count++; 19 | } 20 | } 21 | 22 | return count; 23 | } 24 | 25 | void solve() 26 | { 27 | int no_of_elements, X; 28 | cin >> no_of_elements >> X; 29 | 30 | vector A(no_of_elements + 1); 31 | for(int i = 1; i <= no_of_elements; i++) 32 | { 33 | cin >> A[i]; 34 | } 35 | 36 | const int NO_OF_BITS = 30; 37 | int prefix_mask = 0, largest_subsequence = get_submask_subsequence(A, X); 38 | for(int bit = NO_OF_BITS; bit >= 0; bit--) 39 | { 40 | if(!is_bit_set(X, bit)) 41 | { 42 | int larger_mask = (prefix_mask|(1 << bit)); 43 | int subsequence_here = get_submask_subsequence(A, larger_mask); 44 | 45 | largest_subsequence = max(largest_subsequence, subsequence_here); 46 | } 47 | else 48 | { 49 | prefix_mask |= (1 << bit); 50 | } 51 | } 52 | 53 | int answer = (largest_subsequence == 0 ? -1 : largest_subsequence); 54 | cout << answer << "\n"; 55 | } 56 | 57 | int main() 58 | { 59 | int no_of_test_cases; 60 | cin >> no_of_test_cases; 61 | 62 | while(no_of_test_cases--) 63 | solve(); 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Contests/May Circuits 2023/Programs/Compatibility Queries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int is_bit_set(int n, int bit) 7 | { 8 | return ( (n&(1 << bit)) != 0 ); 9 | } 10 | 11 | int main() 12 | { 13 | int no_of_elements; 14 | cin >> no_of_elements; 15 | 16 | const int NO_OF_BITS = 20; 17 | const int MAX_MASK = (1 << NO_OF_BITS); 18 | vector mask_sum(MAX_MASK, 0); 19 | vector A(no_of_elements + 1); 20 | for(int i = 1; i <= no_of_elements; i++) 21 | { 22 | cin >> A[i]; 23 | mask_sum[A[i]] += A[i]; 24 | } 25 | 26 | for(int bit = 0; bit < NO_OF_BITS; bit++) 27 | { 28 | for(int m = 0; m < MAX_MASK; m++) 29 | { 30 | if(!is_bit_set(m, bit)) 31 | { 32 | int future_mask = m|(1 << bit); 33 | mask_sum[future_mask] += mask_sum[m]; 34 | } 35 | } 36 | } 37 | 38 | int no_of_queries; 39 | cin >> no_of_queries; 40 | 41 | for(int i = 1; i <= no_of_queries; i++) 42 | { 43 | int x; 44 | cin >> x; 45 | 46 | int complement = MAX_MASK - 1 - x; 47 | cout << mask_sum[complement] << "\n"; 48 | } 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Contests/May Circuits 2023/Programs/Operation X.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 40 | 41 | -------------------------------------------------------------------------------- /Contests/May Easy 2019/Explanations/Good Numbers Explanation.txt: -------------------------------------------------------------------------------- 1 | We can do each query in O(1) time. 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | void solve() 8 | { 9 | int n; 10 | cin >> n; 11 | 12 | int answer = n/4 + (n%4 >= 2); 13 | cout << answer << endl; 14 | } 15 | 16 | int main() 17 | { 18 | int no_of_test_cases; 19 | cin >> no_of_test_cases; 20 | 21 | while(no_of_test_cases--) 22 | solve(); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Contests/May Easy 2019/Programs/Close Subsequences.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | const int MAX_N = 1e5 + 5, MOD = 1e9 + 7; 9 | 10 | int no_of_elements; 11 | cin >> no_of_elements; 12 | 13 | vector A(no_of_elements + 1); 14 | for(int i = 1; i <= no_of_elements; i++) 15 | { 16 | cin >> A[i]; 17 | } 18 | 19 | vector good_sequences_ending_at(MAX_N, 0); 20 | vector bad_sequences_ending_at(MAX_N, 0); 21 | 22 | long long good_sequences = 0, bad_sequences = 0; 23 | 24 | for(int i = 1; i <= no_of_elements; i++) 25 | { 26 | long long bad_sequences_turned_good = (bad_sequences_ending_at[A[i] - 1] + bad_sequences_ending_at[A[i]] + bad_sequences_ending_at[A[i] + 1])%MOD; 27 | 28 | long long new_good_sequences = (bad_sequences_turned_good + good_sequences)%MOD; 29 | 30 | good_sequences_ending_at[A[i]] = (good_sequences_ending_at[A[i]] + new_good_sequences)%MOD; 31 | 32 | good_sequences = (good_sequences + new_good_sequences)%MOD; 33 | 34 | long long new_bad_sequences = (bad_sequences + 1 - bad_sequences_turned_good + MOD)%MOD; 35 | 36 | bad_sequences_ending_at[A[i]] = (bad_sequences_ending_at[A[i]] + new_bad_sequences)%MOD; 37 | 38 | bad_sequences = (bad_sequences + new_bad_sequences)%MOD; 39 | } 40 | 41 | cout << good_sequences; 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Contests/May Easy 2019/Programs/Good Numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void solve() 6 | { 7 | int n; 8 | cin >> n; 9 | 10 | int answer = n/4 + (n%4 >= 2); 11 | cout << answer << endl; 12 | } 13 | 14 | int main() 15 | { 16 | int no_of_test_cases; 17 | cin >> no_of_test_cases; 18 | 19 | while(no_of_test_cases--) 20 | solve(); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Contests/May Easy 2023/Explanations/Birthday Gift Explanation.txt: -------------------------------------------------------------------------------- 1 | Add an arithmetic series of length Z to X and check if we are able to reach Y. 2 | 3 | ----- 4 | 5 | void solve() 6 | { 7 | long long x, y, z; 8 | cin >> x >> y >> z; 9 | 10 | long long need = y - x; 11 | long long max_addition = (z*1LL*(z + 1))/2; 12 | 13 | cout << (max_addition >= need ? "YES" : "NO") << "\n"; 14 | } 15 | -------------------------------------------------------------------------------- /Contests/May Easy 2023/Explanations/Edge Letters Explanation.txt: -------------------------------------------------------------------------------- 1 | There are only 26^2 pairs so let us precompute the answer for all of them. 2 | 3 | Let us answer, how many strings [A, B] are there ? 4 | 5 | Every occurrence of A can be matched to every occurence of B that occurs after it or in it's suffix. 6 | 7 | Let us keep track of the suffix frequency of every alphabet 8 | 9 | And then update answer [S[i], alpha] += suffix_frequency[alpha], for every alpha in the suffix. 10 | 11 | ----- 12 | 13 | int main() 14 | { 15 | string S; 16 | cin >> S; 17 | 18 | const int NO_OF_ALPHABETS = 26; 19 | vector suffix_frequency(NO_OF_ALPHABETS, 0); 20 | vector > answer(NO_OF_ALPHABETS, vector (NO_OF_ALPHABETS, 0)); 21 | for(int i = S.size() - 1; i >= 0; i--) 22 | { 23 | suffix_frequency[S[i] - 'a']++; 24 | 25 | int left_alpha = S[i] - 'a'; 26 | for(int right_alpha = 0; right_alpha < NO_OF_ALPHABETS; right_alpha++) 27 | { 28 | int new_segments = suffix_frequency[right_alpha]; 29 | 30 | answer[left_alpha][right_alpha] += new_segments; 31 | } 32 | } 33 | 34 | int no_of_queries; 35 | cin >> no_of_queries; 36 | 37 | for(int i = 1; i <= no_of_queries; i++) 38 | { 39 | char L, R; 40 | cin >> L >> R; 41 | 42 | int left = L - 'a', right = R - 'a'; 43 | cout << answer[left][right] << "\n"; 44 | } 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Contests/May Easy 2023/Explanations/Favourite Singer Explanation.txt: -------------------------------------------------------------------------------- 1 | Let us keep track of the frequency of every element and count the elements with the maximum frequency 2 | 3 | ------ 4 | 5 | int main() 6 | { 7 | int no_of_elements; 8 | cin >> no_of_elements; 9 | 10 | vector A(no_of_elements + 1); 11 | for(int i = 1; i <= no_of_elements; i++) 12 | { 13 | cin >> A[i]; 14 | } 15 | 16 | int max_frequency = 0; 17 | map frequency; 18 | for(int i = 1; i <= no_of_elements; i++) 19 | { 20 | frequency[A[i]]++; 21 | 22 | max_frequency = max(max_frequency, frequency[A[i]]); 23 | } 24 | 25 | int max_frequency_count = 0; 26 | for(auto it = frequency.begin(); it != frequency.end(); it++) 27 | { 28 | if(it->second == max_frequency) 29 | { 30 | max_frequency_count++; 31 | } 32 | } 33 | 34 | cout << max_frequency_count << "\n"; 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Contests/May Easy 2023/Explanations/Swapping Pairs Explanation.txt: -------------------------------------------------------------------------------- 1 | Let f(S, i) be true if it is possible to reach a sum of S using the first i elements. 2 | 3 | f(S, i) = f(S - A[i], i - 1) OR f(S - B[i], i - 1) OR f(S, i - 1) 4 | 5 | S is reachable with i elements only if (S - A[i]) or (S - B[i]) is reachable with i - 1 elements or if S is reachable on it's own with (i - 1) elements. 6 | 7 | I have saved space in my DP by only keeping track of F(S) of the current and previous element. 8 | 9 | ----- 10 | 11 | void solve() 12 | { 13 | int no_of_elements, M; 14 | cin >> no_of_elements >> M; 15 | 16 | vector A(no_of_elements + 1), B(no_of_elements + 1); 17 | for(int i = 1; i <= no_of_elements; i++) 18 | { 19 | cin >> A[i] >> B[i]; 20 | } 21 | 22 | vector reachable(M + 1, false); 23 | reachable[0] = true; 24 | for(int i = 1; i <= no_of_elements; i++) 25 | { 26 | vector reachable_here(M + 1, false); 27 | for(int m = 0; m <= M; m++) 28 | { 29 | if(m - A[i] >= 0 && reachable[m - A[i]]) 30 | { 31 | reachable_here[m] = true; 32 | } 33 | 34 | if(m - B[i] >= 0 && reachable[m - B[i]]) 35 | { 36 | reachable_here[m] = true; 37 | } 38 | } 39 | 40 | reachable = reachable_here; 41 | } 42 | 43 | cout << (reachable[M] ? "YES" : "NO") << "\n"; 44 | } 45 | -------------------------------------------------------------------------------- /Contests/May Easy 2023/Explanations/Twin String Explanation.txt: -------------------------------------------------------------------------------- 1 | A string [L, R] is good if every letter's frequency is even. 2 | In other words, the parity of the frequency of every letter is the same in [1, L - 1] and [1, R] 3 | 4 | Let us represent all prefix strings by a bitmask of length 26. 5 | The i-th bit of the bitmask represents the parity of the frequency of alphabet i. 6 | 7 | We will keep track of the frequency of the prefix masks. 8 | 9 | The number of good strings ending at i is = frequency[mask[i]] 10 | 11 | ------ 12 | 13 | void solve() 14 | { 15 | int length; 16 | string S; 17 | cin >> length >> S; 18 | 19 | map prefix_frequency; 20 | int prefix_mask = 0; 21 | prefix_frequency[0] = 1; 22 | 23 | long long answer = 0; 24 | for(int i = 0; i < S.size(); i++) 25 | { 26 | int alpha = (S[i] - 'a'); 27 | int bit = (1 << alpha); 28 | 29 | prefix_mask ^= bit; 30 | answer += prefix_frequency[prefix_mask]; 31 | 32 | prefix_frequency[prefix_mask]++; 33 | } 34 | 35 | cout << answer << "\n"; 36 | } 37 | -------------------------------------------------------------------------------- /Contests/May Easy 2023/Programs/Birthday Gift.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void solve() 5 | { 6 | long long x, y, z; 7 | cin >> x >> y >> z; 8 | 9 | long long need = y - x; 10 | long long max_addition = (z*1LL*(z + 1))/2; 11 | 12 | cout << (max_addition >= need ? "YES" : "NO") << "\n"; 13 | } 14 | 15 | int main() 16 | { 17 | int no_of_test_cases; 18 | cin >> no_of_test_cases; 19 | 20 | while(no_of_test_cases--) 21 | solve(); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Contests/May Easy 2023/Programs/Edge Letters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | int main() 6 | { 7 | string S; 8 | cin >> S; 9 | 10 | const int NO_OF_ALPHABETS = 26; 11 | vector suffix_frequency(NO_OF_ALPHABETS, 0); 12 | vector > answer(NO_OF_ALPHABETS, vector (NO_OF_ALPHABETS, 0)); 13 | for(int i = S.size() - 1; i >= 0; i--) 14 | { 15 | suffix_frequency[S[i] - 'a']++; 16 | 17 | int left_alpha = S[i] - 'a'; 18 | for(int right_alpha = 0; right_alpha < NO_OF_ALPHABETS; right_alpha++) 19 | { 20 | int new_segments = suffix_frequency[right_alpha]; 21 | 22 | answer[left_alpha][right_alpha] += new_segments; 23 | } 24 | } 25 | 26 | int no_of_queries; 27 | cin >> no_of_queries; 28 | 29 | for(int i = 1; i <= no_of_queries; i++) 30 | { 31 | char L, R; 32 | cin >> L >> R; 33 | 34 | int left = L - 'a', right = R - 'a'; 35 | cout << answer[left][right] << "\n"; 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Contests/May Easy 2023/Programs/Favourite Singer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int no_of_elements; 10 | cin >> no_of_elements; 11 | 12 | vector A(no_of_elements + 1); 13 | for(int i = 1; i <= no_of_elements; i++) 14 | { 15 | cin >> A[i]; 16 | } 17 | 18 | int max_frequency = 0; 19 | map frequency; 20 | for(int i = 1; i <= no_of_elements; i++) 21 | { 22 | frequency[A[i]]++; 23 | 24 | max_frequency = max(max_frequency, frequency[A[i]]); 25 | } 26 | 27 | int max_frequency_count = 0; 28 | for(auto it = frequency.begin(); it != frequency.end(); it++) 29 | { 30 | if(it->second == max_frequency) 31 | { 32 | max_frequency_count++; 33 | } 34 | } 35 | 36 | cout << max_frequency_count << "\n"; 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Contests/May Easy 2023/Programs/Swapping Pairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void solve() 6 | { 7 | int no_of_elements, M; 8 | cin >> no_of_elements >> M; 9 | 10 | vector A(no_of_elements + 1), B(no_of_elements + 1); 11 | for(int i = 1; i <= no_of_elements; i++) 12 | { 13 | cin >> A[i] >> B[i]; 14 | } 15 | 16 | vector reachable(M + 1, false); 17 | reachable[0] = true; 18 | for(int i = 1; i <= no_of_elements; i++) 19 | { 20 | vector reachable_here(M + 1, false); 21 | for(int m = 0; m <= M; m++) 22 | { 23 | if(m - A[i] >= 0 && reachable[m - A[i]]) 24 | { 25 | reachable_here[m] = true; //cout << m << " is reachable from " << A[i] << "\n"; 26 | } 27 | 28 | if(m - B[i] >= 0 && reachable[m - B[i]]) 29 | { 30 | reachable_here[m] = true; //cout << m << " is reachable from " << B[i] << "\n"; 31 | } 32 | } 33 | 34 | reachable = reachable_here; 35 | } 36 | 37 | cout << (reachable[M] ? "YES" : "NO") << "\n"; 38 | } 39 | 40 | int main() 41 | { 42 | int no_of_test_cases; 43 | cin >> no_of_test_cases; 44 | 45 | while(no_of_test_cases--) 46 | solve(); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Contests/May Easy 2023/Programs/Twin Strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void solve() 7 | { 8 | int length; 9 | string S; 10 | cin >> length >> S; 11 | 12 | map prefix_frequency; 13 | int prefix_mask = 0; 14 | prefix_frequency[0] = 1; 15 | 16 | long long answer = 0; 17 | for(int i = 0; i < S.size(); i++) 18 | { 19 | int alpha = (S[i] - 'a'); 20 | int bit = (1 << alpha); 21 | 22 | prefix_mask ^= bit; 23 | answer += prefix_frequency[prefix_mask]; 24 | 25 | prefix_frequency[prefix_mask]++; 26 | } 27 | 28 | cout << answer << "\n"; 29 | } 30 | 31 | int main() 32 | { 33 | int no_of_test_cases; 34 | cin >> no_of_test_cases; 35 | 36 | while(no_of_test_cases--) 37 | solve(); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Contests/November Circuits 2022/Explanations/Four Distinct Factors Explanation.txt: -------------------------------------------------------------------------------- 1 | We can precalculate the number of factors for all numbers with a sieve 2 | 3 | We will then build a prefix sum over this array and answer each query in O(1) time 4 | 5 | ----- 6 | 7 | void sieve(int N, vector &no_of_factors) 8 | { 9 | for(long long i = 1; i < N; i++) 10 | { 11 | for(int multiple = i; multiple < N; multiple += i) 12 | { 13 | no_of_factors[multiple]++; 14 | } 15 | } 16 | } 17 | 18 | int main() 19 | { 20 | const int MAX_N = 1e6 + 5; 21 | vector no_of_factors(MAX_N); 22 | sieve(MAX_N, no_of_factors); 23 | 24 | vector good_numbers_till(MAX_N, 0); 25 | for(int i = 1; i < MAX_N; i++) 26 | { 27 | good_numbers_till[i] = good_numbers_till[i - 1] + (no_of_factors[i] == 4); 28 | } 29 | 30 | int no_of_queries; 31 | cin >> no_of_queries; 32 | 33 | for(int i = 1; i <= no_of_queries; i++) 34 | { 35 | int left, right; 36 | cin >> left >> right; 37 | cout << good_numbers_till[right] - good_numbers_till[left - 1] << "\n"; 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Contests/November Circuits 2022/Explanations/Max And Explanation.txt: -------------------------------------------------------------------------------- 1 | Let us notice that a bit will be set in the final answer only if it is set in every integer of both A and B 2 | 3 | If it is not set in even one integer, at least one integer of C will have that bit = 0 4 | 5 | This will result in it being 0 in the answer as well 6 | 7 | ----- 8 | 9 | void solve() 10 | { 11 | int no_of_elements; 12 | cin >> no_of_elements; 13 | 14 | vector A(no_of_elements + 1); 15 | for(int i = 1; i <= no_of_elements; i++) 16 | { 17 | cin >> A[i]; 18 | } 19 | 20 | vector B(no_of_elements + 1); 21 | for(int i = 1; i <= no_of_elements; i++) 22 | { 23 | cin >> B[i]; 24 | } 25 | 26 | const int NO_OF_BITS = 30; 27 | vector is_set(NO_OF_BITS, true); 28 | for(int i = 1; i <= no_of_elements; i++) 29 | { 30 | for(int bit = 0; bit < NO_OF_BITS; bit++) 31 | { 32 | is_set[bit] &= (is_bit_set(A[i], bit)); 33 | is_set[bit] &= (is_bit_set(B[i], bit)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Contests/November Circuits 2022/Programs/Distribute the Numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int possible(long long n, long long n1, long long n2, long long x, long long y) 5 | { 6 | long long x_multiples = n/x, y_multiples = n/y; 7 | long long xy_multiples = n/(x*y); 8 | 9 | long long non_multiples = n - x_multiples - y_multiples + xy_multiples; 10 | 11 | long long n1_stack = y_multiples - xy_multiples; 12 | long long n2_stack = x_multiples - xy_multiples; 13 | 14 | non_multiples -= max(0LL, n1 - n1_stack); 15 | n1_stack = max(n1, n1_stack); 16 | 17 | non_multiples -= max(0LL, n2 - n2_stack); 18 | n2_stack = max(n2, n2_stack); 19 | 20 | return (non_multiples >= 0); 21 | } 22 | 23 | void solve() 24 | { 25 | long long n1, n2, x, y; 26 | cin >> n1 >> n2 >> x >> y; 27 | 28 | long long left = 0, right = 1e15; 29 | while(right - left > 1) 30 | { 31 | long long mid = (left + right)/2; 32 | 33 | if(possible(mid, n1, n2, x, y)) 34 | { 35 | right = mid; 36 | } 37 | else 38 | { 39 | left = mid; 40 | } 41 | } 42 | 43 | cout << right << "\n"; 44 | } 45 | int main() 46 | { 47 | int no_of_test_cases; 48 | cin >> no_of_test_cases; 49 | 50 | while(no_of_test_cases--) 51 | solve(); 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /Contests/November Circuits 2022/Programs/Four Distinct Factors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void sieve(int N, vector &no_of_factors) 6 | { 7 | for(long long i = 1; i < N; i++) 8 | { 9 | for(int multiple = i; multiple < N; multiple += i) 10 | { 11 | no_of_factors[multiple]++; 12 | } 13 | } 14 | } 15 | 16 | int main() 17 | { 18 | const int MAX_N = 1e6 + 5; 19 | vector no_of_factors(MAX_N); 20 | sieve(MAX_N, no_of_factors); 21 | 22 | vector good_numbers_till(MAX_N, 0); 23 | for(int i = 1; i < MAX_N; i++) 24 | { 25 | good_numbers_till[i] = good_numbers_till[i - 1] + (no_of_factors[i] == 4); 26 | } 27 | 28 | int no_of_queries; 29 | cin >> no_of_queries; 30 | 31 | for(int i = 1; i <= no_of_queries; i++) 32 | { 33 | int left, right; 34 | cin >> left >> right; 35 | cout << good_numbers_till[right] - good_numbers_till[left - 1] << "\n"; 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Contests/November Circuits 2022/Programs/MAX AND.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int is_bit_set(int n, int bit) 6 | { 7 | return ( (n&(1<< bit)) != 0 ); 8 | } 9 | 10 | void solve() 11 | { 12 | int no_of_elements; 13 | cin >> no_of_elements; 14 | 15 | vector A(no_of_elements + 1); 16 | for(int i = 1; i <= no_of_elements; i++) 17 | { 18 | cin >> A[i]; 19 | } 20 | 21 | vector B(no_of_elements + 1); 22 | for(int i = 1; i <= no_of_elements; i++) 23 | { 24 | cin >> B[i]; 25 | } 26 | 27 | const int NO_OF_BITS = 30; 28 | vector is_set(NO_OF_BITS, true); 29 | for(int i = 1; i <= no_of_elements; i++) 30 | { 31 | for(int bit = 0; bit < NO_OF_BITS; bit++) 32 | { 33 | is_set[bit] &= (is_bit_set(A[i], bit)); 34 | is_set[bit] &= (is_bit_set(B[i], bit)); 35 | } 36 | } 37 | 38 | int answer = 0; 39 | for(int bit = 0; bit < NO_OF_BITS; bit++) 40 | { 41 | if(is_set[bit]) 42 | { 43 | answer |= (1 << bit); 44 | } 45 | } 46 | 47 | cout << answer << "\n"; 48 | } 49 | 50 | int main() 51 | { 52 | int no_of_test_cases; 53 | cin >> no_of_test_cases; 54 | 55 | while(no_of_test_cases--) 56 | solve(); 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Contests/November Easy 2019/Explanations/Determining Numbers Explanation.txt: -------------------------------------------------------------------------------- 1 | Keep track of the frequency of every integer. 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #define all(v) (v).begin(), (v).end() 8 | using namespace std; 9 | 10 | int main() 11 | { 12 | int no_of_elements; 13 | cin >> no_of_elements; 14 | 15 | const int MAX = 1e6 + 5; 16 | vector A(no_of_elements + 1, 0); 17 | vector frequency(MAX + 1, 0); 18 | for(int i = 1; i <= no_of_elements; i++) 19 | { 20 | cin >> A[i]; 21 | 22 | frequency[A[i]]++; 23 | } 24 | 25 | vector answer; 26 | for(int i = 1; i <= no_of_elements; i++) 27 | { 28 | if(frequency[A[i]] == 1) 29 | { 30 | answer.push_back(A[i]); 31 | } 32 | } 33 | 34 | sort(all(answer)); 35 | 36 | for(int i = 0; i < answer.size(); i++) 37 | { 38 | cout << answer[i] << " "; 39 | } 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Contests/November Easy 2019/Explanations/Determining Numbers XOR Solution.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MathProgrammer/Hacker-Earth/8274bb0d6899191ccb63e5285f2e1646fb077dde/Contests/November Easy 2019/Explanations/Determining Numbers XOR Solution.txt -------------------------------------------------------------------------------- /Contests/November Easy 2019/Explanations/Exchanging Money Explanation.txt: -------------------------------------------------------------------------------- 1 | Suppose the gcd of the array is G. 2 | 3 | Then, we can reach N only if G is a multiple of N. 4 | 5 | Proof - 6 | 7 | 8 | A1.w1 + A2.w2 + ... + Ak.wk = N 9 | 10 | Let g = gcd(A1, A2, ... , Ak) 11 | 12 | 13 | g.A1'.w1 + g.A2'.w2 + ... + g.Ak'.wk = N 14 | 15 | g(A1'.w1 + A2'.w2 + ... + Ak'.wk) = N 16 | 17 | ---- 18 | 19 | If N is not a multiple of g, then we cannot reach N. 20 | 21 | Bezout's Identity tells us that we can always reach N if g|N. 22 | 23 | --- 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #define all(v) (v).begin(), (v).end() 30 | using namespace std; 31 | 32 | int main() 33 | { 34 | int no_of_elements, no_of_queries; 35 | cin >> no_of_elements >> no_of_queries; 36 | 37 | int array_gcd; 38 | for(int i = 1; i <= no_of_elements; i++) 39 | { 40 | int x; 41 | cin >> x; 42 | array_gcd = (i == 1 ? x : __gcd(array_gcd, x)); 43 | } 44 | 45 | for(int i = 1; i <= no_of_queries; i++) 46 | { 47 | int P; 48 | cin >> P; 49 | cout << (P%array_gcd == 0 ? "YES\n" : "NO\n"); 50 | } 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Contests/November Easy 2019/Explanations/Zero Path Operations Explanation.txt: -------------------------------------------------------------------------------- 1 | Claim - We only need to operate on the vertices that are not leaves. 2 | 3 | Proof - If a vertex is a leaf, it will never lie in the path between two vertices (u, v) and thus never be counted. 4 | 5 | All non-leaf vertices lie on the path between two vertices and their cost must be set to 0 if it isn't already 0. 6 | 7 | ---- 8 | 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | void solve() 15 | { 16 | int no_of_vertices; 17 | cin >> no_of_vertices; 18 | 19 | vector degree(no_of_vertices + 1, 0); 20 | for(int i = 1; i <= no_of_vertices - 1; i++) 21 | { 22 | int u, v; 23 | cin >> u >> v; 24 | 25 | degree[u]++; 26 | degree[v]++; 27 | } 28 | 29 | vector cost(no_of_vertices + 1, 0); 30 | for(int i = 1; i <= no_of_vertices; i++) 31 | { 32 | cin >> cost[i]; 33 | } 34 | 35 | long long answer = 0; 36 | for(int i = 1; i <= no_of_vertices; i++) 37 | { 38 | if(degree[i] > 1 && cost[i] > 0) 39 | { 40 | answer++; 41 | } 42 | } 43 | 44 | cout << answer << "\n"; 45 | } 46 | 47 | int main() 48 | { 49 | int no_of_test_cases; 50 | cin >> no_of_test_cases; 51 | 52 | while(no_of_test_cases--) 53 | solve(); 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Contests/November Easy 2019/Programs/Determining Numbers XOR Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define all(v) (v).begin(), (v).end() 6 | using namespace std; 7 | 8 | int is_bit_set(int n, int bit) 9 | { 10 | return ( (n & (1LL << bit)) != 0); 11 | } 12 | 13 | int main() 14 | { 15 | int no_of_elements; 16 | cin >> no_of_elements; 17 | 18 | vector A(no_of_elements + 1, 0); 19 | for(int i = 1; i <= no_of_elements; i++) 20 | { 21 | cin >> A[i]; 22 | } 23 | 24 | int array_xor = 0; 25 | for(int i = 1; i <= no_of_elements; i++) 26 | { 27 | array_xor ^= A[i]; 28 | } 29 | 30 | int bit_position = 35; 31 | for(int bit = 31; bit >= 0; bit--) 32 | { 33 | if(is_bit_set(array_xor, bit)) 34 | { 35 | bit_position = bit; 36 | break; 37 | } 38 | } 39 | 40 | int u = 0, v = 0; 41 | for(int i = 1; i <= no_of_elements; i++) 42 | { 43 | if(is_bit_set(A[i], bit_position)) 44 | { 45 | v ^= A[i]; 46 | } 47 | else 48 | { 49 | u ^= A[i]; 50 | } 51 | } 52 | 53 | cout << u << " " << v << "\n"; 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Contests/November Easy 2019/Programs/Determining Numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define all(v) (v).begin(), (v).end() 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int no_of_elements; 11 | cin >> no_of_elements; 12 | 13 | const int MAX = 1e6 + 5; 14 | vector A(no_of_elements + 1, 0); 15 | vector frequency(MAX + 1, 0); 16 | for(int i = 1; i <= no_of_elements; i++) 17 | { 18 | cin >> A[i]; 19 | 20 | frequency[A[i]]++; 21 | } 22 | 23 | vector answer; 24 | for(int i = 1; i <= no_of_elements; i++) 25 | { 26 | if(frequency[A[i]] == 1) 27 | { 28 | answer.push_back(A[i]); 29 | } 30 | } 31 | 32 | sort(all(answer)); 33 | 34 | for(int i = 0; i < answer.size(); i++) 35 | { 36 | cout << answer[i] << " "; 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Contests/November Easy 2019/Programs/Exchanging Money.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define all(v) (v).begin(), (v).end() 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int no_of_elements, no_of_queries; 11 | cin >> no_of_elements >> no_of_queries; 12 | 13 | int array_gcd; 14 | for(int i = 1; i <= no_of_elements; i++) 15 | { 16 | int x; 17 | cin >> x; 18 | array_gcd = (i == 1 ? x : __gcd(array_gcd, x)); 19 | } 20 | 21 | for(int i = 1; i <= no_of_queries; i++) 22 | { 23 | int P; 24 | cin >> P; 25 | cout << (P%array_gcd == 0 ? "YES\n" : "NO\n"); 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Contests/November Easy 2019/Programs/Zero Path Operations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void solve() 7 | { 8 | int no_of_vertices; 9 | cin >> no_of_vertices; 10 | 11 | vector degree(no_of_vertices + 1, 0); 12 | for(int i = 1; i <= no_of_vertices - 1; i++) 13 | { 14 | int u, v; 15 | cin >> u >> v; 16 | 17 | degree[u]++; 18 | degree[v]++; 19 | } 20 | 21 | vector cost(no_of_vertices + 1, 0); 22 | for(int i = 1; i <= no_of_vertices; i++) 23 | { 24 | cin >> cost[i]; 25 | } 26 | 27 | long long answer = 0; 28 | for(int i = 1; i <= no_of_vertices; i++) 29 | { 30 | if(degree[i] > 1 && cost[i] > 0) 31 | { 32 | answer++; 33 | } 34 | } 35 | 36 | cout << answer << "\n"; 37 | } 38 | 39 | int main() 40 | { 41 | int no_of_test_cases; 42 | cin >> no_of_test_cases; 43 | 44 | while(no_of_test_cases--) 45 | solve(); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Contests/October Easy 2019/Explanations/Finite or Infinite Explanation.txt: -------------------------------------------------------------------------------- 1 | https://qr.ae/TW2Zwd 2 | 3 | Basically, if we have two coprime integers, then we can reach all integers after a certain limit L and the number of unreachable integers will always be finite. 4 | 5 | We can find out if there are coprime integers by computing the GCD of the entire array and checking if it is 1. 6 | 7 | ---- 8 | 9 | void solve() 10 | { 11 | int no_of_elements; 12 | cin >> no_of_elements; 13 | 14 | int array_gcd; 15 | for(int i = 1; i <= no_of_elements; i++) 16 | { 17 | int element; 18 | cin >> element; 19 | 20 | array_gcd = (i == 1 ? element : __gcd(array_gcd, element)); 21 | } 22 | 23 | cout << (array_gcd == 1 ? "FINITE\n" : "INFINITE\n"); 24 | } 25 | -------------------------------------------------------------------------------- /Contests/October Easy 2019/Programs/Finite or Infinite.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void solve() 7 | { 8 | int no_of_elements; 9 | cin >> no_of_elements; 10 | 11 | int array_gcd; 12 | for(int i = 1; i <= no_of_elements; i++) 13 | { 14 | int element; 15 | cin >> element; 16 | 17 | array_gcd = (i == 1 ? element : __gcd(array_gcd, element)); 18 | } 19 | 20 | cout << (array_gcd == 1 ? "FINITE\n" : "INFINITE\n"); 21 | } 22 | 23 | int main() 24 | { 25 | int no_of_test_cases; 26 | cin >> no_of_test_cases; 27 | 28 | while(no_of_test_cases--) 29 | solve(); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Contests/September Easy 2018/Programs/Pair Recovery.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int a_3, a_4; 8 | cin >> a_3 >> a_4; 9 | 10 | /*a1 + a2 = a3 11 | a2 + a3 = a4 12 | 13 | a1 + 2a2 = a4*/ 14 | 15 | int a_2 = a_4 - a_3; 16 | int a_1 = a_3 - a_2; 17 | cout << a_1 << " " << a_2; 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Contests/Sprint Challenge 2 - NIT Jalandhar/Explanation/Points on a Rectangle Explanation.txt: -------------------------------------------------------------------------------- 1 | 2 | For each point, check if it's x and y coordinate is in between the two corner points of the rectangle. 3 | 4 | #define min(a, b) (a < b ? a : b) 5 | #define max(a, b) (a > b ? a : b) 6 | 7 | void solve() 8 | { 9 | int x1, y1, x2, y2, n, no_of_points = 0; 10 | scanf("%d %d %d %d", &x1, &y1, &x2, &y2); 11 | 12 | scanf("%d", &n); 13 | 14 | while(n--) 15 | { 16 | int x, y; 17 | scanf("%d %d", &x, &y); 18 | 19 | no_of_points += (x > min(x1, x2) && x < max(x1, x2) && y > min(y1, y2) && y< max(y1, y2)); 20 | } 21 | printf("%d\n", no_of_points); 22 | } 23 | 24 | int main() 25 | { 26 | int t; 27 | scanf("%d", &t); 28 | 29 | while(t--) 30 | solve(); 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Contests/Sprint Challenge 2 - NIT Jalandhar/Explanation/Reduce the Fraction Explanation.txt: -------------------------------------------------------------------------------- 1 | Divide both numerator and denominator by GCD. 2 | 3 | void solve() 4 | { 5 | int A, B; 6 | scanf("%d %d", &A, &B); 7 | 8 | printf("%d/%d\n", A/gcd(A, B), B/gcd(A, B)); 9 | } -------------------------------------------------------------------------------- /Contests/Sprint Challenge 2 - NIT Jalandhar/Programs/Calculate Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int MOD = 1e9 + 7; 4 | 5 | long long power(long long x, long long power) 6 | { 7 | long long result = 1; 8 | 9 | while(power) 10 | { 11 | if(power%2 == 1) 12 | result = (result*x)%MOD; 13 | 14 | x = (x*x)%MOD; 15 | power = power >> 1; 16 | } 17 | 18 | return result; 19 | } 20 | 21 | long long inverse(long long x) 22 | { 23 | return power(x, MOD - 2); 24 | } 25 | 26 | long long cube_sum(long long n) 27 | { 28 | long long sum = ((n*(n + 1)%MOD)*inverse(2))%MOD; 29 | 30 | return (sum*sum)%MOD; 31 | } 32 | 33 | long long square_sum(long long n) 34 | { 35 | return ( ( (n*(n + 1)%MOD )* ( (2*n + 1)%MOD )%MOD )*inverse(6) )%MOD; 36 | } 37 | 38 | void solve() 39 | { 40 | long long n; 41 | scanf("%lld", &n); 42 | 43 | n %= MOD; 44 | 45 | long long answer = square_sum(n)*inverse(2) + cube_sum(n)*inverse(2); 46 | answer %= MOD; 47 | 48 | printf("%lld\n", answer); 49 | } 50 | 51 | int main() 52 | { 53 | int no_of_test_cases; 54 | scanf("%d", &no_of_test_cases); 55 | 56 | while(no_of_test_cases--) 57 | solve(); 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Contests/Sprint Challenge 2 - NIT Jalandhar/Programs/Kingdom of Dreams.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define all(v) (v).begin(), (v).end() 4 | 5 | using namespace std; 6 | 7 | void solve() 8 | { 9 | int no_of_people; 10 | scanf("%d", &no_of_people); 11 | 12 | vector cost(no_of_people + 1, 0); 13 | for(int i = 0; i < no_of_people; i++) 14 | scanf("%d", &cost[i]); 15 | 16 | sort(all(cost)); 17 | 18 | long long total_cost = 0; 19 | 20 | while(no_of_people >= 4) 21 | { 22 | total_cost += min(cost[2] + cost[1] + cost[no_of_people] + cost[2], 23 | cost[no_of_people] + cost[1] + cost[no_of_people - 1] + cost[1]); 24 | 25 | no_of_people -= 2; 26 | } 27 | 28 | if(no_of_people == 3) total_cost += cost[3] + cost[1] + cost[2]; 29 | 30 | if(no_of_people == 2) total_cost += cost[2]; 31 | 32 | if(no_of_people == 1) total_cost += cost[1]; 33 | 34 | printf("%lld\n", total_cost); 35 | } 36 | 37 | int main() 38 | { 39 | int no_of_test_cases; 40 | scanf("%d", &no_of_test_cases); 41 | 42 | while(no_of_test_cases--) 43 | solve(); 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Contests/Sprint Challenge 2 - NIT Jalandhar/Programs/Points on a Rectangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define min(a, b) (a < b ? a : b) 4 | #define max(a, b) (a > b ? a : b) 5 | 6 | void solve() 7 | { 8 | int x1, y1, x2, y2, no_of_points, no_of_internal_points = 0; 9 | scanf("%d %d %d %d", &x1, &y1, &x2, &y2); 10 | 11 | scanf("%d", &no_of_points); 12 | 13 | while(no_of_points--) 14 | { 15 | int x, y; 16 | scanf("%d %d", &x, &y); 17 | 18 | no_of_internal_points += (x > min(x1, x2) && x < max(x1, x2) && y > min(y1, y2) && y 2 | 3 | #define min(a, b) (a < b ? a : b) 4 | #define max(a, b) (a > b ? a : b) 5 | 6 | int gcd(int a, int b) 7 | { 8 | if(a == 0 || b == 0) 9 | return (a + b); 10 | 11 | return gcd(min(a, b), max(a, b)%min(a, b)); 12 | } 13 | 14 | void solve() 15 | { 16 | int A, B; 17 | scanf("%d %d", &A, &B); 18 | 19 | printf("%d/%d\n", A/gcd(A, B), B/gcd(A, B)); 20 | } 21 | 22 | int main() 23 | { 24 | int no_of_test_cases; 25 | scanf("%d", &no_of_test_cases); 26 | 27 | while(no_of_test_cases--) 28 | solve(); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Contests/TPC v2.0 Online Round/Explanations/Bee Explanation.txt: -------------------------------------------------------------------------------- 1 | The answer is ceil(Sum/N) 2 | 3 | ----- 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | long long total_sum, no_of_elements; 12 | cin >> total_sum >> no_of_elements; 13 | 14 | long long sum = 0; 15 | for(int i = 1; i <= no_of_elements; i++) 16 | { 17 | int x; 18 | cin >> x; 19 | 20 | sum += x; 21 | } 22 | 23 | long long answer = (total_sum/sum) + (total_sum%sum != 0); 24 | 25 | cout << answer << "\n"; 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Contests/TPC v2.0 Online Round/Explanations/Minimum Table Size Explanation.txt: -------------------------------------------------------------------------------- 1 | Let us precompute the powers of P till 10^5 2 | 3 | There will not be more than 20 such powers. 4 | 5 | Then for every A[i], we will binary search and find the closest value of A[i] 6 | 7 | ----- 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | const long long oo = 1e6; 18 | 19 | set powers; 20 | 21 | 22 | int no_of_elements, power; 23 | cin >> no_of_elements >> power; 24 | 25 | for(long long p = power; p <= oo; p *= power) 26 | { 27 | powers.insert(p); 28 | } 29 | 30 | long long total_cost = 0; 31 | for(int i = 1; i <= no_of_elements; i++) 32 | { 33 | int x; 34 | cin >> x; 35 | 36 | auto it = upper_bound(powers.begin(), powers.end(), x - 1); 37 | 38 | total_cost += *it; 39 | } 40 | 41 | cout << total_cost << "\n"; 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Contests/TPC v2.0 Online Round/Programs/Bee.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | long long total_sum, no_of_elements; 8 | cin >> total_sum >> no_of_elements; 9 | 10 | long long sum = 0; 11 | for(int i = 1; i <= no_of_elements; i++) 12 | { 13 | int x; 14 | cin >> x; 15 | 16 | sum += x; 17 | } 18 | 19 | long long answer = (total_sum/sum) + (total_sum%sum != 0); 20 | 21 | cout << answer << "\n"; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Contests/TPC v2.0 Online Round/Programs/Corona Virus.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int length; 9 | string S; 10 | cin >> length >> S; 11 | 12 | const int MOD = 1e9 + 7; 13 | vector no_of_ways(length + 1); 14 | for(int i = 0; i < length; i++) 15 | { 16 | int ones_here = 0; 17 | 18 | for(int j = i; j >= 0 && ones_here <= 2; j--) 19 | { 20 | ones_here += (S[j] == '1'); 21 | 22 | if(ones_here == 2) 23 | { 24 | if(j >= 1) 25 | { 26 | no_of_ways[i] += no_of_ways[j - 1]; 27 | } 28 | else 29 | { 30 | no_of_ways[i]++; 31 | } 32 | } 33 | } 34 | 35 | no_of_ways[i] %= MOD; 36 | } 37 | 38 | long long answer = no_of_ways[length - 1]; 39 | 40 | if(no_of_ways[length - 1] == 0) 41 | { 42 | answer = -1; 43 | } 44 | 45 | cout << answer << "\n"; 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Contests/TPC v2.0 Online Round/Programs/Minimum Table Size.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | const long long oo = 1e6; 10 | 11 | set powers; 12 | 13 | 14 | int no_of_elements, power; 15 | cin >> no_of_elements >> power; 16 | 17 | for(long long p = power; p <= oo; p *= power) 18 | { 19 | powers.insert(p); 20 | } 21 | 22 | long long total_cost = 0; 23 | for(int i = 1; i <= no_of_elements; i++) 24 | { 25 | int x; 26 | cin >> x; 27 | 28 | auto it = upper_bound(powers.begin(), powers.end(), x - 1); 29 | 30 | total_cost += *it; 31 | } 32 | 33 | cout << total_cost << "\n"; 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Contests/TPC v2.0 Online Round/Programs/Sum of Powers of Three.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const long long oo = 1e18; 8 | vector powers; 9 | 10 | void precompute() 11 | { 12 | powers.push_back(1); 13 | 14 | while(powers.back() < oo/3) 15 | { 16 | powers.push_back(powers.back()*3); 17 | } 18 | } 19 | 20 | void solve() 21 | { 22 | long long n, no_of_tribits; 23 | cin >> n >> no_of_tribits; 24 | 25 | long long temp_n = n; 26 | int tribits = 0; 27 | 28 | for(int i = powers.size() - 1; i >= 0; i--) 29 | { 30 | while(temp_n >= powers[i]) 31 | { 32 | temp_n -= powers[i]; 33 | 34 | tribits++; 35 | } 36 | } 37 | 38 | if(tribits > no_of_tribits || no_of_tribits > n) 39 | { 40 | cout << "NO\n"; 41 | 42 | return; 43 | } 44 | 45 | cout << (no_of_tribits%2 == tribits%2 ? "YES\n" : "NO\n"); 46 | } 47 | 48 | int main() 49 | { 50 | precompute(); 51 | 52 | int no_of_test_cases; 53 | cin >> no_of_test_cases; 54 | 55 | while(no_of_test_cases--) 56 | solve(); 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Explanations/Explanations - 1/Can You Guess Explanation.txt: -------------------------------------------------------------------------------- 1 | This was tricky. I couldn't figure it out. 2 | 3 | I tried searching for digital sums, complement digit sums, alternate bases (8, 7, 9 and binary) ... numbers with the same binary prefix, output a composite number if 4 | inputed a composite number. I just couldn't solve it. 5 | 6 | Then, I looked at the answer and it was the sum of the proper divisors of the number ! 7 | 8 | I coded a O(root(n)) solution. 9 | 10 | Important to handle the case n = 1 and perfect square n ... Otherwise no bugs 11 | 12 | ------------------------------------------------------ 13 | 14 | int number, sum = 1; 15 | cin >> number; 16 | 17 | if(number == 1) 18 | sum = 0; 19 | 20 | for(int divisor = 2; divisor*divisor <= number; divisor++) 21 | if(number%divisor == 0) 22 | sum += (divisor*divisor == number ? divisor : divisor + number/divisor); 23 | 24 | cout << sum << "\n"; -------------------------------------------------------------------------------- /Explanations/Explanations - 1/Choosing the Judges - Explanation.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MathProgrammer/Hacker-Earth/8274bb0d6899191ccb63e5285f2e1646fb077dde/Explanations/Explanations - 1/Choosing the Judges - Explanation.txt -------------------------------------------------------------------------------- /Explanations/Explanations - 1/Closest Gift - Explanation.txt: -------------------------------------------------------------------------------- 1 | There are an infinite number of primes so we'll always have a prime after the number. 2 | We need to apply a sqrt(n) primality test on the numbers. 3 | 4 | Note - It is allowed for distance to be 0. 5 | 6 | for(distance = 0; ; distance++) 7 | { 8 | if(isPrime(number + distance)) 9 | break; 10 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 1/Dhoni and Holiday - Explanation.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MathProgrammer/Hacker-Earth/8274bb0d6899191ccb63e5285f2e1646fb077dde/Explanations/Explanations - 1/Dhoni and Holiday - Explanation.txt -------------------------------------------------------------------------------- /Explanations/Explanations - 1/Dorsplen - Explanation.txt: -------------------------------------------------------------------------------- 1 | Solve the simpler problem for 2 colours ... If there are a and b stones ... We take min(a, b) stones first and then take the remaining stones ... 2 | 3 | This is given by (a + b)/2 + (a + b)%2... In each turn we can only take two stones... 4 | 5 | If there are three piles ... First we take as many 3 as possible and then reduce it to the simpler problem ... 6 | 7 | I made the mistake of thinking first take min(a, b, c) and then take from individual piles. This is not optimal 8 | 9 | For eg, 7, 3, 2 ... WE take 3 from each - 5, 1, 0 ... If we take seperately it takes 3 + 1 = 4 moves. If we take together, then it takes 1 + 2= 3 moves 10 | 11 | ---------------------------------------------------------- 12 | 13 | min_colour = min(red, min(green, blue)); 14 | 15 | 16 | 17 | red -= min_colour; 18 | 19 | green -= min_colour; 20 | 21 | blue -= min_colour; 22 | 23 | 24 | 25 | no_of_artifacts = min_colour + (red + green + +blue)/2 + (red + green + +blue)%2; -------------------------------------------------------------------------------- /Explanations/Explanations - 1/In Love with Primes Alternate Solutions Explanation.txt: -------------------------------------------------------------------------------- 1 | This reminded me of a Codeforces which required invoking Goldbach's conjecture (which is a theorem in the given range) and use the existence without actually finding the 2 | primes. 3 | 4 | I found out what was wrong ... Goldbach conjecture is for all even numbers GREATER than 2... 5 | 6 | So, the only time Arjit wins is when weight = 2 ... Overlooked this... 7 | 8 | Fixed this and got AC. 9 | 10 | ------------------------------------------ 11 | 12 | int main() 13 | { 14 | int no_of_cakes; 15 | scanf("%d", &no_of_cakes); 16 | 17 | while(no_of_cakes--) 18 | { 19 | int weight; 20 | scanf("%d", &weight); 21 | 22 | printf(weight == 2 ? "Arjit\n": "Deepa\n"); 23 | } 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 1/Lonely Integer - Explanation.txt: -------------------------------------------------------------------------------- 1 | Consider an array of integers, , where all but one of the integers occur in pairs. In other words, every element in occurs exactly twice except for one unique element. 2 | 3 | Given , find and print the unique element. 4 | 5 | Input Format 6 | 7 | The first line contains a single integer, , denoting the number of integers in the array. 8 | The second line contains space-separated integers describing the respective values in . 9 | 10 | Constraints 11 | 12 | It is guaranteed that is an odd number. 13 | , where . 14 | Output Format 15 | 16 | Print the unique number that occurs only once in on a new line. 17 | 18 | ----------------------------------------------------------------------- 19 | 20 | Imagine all the numbers are written in their binary forms. The bits that are set in this number will be set an even number of times. 21 | 22 | For example, 5 - 101. If 5 occurs twice, the bits at the first and third position will occur atleast twice. 23 | 24 | So, when we apply bitwise XOR over all the numbers, the only bits that remain set belong to the lonely integer since it doesn't have any counterpart to 'neutralise' it. 25 | 26 | The answer is the bitwise XOR of all the numbers. -------------------------------------------------------------------------------- /Explanations/Explanations - 1/Love Triangle Explanation.txt: -------------------------------------------------------------------------------- 1 | Couldn't solve this one ... Thought it had something to do with palindromes ... 2 | 3 | But, turns out this problem requires a base conversion. lol. I had thought of the idea for another problem but didn't apply it here. 4 | Base 10 to base 9. 5 | 6 | So, how do we convert in between two number bases ? 7 | 8 | Take num%new base ... num = num/ new base ... Collect all the remainders and write them from right to left. 9 | We don't really use a string ... So multiply by 10 to move to the next position. 10 | 11 | ------------------------------------------------- 12 | 13 | long long base_9(long long num) 14 | { 15 | if(num < 9) 16 | return num; 17 | else 18 | return num%9 + 10*base_9(num/9);//Multiply by 10 to shift significant digits 19 | } 20 | 21 | int main() 22 | { 23 | long long number; 24 | 25 | while(scanf("%lld", &number) != EOF) 26 | { 27 | printf("%lld\n", base_9(number)); 28 | } 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 1/Mogu Loves Numbers - Explanation.txt: -------------------------------------------------------------------------------- 1 | 2 is the only even prime number. Numbers with 2 divisors (1 and itself) are prime. 2 | 3 | Use Sieve of Erathosthenes and then an array to keep track of sum of all primes. 4 | 5 | is_Prime[0] = is_Prime[1] = 0; 6 | for(i = 2; i*i <= SIZE; i++) 7 | if(is_Prime[i] == 1) 8 | for(multiple = 2; i*multiple <= SIZE; multiple++) 9 | is_Prime[i*multiple] = 0; 10 | 11 | 12 | sum_of_primes_till[0] = 0; 13 | for(i = 1; i <= SIZE; i++) 14 | sum_of_primes_till[i] = is_Prime[i] + sum_of_primes_till[i-1]; 15 | 16 | 17 | Then, just display the difference. 18 | 19 | if(left > right) 20 | swap(left, right); 21 | 22 | printf("%d\n", sum_of_primes_till[right] - sum_of_primes_till[left - 1]); 23 | 24 | The array was too big to use an array. Using a static array worked for 98 test cases ... Using a global array worked for all of them. -------------------------------------------------------------------------------- /Explanations/Explanations - 1/Monk Takes a Walk - Explanation.txt: -------------------------------------------------------------------------------- 1 | Today, Monk went for a walk in a garden. There are many trees in the garden and each tree has an English alphabet on it. 2 | While Monk was walking, he noticed that all trees with vowels on it are not in good state. He decided to take care of them. 3 | So, he asked you to tell him the count of such trees in the garden. 4 | Note : The following letters are vowels: 'A', 'E', 'I', 'O', 'U' ,'a','e','i','o' and 'u'. 5 | 6 | Input: 7 | The first line consists of an integer 8 | T 9 | T denoting the number of test cases. 10 | Each test case consists of only one string, each character of string denoting the alphabet (may be lowercase or uppercase) on a tree in the garden. 11 | 12 | Output: 13 | For each test case, print the count in a new line. 14 | 15 | ------------------------------------------- 16 | 17 | Simple Linear Search -------------------------------------------------------------------------------- /Explanations/Explanations - 1/Monk at the Graph Factory - Explanation.txt: -------------------------------------------------------------------------------- 1 | A tree has N-1 edges ... We're counting every edge twice ... 2 | 3 | for(int i = 1; i <= no_of_vertices; i++) 4 | { 5 | int degree_i; 6 | cin >> degree_i; 7 | 8 | no_of_edges += degree_i; 9 | } 10 | 11 | cout << (no_of_edges == 2*(no_of_vertices - 1) ? "Yes\n" : "No\n"); -------------------------------------------------------------------------------- /Explanations/Explanations - 1/Monk in the Real Estate - Explanation.txt: -------------------------------------------------------------------------------- 1 | I thought there was something better than the naive approach than maintaing a boolean vector of the maximum number of cities but since the number is small, it's okay. 2 | 3 | for(int i = 1; i <= no_of_roads; i++) 4 | { 5 | scanf("%d %d", &city_x, &city_y); 6 | visited[city_x] = visited[city_y] = true; 7 | } 8 | 9 | for(int i = 1; i < MAX; i++) 10 | if(visited[i] == true) 11 | no_of_cities++; 12 | 13 | printf("%d\n", no_of_cities); -------------------------------------------------------------------------------- /Explanations/Explanations - 1/Mystery Explanation.txt: -------------------------------------------------------------------------------- 1 | Another guess the question from the input output sample question. 2 | 3 | I couldn't solve the previous one (sum of divisors) so I was already looking for something related to divisors. 4 | 5 | Didn't take me long to figure out this is the number of divisors of a number (including 1 and itself) 6 | There is no special case of n = 1 here, but be careful while handling perfect squares. 7 | 8 | Here's the O(root (n)) solution. 9 | 10 | ----------------------------------------------- 11 | 12 | void solve() 13 | { 14 | int number, number_of_divisors = 0; 15 | cin >> number; 16 | 17 | for(int divisor = 1; divisor*divisor <= number; divisor++) 18 | if(number%divisor == 0) 19 | number_of_divisors += (divisor*divisor == number ? 1 : 2); 20 | 21 | cout << number_of_divisors << "\n"; 22 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 1/Prime Interval Explanation.txt: -------------------------------------------------------------------------------- 1 | Print all primes in between L and R. 2 | 3 | --------------------------------- 4 | 5 | Simple Sieve will do ... Precompute all the vectors till 1e4 and then print in the interval. 6 | 7 | void precompute_primes() 8 | { 9 | vector is_prime(1e4 + 11, true); 10 | 11 | for(int i = 2; i <= 1e4 + 10; i++) 12 | { 13 | if(is_prime[i]) 14 | { 15 | for(int multiple = 2*i; multiple <= 1e4 + 10; multiple +=i) 16 | { 17 | is_prime[multiple] = false; 18 | } 19 | prime.push_back(i); 20 | } 21 | 22 | } 23 | } 24 | 25 | int main() 26 | { 27 | int left, right; 28 | scanf("%d %d", &left, &right); 29 | 30 | precompute_primes(); 31 | 32 | int i = 0; 33 | while(prime[i] < left) 34 | i++; 35 | 36 | for(; prime[i] <= right; i++) 37 | { 38 | printf("%d ",prime[i]); 39 | } 40 | return 0; 41 | } 42 | 43 | Vector was giving a wrong answer in the border case ... so I just padded it and put some extra primes. After i gets incremented after the last prime ... undefined behaviour 44 | is caused. One solution is to write prime[i] != prime.back() ... But I just padded it with an extra prime so that there will only be one condition. -------------------------------------------------------------------------------- /Explanations/Explanations - 1/Sum of Primes Explanation.txt: -------------------------------------------------------------------------------- 1 | Precompute a prefix sum array ... where A[i] contains the sum of all primes till i and then print A[r] - A[l - 1]. 2 | 3 | Each Query is answered in O(1) time and the sieve takes about O(N Log log N) time and the sum array takes O(n) time. 4 | 5 | Use 64 bit data type to avoid overflow. 6 | 7 | ---------------------------------------------------------------- 8 | 9 | void precompute_prime_sum() 10 | { 11 | vector is_prime(1e6 + 1, true); 12 | is_prime[0] = is_prime[1] = false; 13 | 14 | for(int i = 2; i <= 1e6; i++) 15 | { 16 | if(is_prime[i]) 17 | { 18 | for(int j = 2*i ; j <= 1e6; j += i) 19 | { 20 | is_prime[j] = false; 21 | 22 | } 23 | } 24 | } 25 | 26 | for(int i = 2; i <= 1e6; i++) 27 | { 28 | prime_sum[i] = prime_sum[i - 1] + (is_prime[i] ? i : 0); 29 | } 30 | 31 | } 32 | 33 | int main() 34 | { 35 | int no_of_test_cases; 36 | scanf("%d", &no_of_test_cases); 37 | 38 | precompute_prime_sum(); 39 | 40 | while(no_of_test_cases--) 41 | { 42 | int left, right; 43 | scanf("%d %d", &left, &right); 44 | 45 | printf("%lld\n", prime_sum[right] - prime_sum[left - 1]); 46 | } 47 | 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 1/Xsquare and Coin Collection - Explanation.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MathProgrammer/Hacker-Earth/8274bb0d6899191ccb63e5285f2e1646fb077dde/Explanations/Explanations - 1/Xsquare and Coin Collection - Explanation.txt -------------------------------------------------------------------------------- /Explanations/Explanations - 2/ASCII value - Explanation.txt: -------------------------------------------------------------------------------- 1 | Given a character 2 | C print the ASCII value of that character. -------------------------------------------------------------------------------- /Explanations/Explanations - 2/Calculate the Power - Explanation.txt: -------------------------------------------------------------------------------- 1 | Binary Exponentiation 2 | 3 | for( ;exponent > 0; exponent = exponent >> 1) 4 | { 5 | if(exponent%2 == 1) 6 | answer = (answer*base)%MOD; 7 | 8 | base = (base*base)%MOD; 9 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 2/Confused Monk - Explanation.txt: -------------------------------------------------------------------------------- 1 | Firstly, notice that a^b (mod m) = a. a. a.... a (b times) (mod m). 2 | Each a can be replaced by it's residue mod m. So, a^b (mod m) = (a mod m)^b (mod m) 3 | 4 | for(i = 1; i <= no_of_numbers; i++) 5 | { 6 | scanf("%d",¤t_number); 7 | exponent = (i == 1 ? current_number : gcd(exponent, current_number) ); 8 | base = (base*current_number)%MOD; 9 | } 10 | 11 | Binary GCD algorithm used for GCD and Binary Exponentiation to calculate the power. -------------------------------------------------------------------------------- /Explanations/Explanations - 2/Fredo and Array Update Explanation.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------- 3 | 4 | We need to set the array to value x, such that x.n > sum ... This is ceil(sum/n). 5 | 6 | int main() 7 | { 8 | int no_of_elements, element_i; 9 | scanf("%d", &no_of_elements); 10 | 11 | int sum = 0; 12 | for(int i = 1; i <= no_of_elements; i++) 13 | { 14 | scanf("%d", &element_i); 15 | sum += element_i; 16 | } 17 | 18 | int x = sum/no_of_elements + 1 ; 19 | printf("%d\n", x); 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 2/Gas Stations - Explanation.txt: -------------------------------------------------------------------------------- 1 | Use a simple greedy algorithm. Go as far as possible with the fuel. 2 | 3 | furthest_milestone_travelled = no_of_milestones; 4 | for(i = 1; i <= no_of_milestones; i++) 5 | { 6 | scanf("%d", &petrol_at_current_milestone); 7 | fuel_available = fuel_available - petrol_at_current_milestone; 8 | if(fuel_available < 0) 9 | { 10 | furthest_milestone_travelled = i; 11 | break; 12 | } 13 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 2/Intelligent Girl - Explanation.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MathProgrammer/Hacker-Earth/8274bb0d6899191ccb63e5285f2e1646fb077dde/Explanations/Explanations - 2/Intelligent Girl - Explanation.txt -------------------------------------------------------------------------------- /Explanations/Explanations - 2/Kiljil and Sorted Array Explanation.txt: -------------------------------------------------------------------------------- 1 | You have an array. The only allowed operation is to add 1 to an element. How many operations are required to make the array strictly increasing. 2 | 3 | Keep track of the maximum till i. Change element to 1 more than the max till i or keep it as it is, whichever is higher. 4 | 5 | ---------------------------------- 6 | 7 | int main() 8 | { 9 | int no_of_numbers, number_i; 10 | scanf("%d", &no_of_numbers); 11 | 12 | int max_so_far = 0; 13 | long long added_amount = 0; 14 | for(int i = 1; i <= no_of_numbers; i++) 15 | { 16 | scanf("%d", &number_i); 17 | 18 | if(number_i <= max_so_far) 19 | { 20 | added_amount += (max_so_far + 1) - number_i; 21 | } 22 | 23 | max_so_far = max(max_so_far+1, number_i); 24 | } 25 | printf("%lld\n", added_amount); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 2/Little Shino and Common Factors Explanation.txt: -------------------------------------------------------------------------------- 1 | 2 | The number of numbers which divide both numbers is the number of factors of the gcd. 3 | 4 | ------------------------------------------------ 5 | 6 | long long no_of_factors(long long n) 7 | { 8 | int no_of_factors = 0; 9 | for(long long i = 1; i*i <= n; i++) 10 | { 11 | if(n%i == 0) 12 | { 13 | no_of_factors += (i*i == n ? 1 : 2); 14 | } 15 | } 16 | return no_of_factors; 17 | } 18 | 19 | int main() 20 | { 21 | long long a, b; 22 | scanf("%lld %lld", &a, &b); 23 | 24 | printf("%lld\n", no_of_factors(gcd(max(a, b), min(a, b) ) ) ); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 2/Maximise GCD Explanation.txt: -------------------------------------------------------------------------------- 1 | Print the size of the smallest prefix array P, that has to be removed from Array A such that the gcd of 2 | 3 | A - P is maximised. 4 | 5 | ----------------------------------------------- 6 | 7 | Firstly, gcd(a, b) <= min(a, b) 8 | 9 | And gcd(a, b, c) <= gcd(a, b) 10 | 11 | When we include a new number into the gcd of a set of numbers, then the gcd must stay the same or decrease. It cannot increase. 12 | 13 | Secondly, the largest suffix gcd belongs to the last element i.e. itself. 14 | 15 | Search for the first i from the end, such that gcd(suffix(i)) != A[n], and remove all elements from 1 to i. 16 | 17 | ------------------------------------------ 18 | 19 | int main() 20 | { 21 | int no_of_numbers; 22 | scanf("%d", &no_of_numbers); 23 | 24 | vector number(no_of_numbers + 1, 0); 25 | for(int i = 1; i <= no_of_numbers; i++) 26 | scanf("%d", &number[i]); 27 | 28 | int suffix_gcd = number[no_of_numbers], removable_prefix_length = 0; 29 | for(int i = no_of_numbers - 1; i >= 1; i--) 30 | { 31 | suffix_gcd = gcd(suffix_gcd, number[i]); 32 | 33 | if(suffix_gcd != number[no_of_numbers]) 34 | { 35 | removable_prefix_length = i; 36 | break; 37 | } 38 | } 39 | 40 | printf("%d\n", removable_prefix_length); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 2/Monk and Box of Cookies - Explanation.txt: -------------------------------------------------------------------------------- 1 | Suzie came over to Monk's place today with a box of Monk's favorite cookies! But Monk has to play a game with her in order to win the cookies. 2 | Suzie takes out 3 | N 4 | N balls from her bag, each ball having an integer on it. All the integers are 32-bit. She places all these balls in a box and closes it. 5 | The game starts by Monk choosing an integer from 0 to 31, representing a bit position. It is called as Monk's chosen bit. 6 | Post this, Suzie draws 1 balls from the box randomly and notes the integers on them. The rule is simple, Monk wins if Monk's chosen bit is set in the binary representation 7 | of the drawn ball. 8 | Help Monk choose a bit that maximizes his probability of winning the game! If there are multiple bit positions having the same probability, choose the smallest one. 9 | 10 | Input: 11 | The first line consists of an integer T. 12 | T testcases follow. The first line of each testcase consists of an integer N. 13 | In next N lines, each line will contain 1 integer denoting the integers on the ball. 14 | 15 | Output: 16 | For each testcase, print the answer in a single line. 17 | 18 | ------------------------------------------------------------------------------------------------ 19 | 20 | Maintain a 32-length vector V, 21 | where V[i] is the number of binary strings where the i-th bit is set. 22 | 23 | The answer is the smallest i for which V[i] is maximised. -------------------------------------------------------------------------------- /Explanations/Explanations - 2/Monk and his Friend - Explanation.txt: -------------------------------------------------------------------------------- 1 | Monk has a very good friend, Puchi. As weird as his name, are the games he plays. 2 | One fine day, they decided to play a game to test how diverse their choices are. Both of them choose exactly one integer each. 3 | Monk chooses an integer M and Puchi chooses an integer P. 4 | The diversity of their choices is defined as the number of bits whose status is different in the binary representation of M and P , i.e. , count of bits that are , 5 | either set in M and unset in P or set in P and unset in M. 6 | Find the answer to their game. 7 | 8 | Input: 9 | First line contains T. T test cases follow. 10 | Each test case consists of 2 space-separated integers P and M. 11 | 12 | Output: 13 | Print the answer to each test case in a new line. 14 | 15 | ---------------------------------------------------------------------------- 16 | 17 | Find the population count of (p XOR q) -------------------------------------------------------------------------------- /Explanations/Explanations - 2/Panda and Chain Reaction Explanation.txt: -------------------------------------------------------------------------------- 1 | --------------------------------------------- 2 | 3 | It is quite easy to see that the answer is X.N! ... 4 | 5 | If N > 1e6 + 3, answer = 0 6 | 7 | Else, Answer = X. N! ... Precompute factorials 8 | 9 | ------------------------------------------ 10 | 11 | void precompute_factorial(vector &factorial) 12 | { 13 | const int MOD = 1e6 + 3; 14 | for(int i = 1; i < MOD; i++) 15 | factorial[i] = (factorial[i - 1]*1LL*i)%MOD; 16 | } 17 | 18 | void solve(vector &factorial) 19 | { 20 | long long no_of_chambers, particles_in_chamber_zero; 21 | scanf("%lld %lld", &no_of_chambers, &particles_in_chamber_zero); 22 | 23 | const int MOD = 1e6 + 3; 24 | long long answer; 25 | 26 | if(no_of_chambers >= MOD) 27 | { 28 | answer = 0; 29 | } 30 | else 31 | { 32 | answer = (particles_in_chamber_zero*factorial[no_of_chambers])%MOD; 33 | } 34 | 35 | printf("%lld\n", answer); 36 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 2/Sherlock and XOR - Explanation.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MathProgrammer/Hacker-Earth/8274bb0d6899191ccb63e5285f2e1646fb077dde/Explanations/Explanations - 2/Sherlock and XOR - Explanation.txt -------------------------------------------------------------------------------- /Explanations/Explanations - 2/Tree of Many Leaves Explanation.txt: -------------------------------------------------------------------------------- 1 | This question is very interesting and a disguised form of asking what the bitwise XOR of all numbers are... 2 | 3 | Initially - 4 | 5 | XOR(a, b) XOR(c, d) XOR(e, f) XOR(g, h) 6 | 7 | XOR(a, b, c, d) XOR(e, f, g, h) 8 | 9 | XOR(a, b, c, d, e, f, g, h) - Root value. 10 | 11 | This is because XOR obeys the property - XOR(x, XOR(y, z) ) = XOR(x, y, z) 12 | 13 | --------------------------------------------------------------- 14 | 15 | void solve() 16 | { 17 | int no_of_leaves, leaf_i; 18 | scanf("%d", &no_of_leaves); 19 | 20 | int root = 0; 21 | for(int i = 1; i <= no_of_leaves; i++) 22 | { 23 | scanf("%d", &leaf_i); 24 | root = root^leaf_i; 25 | } 26 | 27 | printf("%d\n",root); 28 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 3/Binary Queries Explanation.txt: -------------------------------------------------------------------------------- 1 | To know the parity of a number, we only need to know the last bit. So, although it looks like a range query, it's not. No segment trees required for this one. 2 | 3 | int main() 4 | { 5 | int no_of_bits, no_of_queries; 6 | scanf("%d %d", &no_of_bits, &no_of_queries); 7 | 8 | vector number(no_of_bits + 1); 9 | for(int i = 1; i <= no_of_bits; i++) 10 | scanf("%d", &number[i]); 11 | 12 | while(no_of_queries--) 13 | { 14 | const int PARITY_CHECK = 0, FLIP = 1; 15 | 16 | int query_type; 17 | scanf("%d", &query_type); 18 | 19 | if(query_type == PARITY_CHECK) 20 | { 21 | int left, right; 22 | scanf("%d %d", &left, &right); 23 | 24 | printf(number[right] == 1 ? "ODD\n" : "EVEN\n"); 25 | } 26 | else if(query_type == FLIP) 27 | { 28 | int position; 29 | scanf("%d", &position); 30 | 31 | number[position] ^= 1; 32 | } 33 | } 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 3/Can you answer these queries Explanation.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | Maintain a segment tree to keep track of maximum element and a normal prefix sum array. 4 | 5 | void build(int node, int left, int right) 6 | { 7 | if(left == right) 8 | { 9 | max_tree[node] = element[left]; 10 | return; 11 | } 12 | 13 | int mid = (left + right) >> 1; 14 | 15 | build(2*node, left, mid); 16 | build(2*node + 1, mid + 1, right); 17 | 18 | max_tree[node] = max(max_tree[2*node], max_tree[2*node + 1]); 19 | } 20 | 21 | int get_max(int node, int left, int right, int query_left, int query_right) 22 | { 23 | if(query_left > right || query_right < left) 24 | return 0; 25 | 26 | if(query_left <= left && right <= query_right) 27 | return max_tree[node]; 28 | 29 | int mid = (left + right) >> 1; 30 | 31 | int left_max = get_max(2*node, left, mid, query_left, query_right); 32 | int right_max = get_max(2*node + 1, mid + 1, right, query_left, query_right); 33 | 34 | return max(left_max, right_max); 35 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 3/Contribution from Divisors Explanation.txt: -------------------------------------------------------------------------------- 1 | Precompute the answer for all N in the range. 2 | A sieve like idea can be used. It is in O(n log n) 3 | 4 | Query answered in O(1) time. 5 | 6 | ----------------------------------------------------------------- 7 | 8 | int get_odd_digit_sum(int n) 9 | { 10 | int sum = 0; 11 | 12 | while(n > 0) 13 | { 14 | int digit = n%10; 15 | sum += digit*(digit%2); 16 | 17 | n = n/10; 18 | } 19 | 20 | return sum; 21 | } 22 | 23 | void precompute(vector &answer, int LIMIT) 24 | { 25 | for(int i = 1; i <= LIMIT; i++) 26 | { 27 | int odd_digit_sum = get_odd_digit_sum(i); 28 | 29 | for(int multiple = i; multiple <= LIMIT; multiple += i) 30 | { 31 | answer[multiple] += odd_digit_sum; 32 | } 33 | } 34 | } 35 | 36 | int main() 37 | { 38 | const int LIMIT = 1e6; 39 | vector answer(LIMIT + 1, 0); 40 | precompute(answer, LIMIT); 41 | 42 | int no_of_queries; 43 | scanf("%d", &no_of_queries); 44 | 45 | while(no_of_queries--) 46 | { 47 | int n; 48 | scanf("%d", &n); 49 | 50 | printf("%d\n", answer[n]); 51 | } 52 | 53 | return 0; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /Explanations/Explanations - 3/Fredo and Sums.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------- 2 | 3 | Sort the list. 4 | 5 | The differences are least when we take the difference in between two adjacent elements, and maximum when we match elements from the ends. 6 | 7 | ---------------------------------------- 8 | 9 | void solve() 10 | { 11 | int no_of_elements; 12 | scanf("%d", &no_of_elements); 13 | 14 | vector element(no_of_elements); 15 | for(int i = 0; i < no_of_elements; i++) 16 | scanf("%d", &element[i]); 17 | 18 | sort(all(element)); 19 | 20 | long long minimum_s = 0, maximum_s = 0; 21 | for(int i = 0; i < no_of_elements; i+= 2) 22 | minimum_s += element[i + 1] - element[i]; 23 | 24 | for(int front_i = 0, back_i = no_of_elements - 1; front_i < back_i; front_i++, back_i--) 25 | maximum_s += element[back_i] - element[front_i]; 26 | 27 | printf("%lld %lld\n", minimum_s, maximum_s); 28 | } 29 | -------------------------------------------------------------------------------- /Explanations/Explanations - 3/Game of Coins Explanation.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------- 3 | 4 | If there are an odd number of coins, Alice takes the middle coin. If even, Alice takes the middle two coins. 5 | 6 | After that Alice copies Bob's moves onto the other pile and ensures there are two symmetric piles. 7 | 8 | #include 9 | 10 | 11 | int main() 12 | 13 | { 14 | int no_of_test_cases; 15 | 16 | scanf("%d", &no_of_test_cases); 17 | 18 | 19 | while(no_of_test_cases--) 20 | 21 | { 22 | 23 | int no_of_coins; 24 | 25 | scanf("%d", &no_of_coins); 26 | 27 | 28 | printf("Alice\n"); 29 | 30 | } 31 | 32 | 33 | return 0; 34 | 35 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 3/Kya Ye Wala Ho Payega Explanation.txt: -------------------------------------------------------------------------------- 1 | Precompute f(i), where f(i) is the power of 2 that divides all numbers till the i-th index. 2 | 3 | ---------------------------------------------- 4 | 5 | int power_of_2_that_divides(long long n) 6 | { 7 | int power = 0; 8 | 9 | while(n%2 == 0) 10 | { 11 | n = n/2; 12 | power++; 13 | } 14 | return power; 15 | } 16 | 17 | int main() 18 | { 19 | int no_of_elements; 20 | scanf("%d", &no_of_elements); 21 | 22 | vector sum_of_power_of_2_till(no_of_elements + 1, 0); 23 | for(int i = 1; i <= no_of_elements; i++) 24 | { 25 | long long element_i; 26 | scanf("%lld", &element_i); 27 | 28 | sum_of_power_of_2_till[i] = sum_of_power_of_2_till[i - 1] + power_of_2_that_divides(element_i); 29 | } 30 | 31 | int no_of_queries; 32 | scanf("%d", &no_of_queries); 33 | 34 | while(no_of_queries--) 35 | { 36 | int left, right; 37 | scanf("%d %d", &left, &right); 38 | 39 | printf("%d\n", sum_of_power_of_2_till[right] - sum_of_power_of_2_till[left - 1]); 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Explanations/Explanations - 3/Micro and Array Update Explanation.txt: -------------------------------------------------------------------------------- 1 | 2 | The time it takes for the entire array >= k, is the time it takes the smallest element to be >= k 3 | 4 | #include 5 | 6 | #define min(a, b) (a < b ? a : b) 7 | #define max(a, b) (a > b ? a : b) 8 | 9 | void solve() 10 | { 11 | int no_of_elements, k; 12 | scanf("%d %d", &no_of_elements, &k); 13 | 14 | const int oo = 1e9; 15 | int minimum = oo; 16 | 17 | while(no_of_elements--) 18 | { 19 | int element; 20 | scanf("%d", &element); 21 | 22 | minimum = min(minimum, element); 23 | } 24 | 25 | int answer = max(0, k - minimum); 26 | printf("%d\n", answer); 27 | } 28 | 29 | int main() 30 | { 31 | int no_of_test_cases; 32 | scanf("%d", &no_of_test_cases); 33 | 34 | while(no_of_test_cases--) 35 | solve(); 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 3/Naruto and his New Jutsu Explanation.txt: -------------------------------------------------------------------------------- 1 | 2 | Answer each test in O(root(n)) time 3 | 4 | void solve() 5 | { 6 | int n; 7 | scanf("%d", &n); 8 | 9 | int sum_of_odd_divisors = 0; 10 | 11 | while(n%2 == 0) 12 | n = n/2; 13 | 14 | for(int i = 1; i*i <= n; i++) 15 | { 16 | if(n%i == 0) 17 | { 18 | sum_of_odd_divisors += (i*i == n ? i : i + n/i); 19 | } 20 | } 21 | 22 | printf("%d\n", sum_of_odd_divisors); 23 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 3/Reduce the Fraction Explanation.txt: -------------------------------------------------------------------------------- 1 | Divide both numerator and denominator by GCD. 2 | 3 | void solve() 4 | { 5 | int A, B; 6 | scanf("%d %d", &A, &B); 7 | 8 | printf("%d/%d\n", A/gcd(A, B), B/gcd(A, B)); 9 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 3/Riya's Birthday Party Explanation.txt: -------------------------------------------------------------------------------- 1 | 2 | Writing out a few terms of the series, we can see that the numbers are the odd-indexed triangular numbers. 3 | T(N) = (2n - 1)(2n)/2 = (2n - 1).n 4 | 5 | T(n - 1) = (2n - 3).(n - 1) 6 | T(n) = (2n - 1).n 7 | T(n + 1) = (2n + 1).(n + 1) 8 | 9 | {T(n - 1) + T(n + 1)}/2 - 2 = {(2n - 1 - 2).(n - 1) + (2n - 1 + 2).(n + 1)}/2 - 1 10 | 11 | = (2n - 1)(n - 1 + n + 1)/2 - 2 + 2 12 | = (2n - 1)(2n)/2 - 2 + 2 13 | = (2n - 1)(n) 14 | 15 | We see that these terms fulfil the condition specified. 16 | 17 | ---------------------------------------- 18 | 19 | void solve() 20 | { 21 | long long n; 22 | const int mod = 1e9 + 7; 23 | 24 | scanf("%lld", &n); 25 | n %= mod; 26 | 27 | long long answer = n*(2*n - 1); 28 | answer %= mod; 29 | 30 | printf("%lld\n", answer); 31 | } 32 | 33 | int main() 34 | { 35 | int no_of_test_cases; 36 | scanf("%d", &no_of_test_cases); 37 | 38 | while(no_of_test_cases--) 39 | solve(); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Explanations/Explanations - 3/Roy and Ropes Explanation.txt: -------------------------------------------------------------------------------- 1 | 2 | Notice that the rope will burn for L seconds regardless of what the upper and lower ropes of each section are. 3 | 4 | Also notice, that at position i, both the upper and lower ropes will be lit up for (L- i) seconds at least. 5 | 6 | 1. Reduce, every upper and lower i to max(0, upper[i] - (L - i)) 7 | 2. Now, you have a bunch of independent rops of certain lengths burning together. The time it will take to burn all of them is the time it will take to burn the longest 8 | 9 | ----------------------------------------- 10 | 11 | void solve() 12 | { 13 | int length; 14 | scanf("%d", &length); 15 | 16 | vector upper(length); 17 | for(int i = 1; i < length; i++) 18 | scanf("%d", &upper[i]); 19 | 20 | vector lower(length + 1); 21 | for(int i = 1; i < length; i++) 22 | scanf("%d", &lower[i]); 23 | 24 | for(int i = length - 1; i >= 1; i--) 25 | { 26 | int remaining_length = length - i; 27 | upper[i] = max(0, upper[i] - remaining_length); 28 | lower[i] = max(0, lower[i] - remaining_length); 29 | } 30 | 31 | int extra_time = 0; 32 | for(int i = 1; i < length; i++) 33 | { 34 | extra_time = max(extra_time, upper[i]); 35 | extra_time= max(extra_time, lower[i]); 36 | } 37 | 38 | int ans = length + extra_time; 39 | printf("%d\n", ans); 40 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 3/Selection of Cities Explanation.txt: -------------------------------------------------------------------------------- 1 | 2 | --------------------------------------- 3 | 4 | There are choose(n, 1) ways of choosing 1 city. 5 | Choose(n, 2) ways of choosing 2 cities. 6 | 7 | sum[choose(n, i)] = 2^n - 1 8 | 9 | This can be done either by noticing that for each path we have two choices for every city - either we select it, or we don't. 10 | WE don't count the combination where no city is selected so we subtract 1. 11 | 12 | Another way of visualising the same thing is that we can plug in x = 1, in (1 + x)^n -1 to get the required combination sum. 13 | 14 | Use binary exponentiation to find 2^n. Don't subtract 1 directly as the answer may become negative. Instead add (mod - 1). 15 | 16 | ------------------------------------------------- 17 | long long power_mod(int x, long long power, int mod) 18 | { 19 | long long result = 1, y = x; 20 | 21 | while(power > 0) 22 | { 23 | if(power%2 == 1) 24 | result = (result*y)%mod; 25 | 26 | y = (y*y)%mod; 27 | power = power >> 1; 28 | } 29 | 30 | return result; 31 | } 32 | 33 | void solve() 34 | { 35 | long long no_of_cities; 36 | scanf("%lld", &no_of_cities); 37 | 38 | const int mod = 1e9 + 7; 39 | long long no_of_choices = power_mod(2, no_of_cities, mod) + (mod - 1); //Adding -1 40 | no_of_choices %= mod; 41 | 42 | printf("%lld\n", no_of_choices); 43 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 4/Cheapest Subarry Explanation.txt: -------------------------------------------------------------------------------- 1 | Blog Link - http://qr.ae/TUppyf 2 | 3 | Basically, the optimum answer always consists of consecutive elements. 4 | 5 | If we have A[L, ... , R] 6 | 7 | Then A[i, i + 1] will always have a smaller sum, where i is the index of the minimum element. 8 | 9 | ---------------------------- 10 | 11 | void solve() 12 | { 13 | int n; 14 | scanf("%d", &n); 15 | 16 | vector A(n); 17 | for(int i = 0; i < n; i++) scanf("%d", &A[i]); 18 | 19 | const int oo = 2e9; 20 | int answer = oo; 21 | 22 | for(int i = 0; i + 1 < n; i++) 23 | { 24 | answer = min(answer, A[i] + A[i + 1]); 25 | } 26 | 27 | printf("%d\n", answer); 28 | } 29 | -------------------------------------------------------------------------------- /Explanations/Explanations - 4/Holi and Colourful Houses Explanation.txt: -------------------------------------------------------------------------------- 1 | 2 | Keep an array, f(i) is the number of changes from House 1 to House i. 3 | 4 | Clockwise = f(r) - f(l) 5 | 6 | Anti clockwise = {f(N) - f(r)} + {f(l) - f(1)} + {House[N] != House[1]} 7 | 8 | Answer = min{Clockwise, Anti clockwise} 9 | 10 | ------------------------------------------------------------------------------ 11 | 12 | void solve() 13 | { 14 | int length, no_of_queries; 15 | cin >> length >> no_of_queries; 16 | 17 | char house[length + 1]; cin >> (house + 1); 18 | 19 | vector no_of_changes_till(length + 1, 0); 20 | for(int i = 2; i <= length; i++) 21 | { 22 | no_of_changes_till[i] = no_of_changes_till[i - 1] + (house[i] != house[i - 1]); 23 | } 24 | 25 | while(no_of_queries--) 26 | { 27 | int left, right; 28 | cin >> left >> right; 29 | 30 | if(left > right) swap(left, right); 31 | 32 | int clockwise_answer = no_of_changes_till[right] - no_of_changes_till[left]; 33 | 34 | int anti_clockwise_answer = (no_of_changes_till[length] - no_of_changes_till[right]) 35 | + (no_of_changes_till[left] - no_of_changes_till[1]) 36 | + (house[length] != house[1]); 37 | 38 | int answer = min(clockwise_answer, anti_clockwise_answer); 39 | cout << answer << "\n"; 40 | } 41 | } -------------------------------------------------------------------------------- /Explanations/Explanations - 4/Phineas and Ferb Explanation.txt: -------------------------------------------------------------------------------- 1 | 2 | For the answer we need to take XOR of the stones. For the queries, maintain prefix XOR. 3 | 4 | ---------------------------------------------------------------------------------------------- 5 | 6 | int main() 7 | { 8 | int no_of_piles; 9 | scanf("%d", &no_of_piles); 10 | 11 | vector xor_of_all_piles_till(no_of_piles + 1, 0); 12 | for(int i = 1; i <= no_of_piles; i++) 13 | { 14 | int stones_i; 15 | scanf("%d", &stones_i); 16 | 17 | xor_of_all_piles_till[i] = xor_of_all_piles_till[i - 1]^stones_i; 18 | } 19 | 20 | int no_of_queries; 21 | scanf("%d", &no_of_queries); 22 | 23 | for(int i = 1; i <= no_of_queries; i++) 24 | { 25 | int left, right; 26 | scanf("%d %d", &left, &right); 27 | 28 | int bitwise_xor_piles = xor_of_all_piles_till[right]^ xor_of_all_piles_till[left - 1]; 29 | 30 | printf(bitwise_xor_piles == 0 ? "Ferb\n" : "Phineas\n"); 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Explanations/Explanations - 4/Points on a Rectangle Explanation.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hacker-Earth --------------------------------------------------------------------------------