├── D1-String.cpp ├── D10-Array.cpp ├── D11-Array.cpp ├── D12-Array.cpp ├── D13-Array.cpp ├── D14-Array.cpp ├── D15-Array.cpp ├── D16-Array.cpp ├── D17-Array.cpp ├── D18-Array.cpp ├── D19-Array.cpp ├── D2-String.cpp ├── D20-Array.cpp ├── D21-String.cpp ├── D22-String.cpp ├── D23-String.cpp ├── D24-String.cpp ├── D25-String.cpp ├── D26-String.cpp ├── D27-String.cpp ├── D28-String.cpp ├── D29-String.cpp ├── D3-String.cpp ├── D4-Array.cpp ├── D5-Array.cpp ├── D6-Array.cpp ├── D7-Array.cpp ├── D8-Array.cpp ├── D9-Array.cpp └── README.md /D1-String.cpp: -------------------------------------------------------------------------------- 1 | 2 | // 53. Maximum Subarray 3 | // Approach 1: Brute Force 4 | // for(int i=0 ->n){ 5 | // for(int j=0 ->n){ 6 | // for(int k=i -> j){ 7 | // sum = sum + arr[k]; 8 | // } 9 | // } 10 | // } 11 | 12 | // for(int i=0 ->n){ 13 | // sum 14 | // for(int j=0 ->n){ 15 | // for(int k=i -> j){ 16 | // sum = sum + arr[k]; 17 | // } 18 | // } 19 | // } 20 | 21 | 22 | 23 | class Solution { 24 | public: 25 | int maxSubArray(vector& nums) { 26 | int sum = 0; 27 | int maxi = nums[0]; 28 | 29 | 30 | for(int i=0; i 49 | using namespace std; 50 | 51 | int main() { 52 | int n = 0; 53 | int arr[n]; 54 | int max, min; 55 | cin>>n; 56 | for(int i=0; i>arr[i]; 58 | } 59 | if(n==0){ 60 | max = arr[0]; 61 | min = arr[0]; 62 | } 63 | if(n==1){ 64 | if(arr[0]>arr[1]){ 65 | max = arr[0]; 66 | min = arr[1]; 67 | } 68 | else{ 69 | max = arr[1]; 70 | min = arr[0]; 71 | } 72 | } 73 | else{ 74 | if(arr[0]>arr[1]){ 75 | max = arr[0]; 76 | min = arr[1]; 77 | } 78 | else{ 79 | max = arr[1]; 80 | min = arr[0]; 81 | } 82 | for(int i=2; iarr[i]){ 87 | min = arr[i]; 88 | } 89 | } 90 | } 91 | cout<<"Max" << max< 101 | using namespace std; 102 | 103 | int main() { 104 | 105 | int n, i, j, temp; 106 | int arr[n]; 107 | 108 | cin>>n; 109 | 110 | for(int k=0; k> arr[k]; 112 | } 113 | 114 | 115 | for(int i=0, j=n-1; i 133 | // using namespace std; 134 | 135 | // /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 136 | 137 | // void printSum(int arr[][3], int row, int col){ 138 | // for(int row=0; row<3; row++){ 139 | // int sum = 0; 140 | // for(int col=0; col<3; col++){ 141 | // sum += arr[row][col]; 142 | // } 143 | // cout< maxi){ 181 | // maxi = sum; 182 | // rowIndex = row; 183 | // } 184 | // } 185 | // return rowIndex; 186 | // } 187 | 188 | 189 | // int main(int argc, char** argv) { 190 | 191 | // // int arr1[3][4] = {{1,11,111,1111}, {2,22,222,2222}, {3,33,333,3333}}; 192 | // int arr2[3][3]; 193 | 194 | // for(int row=0; row<3; row++){ 195 | // for(int col=0; col<3; col++){ 196 | // cin>>arr2[row][col]; 197 | // } 198 | // } 199 | 200 | // for(int row=0; row<3; row++){ 201 | // for(int col=0; col<3; col++){ 202 | // cout< prime(n+1, true); 215 | // // prime[0] = prime[1] = false; 216 | // // 217 | // // for(int i=2; i>target; 257 | 258 | // // if(isPresent(arr1, target, 3, 4)){ 259 | // // cout<<"Element Found"; 260 | // // } 261 | // // else{ 262 | // // cout<<"Element Not Found"; 263 | // // } 264 | 265 | // cout<<"Row Wise Sum = "; 266 | // printSum(arr2, 3, 3); 267 | // cout< spiralOrder(vector>& matrix) { 282 | // // int row = matrix.size(); 283 | // // int col = matrix[0].size(); 284 | // // vector ans; 285 | // // int count = 0; 286 | // // int total = row*col; 287 | // // int startingRow = 0; 288 | // // int startingCol = 0; 289 | // // int endingRow = row-1; 290 | // // int endingCol = col-1; 291 | // // 292 | // // while(count=startingCol; i--){ 307 | // // ans.push_back(matrix[endingRow][i]); 308 | // // count++; 309 | // // } 310 | // // endingRow--; 311 | // // 312 | // // for(int i=endingRow; count=startingRow; i--){ 313 | // // ans.push_back(matrix[i][startingCol]); 314 | // // count++; 315 | // // } 316 | // // startingCol++; 317 | // // } 318 | // // return ans; 319 | // // } 320 | // //}; 321 | // //class Solution { 322 | // //public: 323 | // // vector spiralOrder(vector>& matrix) { 324 | // // int n=matrix.size(),m=matrix[0].size(); 325 | // // int left=0,top=0; 326 | // // int bottom=n-1,right=m-1; 327 | // // vectorans; 328 | // // while(left<=right&&top<=bottom) 329 | // // { 330 | // // for(int i=left;i<=right;i++) 331 | // // { 332 | // // ans.push_back(matrix[top][i]); 333 | // // } 334 | // // top++; 335 | // // for(int i=top;i<=bottom;i++) 336 | // // { 337 | // // ans.push_back(matrix[i][right]); 338 | // // } 339 | // // right--; 340 | // // if(top<=bottom) 341 | // // { 342 | // // for(int i=right;i>=left;i--) 343 | // // { 344 | // // ans.push_back(matrix[bottom][i]); 345 | // // } 346 | // // bottom--; 347 | // // } 348 | // // if(left<=right) 349 | // // { 350 | // // for(int i=bottom;i>=top;i--) 351 | // // { 352 | // // ans.push_back(matrix[i][left]); 353 | // // } 354 | // // left++; 355 | // // } 356 | // // } 357 | // // return ans; 358 | // // } 359 | // //}; 360 | 361 | // //Rotate Image by 90 degree 362 | // //class Solution { 363 | // //public: 364 | // // void rotate(vector>& matrix) { 365 | // // int n = matrix.size(); 366 | // // for(int i=0; i<(n+1)/2;i++){ 367 | // // for(int j=0; j 393 | // //vector wavePrint(vector> arr, int nRows, int mCols) 394 | // //{ 395 | // // vector ans; 396 | // // 397 | // // for(int col=0; col=0; row--){ 400 | // // ans.push_back(arr[row][col]); 401 | // // } 402 | // // } 403 | // // else{ 404 | // // for(int row=0; row findDiagonalOrder(vector>& matrix) { 420 | // // // if(matrix.size()==0 || matrix[0].size()==0){ 421 | // // // return matrix; 422 | // // // } 423 | // // vector ans; 424 | // // int m = matrix.size(); 425 | // // int n = matrix[0].size(); 426 | // // int row=0, col=0; 427 | // // bool up = true; 428 | // // 429 | // // while(row0 && col0 && row 469 | // // 470 | // //int modularExponentiation(int x, int n, int m) { 471 | // // int res = 1; 472 | // // while(n>0){ 473 | // // if(n&1){ 474 | // // res = (1LL*(res) * (x)%m)%m; 475 | // // } 476 | // // x = (1LL * (x)%m * (x)%m) %m; 477 | // // n = n>>1; 478 | // // } 479 | // // return res; 480 | // //} 481 | -------------------------------------------------------------------------------- /D10-Array.cpp: -------------------------------------------------------------------------------- 1 | //152. Maximum Product Subarray 2 | class Solution { 3 | public: 4 | int maxProduct(vector& nums) { 5 | int n = nums.size(); 6 | int Max = nums[0], a = 1, b = 1; 7 | 8 | for(int i = 0; i < n; i++){ 9 | a = (a == 0 ? 1 : a) * nums[i]; 10 | b = (b == 0 ? 1 : b) * nums[n - 1 - i]; 11 | 12 | Max = max(Max, max(a, b)); 13 | } 14 | 15 | return Max; 16 | } 17 | }; 18 | 19 | 20 | class Solution { 21 | public: 22 | int maxProduct(vector& nums) { 23 | int n = nums.size(); 24 | if (n == 0) return 0; 25 | 26 | // Use long long to handle large products 27 | long long maxProd = nums[0]; 28 | long long minProd = nums[0]; 29 | long long result = nums[0]; 30 | 31 | // Traverse the array starting from the second element 32 | for (int i = 1; i < n; i++) { 33 | // If nums[i] is negative, swap maxProd and minProd 34 | if (nums[i] < 0) { 35 | swap(maxProd, minProd); 36 | } 37 | 38 | // Update maxProd and minProd 39 | maxProd = max((long long)nums[i], maxProd * nums[i]); 40 | minProd = min((long long)nums[i], minProd * nums[i]); 41 | 42 | // Update the result 43 | result = max(result, maxProd); 44 | } 45 | 46 | return (int)result; // Cast back to int before returning 47 | } 48 | }; 49 | 50 | 51 | 52 | 53 | 54 | //42. Trapping Rain Water 55 | class Solution { 56 | public: 57 | int trap(vector& height) { 58 | int n = height.size(); 59 | int left[n]; 60 | int right[n]; 61 | left[0] = height[0]; 62 | 63 | for(int i=1; i=0; i--){ 69 | right[i] = max(right[i+1], height[i]); 70 | } 71 | int ssum=0; 72 | for(int i=0; i& height) { 82 | int min = height[0]; 83 | vector left; 84 | for(int i=0; i right; 90 | for(int i=height.size(); i>-0; i--){ 91 | Max = max(Max, height[i]); 92 | right.push_back(Max); 93 | } 94 | vector sumarr; 95 | for(int i=0; i& nums, int k) { 113 | sort(nums.begin(), nums.end()); 114 | int n = nums.size(); 115 | return nums[n-k]; 116 | } 117 | }; 118 | 119 | 120 | class Solution { 121 | public: 122 | int findKthLargest(vector& nums, int k) { 123 | sort(nums.begin(), nums.end()); 124 | int max=0; 125 | int size = nums.size(); 126 | if(size==1){ 127 | return 1; 128 | } 129 | int till = size-k; 130 | for(int i=0; i frequencies(arr, arr + 26); 153 | sort(frequencies.begin(), frequencies.end()); 154 | 155 | int count = 0; 156 | int minKeyPress = 0; 157 | 158 | for(int i = 25; i >= 0; i--) { 159 | if(frequencies[i] == 0) break; 160 | int val = frequencies[i] * (count / 8 + 1); 161 | minKeyPress += val; 162 | count++; 163 | } 164 | 165 | return minKeyPress; 166 | } 167 | }; 168 | 169 | class Solution { 170 | public: 171 | int minimumPushes(string word) { 172 | int arr[26]; 173 | for(char ch: word){ 174 | arr[ch-'a']++; 175 | } 176 | sort(arr[].begin(), arr[].end()); 177 | 178 | int count=0; 179 | int minkeypress=0; 180 | 181 | for(int i=25; i>=0; i--){ 182 | if(arr[i]==0) break; 183 | int val = arr[i]*(count/8 + 1); 184 | minkeypress += val; 185 | count++; 186 | } 187 | return minkeypress; 188 | } 189 | }; 190 | 191 | 192 | #include 193 | #include 194 | 195 | class Solution { 196 | public: 197 | int minimumPushes(std::string word) { 198 | std::unordered_map keyMap = { 199 | {'a', 2}, {'b', 3}, {'c', 4}, {'d', 5}, {'e', 6}, {'f', 2}, {'g', 2}, 200 | {'h', 3}, {'i', 3}, {'j', 4}, {'k', 4}, {'l', 5}, {'m', 5}, {'n', 6}, 201 | {'o', 6}, {'p', 7}, {'q', 7}, {'r', 7}, {'s', 7}, {'t', 8}, {'u', 8}, 202 | {'v', 8}, {'w', 9}, {'x', 9}, {'y', 9}, {'z', 9} 203 | }; 204 | 205 | int totalPushes = 0; 206 | for (char c : word) { 207 | totalPushes += keyMap[c]; 208 | } 209 | 210 | return totalPushes; 211 | } 212 | }; 213 | 214 | 215 | class Solution { 216 | public: 217 | int minimumPushes(string word) { 218 | int two = 0, three = 0, four=0, five=0, six=0, seven=0, eight=0, nine=0; 219 | for( char c: word){ 220 | if(word[c]=='a' || word[c]=='f' || word[c]=='g'){ 221 | two++; 222 | } 223 | if(word[c]=='b' || word[c]=='h' || word[c]=='i'){ 224 | three++; 225 | } 226 | if(word[c]=='c' || word[c]=='j' || word[c]=='k'){ 227 | four++; 228 | } 229 | if(word[c]=='d' || word[c]=='l' || word[c]=='m'){ 230 | five++; 231 | } 232 | if(word[c]=='e' || word[c]=='n' || word[c]=='o'){ 233 | six++; 234 | } 235 | if(word[c]=='p' || word[c]=='q' || word[c]=='r' || word[c]=='s'){ 236 | seven++; 237 | } 238 | if(word[c]=='t' || word[c]=='u' || word[c]=='v'){ 239 | eight++; 240 | } 241 | if(word[c]=='w' || word[c]=='x' || word[c]=='y' || word[c]=='z'){ 242 | nine++; 243 | } 244 | } 245 | int ans = two+three+four+five+six+seven+eight+nine; 246 | return ans; 247 | } 248 | }; 249 | 250 | 251 | //2053. Kth Distinct String in an Array 252 | 253 | class Solution { 254 | public: 255 | string kthDistinct(vector& arr, int k) { 256 | unordered_map mp; 257 | int i; 258 | 259 | for(i=0; i& arr, int k) { 279 | vector& ans; 280 | int j=0, count=0; 281 | for(int i=0; i0){ 291 | ans = arr[i]; 292 | } 293 | } 294 | j=0; 295 | } 296 | // if(k==0){ 297 | // return ans; 298 | // } 299 | return ans; 300 | } 301 | }; 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | //1508. Range Sum of Sorted Subarray Sums 310 | //SOLUTION-1 311 | class Solution { 312 | public: 313 | int rangeSum(vector& nums, int n, int left, int right) { 314 | const unsigned int M = 1000000007; 315 | vector sums; 316 | 317 | // Generate all subarray sums 318 | for(int i = 0; i < n; i++) { 319 | int current_sum = 0; 320 | for(int j = i; j < n; j++) { 321 | current_sum = (current_sum + nums[j]) % M; 322 | sums.push_back(current_sum); 323 | } 324 | } 325 | 326 | // Sort the subarray sums 327 | sort(sums.begin(), sums.end()); 328 | 329 | // Calculate the sum of elements from `left-1` to `right-1` 330 | long long result = 0; 331 | for(int i = left - 1; i < right; i++) { 332 | result = (result + sums[i]) % M; 333 | } 334 | 335 | return (int)result; 336 | } 337 | }; 338 | 339 | 340 | //SOLUTION-2 341 | class Solution { 342 | public: 343 | int rangeSum(vector& nums, int n, int left, int right) { 344 | long long sum = 0; 345 | const unsigned int M = 1000000007; 346 | vector csum; 347 | vector sums; 348 | for(int i=0; i& nums, int target) { 372 | int low=0; 373 | int high = nums.size() -1; 374 | 375 | while( low <= high){ 376 | int mid = (low + high) / 2; 377 | 378 | if(nums[mid] == target) return mid; 379 | 380 | if(nums[low] <= nums[mid]){ 381 | if(nums[low] <= target && target <= nums[mid]){ 382 | high = mid - 1; 383 | }else{ 384 | low = mid + 1; 385 | } 386 | }else{ 387 | if(nums[mid] <= target && target <= nums[high]){ 388 | low = mid + 1; 389 | }else{ 390 | high = mid - 1; 391 | } 392 | } 393 | } 394 | return -1; 395 | } 396 | }; 397 | 398 | 399 | 400 | //1, 3, 4, 7, 9, 9 12, 56 401 | 402 | int min = LLONG_MAX; 403 | 404 | for(int i=0; i i& nums) { 417 | unordered_sets(nums.begin(), nums.end()); 418 | return nums.size() != s.size(); 419 | } 420 | }; 421 | 422 | 423 | class Solution { 424 | public: 425 | bool containsDuplicate(vector& nums) { 426 | sort(nums.begin(), nums.end()); 427 | int n = nums.size(); 428 | for(int i=1; i& nums) { 442 | int n = nums.size(); 443 | int o=0, z=0; 444 | 445 | for(int i=0; i60){ 476 | count++; 477 | } 478 | return count; 479 | } 480 | 481 | 482 | 483 | 484 | 485 | // 53. Maximum Subarray 486 | // Approach 1: Brute Force 487 | // for(int i=0 ->n){ 488 | // for(int j=0 ->n){ 489 | // for(int k=i -> j){ 490 | // sum = sum + arr[k]; 491 | // } 492 | // } 493 | // } 494 | 495 | // for(int i=0 ->n){ 496 | // sum 497 | // for(int j=0 ->n){ 498 | // for(int k=i -> j){ 499 | // sum = sum + arr[k]; 500 | // } 501 | // } 502 | // } 503 | 504 | 505 | 506 | class Solution { 507 | public: 508 | int maxSubArray(vector& nums) { 509 | int sum = 0; 510 | int maxi = nums[0]; 511 | 512 | 513 | for(int i=0; i 532 | using namespace std; 533 | 534 | int main() { 535 | int n = 0; 536 | int arr[n]; 537 | int max, min; 538 | cin>>n; 539 | for(int i=0; i>arr[i]; 541 | } 542 | if(n==0){ 543 | max = arr[0]; 544 | min = arr[0]; 545 | } 546 | if(n==1){ 547 | if(arr[0]>arr[1]){ 548 | max = arr[0]; 549 | min = arr[1]; 550 | } 551 | else{ 552 | max = arr[1]; 553 | min = arr[0]; 554 | } 555 | } 556 | else{ 557 | if(arr[0]>arr[1]){ 558 | max = arr[0]; 559 | min = arr[1]; 560 | } 561 | else{ 562 | max = arr[1]; 563 | min = arr[0]; 564 | } 565 | for(int i=2; iarr[i]){ 570 | min = arr[i]; 571 | } 572 | } 573 | } 574 | cout<<"Max" << max< 584 | using namespace std; 585 | 586 | int main() { 587 | 588 | int n, i, j, temp; 589 | int arr[n]; 590 | 591 | cin>>n; 592 | 593 | for(int k=0; k> arr[k]; 595 | } 596 | 597 | 598 | for(int i=0, j=n-1; i 616 | // using namespace std; 617 | 618 | // /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 619 | 620 | // void printSum(int arr[][3], int row, int col){ 621 | // for(int row=0; row<3; row++){ 622 | // int sum = 0; 623 | // for(int col=0; col<3; col++){ 624 | // sum += arr[row][col]; 625 | // } 626 | // cout< maxi){ 664 | // maxi = sum; 665 | // rowIndex = row; 666 | // } 667 | // } 668 | // return rowIndex; 669 | // } 670 | 671 | 672 | // int main(int argc, char** argv) { 673 | 674 | // // int arr1[3][4] = {{1,11,111,1111}, {2,22,222,2222}, {3,33,333,3333}}; 675 | // int arr2[3][3]; 676 | 677 | // for(int row=0; row<3; row++){ 678 | // for(int col=0; col<3; col++){ 679 | // cin>>arr2[row][col]; 680 | // } 681 | // } 682 | 683 | // for(int row=0; row<3; row++){ 684 | // for(int col=0; col<3; col++){ 685 | // cout< prime(n+1, true); 698 | // // prime[0] = prime[1] = false; 699 | // // 700 | // // for(int i=2; i>target; 740 | 741 | // // if(isPresent(arr1, target, 3, 4)){ 742 | // // cout<<"Element Found"; 743 | // // } 744 | // // else{ 745 | // // cout<<"Element Not Found"; 746 | // // } 747 | 748 | // cout<<"Row Wise Sum = "; 749 | // printSum(arr2, 3, 3); 750 | // cout< spiralOrder(vector>& matrix) { 765 | // // int row = matrix.size(); 766 | // // int col = matrix[0].size(); 767 | // // vector ans; 768 | // // int count = 0; 769 | // // int total = row*col; 770 | // // int startingRow = 0; 771 | // // int startingCol = 0; 772 | // // int endingRow = row-1; 773 | // // int endingCol = col-1; 774 | // // 775 | // // while(count=startingCol; i--){ 790 | // // ans.push_back(matrix[endingRow][i]); 791 | // // count++; 792 | // // } 793 | // // endingRow--; 794 | // // 795 | // // for(int i=endingRow; count=startingRow; i--){ 796 | // // ans.push_back(matrix[i][startingCol]); 797 | // // count++; 798 | // // } 799 | // // startingCol++; 800 | // // } 801 | // // return ans; 802 | // // } 803 | // //}; 804 | // //class Solution { 805 | // //public: 806 | // // vector spiralOrder(vector>& matrix) { 807 | // // int n=matrix.size(),m=matrix[0].size(); 808 | // // int left=0,top=0; 809 | // // int bottom=n-1,right=m-1; 810 | // // vectorans; 811 | // // while(left<=right&&top<=bottom) 812 | // // { 813 | // // for(int i=left;i<=right;i++) 814 | // // { 815 | // // ans.push_back(matrix[top][i]); 816 | // // } 817 | // // top++; 818 | // // for(int i=top;i<=bottom;i++) 819 | // // { 820 | // // ans.push_back(matrix[i][right]); 821 | // // } 822 | // // right--; 823 | // // if(top<=bottom) 824 | // // { 825 | // // for(int i=right;i>=left;i--) 826 | // // { 827 | // // ans.push_back(matrix[bottom][i]); 828 | // // } 829 | // // bottom--; 830 | // // } 831 | // // if(left<=right) 832 | // // { 833 | // // for(int i=bottom;i>=top;i--) 834 | // // { 835 | // // ans.push_back(matrix[i][left]); 836 | // // } 837 | // // left++; 838 | // // } 839 | // // } 840 | // // return ans; 841 | // // } 842 | // //}; 843 | 844 | // //Rotate Image by 90 degree 845 | // //class Solution { 846 | // //public: 847 | // // void rotate(vector>& matrix) { 848 | // // int n = matrix.size(); 849 | // // for(int i=0; i<(n+1)/2;i++){ 850 | // // for(int j=0; j 876 | // //vector wavePrint(vector> arr, int nRows, int mCols) 877 | // //{ 878 | // // vector ans; 879 | // // 880 | // // for(int col=0; col=0; row--){ 883 | // // ans.push_back(arr[row][col]); 884 | // // } 885 | // // } 886 | // // else{ 887 | // // for(int row=0; row findDiagonalOrder(vector>& matrix) { 903 | // // // if(matrix.size()==0 || matrix[0].size()==0){ 904 | // // // return matrix; 905 | // // // } 906 | // // vector ans; 907 | // // int m = matrix.size(); 908 | // // int n = matrix[0].size(); 909 | // // int row=0, col=0; 910 | // // bool up = true; 911 | // // 912 | // // while(row0 && col0 && row 952 | // // 953 | // //int modularExponentiation(int x, int n, int m) { 954 | // // int res = 1; 955 | // // while(n>0){ 956 | // // if(n&1){ 957 | // // res = (1LL*(res) * (x)%m)%m; 958 | // // } 959 | // // x = (1LL * (x)%m * (x)%m) %m; 960 | // // n = n>>1; 961 | // // } 962 | // // return res; 963 | // //} 964 | 965 | 966 | 967 | #include 968 | using namespace std; 969 | 970 | int main() { 971 | int t; 972 | cin >> t; // Read number of test cases 973 | 974 | while (t--) { 975 | int a1, a2, b1, b2; 976 | cin >> a1 >> a2 >> b1 >> b2; 977 | 978 | int suneeWins = 0; 979 | 980 | // Compare the values in each possible way to determine Sunee's wins 981 | if ((a1 > b1 && a2 > b2) || (a1 > b2 && a2 > b1)) { 982 | suneeWins = 1; 983 | } 984 | 985 | cout << suneeWins << endl; 986 | } 987 | 988 | return 0; 989 | } 990 | 991 | 992 | #include 993 | using namespace std; 994 | 995 | int main() { 996 | int t; 997 | cin >> t; // Read number of test cases 998 | 999 | while(t--) { 1000 | int n; 1001 | cin >> n; // Read the two-digit positive integer 1002 | int sum = (n / 10) + (n % 10); // Calculate the sum of the digits 1003 | cout << sum << endl; // Output the result 1004 | } 1005 | 1006 | return 0; 1007 | } 1008 | 1009 | 1010 | #include 1011 | using namespace std; 1012 | 1013 | 1014 | int main() { 1015 | int t; 1016 | cin >> t; // Read number of test cases 1017 | 1018 | while(t--){ 1019 | int a1, a2, b1, b2; 1020 | cin>>a1>>a2>>b1>>b2; 1021 | 1022 | int suneeWins = 0; 1023 | 1024 | if(a1>b1 && a2>b2){ 1025 | suneeWins++; 1026 | } 1027 | if(a1>b2 && a2>b1){ 1028 | suneeWins++; 1029 | } 1030 | if(a2>b1 && a1>b2){ 1031 | suneeWins++; 1032 | } 1033 | 1034 | cout<& nums) { 5 | int n = nums.size(); 6 | int Max = nums[0], a = 1, b = 1; 7 | 8 | for(int i = 0; i < n; i++){ 9 | a = (a == 0 ? 1 : a) * nums[i]; 10 | b = (b == 0 ? 1 : b) * nums[n - 1 - i]; 11 | 12 | Max = max(Max, max(a, b)); 13 | } 14 | 15 | return Max; 16 | } 17 | }; 18 | 19 | 20 | class Solution { 21 | public: 22 | int maxProduct(vector& nums) { 23 | int n = nums.size(); 24 | if (n == 0) return 0; 25 | 26 | // Use long long to handle large products 27 | long long maxProd = nums[0]; 28 | long long minProd = nums[0]; 29 | long long result = nums[0]; 30 | 31 | // Traverse the array starting from the second element 32 | for (int i = 1; i < n; i++) { 33 | // If nums[i] is negative, swap maxProd and minProd 34 | if (nums[i] < 0) { 35 | swap(maxProd, minProd); 36 | } 37 | 38 | // Update maxProd and minProd 39 | maxProd = max((long long)nums[i], maxProd * nums[i]); 40 | minProd = min((long long)nums[i], minProd * nums[i]); 41 | 42 | // Update the result 43 | result = max(result, maxProd); 44 | } 45 | 46 | return (int)result; // Cast back to int before returning 47 | } 48 | }; 49 | 50 | 51 | 52 | 53 | 54 | //42. Trapping Rain Water 55 | class Solution { 56 | public: 57 | int trap(vector& height) { 58 | int n = height.size(); 59 | int left[n]; 60 | int right[n]; 61 | left[0] = height[0]; 62 | 63 | for(int i=1; i=0; i--){ 69 | right[i] = max(right[i+1], height[i]); 70 | } 71 | int ssum=0; 72 | for(int i=0; i& height) { 82 | int min = height[0]; 83 | vector left; 84 | for(int i=0; i right; 90 | for(int i=height.size(); i>-0; i--){ 91 | Max = max(Max, height[i]); 92 | right.push_back(Max); 93 | } 94 | vector sumarr; 95 | for(int i=0; i& nums, int k) { 113 | sort(nums.begin(), nums.end()); 114 | int n = nums.size(); 115 | return nums[n-k]; 116 | } 117 | }; 118 | 119 | 120 | class Solution { 121 | public: 122 | int findKthLargest(vector& nums, int k) { 123 | sort(nums.begin(), nums.end()); 124 | int max=0; 125 | int size = nums.size(); 126 | if(size==1){ 127 | return 1; 128 | } 129 | int till = size-k; 130 | for(int i=0; i frequencies(arr, arr + 26); 153 | sort(frequencies.begin(), frequencies.end()); 154 | 155 | int count = 0; 156 | int minKeyPress = 0; 157 | 158 | for(int i = 25; i >= 0; i--) { 159 | if(frequencies[i] == 0) break; 160 | int val = frequencies[i] * (count / 8 + 1); 161 | minKeyPress += val; 162 | count++; 163 | } 164 | 165 | return minKeyPress; 166 | } 167 | }; 168 | 169 | class Solution { 170 | public: 171 | int minimumPushes(string word) { 172 | int arr[26]; 173 | for(char ch: word){ 174 | arr[ch-'a']++; 175 | } 176 | sort(arr[].begin(), arr[].end()); 177 | 178 | int count=0; 179 | int minkeypress=0; 180 | 181 | for(int i=25; i>=0; i--){ 182 | if(arr[i]==0) break; 183 | int val = arr[i]*(count/8 + 1); 184 | minkeypress += val; 185 | count++; 186 | } 187 | return minkeypress; 188 | } 189 | }; 190 | 191 | 192 | #include 193 | #include 194 | 195 | class Solution { 196 | public: 197 | int minimumPushes(std::string word) { 198 | std::unordered_map keyMap = { 199 | {'a', 2}, {'b', 3}, {'c', 4}, {'d', 5}, {'e', 6}, {'f', 2}, {'g', 2}, 200 | {'h', 3}, {'i', 3}, {'j', 4}, {'k', 4}, {'l', 5}, {'m', 5}, {'n', 6}, 201 | {'o', 6}, {'p', 7}, {'q', 7}, {'r', 7}, {'s', 7}, {'t', 8}, {'u', 8}, 202 | {'v', 8}, {'w', 9}, {'x', 9}, {'y', 9}, {'z', 9} 203 | }; 204 | 205 | int totalPushes = 0; 206 | for (char c : word) { 207 | totalPushes += keyMap[c]; 208 | } 209 | 210 | return totalPushes; 211 | } 212 | }; 213 | 214 | 215 | class Solution { 216 | public: 217 | int minimumPushes(string word) { 218 | int two = 0, three = 0, four=0, five=0, six=0, seven=0, eight=0, nine=0; 219 | for( char c: word){ 220 | if(word[c]=='a' || word[c]=='f' || word[c]=='g'){ 221 | two++; 222 | } 223 | if(word[c]=='b' || word[c]=='h' || word[c]=='i'){ 224 | three++; 225 | } 226 | if(word[c]=='c' || word[c]=='j' || word[c]=='k'){ 227 | four++; 228 | } 229 | if(word[c]=='d' || word[c]=='l' || word[c]=='m'){ 230 | five++; 231 | } 232 | if(word[c]=='e' || word[c]=='n' || word[c]=='o'){ 233 | six++; 234 | } 235 | if(word[c]=='p' || word[c]=='q' || word[c]=='r' || word[c]=='s'){ 236 | seven++; 237 | } 238 | if(word[c]=='t' || word[c]=='u' || word[c]=='v'){ 239 | eight++; 240 | } 241 | if(word[c]=='w' || word[c]=='x' || word[c]=='y' || word[c]=='z'){ 242 | nine++; 243 | } 244 | } 245 | int ans = two+three+four+five+six+seven+eight+nine; 246 | return ans; 247 | } 248 | }; 249 | 250 | 251 | //2053. Kth Distinct String in an Array 252 | 253 | class Solution { 254 | public: 255 | string kthDistinct(vector& arr, int k) { 256 | unordered_map mp; 257 | int i; 258 | 259 | for(i=0; i& arr, int k) { 279 | vector& ans; 280 | int j=0, count=0; 281 | for(int i=0; i0){ 291 | ans = arr[i]; 292 | } 293 | } 294 | j=0; 295 | } 296 | // if(k==0){ 297 | // return ans; 298 | // } 299 | return ans; 300 | } 301 | }; 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | //1508. Range Sum of Sorted Subarray Sums 310 | //SOLUTION-1 311 | class Solution { 312 | public: 313 | int rangeSum(vector& nums, int n, int left, int right) { 314 | const unsigned int M = 1000000007; 315 | vector sums; 316 | 317 | // Generate all subarray sums 318 | for(int i = 0; i < n; i++) { 319 | int current_sum = 0; 320 | for(int j = i; j < n; j++) { 321 | current_sum = (current_sum + nums[j]) % M; 322 | sums.push_back(current_sum); 323 | } 324 | } 325 | 326 | // Sort the subarray sums 327 | sort(sums.begin(), sums.end()); 328 | 329 | // Calculate the sum of elements from `left-1` to `right-1` 330 | long long result = 0; 331 | for(int i = left - 1; i < right; i++) { 332 | result = (result + sums[i]) % M; 333 | } 334 | 335 | return (int)result; 336 | } 337 | }; 338 | 339 | 340 | //SOLUTION-2 341 | class Solution { 342 | public: 343 | int rangeSum(vector& nums, int n, int left, int right) { 344 | long long sum = 0; 345 | const unsigned int M = 1000000007; 346 | vector csum; 347 | vector sums; 348 | for(int i=0; i& nums, int target) { 372 | int low=0; 373 | int high = nums.size() -1; 374 | 375 | while( low <= high){ 376 | int mid = (low + high) / 2; 377 | 378 | if(nums[mid] == target) return mid; 379 | 380 | if(nums[low] <= nums[mid]){ 381 | if(nums[low] <= target && target <= nums[mid]){ 382 | high = mid - 1; 383 | }else{ 384 | low = mid + 1; 385 | } 386 | }else{ 387 | if(nums[mid] <= target && target <= nums[high]){ 388 | low = mid + 1; 389 | }else{ 390 | high = mid - 1; 391 | } 392 | } 393 | } 394 | return -1; 395 | } 396 | }; 397 | 398 | 399 | 400 | //1, 3, 4, 7, 9, 9 12, 56 401 | 402 | int min = LLONG_MAX; 403 | 404 | for(int i=0; i i& nums) { 417 | unordered_sets(nums.begin(), nums.end()); 418 | return nums.size() != s.size(); 419 | } 420 | }; 421 | 422 | 423 | class Solution { 424 | public: 425 | bool containsDuplicate(vector& nums) { 426 | sort(nums.begin(), nums.end()); 427 | int n = nums.size(); 428 | for(int i=1; i& nums) { 442 | int n = nums.size(); 443 | int o=0, z=0; 444 | 445 | for(int i=0; i60){ 476 | count++; 477 | } 478 | return count; 479 | } 480 | 481 | 482 | 483 | 484 | 485 | // 53. Maximum Subarray 486 | // Approach 1: Brute Force 487 | // for(int i=0 ->n){ 488 | // for(int j=0 ->n){ 489 | // for(int k=i -> j){ 490 | // sum = sum + arr[k]; 491 | // } 492 | // } 493 | // } 494 | 495 | // for(int i=0 ->n){ 496 | // sum 497 | // for(int j=0 ->n){ 498 | // for(int k=i -> j){ 499 | // sum = sum + arr[k]; 500 | // } 501 | // } 502 | // } 503 | 504 | 505 | 506 | class Solution { 507 | public: 508 | int maxSubArray(vector& nums) { 509 | int sum = 0; 510 | int maxi = nums[0]; 511 | 512 | 513 | for(int i=0; i 532 | using namespace std; 533 | 534 | int main() { 535 | int n = 0; 536 | int arr[n]; 537 | int max, min; 538 | cin>>n; 539 | for(int i=0; i>arr[i]; 541 | } 542 | if(n==0){ 543 | max = arr[0]; 544 | min = arr[0]; 545 | } 546 | if(n==1){ 547 | if(arr[0]>arr[1]){ 548 | max = arr[0]; 549 | min = arr[1]; 550 | } 551 | else{ 552 | max = arr[1]; 553 | min = arr[0]; 554 | } 555 | } 556 | else{ 557 | if(arr[0]>arr[1]){ 558 | max = arr[0]; 559 | min = arr[1]; 560 | } 561 | else{ 562 | max = arr[1]; 563 | min = arr[0]; 564 | } 565 | for(int i=2; iarr[i]){ 570 | min = arr[i]; 571 | } 572 | } 573 | } 574 | cout<<"Max" << max< 584 | using namespace std; 585 | 586 | int main() { 587 | 588 | int n, i, j, temp; 589 | int arr[n]; 590 | 591 | cin>>n; 592 | 593 | for(int k=0; k> arr[k]; 595 | } 596 | 597 | 598 | for(int i=0, j=n-1; i 616 | // using namespace std; 617 | 618 | // /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 619 | 620 | // void printSum(int arr[][3], int row, int col){ 621 | // for(int row=0; row<3; row++){ 622 | // int sum = 0; 623 | // for(int col=0; col<3; col++){ 624 | // sum += arr[row][col]; 625 | // } 626 | // cout< maxi){ 664 | // maxi = sum; 665 | // rowIndex = row; 666 | // } 667 | // } 668 | // return rowIndex; 669 | // } 670 | 671 | 672 | // int main(int argc, char** argv) { 673 | 674 | // // int arr1[3][4] = {{1,11,111,1111}, {2,22,222,2222}, {3,33,333,3333}}; 675 | // int arr2[3][3]; 676 | 677 | // for(int row=0; row<3; row++){ 678 | // for(int col=0; col<3; col++){ 679 | // cin>>arr2[row][col]; 680 | // } 681 | // } 682 | 683 | // for(int row=0; row<3; row++){ 684 | // for(int col=0; col<3; col++){ 685 | // cout< prime(n+1, true); 698 | // // prime[0] = prime[1] = false; 699 | // // 700 | // // for(int i=2; i>target; 740 | 741 | // // if(isPresent(arr1, target, 3, 4)){ 742 | // // cout<<"Element Found"; 743 | // // } 744 | // // else{ 745 | // // cout<<"Element Not Found"; 746 | // // } 747 | 748 | // cout<<"Row Wise Sum = "; 749 | // printSum(arr2, 3, 3); 750 | // cout< spiralOrder(vector>& matrix) { 765 | // // int row = matrix.size(); 766 | // // int col = matrix[0].size(); 767 | // // vector ans; 768 | // // int count = 0; 769 | // // int total = row*col; 770 | // // int startingRow = 0; 771 | // // int startingCol = 0; 772 | // // int endingRow = row-1; 773 | // // int endingCol = col-1; 774 | // // 775 | // // while(count=startingCol; i--){ 790 | // // ans.push_back(matrix[endingRow][i]); 791 | // // count++; 792 | // // } 793 | // // endingRow--; 794 | // // 795 | // // for(int i=endingRow; count=startingRow; i--){ 796 | // // ans.push_back(matrix[i][startingCol]); 797 | // // count++; 798 | // // } 799 | // // startingCol++; 800 | // // } 801 | // // return ans; 802 | // // } 803 | // //}; 804 | // //class Solution { 805 | // //public: 806 | // // vector spiralOrder(vector>& matrix) { 807 | // // int n=matrix.size(),m=matrix[0].size(); 808 | // // int left=0,top=0; 809 | // // int bottom=n-1,right=m-1; 810 | // // vectorans; 811 | // // while(left<=right&&top<=bottom) 812 | // // { 813 | // // for(int i=left;i<=right;i++) 814 | // // { 815 | // // ans.push_back(matrix[top][i]); 816 | // // } 817 | // // top++; 818 | // // for(int i=top;i<=bottom;i++) 819 | // // { 820 | // // ans.push_back(matrix[i][right]); 821 | // // } 822 | // // right--; 823 | // // if(top<=bottom) 824 | // // { 825 | // // for(int i=right;i>=left;i--) 826 | // // { 827 | // // ans.push_back(matrix[bottom][i]); 828 | // // } 829 | // // bottom--; 830 | // // } 831 | // // if(left<=right) 832 | // // { 833 | // // for(int i=bottom;i>=top;i--) 834 | // // { 835 | // // ans.push_back(matrix[i][left]); 836 | // // } 837 | // // left++; 838 | // // } 839 | // // } 840 | // // return ans; 841 | // // } 842 | // //}; 843 | 844 | // //Rotate Image by 90 degree 845 | // //class Solution { 846 | // //public: 847 | // // void rotate(vector>& matrix) { 848 | // // int n = matrix.size(); 849 | // // for(int i=0; i<(n+1)/2;i++){ 850 | // // for(int j=0; j 876 | // //vector wavePrint(vector> arr, int nRows, int mCols) 877 | // //{ 878 | // // vector ans; 879 | // // 880 | // // for(int col=0; col=0; row--){ 883 | // // ans.push_back(arr[row][col]); 884 | // // } 885 | // // } 886 | // // else{ 887 | // // for(int row=0; row findDiagonalOrder(vector>& matrix) { 903 | // // // if(matrix.size()==0 || matrix[0].size()==0){ 904 | // // // return matrix; 905 | // // // } 906 | // // vector ans; 907 | // // int m = matrix.size(); 908 | // // int n = matrix[0].size(); 909 | // // int row=0, col=0; 910 | // // bool up = true; 911 | // // 912 | // // while(row0 && col0 && row 952 | // // 953 | // //int modularExponentiation(int x, int n, int m) { 954 | // // int res = 1; 955 | // // while(n>0){ 956 | // // if(n&1){ 957 | // // res = (1LL*(res) * (x)%m)%m; 958 | // // } 959 | // // x = (1LL * (x)%m * (x)%m) %m; 960 | // // n = n>>1; 961 | // // } 962 | // // return res; 963 | // //} 964 | 965 | 966 | 967 | #include 968 | using namespace std; 969 | 970 | int main() { 971 | int t; 972 | cin >> t; // Read number of test cases 973 | 974 | while (t--) { 975 | int a1, a2, b1, b2; 976 | cin >> a1 >> a2 >> b1 >> b2; 977 | 978 | int suneeWins = 0; 979 | 980 | // Compare the values in each possible way to determine Sunee's wins 981 | if ((a1 > b1 && a2 > b2) || (a1 > b2 && a2 > b1)) { 982 | suneeWins = 1; 983 | } 984 | 985 | cout << suneeWins << endl; 986 | } 987 | 988 | return 0; 989 | } 990 | 991 | 992 | #include 993 | using namespace std; 994 | 995 | int main() { 996 | int t; 997 | cin >> t; // Read number of test cases 998 | 999 | while(t--) { 1000 | int n; 1001 | cin >> n; // Read the two-digit positive integer 1002 | int sum = (n / 10) + (n % 10); // Calculate the sum of the digits 1003 | cout << sum << endl; // Output the result 1004 | } 1005 | 1006 | return 0; 1007 | } 1008 | 1009 | 1010 | #include 1011 | using namespace std; 1012 | 1013 | 1014 | int main() { 1015 | int t; 1016 | cin >> t; // Read number of test cases 1017 | 1018 | while(t--){ 1019 | int a1, a2, b1, b2; 1020 | cin>>a1>>a2>>b1>>b2; 1021 | 1022 | int suneeWins = 0; 1023 | 1024 | if(a1>b1 && a2>b2){ 1025 | suneeWins++; 1026 | } 1027 | if(a1>b2 && a2>b1){ 1028 | suneeWins++; 1029 | } 1030 | if(a2>b1 && a1>b2){ 1031 | suneeWins++; 1032 | } 1033 | 1034 | cout<& nums) { 5 | int n = nums.size(); 6 | int Max = nums[0], a = 1, b = 1; 7 | 8 | for(int i = 0; i < n; i++){ 9 | a = (a == 0 ? 1 : a) * nums[i]; 10 | b = (b == 0 ? 1 : b) * nums[n - 1 - i]; 11 | 12 | Max = max(Max, max(a, b)); 13 | } 14 | 15 | return Max; 16 | } 17 | }; 18 | 19 | 20 | class Solution { 21 | public: 22 | int maxProduct(vector& nums) { 23 | int n = nums.size(); 24 | if (n == 0) return 0; 25 | 26 | // Use long long to handle large products 27 | long long maxProd = nums[0]; 28 | long long minProd = nums[0]; 29 | long long result = nums[0]; 30 | 31 | // Traverse the array starting from the second element 32 | for (int i = 1; i < n; i++) { 33 | // If nums[i] is negative, swap maxProd and minProd 34 | if (nums[i] < 0) { 35 | swap(maxProd, minProd); 36 | } 37 | 38 | // Update maxProd and minProd 39 | maxProd = max((long long)nums[i], maxProd * nums[i]); 40 | minProd = min((long long)nums[i], minProd * nums[i]); 41 | 42 | // Update the result 43 | result = max(result, maxProd); 44 | } 45 | 46 | return (int)result; // Cast back to int before returning 47 | } 48 | }; 49 | 50 | 51 | 52 | 53 | 54 | //42. Trapping Rain Water 55 | class Solution { 56 | public: 57 | int trap(vector& height) { 58 | int n = height.size(); 59 | int left[n]; 60 | int right[n]; 61 | left[0] = height[0]; 62 | 63 | for(int i=1; i=0; i--){ 69 | right[i] = max(right[i+1], height[i]); 70 | } 71 | int ssum=0; 72 | for(int i=0; i& height) { 82 | int min = height[0]; 83 | vector left; 84 | for(int i=0; i right; 90 | for(int i=height.size(); i>-0; i--){ 91 | Max = max(Max, height[i]); 92 | right.push_back(Max); 93 | } 94 | vector sumarr; 95 | for(int i=0; i& nums, int k) { 113 | sort(nums.begin(), nums.end()); 114 | int n = nums.size(); 115 | return nums[n-k]; 116 | } 117 | }; 118 | 119 | 120 | class Solution { 121 | public: 122 | int findKthLargest(vector& nums, int k) { 123 | sort(nums.begin(), nums.end()); 124 | int max=0; 125 | int size = nums.size(); 126 | if(size==1){ 127 | return 1; 128 | } 129 | int till = size-k; 130 | for(int i=0; i frequencies(arr, arr + 26); 153 | sort(frequencies.begin(), frequencies.end()); 154 | 155 | int count = 0; 156 | int minKeyPress = 0; 157 | 158 | for(int i = 25; i >= 0; i--) { 159 | if(frequencies[i] == 0) break; 160 | int val = frequencies[i] * (count / 8 + 1); 161 | minKeyPress += val; 162 | count++; 163 | } 164 | 165 | return minKeyPress; 166 | } 167 | }; 168 | 169 | class Solution { 170 | public: 171 | int minimumPushes(string word) { 172 | int arr[26]; 173 | for(char ch: word){ 174 | arr[ch-'a']++; 175 | } 176 | sort(arr[].begin(), arr[].end()); 177 | 178 | int count=0; 179 | int minkeypress=0; 180 | 181 | for(int i=25; i>=0; i--){ 182 | if(arr[i]==0) break; 183 | int val = arr[i]*(count/8 + 1); 184 | minkeypress += val; 185 | count++; 186 | } 187 | return minkeypress; 188 | } 189 | }; 190 | 191 | 192 | #include 193 | #include 194 | 195 | class Solution { 196 | public: 197 | int minimumPushes(std::string word) { 198 | std::unordered_map keyMap = { 199 | {'a', 2}, {'b', 3}, {'c', 4}, {'d', 5}, {'e', 6}, {'f', 2}, {'g', 2}, 200 | {'h', 3}, {'i', 3}, {'j', 4}, {'k', 4}, {'l', 5}, {'m', 5}, {'n', 6}, 201 | {'o', 6}, {'p', 7}, {'q', 7}, {'r', 7}, {'s', 7}, {'t', 8}, {'u', 8}, 202 | {'v', 8}, {'w', 9}, {'x', 9}, {'y', 9}, {'z', 9} 203 | }; 204 | 205 | int totalPushes = 0; 206 | for (char c : word) { 207 | totalPushes += keyMap[c]; 208 | } 209 | 210 | return totalPushes; 211 | } 212 | }; 213 | 214 | 215 | class Solution { 216 | public: 217 | int minimumPushes(string word) { 218 | int two = 0, three = 0, four=0, five=0, six=0, seven=0, eight=0, nine=0; 219 | for( char c: word){ 220 | if(word[c]=='a' || word[c]=='f' || word[c]=='g'){ 221 | two++; 222 | } 223 | if(word[c]=='b' || word[c]=='h' || word[c]=='i'){ 224 | three++; 225 | } 226 | if(word[c]=='c' || word[c]=='j' || word[c]=='k'){ 227 | four++; 228 | } 229 | if(word[c]=='d' || word[c]=='l' || word[c]=='m'){ 230 | five++; 231 | } 232 | if(word[c]=='e' || word[c]=='n' || word[c]=='o'){ 233 | six++; 234 | } 235 | if(word[c]=='p' || word[c]=='q' || word[c]=='r' || word[c]=='s'){ 236 | seven++; 237 | } 238 | if(word[c]=='t' || word[c]=='u' || word[c]=='v'){ 239 | eight++; 240 | } 241 | if(word[c]=='w' || word[c]=='x' || word[c]=='y' || word[c]=='z'){ 242 | nine++; 243 | } 244 | } 245 | int ans = two+three+four+five+six+seven+eight+nine; 246 | return ans; 247 | } 248 | }; 249 | 250 | 251 | //2053. Kth Distinct String in an Array 252 | 253 | class Solution { 254 | public: 255 | string kthDistinct(vector& arr, int k) { 256 | unordered_map mp; 257 | int i; 258 | 259 | for(i=0; i& arr, int k) { 279 | vector& ans; 280 | int j=0, count=0; 281 | for(int i=0; i0){ 291 | ans = arr[i]; 292 | } 293 | } 294 | j=0; 295 | } 296 | // if(k==0){ 297 | // return ans; 298 | // } 299 | return ans; 300 | } 301 | }; 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | //1508. Range Sum of Sorted Subarray Sums 310 | //SOLUTION-1 311 | class Solution { 312 | public: 313 | int rangeSum(vector& nums, int n, int left, int right) { 314 | const unsigned int M = 1000000007; 315 | vector sums; 316 | 317 | // Generate all subarray sums 318 | for(int i = 0; i < n; i++) { 319 | int current_sum = 0; 320 | for(int j = i; j < n; j++) { 321 | current_sum = (current_sum + nums[j]) % M; 322 | sums.push_back(current_sum); 323 | } 324 | } 325 | 326 | // Sort the subarray sums 327 | sort(sums.begin(), sums.end()); 328 | 329 | // Calculate the sum of elements from `left-1` to `right-1` 330 | long long result = 0; 331 | for(int i = left - 1; i < right; i++) { 332 | result = (result + sums[i]) % M; 333 | } 334 | 335 | return (int)result; 336 | } 337 | }; 338 | 339 | 340 | //SOLUTION-2 341 | class Solution { 342 | public: 343 | int rangeSum(vector& nums, int n, int left, int right) { 344 | long long sum = 0; 345 | const unsigned int M = 1000000007; 346 | vector csum; 347 | vector sums; 348 | for(int i=0; i& nums, int target) { 372 | int low=0; 373 | int high = nums.size() -1; 374 | 375 | while( low <= high){ 376 | int mid = (low + high) / 2; 377 | 378 | if(nums[mid] == target) return mid; 379 | 380 | if(nums[low] <= nums[mid]){ 381 | if(nums[low] <= target && target <= nums[mid]){ 382 | high = mid - 1; 383 | }else{ 384 | low = mid + 1; 385 | } 386 | }else{ 387 | if(nums[mid] <= target && target <= nums[high]){ 388 | low = mid + 1; 389 | }else{ 390 | high = mid - 1; 391 | } 392 | } 393 | } 394 | return -1; 395 | } 396 | }; 397 | 398 | 399 | 400 | //1, 3, 4, 7, 9, 9 12, 56 401 | 402 | int min = LLONG_MAX; 403 | 404 | for(int i=0; i i& nums) { 417 | unordered_sets(nums.begin(), nums.end()); 418 | return nums.size() != s.size(); 419 | } 420 | }; 421 | 422 | 423 | class Solution { 424 | public: 425 | bool containsDuplicate(vector& nums) { 426 | sort(nums.begin(), nums.end()); 427 | int n = nums.size(); 428 | for(int i=1; i& nums) { 442 | int n = nums.size(); 443 | int o=0, z=0; 444 | 445 | for(int i=0; i60){ 476 | count++; 477 | } 478 | return count; 479 | } 480 | 481 | 482 | 483 | 484 | 485 | // 53. Maximum Subarray 486 | // Approach 1: Brute Force 487 | // for(int i=0 ->n){ 488 | // for(int j=0 ->n){ 489 | // for(int k=i -> j){ 490 | // sum = sum + arr[k]; 491 | // } 492 | // } 493 | // } 494 | 495 | // for(int i=0 ->n){ 496 | // sum 497 | // for(int j=0 ->n){ 498 | // for(int k=i -> j){ 499 | // sum = sum + arr[k]; 500 | // } 501 | // } 502 | // } 503 | 504 | 505 | 506 | class Solution { 507 | public: 508 | int maxSubArray(vector& nums) { 509 | int sum = 0; 510 | int maxi = nums[0]; 511 | 512 | 513 | for(int i=0; i 532 | using namespace std; 533 | 534 | int main() { 535 | int n = 0; 536 | int arr[n]; 537 | int max, min; 538 | cin>>n; 539 | for(int i=0; i>arr[i]; 541 | } 542 | if(n==0){ 543 | max = arr[0]; 544 | min = arr[0]; 545 | } 546 | if(n==1){ 547 | if(arr[0]>arr[1]){ 548 | max = arr[0]; 549 | min = arr[1]; 550 | } 551 | else{ 552 | max = arr[1]; 553 | min = arr[0]; 554 | } 555 | } 556 | else{ 557 | if(arr[0]>arr[1]){ 558 | max = arr[0]; 559 | min = arr[1]; 560 | } 561 | else{ 562 | max = arr[1]; 563 | min = arr[0]; 564 | } 565 | for(int i=2; iarr[i]){ 570 | min = arr[i]; 571 | } 572 | } 573 | } 574 | cout<<"Max" << max< 584 | using namespace std; 585 | 586 | int main() { 587 | 588 | int n, i, j, temp; 589 | int arr[n]; 590 | 591 | cin>>n; 592 | 593 | for(int k=0; k> arr[k]; 595 | } 596 | 597 | 598 | for(int i=0, j=n-1; i 616 | // using namespace std; 617 | 618 | // /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 619 | 620 | // void printSum(int arr[][3], int row, int col){ 621 | // for(int row=0; row<3; row++){ 622 | // int sum = 0; 623 | // for(int col=0; col<3; col++){ 624 | // sum += arr[row][col]; 625 | // } 626 | // cout< maxi){ 664 | // maxi = sum; 665 | // rowIndex = row; 666 | // } 667 | // } 668 | // return rowIndex; 669 | // } 670 | 671 | 672 | // int main(int argc, char** argv) { 673 | 674 | // // int arr1[3][4] = {{1,11,111,1111}, {2,22,222,2222}, {3,33,333,3333}}; 675 | // int arr2[3][3]; 676 | 677 | // for(int row=0; row<3; row++){ 678 | // for(int col=0; col<3; col++){ 679 | // cin>>arr2[row][col]; 680 | // } 681 | // } 682 | 683 | // for(int row=0; row<3; row++){ 684 | // for(int col=0; col<3; col++){ 685 | // cout< prime(n+1, true); 698 | // // prime[0] = prime[1] = false; 699 | // // 700 | // // for(int i=2; i>target; 740 | 741 | // // if(isPresent(arr1, target, 3, 4)){ 742 | // // cout<<"Element Found"; 743 | // // } 744 | // // else{ 745 | // // cout<<"Element Not Found"; 746 | // // } 747 | 748 | // cout<<"Row Wise Sum = "; 749 | // printSum(arr2, 3, 3); 750 | // cout< spiralOrder(vector>& matrix) { 765 | // // int row = matrix.size(); 766 | // // int col = matrix[0].size(); 767 | // // vector ans; 768 | // // int count = 0; 769 | // // int total = row*col; 770 | // // int startingRow = 0; 771 | // // int startingCol = 0; 772 | // // int endingRow = row-1; 773 | // // int endingCol = col-1; 774 | // // 775 | // // while(count=startingCol; i--){ 790 | // // ans.push_back(matrix[endingRow][i]); 791 | // // count++; 792 | // // } 793 | // // endingRow--; 794 | // // 795 | // // for(int i=endingRow; count=startingRow; i--){ 796 | // // ans.push_back(matrix[i][startingCol]); 797 | // // count++; 798 | // // } 799 | // // startingCol++; 800 | // // } 801 | // // return ans; 802 | // // } 803 | // //}; 804 | // //class Solution { 805 | // //public: 806 | // // vector spiralOrder(vector>& matrix) { 807 | // // int n=matrix.size(),m=matrix[0].size(); 808 | // // int left=0,top=0; 809 | // // int bottom=n-1,right=m-1; 810 | // // vectorans; 811 | // // while(left<=right&&top<=bottom) 812 | // // { 813 | // // for(int i=left;i<=right;i++) 814 | // // { 815 | // // ans.push_back(matrix[top][i]); 816 | // // } 817 | // // top++; 818 | // // for(int i=top;i<=bottom;i++) 819 | // // { 820 | // // ans.push_back(matrix[i][right]); 821 | // // } 822 | // // right--; 823 | // // if(top<=bottom) 824 | // // { 825 | // // for(int i=right;i>=left;i--) 826 | // // { 827 | // // ans.push_back(matrix[bottom][i]); 828 | // // } 829 | // // bottom--; 830 | // // } 831 | // // if(left<=right) 832 | // // { 833 | // // for(int i=bottom;i>=top;i--) 834 | // // { 835 | // // ans.push_back(matrix[i][left]); 836 | // // } 837 | // // left++; 838 | // // } 839 | // // } 840 | // // return ans; 841 | // // } 842 | // //}; 843 | 844 | // //Rotate Image by 90 degree 845 | // //class Solution { 846 | // //public: 847 | // // void rotate(vector>& matrix) { 848 | // // int n = matrix.size(); 849 | // // for(int i=0; i<(n+1)/2;i++){ 850 | // // for(int j=0; j 876 | // //vector wavePrint(vector> arr, int nRows, int mCols) 877 | // //{ 878 | // // vector ans; 879 | // // 880 | // // for(int col=0; col=0; row--){ 883 | // // ans.push_back(arr[row][col]); 884 | // // } 885 | // // } 886 | // // else{ 887 | // // for(int row=0; row findDiagonalOrder(vector>& matrix) { 903 | // // // if(matrix.size()==0 || matrix[0].size()==0){ 904 | // // // return matrix; 905 | // // // } 906 | // // vector ans; 907 | // // int m = matrix.size(); 908 | // // int n = matrix[0].size(); 909 | // // int row=0, col=0; 910 | // // bool up = true; 911 | // // 912 | // // while(row0 && col0 && row 952 | // // 953 | // //int modularExponentiation(int x, int n, int m) { 954 | // // int res = 1; 955 | // // while(n>0){ 956 | // // if(n&1){ 957 | // // res = (1LL*(res) * (x)%m)%m; 958 | // // } 959 | // // x = (1LL * (x)%m * (x)%m) %m; 960 | // // n = n>>1; 961 | // // } 962 | // // return res; 963 | // //} 964 | 965 | 966 | 967 | #include 968 | using namespace std; 969 | 970 | int main() { 971 | int t; 972 | cin >> t; // Read number of test cases 973 | 974 | while (t--) { 975 | int a1, a2, b1, b2; 976 | cin >> a1 >> a2 >> b1 >> b2; 977 | 978 | int suneeWins = 0; 979 | 980 | // Compare the values in each possible way to determine Sunee's wins 981 | if ((a1 > b1 && a2 > b2) || (a1 > b2 && a2 > b1)) { 982 | suneeWins = 1; 983 | } 984 | 985 | cout << suneeWins << endl; 986 | } 987 | 988 | return 0; 989 | } 990 | 991 | 992 | #include 993 | using namespace std; 994 | 995 | int main() { 996 | int t; 997 | cin >> t; // Read number of test cases 998 | 999 | while(t--) { 1000 | int n; 1001 | cin >> n; // Read the two-digit positive integer 1002 | int sum = (n / 10) + (n % 10); // Calculate the sum of the digits 1003 | cout << sum << endl; // Output the result 1004 | } 1005 | 1006 | return 0; 1007 | } 1008 | 1009 | 1010 | #include 1011 | using namespace std; 1012 | 1013 | 1014 | int main() { 1015 | int t; 1016 | cin >> t; // Read number of test cases 1017 | 1018 | while(t--){ 1019 | int a1, a2, b1, b2; 1020 | cin>>a1>>a2>>b1>>b2; 1021 | 1022 | int suneeWins = 0; 1023 | 1024 | if(a1>b1 && a2>b2){ 1025 | suneeWins++; 1026 | } 1027 | if(a1>b2 && a2>b1){ 1028 | suneeWins++; 1029 | } 1030 | if(a2>b1 && a1>b2){ 1031 | suneeWins++; 1032 | } 1033 | 1034 | cout<& nums) { 5 | int n = nums.size(); 6 | int Max = nums[0], a = 1, b = 1; 7 | 8 | for(int i = 0; i < n; i++){ 9 | a = (a == 0 ? 1 : a) * nums[i]; 10 | b = (b == 0 ? 1 : b) * nums[n - 1 - i]; 11 | 12 | Max = max(Max, max(a, b)); 13 | } 14 | 15 | return Max; 16 | } 17 | }; 18 | 19 | 20 | class Solution { 21 | public: 22 | int maxProduct(vector& nums) { 23 | int n = nums.size(); 24 | if (n == 0) return 0; 25 | 26 | // Use long long to handle large products 27 | long long maxProd = nums[0]; 28 | long long minProd = nums[0]; 29 | long long result = nums[0]; 30 | 31 | // Traverse the array starting from the second element 32 | for (int i = 1; i < n; i++) { 33 | // If nums[i] is negative, swap maxProd and minProd 34 | if (nums[i] < 0) { 35 | swap(maxProd, minProd); 36 | } 37 | 38 | // Update maxProd and minProd 39 | maxProd = max((long long)nums[i], maxProd * nums[i]); 40 | minProd = min((long long)nums[i], minProd * nums[i]); 41 | 42 | // Update the result 43 | result = max(result, maxProd); 44 | } 45 | 46 | return (int)result; // Cast back to int before returning 47 | } 48 | }; 49 | 50 | 51 | 52 | 53 | 54 | //42. Trapping Rain Water 55 | class Solution { 56 | public: 57 | int trap(vector& height) { 58 | int n = height.size(); 59 | int left[n]; 60 | int right[n]; 61 | left[0] = height[0]; 62 | 63 | for(int i=1; i=0; i--){ 69 | right[i] = max(right[i+1], height[i]); 70 | } 71 | int ssum=0; 72 | for(int i=0; i& height) { 82 | int min = height[0]; 83 | vector left; 84 | for(int i=0; i right; 90 | for(int i=height.size(); i>-0; i--){ 91 | Max = max(Max, height[i]); 92 | right.push_back(Max); 93 | } 94 | vector sumarr; 95 | for(int i=0; i& nums, int k) { 113 | sort(nums.begin(), nums.end()); 114 | int n = nums.size(); 115 | return nums[n-k]; 116 | } 117 | }; 118 | 119 | 120 | class Solution { 121 | public: 122 | int findKthLargest(vector& nums, int k) { 123 | sort(nums.begin(), nums.end()); 124 | int max=0; 125 | int size = nums.size(); 126 | if(size==1){ 127 | return 1; 128 | } 129 | int till = size-k; 130 | for(int i=0; i frequencies(arr, arr + 26); 153 | sort(frequencies.begin(), frequencies.end()); 154 | 155 | int count = 0; 156 | int minKeyPress = 0; 157 | 158 | for(int i = 25; i >= 0; i--) { 159 | if(frequencies[i] == 0) break; 160 | int val = frequencies[i] * (count / 8 + 1); 161 | minKeyPress += val; 162 | count++; 163 | } 164 | 165 | return minKeyPress; 166 | } 167 | }; 168 | 169 | class Solution { 170 | public: 171 | int minimumPushes(string word) { 172 | int arr[26]; 173 | for(char ch: word){ 174 | arr[ch-'a']++; 175 | } 176 | sort(arr[].begin(), arr[].end()); 177 | 178 | int count=0; 179 | int minkeypress=0; 180 | 181 | for(int i=25; i>=0; i--){ 182 | if(arr[i]==0) break; 183 | int val = arr[i]*(count/8 + 1); 184 | minkeypress += val; 185 | count++; 186 | } 187 | return minkeypress; 188 | } 189 | }; 190 | 191 | 192 | #include 193 | #include 194 | 195 | class Solution { 196 | public: 197 | int minimumPushes(std::string word) { 198 | std::unordered_map keyMap = { 199 | {'a', 2}, {'b', 3}, {'c', 4}, {'d', 5}, {'e', 6}, {'f', 2}, {'g', 2}, 200 | {'h', 3}, {'i', 3}, {'j', 4}, {'k', 4}, {'l', 5}, {'m', 5}, {'n', 6}, 201 | {'o', 6}, {'p', 7}, {'q', 7}, {'r', 7}, {'s', 7}, {'t', 8}, {'u', 8}, 202 | {'v', 8}, {'w', 9}, {'x', 9}, {'y', 9}, {'z', 9} 203 | }; 204 | 205 | int totalPushes = 0; 206 | for (char c : word) { 207 | totalPushes += keyMap[c]; 208 | } 209 | 210 | return totalPushes; 211 | } 212 | }; 213 | 214 | 215 | class Solution { 216 | public: 217 | int minimumPushes(string word) { 218 | int two = 0, three = 0, four=0, five=0, six=0, seven=0, eight=0, nine=0; 219 | for( char c: word){ 220 | if(word[c]=='a' || word[c]=='f' || word[c]=='g'){ 221 | two++; 222 | } 223 | if(word[c]=='b' || word[c]=='h' || word[c]=='i'){ 224 | three++; 225 | } 226 | if(word[c]=='c' || word[c]=='j' || word[c]=='k'){ 227 | four++; 228 | } 229 | if(word[c]=='d' || word[c]=='l' || word[c]=='m'){ 230 | five++; 231 | } 232 | if(word[c]=='e' || word[c]=='n' || word[c]=='o'){ 233 | six++; 234 | } 235 | if(word[c]=='p' || word[c]=='q' || word[c]=='r' || word[c]=='s'){ 236 | seven++; 237 | } 238 | if(word[c]=='t' || word[c]=='u' || word[c]=='v'){ 239 | eight++; 240 | } 241 | if(word[c]=='w' || word[c]=='x' || word[c]=='y' || word[c]=='z'){ 242 | nine++; 243 | } 244 | } 245 | int ans = two+three+four+five+six+seven+eight+nine; 246 | return ans; 247 | } 248 | }; 249 | 250 | 251 | //2053. Kth Distinct String in an Array 252 | 253 | class Solution { 254 | public: 255 | string kthDistinct(vector& arr, int k) { 256 | unordered_map mp; 257 | int i; 258 | 259 | for(i=0; i& arr, int k) { 279 | vector& ans; 280 | int j=0, count=0; 281 | for(int i=0; i0){ 291 | ans = arr[i]; 292 | } 293 | } 294 | j=0; 295 | } 296 | // if(k==0){ 297 | // return ans; 298 | // } 299 | return ans; 300 | } 301 | }; 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | //1508. Range Sum of Sorted Subarray Sums 310 | //SOLUTION-1 311 | class Solution { 312 | public: 313 | int rangeSum(vector& nums, int n, int left, int right) { 314 | const unsigned int M = 1000000007; 315 | vector sums; 316 | 317 | // Generate all subarray sums 318 | for(int i = 0; i < n; i++) { 319 | int current_sum = 0; 320 | for(int j = i; j < n; j++) { 321 | current_sum = (current_sum + nums[j]) % M; 322 | sums.push_back(current_sum); 323 | } 324 | } 325 | 326 | // Sort the subarray sums 327 | sort(sums.begin(), sums.end()); 328 | 329 | // Calculate the sum of elements from `left-1` to `right-1` 330 | long long result = 0; 331 | for(int i = left - 1; i < right; i++) { 332 | result = (result + sums[i]) % M; 333 | } 334 | 335 | return (int)result; 336 | } 337 | }; 338 | 339 | 340 | //SOLUTION-2 341 | class Solution { 342 | public: 343 | int rangeSum(vector& nums, int n, int left, int right) { 344 | long long sum = 0; 345 | const unsigned int M = 1000000007; 346 | vector csum; 347 | vector sums; 348 | for(int i=0; i& nums, int target) { 372 | int low=0; 373 | int high = nums.size() -1; 374 | 375 | while( low <= high){ 376 | int mid = (low + high) / 2; 377 | 378 | if(nums[mid] == target) return mid; 379 | 380 | if(nums[low] <= nums[mid]){ 381 | if(nums[low] <= target && target <= nums[mid]){ 382 | high = mid - 1; 383 | }else{ 384 | low = mid + 1; 385 | } 386 | }else{ 387 | if(nums[mid] <= target && target <= nums[high]){ 388 | low = mid + 1; 389 | }else{ 390 | high = mid - 1; 391 | } 392 | } 393 | } 394 | return -1; 395 | } 396 | }; 397 | 398 | 399 | 400 | //1, 3, 4, 7, 9, 9 12, 56 401 | 402 | int min = LLONG_MAX; 403 | 404 | for(int i=0; i i& nums) { 417 | unordered_sets(nums.begin(), nums.end()); 418 | return nums.size() != s.size(); 419 | } 420 | }; 421 | 422 | 423 | class Solution { 424 | public: 425 | bool containsDuplicate(vector& nums) { 426 | sort(nums.begin(), nums.end()); 427 | int n = nums.size(); 428 | for(int i=1; i& nums) { 442 | int n = nums.size(); 443 | int o=0, z=0; 444 | 445 | for(int i=0; i60){ 476 | count++; 477 | } 478 | return count; 479 | } 480 | 481 | 482 | 483 | 484 | 485 | // 53. Maximum Subarray 486 | // Approach 1: Brute Force 487 | // for(int i=0 ->n){ 488 | // for(int j=0 ->n){ 489 | // for(int k=i -> j){ 490 | // sum = sum + arr[k]; 491 | // } 492 | // } 493 | // } 494 | 495 | // for(int i=0 ->n){ 496 | // sum 497 | // for(int j=0 ->n){ 498 | // for(int k=i -> j){ 499 | // sum = sum + arr[k]; 500 | // } 501 | // } 502 | // } 503 | 504 | 505 | 506 | class Solution { 507 | public: 508 | int maxSubArray(vector& nums) { 509 | int sum = 0; 510 | int maxi = nums[0]; 511 | 512 | 513 | for(int i=0; i 532 | using namespace std; 533 | 534 | int main() { 535 | int n = 0; 536 | int arr[n]; 537 | int max, min; 538 | cin>>n; 539 | for(int i=0; i>arr[i]; 541 | } 542 | if(n==0){ 543 | max = arr[0]; 544 | min = arr[0]; 545 | } 546 | if(n==1){ 547 | if(arr[0]>arr[1]){ 548 | max = arr[0]; 549 | min = arr[1]; 550 | } 551 | else{ 552 | max = arr[1]; 553 | min = arr[0]; 554 | } 555 | } 556 | else{ 557 | if(arr[0]>arr[1]){ 558 | max = arr[0]; 559 | min = arr[1]; 560 | } 561 | else{ 562 | max = arr[1]; 563 | min = arr[0]; 564 | } 565 | for(int i=2; iarr[i]){ 570 | min = arr[i]; 571 | } 572 | } 573 | } 574 | cout<<"Max" << max< 584 | using namespace std; 585 | 586 | int main() { 587 | 588 | int n, i, j, temp; 589 | int arr[n]; 590 | 591 | cin>>n; 592 | 593 | for(int k=0; k> arr[k]; 595 | } 596 | 597 | 598 | for(int i=0, j=n-1; i 616 | // using namespace std; 617 | 618 | // /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 619 | 620 | // void printSum(int arr[][3], int row, int col){ 621 | // for(int row=0; row<3; row++){ 622 | // int sum = 0; 623 | // for(int col=0; col<3; col++){ 624 | // sum += arr[row][col]; 625 | // } 626 | // cout< maxi){ 664 | // maxi = sum; 665 | // rowIndex = row; 666 | // } 667 | // } 668 | // return rowIndex; 669 | // } 670 | 671 | 672 | // int main(int argc, char** argv) { 673 | 674 | // // int arr1[3][4] = {{1,11,111,1111}, {2,22,222,2222}, {3,33,333,3333}}; 675 | // int arr2[3][3]; 676 | 677 | // for(int row=0; row<3; row++){ 678 | // for(int col=0; col<3; col++){ 679 | // cin>>arr2[row][col]; 680 | // } 681 | // } 682 | 683 | // for(int row=0; row<3; row++){ 684 | // for(int col=0; col<3; col++){ 685 | // cout< prime(n+1, true); 698 | // // prime[0] = prime[1] = false; 699 | // // 700 | // // for(int i=2; i>target; 740 | 741 | // // if(isPresent(arr1, target, 3, 4)){ 742 | // // cout<<"Element Found"; 743 | // // } 744 | // // else{ 745 | // // cout<<"Element Not Found"; 746 | // // } 747 | 748 | // cout<<"Row Wise Sum = "; 749 | // printSum(arr2, 3, 3); 750 | // cout< spiralOrder(vector>& matrix) { 765 | // // int row = matrix.size(); 766 | // // int col = matrix[0].size(); 767 | // // vector ans; 768 | // // int count = 0; 769 | // // int total = row*col; 770 | // // int startingRow = 0; 771 | // // int startingCol = 0; 772 | // // int endingRow = row-1; 773 | // // int endingCol = col-1; 774 | // // 775 | // // while(count=startingCol; i--){ 790 | // // ans.push_back(matrix[endingRow][i]); 791 | // // count++; 792 | // // } 793 | // // endingRow--; 794 | // // 795 | // // for(int i=endingRow; count=startingRow; i--){ 796 | // // ans.push_back(matrix[i][startingCol]); 797 | // // count++; 798 | // // } 799 | // // startingCol++; 800 | // // } 801 | // // return ans; 802 | // // } 803 | // //}; 804 | // //class Solution { 805 | // //public: 806 | // // vector spiralOrder(vector>& matrix) { 807 | // // int n=matrix.size(),m=matrix[0].size(); 808 | // // int left=0,top=0; 809 | // // int bottom=n-1,right=m-1; 810 | // // vectorans; 811 | // // while(left<=right&&top<=bottom) 812 | // // { 813 | // // for(int i=left;i<=right;i++) 814 | // // { 815 | // // ans.push_back(matrix[top][i]); 816 | // // } 817 | // // top++; 818 | // // for(int i=top;i<=bottom;i++) 819 | // // { 820 | // // ans.push_back(matrix[i][right]); 821 | // // } 822 | // // right--; 823 | // // if(top<=bottom) 824 | // // { 825 | // // for(int i=right;i>=left;i--) 826 | // // { 827 | // // ans.push_back(matrix[bottom][i]); 828 | // // } 829 | // // bottom--; 830 | // // } 831 | // // if(left<=right) 832 | // // { 833 | // // for(int i=bottom;i>=top;i--) 834 | // // { 835 | // // ans.push_back(matrix[i][left]); 836 | // // } 837 | // // left++; 838 | // // } 839 | // // } 840 | // // return ans; 841 | // // } 842 | // //}; 843 | 844 | // //Rotate Image by 90 degree 845 | // //class Solution { 846 | // //public: 847 | // // void rotate(vector>& matrix) { 848 | // // int n = matrix.size(); 849 | // // for(int i=0; i<(n+1)/2;i++){ 850 | // // for(int j=0; j 876 | // //vector wavePrint(vector> arr, int nRows, int mCols) 877 | // //{ 878 | // // vector ans; 879 | // // 880 | // // for(int col=0; col=0; row--){ 883 | // // ans.push_back(arr[row][col]); 884 | // // } 885 | // // } 886 | // // else{ 887 | // // for(int row=0; row findDiagonalOrder(vector>& matrix) { 903 | // // // if(matrix.size()==0 || matrix[0].size()==0){ 904 | // // // return matrix; 905 | // // // } 906 | // // vector ans; 907 | // // int m = matrix.size(); 908 | // // int n = matrix[0].size(); 909 | // // int row=0, col=0; 910 | // // bool up = true; 911 | // // 912 | // // while(row0 && col0 && row 952 | // // 953 | // //int modularExponentiation(int x, int n, int m) { 954 | // // int res = 1; 955 | // // while(n>0){ 956 | // // if(n&1){ 957 | // // res = (1LL*(res) * (x)%m)%m; 958 | // // } 959 | // // x = (1LL * (x)%m * (x)%m) %m; 960 | // // n = n>>1; 961 | // // } 962 | // // return res; 963 | // //} 964 | 965 | 966 | 967 | #include 968 | using namespace std; 969 | 970 | int main() { 971 | int t; 972 | cin >> t; // Read number of test cases 973 | 974 | while (t--) { 975 | int a1, a2, b1, b2; 976 | cin >> a1 >> a2 >> b1 >> b2; 977 | 978 | int suneeWins = 0; 979 | 980 | // Compare the values in each possible way to determine Sunee's wins 981 | if ((a1 > b1 && a2 > b2) || (a1 > b2 && a2 > b1)) { 982 | suneeWins = 1; 983 | } 984 | 985 | cout << suneeWins << endl; 986 | } 987 | 988 | return 0; 989 | } 990 | 991 | 992 | #include 993 | using namespace std; 994 | 995 | int main() { 996 | int t; 997 | cin >> t; // Read number of test cases 998 | 999 | while(t--) { 1000 | int n; 1001 | cin >> n; // Read the two-digit positive integer 1002 | int sum = (n / 10) + (n % 10); // Calculate the sum of the digits 1003 | cout << sum << endl; // Output the result 1004 | } 1005 | 1006 | return 0; 1007 | } 1008 | 1009 | 1010 | #include 1011 | using namespace std; 1012 | 1013 | 1014 | int main() { 1015 | int t; 1016 | cin >> t; // Read number of test cases 1017 | 1018 | while(t--){ 1019 | int a1, a2, b1, b2; 1020 | cin>>a1>>a2>>b1>>b2; 1021 | 1022 | int suneeWins = 0; 1023 | 1024 | if(a1>b1 && a2>b2){ 1025 | suneeWins++; 1026 | } 1027 | if(a1>b2 && a2>b1){ 1028 | suneeWins++; 1029 | } 1030 | if(a2>b1 && a1>b2){ 1031 | suneeWins++; 1032 | } 1033 | 1034 | cout<n){ 5 | // for(int j=0 ->n){ 6 | // for(int k=i -> j){ 7 | // sum = sum + arr[k]; 8 | // } 9 | // } 10 | // } 11 | 12 | // for(int i=0 ->n){ 13 | // sum 14 | // for(int j=0 ->n){ 15 | // for(int k=i -> j){ 16 | // sum = sum + arr[k]; 17 | // } 18 | // } 19 | // } 20 | 21 | 22 | 23 | class Solution { 24 | public: 25 | int maxSubArray(vector& nums) { 26 | int sum = 0; 27 | int maxi = nums[0]; 28 | 29 | 30 | for(int i=0; i 49 | using namespace std; 50 | 51 | int main() { 52 | int n = 0; 53 | int arr[n]; 54 | int max, min; 55 | cin>>n; 56 | for(int i=0; i>arr[i]; 58 | } 59 | if(n==0){ 60 | max = arr[0]; 61 | min = arr[0]; 62 | } 63 | if(n==1){ 64 | if(arr[0]>arr[1]){ 65 | max = arr[0]; 66 | min = arr[1]; 67 | } 68 | else{ 69 | max = arr[1]; 70 | min = arr[0]; 71 | } 72 | } 73 | else{ 74 | if(arr[0]>arr[1]){ 75 | max = arr[0]; 76 | min = arr[1]; 77 | } 78 | else{ 79 | max = arr[1]; 80 | min = arr[0]; 81 | } 82 | for(int i=2; iarr[i]){ 87 | min = arr[i]; 88 | } 89 | } 90 | } 91 | cout<<"Max" << max< 101 | using namespace std; 102 | 103 | int main() { 104 | 105 | int n, i, j, temp; 106 | int arr[n]; 107 | 108 | cin>>n; 109 | 110 | for(int k=0; k> arr[k]; 112 | } 113 | 114 | 115 | for(int i=0, j=n-1; i 133 | // using namespace std; 134 | 135 | // /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 136 | 137 | // void printSum(int arr[][3], int row, int col){ 138 | // for(int row=0; row<3; row++){ 139 | // int sum = 0; 140 | // for(int col=0; col<3; col++){ 141 | // sum += arr[row][col]; 142 | // } 143 | // cout< maxi){ 181 | // maxi = sum; 182 | // rowIndex = row; 183 | // } 184 | // } 185 | // return rowIndex; 186 | // } 187 | 188 | 189 | // int main(int argc, char** argv) { 190 | 191 | // // int arr1[3][4] = {{1,11,111,1111}, {2,22,222,2222}, {3,33,333,3333}}; 192 | // int arr2[3][3]; 193 | 194 | // for(int row=0; row<3; row++){ 195 | // for(int col=0; col<3; col++){ 196 | // cin>>arr2[row][col]; 197 | // } 198 | // } 199 | 200 | // for(int row=0; row<3; row++){ 201 | // for(int col=0; col<3; col++){ 202 | // cout< prime(n+1, true); 215 | // // prime[0] = prime[1] = false; 216 | // // 217 | // // for(int i=2; i>target; 257 | 258 | // // if(isPresent(arr1, target, 3, 4)){ 259 | // // cout<<"Element Found"; 260 | // // } 261 | // // else{ 262 | // // cout<<"Element Not Found"; 263 | // // } 264 | 265 | // cout<<"Row Wise Sum = "; 266 | // printSum(arr2, 3, 3); 267 | // cout< spiralOrder(vector>& matrix) { 282 | // // int row = matrix.size(); 283 | // // int col = matrix[0].size(); 284 | // // vector ans; 285 | // // int count = 0; 286 | // // int total = row*col; 287 | // // int startingRow = 0; 288 | // // int startingCol = 0; 289 | // // int endingRow = row-1; 290 | // // int endingCol = col-1; 291 | // // 292 | // // while(count=startingCol; i--){ 307 | // // ans.push_back(matrix[endingRow][i]); 308 | // // count++; 309 | // // } 310 | // // endingRow--; 311 | // // 312 | // // for(int i=endingRow; count=startingRow; i--){ 313 | // // ans.push_back(matrix[i][startingCol]); 314 | // // count++; 315 | // // } 316 | // // startingCol++; 317 | // // } 318 | // // return ans; 319 | // // } 320 | // //}; 321 | // //class Solution { 322 | // //public: 323 | // // vector spiralOrder(vector>& matrix) { 324 | // // int n=matrix.size(),m=matrix[0].size(); 325 | // // int left=0,top=0; 326 | // // int bottom=n-1,right=m-1; 327 | // // vectorans; 328 | // // while(left<=right&&top<=bottom) 329 | // // { 330 | // // for(int i=left;i<=right;i++) 331 | // // { 332 | // // ans.push_back(matrix[top][i]); 333 | // // } 334 | // // top++; 335 | // // for(int i=top;i<=bottom;i++) 336 | // // { 337 | // // ans.push_back(matrix[i][right]); 338 | // // } 339 | // // right--; 340 | // // if(top<=bottom) 341 | // // { 342 | // // for(int i=right;i>=left;i--) 343 | // // { 344 | // // ans.push_back(matrix[bottom][i]); 345 | // // } 346 | // // bottom--; 347 | // // } 348 | // // if(left<=right) 349 | // // { 350 | // // for(int i=bottom;i>=top;i--) 351 | // // { 352 | // // ans.push_back(matrix[i][left]); 353 | // // } 354 | // // left++; 355 | // // } 356 | // // } 357 | // // return ans; 358 | // // } 359 | // //}; 360 | 361 | // //Rotate Image by 90 degree 362 | // //class Solution { 363 | // //public: 364 | // // void rotate(vector>& matrix) { 365 | // // int n = matrix.size(); 366 | // // for(int i=0; i<(n+1)/2;i++){ 367 | // // for(int j=0; j 393 | // //vector wavePrint(vector> arr, int nRows, int mCols) 394 | // //{ 395 | // // vector ans; 396 | // // 397 | // // for(int col=0; col=0; row--){ 400 | // // ans.push_back(arr[row][col]); 401 | // // } 402 | // // } 403 | // // else{ 404 | // // for(int row=0; row findDiagonalOrder(vector>& matrix) { 420 | // // // if(matrix.size()==0 || matrix[0].size()==0){ 421 | // // // return matrix; 422 | // // // } 423 | // // vector ans; 424 | // // int m = matrix.size(); 425 | // // int n = matrix[0].size(); 426 | // // int row=0, col=0; 427 | // // bool up = true; 428 | // // 429 | // // while(row0 && col0 && row 469 | // // 470 | // //int modularExponentiation(int x, int n, int m) { 471 | // // int res = 1; 472 | // // while(n>0){ 473 | // // if(n&1){ 474 | // // res = (1LL*(res) * (x)%m)%m; 475 | // // } 476 | // // x = (1LL * (x)%m * (x)%m) %m; 477 | // // n = n>>1; 478 | // // } 479 | // // return res; 480 | // //} 481 | -------------------------------------------------------------------------------- /D3-String.cpp: -------------------------------------------------------------------------------- 1 | //Count Senior number of citizen 2 | 3 | int count = 0; 4 | 5 | for(String s: arr){ 6 | int a = (s[11]-'O' * 10)+(s[12]-'O'); 7 | if(a>60){ 8 | count++; 9 | } 10 | return count; 11 | } 12 | 13 | 14 | 15 | 16 | 17 | // 53. Maximum Subarray 18 | // Approach 1: Brute Force 19 | // for(int i=0 ->n){ 20 | // for(int j=0 ->n){ 21 | // for(int k=i -> j){ 22 | // sum = sum + arr[k]; 23 | // } 24 | // } 25 | // } 26 | 27 | // for(int i=0 ->n){ 28 | // sum 29 | // for(int j=0 ->n){ 30 | // for(int k=i -> j){ 31 | // sum = sum + arr[k]; 32 | // } 33 | // } 34 | // } 35 | 36 | 37 | 38 | class Solution { 39 | public: 40 | int maxSubArray(vector& nums) { 41 | int sum = 0; 42 | int maxi = nums[0]; 43 | 44 | 45 | for(int i=0; i 64 | using namespace std; 65 | 66 | int main() { 67 | int n = 0; 68 | int arr[n]; 69 | int max, min; 70 | cin>>n; 71 | for(int i=0; i>arr[i]; 73 | } 74 | if(n==0){ 75 | max = arr[0]; 76 | min = arr[0]; 77 | } 78 | if(n==1){ 79 | if(arr[0]>arr[1]){ 80 | max = arr[0]; 81 | min = arr[1]; 82 | } 83 | else{ 84 | max = arr[1]; 85 | min = arr[0]; 86 | } 87 | } 88 | else{ 89 | if(arr[0]>arr[1]){ 90 | max = arr[0]; 91 | min = arr[1]; 92 | } 93 | else{ 94 | max = arr[1]; 95 | min = arr[0]; 96 | } 97 | for(int i=2; iarr[i]){ 102 | min = arr[i]; 103 | } 104 | } 105 | } 106 | cout<<"Max" << max< 116 | using namespace std; 117 | 118 | int main() { 119 | 120 | int n, i, j, temp; 121 | int arr[n]; 122 | 123 | cin>>n; 124 | 125 | for(int k=0; k> arr[k]; 127 | } 128 | 129 | 130 | for(int i=0, j=n-1; i 148 | // using namespace std; 149 | 150 | // /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 151 | 152 | // void printSum(int arr[][3], int row, int col){ 153 | // for(int row=0; row<3; row++){ 154 | // int sum = 0; 155 | // for(int col=0; col<3; col++){ 156 | // sum += arr[row][col]; 157 | // } 158 | // cout< maxi){ 196 | // maxi = sum; 197 | // rowIndex = row; 198 | // } 199 | // } 200 | // return rowIndex; 201 | // } 202 | 203 | 204 | // int main(int argc, char** argv) { 205 | 206 | // // int arr1[3][4] = {{1,11,111,1111}, {2,22,222,2222}, {3,33,333,3333}}; 207 | // int arr2[3][3]; 208 | 209 | // for(int row=0; row<3; row++){ 210 | // for(int col=0; col<3; col++){ 211 | // cin>>arr2[row][col]; 212 | // } 213 | // } 214 | 215 | // for(int row=0; row<3; row++){ 216 | // for(int col=0; col<3; col++){ 217 | // cout< prime(n+1, true); 230 | // // prime[0] = prime[1] = false; 231 | // // 232 | // // for(int i=2; i>target; 272 | 273 | // // if(isPresent(arr1, target, 3, 4)){ 274 | // // cout<<"Element Found"; 275 | // // } 276 | // // else{ 277 | // // cout<<"Element Not Found"; 278 | // // } 279 | 280 | // cout<<"Row Wise Sum = "; 281 | // printSum(arr2, 3, 3); 282 | // cout< spiralOrder(vector>& matrix) { 297 | // // int row = matrix.size(); 298 | // // int col = matrix[0].size(); 299 | // // vector ans; 300 | // // int count = 0; 301 | // // int total = row*col; 302 | // // int startingRow = 0; 303 | // // int startingCol = 0; 304 | // // int endingRow = row-1; 305 | // // int endingCol = col-1; 306 | // // 307 | // // while(count=startingCol; i--){ 322 | // // ans.push_back(matrix[endingRow][i]); 323 | // // count++; 324 | // // } 325 | // // endingRow--; 326 | // // 327 | // // for(int i=endingRow; count=startingRow; i--){ 328 | // // ans.push_back(matrix[i][startingCol]); 329 | // // count++; 330 | // // } 331 | // // startingCol++; 332 | // // } 333 | // // return ans; 334 | // // } 335 | // //}; 336 | // //class Solution { 337 | // //public: 338 | // // vector spiralOrder(vector>& matrix) { 339 | // // int n=matrix.size(),m=matrix[0].size(); 340 | // // int left=0,top=0; 341 | // // int bottom=n-1,right=m-1; 342 | // // vectorans; 343 | // // while(left<=right&&top<=bottom) 344 | // // { 345 | // // for(int i=left;i<=right;i++) 346 | // // { 347 | // // ans.push_back(matrix[top][i]); 348 | // // } 349 | // // top++; 350 | // // for(int i=top;i<=bottom;i++) 351 | // // { 352 | // // ans.push_back(matrix[i][right]); 353 | // // } 354 | // // right--; 355 | // // if(top<=bottom) 356 | // // { 357 | // // for(int i=right;i>=left;i--) 358 | // // { 359 | // // ans.push_back(matrix[bottom][i]); 360 | // // } 361 | // // bottom--; 362 | // // } 363 | // // if(left<=right) 364 | // // { 365 | // // for(int i=bottom;i>=top;i--) 366 | // // { 367 | // // ans.push_back(matrix[i][left]); 368 | // // } 369 | // // left++; 370 | // // } 371 | // // } 372 | // // return ans; 373 | // // } 374 | // //}; 375 | 376 | // //Rotate Image by 90 degree 377 | // //class Solution { 378 | // //public: 379 | // // void rotate(vector>& matrix) { 380 | // // int n = matrix.size(); 381 | // // for(int i=0; i<(n+1)/2;i++){ 382 | // // for(int j=0; j 408 | // //vector wavePrint(vector> arr, int nRows, int mCols) 409 | // //{ 410 | // // vector ans; 411 | // // 412 | // // for(int col=0; col=0; row--){ 415 | // // ans.push_back(arr[row][col]); 416 | // // } 417 | // // } 418 | // // else{ 419 | // // for(int row=0; row findDiagonalOrder(vector>& matrix) { 435 | // // // if(matrix.size()==0 || matrix[0].size()==0){ 436 | // // // return matrix; 437 | // // // } 438 | // // vector ans; 439 | // // int m = matrix.size(); 440 | // // int n = matrix[0].size(); 441 | // // int row=0, col=0; 442 | // // bool up = true; 443 | // // 444 | // // while(row0 && col0 && row 484 | // // 485 | // //int modularExponentiation(int x, int n, int m) { 486 | // // int res = 1; 487 | // // while(n>0){ 488 | // // if(n&1){ 489 | // // res = (1LL*(res) * (x)%m)%m; 490 | // // } 491 | // // x = (1LL * (x)%m * (x)%m) %m; 492 | // // n = n>>1; 493 | // // } 494 | // // return res; 495 | // //} 496 | -------------------------------------------------------------------------------- /D4-Array.cpp: -------------------------------------------------------------------------------- 1 | //33. Search in Rotated Sorted Array 2 | class Solution { 3 | public: 4 | int search(vector& nums, int target) { 5 | int low=0; 6 | int high = nums.size() -1; 7 | 8 | while( low <= high){ 9 | int mid = (low + high) / 2; 10 | 11 | if(nums[mid] == target) return mid; 12 | 13 | if(nums[low] <= nums[mid]){ 14 | if(nums[low] <= target && target <= nums[mid]){ 15 | high = mid - 1; 16 | }else{ 17 | low = mid + 1; 18 | } 19 | }else{ 20 | if(nums[mid] <= target && target <= nums[high]){ 21 | low = mid + 1; 22 | }else{ 23 | high = mid - 1; 24 | } 25 | } 26 | } 27 | return -1; 28 | } 29 | }; 30 | 31 | 32 | 33 | //1, 3, 4, 7, 9, 9 12, 56 34 | 35 | int min = LLONG_MAX; 36 | 37 | for(int i=0; i i& nums) { 50 | unordered_sets(nums.begin(), nums.end()); 51 | return nums.size() != s.size(); 52 | } 53 | }; 54 | 55 | 56 | class Solution { 57 | public: 58 | bool containsDuplicate(vector& nums) { 59 | sort(nums.begin(), nums.end()); 60 | int n = nums.size(); 61 | for(int i=1; i& nums) { 75 | int n = nums.size(); 76 | int o=0, z=0; 77 | 78 | for(int i=0; i60){ 109 | count++; 110 | } 111 | return count; 112 | } 113 | 114 | 115 | 116 | 117 | 118 | // 53. Maximum Subarray 119 | // Approach 1: Brute Force 120 | // for(int i=0 ->n){ 121 | // for(int j=0 ->n){ 122 | // for(int k=i -> j){ 123 | // sum = sum + arr[k]; 124 | // } 125 | // } 126 | // } 127 | 128 | // for(int i=0 ->n){ 129 | // sum 130 | // for(int j=0 ->n){ 131 | // for(int k=i -> j){ 132 | // sum = sum + arr[k]; 133 | // } 134 | // } 135 | // } 136 | 137 | 138 | 139 | class Solution { 140 | public: 141 | int maxSubArray(vector& nums) { 142 | int sum = 0; 143 | int maxi = nums[0]; 144 | 145 | 146 | for(int i=0; i 165 | using namespace std; 166 | 167 | int main() { 168 | int n = 0; 169 | int arr[n]; 170 | int max, min; 171 | cin>>n; 172 | for(int i=0; i>arr[i]; 174 | } 175 | if(n==0){ 176 | max = arr[0]; 177 | min = arr[0]; 178 | } 179 | if(n==1){ 180 | if(arr[0]>arr[1]){ 181 | max = arr[0]; 182 | min = arr[1]; 183 | } 184 | else{ 185 | max = arr[1]; 186 | min = arr[0]; 187 | } 188 | } 189 | else{ 190 | if(arr[0]>arr[1]){ 191 | max = arr[0]; 192 | min = arr[1]; 193 | } 194 | else{ 195 | max = arr[1]; 196 | min = arr[0]; 197 | } 198 | for(int i=2; iarr[i]){ 203 | min = arr[i]; 204 | } 205 | } 206 | } 207 | cout<<"Max" << max< 217 | using namespace std; 218 | 219 | int main() { 220 | 221 | int n, i, j, temp; 222 | int arr[n]; 223 | 224 | cin>>n; 225 | 226 | for(int k=0; k> arr[k]; 228 | } 229 | 230 | 231 | for(int i=0, j=n-1; i 249 | // using namespace std; 250 | 251 | // /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 252 | 253 | // void printSum(int arr[][3], int row, int col){ 254 | // for(int row=0; row<3; row++){ 255 | // int sum = 0; 256 | // for(int col=0; col<3; col++){ 257 | // sum += arr[row][col]; 258 | // } 259 | // cout< maxi){ 297 | // maxi = sum; 298 | // rowIndex = row; 299 | // } 300 | // } 301 | // return rowIndex; 302 | // } 303 | 304 | 305 | // int main(int argc, char** argv) { 306 | 307 | // // int arr1[3][4] = {{1,11,111,1111}, {2,22,222,2222}, {3,33,333,3333}}; 308 | // int arr2[3][3]; 309 | 310 | // for(int row=0; row<3; row++){ 311 | // for(int col=0; col<3; col++){ 312 | // cin>>arr2[row][col]; 313 | // } 314 | // } 315 | 316 | // for(int row=0; row<3; row++){ 317 | // for(int col=0; col<3; col++){ 318 | // cout< prime(n+1, true); 331 | // // prime[0] = prime[1] = false; 332 | // // 333 | // // for(int i=2; i>target; 373 | 374 | // // if(isPresent(arr1, target, 3, 4)){ 375 | // // cout<<"Element Found"; 376 | // // } 377 | // // else{ 378 | // // cout<<"Element Not Found"; 379 | // // } 380 | 381 | // cout<<"Row Wise Sum = "; 382 | // printSum(arr2, 3, 3); 383 | // cout< spiralOrder(vector>& matrix) { 398 | // // int row = matrix.size(); 399 | // // int col = matrix[0].size(); 400 | // // vector ans; 401 | // // int count = 0; 402 | // // int total = row*col; 403 | // // int startingRow = 0; 404 | // // int startingCol = 0; 405 | // // int endingRow = row-1; 406 | // // int endingCol = col-1; 407 | // // 408 | // // while(count=startingCol; i--){ 423 | // // ans.push_back(matrix[endingRow][i]); 424 | // // count++; 425 | // // } 426 | // // endingRow--; 427 | // // 428 | // // for(int i=endingRow; count=startingRow; i--){ 429 | // // ans.push_back(matrix[i][startingCol]); 430 | // // count++; 431 | // // } 432 | // // startingCol++; 433 | // // } 434 | // // return ans; 435 | // // } 436 | // //}; 437 | // //class Solution { 438 | // //public: 439 | // // vector spiralOrder(vector>& matrix) { 440 | // // int n=matrix.size(),m=matrix[0].size(); 441 | // // int left=0,top=0; 442 | // // int bottom=n-1,right=m-1; 443 | // // vectorans; 444 | // // while(left<=right&&top<=bottom) 445 | // // { 446 | // // for(int i=left;i<=right;i++) 447 | // // { 448 | // // ans.push_back(matrix[top][i]); 449 | // // } 450 | // // top++; 451 | // // for(int i=top;i<=bottom;i++) 452 | // // { 453 | // // ans.push_back(matrix[i][right]); 454 | // // } 455 | // // right--; 456 | // // if(top<=bottom) 457 | // // { 458 | // // for(int i=right;i>=left;i--) 459 | // // { 460 | // // ans.push_back(matrix[bottom][i]); 461 | // // } 462 | // // bottom--; 463 | // // } 464 | // // if(left<=right) 465 | // // { 466 | // // for(int i=bottom;i>=top;i--) 467 | // // { 468 | // // ans.push_back(matrix[i][left]); 469 | // // } 470 | // // left++; 471 | // // } 472 | // // } 473 | // // return ans; 474 | // // } 475 | // //}; 476 | 477 | // //Rotate Image by 90 degree 478 | // //class Solution { 479 | // //public: 480 | // // void rotate(vector>& matrix) { 481 | // // int n = matrix.size(); 482 | // // for(int i=0; i<(n+1)/2;i++){ 483 | // // for(int j=0; j 509 | // //vector wavePrint(vector> arr, int nRows, int mCols) 510 | // //{ 511 | // // vector ans; 512 | // // 513 | // // for(int col=0; col=0; row--){ 516 | // // ans.push_back(arr[row][col]); 517 | // // } 518 | // // } 519 | // // else{ 520 | // // for(int row=0; row findDiagonalOrder(vector>& matrix) { 536 | // // // if(matrix.size()==0 || matrix[0].size()==0){ 537 | // // // return matrix; 538 | // // // } 539 | // // vector ans; 540 | // // int m = matrix.size(); 541 | // // int n = matrix[0].size(); 542 | // // int row=0, col=0; 543 | // // bool up = true; 544 | // // 545 | // // while(row0 && col0 && row 585 | // // 586 | // //int modularExponentiation(int x, int n, int m) { 587 | // // int res = 1; 588 | // // while(n>0){ 589 | // // if(n&1){ 590 | // // res = (1LL*(res) * (x)%m)%m; 591 | // // } 592 | // // x = (1LL * (x)%m * (x)%m) %m; 593 | // // n = n>>1; 594 | // // } 595 | // // return res; 596 | // //} 597 | -------------------------------------------------------------------------------- /D5-Array.cpp: -------------------------------------------------------------------------------- 1 | //1508. Range Sum of Sorted Subarray Sums 2 | //SOLUTION-1 3 | class Solution { 4 | public: 5 | int rangeSum(vector& nums, int n, int left, int right) { 6 | const unsigned int M = 1000000007; 7 | vector sums; 8 | 9 | // Generate all subarray sums 10 | for(int i = 0; i < n; i++) { 11 | int current_sum = 0; 12 | for(int j = i; j < n; j++) { 13 | current_sum = (current_sum + nums[j]) % M; 14 | sums.push_back(current_sum); 15 | } 16 | } 17 | 18 | // Sort the subarray sums 19 | sort(sums.begin(), sums.end()); 20 | 21 | // Calculate the sum of elements from `left-1` to `right-1` 22 | long long result = 0; 23 | for(int i = left - 1; i < right; i++) { 24 | result = (result + sums[i]) % M; 25 | } 26 | 27 | return (int)result; 28 | } 29 | }; 30 | 31 | 32 | //SOLUTION-2 33 | class Solution { 34 | public: 35 | int rangeSum(vector& nums, int n, int left, int right) { 36 | long long sum = 0; 37 | const unsigned int M = 1000000007; 38 | vector csum; 39 | vector sums; 40 | for(int i=0; i& nums, int target) { 64 | int low=0; 65 | int high = nums.size() -1; 66 | 67 | while( low <= high){ 68 | int mid = (low + high) / 2; 69 | 70 | if(nums[mid] == target) return mid; 71 | 72 | if(nums[low] <= nums[mid]){ 73 | if(nums[low] <= target && target <= nums[mid]){ 74 | high = mid - 1; 75 | }else{ 76 | low = mid + 1; 77 | } 78 | }else{ 79 | if(nums[mid] <= target && target <= nums[high]){ 80 | low = mid + 1; 81 | }else{ 82 | high = mid - 1; 83 | } 84 | } 85 | } 86 | return -1; 87 | } 88 | }; 89 | 90 | 91 | 92 | //1, 3, 4, 7, 9, 9 12, 56 93 | 94 | int min = LLONG_MAX; 95 | 96 | for(int i=0; i i& nums) { 109 | unordered_sets(nums.begin(), nums.end()); 110 | return nums.size() != s.size(); 111 | } 112 | }; 113 | 114 | 115 | class Solution { 116 | public: 117 | bool containsDuplicate(vector& nums) { 118 | sort(nums.begin(), nums.end()); 119 | int n = nums.size(); 120 | for(int i=1; i& nums) { 134 | int n = nums.size(); 135 | int o=0, z=0; 136 | 137 | for(int i=0; i60){ 168 | count++; 169 | } 170 | return count; 171 | } 172 | 173 | 174 | 175 | 176 | 177 | // 53. Maximum Subarray 178 | // Approach 1: Brute Force 179 | // for(int i=0 ->n){ 180 | // for(int j=0 ->n){ 181 | // for(int k=i -> j){ 182 | // sum = sum + arr[k]; 183 | // } 184 | // } 185 | // } 186 | 187 | // for(int i=0 ->n){ 188 | // sum 189 | // for(int j=0 ->n){ 190 | // for(int k=i -> j){ 191 | // sum = sum + arr[k]; 192 | // } 193 | // } 194 | // } 195 | 196 | 197 | 198 | class Solution { 199 | public: 200 | int maxSubArray(vector& nums) { 201 | int sum = 0; 202 | int maxi = nums[0]; 203 | 204 | 205 | for(int i=0; i 224 | using namespace std; 225 | 226 | int main() { 227 | int n = 0; 228 | int arr[n]; 229 | int max, min; 230 | cin>>n; 231 | for(int i=0; i>arr[i]; 233 | } 234 | if(n==0){ 235 | max = arr[0]; 236 | min = arr[0]; 237 | } 238 | if(n==1){ 239 | if(arr[0]>arr[1]){ 240 | max = arr[0]; 241 | min = arr[1]; 242 | } 243 | else{ 244 | max = arr[1]; 245 | min = arr[0]; 246 | } 247 | } 248 | else{ 249 | if(arr[0]>arr[1]){ 250 | max = arr[0]; 251 | min = arr[1]; 252 | } 253 | else{ 254 | max = arr[1]; 255 | min = arr[0]; 256 | } 257 | for(int i=2; iarr[i]){ 262 | min = arr[i]; 263 | } 264 | } 265 | } 266 | cout<<"Max" << max< 276 | using namespace std; 277 | 278 | int main() { 279 | 280 | int n, i, j, temp; 281 | int arr[n]; 282 | 283 | cin>>n; 284 | 285 | for(int k=0; k> arr[k]; 287 | } 288 | 289 | 290 | for(int i=0, j=n-1; i 308 | // using namespace std; 309 | 310 | // /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 311 | 312 | // void printSum(int arr[][3], int row, int col){ 313 | // for(int row=0; row<3; row++){ 314 | // int sum = 0; 315 | // for(int col=0; col<3; col++){ 316 | // sum += arr[row][col]; 317 | // } 318 | // cout< maxi){ 356 | // maxi = sum; 357 | // rowIndex = row; 358 | // } 359 | // } 360 | // return rowIndex; 361 | // } 362 | 363 | 364 | // int main(int argc, char** argv) { 365 | 366 | // // int arr1[3][4] = {{1,11,111,1111}, {2,22,222,2222}, {3,33,333,3333}}; 367 | // int arr2[3][3]; 368 | 369 | // for(int row=0; row<3; row++){ 370 | // for(int col=0; col<3; col++){ 371 | // cin>>arr2[row][col]; 372 | // } 373 | // } 374 | 375 | // for(int row=0; row<3; row++){ 376 | // for(int col=0; col<3; col++){ 377 | // cout< prime(n+1, true); 390 | // // prime[0] = prime[1] = false; 391 | // // 392 | // // for(int i=2; i>target; 432 | 433 | // // if(isPresent(arr1, target, 3, 4)){ 434 | // // cout<<"Element Found"; 435 | // // } 436 | // // else{ 437 | // // cout<<"Element Not Found"; 438 | // // } 439 | 440 | // cout<<"Row Wise Sum = "; 441 | // printSum(arr2, 3, 3); 442 | // cout< spiralOrder(vector>& matrix) { 457 | // // int row = matrix.size(); 458 | // // int col = matrix[0].size(); 459 | // // vector ans; 460 | // // int count = 0; 461 | // // int total = row*col; 462 | // // int startingRow = 0; 463 | // // int startingCol = 0; 464 | // // int endingRow = row-1; 465 | // // int endingCol = col-1; 466 | // // 467 | // // while(count=startingCol; i--){ 482 | // // ans.push_back(matrix[endingRow][i]); 483 | // // count++; 484 | // // } 485 | // // endingRow--; 486 | // // 487 | // // for(int i=endingRow; count=startingRow; i--){ 488 | // // ans.push_back(matrix[i][startingCol]); 489 | // // count++; 490 | // // } 491 | // // startingCol++; 492 | // // } 493 | // // return ans; 494 | // // } 495 | // //}; 496 | // //class Solution { 497 | // //public: 498 | // // vector spiralOrder(vector>& matrix) { 499 | // // int n=matrix.size(),m=matrix[0].size(); 500 | // // int left=0,top=0; 501 | // // int bottom=n-1,right=m-1; 502 | // // vectorans; 503 | // // while(left<=right&&top<=bottom) 504 | // // { 505 | // // for(int i=left;i<=right;i++) 506 | // // { 507 | // // ans.push_back(matrix[top][i]); 508 | // // } 509 | // // top++; 510 | // // for(int i=top;i<=bottom;i++) 511 | // // { 512 | // // ans.push_back(matrix[i][right]); 513 | // // } 514 | // // right--; 515 | // // if(top<=bottom) 516 | // // { 517 | // // for(int i=right;i>=left;i--) 518 | // // { 519 | // // ans.push_back(matrix[bottom][i]); 520 | // // } 521 | // // bottom--; 522 | // // } 523 | // // if(left<=right) 524 | // // { 525 | // // for(int i=bottom;i>=top;i--) 526 | // // { 527 | // // ans.push_back(matrix[i][left]); 528 | // // } 529 | // // left++; 530 | // // } 531 | // // } 532 | // // return ans; 533 | // // } 534 | // //}; 535 | 536 | // //Rotate Image by 90 degree 537 | // //class Solution { 538 | // //public: 539 | // // void rotate(vector>& matrix) { 540 | // // int n = matrix.size(); 541 | // // for(int i=0; i<(n+1)/2;i++){ 542 | // // for(int j=0; j 568 | // //vector wavePrint(vector> arr, int nRows, int mCols) 569 | // //{ 570 | // // vector ans; 571 | // // 572 | // // for(int col=0; col=0; row--){ 575 | // // ans.push_back(arr[row][col]); 576 | // // } 577 | // // } 578 | // // else{ 579 | // // for(int row=0; row findDiagonalOrder(vector>& matrix) { 595 | // // // if(matrix.size()==0 || matrix[0].size()==0){ 596 | // // // return matrix; 597 | // // // } 598 | // // vector ans; 599 | // // int m = matrix.size(); 600 | // // int n = matrix[0].size(); 601 | // // int row=0, col=0; 602 | // // bool up = true; 603 | // // 604 | // // while(row0 && col0 && row 644 | // // 645 | // //int modularExponentiation(int x, int n, int m) { 646 | // // int res = 1; 647 | // // while(n>0){ 648 | // // if(n&1){ 649 | // // res = (1LL*(res) * (x)%m)%m; 650 | // // } 651 | // // x = (1LL * (x)%m * (x)%m) %m; 652 | // // n = n>>1; 653 | // // } 654 | // // return res; 655 | // //} 656 | -------------------------------------------------------------------------------- /D6-Array.cpp: -------------------------------------------------------------------------------- 1 | //42. Trapping Rain Water 2 | class Solution { 3 | public: 4 | int trap(vector& height) { 5 | int n = height.size(); 6 | int left[n]; 7 | int right[n]; 8 | left[0] = height[0]; 9 | 10 | for(int i=1; i=0; i--){ 16 | right[i] = max(right[i+1], height[i]); 17 | } 18 | int ssum=0; 19 | for(int i=0; i& height) { 29 | int min = height[0]; 30 | vector left; 31 | for(int i=0; i right; 37 | for(int i=height.size(); i>-0; i--){ 38 | Max = max(Max, height[i]); 39 | right.push_back(Max); 40 | } 41 | vector sumarr; 42 | for(int i=0; i& nums, int k) { 60 | sort(nums.begin(), nums.end()); 61 | int n = nums.size(); 62 | return nums[n-k]; 63 | } 64 | }; 65 | 66 | 67 | class Solution { 68 | public: 69 | int findKthLargest(vector& nums, int k) { 70 | sort(nums.begin(), nums.end()); 71 | int max=0; 72 | int size = nums.size(); 73 | if(size==1){ 74 | return 1; 75 | } 76 | int till = size-k; 77 | for(int i=0; i frequencies(arr, arr + 26); 100 | sort(frequencies.begin(), frequencies.end()); 101 | 102 | int count = 0; 103 | int minKeyPress = 0; 104 | 105 | for(int i = 25; i >= 0; i--) { 106 | if(frequencies[i] == 0) break; 107 | int val = frequencies[i] * (count / 8 + 1); 108 | minKeyPress += val; 109 | count++; 110 | } 111 | 112 | return minKeyPress; 113 | } 114 | }; 115 | 116 | class Solution { 117 | public: 118 | int minimumPushes(string word) { 119 | int arr[26]; 120 | for(char ch: word){ 121 | arr[ch-'a']++; 122 | } 123 | sort(arr[].begin(), arr[].end()); 124 | 125 | int count=0; 126 | int minkeypress=0; 127 | 128 | for(int i=25; i>=0; i--){ 129 | if(arr[i]==0) break; 130 | int val = arr[i]*(count/8 + 1); 131 | minkeypress += val; 132 | count++; 133 | } 134 | return minkeypress; 135 | } 136 | }; 137 | 138 | 139 | #include 140 | #include 141 | 142 | class Solution { 143 | public: 144 | int minimumPushes(std::string word) { 145 | std::unordered_map keyMap = { 146 | {'a', 2}, {'b', 3}, {'c', 4}, {'d', 5}, {'e', 6}, {'f', 2}, {'g', 2}, 147 | {'h', 3}, {'i', 3}, {'j', 4}, {'k', 4}, {'l', 5}, {'m', 5}, {'n', 6}, 148 | {'o', 6}, {'p', 7}, {'q', 7}, {'r', 7}, {'s', 7}, {'t', 8}, {'u', 8}, 149 | {'v', 8}, {'w', 9}, {'x', 9}, {'y', 9}, {'z', 9} 150 | }; 151 | 152 | int totalPushes = 0; 153 | for (char c : word) { 154 | totalPushes += keyMap[c]; 155 | } 156 | 157 | return totalPushes; 158 | } 159 | }; 160 | 161 | 162 | class Solution { 163 | public: 164 | int minimumPushes(string word) { 165 | int two = 0, three = 0, four=0, five=0, six=0, seven=0, eight=0, nine=0; 166 | for( char c: word){ 167 | if(word[c]=='a' || word[c]=='f' || word[c]=='g'){ 168 | two++; 169 | } 170 | if(word[c]=='b' || word[c]=='h' || word[c]=='i'){ 171 | three++; 172 | } 173 | if(word[c]=='c' || word[c]=='j' || word[c]=='k'){ 174 | four++; 175 | } 176 | if(word[c]=='d' || word[c]=='l' || word[c]=='m'){ 177 | five++; 178 | } 179 | if(word[c]=='e' || word[c]=='n' || word[c]=='o'){ 180 | six++; 181 | } 182 | if(word[c]=='p' || word[c]=='q' || word[c]=='r' || word[c]=='s'){ 183 | seven++; 184 | } 185 | if(word[c]=='t' || word[c]=='u' || word[c]=='v'){ 186 | eight++; 187 | } 188 | if(word[c]=='w' || word[c]=='x' || word[c]=='y' || word[c]=='z'){ 189 | nine++; 190 | } 191 | } 192 | int ans = two+three+four+five+six+seven+eight+nine; 193 | return ans; 194 | } 195 | }; 196 | 197 | 198 | //2053. Kth Distinct String in an Array 199 | 200 | class Solution { 201 | public: 202 | string kthDistinct(vector& arr, int k) { 203 | unordered_map mp; 204 | int i; 205 | 206 | for(i=0; i& arr, int k) { 226 | vector& ans; 227 | int j=0, count=0; 228 | for(int i=0; i0){ 238 | ans = arr[i]; 239 | } 240 | } 241 | j=0; 242 | } 243 | // if(k==0){ 244 | // return ans; 245 | // } 246 | return ans; 247 | } 248 | }; 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | //1508. Range Sum of Sorted Subarray Sums 257 | //SOLUTION-1 258 | class Solution { 259 | public: 260 | int rangeSum(vector& nums, int n, int left, int right) { 261 | const unsigned int M = 1000000007; 262 | vector sums; 263 | 264 | // Generate all subarray sums 265 | for(int i = 0; i < n; i++) { 266 | int current_sum = 0; 267 | for(int j = i; j < n; j++) { 268 | current_sum = (current_sum + nums[j]) % M; 269 | sums.push_back(current_sum); 270 | } 271 | } 272 | 273 | // Sort the subarray sums 274 | sort(sums.begin(), sums.end()); 275 | 276 | // Calculate the sum of elements from `left-1` to `right-1` 277 | long long result = 0; 278 | for(int i = left - 1; i < right; i++) { 279 | result = (result + sums[i]) % M; 280 | } 281 | 282 | return (int)result; 283 | } 284 | }; 285 | 286 | 287 | //SOLUTION-2 288 | class Solution { 289 | public: 290 | int rangeSum(vector& nums, int n, int left, int right) { 291 | long long sum = 0; 292 | const unsigned int M = 1000000007; 293 | vector csum; 294 | vector sums; 295 | for(int i=0; i& nums, int target) { 319 | int low=0; 320 | int high = nums.size() -1; 321 | 322 | while( low <= high){ 323 | int mid = (low + high) / 2; 324 | 325 | if(nums[mid] == target) return mid; 326 | 327 | if(nums[low] <= nums[mid]){ 328 | if(nums[low] <= target && target <= nums[mid]){ 329 | high = mid - 1; 330 | }else{ 331 | low = mid + 1; 332 | } 333 | }else{ 334 | if(nums[mid] <= target && target <= nums[high]){ 335 | low = mid + 1; 336 | }else{ 337 | high = mid - 1; 338 | } 339 | } 340 | } 341 | return -1; 342 | } 343 | }; 344 | 345 | 346 | 347 | //1, 3, 4, 7, 9, 9 12, 56 348 | 349 | int min = LLONG_MAX; 350 | 351 | for(int i=0; i i& nums) { 364 | unordered_sets(nums.begin(), nums.end()); 365 | return nums.size() != s.size(); 366 | } 367 | }; 368 | 369 | 370 | class Solution { 371 | public: 372 | bool containsDuplicate(vector& nums) { 373 | sort(nums.begin(), nums.end()); 374 | int n = nums.size(); 375 | for(int i=1; i& nums) { 389 | int n = nums.size(); 390 | int o=0, z=0; 391 | 392 | for(int i=0; i60){ 423 | count++; 424 | } 425 | return count; 426 | } 427 | 428 | 429 | 430 | 431 | 432 | // 53. Maximum Subarray 433 | // Approach 1: Brute Force 434 | // for(int i=0 ->n){ 435 | // for(int j=0 ->n){ 436 | // for(int k=i -> j){ 437 | // sum = sum + arr[k]; 438 | // } 439 | // } 440 | // } 441 | 442 | // for(int i=0 ->n){ 443 | // sum 444 | // for(int j=0 ->n){ 445 | // for(int k=i -> j){ 446 | // sum = sum + arr[k]; 447 | // } 448 | // } 449 | // } 450 | 451 | 452 | 453 | class Solution { 454 | public: 455 | int maxSubArray(vector& nums) { 456 | int sum = 0; 457 | int maxi = nums[0]; 458 | 459 | 460 | for(int i=0; i 479 | using namespace std; 480 | 481 | int main() { 482 | int n = 0; 483 | int arr[n]; 484 | int max, min; 485 | cin>>n; 486 | for(int i=0; i>arr[i]; 488 | } 489 | if(n==0){ 490 | max = arr[0]; 491 | min = arr[0]; 492 | } 493 | if(n==1){ 494 | if(arr[0]>arr[1]){ 495 | max = arr[0]; 496 | min = arr[1]; 497 | } 498 | else{ 499 | max = arr[1]; 500 | min = arr[0]; 501 | } 502 | } 503 | else{ 504 | if(arr[0]>arr[1]){ 505 | max = arr[0]; 506 | min = arr[1]; 507 | } 508 | else{ 509 | max = arr[1]; 510 | min = arr[0]; 511 | } 512 | for(int i=2; iarr[i]){ 517 | min = arr[i]; 518 | } 519 | } 520 | } 521 | cout<<"Max" << max< 531 | using namespace std; 532 | 533 | int main() { 534 | 535 | int n, i, j, temp; 536 | int arr[n]; 537 | 538 | cin>>n; 539 | 540 | for(int k=0; k> arr[k]; 542 | } 543 | 544 | 545 | for(int i=0, j=n-1; i 563 | // using namespace std; 564 | 565 | // /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 566 | 567 | // void printSum(int arr[][3], int row, int col){ 568 | // for(int row=0; row<3; row++){ 569 | // int sum = 0; 570 | // for(int col=0; col<3; col++){ 571 | // sum += arr[row][col]; 572 | // } 573 | // cout< maxi){ 611 | // maxi = sum; 612 | // rowIndex = row; 613 | // } 614 | // } 615 | // return rowIndex; 616 | // } 617 | 618 | 619 | // int main(int argc, char** argv) { 620 | 621 | // // int arr1[3][4] = {{1,11,111,1111}, {2,22,222,2222}, {3,33,333,3333}}; 622 | // int arr2[3][3]; 623 | 624 | // for(int row=0; row<3; row++){ 625 | // for(int col=0; col<3; col++){ 626 | // cin>>arr2[row][col]; 627 | // } 628 | // } 629 | 630 | // for(int row=0; row<3; row++){ 631 | // for(int col=0; col<3; col++){ 632 | // cout< prime(n+1, true); 645 | // // prime[0] = prime[1] = false; 646 | // // 647 | // // for(int i=2; i>target; 687 | 688 | // // if(isPresent(arr1, target, 3, 4)){ 689 | // // cout<<"Element Found"; 690 | // // } 691 | // // else{ 692 | // // cout<<"Element Not Found"; 693 | // // } 694 | 695 | // cout<<"Row Wise Sum = "; 696 | // printSum(arr2, 3, 3); 697 | // cout< spiralOrder(vector>& matrix) { 712 | // // int row = matrix.size(); 713 | // // int col = matrix[0].size(); 714 | // // vector ans; 715 | // // int count = 0; 716 | // // int total = row*col; 717 | // // int startingRow = 0; 718 | // // int startingCol = 0; 719 | // // int endingRow = row-1; 720 | // // int endingCol = col-1; 721 | // // 722 | // // while(count=startingCol; i--){ 737 | // // ans.push_back(matrix[endingRow][i]); 738 | // // count++; 739 | // // } 740 | // // endingRow--; 741 | // // 742 | // // for(int i=endingRow; count=startingRow; i--){ 743 | // // ans.push_back(matrix[i][startingCol]); 744 | // // count++; 745 | // // } 746 | // // startingCol++; 747 | // // } 748 | // // return ans; 749 | // // } 750 | // //}; 751 | // //class Solution { 752 | // //public: 753 | // // vector spiralOrder(vector>& matrix) { 754 | // // int n=matrix.size(),m=matrix[0].size(); 755 | // // int left=0,top=0; 756 | // // int bottom=n-1,right=m-1; 757 | // // vectorans; 758 | // // while(left<=right&&top<=bottom) 759 | // // { 760 | // // for(int i=left;i<=right;i++) 761 | // // { 762 | // // ans.push_back(matrix[top][i]); 763 | // // } 764 | // // top++; 765 | // // for(int i=top;i<=bottom;i++) 766 | // // { 767 | // // ans.push_back(matrix[i][right]); 768 | // // } 769 | // // right--; 770 | // // if(top<=bottom) 771 | // // { 772 | // // for(int i=right;i>=left;i--) 773 | // // { 774 | // // ans.push_back(matrix[bottom][i]); 775 | // // } 776 | // // bottom--; 777 | // // } 778 | // // if(left<=right) 779 | // // { 780 | // // for(int i=bottom;i>=top;i--) 781 | // // { 782 | // // ans.push_back(matrix[i][left]); 783 | // // } 784 | // // left++; 785 | // // } 786 | // // } 787 | // // return ans; 788 | // // } 789 | // //}; 790 | 791 | // //Rotate Image by 90 degree 792 | // //class Solution { 793 | // //public: 794 | // // void rotate(vector>& matrix) { 795 | // // int n = matrix.size(); 796 | // // for(int i=0; i<(n+1)/2;i++){ 797 | // // for(int j=0; j 823 | // //vector wavePrint(vector> arr, int nRows, int mCols) 824 | // //{ 825 | // // vector ans; 826 | // // 827 | // // for(int col=0; col=0; row--){ 830 | // // ans.push_back(arr[row][col]); 831 | // // } 832 | // // } 833 | // // else{ 834 | // // for(int row=0; row findDiagonalOrder(vector>& matrix) { 850 | // // // if(matrix.size()==0 || matrix[0].size()==0){ 851 | // // // return matrix; 852 | // // // } 853 | // // vector ans; 854 | // // int m = matrix.size(); 855 | // // int n = matrix[0].size(); 856 | // // int row=0, col=0; 857 | // // bool up = true; 858 | // // 859 | // // while(row0 && col0 && row 899 | // // 900 | // //int modularExponentiation(int x, int n, int m) { 901 | // // int res = 1; 902 | // // while(n>0){ 903 | // // if(n&1){ 904 | // // res = (1LL*(res) * (x)%m)%m; 905 | // // } 906 | // // x = (1LL * (x)%m * (x)%m) %m; 907 | // // n = n>>1; 908 | // // } 909 | // // return res; 910 | // //} 911 | 912 | 913 | 914 | #include 915 | using namespace std; 916 | 917 | int main() { 918 | int t; 919 | cin >> t; // Read number of test cases 920 | 921 | while (t--) { 922 | int a1, a2, b1, b2; 923 | cin >> a1 >> a2 >> b1 >> b2; 924 | 925 | int suneeWins = 0; 926 | 927 | // Compare the values in each possible way to determine Sunee's wins 928 | if ((a1 > b1 && a2 > b2) || (a1 > b2 && a2 > b1)) { 929 | suneeWins = 1; 930 | } 931 | 932 | cout << suneeWins << endl; 933 | } 934 | 935 | return 0; 936 | } 937 | 938 | 939 | #include 940 | using namespace std; 941 | 942 | int main() { 943 | int t; 944 | cin >> t; // Read number of test cases 945 | 946 | while(t--) { 947 | int n; 948 | cin >> n; // Read the two-digit positive integer 949 | int sum = (n / 10) + (n % 10); // Calculate the sum of the digits 950 | cout << sum << endl; // Output the result 951 | } 952 | 953 | return 0; 954 | } 955 | 956 | 957 | #include 958 | using namespace std; 959 | 960 | 961 | int main() { 962 | int t; 963 | cin >> t; // Read number of test cases 964 | 965 | while(t--){ 966 | int a1, a2, b1, b2; 967 | cin>>a1>>a2>>b1>>b2; 968 | 969 | int suneeWins = 0; 970 | 971 | if(a1>b1 && a2>b2){ 972 | suneeWins++; 973 | } 974 | if(a1>b2 && a2>b1){ 975 | suneeWins++; 976 | } 977 | if(a2>b1 && a1>b2){ 978 | suneeWins++; 979 | } 980 | 981 | cout<