├── Sorting and Searching ├── 26_movie_festival_2.cpp ├── 25_sliding_cost.cpp ├── 16_reading_books.cpp ├── 08_maximum_subaray_sum.cpp ├── 01_distinct_numbers.cpp ├── 22_subarray_divisibility.cpp ├── 21_subarray_sums_2.cpp ├── 09_stick_lengths.cpp ├── 11_towers.cpp ├── 19_nearest_smaller_values.cpp ├── 15_tasks_and_deadlines.cpp ├── 20_subarray_sums_1.cpp ├── 24_sliding_median.cpp ├── 07_sum_of_two_values.cpp ├── 06_movie_festival.cpp ├── 03_ferris_wheel.cpp ├── 10_playlist.cpp ├── 04_concert_tickets.cpp ├── 12_traffic_lights.cpp ├── 17_sum_of_three_values.cpp ├── 27_maximum_subarray_sum_2.cpp ├── 05_restaurant_customers.cpp ├── 02_apartments.cpp ├── 18_sum_of_four_values.cpp ├── 23_array_divison.cpp ├── 14_factory_machines.cpp └── 13_room_allocation.cpp ├── .gitignore ├── Guide-to-Competitive-Programming-Learning-and-improving-Algorithms-through-Contests.pdf ├── String Algorithms ├── 04_finding_periods.cpp ├── 03_finding_borders.cpp ├── 05_minimal_rotation.cpp ├── 02_string_matching.cpp └── 01_word_combinations.cpp ├── Introductory Problems ├── 10_trailing_zeros.cpp ├── 02_missing_number.cpp ├── 09_bit_strings.cpp ├── 07_two_knights.cpp ├── 03_repetitions.cpp ├── 01_weird_algorithm.cpp ├── 04_increasing_array.cpp ├── 05_permutations.cpp ├── 13_creating_strings_1.cpp ├── temp.cpp ├── 12_palindrome_reorder.cpp ├── 14_apple_division.cpp ├── 11_coin_piles.cpp ├── 08_two_sets.cpp ├── 06_number_spiral.cpp └── 15_chessboard_nd_queens.cpp ├── README.md ├── Dynamic Programming ├── 01_dice_combinations.cpp ├── 05_removing_digits.cpp ├── 14_increasing_subsequence.cpp ├── 04_coin_combinations_2.cpp ├── 11_money_sums.cpp ├── 15_projects.cpp ├── 10_rectangle_cutting.cpp ├── 13_two_sets_2.cpp ├── 07_book_shop.cpp ├── 12_removal_game.cpp ├── 03_coin_combinations_1.cpp ├── 09_edit_distance.cpp ├── 06_grid_paths.cpp ├── 02_minimizing_coins.cpp └── 08_array_description.cpp └── Path Finding Algorithms ├── dijkistra_algo.cpp └── floyd_warshall_algo.cpp /Sorting and Searching/26_movie_festival_2.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | Sorting and Searching/.vscode/settings.json 3 | Dynamic Programming/.vscode/settings.json 4 | -------------------------------------------------------------------------------- /Guide-to-Competitive-Programming-Learning-and-improving-Algorithms-through-Contests.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ravi-kr-singh/CSES_Competitive_Programming/HEAD/Guide-to-Competitive-Programming-Learning-and-improving-Algorithms-through-Contests.pdf -------------------------------------------------------------------------------- /String Algorithms/04_finding_periods.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Time limit: 1.00 s Memory limit: 512 MB 3 | A period of a string is a prefix that can be used to generate the whole string by repeating the prefix. The last repetition may be partial. For example, the periods of abcabca are abc, abcabc and abcabca. 4 | 5 | Your task is to find all period lengths of a string. 6 | 7 | Input 8 | 9 | The only input line has a string of length n consisting of characters a–z. 10 | 11 | Output 12 | 13 | Print all period lengths in increasing order. 14 | 15 | Constraints 16 | 1≤n≤10^6 17 | Example 18 | 19 | Input: 20 | abcabca 21 | 22 | Output: 23 | 3 6 7 24 | */ -------------------------------------------------------------------------------- /Introductory Problems/10_trailing_zeros.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Time limit: 1.00 s Memory limit: 512 MB 3 | Your task is to calculate the number of trailing zeros in the factorial n!. 4 | 5 | For example, 20!=2432902008176640000 and it has 4 trailing zeros. 6 | 7 | Input 8 | 9 | The only input line has an integer n. 10 | 11 | Output 12 | 13 | Print the number of trailing zeros in n!. 14 | 15 | Constraints 16 | 1≤n≤10^9 17 | Example 18 | 19 | Input: 20 | 20 21 | 22 | Output: 23 | 4 24 | 25 | */ 26 | 27 | #include 28 | 29 | using namespace std; 30 | 31 | int main(){ 32 | int n; 33 | cin>>n; 34 | 35 | long long ans=0; 36 | while(n>0){ 37 | ans+=n/5; 38 | n/=5; 39 | } 40 | cout< 27 | using namespace std; 28 | 29 | int main(){ 30 | string str; 31 | cin>>str; 32 | int n = str.size(); 33 | 34 | 35 | 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## CSES Problem Set Solutions 2 | 3 | These are solutions of 200 problems mentioned on https://cses.fi/problemset/ in C++. 4 | It is a finnish competitive programming site. 5 | 6 | ### Introductory Problems 7 | 1. Weird Algorithm 8 | 2. Missing Number 9 | 3. Repetitions 10 | 4. Increasing Array 11 | 5. Permutations 12 | 6. Number Spiral 13 | 7. Two Knights 14 | 8. Two Sets 15 | 9. Bit Strings 16 | 10. Trailing Zeros 17 | 11. Coin Piles 18 | 12. Palindrome Reorder 19 | 13. Creating Strings I 20 | 14. Apple Division 21 | 15. Chessboard and Queens 22 | 16. Grid Paths 23 | 24 | ### Sorting and Searching 25 | 1. Distinct Numbers 26 | 2. Apartments 27 | 3. Ferris Wheel 28 | 4. Concert Tickets 29 | 5. Restaurant Customers 30 | 6. Movie Festival 31 | 7. Sum of Two Values 32 | 8. Maximum Subarray Sum 33 | 34 | -------------------------------------------------------------------------------- /String Algorithms/05_minimal_rotation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | A rotation of a string can be generated by moving characters one after another from beginning to end. For example, the rotations of acab are acab, caba, abac, and baca. 3 | 4 | Your task is to determine the lexicographically minimal rotation of a string. 5 | 6 | Input 7 | 8 | The only input line contains a string of length n. Each character is one of a–z. 9 | 10 | Output 11 | 12 | Print the lexicographically minimal rotation. 13 | 14 | Constraints 15 | 1≤n≤10^6 16 | Example 17 | 18 | Input: 19 | acab 20 | 21 | Output: 22 | abac 23 | */ 24 | 25 | #include 26 | #include 27 | using namespace std; 28 | 29 | 30 | int main(){ 31 | string str; 32 | cin>>str; 33 | next_permutation(str.begin(),str.end()); 34 | cout< 29 | 30 | using namespace std; 31 | 32 | 33 | int main(){ 34 | int n; 35 | cin>>n; 36 | int* arr=new int[n+1]{0}; 37 | for(int i=1;i>t; 40 | arr[t]++; 41 | } 42 | for(int i=1;i<=n;i++){ 43 | if(arr[i]==0){ 44 | cout< 31 | using namespace std; 32 | 33 | int main(){ 34 | int n; 35 | cin>>n; 36 | int* books = new int[n]; 37 | for(int i=0;i>books[i]; 39 | 40 | 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Introductory Problems/09_bit_strings.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Time limit: 1.00 s Memory limit: 512 MB 3 | Your task is to calculate the number of bit strings of length n. 4 | 5 | For example, if n=3, the correct answer is 8, because the possible bit strings are 000, 001, 010, 011, 100, 101, 110, and 111. 6 | 7 | Input 8 | 9 | The only input line has an integer n. 10 | 11 | Output 12 | 13 | Print the result modulo 10^9+7. 14 | 15 | Constraints 16 | 1≤n≤10^6 17 | Example 18 | 19 | Input: 20 | 3 21 | 22 | Output: 23 | 8 24 | 25 | */ 26 | 27 | #include 28 | using namespace std; 29 | 30 | #define Limit 1000000007 31 | 32 | long long find_ans(int n){ 33 | if(n==0) 34 | return 1; 35 | 36 | long long ans=find_ans(n/2); 37 | ans*=ans; 38 | ans%=Limit; 39 | if(n&1){ 40 | ans*=2; 41 | ans%=Limit; 42 | } 43 | return ans; 44 | } 45 | 46 | int main(){ 47 | int n; 48 | cin>>n; 49 | cout< 33 | using namespace std; 34 | 35 | int m=0; 36 | int f=0; 37 | 38 | long long number_of_ways(long long t){ 39 | long long total_ways=(t*(t-1))/2; 40 | long long ans=total_ways-(8*f); 41 | m++; 42 | f+=m; 43 | return ans; 44 | 45 | } 46 | 47 | int main(){ 48 | int n; 49 | cin>>n; 50 | cout<<0< 29 | using namespace std; 30 | 31 | int main(){ 32 | int n; 33 | cin>>n; 34 | int* arr = new int[n]; 35 | for(int i=0;i>arr[i]; 37 | 38 | long long ans = arr[0]; 39 | long long temp = arr[0]; 40 | for(int i=1;i 27 | #include 28 | using namespace std; 29 | 30 | int main(){ 31 | string sequence; 32 | getline(cin,sequence); 33 | 34 | int ans=1; 35 | int temp=1; 36 | for(int i=1;i 29 | #include 30 | using namespace std; 31 | 32 | int main(){ 33 | int n; 34 | cin>>n; 35 | int* arr = new int[n]; 36 | for(int i=0;i>arr[i]; 38 | sort(arr,arr+n); 39 | 40 | int count = 1; 41 | int i = 1; 42 | while(i 30 | #include 31 | using namespace std; 32 | 33 | int find_ways(int i,int* dp){ 34 | if(i<0) 35 | return 0; 36 | if(dp[i]!=0) 37 | return dp[i]; 38 | else{ 39 | dp[i] = 0; 40 | for(int j=1;j<=6;j++){ 41 | dp[i] += find_ways(i-j,dp); 42 | dp[i] = dp[i] % 1000000007; 43 | } 44 | return dp[i]; 45 | 46 | } 47 | } 48 | 49 | int main(){ 50 | int n; 51 | cin>>n; 52 | int* dp = new int[n+1]{1}; 53 | cout< 29 | #include 30 | #include 31 | using namespace std; 32 | 33 | int min_steps_required(int n,vector& dp){ 34 | if(dp[n]!=-1) 35 | return dp[n]; 36 | 37 | int digit = INT_MIN; 38 | for(int i=n;i>0;i/=10) 39 | digit = max(digit,i%10); 40 | 41 | dp[n] = 1+min_steps_required(n-digit,dp); 42 | return dp[n]; 43 | } 44 | 45 | int main(){ 46 | int n; 47 | cin>>n; 48 | vector dp(n+1,-1); 49 | dp[0] = 0; 50 | cout< 30 | 31 | using namespace std; 32 | 33 | void weird(long long n){ 34 | while(n!=1){ 35 | cout<>n; 50 | weird(n); 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Introductory Problems/04_increasing_array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Time limit: 1.00 s Memory limit: 512 MB 3 | You are given an array of n integers. You want to modify the array so that it is increasing, i.e., every element is at least as large as the previous element. 4 | 5 | On each turn, you may increase the value of any element by one. What is the minimum number of turns required? 6 | 7 | Input 8 | 9 | The first input line contains an integer n: the size of the array. 10 | 11 | Then, the second line contains n integers x1,x2,…,xn: the contents of the array. 12 | 13 | Output 14 | 15 | Print the minimum number of turns. 16 | 17 | Constraints 18 | 1≤n≤2⋅10^5 19 | 1≤xi≤10^9 20 | Example 21 | 22 | Input: 23 | 5 24 | 3 2 5 1 7 25 | 26 | Output: 27 | 5 28 | 29 | */ 30 | 31 | #include 32 | #include 33 | using namespace std; 34 | 35 | int main(){ 36 | int n; 37 | cin>>n; 38 | int* arr=new int[n]; 39 | for(int i=0;i>arr[i]; 41 | 42 | long long ans=0; 43 | int big=arr[0]; 44 | 45 | for(int i=1;i 29 | #include 30 | using namespace std; 31 | 32 | int main(){ 33 | int n; 34 | cin>>n; 35 | int* arr = new int[n]; 36 | for(int i=0;i>arr[i]; 38 | } 39 | map freqSum; 40 | 41 | long long curr_sum = 0; 42 | long long count = 0; 43 | 44 | for(int i=0;i 29 | #include 30 | using namespace std; 31 | 32 | int main(){ 33 | 34 | int n,x; 35 | cin>>n>>x; 36 | 37 | int* arr = new int[n]; 38 | for(int i=0;i>arr[i]; 40 | 41 | long long curr_sum = 0; 42 | long long count = 0; 43 | map freqSum; 44 | 45 | 46 | for(int i=0;i 33 | #include 34 | #include 35 | using namespace std; 36 | 37 | 38 | int main(){ 39 | int n; 40 | cin>>n; 41 | int* arr = new int[n]; 42 | for(int i=0;i>arr[i]; 44 | } 45 | 46 | nth_element(arr,arr+n/2,arr+n); // Linear Time 47 | 48 | int temp = arr[n/2]; 49 | //cout< 31 | #include 32 | 33 | using namespace std; 34 | 35 | 36 | 37 | int main(){ 38 | int n; 39 | cin>>n; 40 | 41 | multiset nums; 42 | 43 | for(int i=0;i>x; 46 | auto it = nums.upper_bound(x); 47 | if(it == nums.end()) 48 | nums.insert(x); 49 | else{ 50 | nums.erase(it); 51 | nums.insert(x); 52 | } 53 | } 54 | 55 | cout< 29 | #include 30 | #include 31 | #include 32 | using namespace std; 33 | 34 | int main(){ 35 | int n; 36 | cin>>n; 37 | vector> arr(n); 38 | stack> store; 39 | for(int i=0;i>arr[i].first; 41 | arr[i].second = i+1; 42 | } 43 | 44 | 45 | for(int i=0;i= arr[i].first ) 47 | store.pop(); 48 | if(store.empty()) 49 | cout<<"0 "; 50 | else 51 | cout< 31 | using namespace std; 32 | 33 | int main(){ 34 | int n; 35 | cin>>n; 36 | int* arr = new int[n]; 37 | for(int i=0;i>arr[i]; 39 | } 40 | int dp[n]; 41 | dp[0] = 1; 42 | int ans = 1; 43 | for(int i=1;iarr[j] && dp[j]>=dp[i]) 47 | dp[i] = dp[j] + 1; 48 | } 49 | ans = max(ans,dp[i]); 50 | } 51 | cout< 36 | 37 | using namespace std; 38 | 39 | int main(){ 40 | int n; 41 | cin>>n; 42 | if(n==1) 43 | cout< 34 | using namespace std; 35 | 36 | int distinct_ways(int* coins, int n, int sum){ 37 | int dp[sum+1]{0}; 38 | dp[0] = 1; 39 | for(int i=0;i>n>>x; 50 | int* coins = new int[n]; 51 | for(int i=0;i>coins[i]; 53 | cout< 30 | #include 31 | #include 32 | using namespace std; 33 | 34 | int main(){ 35 | int n; 36 | cin>>n; 37 | int* coins = new int[n]; 38 | for(int i=0;i>coins[i]; 40 | 41 | set answer; 42 | 43 | answer.insert(coins[0]); 44 | 45 | for(int j=1;j temp(answer.size()); 47 | int i=0; 48 | for(auto it=answer.begin();it!=answer.end();it++){ 49 | temp[i] = coins[j]+(*it); 50 | i++; 51 | } 52 | for(i=0;i 45 | #include 46 | #include 47 | using namespace std; 48 | 49 | int factorial(int n){ 50 | if(n==0 || n==1) 51 | return 1; 52 | return n*factorial(n-1); 53 | } 54 | 55 | int main(){ 56 | string str; 57 | getline(cin,str); 58 | int n=str.size(); 59 | int* freq=new int[26]{0}; 60 | for(int i=0;i1) 65 | count/=factorial(freq[i]); 66 | } 67 | sort(str.begin(),str.end()); 68 | cout< 33 | #include 34 | #include 35 | #include 36 | using namespace std; 37 | 38 | int main(){ 39 | int n; 40 | cin>>n; 41 | vector> tasks(n); 42 | for(int i=0;i>tasks[i].first>>tasks[i].second; 44 | sort(tasks.begin(),tasks.end()); // Shortest job first 45 | 46 | long long ans = 0; 47 | long long finish = 0; 48 | for(int i=0;i 29 | 30 | using namespace std; 31 | 32 | int main(){ 33 | 34 | int n,x; 35 | cin>>n>>x; 36 | 37 | int* arr = new int[n]; 38 | for(int i=0;i>arr[i]; 40 | 41 | int curr_sum = 0; 42 | int left = 0; 43 | int right = -1; 44 | int count = 0; 45 | while(left x){ 53 | curr_sum -= arr[left]; 54 | left ++; 55 | } 56 | else{ 57 | count ++; 58 | right ++; 59 | curr_sum -= arr[left]; 60 | if(right < n) 61 | curr_sum += arr[right]; 62 | left++; 63 | 64 | } 65 | } 66 | 67 | 68 | cout< 3 | 4 | using namespace std; 5 | 6 | string s = "??????R??????U???????????R??????????????LD????D?"; 7 | int g[7][7]; 8 | 9 | int f(int y, int x, int i, char m) { 10 | if (i == 48) return 1; 11 | if (g[6][0]) return 0; 12 | if (m == 'L' && (x == 0 || g[y][x-1]) && y > 0 && y < 6 && !g[y-1][x] && !g[y+1][x]) return 0; 13 | if (m == 'R' && (x == 6 || g[y][x+1]) && y > 0 && y < 6 && !g[y-1][x] && !g[y+1][x]) return 0; 14 | if (m == 'U' && (y == 0 || g[y-1][x]) && x > 0 && x < 6 && !g[y][x-1] && !g[y][x+1]) return 0; 15 | if (m == 'D' && (y == 6 || g[y+1][x]) && x > 0 && x < 6 && !g[y][x-1] && !g[y][x+1]) return 0; 16 | if (s[i] == '?') { 17 | int r[] = {-1, 0, 1, 0}; 18 | int c[] = {0, -1, 0, 1}; 19 | int k = 0; 20 | for (int j = 0; j < 4; j++) { 21 | int yy = y + r[j]; 22 | int xx = x + c[j]; 23 | if (yy < 0 || yy > 6) continue; 24 | if (xx < 0 || xx > 6) continue; 25 | if (g[yy][xx]) continue; 26 | g[yy][xx] = true; 27 | k += f(yy, xx, i + 1, "ULDR"[j]); 28 | g[yy][xx] = false; 29 | } 30 | return k; 31 | } 32 | if (s[i] == 'L') x--; 33 | else if (s[i] == 'R') x++; 34 | else if (s[i] == 'U') y--; 35 | else if (s[i] == 'D') y++; 36 | if (x < 0 || x > 6) return 0; 37 | if (y < 0 || y > 6) return 0; 38 | if (g[y][x]) return 0; 39 | g[y][x] = true; 40 | int k = f(y, x, i + 1, s[i]); 41 | g[y][x] = false; 42 | return k; 43 | } 44 | 45 | 46 | int main() { 47 | 48 | g[0][0] = true; 49 | cout << f(0, 0, 0, 0) << endl; 50 | } -------------------------------------------------------------------------------- /Sorting and Searching/24_sliding_median.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You are given an array of n integers. Your task is to calculate the median of each window of k elements, from left to right. 3 | 4 | The median is the middle element when the elements are sorted. If the number of elements is even, there are two possible medians and we assume that the median is the smaller of them. 5 | 6 | Input 7 | 8 | The first input line contains two integers n and k: the number of elements and the size of the window. 9 | 10 | Then there are n integers x1,x2,…,xn: the contents of the array. 11 | 12 | Output 13 | 14 | Print n−k+1 values: the medians. 15 | 16 | Constraints 17 | 1≤k≤n≤2⋅10^5 18 | 1≤xi≤10^9 19 | Example 20 | 21 | Input: 22 | 8 3 23 | 2 4 3 5 8 1 2 1 24 | 25 | Output: 26 | 3 4 5 5 2 1 27 | */ 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | using namespace std; 34 | 35 | 36 | 37 | int main(){ 38 | int n,k; 39 | cin>>n>>k; 40 | vector> arr(n); 41 | for(int i=0;i>arr[i].first; 43 | arr[i].second = i; 44 | } 45 | 46 | 47 | nth_element(arr.begin(),arr.begin()+(k-1)/2,arr.begin()+k); 48 | cout< temp = arr[i-k+1+(k-1)/2]; 54 | cout< 29 | #include 30 | #include 31 | #include 32 | using namespace std; 33 | 34 | int main(){ 35 | int n,x; 36 | cin>>n>>x; 37 | vector> arr(n); 38 | for(int i=1;i<=n;i++){ 39 | cin>>arr[i-1].first; 40 | arr[i-1].second = i; 41 | } 42 | sort(arr.begin(),arr.end()); 43 | 44 | int start=0; 45 | int end = n-1; 46 | bool flag = true; 47 | while(end>start){ 48 | if(arr[start].first + arr[end].first == x){ 49 | cout< x){ 54 | end--; 55 | } 56 | else 57 | start++; 58 | } 59 | 60 | if(flag) 61 | cout<<"IMPOSSIBLE"; 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Sorting and Searching/06_movie_festival.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Time limit: 1.00 s Memory limit: 512 MB 3 | In a movie festival n movies will be shown. You know the starting and ending time of each movie. What is the maximum number of movies you can watch entirely? 4 | 5 | Input 6 | 7 | The first input line has an integer n: the number of movies. 8 | 9 | After this, there are n lines that describe the movies. Each line has two integers a and b: the starting and ending times of a movie. 10 | 11 | Output 12 | 13 | Print one integer: the maximum number of movies. 14 | 15 | Constraints 16 | 1≤n≤2⋅10^5 17 | 1≤a 31 | #include 32 | #include 33 | #include 34 | 35 | using namespace std; 36 | 37 | bool compf(pair a ,pair b){ 38 | if(a.second != b.second) 39 | return a.second < b.second; 40 | return a.first > b.first; 41 | } 42 | 43 | int max_movies(vector>& timings,int& n){ 44 | sort(timings.begin(),timings.end(),compf); 45 | 46 | int count = 1; 47 | int temp = timings[0].second; 48 | for(int i=1;i= temp){ 50 | count ++ ; 51 | temp = timings[i].second; 52 | } 53 | } 54 | return count; 55 | 56 | } 57 | 58 | int main(){ 59 | int n; 60 | cin>>n; 61 | vector> timings(n); 62 | for(int i=0;i>timings[i].first>>timings[i].second; 64 | } 65 | cout< 35 | #include 36 | using namespace std; 37 | 38 | struct triplet{ 39 | int start; 40 | int end; 41 | int reward; 42 | 43 | triplet(){ 44 | 45 | } 46 | triplet(int a,int b,int c){ 47 | start = a; 48 | end = b; 49 | reward = c; 50 | } 51 | void input(int a,int b,int c){ 52 | start = a; 53 | end = b; 54 | reward = c; 55 | } 56 | 57 | }; 58 | 59 | int main(){ 60 | int n; 61 | cin>>n; 62 | 63 | /*vector projects(n); 64 | for(int i=0;i>projects[i].start>>projects[i].end>>projects[i].reward;*/ 66 | 67 | triplet temp; 68 | temp.input(1,2,3); 69 | cout< 25 | #include 26 | #include 27 | using namespace std; 28 | 29 | int main(){ 30 | int a,b; 31 | cin>>a>>b; 32 | if(a>b){ 33 | int temp = a; 34 | a = b; 35 | b = temp; 36 | } 37 | vector> dp(a+1,vector(b+1,0)); 38 | 39 | 40 | 41 | dp[1][1] = 0; 42 | 43 | 44 | for(int i=2;i<=b;i++) 45 | dp[1][i] = i-1; 46 | 47 | for(int i=2;i<=a;i++){ 48 | for(int j=i+1;j<=b;j++){ 49 | dp[i][j] = INT_MAX; 50 | for(int k=1;k 30 | #include 31 | using namespace std; 32 | 33 | int number_of_ways(int i,int sum1,vector>& dp,int& n, int& s){ 34 | 35 | if(sum1 == s) 36 | return 1; 37 | 38 | if(i > n || sum1 >s) 39 | return 0; 40 | 41 | 42 | if(dp[i][sum1]!= -1) 43 | return dp[i][sum1]; 44 | 45 | 46 | dp[i][sum1] = number_of_ways(i+1,sum1+i,dp,n,s); 47 | 48 | dp[i][sum1] += number_of_ways(i+1,sum1,dp,n,s); 49 | dp[i][sum1] %= 1000000007; 50 | 51 | return dp[i][sum1]; 52 | 53 | } 54 | 55 | int main(){ 56 | int n; 57 | cin>>n; 58 | int s = (n*(n+1))/2; 59 | if(s&1){ 60 | cout<<0; 61 | } 62 | else{ 63 | s /= 2; 64 | vector> dp(n+1,vector(s+1,-1)); 65 | long long temp = number_of_ways(1,0,dp,n,s); 66 | long long inverse = 500000004; // MMMI(Modular Multiplicative inverse) of 2 in 10^9+7 67 | long long answer = (temp * inverse) % 1000000007; 68 | cout< 25 | #include 26 | using namespace std; 27 | 28 | string find_palindrome(int* freq){ 29 | string ans; 30 | char middle_element; 31 | bool flag = true; 32 | for(int i=0;i<26;i++){ 33 | if ((freq[i]&1) && flag){ 34 | middle_element = i + 'A'; 35 | flag = false; 36 | freq[i]--; 37 | } 38 | else if (freq[i]&1) 39 | return "NO SOLUTION"; 40 | 41 | while(freq[i]>0){ 42 | ans.push_back(i+'A'); 43 | freq[i] -= 2; 44 | } 45 | 46 | } 47 | string final_ans = ans; 48 | if(!flag) 49 | final_ans.push_back(middle_element); 50 | for(int i=ans.size()-1;i>=0;i--) 51 | final_ans.push_back(ans[i]); 52 | return final_ans; 53 | } 54 | 55 | int main(){ 56 | 57 | string str; 58 | getline(cin,str); 59 | int* arr = new int[26]{0}; 60 | for(int i=0;i 26 | #include 27 | using namespace std; 28 | 29 | int main(){ 30 | string str,pattern; 31 | cin>>str>>pattern; 32 | 33 | int n = str.size(); 34 | int m = pattern.size(); 35 | 36 | 37 | int* lps = new int[m]{-1}; 38 | int j = 0; 39 | for(int i=1;i 0 ){ 62 | int temp = j; 63 | j = lps[j-1] + 1; 64 | if(temp != j) 65 | i--; 66 | } 67 | i++; 68 | } 69 | } 70 | 71 | if(j == m) 72 | count ++; 73 | 74 | cout< 31 | #include 32 | using namespace std; 33 | 34 | long long min_difference(int* arr,int size,long long sum){ 35 | int limit = pow(2,size); 36 | long long ans = sum; 37 | for(int i=1;i=0;j--){ 42 | int t = temp&1; 43 | if(t == 1){ 44 | sum1 += arr[j]; 45 | } 46 | else{ 47 | sum2 += arr[j]; 48 | } 49 | temp /= 2; 50 | } 51 | ans = min(ans,abs(sum1-sum2)); 52 | } 53 | return ans; 54 | } 55 | 56 | int main(){ 57 | int n; 58 | cin>>n; 59 | int* arr = new int[n]; 60 | long long sum = 0; 61 | for(int i=0;i>arr[i]; 63 | sum += arr[i]; 64 | } 65 | cout< 34 | #include 35 | using namespace std; 36 | 37 | int min_gondolas_required(int* childWts,int& n,int& x){ 38 | sort(childWts,childWts+n); 39 | int count = 0; 40 | int start = 0; 41 | int end = n - 1; 42 | while(end>start){ 43 | if(childWts[end]<=x){ 44 | if(childWts[end]+childWts[start]<=x){ 45 | start++; 46 | } 47 | end--; 48 | count++; 49 | } 50 | else{ 51 | end--; 52 | } 53 | } 54 | if(end == start) 55 | count++; 56 | return count; 57 | } 58 | 59 | int main(){ 60 | int n,x; 61 | cin>>n>>x; 62 | int* childWeights = new int[n]; 63 | for(int i=0;i>childWeights[i]; 65 | cout< 37 | using namespace std; 38 | 39 | int main(){ 40 | 41 | int n,x; 42 | cin>>n>>x; 43 | int* price = new int[n]; 44 | int* pages = new int[n]; 45 | 46 | for(int i=0;i>price[i]; 48 | 49 | for(int i=0;i>pages[i]; 51 | 52 | int dp[n+1][x+1]; 53 | 54 | for(int i=0;i<=n;i++){ 55 | for(int j=0;j<=x;j++){ 56 | if(j == 0 || i == 0) 57 | dp[i][j] = 0; 58 | else if(price[i-1] <= j) 59 | dp[i][j] = max(pages[i-1]+dp[i-1][j-price[i-1]],dp[i-1][j]); 60 | 61 | else 62 | dp[i][j] = dp[i-1][j]; 63 | } 64 | } 65 | 66 | cout< 36 | using namespace std; 37 | 38 | int main(){ 39 | 40 | int t; 41 | cin>>t; 42 | while(t--){ 43 | int a,b; 44 | cin>>a>>b; 45 | /* 46 | x is the total number of moves--> 2 from A and 1 from B. 47 | y is the total number of moves--> 2 from B and 1 from A. 48 | Therefore, 49 | a = 2x + y 50 | b = x + 2y 51 | On solving for x and y, 52 | x = (2a - b)/3; 53 | y = (2b - a)/3; 54 | if x and y comes out to be integer then answer is YES 55 | otherwise no; 56 | */ 57 | 58 | if((2*a-b)>=0 && (2*a-b)%3==0 && (2*b-a)>=0 && (2*b-a)%3==0) 59 | cout<<"YES"< 31 | #include 32 | #include 33 | #include 34 | using namespace std; 35 | 36 | int freq_of_num(int& element,multiset& set_sorted){ 37 | auto itLow = set_sorted.lower_bound(element); 38 | if(itLow == set_sorted.end()) 39 | return 0; 40 | auto itHigh = set_sorted.upper_bound(element); 41 | return distance(itLow,itHigh); 42 | 43 | } 44 | 45 | int main(){ 46 | int n; 47 | cin>>n; 48 | int* arr = new int[n]; 49 | multiset set_sorted; 50 | 51 | for(int i=0;i>arr[i]; 53 | 54 | 55 | int left = 0; 56 | int right = 0; 57 | int ans = 1; 58 | while(left 38 | #include 39 | using namespace std; 40 | 41 | int main(){ 42 | int n,m; 43 | cin>>n>>m; 44 | multiset tktPrice; 45 | int* maxPrice = new int[m]; 46 | 47 | for(int i=0;i>temp; 50 | tktPrice.insert(temp); 51 | } 52 | for(int i=0;i>maxPrice[i]; 54 | 55 | 56 | for(int i=0;i 31 | using namespace std; 32 | 33 | int main(){ 34 | int n; 35 | cin>>n; 36 | long long* arr = new long long[n]; 37 | for(int i=0;i>arr[i]; 39 | 40 | long long dp[n][n]; 41 | for(int i=0;i 39 | #include 40 | using namespace std; 41 | 42 | long long number_of_ways(int sum,int* coins,int& n,long long* dp,bool* isVisited){ 43 | 44 | if(isVisited[sum]) 45 | return dp[sum]; 46 | 47 | dp[sum] = 0; 48 | for(int i=0;i>n>>x; 63 | int* coins = new int[n]; 64 | for(int i=0;i>coins[i]; 66 | 67 | sort(coins,coins+n); 68 | 69 | long long* dp = new long long[x+1]; 70 | bool* isVisited = new bool[x+1]{false}; 71 | 72 | dp[0] = 1; 73 | isVisited[0] = true; 74 | cout< 36 | #include 37 | #include 38 | using namespace std; 39 | 40 | int main(){ 41 | string str1,str2; 42 | getline(cin,str1); 43 | getline(cin,str2); 44 | 45 | int size1=str1.size(); 46 | int size2=str2.size(); 47 | 48 | vector> dp(size1+1,vector(size2+1,0)); 49 | 50 | 51 | for(int i=size1-1;i>=0;i--){ 52 | dp[i][size2] = size1 -i; 53 | } 54 | for(int i=size2-1;i>=0;i--){ 55 | dp[size1][i] = size2 - i; 56 | } 57 | 58 | for(int i=size1-1;i>=0;i--){ 59 | for(int j=size2-1;j>=0;j--){ 60 | if(str1[i] == str2[j]){ 61 | dp[i][j] = dp[i+1][j+1]; 62 | } 63 | else{ 64 | dp[i][j] = 1 + min(dp[i][j+1],dp[i+1][j]); 65 | dp[i][j] = min(dp[i][j],1+dp[i+1][j+1]); 66 | } 67 | } 68 | } 69 | cout< 32 | #include 33 | #include 34 | using namespace std; 35 | 36 | int main(){ 37 | int x,n; 38 | cin>>x>>n; 39 | set lights; 40 | lights.insert(0); 41 | lights.insert(x); 42 | 43 | multiset maxDistances; 44 | maxDistances.insert(x); 45 | 46 | 47 | for(int i=0;i>pos; 50 | lights.insert(pos); 51 | 52 | auto it1 = lights.upper_bound(pos); 53 | auto it2 = it1; 54 | it2--; 55 | auto currentLightPos = it2; 56 | it2--; 57 | int temp = (*it1)-(*it2); 58 | auto distIterator = maxDistances.lower_bound(temp); 59 | maxDistances.erase(distIterator); 60 | maxDistances.insert((*currentLightPos)-(*it2)); 61 | maxDistances.insert((*it1)-(*currentLightPos)); 62 | cout<<(*maxDistances.rbegin())<<" "; 63 | 64 | } 65 | 66 | 67 | 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Sorting and Searching/17_sum_of_three_values.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Time limit: 1.00 s Memory limit: 512 MB 3 | You are given an array of n integers, and your task is to find three values (at distinct positions) whose sum is x. 4 | 5 | Input 6 | 7 | The first input line has two integers n and x: the array size and the target sum. 8 | 9 | The second line has n integers a1,a2,…,an: the array values. 10 | 11 | Output 12 | 13 | Print three integers: the positions of the values. If there are several solutions, you may print any of them. If there are no solutions, print IMPOSSIBLE. 14 | 15 | Constraints 16 | 1≤n≤5000 17 | 1≤x,ai≤10^9 18 | Example 19 | 20 | Input: 21 | 4 8 22 | 2 7 5 1 23 | 24 | Output: 25 | 1 3 4 26 | */ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | using namespace std; 33 | 34 | void find_ans(vector>& arr,int& n,int& x){ 35 | for(int i=n-1;i>=0;i--){ 36 | int required_sum = x - arr[i].first; 37 | if(required_sum>0){ 38 | int start = 0; 39 | int end = i-1; 40 | while(start required_sum) 46 | end--; 47 | else 48 | start++; 49 | 50 | } 51 | } 52 | } 53 | cout<<"IMPOSSIBLE"; 54 | } 55 | 56 | int main(){ 57 | int n,x; 58 | cin>>n>>x; 59 | vector> arr(n); 60 | 61 | for(int i=0;i>arr[i].first; 63 | arr[i].second = i+1; 64 | } 65 | sort(arr.begin(),arr.end()); 66 | 67 | find_ans(arr,n,x); 68 | 69 | return 0; 70 | } -------------------------------------------------------------------------------- /Sorting and Searching/27_maximum_subarray_sum_2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Time limit: 1.00 s Memory limit: 512 MB 3 | Given an array of n integers, your task is to find the maximum sum of values in a contiguous subarray with length between a and b. 4 | 5 | Input 6 | 7 | The first input line has three integers n, a and b: the size of the array and the minimum and maximum subarray length. 8 | 9 | The second line has n integers x1,x2,…,xn: the array values. 10 | 11 | Output 12 | 13 | Print one integer: the maximum subarray sum. 14 | 15 | Constraints 16 | 1≤n≤2⋅10^5 17 | 1≤a≤b≤n 18 | −10^9≤xi≤10^9 19 | Example 20 | 21 | Input: 22 | 8 1 2 23 | -1 3 -2 5 3 -5 2 2 24 | 25 | Output: 26 | 8 27 | */ 28 | 29 | #include 30 | using namespace std; 31 | 32 | int main(){ 33 | int n,a,b; 34 | cin>>n>>a>>b; 35 | int* arr = new int[n]; 36 | for(int i=0;i>arr[i]; 38 | 39 | int start = 0; 40 | long long local_sum = 0; 41 | long long max_sum = 0; 42 | for(int i=0;i b){ 48 | local_sum += arr[i]; 49 | local_sum -= arr[start]; 50 | start ++; 51 | if(length>=a && length <=b) 52 | max_sum = max(max_sum,local_sum); 53 | } 54 | else{ 55 | if(arr[i]+local_sum >= arr[i]){ 56 | local_sum += arr[i]; 57 | max_sum = max(max_sum,local_sum); 58 | } 59 | else{ 60 | local_sum = arr[i]; 61 | start = i; 62 | length = i-start+1; 63 | if(length>=a && length <=b) 64 | max_sum = max(max_sum,local_sum); 65 | } 66 | } 67 | 68 | 69 | } 70 | cout< 33 | #include 34 | using namespace std; 35 | 36 | int main(){ 37 | int n; 38 | cin>>n; 39 | vector> grid(n,vector(n)); 40 | vector> dp(n,vector(n,0)); 41 | 42 | for(int i=0;i>grid[i][j]; 45 | } 46 | 47 | if(grid[n-1][n-1] == '*'){ 48 | cout<<"0"; 49 | return 0; 50 | } 51 | 52 | dp[n-1][n-1] = 1; 53 | for(int i=n-2;i>=0;i--){ 54 | if(grid[n-1][i] != '*') 55 | dp[n-1][i] = dp[n-1][i+1]; 56 | } 57 | 58 | 59 | for(int i=n-2;i>=0;i--){ 60 | if(grid[i][n-1] != '*') 61 | dp[i][n-1] = dp[i+1][n-1]; 62 | } 63 | 64 | for(int i=n-2;i>=0;i--){ 65 | for(int j=n-2;j>=0;j--){ 66 | if(grid[i][j] != '*') 67 | dp[i][j] = (dp[i][j+1] + dp[i+1][j]) % 1000000007; 68 | } 69 | } 70 | 71 | /*cout< 40 | #include 41 | using namespace std; 42 | 43 | int main(){ 44 | int n; 45 | cin>>n; 46 | long long sum=n; 47 | sum*=sum+1; 48 | sum/=2; 49 | if(sum&1) //odd 50 | cout<<"NO"; 51 | else{ 52 | sum/=2; 53 | 54 | cout<<"YES\n"; 55 | vector> nums(2,vector(n+1,1)); 56 | for(int i=1;i<=n;i++){ 57 | nums[0][i]=i; 58 | 59 | } 60 | int i=n; 61 | int size=0; 62 | while(sum>0){ 63 | if(i<=sum){ 64 | nums[1][i]=0; 65 | sum-=i; 66 | i--; 67 | } 68 | else{ 69 | nums[1][sum]=0; 70 | sum=0; 71 | } 72 | size++; 73 | } 74 | 75 | cout< 32 | #include 33 | #include 34 | #include 35 | using namespace std; 36 | 37 | int minimum_coins(int sum,int* coins,int* dp,vector& isVisited,int& n){ 38 | 39 | 40 | if(isVisited[sum]) 41 | return dp[sum]; 42 | 43 | dp[sum] = INT_MAX; 44 | bool flag = true; 45 | for(int i=0;i>n>>x; 64 | int* coins = new int[n]; 65 | for(int i=0;i>coins[i]; 67 | } 68 | sort(coins,coins+n);; 69 | int* dp = new int[x+1]; 70 | dp[0] = 0; 71 | vector isVisited(x+1,false); 72 | isVisited[0] = true; 73 | cout< 35 | #include 36 | #include 37 | #include 38 | using namespace std; 39 | 40 | 41 | 42 | int max_customers(vector>& customerAtTimes,int& n){ 43 | 44 | sort(customerAtTimes.begin(),customerAtTimes.end()); 45 | 46 | 47 | int ans = 1; 48 | 49 | for(int i=1;i>n; 69 | 70 | vector> customerAtTimes(2*n); 71 | 72 | int j = 0; 73 | for(int i=0;i>customerAtTimes[j].first; 75 | customerAtTimes[j].second = 1; 76 | j++; 77 | cin>>customerAtTimes[j].first; 78 | customerAtTimes[j].second = -1; 79 | j++; 80 | } 81 | n *=2; 82 | cout< 42 | using namespace std; 43 | 44 | long long find_number(long long& row,long long& col){ 45 | long long t=max(row,col)-1; 46 | long long lower_limit=(t*t)+1; 47 | long long upper_limit=(t+1)*(t+1); 48 | 49 | if(row>=col){ 50 | 51 | if(row&1) //odd numbered L shaped 52 | return lower_limit+col-1; 53 | return upper_limit-col+1; 54 | } 55 | else{ 56 | 57 | if(col&1) //odd numbered L shaped 58 | return upper_limit-row+1; 59 | return lower_limit+row-1; 60 | } 61 | 62 | 63 | } 64 | 65 | 66 | int main(){ 67 | 68 | int t; 69 | cin>>t; 70 | while(t--){ 71 | long long y,x; 72 | cin>>y>>x; 73 | cout< 35 | #include 36 | #include 37 | using namespace std; 38 | 39 | int num_of_successful_applicants(int* person,int& n,int* flats,int& m,int& k){ 40 | sort(person,person+n); 41 | sort(flats,flats+m); 42 | int ans = 0; 43 | int j = 0; 44 | int i = 0; 45 | while(ik){ 47 | i++; 48 | } 49 | else if(person[j]-flats[i]<(-1*k)){ 50 | j++; 51 | } 52 | else{ 53 | i++; 54 | j++; 55 | ans++; 56 | } 57 | } 58 | return ans; 59 | 60 | } 61 | 62 | int main(){ 63 | int n,m,k; 64 | cin>>n>>m>>k; 65 | int* applicants = new int[n]; 66 | int* apartments = new int[m]; 67 | for(int i=0;i>applicants[i]; 69 | 70 | for(int i=0;i>apartments[i]; 72 | 73 | cout< 38 | #include 39 | #include 40 | #include 41 | using namespace std; 42 | 43 | bool isValidWordforString(string& a,string& b,int index){ 44 | 45 | for(int i=0;i& words,int& k,int index,string& str,vector& dp){ 54 | 55 | if(dp[index]!= -1) 56 | return dp[index]; 57 | 58 | dp[index] = 0 ; 59 | for(int i=0;i (str.size()-index)) 61 | break; 62 | if(isValidWordforString(words[i],str,index)){ 63 | dp[index] += number_of_ways(words,k,index+words[i].size(),str,dp); 64 | dp[index] %= 1000000007; 65 | } 66 | } 67 | return dp[index]; 68 | 69 | } 70 | 71 | bool compf(string& a,string& b){ 72 | return a.size()>str; 78 | int k; 79 | cin>>k; 80 | vector words(k); 81 | vector dp(str.size()+1,-1); 82 | dp[str.size()] = 1; 83 | for(int i=0;i>words[i]; 85 | sort(words.begin(),words.end(),compf); 86 | cout< 29 | #include 30 | #include 31 | #include 32 | using namespace std; 33 | 34 | void find_ans(vector>& arr,int& n,int& x){ 35 | for(int i=n-1;i>2;i--){ 36 | int sum_remaining = x - arr[i].first; 37 | if(sum_remaining > 0){ 38 | for(int j=i-1;j>1;j--){ 39 | int sum_remaining_1 = sum_remaining - arr[j].first; 40 | if(sum_remaining_1 > 0){ 41 | int start = 0; 42 | int end = j-1; 43 | while(start sum_remaining_1) 49 | end--; 50 | else 51 | start++; 52 | 53 | } 54 | } 55 | } 56 | } 57 | 58 | } 59 | cout<<"IMPOSSIBLE"; 60 | } 61 | 62 | int main(){ 63 | int n,x; 64 | cin>>n>>x; 65 | vector> arr(n); 66 | 67 | for(int i=0;i>arr[i].first; 69 | arr[i].second = i+1; 70 | } 71 | sort(arr.begin(),arr.end()); 72 | 73 | find_ans(arr,n,x); 74 | 75 | return 0; 76 | } -------------------------------------------------------------------------------- /Sorting and Searching/23_array_divison.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Time limit: 1.00 s Memory limit: 512 MB 3 | You are given an array containing n positive integers. 4 | 5 | Your task is to divide the array into k subarrays so that the maximum sum in a subarray is as small as possible. 6 | 7 | Input 8 | 9 | The first input line contains two integers n and k: the size of the array and the number of subarrays in the division. 10 | 11 | The next line contains n integers x1,x2,…,xn: the contents of the array. 12 | 13 | Output 14 | 15 | Print one integer: the maximum sum in a subarray in the optimal division. 16 | 17 | Constraints 18 | 1≤n≤2⋅10^5 19 | 1≤k≤n 20 | 1≤xi≤10^9 21 | Example 22 | 23 | Input: 24 | 5 3 25 | 2 4 7 3 5 26 | 27 | Output: 28 | 8 29 | 30 | Explanation: An optimal division is [2,4],[7],[3,5] where the sums of the subarrays are 6,7,8. The largest sum is the last sum 8. 31 | */ 32 | 33 | #include 34 | using namespace std; 35 | 36 | 37 | bool isSolutionPossible(long long& mid,int* arr,int& n,int& k){ 38 | 39 | int count = 1; 40 | long long curr_sum = 0; 41 | for(int i=0;i mid) 44 | return false; 45 | if(curr_sum > mid){ 46 | count ++; 47 | curr_sum = arr[i]; 48 | if(count > k) 49 | return false; 50 | } 51 | } 52 | return true; 53 | 54 | } 55 | 56 | long long divide_array_optimally(int* arr,int& n,int& k,long long& start,long long& end){ 57 | 58 | long long maxSum = end; 59 | while(start<=end){ 60 | long long mid = start + (end - start)/2; 61 | if(isSolutionPossible(mid,arr,n,k)){ 62 | end = mid - 1; 63 | maxSum = mid; 64 | } 65 | else{ 66 | start = mid + 1; 67 | } 68 | 69 | } 70 | return maxSum; 71 | } 72 | 73 | int main(){ 74 | int n,k; 75 | cin>>n>>k; 76 | 77 | int* arr = new int[n]; 78 | long long min_element = 1; 79 | long long max_element = 0; 80 | for(int i=0;i>arr[i]; 82 | max_element += arr[i]; 83 | } 84 | 85 | cout< 34 | #include 35 | using namespace std; 36 | 37 | 38 | 39 | int main(){ 40 | int n,m; 41 | cin>>n>>m; 42 | int* arr = new int[n]; 43 | for(int i=0;i>arr[i]; 45 | 46 | vector> dp(n,vector(m,0)); 47 | 48 | if(arr[n-1] == 0){ 49 | for(int i=0;i=0;i--){ 56 | for(int j=m-1;j>=0;j--){ 57 | if(arr[i] == j+1 || arr[i] == 0){ 58 | if(j-1 >= 0){ 59 | dp[i][j] += dp[i+1][j-1]; 60 | dp[i][j] %= 1000000007; 61 | } 62 | if(j+1 < m){ 63 | dp[i][j] += dp[i+1][j+1]; 64 | dp[i][j] %= 1000000007; 65 | } 66 | dp[i][j] += dp[i+1][j]; 67 | dp[i][j] %= 1000000007; 68 | } 69 | } 70 | } 71 | int arr_count = 0; 72 | if(arr[0] == 0){ 73 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int find_min(vector& min_distances,vector& isVisited,int& total_nodes){ 9 | 10 | int index = -1; 11 | int value = INT_MAX; 12 | for(int i=1;i<=total_nodes;i++){ 13 | if(!isVisited[i] && min_distances[i] <= value){ 14 | value = min_distances[i]; 15 | index = i; 16 | } 17 | } 18 | return index; 19 | 20 | } 21 | 22 | void dijkistra_algo(unordered_map>>& graph,int total_nodes,int starting_node){ 23 | 24 | vector min_distances(total_nodes+1,INT_MAX); 25 | min_distances[starting_node] = 0; 26 | 27 | vector isVisited(total_nodes+1,false); 28 | 29 | 30 | for(int i=0;i>> graph; 57 | graph[1].push_back({2,12}); 58 | graph[1].push_back({3,3}); 59 | graph[1].push_back({4,9}); 60 | graph[2].push_back({7,23}); 61 | graph[2].push_back({6,2}); 62 | graph[2].push_back({5,15}); 63 | graph[3].push_back({7,10}); 64 | graph[3].push_back({6,5}); 65 | graph[3].push_back({1,3}); 66 | graph[3].push_back({5,7}); 67 | graph[4].push_back({7,41}); 68 | graph[4].push_back({1,9}); 69 | graph[5].push_back({7,12}); 70 | graph[5].push_back({3,7}); 71 | graph[5].push_back({2,15}); 72 | graph[6].push_back({7,1}); 73 | graph[6].push_back({2,2}); 74 | graph[7].push_back({4,41}); 75 | graph[7].push_back({6,1}); 76 | 77 | int total_nodes = 7; 78 | int starting_node = 1; 79 | dijkistra_algo(graph,total_nodes,starting_node); 80 | 81 | return 0; 82 | } -------------------------------------------------------------------------------- /Introductory Problems/15_chessboard_nd_queens.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Time limit: 1.00 s Memory limit: 512 MB 3 | Your task is to place eight queens on a chessboard so that no two queens are attacking each other. As an additional challenge, each square is either free or reserved, and you can only place queens on the free squares. However, the reserved squares do not prevent queens from attacking each other. 4 | 5 | How many possible ways are there to place the queens? 6 | 7 | Input 8 | 9 | The input has eight lines, and each of them has eight characters. Each square is either free (.) or reserved (*). 10 | 11 | Output 12 | 13 | Print one integer: the number of ways you can place the queens. 14 | 15 | Example 16 | 17 | Input: 18 | ........ 19 | ........ 20 | ..*..... 21 | ........ 22 | ........ 23 | .....**. 24 | ...*.... 25 | ........ 26 | 27 | Output: 28 | 65 29 | */ 30 | 31 | #include 32 | #include 33 | using namespace std; 34 | 35 | bool isSafeMove(vector>& board,int& row, int& col,int& n){ 36 | for(int i=0;i=0 && c>=0){ 53 | if(board[r][c] == 'Q') 54 | return false; 55 | r--; 56 | c--; 57 | } 58 | 59 | r = row - 1; 60 | c = col + 1; 61 | while( r>=0 && c=0){ 71 | if(board[r][c] == 'Q') 72 | return false; 73 | r++; 74 | c--; 75 | } 76 | return true; 77 | } 78 | 79 | void number_of_ways(vector>& board,int& size,int row_number,int& count){ 80 | if(row_number == size){ 81 | count++; 82 | return; 83 | } 84 | else{ 85 | for(int i=0;i> board(8,vector(8)); 98 | 99 | for(int i=0;i<8;i++){ 100 | for(int j=0;j<8;j++) 101 | cin>>board[i][j]; 102 | } 103 | int count = 0; 104 | int board_size = 8; 105 | number_of_ways(board,board_size,0,count); 106 | cout< 36 | #include 37 | using namespace std; 38 | 39 | long long bin_search(long long start,long long end,int& packets,int* arr,int& n){ 40 | 41 | long long answer = end; 42 | 43 | while(start<=end){ 44 | long long mid = start + (end-start)/2; 45 | long long temp = 0; 46 | bool flag = false; 47 | for(int i=0;i 1000000000000000000){ // to avoid getting out of range of long 50 | flag = true; 51 | break; 52 | } 53 | } 54 | //cout<= packets || flag ){ 56 | answer = min(answer,mid); 57 | end = mid - 1; 58 | } 59 | else 60 | start = mid + 1; 61 | } 62 | return answer; 63 | 64 | } 65 | 66 | /*long long min_time_taken(int* arr,int& n,int& t){ 67 | if(t == 0) 68 | return 0; 69 | long long candidate_time = 1; 70 | 71 | while(true){ 72 | long long packets = 0; 73 | for(int i=0;i t) 78 | break; 79 | candidate_time *= 2; 80 | } 81 | //cout<>n>>t; 91 | int* time = new int[n];//{6 ,6 ,4 ,3 ,4 ,9 ,3 ,2 ,6 ,10}; 92 | 93 | 94 | for(int i=0;i>time[i]; 96 | 97 | //cout< 38 | #include 39 | #include 40 | #include 41 | #include 42 | using namespace std; 43 | 44 | /*bool compf(pair,int>& a, pair,int>& b){ 45 | if( a.first.first != b.first.first) 46 | return a.first.first < b.first.first; 47 | else { 48 | if(a.first.second != b.first.second) 49 | return a.first.second < b.first.second; 50 | return a.second < b.second; 51 | } 52 | 53 | }*/ 54 | 55 | int main(){ 56 | int n; 57 | cin>>n; 58 | 59 | vector,int>> timings(n); 60 | for(int i=0;i>timings[i].first.first>>timings[i].first.second; 62 | timings[i].second = i+1; 63 | } 64 | sort(timings.begin(),timings.end());//,compf); 65 | 66 | /*for(int i=0;i roomFinishTimes; 72 | vector respectiveRooms(n+1,0); 73 | int roomNum = 1; 74 | roomFinishTimes.insert(timings[0].first.second); 75 | respectiveRooms[timings[0].second] = roomNum; 76 | roomNum++; 77 | 78 | for(int i=1;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | void floyd_warshall_algo(unordered_map>>& graph,int total_nodes){ 9 | 10 | vector> min_dist_matrix(total_nodes+1,vector(total_nodes+1,INT_MAX)); 11 | for(int i=1;i<=total_nodes;i++) 12 | min_dist_matrix[i][i] = 0; 13 | 14 | for(int i=1;i<= total_nodes;i++){ 15 | for(int j=0;j>> graph; 46 | graph[1].push_back({2,12}); 47 | graph[1].push_back({3,3}); 48 | graph[1].push_back({4,9}); 49 | //graph[2].push_back({7,23}); 50 | graph[2].push_back({6,2}); 51 | graph[2].push_back({5,15}); 52 | //graph[3].push_back({7,10}); 53 | graph[3].push_back({6,5}); 54 | graph[3].push_back({1,3}); 55 | graph[3].push_back({5,7}); 56 | //graph[4].push_back({7,41}); 57 | graph[4].push_back({1,9}); 58 | //graph[5].push_back({7,12}); 59 | graph[5].push_back({3,7}); 60 | graph[5].push_back({2,15}); 61 | //graph[6].push_back({7,1}); 62 | graph[6].push_back({2,2}); 63 | graph[7].push_back({4,41}); 64 | graph[7].push_back({6,1}); 65 | 66 | int total_nodes = 7; 67 | 68 | 69 | floyd_warshall_algo(graph,total_nodes); 70 | 71 | return 0; 72 | }#include 73 | #include 74 | #include 75 | #include 76 | #include 77 | using namespace std; 78 | 79 | void floyd_warshall_algo(unordered_map>>& graph,int total_nodes){ 80 | 81 | vector> min_dist_matrix(total_nodes+1,vector(total_nodes+1,INT_MAX)); 82 | for(int i=1;i<=total_nodes;i++) 83 | min_dist_matrix[i][i] = 0; 84 | 85 | for(int i=1;i<= total_nodes;i++){ 86 | for(int j=0;j>> graph; 117 | graph[1].push_back({2,12}); 118 | graph[1].push_back({3,3}); 119 | graph[1].push_back({4,9}); 120 | //graph[2].push_back({7,23}); 121 | graph[2].push_back({6,2}); 122 | graph[2].push_back({5,15}); 123 | //graph[3].push_back({7,10}); 124 | graph[3].push_back({6,5}); 125 | graph[3].push_back({1,3}); 126 | graph[3].push_back({5,7}); 127 | //graph[4].push_back({7,41}); 128 | graph[4].push_back({1,9}); 129 | //graph[5].push_back({7,12}); 130 | graph[5].push_back({3,7}); 131 | graph[5].push_back({2,15}); 132 | //graph[6].push_back({7,1}); 133 | graph[6].push_back({2,2}); 134 | graph[7].push_back({4,41}); 135 | graph[7].push_back({6,1}); 136 | 137 | int total_nodes = 7; 138 | 139 | 140 | floyd_warshall_algo(graph,total_nodes); 141 | 142 | return 0; 143 | } --------------------------------------------------------------------------------