├── Class3 ├── BitwiseOperators.cpp ├── Factorial.cpp ├── FahrenheitToCelsius.cpp ├── UniqueNumber.cpp ├── FibonacciSeries.cpp ├── TernaryOperator.cpp ├── Forloop.cpp ├── MinAndMax.cpp └── SwitchCase.cpp ├── Class2 ├── whileloop.cpp ├── HelloWorld.cpp ├── SumOfDigits.cpp ├── PosNegZero.cpp ├── SumOfNaturalNo.cpp ├── Largestof3Numbers.cpp ├── OddEven.cpp ├── AddNumbers.cpp ├── FizzBuzz.cpp ├── Operators.cpp └── DataTypes.cpp ├── Class4 ├── Continue.cpp ├── StarPattern.cpp ├── UpperLowerCase.cpp ├── NumberPattern.cpp ├── FunctionAdd.cpp ├── Break.cpp ├── PrimeNumber.cpp ├── Functions.cpp ├── PrimeNumUsingFun.cpp └── PatternOddEven.cpp ├── Class17 ├── Power.cpp ├── Factorial.cpp ├── Fibonacci.cpp ├── LinearSearch.cpp ├── IncDecSequence.cpp └── IsArraySorted.cpp ├── Class16 ├── SingleNumber(ii).cpp ├── DecimalToBinary.cpp ├── CountSetBits.cpp ├── BitBasics.cpp └── SingleNumber(iii).cpp ├── Class11 ├── IsPalindrome.cpp ├── ReverseWordsInString.cpp ├── StringCompression.cpp ├── RobotReturnToOrigin.cpp ├── ValidAnagram.cpp └── BasicsString.cpp ├── Class19 ├── TowerOfHanoi.cpp ├── Subsets.cpp ├── Permutations.cpp └── FloodFill.cpp ├── Class13 ├── PassByRefrence.cpp ├── SplitInBalancedStrings.cpp ├── Pointers.cpp ├── LongestPalindromicSubstring.cpp └── MultiplyStrings.cpp ├── Class7 ├── PlusOne.cpp ├── SortArrayByParity.cpp ├── Vector.cpp ├── TrappingRainWater.cpp └── MaxProductOf3Num.cpp ├── Class5 ├── MissingNumber.cpp ├── MoveZeros.cpp ├── ArrayBasics.cpp ├── MaxAndMin.cpp ├── SecondLargest.cpp └── LinearSearch.cpp ├── Class14 ├── SearchA2DMatrix(ii).cpp ├── Sqrt(x).cpp ├── BinarySearch.cpp ├── SearchA2dMatrix.cpp └── FirstAndLastPositionOfElement.cpp ├── Class10 ├── BubbleSort.cpp ├── SelectionSort.cpp ├── SortColors.cpp ├── CountingSort.cpp └── MergeIntervals.cpp ├── Class6 ├── KadanesAlgo.cpp ├── GeneratingSubarrays.cpp ├── PivotIndex.cpp └── CountPairsWithSum.cpp ├── Class8 ├── MergeSortedArray.cpp ├── ProductOfArrayExceptSelf.cpp ├── TripletSumArray.cpp └── NextPermutation.cpp ├── Class9 ├── RowWithMax1s.cpp ├── 2DArrayBasics.cpp ├── WavePrint.cpp ├── RotateImage.cpp └── SpiralMatrix.cpp ├── Class15 ├── FindPeakElement.cpp ├── SearchInRotatedSortedArray.cpp ├── SingleElementInSortedArray.cpp └── MedianOfTwoSortedArrays.cpp ├── Class12 ├── LongestCommonPrefix.cpp ├── AddBinary.cpp ├── ValidPalindrome(ii).cpp └── RomanToInteger.cpp ├── Class18 ├── QuickSort.cpp ├── NumberOfIslands.cpp ├── MergeSort.cpp └── InversionCount.cpp ├── Class20 ├── GenerateParenthesis.cpp ├── LetterCombinationsOfPhoneNumber.cpp ├── CombinationalSum.cpp └── WordSearch.cpp └── Class21 ├── PalindromePartitioning.cpp ├── RatInAMaze.cpp ├── SurroundedRegions.cpp └── SudokuSolver.cpp /Class3/BitwiseOperators.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | cout<<(5&7)< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int count = 1; 7 | 8 | while(count < 6) 9 | { 10 | cout<<"Hello World! "< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int num; 7 | cin>>num; 8 | 9 | int fact = 1; 10 | for(int i=1; i<=num; i++) 11 | { 12 | // fact = fact * i; 13 | fact *= i; 14 | } 15 | 16 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | 7 | int f; 8 | cin>>f; 9 | int c; 10 | 11 | while(f<=200) 12 | { 13 | c = (5*(f-32)) / 9; 14 | cout< 2 | using namespace std; 3 | 4 | // starting point of program 5 | int main(){ 6 | 7 | /* 8 | multi line comment 9 | */ 10 | cout<<"Hello World!"< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | // 4, 5, 1, 5, 3, 1, 4 7 | int n; 8 | cin>>n; 9 | int no; 10 | int ans = 0; 11 | for(int i=0; i>no; 14 | ans ^= no; 15 | } 16 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | for(int i=1; i<=10; i++) 7 | { 8 | if(i%2==0) 9 | { 10 | continue; // start of the loop line no 6 11 | } 12 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int num; 7 | cin>>num; 8 | int sum = 0; 9 | 10 | while(num > 0) 11 | { 12 | int lastDigit = num%10; 13 | sum += lastDigit; 14 | num /= 10; 15 | } 16 | 17 | cout<<"Sum of digits :"< 2 | using namespace std; 3 | 4 | int power(int a, int b) 5 | { 6 | // base case 7 | if(b==0) 8 | { 9 | return 1; 10 | } 11 | else 12 | { 13 | return a*power(a, b-1); 14 | } 15 | } 16 | 17 | int main(){ 18 | int a,b; 19 | cin>>a>>b; 20 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int num; 7 | cin>>num; 8 | 9 | if(num > 0) 10 | { 11 | cout<<"Positive"; 12 | } 13 | else if(num == 0) 14 | { 15 | cout<<"Zero"; 16 | } 17 | else 18 | { 19 | cout<<"Negative"; 20 | } 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Class2/SumOfNaturalNo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int n; //5 7 | cin>>n; 8 | int i=1; 9 | int sum = 0; 10 | while(i<=n) 11 | { 12 | //sum = sum + i; 0 + 1 = 1 + 2 = 3 + 3 = 6 + 4 = 10 + 5 = 15 13 | sum += i; 14 | i++; // 6 15 | } 16 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int a=20, b=20, c=10; 7 | 8 | if(a>=b && a>=c) 9 | { 10 | cout<<"a :"<=a && b>=c) 13 | { 14 | cout<<"b :"< 2 | using namespace std; 3 | 4 | int fact(int n) 5 | { 6 | // base case 7 | if(n==0) 8 | { 9 | return 1; 10 | } 11 | // recursive case 12 | int smallAns = n * fact(n-1); 13 | return smallAns; 14 | } 15 | 16 | int main(){ 17 | int n; 18 | cin>>n; 19 | cout< 2 | using namespace std; 3 | 4 | void patternPrint(int n) 5 | { 6 | for(int row=1; row<=n; row++) 7 | { 8 | for(int col=1; col<=row; col++) 9 | { 10 | cout<<"* "; 11 | } 12 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int num = 11; 7 | // condition 8 | if(num%2==0) 9 | { 10 | cout<<"Entering if block"< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int n; 7 | cin>>n; 8 | int n1 = 0; 9 | int n2 = 1; 10 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | char ch; 7 | cin>>ch; 8 | 9 | if(ch>='A' && ch<='Z') 10 | { 11 | cout<<"UPPERCASE"; 12 | } 13 | else if(ch>='a' && ch<='z') 14 | { 15 | cout<<"Lowecase"; 16 | } 17 | else 18 | { 19 | cout<<"Invalid"; 20 | } 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Class17/Fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fib(int n) 5 | { 6 | // base case 7 | if(n==0 || n==1) 8 | { 9 | return n; 10 | } 11 | // recursive case 12 | int f1 = fib(n-1); 13 | int f2 = fib(n-2); 14 | return f1+f2; 15 | } 16 | 17 | int main(){ 18 | int n; 19 | cin>>n; 20 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | // int num1 = 3; 7 | // int num2 = 5; 8 | // int result = num1+num2; 9 | // cout<>num1>>num2; 13 | 14 | // int result = num1+num2; 15 | // cout<& nums) { 6 | 7 | int ones = 0; 8 | int twos = 0; 9 | 10 | for(int i:nums) 11 | { 12 | ones = (ones^i) & (~twos); 13 | twos = (twos^i) & (~ones); 14 | } 15 | return ones; 16 | } 17 | }; -------------------------------------------------------------------------------- /Class2/FizzBuzz.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int n = 28; 7 | 8 | if(n%3==0 && n%4==0) 9 | { 10 | cout<<"FizzBuzz"; 11 | } 12 | else if(n%3==0) 13 | { 14 | cout<<"Fizz"; 15 | } 16 | else if(n%4==0) 17 | { 18 | cout<<"Buzz"; 19 | } 20 | else 21 | { 22 | cout<<"Bye"; 23 | } 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Class11/IsPalindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | string isPalindrome(string str) 5 | { 6 | int n = str.size(); 7 | for(int i=0; i>str; 20 | cout< 2 | using namespace std; 3 | 4 | int decimalToBinary(int n) 5 | { 6 | int ans = 0; 7 | int p = 1; 8 | while(n>0) 9 | { 10 | int lastBit = (n&1); 11 | ans += p*lastBit; 12 | p = p*10; 13 | n = n>>1; 14 | } 15 | return ans; 16 | } 17 | 18 | int main(){ 19 | int n; 20 | cin>>n; 21 | cout< 2 | using namespace std; 3 | 4 | void numberPat(int n) 5 | { 6 | int value = 1; 7 | for(int row=1; row<=n; row++) 8 | { 9 | for(int col=1; col<=row; col++) 10 | { 11 | cout< 2 | using namespace std; 3 | 4 | // void addTwoNumbers(int num1, int num2) 5 | // { 6 | // cout<>num1>>num2; 17 | 18 | // addTwoNumbers(5, 11); 19 | 20 | cout< 2 | using namespace std; 3 | 4 | void TOH(int n, char src, char helper, char dest) 5 | { 6 | // base case 7 | if(n==0) 8 | { 9 | return; 10 | } 11 | // recursive case 12 | TOH(n-1, src, dest, helper); 13 | cout<<"Move disk "<>n; 20 | TOH(n, 'A', 'B', 'C'); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Class11/ReverseWordsInString.cpp: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/reverse-words-in-a-string-iii/ 2 | 3 | class Solution { 4 | public: 5 | string reverseWords(string s) { 6 | 7 | int i=0; 8 | for(int j=0; j 2 | using namespace std; 3 | 4 | int linearSearch(int arr[], int i, int n, int key) 5 | { 6 | // base case 7 | if(i==n) 8 | { 9 | return -1; 10 | } 11 | if(arr[i]==key) 12 | { 13 | return i; 14 | } 15 | return linearSearch(arr, i+1, n, key); 16 | } 17 | 18 | int main(){ 19 | int arr[] = {2,4,5,1,3,7}; 20 | int n = 6; 21 | int key = 13; 22 | cout< 2 | using namespace std; 3 | 4 | void dec(int n) 5 | { 6 | // base case 7 | if(n==0) 8 | { 9 | return; 10 | } 11 | cout<>n; 29 | // dec(n); 30 | inc(n); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Class11/StringCompression.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void compressString(string str) 5 | { 6 | int n = str.size(); 7 | for(int i=0; i>str; 22 | compressString(str); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Class3/TernaryOperator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | // int num = 8; 7 | // bool checkEven = num%2==0 ? true : false; 8 | // cout<>marks; 12 | // string result = (marks >= 50) ? "pass" : "fail"; 13 | // cout<>num; 17 | 18 | string res; 19 | 20 | res = (num==0) ? "zero" : ((num>0) ? "positive" : "negative"); 21 | 22 | cout< 2 | using namespace std; 3 | 4 | // void multiply(int x) 5 | // { 6 | // x *=2; 7 | // cout<<"Inside function : "< plusOne(vector& digits) { 6 | 7 | int n = digits.size(); 8 | for(int i=n-1; i>=0; i--) 9 | { 10 | if(digits[i] < 9) 11 | { 12 | digits[i] += 1; 13 | return digits; 14 | } 15 | digits[i] = 0; 16 | } 17 | digits[0] = 1; 18 | digits.push_back(0); 19 | 20 | return digits; 21 | } 22 | }; -------------------------------------------------------------------------------- /Class4/Break.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | // for(int i=1; i<=10; i++) 7 | // { 8 | // if(i==7) 9 | // { 10 | // break; // it will go to line no 13 11 | // } 12 | // cout<>no; 19 | if(no%5==0) 20 | { 21 | break; 22 | } 23 | cout<<"Not multiple of 5"< 2 | using namespace std; 3 | 4 | bool isSorted(int arr[], int n) 5 | { 6 | // base case 7 | if(n==0 || n==1) 8 | { 9 | return true; 10 | } 11 | if(arr[0] <= arr[1] && isSorted(arr+1, n-1)) 12 | { 13 | return true; 14 | } 15 | return false; 16 | } 17 | 18 | int main(){ 19 | int arr[] = {12,4,6,7,9}; 20 | int n = 5; 21 | if(isSorted(arr, n)){ 22 | cout<<"Sorted Array"; 23 | } 24 | else{ 25 | cout<<"Not sorted"; 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Class7/SortArrayByParity.cpp: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/sort-array-by-parity/ 2 | 3 | class Solution { 4 | public: 5 | vector sortArrayByParity(vector& A) { 6 | 7 | int n = A.size(); 8 | int i=0; 9 | int j=n-1; 10 | 11 | while(i 2 | #include 3 | // #include 4 | using namespace std; 5 | 6 | int main(){ 7 | 8 | int num; 9 | cin>>num; 10 | bool isPrime = true; 11 | 12 | for(int i=2; i<=sqrt(num); i++) 13 | { 14 | if(num%i==0) 15 | { 16 | isPrime = false; 17 | break; 18 | } 19 | } 20 | 21 | if(isPrime==true) 22 | { 23 | cout<<"Num is prime"; 24 | } 25 | else 26 | { 27 | cout<<"Num is not prime"; 28 | } 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Class5/MissingNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // #include 4 | 5 | int missingNumber(int arr[], int n) 6 | { 7 | int totalSum = n*(n+1)/2; 8 | int arraySum = 0; 9 | 10 | for(int i=0; i>n; 22 | int arr[n]; 23 | for(int i=0; i>arr[i]; 26 | } 27 | 28 | cout<>& matrix, int target) { 6 | 7 | int row = matrix.size(); 8 | int col = matrix[0].size(); 9 | 10 | int i=0; 11 | int j=col-1; 12 | 13 | while(i=0) 14 | { 15 | if(matrix[i][j]==target) 16 | { 17 | return true; 18 | } 19 | target > matrix[i][j] ? i++ : j--; 20 | } 21 | return false; 22 | } 23 | }; -------------------------------------------------------------------------------- /Class4/Functions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // void printHello() 5 | // { 6 | // cout<<"Hello"< 2 | using namespace std; 3 | 4 | void bubbleSort(int arr[], int n) 5 | { 6 | for(int i=0; i arr[j+1]) 11 | { 12 | swap(arr[j], arr[j+1]); 13 | } 14 | } 15 | } 16 | } 17 | 18 | int main(){ 19 | int n; 20 | cin>>n; 21 | int arr[n]; 22 | for(int i=0; i>arr[i]; 25 | } 26 | bubbleSort(arr, n); 27 | for(int i=0; i 2 | using namespace std; 3 | 4 | int countSetBits(int n) 5 | { 6 | int ans = 0; 7 | while(n>0) 8 | { 9 | ans += (n&1); 10 | n = n>>1; 11 | } 12 | return ans; 13 | } 14 | 15 | int countSetBitsOptimised(int n) 16 | { 17 | int ans = 0; 18 | while(n>0) 19 | { 20 | n = n & (n-1); 21 | ans++; 22 | } 23 | return ans; 24 | } 25 | 26 | int main(){ 27 | 28 | int n; 29 | cin>>n; 30 | // cout< 2 | using namespace std; 3 | 4 | void moveZeros(int arr[], int n) 5 | { 6 | int j=0; // index of non-zero element 7 | 8 | for(int i=0; i>n; 22 | int arr[n]; 23 | for(int i=0; i>arr[i]; 26 | } 27 | 28 | moveZeros(arr, n); 29 | 30 | for(int i=0; i 2 | using namespace std; 3 | 4 | int maximumSumSubarray(int arr[], int n) 5 | { 6 | int currSum = 0; 7 | int maxSum = INT_MIN; 8 | 9 | for(int i=0; i>n; 25 | int arr[n]; 26 | for(int i=0; i>arr[i]; 29 | } 30 | 31 | cout< 2 | #include 3 | using namespace std; 4 | 5 | bool checkPrime(int num) 6 | { 7 | for(int i=2; i<=sqrt(num); i++) 8 | { 9 | if(num%i==0) 10 | { 11 | return false; // not prime 12 | } 13 | } 14 | return true; // prime 15 | } 16 | 17 | int main(){ 18 | 19 | if(checkPrime(19)){ 20 | cout<<"Prime Number"< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | // int n = 7; 7 | 8 | // for(int i=0; i=1; i--) 19 | // { 20 | // cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int n; 7 | cin>>n; 8 | int no; 9 | int minSoFar = INT_MAX; // biggest int val 10 | int maxSoFar = INT_MIN; // smallest int val 11 | //4 6 -3 10 2 -6 1 12 | for(int i=0; i>no; 15 | if(no < minSoFar){ 16 | minSoFar = no; // 4, -3, -6 17 | } 18 | if(no > maxSoFar){ 19 | maxSoFar = no; // 4, 6, 10 20 | } 21 | } 22 | 23 | cout<<"Minimum Value is : "< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int n; 7 | cin>>n; 8 | vector v; 9 | vector a(n); 10 | vector b(5, 10); 11 | vector c{1,3,4,2,6}; 12 | 13 | // iterate over vector 14 | for(int i=0; i>no; 25 | v.push_back(no); 26 | } 27 | 28 | // for each loop 29 | for(int i:v) 30 | { 31 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int arr[5] = {3, 1, 2, 6, 8}; 7 | 8 | cout<>n; 17 | int arr[n]; 18 | for(int i=0; i>arr[i]; 21 | } 22 | 23 | for(int i=0; i 2 | using namespace std; 3 | 4 | void generateSubarrays(int arr[], int n) 5 | { 6 | // starting point 7 | for(int i=0; i>n; 26 | int arr[n]; 27 | for(int i=0; i>arr[i]; 30 | } 31 | 32 | generateSubarrays(arr, n); 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Class8/MergeSortedArray.cpp: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/merge-sorted-array/ 2 | 3 | class Solution { 4 | public: 5 | void merge(vector& nums1, int m, vector& nums2, int n) { 6 | 7 | int i = m-1; 8 | int j = n-1; 9 | int k = m+n-1; 10 | 11 | while(i>=0 && j>=0) 12 | { 13 | if(nums1[i] > nums2[j]) 14 | { 15 | nums1[k--] = nums1[i--]; 16 | } 17 | else 18 | { 19 | nums1[k--] = nums2[j--]; 20 | } 21 | } 22 | 23 | while(j>=0) 24 | { 25 | nums1[k--] = nums2[j--]; 26 | } 27 | 28 | } 29 | }; -------------------------------------------------------------------------------- /Class10/SelectionSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void selectionSort(int arr[], int n) 5 | { 6 | for(int i=0; i>n; 23 | int arr[n]; 24 | for(int i=0; i>arr[i]; 27 | } 28 | selectionSort(arr, n); 29 | for(int i=0; i productExceptSelf(vector& nums) { 6 | 7 | int n = nums.size(); 8 | vector res; 9 | 10 | int product = 1; 11 | for(int i=0; i=1; i--) 19 | { 20 | res[i] = res[i-1]*product; 21 | product *= nums[i]; 22 | } 23 | res[0] = product; 24 | 25 | return res; 26 | } 27 | }; -------------------------------------------------------------------------------- /Class13/SplitInBalancedStrings.cpp: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/split-a-string-in-balanced-strings/ 2 | 3 | class Solution { 4 | public: 5 | int balancedStringSplit(string s) { 6 | 7 | int n = s.size(); 8 | int l = 0; 9 | int r = 0; 10 | int count = 0; 11 | 12 | for(int i=0; i 2 | #include 3 | using namespace std; 4 | 5 | int rowWithMax1s(vector> matrix, int n, int m) 6 | { 7 | int row=-1; 8 | int j=m-1; 9 | for(int i=0; i>n>>m; 23 | vector> matrix(n, vector(m)); 24 | for(int i=0; i>matrix[i][j]; 29 | } 30 | } 31 | 32 | cout<& nums) { 6 | 7 | int n = nums.size(); 8 | int s = 0; 9 | int i = 0; 10 | int e = n-1; 11 | 12 | while(i<=e) 13 | { 14 | if(nums[i]==0) 15 | { 16 | swap(nums[i], nums[s]); 17 | s++; 18 | i++; 19 | } 20 | else if(nums[i]==1) 21 | { 22 | i++; 23 | } 24 | else 25 | { 26 | swap(nums[e], nums[i]); 27 | e--; 28 | } 29 | } 30 | 31 | } 32 | }; -------------------------------------------------------------------------------- /Class5/MaxAndMin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int n; 7 | cin>>n; 8 | int arr[n]; 9 | for(int i=0; i>arr[i]; 12 | } 13 | 14 | int minVal = INT_MAX; 15 | int maxVal = INT_MIN; 16 | 17 | for(int i=0; i maxVal) 20 | { 21 | maxVal = arr[i]; 22 | } 23 | // maxVal = max(arr[i], maxVal); 24 | if(arr[i] < minVal) 25 | { 26 | minVal = arr[i]; 27 | } 28 | // minVal = min(arr[i], minVal); 29 | } 30 | 31 | cout<<"Minimum value is "< 2 | using namespace std; 3 | 4 | int secondLargest(int arr[], int n) 5 | { 6 | int firstMax = INT_MIN; 7 | int secondMax = INT_MIN; 8 | 9 | for(int i=0; i firstMax) 12 | { 13 | secondMax = firstMax; 14 | firstMax = arr[i]; 15 | } 16 | else if(arr[i] > secondMax) 17 | { 18 | secondMax = arr[i]; 19 | } 20 | } 21 | 22 | return secondMax; 23 | } 24 | 25 | int main(){ 26 | 27 | int n; 28 | cin>>n; 29 | int arr[n]; 30 | for(int i=0; i>arr[i]; 33 | } 34 | 35 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int a = 4; 7 | // a = a + 5; 8 | a += 5; 9 | a -= 5; 10 | 11 | a++; // increment 1 12 | a--; // decrement 1 13 | 14 | int x = 3; 15 | int y = 3; 16 | cout<<(x>y)< 5 && q < 10)< 5 || q < 10)< 2 | using namespace std; 3 | 4 | int pivotIndex(int arr[], int n) 5 | { 6 | int totalSum = 0; 7 | for(int i=0; i>n; 31 | int arr[n]; 32 | for(int i=0; i>arr[i]; 35 | } 36 | 37 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | // int x = 5; 7 | // cout<<&x< 2 | using namespace std; 3 | 4 | int main(){ 5 | // int matrix[5][3] = {0}; 6 | // for(int i=0; i<5; i++) 7 | // { 8 | // for(int j=0; j<3; j++) 9 | // { 10 | // cout<>n>>m; 17 | int matrix[n][m]; 18 | for(int i=0; i>matrix[i][j]; 23 | } 24 | } 25 | 26 | for(int i=0; i 2 | using namespace std; 3 | 4 | bool isOdd(int n) 5 | { 6 | return n&1; 7 | } 8 | 9 | int getBit(int n, int i) 10 | { 11 | int mask = 1< 0 ? 1 : 0; 13 | return bit; 14 | } 15 | 16 | int setBit(int n, int i) 17 | { 18 | int mask = 1<>n; 34 | // cout<>i; 37 | // cout<& nums) { 6 | 7 | int n = nums.size(); 8 | int s = 0; 9 | int e = n-1; 10 | 11 | while(s<=e) 12 | { 13 | int mid = (s+e)/2; 14 | if((mid==0 || nums[mid] > nums[mid-1]) && (mid==n-1 || nums[mid] > nums[mid+1])) 15 | { 16 | return mid; 17 | } 18 | else if(mid < n-1 && nums[mid] < nums[mid+1]) 19 | { 20 | s = mid+1; 21 | } 22 | else 23 | { 24 | e = mid-1; 25 | } 26 | } 27 | return -1; 28 | } 29 | }; -------------------------------------------------------------------------------- /Class12/LongestCommonPrefix.cpp: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/longest-common-prefix/ 2 | 3 | class Solution { 4 | public: 5 | string longestCommonPrefix(vector& strs) { 6 | 7 | string ans=""; 8 | int n = strs.size(); 9 | 10 | sort(strs.begin(), strs.end()); 11 | string first = strs[0]; 12 | string last = strs[n-1]; 13 | int length = min(first.size(), last.size()); 14 | 15 | for(int i=0; i 2 | using namespace std; 3 | 4 | void patternOddEven(int n) 5 | { 6 | int row = 1; 7 | while(row<=n) 8 | { 9 | int col = 1; 10 | // odd row 11 | if(row%2!=0) 12 | { 13 | while(col<=row) 14 | { 15 | cout<<1<<" "; 16 | col++; 17 | } 18 | } 19 | // even row 20 | else 21 | { 22 | cout<<1<<" "; 23 | while(col<=row-2) 24 | { 25 | cout<<0<<" "; 26 | col++; 27 | } 28 | cout<<1<<" "; 29 | } 30 | cout<& nums, vector>& res, vector& path, int i) 6 | { 7 | if(i==nums.size()) 8 | { 9 | res.push_back(path); 10 | return; 11 | } 12 | 13 | // include 14 | path.push_back(nums[i]); 15 | generateSubsets(nums, res, path, i+1); 16 | path.pop_back(); 17 | 18 | // exclude 19 | generateSubsets(nums, res, path, i+1); 20 | } 21 | 22 | public: 23 | vector> subsets(vector& nums) { 24 | 25 | vector> res; 26 | vector path; 27 | 28 | generateSubsets(nums, res, path, 0); 29 | return res; 30 | } 31 | }; -------------------------------------------------------------------------------- /Class19/Permutations.cpp: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/permutations/ 2 | 3 | class Solution { 4 | 5 | void generatePermutations(vector& nums, int i, vector>& res) 6 | { 7 | if(i==nums.size()) 8 | { 9 | res.push_back(nums); 10 | return; 11 | } 12 | 13 | for(int j=i; j> permute(vector& nums) { 25 | 26 | vector> res; 27 | generatePermutations(nums, 0, res); 28 | return res; 29 | } 30 | }; -------------------------------------------------------------------------------- /Class9/WavePrint.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int n,m; 7 | cin>>n>>m; 8 | int matrix[n][m]; 9 | for(int i=0; i>matrix[i][j]; 14 | } 15 | } 16 | 17 | for(int col=0; col=0; row--) 31 | { 32 | cout< map(26, 0); 17 | 18 | for(char i:s) 19 | { 20 | map[i-'a']++; 21 | } 22 | 23 | for(char i:t) 24 | { 25 | map[i-'a']--; 26 | } 27 | 28 | for(int i=0; i<26; i++) 29 | { 30 | if(map[i]!=0) 31 | { 32 | return false; 33 | } 34 | } 35 | return true; 36 | } 37 | }; -------------------------------------------------------------------------------- /Class12/AddBinary.cpp: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/add-binary/ 2 | 3 | class Solution { 4 | public: 5 | string addBinary(string a, string b) { 6 | 7 | int i=0; 8 | int lenA = a.size(); 9 | int lenB = b.size(); 10 | int carry = 0; 11 | string res; 12 | 13 | while(i>& matrix) { 6 | 7 | int n = matrix.size(); 8 | 9 | // transpose 10 | for(int i=0; i 2 | using namespace std; 3 | 4 | int binarySearch(vector arr, int target) 5 | { 6 | int n = arr.size()-1; 7 | int s=0; 8 | int e=n-1; 9 | while(s<=e) 10 | { 11 | int mid = (s+e)/2; 12 | if(arr[mid]==target) 13 | { 14 | return mid; 15 | } 16 | else if(arr[mid] < target) 17 | { 18 | s = mid+1; 19 | } 20 | else 21 | { 22 | e = mid-1; 23 | } 24 | } 25 | return -1; 26 | } 27 | 28 | int main(){ 29 | int n; 30 | cin>>n; 31 | int target; 32 | cin>>target; 33 | vector arr; 34 | for(int i=0; i>no; 38 | arr.push_back(no); 39 | } 40 | cout<& height) { 6 | 7 | int n = height.size(); 8 | vector leftMax(n); 9 | vector rightMax(n); 10 | 11 | leftMax[0] = height[0]; 12 | for(int i=1; i=0; i--) 19 | { 20 | rightMax[i] = max(rightMax[i+1], height[i]); 21 | } 22 | 23 | int water = 0; 24 | for(int i=1; i>& matrix, int target) { 6 | 7 | int n = matrix.size(); 8 | int m = matrix[0].size(); 9 | 10 | int s = 0; 11 | int e = n*m-1; 12 | 13 | while(s<=e) 14 | { 15 | int mid = (s+e)/2; 16 | int row = mid/m; 17 | int col = mid%m; 18 | 19 | if(matrix[row][col]==target) 20 | { 21 | return true; 22 | } 23 | else if(matrix[row][col] < target) 24 | { 25 | s = mid+1; 26 | } 27 | else 28 | { 29 | e = mid-1; 30 | } 31 | } 32 | return false; 33 | } 34 | }; -------------------------------------------------------------------------------- /Class5/LinearSearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // int linearSearch(int arr[], int n, int key) 5 | // { 6 | // // 6 7 | // // 4, 2, 16, 8 8 | // for(int i=0; i>n; 36 | int arr[n]; 37 | for(int i=0; i>arr[i]; 40 | } 41 | int key; 42 | cin>>key; 43 | linearSearch(arr, n, key); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Class16/SingleNumber(iii).cpp: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/single-number-iii/ 2 | 3 | class Solution { 4 | public: 5 | vector singleNumber(vector& nums) { 6 | 7 | int n = nums.size(); 8 | int res = 0; 9 | for(int i=0; i>1; 20 | } 21 | 22 | int mask = 1< 0) 29 | { 30 | num1 ^= nums[i]; 31 | } 32 | } 33 | 34 | num2 = res^num1; 35 | return {num1, num2}; 36 | } 37 | }; -------------------------------------------------------------------------------- /Class12/ValidPalindrome(ii).cpp: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/valid-palindrome-ii/ 2 | 3 | class Solution { 4 | 5 | // check whether substring is palindrome or not 6 | bool deleteString(string s, int i, int j) 7 | { 8 | while(i 2 | using namespace std; 3 | 4 | void countingSort(int arr[], int n) 5 | { 6 | int maxVal = -1; 7 | for(int i=0; i0) 24 | { 25 | arr[j]=i; 26 | j++; 27 | freq[i]--; 28 | } 29 | } 30 | } 31 | 32 | int main(){ 33 | int n; 34 | cin>>n; 35 | int arr[n]; 36 | for(int i=0; i>arr[i]; 39 | } 40 | countingSort(arr, n); 41 | for(int i=0; i 2 | using namespace std; 3 | 4 | int partition(int arr[], int s, int e) 5 | { 6 | int j = s; 7 | int i = s-1; 8 | int pivot = arr[e]; 9 | for(j=s; j=e) 24 | { 25 | return; 26 | } 27 | int p = partition(arr, s, e); 28 | quickSort(arr, s, p-1); 29 | quickSort(arr, p+1, e); 30 | } 31 | 32 | int main(){ 33 | int arr[100]; 34 | int n; 35 | cin>>n; 36 | for(int i=0; i>arr[i]; 39 | } 40 | quickSort(arr, 0, n-1); 41 | for(int i=0; i 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | //data types 7 | int num = 5; 8 | char val = 'A'; 9 | string a = "Hello"; 10 | float b = 3.14; 11 | double c = 4.5612398; 12 | bool myBoolean = false; 13 | 14 | cout<<"Integer : "<&res, string& curr, int n, int i, int open, int close) 4 | { 5 | if(i==n*2) 6 | { 7 | res.push_back(curr); 8 | return; 9 | } 10 | 11 | if(open generateParenthesis(int n) { 29 | 30 | vector res; 31 | string curr=""; 32 | 33 | parenthesis(res, curr, n, 0, 0, 0); 34 | return res; 35 | } 36 | }; -------------------------------------------------------------------------------- /Class10/MergeIntervals.cpp: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/merge-intervals/ 2 | 3 | class Solution { 4 | public: 5 | vector> merge(vector>& intervals) { 6 | 7 | int n = intervals.size(); 8 | vector> result; 9 | 10 | sort(intervals.begin(), intervals.end()); 11 | 12 | int s1 = intervals[0][0]; 13 | int e1 = intervals[0][1]; 14 | 15 | for(int i=1; i 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | // char ch; 7 | // cin>>ch; 8 | 9 | // switch(ch){ 10 | // case 'B': 11 | // cout<<"Burger"<>day; 28 | 29 | switch(day) 30 | { 31 | case 6: 32 | cout<<"Saturday"<& res, string mappings[], string curr, int index) 4 | { 5 | if(index==digits.size()) 6 | { 7 | res.push_back(curr); 8 | return; 9 | } 10 | 11 | string val = mappings[digits[index]-'0']; //a b c 12 | 13 | for(int i=0; i letterCombinations(string digits) { 22 | 23 | vector res; 24 | if(digits.size()==0) 25 | { 26 | return res; 27 | } 28 | 29 | string mappings[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 30 | 31 | phone(digits, res, mappings, "", 0); 32 | return res; 33 | } 34 | }; -------------------------------------------------------------------------------- /Class13/LongestPalindromicSubstring.cpp: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/longest-palindromic-substring/ 2 | 3 | class Solution { 4 | 5 | void longestSubstring(int l, int r, string &s, int &maxLen, int &start) 6 | { 7 | int n = s.size(); 8 | while(l>=0 && r maxLen) 15 | { 16 | start = l+1; 17 | maxLen = currLen; 18 | } 19 | } 20 | 21 | public: 22 | string longestPalindrome(string s) { 23 | 24 | int n = s.size(); 25 | int maxLen = 1; 26 | int start = 0; 27 | 28 | for(int i=0; i 2 | #include 3 | using namespace std; 4 | 5 | int triplet(int arr[], int n, int sum) 6 | { 7 | sort(arr, arr+n); 8 | for(int k=0; k>n; 35 | int arr[n]; 36 | for(int i=0; i>arr[i]; 39 | } 40 | int sum; 41 | cin>>sum; 42 | 43 | if(triplet(arr, n, sum)) 44 | { 45 | cout<<"True"; 46 | } 47 | else 48 | { 49 | cout<<"False"; 50 | } 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /Class13/MultiplyStrings.cpp: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/multiply-strings/ 2 | 3 | class Solution { 4 | public: 5 | string multiply(string num1, string num2) { 6 | 7 | if(num1=="0" || num2=="0") 8 | { 9 | return "0"; 10 | } 11 | 12 | int n = num1.size(); 13 | int m = num2.size(); 14 | vector res(n+m, 0); 15 | 16 | for(int i=n-1; i>=0; i--) 17 | { 18 | for(int j=m-1; j>=0; j--) 19 | { 20 | res[i+j+1] += (num1[i]-'0') * (num2[j]-'0'); 21 | res[i+j] += res[i+j+1]/10; //adding carry 22 | res[i+j+1] %=10; 23 | } 24 | } 25 | 26 | int skip=0; 27 | while(skip>& image, int sr, int sc, int newColor, int original) 6 | { 7 | int n = image.size(); 8 | int m = image[0].size(); 9 | 10 | if(sr<0 || sr>=n || sc<0 || sc>=m || image[sr][sc]!=original) 11 | { 12 | return; 13 | } 14 | 15 | image[sr][sc] = newColor; 16 | 17 | dfs(image, sr-1, sc, newColor, original); 18 | dfs(image, sr+1, sc, newColor, original); 19 | dfs(image, sr, sc-1, newColor, original); 20 | dfs(image, sr, sc+1, newColor, original); 21 | } 22 | 23 | public: 24 | vector> floodFill(vector>& image, int sr, int sc, int newColor) 25 | { 26 | if(newColor == image[sr][sc]) 27 | { 28 | return image; 29 | } 30 | int original = image[sr][sc]; 31 | 32 | dfs(image, sr, sc, newColor, original); 33 | return image; 34 | } 35 | }; -------------------------------------------------------------------------------- /Class18/NumberOfIslands.cpp: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/number-of-islands/ 2 | 3 | class Solution { 4 | 5 | void dfs(vector>& grid, int i, int j) 6 | { 7 | int n = grid.size(); 8 | int m = grid[0].size(); 9 | 10 | if(i>=n || i<0 || j>=m || j<0 || grid[i][j]!='1') 11 | { 12 | return; 13 | } 14 | 15 | grid[i][j] = '2'; 16 | 17 | dfs(grid, i-1, j); 18 | dfs(grid, i+1, j); 19 | dfs(grid, i, j-1); 20 | dfs(grid, i, j+1); 21 | } 22 | 23 | public: 24 | int numIslands(vector>& grid) { 25 | 26 | int n = grid.size(); 27 | int m = grid[0].size(); 28 | int count = 0; 29 | 30 | for(int i=0; i& nums) { 6 | 7 | int n = nums.size(); 8 | if(n==1) 9 | { 10 | return; 11 | } 12 | 13 | //1. find decreasing point 14 | int replaceIdx = -1; 15 | for(int i=n-1; i>=1; i--) 16 | { 17 | if(nums[i-1] < nums[i]) 18 | { 19 | replaceIdx = i-1; 20 | break; 21 | } 22 | } 23 | 24 | //2. Swap 25 | if(replaceIdx==-1) 26 | { 27 | reverse(nums.begin(), nums.end()); 28 | return; 29 | } 30 | 31 | for(int i=n-1; i>replaceIdx; i--) 32 | { 33 | if(nums[i] > nums[replaceIdx]) 34 | { 35 | swap(nums[replaceIdx], nums[i]); 36 | //3. sorting 37 | sort(nums.begin()+replaceIdx+1, nums.end()); 38 | break; 39 | } 40 | } 41 | } 42 | }; -------------------------------------------------------------------------------- /Class20/CombinationalSum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | void allCombinations(vector& candidates, int index, int target, vector& path, vector>& res) 4 | { 5 | if(index==candidates.size()) 6 | { 7 | return; 8 | } 9 | 10 | if(target==0) 11 | { 12 | res.push_back(path); 13 | return; 14 | } 15 | 16 | // include 17 | if(candidates[index]<=target) 18 | { 19 | path.push_back(candidates[index]); 20 | allCombinations(candidates, index, target-candidates[index], path, res); 21 | path.pop_back(); 22 | } 23 | 24 | //exclude 25 | allCombinations(candidates, index+1, target, path, res); 26 | } 27 | 28 | public: 29 | vector> combinationSum(vector& candidates, int target) { 30 | 31 | vector> res; 32 | vector path; 33 | 34 | allCombinations(candidates, 0, target, path, res); 35 | 36 | return res; 37 | } 38 | }; -------------------------------------------------------------------------------- /Class6/CountPairsWithSum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // int countPairs(int arr[], int n, int sum) 5 | // { 6 | // int count = 0; 7 | // for(int i=0; i>n; 49 | int arr[n]; 50 | for(int i=0; i>arr[i]; 53 | } 54 | int sum; 55 | cin>>sum; 56 | 57 | cout<& nums, int target) { 6 | 7 | int n = nums.size(); 8 | int s = 0; 9 | int e = n-1; 10 | 11 | while(s<=e) 12 | { 13 | int mid = (s+e)/2; 14 | if(nums[mid]==target) 15 | { 16 | return mid; 17 | } 18 | // left half is sorted 19 | else if(nums[s]<=nums[mid]) 20 | { 21 | if(nums[s] <= target && target <= nums[mid]) 22 | { 23 | e = mid-1; 24 | } 25 | else 26 | { 27 | s = mid+1; 28 | } 29 | } 30 | // right half is sorted 31 | else 32 | { 33 | if(nums[mid] <= target && target <= nums[e]) 34 | { 35 | s = mid+1; 36 | } 37 | else 38 | { 39 | e = mid-1; 40 | } 41 | } 42 | } 43 | return -1; 44 | } 45 | }; -------------------------------------------------------------------------------- /Class21/PalindromePartitioning.cpp: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/palindrome-partitioning/ 2 | 3 | class Solution { 4 | 5 | bool validPalindrome(string s, int start, int end) 6 | { 7 | while(start<=end) 8 | { 9 | if(s[start]!=s[end]) 10 | { 11 | return false; 12 | } 13 | start++; 14 | end--; 15 | } 16 | return true; 17 | } 18 | 19 | void validPartitions(int index, string s, vector>& ans, vector& curr) 20 | { 21 | // base case 22 | if(index == s.size()) 23 | { 24 | ans.push_back(curr); 25 | return; 26 | } 27 | 28 | for(int i=index; i> partition(string s) { 42 | 43 | vector> ans; 44 | vector curr; 45 | validPartitions(0, s, ans, curr); 46 | return ans; 47 | } 48 | }; -------------------------------------------------------------------------------- /Class21/RatInAMaze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | char maze[1001][1001]={0}; 5 | int sol[1001][1001]={0}; 6 | 7 | bool ratInMaze(int i, int j, int m, int n) 8 | { 9 | // base case 10 | if(i==m-1 && j==n-1) 11 | { 12 | sol[i][j] = 1; 13 | //print path 14 | for(int i=0; i=m || j>=n || maze[i][j]=='X') 28 | { 29 | return false; 30 | } 31 | sol[i][j] = 1; 32 | 33 | // recursive 34 | bool rightSuccess = ratInMaze(i,j+1,m,n); 35 | bool downSuccess = ratInMaze(i+1,j,m,n); 36 | 37 | sol[i][j] = 0; 38 | 39 | if(rightSuccess || downSuccess) 40 | { 41 | return true; 42 | } 43 | return false; 44 | } 45 | 46 | int main(){ 47 | int m,n; 48 | cin>>m>>n; 49 | for(int i=0; i>maze[i][j]; 54 | } 55 | } 56 | bool res = ratInMaze(0, 0, m, n); 57 | if(!res) 58 | { 59 | cout<<-1< 2 | using namespace std; 3 | 4 | void merge(int arr[], int s, int e) 5 | { 6 | int mid = (s+e)/2; 7 | int i = s; 8 | int j = mid+1; 9 | int k = s; 10 | int temp[100]; 11 | while(i<=mid && j<=e) 12 | { 13 | if(arr[i] < arr[j]) 14 | { 15 | temp[k++] = arr[i++]; 16 | // i++; 17 | // k++; 18 | } 19 | else 20 | { 21 | temp[k++] = arr[j++]; 22 | } 23 | } 24 | while(i<=mid) 25 | { 26 | temp[k++] = arr[i++]; 27 | } 28 | while(j<=e) 29 | { 30 | temp[k++] = arr[j++]; 31 | } 32 | for(int i=s; i<=e; i++) 33 | { 34 | arr[i] = temp[i]; 35 | } 36 | } 37 | 38 | void mergeSort(int arr[], int s, int e) 39 | { 40 | if(s>=e) 41 | { 42 | return; 43 | } 44 | // 1. divide 45 | int mid = (s+e)/2; 46 | // 2. recursively sort 47 | mergeSort(arr, s, mid); 48 | mergeSort(arr, mid+1, e); 49 | // 3. merge 50 | merge(arr, s, e); 51 | } 52 | 53 | int main(){ 54 | int arr[100]; 55 | int n; 56 | cin>>n; 57 | for(int i=0; i>arr[i]; 60 | } 61 | mergeSort(arr, 0, n-1); 62 | for(int i=0; i spiralOrder(vector>& matrix) { 6 | 7 | int n = matrix.size(); // rows 8 | int m = matrix[0].size(); // columns 9 | 10 | int sr = 0; 11 | int sc = 0; 12 | int er = n-1; 13 | int ec = m-1; 14 | 15 | vector res; 16 | 17 | while(sr<=er && sc<=ec) 18 | { 19 | for(int i=sc; i<=ec; i++) 20 | { 21 | res.push_back(matrix[sr][i]); 22 | } 23 | sr++; 24 | 25 | for(int i=sr; i<=er; i++) 26 | { 27 | res.push_back(matrix[i][ec]); 28 | } 29 | ec--; 30 | 31 | if(sr<=er) 32 | { 33 | for(int i=ec; i>=sc; i--) 34 | { 35 | res.push_back(matrix[er][i]); 36 | } 37 | } 38 | er--; 39 | 40 | if(sc<=ec) 41 | { 42 | for(int i=er; i>=sr; i--) 43 | { 44 | res.push_back(matrix[i][sc]); 45 | } 46 | } 47 | sc++; 48 | 49 | } 50 | return res; 51 | } 52 | }; -------------------------------------------------------------------------------- /Class20/WordSearch.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | bool check(vector>& board, string word, int index, int n, int m, int i, int j) 4 | { 5 | if(i<0 || i>=n || j<0 || j>=m || board[i][j]!=word[index]) 6 | { 7 | return false; 8 | } 9 | 10 | if(index == word.size()-1) 11 | { 12 | return true; 13 | } 14 | 15 | bool found = false; 16 | char temp = board[i][j]; 17 | board[i][j] = '$'; 18 | 19 | found = check(board, word, index+1, n, m, i-1, j) || 20 | check(board, word, index+1, n, m, i+1, j) || 21 | check(board, word, index+1, n, m, i, j-1) || 22 | check(board, word, index+1, n, m, i, j+1); 23 | 24 | board[i][j] = temp; 25 | return found; 26 | } 27 | 28 | public: 29 | bool exist(vector>& board, string word) { 30 | 31 | int n = board.size(); 32 | int m = board[0].size(); 33 | 34 | for(int i=0; i searchRange(vector& nums, int target) { 6 | 7 | int n = nums.size(); 8 | int s = 0; 9 | int e = n-1; 10 | int first = -1, last = -1; 11 | 12 | // first occurence 13 | while(s<=e) 14 | { 15 | int mid = (s+e)/2; 16 | if(nums[mid]==target) 17 | { 18 | first = mid; 19 | e = mid-1; 20 | } 21 | else if(nums[mid] > target) 22 | { 23 | e = mid-1; 24 | } 25 | else 26 | { 27 | s = mid+1; 28 | } 29 | } 30 | 31 | s = 0; 32 | e = n-1; 33 | // last occurence 34 | while(s<=e) 35 | { 36 | int mid = (s+e)/2; 37 | if(nums[mid]==target) 38 | { 39 | last = mid; 40 | s = mid+1; 41 | } 42 | else if(nums[mid] > target) 43 | { 44 | e = mid-1; 45 | } 46 | else 47 | { 48 | s = mid+1; 49 | } 50 | } 51 | return {first, last}; 52 | } 53 | }; -------------------------------------------------------------------------------- /Class15/SingleElementInSortedArray.cpp: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/single-element-in-a-sorted-array/ 2 | 3 | class Solution { 4 | public: 5 | int singleNonDuplicate(vector& nums) { 6 | 7 | int n = nums.size(); 8 | if(n==1 || nums[0]!=nums[1]) 9 | { 10 | return nums[0]; 11 | } 12 | if(nums[n-1]!=nums[n-2]) 13 | { 14 | return nums[n-1]; 15 | } 16 | 17 | int s = 0; 18 | int e = n-1; 19 | while(s<=e) 20 | { 21 | int mid = (s+e)/2; 22 | if(nums[mid]!=nums[mid-1] && nums[mid]!=nums[mid+1]) 23 | { 24 | return nums[mid]; 25 | } 26 | // index is even 27 | else if(mid%2==0) 28 | { 29 | if(nums[mid]==nums[mid+1]) 30 | { 31 | s = mid+1; 32 | } 33 | else 34 | { 35 | e = mid-1; 36 | } 37 | } 38 | // odd index 39 | else 40 | { 41 | if(nums[mid]==nums[mid-1]) 42 | { 43 | s = mid+1; 44 | } 45 | else 46 | { 47 | e = mid-1; 48 | } 49 | } 50 | } 51 | return -1; 52 | } 53 | }; -------------------------------------------------------------------------------- /Class18/InversionCount.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int merge(int arr[], int s, int e) 5 | { 6 | int mid = (s+e)/2; 7 | int i = s; 8 | int j = mid+1; 9 | int k = s; 10 | int temp[100]; 11 | int count = 0; 12 | while(i<=mid && j<=e) 13 | { 14 | if(arr[i] < arr[j]) 15 | { 16 | temp[k++] = arr[i++]; 17 | // i++; 18 | // k++; 19 | } 20 | else 21 | { 22 | temp[k++] = arr[j++]; 23 | count += mid-i+1; 24 | } 25 | } 26 | while(i<=mid) 27 | { 28 | temp[k++] = arr[i++]; 29 | } 30 | while(j<=e) 31 | { 32 | temp[k++] = arr[j++]; 33 | } 34 | for(int i=s; i<=e; i++) 35 | { 36 | arr[i] = temp[i]; 37 | } 38 | return count; 39 | } 40 | 41 | int findInversion(int arr[], int s, int e) 42 | { 43 | if(s>=e) 44 | { 45 | return 0; 46 | } 47 | // 1. divide 48 | int mid = (s+e)/2; 49 | // 2. recursively sort 50 | int x = findInversion(arr, s, mid); 51 | int y = findInversion(arr, mid+1, e); 52 | // 3. merge 53 | int z = merge(arr, s, e); 54 | return x+y+z; 55 | } 56 | 57 | int main(){ 58 | int arr[100]; 59 | int n; 60 | cin>>n; 61 | for(int i=0; i>arr[i]; 64 | } 65 | cout<=s2) 49 | { 50 | res += s1; 51 | } 52 | else 53 | { 54 | res += s2-s1; 55 | i++; 56 | } 57 | } 58 | else 59 | { 60 | res += s1; 61 | } 62 | } 63 | return res; 64 | } 65 | }; 66 | 67 | -------------------------------------------------------------------------------- /Class21/SurroundedRegions.cpp: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/surrounded-regions/ 2 | 3 | class Solution { 4 | 5 | void marking(vector>& board, int i, int j, int row, int col) 6 | { 7 | if(i<0 || j<0 || i>=row || j>=col || board[i][j]!='O') 8 | { 9 | return; 10 | } 11 | board[i][j] = '$'; 12 | marking(board, i+1, j, row, col); 13 | marking(board, i-1, j, row, col); 14 | marking(board, i, j+1, row, col); 15 | marking(board, i, j-1, row, col); 16 | } 17 | 18 | public: 19 | void solve(vector>& board) { 20 | 21 | int row = board.size(); 22 | int col = board[0].size(); 23 | 24 | for(int i=0; i r2) 41 | { 42 | e = mid1-1; 43 | } 44 | else 45 | { 46 | s = mid1+1; 47 | } 48 | 49 | } 50 | return 0.0; 51 | } 52 | } -------------------------------------------------------------------------------- /Class7/MaxProductOf3Num.cpp: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/maximum-product-of-three-numbers/ 2 | 3 | class Solution { 4 | public: 5 | int maximumProduct(vector& nums) { 6 | 7 | sort(nums.begin(), nums.end()); 8 | 9 | int n = nums.size(); 10 | 11 | int op1 = nums[n-1]*nums[n-2]*nums[n-3]; 12 | int op2 = nums[0]*nums[1]*nums[n-1]; 13 | 14 | return op2>op1 ? op2 : op1; 15 | 16 | } 17 | }; 18 | 19 | class Solution { 20 | public: 21 | int maximumProduct(vector& nums) { 22 | 23 | int n = nums.size(); 24 | int min1 = INT_MAX, min2 = INT_MAX; 25 | int max1 = INT_MIN, max2 = INT_MIN, max3 = INT_MIN; 26 | 27 | for(int i=0; i= max1) 40 | { 41 | max3 = max2; 42 | max2 = max1; 43 | max1 = nums[i]; 44 | } 45 | else if(nums[i] >= max2) 46 | { 47 | max3 = max2; 48 | max2 = nums[i]; 49 | } 50 | else if(nums[i] > max3) 51 | { 52 | max3 = nums[i]; 53 | } 54 | 55 | } 56 | 57 | int op1 = max1 * max2 * max3; 58 | int op2 = min1 * min2 * max1; 59 | return max(op1, op2); 60 | 61 | } 62 | }; -------------------------------------------------------------------------------- /Class11/BasicsString.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | //1. Input/Output 7 | // string firstName; 8 | // cin>>firstName; 9 | // cout<>& board, int i, int j, char c) 6 | { 7 | // row-column 8 | for(int k=0; k<9; k++) 9 | { 10 | if(board[i][k]==c || board[k][j]==c) 11 | { 12 | return false; 13 | } 14 | } 15 | 16 | int sr = (i/3)*3; 17 | int sc = (j/3)*3; 18 | 19 | // sub-board 20 | for(int a=sr; a>& board) 36 | { 37 | for(int i=0; i<9; i++) 38 | { 39 | for(int j=0; j<9; j++) 40 | { 41 | if(board[i][j]=='.') 42 | { 43 | for(char c='1'; c<='9'; c++) 44 | { 45 | if(isValidBoard(board, i, j, c)) 46 | { 47 | board[i][j] = c; 48 | 49 | if(fillSudoku(board)) 50 | { 51 | return true; 52 | } 53 | else 54 | { 55 | board[i][j] = '.'; 56 | } 57 | } 58 | } 59 | return false; 60 | } 61 | } 62 | } 63 | return true; 64 | } 65 | 66 | public: 67 | void solveSudoku(vector>& board) { 68 | 69 | fillSudoku(board); 70 | } 71 | }; --------------------------------------------------------------------------------