├── README.md └── Solutions ├── 2DArrays ├── Exercise_67.cpp ├── Exercise_68.cpp ├── Exercise_69.cpp ├── Exercise_70.cpp ├── Exercise_71.cpp ├── Exercise_72.cpp └── Exercise_73.cpp ├── Arrays ├── Exercise_46.cpp ├── Exercise_47.cpp ├── Exercise_48.cpp ├── Exercise_49.cpp ├── Exercise_50.cpp ├── Exercise_51.cpp ├── Exercise_52.cpp ├── Exercise_53.cpp ├── Exercise_54.cpp └── Exercise_55.cpp ├── Bitmasking ├── Exercise_100.cpp ├── Exercise_101.cpp ├── Exercise_97.cpp ├── Exercise_98.cpp └── Exercise_99.cpp ├── C++DivingDeeper └── Exercise_3.cpp ├── C++GettingStarted ├── Exercise_1.cpp └── Exercise_2.cpp ├── CharacterArrays ├── Exercise_74.cpp ├── Exercise_75.cpp ├── Exercise_76.cpp ├── Exercise_77.cpp └── Exercise_78.cpp ├── ConditionalStatements ├── Exercise_10.cpp ├── Exercise_11.cpp ├── Exercise_12.cpp ├── Exercise_13.cpp ├── Exercise_14.cpp └── Exercise_9.cpp ├── Functions ├── Exercise_33.cpp ├── Exercise_34.cpp ├── Exercise_35.cpp ├── Exercise_36.cpp ├── Exercise_37.cpp ├── Exercise_38.cpp ├── Exercise_39.cpp ├── Exercise_40.cpp ├── Exercise_41.cpp ├── Exercise_42.cpp ├── Exercise_43.cpp ├── Exercise_44.cpp └── Exercise_45.cpp ├── Loops ├── Exercise_15.cpp ├── Exercise_16.cpp ├── Exercise_17.cpp ├── Exercise_18.cpp ├── Exercise_19.cpp ├── Exercise_20.cpp ├── Exercise_21.cpp ├── Exercise_22.cpp ├── Exercise_23.cpp └── Exercise_24.cpp ├── Patterns ├── Exercise_25.cpp ├── Exercise_26.cpp ├── Exercise_27.cpp ├── Exercise_28.cpp ├── Exercise_29.cpp ├── Exercise_30.cpp ├── Exercise_31.cpp └── Exercise_32.cpp ├── Recursion ├── Exercise_102.cpp ├── Exercise_103.cpp ├── Exercise_104.cpp ├── Exercise_105.cpp ├── Exercise_106.cpp ├── Exercise_107.cpp ├── Exercise_108.cpp ├── Exercise_109.cpp ├── Exercise_110.cpp ├── Exercise_111.cpp ├── Exercise_112.cpp ├── Exercise_113.cpp └── Exercise_114.cpp ├── Sorting ├── Exercise_56.cpp ├── Exercise_57.cpp ├── Exercise_58.cpp ├── Exercise_59.cpp ├── Exercise_60.cpp ├── Exercise_61.cpp ├── Exercise_62.cpp ├── Exercise_63.cpp ├── Exercise_64.cpp ├── Exercise_65.cpp └── Exercise_66.cpp ├── Strings ├── Exercise_79.cpp ├── Exercise_80.cpp ├── Exercise_81.cpp ├── Exercise_82.cpp ├── Exercise_83.cpp ├── Exercise_84.cpp ├── Exercise_85.cpp ├── Exercise_86.cpp ├── Exercise_87.cpp ├── Exercise_88.cpp ├── Exercise_89.cpp ├── Exercise_90.cpp └── Exercise_91.cpp ├── VariablesDatatypes ├── Exercise_4.cpp ├── Exercise_5.cpp ├── Exercise_6.cpp ├── Exercise_7.cpp └── Exercise_8.cpp ├── Vectors ├── Exercise_92.cpp ├── Exercise_93.cpp ├── Exercise_94.cpp ├── Exercise_95.cpp └── Exercise_96.cpp └── description /README.md: -------------------------------------------------------------------------------- 1 | # Coding Minutes : CPP Programming for Beginners 2 | 3 | Solutions to CPP Programming for Beginners course code exercises 4 | -------------------------------------------------------------------------------- /Solutions/2DArrays/Exercise_67.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int numberOfZeros(vector> matrix) { 5 | 6 | if (matrix.size() == 0) { 7 | return 0; 8 | } 9 | 10 | int rows = matrix.size(); 11 | int columns = matrix[0].size(); 12 | 13 | int count = 0; 14 | 15 | for (int i = 0; i < rows; i++) { 16 | for (int j = 0; j < columns; j++) { 17 | if (matrix[i][j] == 0) { 18 | count++; 19 | } 20 | } 21 | } 22 | return count; 23 | } -------------------------------------------------------------------------------- /Solutions/2DArrays/Exercise_68.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int checkProductSign(vector> matrix) { 5 | 6 | int ans = 1; 7 | 8 | int row = matrix.size(); 9 | int column = matrix[0].size(); 10 | 11 | for (int i = 0; i < row; i++) { 12 | for (int j = 0; j < column; j++) { 13 | if (matrix[i][j] < 0) { 14 | ans = ans * (-1); 15 | } 16 | if (matrix[i][j] == 0) { 17 | ans = 0; 18 | } 19 | } 20 | } 21 | 22 | return (ans >= 0); 23 | } -------------------------------------------------------------------------------- /Solutions/2DArrays/Exercise_69.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sumOfEvenNumbers(int n, int m, int arr[1000][1000]) 5 | { 6 | int res=0; 7 | for(int i=0; i 2 | using namespace std; 3 | 4 | int sumOfDiagonalElement(vector> A) { 5 | 6 | int r = A.size(); 7 | int c = A[0].size(); 8 | 9 | int sum = 0; 10 | 11 | for (int i = 0; i < r; i++) { 12 | for (int j = 0; j < c; j++) { 13 | if (i == j) { 14 | sum += A[i][j]; 15 | } 16 | } 17 | } 18 | return sum; 19 | } -------------------------------------------------------------------------------- /Solutions/2DArrays/Exercise_71.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | // const int M=3 ; 4 | // const int N=4 ; 5 | 6 | bool searchMatrix(int matrix[][50],int M,int N,int target) { 7 | int low = 0, high = (N*M)-1; 8 | while(low <= high){ 9 | int mid = low + (high - low)/2; 10 | if(matrix[mid/M][mid%M] == target) 11 | return true; 12 | else if(matrix[mid/M][mid%M] < target) 13 | low = mid+1; 14 | else 15 | high = mid-1; 16 | } 17 | return false; 18 | } -------------------------------------------------------------------------------- /Solutions/2DArrays/Exercise_72.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | bool isMirror(int matrix1[][50],int matrix2[][50],int N) { 5 | int row = 0; 6 | int col = 0; 7 | for (int i = 0; i < N; i++) 8 | { 9 | for (int j = N - 1; j >= 0; j--) 10 | { 11 | if (matrix2[row][col] != matrix1[i][j]) return false; 12 | col++; 13 | } 14 | col = 0; 15 | row++; 16 | } 17 | return true; 18 | } -------------------------------------------------------------------------------- /Solutions/2DArrays/Exercise_73.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | void printSpiralMatrix(int a[][50],int m,int n) { 5 | // write your code here 6 | int i, k = 0, l = 0; 7 | 8 | while (k < m && l < n) { 9 | 10 | for (i = l; i < n; ++i) { 11 | cout << a[k][i] << " "; 12 | } 13 | k++; 14 | 15 | 16 | for (i = k; i < m; ++i) { 17 | cout << a[i][n - 1] << " "; 18 | } 19 | n--; 20 | 21 | 22 | if (k < m) { 23 | for (i = n - 1; i >= l; --i) { 24 | cout << a[m - 1][i] << " "; 25 | } 26 | m--; 27 | } 28 | 29 | if (l < n) { 30 | for (i = m - 1; i >= k; --i) { 31 | cout << a[i][l] << " "; 32 | } 33 | l++; 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /Solutions/Arrays/Exercise_46.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | pair largestAndSmallest(int A[], int sizeOfArray) { 5 | int minElement = INT_MAX; 6 | int maxElement = INT_MIN; 7 | 8 | for (int i = 0; i < sizeOfArray; i++) { 9 | int element = A[i]; 10 | minElement = min(minElement, element); 11 | maxElement = max(maxElement, element); 12 | } 13 | 14 | return make_pair(maxElement, minElement); 15 | } -------------------------------------------------------------------------------- /Solutions/Arrays/Exercise_47.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | float calculateMedian(int A[], int sizeOfArray) { 5 | if (sizeOfArray % 2 == 1) { 6 | // For odd-sized array, return the middle element 7 | return A[sizeOfArray / 2]; 8 | } else { 9 | // For even-sized array, return the average of the two middle elements 10 | int mid1 = sizeOfArray / 2 - 1; 11 | int mid2 = sizeOfArray / 2; 12 | return (A[mid1] + A[mid2]) / 2.0; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Solutions/Arrays/Exercise_48.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int kthSmallest(int A[], int k, int sizeOfArray) { 5 | 6 | sort(A, A+sizeOfArray); 7 | 8 | return A[k-1]; 9 | } -------------------------------------------------------------------------------- /Solutions/Arrays/Exercise_49.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | void moveNegativesToEnd(int* arr,int n) 5 | { 6 | int temp[n] ; 7 | int j = 0; 8 | 9 | for (int i = 0; i < n; i++) 10 | if (arr[i] >= 0) 11 | temp[j++] = arr[i]; 12 | 13 | if (j == n || j == 0) 14 | return; 15 | 16 | for (int i = 0; i < n; i++) 17 | if (arr[i] < 0) 18 | temp[j++] = arr[i]; 19 | 20 | for (int i = 0; i < n; i++) 21 | arr[i] = temp[i]; 22 | } -------------------------------------------------------------------------------- /Solutions/Arrays/Exercise_50.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | void printIntersection(int* nums1, int n, int* nums2, int m) 5 | { 6 | // write your code here 7 | 8 | int a_index = 0, b_index = 0; 9 | int a_element, b_element; 10 | int common_elements = 0; 11 | while (a_index < n && b_index < m) 12 | { 13 | a_element = nums1[a_index]; b_element = nums2[b_index]; 14 | if (a_element == b_element) 15 | { 16 | cout << to_string(a_element)+" "; 17 | ++common_elements; 18 | a_index++; b_index++; 19 | } else if (b_element < a_element) { 20 | b_index++; 21 | } else { 22 | a_index++; 23 | } 24 | } 25 | if (common_elements == 0) cout << -1; 26 | } -------------------------------------------------------------------------------- /Solutions/Arrays/Exercise_51.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | int getFrequency(int* arr,int n, int target) 5 | { 6 | int f = 0; 7 | 8 | for (int i=0;i 2 | using namespace std ; 3 | 4 | void twoSum(int* A, int size, int x) { 5 | 6 | for (int i = 0; i < (size - 1); i++) { 7 | for (int j = (i + 1); j < size; j++) { 8 | if (A[i] + A[j] == x) { 9 | cout << "(" << i << "," << j << ")" << endl; 10 | } 11 | } 12 | } 13 | return; 14 | } -------------------------------------------------------------------------------- /Solutions/Arrays/Exercise_53.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | bool threeSum(int* arr, int n) { 5 | bool found = false; 6 | for (int i = 0; i < n - 2; i++) { 7 | for (int j = i + 1; j < n - 1; j++) { 8 | for (int k = j + 1; k < n; k++) { 9 | if (arr[i] + arr[j] + arr[k] == 0) { 10 | cout << arr[i] << " " << arr[j] << " " 11 | << arr[k] << endl; 12 | found = true; 13 | } 14 | } 15 | } 16 | } 17 | return found ; 18 | } -------------------------------------------------------------------------------- /Solutions/Arrays/Exercise_54.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | void rotate(int* nums, int n, int k) { 5 | reverse(nums, nums + n - k); 6 | reverse(nums + n - k, nums + n); 7 | reverse(nums, nums + n); 8 | } 9 | -------------------------------------------------------------------------------- /Solutions/Arrays/Exercise_55.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findMissingNumber(int A[], int sizeOfArray) { 5 | int n = sizeOfArray; 6 | 7 | int sum = 0; 8 | 9 | for (int i = 0; i < sizeOfArray; i++) { 10 | sum += A[i]; 11 | } 12 | 13 | int missingNumber = (n+1)*(n+2)/2 - sum; 14 | 15 | return missingNumber; 16 | } -------------------------------------------------------------------------------- /Solutions/Bitmasking/Exercise_100.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int square(int n) 5 | { 6 | if (n == 0) 7 | return 0; 8 | 9 | int x = n >> 1; 10 | 11 | if (n & 1) 12 | return ((square(x) << 2) + (x << 2) + 1); 13 | else 14 | return (square(x) << 2); 15 | } -------------------------------------------------------------------------------- /Solutions/Bitmasking/Exercise_101.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int singleFrequencyElement(vector A) { 5 | 6 | int xorValue = 0; 7 | 8 | for (auto element : A) { 9 | xorValue = xorValue ^ element; 10 | } 11 | 12 | return xorValue; 13 | } -------------------------------------------------------------------------------- /Solutions/Bitmasking/Exercise_97.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int countSetBit(int n) { 5 | int count = 0; 6 | for (int i = 0; i <= 30; i++) { 7 | if (n & (1 << i)) { 8 | count++; 9 | } 10 | } 11 | return count; 12 | } 13 | -------------------------------------------------------------------------------- /Solutions/Bitmasking/Exercise_98.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int isPowerOfTwo(int n) { 5 | 6 | if (!(n & (n-1))) { 7 | return 1; 8 | } 9 | else { 10 | return 0; 11 | } 12 | } -------------------------------------------------------------------------------- /Solutions/Bitmasking/Exercise_99.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int divide(int X, int Y) { 5 | int quotient = 0; 6 | while (X >= Y) { 7 | X = X-Y; 8 | quotient++; 9 | } 10 | return quotient; 11 | } -------------------------------------------------------------------------------- /Solutions/C++DivingDeeper/Exercise_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | string name; 6 | cin>>name; 7 | 8 | cout <<"Hello " + name; 9 | } -------------------------------------------------------------------------------- /Solutions/C++GettingStarted/Exercise_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | // Your Code goes here 6 | cout << "Hello, World!"; 7 | } -------------------------------------------------------------------------------- /Solutions/C++GettingStarted/Exercise_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | int n; 6 | cin >>n; 7 | cout < 2 | using namespace std; 3 | 4 | char largestFrequencyCharacter (char c[], int size) { 5 | 6 | unordered_map < char, int > freq; 7 | 8 | for (int i = 0; i < size; i++) { 9 | freq[c[i]]++; 10 | } 11 | 12 | char cc; 13 | 14 | int maxFreq = 0; 15 | 16 | for (auto element : freq) { 17 | if (element.second > maxFreq) { 18 | maxFreq = element.second; 19 | cc = element.first; 20 | } 21 | } 22 | return cc; 23 | } -------------------------------------------------------------------------------- /Solutions/CharacterArrays/Exercise_75.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | char largestFrequencyCharacter(char C[], int size) { 5 | 6 | char ans = '#'; 7 | int freq = 0; 8 | for (int i = 0; i < 26; i++) { 9 | int count = 0; 10 | for (int j = 0; j < size; j++) { 11 | if (char(C[j]-'a') == i) { 12 | count++; 13 | } 14 | } 15 | if (count > freq) { 16 | ans = char('a' + i); 17 | freq = count; 18 | } 19 | } 20 | return ans; 21 | } -------------------------------------------------------------------------------- /Solutions/CharacterArrays/Exercise_76.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int isSorted(char C[], int size) { 5 | 6 | int ans = 1; 7 | for (int i = 0; i < size-1; i++) { 8 | if (C[i] > C[i+1]) { 9 | ans = 0; 10 | } 11 | } 12 | 13 | return ans; 14 | } -------------------------------------------------------------------------------- /Solutions/CharacterArrays/Exercise_77.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int countOfDifferentCharacters(char C[], int size) { 5 | unordered_map < char, int > freq; 6 | 7 | for (int i = 0; i < size; i++) { 8 | freq[C[i]]++; 9 | } 10 | 11 | return freq.size(); 12 | } -------------------------------------------------------------------------------- /Solutions/CharacterArrays/Exercise_78.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector> rotateTheMatrix(vector> matrix) { 5 | 6 | vector> v (matrix.size(), vector(matrix[0].size(), 0)); 7 | 8 | for (int i = matrix.size()-1; i >= 0; i--) { 9 | for (int j = 0; j < matrix[i].size(); j++) { 10 | v[j][(matrix.size()-1-i)] = matrix[i][j]; 11 | } 12 | } 13 | return v; 14 | } -------------------------------------------------------------------------------- /Solutions/ConditionalStatements/Exercise_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | int x,y; 6 | cin >> x >> y; 7 | 8 | float c = 0.9 * x; 9 | 10 | if(y>c) cout << "no"; 11 | else cout << "yes"; 12 | } -------------------------------------------------------------------------------- /Solutions/ConditionalStatements/Exercise_11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | 6 | int a,b,c,d,e ; cin >> a >> b >> c >> d >> e ; 7 | float marks= (a+b+c+d+e)/5 ; 8 | 9 | if (marks>=90) cout << "A+" ; 10 | else if (marks>=80 && marks<90) cout << "A" ; 11 | else if (marks>=70 && marks<80) cout << "B" ; 12 | else if (marks>=60 && marks<70) cout << "C" ; 13 | else cout << "D" ; 14 | } -------------------------------------------------------------------------------- /Solutions/ConditionalStatements/Exercise_12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | 6 | int units; 7 | cin >> units; 8 | int ans=0; 9 | 10 | if (units <= 50) 11 | { 12 | ans=units * 20; 13 | } 14 | else if (units <= 100) 15 | { 16 | ans= (50 * 20) + 17 | (units - 50) * 30; 18 | } 19 | else if (units <= 150) 20 | { 21 | ans=(50* 20) + 22 | (50 * 30) + 23 | (units - 100) * 40; 24 | } 25 | else if (units > 150) 26 | { 27 | ans= (50 * 20) + 28 | (50 * 30) + 29 | (50 * 40) + 30 | (units - 150) * 50; 31 | } 32 | 33 | cout << ans; 34 | } -------------------------------------------------------------------------------- /Solutions/ConditionalStatements/Exercise_13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | int p,t,r; 6 | cin >> p >> t; 7 | float si; 8 | 9 | if(t<3) { 10 | r=5; 11 | } 12 | else { 13 | r=6; 14 | } 15 | 16 | si= (float)(p*r*t)/100; 17 | cout << si; 18 | } -------------------------------------------------------------------------------- /Solutions/ConditionalStatements/Exercise_14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | 6 | int a ; cin >> a; 7 | char c ; cin >> c; 8 | int b ; cin >> b; 9 | 10 | int res=0; 11 | 12 | switch(c){ 13 | case '+': 14 | res=a+b; 15 | break; 16 | case '-': 17 | res=a-b; 18 | break; 19 | case '*': 20 | res=a*b; 21 | break; 22 | case '/': 23 | res=a/b; 24 | break; 25 | default: 26 | break; 27 | } 28 | 29 | cout << res ; 30 | } -------------------------------------------------------------------------------- /Solutions/ConditionalStatements/Exercise_9.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | int n ; cin >> n; 6 | if(n%2==0) cout << "even"; 7 | else cout << "odd"; 8 | } -------------------------------------------------------------------------------- /Solutions/Functions/Exercise_33.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sum_of_digits(int n) { 5 | if (n == 0) return 0; 6 | 7 | int sum = 0; 8 | 9 | while (n > 0) { 10 | sum += (n % 10); 11 | n = n - (n % 10); 12 | n = n / 10; 13 | } 14 | return sum; 15 | } -------------------------------------------------------------------------------- /Solutions/Functions/Exercise_34.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int binary_to_decimal(string binaryInteger) { 5 | int decimalInteger = 0; 6 | 7 | int size = binaryInteger.size(); 8 | 9 | for (int i = 0; i < size; i++) { 10 | if (binaryInteger[i] == '1') { 11 | decimalInteger += (1 << (size - i - 1)); 12 | } 13 | } 14 | 15 | return decimalInteger; 16 | } -------------------------------------------------------------------------------- /Solutions/Functions/Exercise_35.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int is_prime(int n) { 5 | if (n == 1) { 6 | return 0; 7 | } 8 | for (int i = 2; i * i <= n; i++) { 9 | if (n % i == 0) { 10 | return 0; 11 | } 12 | } 13 | return 1; 14 | } -------------------------------------------------------------------------------- /Solutions/Functions/Exercise_36.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int factorial(int n) { 5 | 6 | int F[n+1]; 7 | F[0] = 1; 8 | 9 | for (int i = 1; i <=n ; i++) { 10 | F[i] = F[i-1]*i; 11 | } 12 | return F[n]; 13 | } -------------------------------------------------------------------------------- /Solutions/Functions/Exercise_37.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | double getArea(string shape, double dimension1, double dimension2) 5 | { 6 | if(shape=="Square") { 7 | return dimension1*dimension1; 8 | } 9 | 10 | if(shape=="Rectangle"){ 11 | return dimension1*dimension2; 12 | } 13 | 14 | if(shape=="Circle") { 15 | return 3.14*dimension1*dimension1; 16 | } 17 | 18 | return 0.5*dimension1*dimension2; 19 | } -------------------------------------------------------------------------------- /Solutions/Functions/Exercise_38.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int power(int x, int y) { 5 | 6 | int ans = 1; 7 | for (int i = 1; i <= y; i++) { 8 | ans = ans * x; 9 | } 10 | return ans; 11 | } -------------------------------------------------------------------------------- /Solutions/Functions/Exercise_39.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getProduct(int a,int b) 5 | { 6 | int sum = 0; 7 | 8 | for (int i = 1; i <= a; i++) 9 | { 10 | sum += b; 11 | } 12 | return sum; 13 | } -------------------------------------------------------------------------------- /Solutions/Functions/Exercise_40.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int greatestCommonDivisor (int x, int y) { 5 | 6 | if (y == 0) { 7 | return x; 8 | } 9 | 10 | return greatestCommonDivisor (y, x % y); 11 | } -------------------------------------------------------------------------------- /Solutions/Functions/Exercise_41.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector printPrimes(int n) { 5 | if ( n == 1 ) { 6 | return {}; 7 | } 8 | 9 | bool primes[n+1]; 10 | 11 | for (int i = 0; i <= n; i++) { 12 | primes[i] = true; 13 | } 14 | primes[0] = false; 15 | primes[1] = false; 16 | primes[2] = true; 17 | 18 | for (int i = 2; i <= n; i++) { 19 | if (primes[i] && i * i <= n) { 20 | for (int j = 2*i; j <= n; j += i) { 21 | primes[j] = false; 22 | } 23 | } 24 | } 25 | 26 | vector v; 27 | 28 | for (int i = 2; i <= n; i++) { 29 | if (primes[i]) { 30 | v.push_back(i); 31 | } 32 | } 33 | 34 | return v; 35 | } -------------------------------------------------------------------------------- /Solutions/Functions/Exercise_42.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int nCr (int n, int r) { 5 | 6 | int factorial[n+1]; 7 | 8 | factorial[0] = 1; 9 | 10 | for (int i = 1; i <= n; i++) { 11 | factorial[i] = factorial[i-1] * i; 12 | } 13 | 14 | int answer = factorial[n] / (factorial[r] * factorial[n-r]); 15 | 16 | return answer; 17 | } -------------------------------------------------------------------------------- /Solutions/Functions/Exercise_43.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fibonacci(int n) 5 | { 6 | if (n == 0) return 0; 7 | if (n == 1) return 1; 8 | int first = 0, second = 1; 9 | for (int i = 2; i <= n; ++i) 10 | { 11 | int next = first + second; 12 | first = second; 13 | second = next; 14 | } 15 | return second; 16 | } 17 | -------------------------------------------------------------------------------- /Solutions/Functions/Exercise_44.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int noOfTrailingZeroes(int num) 5 | { 6 | if (num == 0) return 1; 7 | int trailingZeroesCount = 0; 8 | 9 | while (num > 0) 10 | { 11 | if (num%10 == 0) 12 | { 13 | ++trailingZeroesCount; 14 | num /= 10; 15 | } 16 | else 17 | { 18 | break; 19 | } 20 | } 21 | 22 | return trailingZeroesCount; 23 | } 24 | -------------------------------------------------------------------------------- /Solutions/Functions/Exercise_45.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int noOfTrailingZeroesInFactorial(int n) 5 | { 6 | int trailingZeroes = 0; 7 | while (n > 0) { 8 | n /= 5; 9 | trailingZeroes += n; 10 | } 11 | return trailingZeroes; 12 | } 13 | -------------------------------------------------------------------------------- /Solutions/Loops/Exercise_15.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int largest (int n, int arr[]) 5 | { 6 | int res = -1; 7 | for(int i=0; i 2 | using namespace std; 3 | 4 | void mainFn() { 5 | int n; 6 | cin >> n; 7 | 8 | int sum = 0; 9 | 10 | while (n != 0) { 11 | sum = sum + n % 10; 12 | n = n / 10; 13 | } 14 | cout<< sum; 15 | } -------------------------------------------------------------------------------- /Solutions/Loops/Exercise_17.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | int n; 6 | cin >> n; 7 | 8 | int sum = 0; 9 | 10 | while (n != 0) { 11 | int r=n%10 ; 12 | if(r%2!=0) sum= sum + r; 13 | n = n / 10; 14 | } 15 | cout<< sum; 16 | } -------------------------------------------------------------------------------- /Solutions/Loops/Exercise_18.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void mainFn() { 6 | int n ; cin >> n; 7 | int sumOdd = 0; 8 | 9 | string num = to_string(n); 10 | 11 | for(int i = 0; i < num.size(); i++) 12 | { 13 | if (i % 2 == 0) 14 | sumOdd = sumOdd + (int(num[i]) - 48); 15 | } 16 | cout << sumOdd; 17 | } -------------------------------------------------------------------------------- /Solutions/Loops/Exercise_19.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | int n; 6 | cin >> n; 7 | int c3=0,c5=0; 8 | 9 | for(int i=1;i<=n;i++){ 10 | c3++ ; c5++ ; 11 | string d="" ; 12 | if(c3==3){ 13 | c3=0 ;d+="Fizz" ; 14 | } 15 | if(c5==5){ 16 | c5=0 ;d+="Buzz" ; 17 | } 18 | string a=to_string(i) ; 19 | if(d=="") cout << a << " " ; 20 | else cout << d << " " ; 21 | } 22 | } -------------------------------------------------------------------------------- /Solutions/Loops/Exercise_20.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | int n ; cin >> n; 6 | if (n == 1) { 7 | cout << 0 ; 8 | return ; 9 | } 10 | for (int i = 2; i * i <= n; i++) { 11 | if (n % i == 0) { 12 | { 13 | cout << 0 ; 14 | return ; 15 | } 16 | } 17 | } 18 | cout << 1 ; 19 | } -------------------------------------------------------------------------------- /Solutions/Loops/Exercise_21.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | int n ; cin >> n; 6 | int sum=0,temp=n ; 7 | 8 | while(n>0) { 9 | int r=n%10; 10 | sum=sum+(r*r*r); 11 | n=n/10; 12 | } 13 | 14 | if(temp==sum) 15 | cout << 1 ; 16 | else 17 | cout << 0; 18 | } -------------------------------------------------------------------------------- /Solutions/Loops/Exercise_22.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | int n ; cin >> n ; 6 | int F[n+1]; 7 | F[0] = 1; 8 | 9 | for (int i = 1; i <=n ; i++) { 10 | F[i] = F[i-1]*i; 11 | } 12 | cout << F[n]; 13 | } -------------------------------------------------------------------------------- /Solutions/Loops/Exercise_23.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | int n ; cin >> n; 6 | int a = 0, b = 1, c, i; 7 | 8 | if( n == 0){ 9 | cout << a ; 10 | return ; 11 | } 12 | 13 | for(i = 2; i <= n; i++){ 14 | c = a + b; 15 | a = b; 16 | b = c; 17 | } 18 | cout << b; 19 | } -------------------------------------------------------------------------------- /Solutions/Loops/Exercise_24.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | int isPrime(int num) 5 | { 6 | for (int i = 2; i*i <= num; ++i) 7 | { 8 | if (num%i == 0) return 0; 9 | } 10 | return 1; 11 | } 12 | void printPrimes(int N) { 13 | for (int i = 2; i <= N; ++i) 14 | { 15 | if (isPrime(i) == 1) cout << i << " "; 16 | } 17 | } -------------------------------------------------------------------------------- /Solutions/Patterns/Exercise_25.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | int n ; cin >> n ; 6 | for(int i=1;i<=n;i++) { 7 | for(int j=1;j<=i;j++) { 8 | cout << i ; 9 | } 10 | cout << "\n" ; 11 | } 12 | } -------------------------------------------------------------------------------- /Solutions/Patterns/Exercise_26.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | int n ; cin >> n ; 6 | for(int i=1;i<=n;i++) { 7 | for(int j=1;j<=i;j++) { 8 | cout << j ; 9 | } 10 | cout << "\n" ; 11 | } 12 | } -------------------------------------------------------------------------------- /Solutions/Patterns/Exercise_27.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | int n ; cin >> n ; 6 | int temp=1 ; 7 | for(int i=1;i<=n;i++) { 8 | for(int j=1;j<=i;j++) { 9 | cout << temp++ ; 10 | } 11 | cout << "\n" ; 12 | } 13 | } -------------------------------------------------------------------------------- /Solutions/Patterns/Exercise_28.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | int n ; cin >> n ; 6 | int st=1; 7 | int sp=n-1 ; 8 | for(int i=1;i<=n;i++) { 9 | 10 | for(int j=1;j<=sp;j++) { 11 | cout << " " ; 12 | } 13 | 14 | for(int j=1;j<=st;j++) { 15 | cout << "*" ; 16 | } 17 | 18 | cout << "\n" ; 19 | 20 | sp-- ; 21 | st+=2 ; 22 | } 23 | } -------------------------------------------------------------------------------- /Solutions/Patterns/Exercise_29.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | int n ; cin >> n ; 6 | int st=1; 7 | int sp=n/2 ; 8 | for(int i=1;i<=n;i++) { 9 | 10 | for(int j=1;j<=sp;j++) { 11 | cout << " " ; 12 | } 13 | 14 | for(int j=1;j<=st;j++) { 15 | cout << "*" ; 16 | } 17 | 18 | cout << "\n" ; 19 | 20 | if(i<=n/2) { 21 | sp-- ; 22 | st+=2 ; 23 | } 24 | else { 25 | sp++ ; 26 | st-=2 ; 27 | } 28 | 29 | } 30 | } -------------------------------------------------------------------------------- /Solutions/Patterns/Exercise_30.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | int n ; cin >> n ; 6 | int i, j, num = 1, gap; 7 | gap = n - 1; 8 | 9 | for ( j = 1 ; j <= n ; j++ ) 10 | { 11 | num = j; 12 | for ( i = 1 ; i <= gap ; i++ ) 13 | cout << " "; 14 | 15 | gap --; 16 | for ( i = 1 ; i <= j ; i++ ) 17 | { 18 | cout << num; 19 | num++; 20 | } 21 | num--; 22 | num--; 23 | for ( i = 1 ; i < j ; i++) 24 | { 25 | cout << num; 26 | num--; 27 | } 28 | cout << "\n"; 29 | 30 | } 31 | } -------------------------------------------------------------------------------- /Solutions/Patterns/Exercise_31.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | int n ; cin >> n ; 6 | for(int i=1;i<=n;i++) { 7 | int temp=i ; 8 | 9 | for(int j=1;j<=i;j++) { 10 | cout << temp%2 ; 11 | temp++ ; 12 | } 13 | 14 | cout << "\n" ; 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /Solutions/Patterns/Exercise_32.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | 6 | int n ; cin >> n ; 7 | char ch; 8 | 9 | for(int i=1;i<=n;i++) 10 | { 11 | ch='A'; 12 | 13 | for(int j=0;j 2 | using namespace std; 3 | 4 | void printIncDec(int N) 5 | { 6 | if (N <= 0) 7 | { 8 | cout << "\n" ; 9 | return; 10 | } 11 | cout << N << " " ; 12 | printIncDec(N-1); 13 | cout << N << " "; 14 | } 15 | -------------------------------------------------------------------------------- /Solutions/Recursion/Exercise_103.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int factorial(int n) { 5 | if (n == 0) return 1; 6 | else { 7 | return n * factorial(n-1); 8 | } 9 | } -------------------------------------------------------------------------------- /Solutions/Recursion/Exercise_104.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fibonacci (int n) { 5 | if ( n == 0 || n == 1) { 6 | return 1; 7 | } 8 | else { 9 | return fibonacci(n-1) + fibonacci(n-2); 10 | } 11 | } -------------------------------------------------------------------------------- /Solutions/Recursion/Exercise_105.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sumOfDigit(int n) { 5 | if ( n == 0) { 6 | return 0; 7 | } 8 | else { 9 | return n % 10 + sumOfDigit(n/10); 10 | } 11 | } -------------------------------------------------------------------------------- /Solutions/Recursion/Exercise_106.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fibonacciSeries(int n) { 5 | 6 | if ( n == 0 || n == 1 || n == 2) { 7 | return 1; 8 | } 9 | else { 10 | return fibonacciSeries(n-1) + fibonacciSeries(n-2) + fibonacciSeries(n-3); 11 | } 12 | } -------------------------------------------------------------------------------- /Solutions/Recursion/Exercise_107.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int C(int n, int k) { 5 | if (k == 0 || k == n) { 6 | return 1; 7 | } 8 | return C(n-1, k-1) + C(n-1, k); 9 | } -------------------------------------------------------------------------------- /Solutions/Recursion/Exercise_108.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getNoOfWaysToTile(int n) 5 | { 6 | if (n <= 2) return n; 7 | return getNoOfWaysToTile(n - 1) + getNoOfWaysToTile(n - 2); 8 | } 9 | -------------------------------------------------------------------------------- /Solutions/Recursion/Exercise_109.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int rev(int n, int temp) 5 | { 6 | if (n == 0) 7 | return temp; 8 | 9 | temp = (temp * 10) + (n % 10); 10 | return rev(n / 10, temp); 11 | } 12 | 13 | 14 | int isPalindrome(int n) 15 | { 16 | int reversedNum = rev(n, 0); 17 | if (reversedNum == n) return 1; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Solutions/Recursion/Exercise_110.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getFirstOccurenceUtility(int* arr, int target, int index) 5 | { 6 | if (arr[index] == target) return index; 7 | return getFirstOccurenceUtility(arr, target, index+1); 8 | } 9 | 10 | int getFirstOccurence(int* arr, int target) 11 | { 12 | return getFirstOccurenceUtility(arr, target, 0); 13 | } -------------------------------------------------------------------------------- /Solutions/Recursion/Exercise_111.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getLastOccurenceUtility(int* arr, int target, int index) 5 | { 6 | if (arr[index] == target) return index; 7 | return getLastOccurenceUtility(arr, target, index-1); 8 | } 9 | 10 | int getLastOccurence(int* arr,int n, int target) 11 | { 12 | return getLastOccurenceUtility(arr, target, n-1); 13 | } 14 | -------------------------------------------------------------------------------- /Solutions/Recursion/Exercise_112.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | int getFrequencyUtility(int *arr, int n, int target, int index) 5 | { 6 | if (index == n) return 0; 7 | 8 | int freq = 0; 9 | if (arr[index] == target) ++freq; 10 | 11 | freq += getFrequencyUtility(arr, n, target, index+1); 12 | return freq; 13 | } 14 | 15 | int getFrequency(int* arr, int n, int target) 16 | { 17 | return getFrequencyUtility(arr,n, target, 0); 18 | } -------------------------------------------------------------------------------- /Solutions/Recursion/Exercise_113.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | int pow(int a, int b) 5 | { 6 | if (b == 0) 7 | return 1; 8 | else 9 | return a * pow(a, b - 1); 10 | } -------------------------------------------------------------------------------- /Solutions/Recursion/Exercise_114.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | int getNoOfBinaryStrings(int n) 5 | { 6 | if (n <= 1) return n+1; 7 | return getNoOfBinaryStrings(n-1) + getNoOfBinaryStrings(n-2); 8 | } -------------------------------------------------------------------------------- /Solutions/Sorting/Exercise_56.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int elementPresentOrNot(vector array, int element) { 5 | int low = 0, high = array.size() - 1; 6 | 7 | while (low <= high) { 8 | int mid = ( low + high ) / 2; 9 | if (array[mid] > element) { 10 | high = mid-1; 11 | continue; 12 | } 13 | if (array[mid] < element) { 14 | low = mid +1; 15 | continue; 16 | } 17 | return 1; 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Solutions/Sorting/Exercise_57.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int frequencyOfLargestElement (vector A) { 5 | 6 | int low = 0, high = A.size() - 1; 7 | 8 | int largestEle = A[high]; 9 | 10 | int ans = 0; 11 | 12 | while (low <= high) { 13 | int mid = ( low + high ) / 2; 14 | if (A[mid] == largestEle) { 15 | ans = mid; 16 | high = mid-1; 17 | } 18 | else { 19 | low = mid+1; 20 | } 21 | } 22 | return (A.size() - ans); 23 | } -------------------------------------------------------------------------------- /Solutions/Sorting/Exercise_58.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int countOfOne(vector A) { 5 | 6 | int l = 0, h = A.size() -1; 7 | 8 | int ans = -1; 9 | 10 | while (l <= h) { 11 | int mid = (l + h)/2; 12 | if (A[mid] == 1) { 13 | ans = mid; 14 | h = mid-1; 15 | } 16 | else { 17 | l = mid+1; 18 | } 19 | } 20 | 21 | if (ans == -1) { 22 | return 0; 23 | } 24 | else { 25 | return (int(A.size()) - ans ); 26 | } 27 | } -------------------------------------------------------------------------------- /Solutions/Sorting/Exercise_59.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int squareRoot(int n) { 5 | 6 | int l = 1, r = n; 7 | 8 | int ans = 1; 9 | 10 | while (l <= r) { 11 | int mid = (l+r)/2; 12 | if (mid * mid <= n) { 13 | ans = mid; 14 | l = mid+1; 15 | } 16 | else { 17 | r = mid-1; 18 | } 19 | } 20 | return ans; 21 | } -------------------------------------------------------------------------------- /Solutions/Sorting/Exercise_60.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int numberOfOne(vector array) { 5 | 6 | int low = 0, high = array.size()-1; 7 | 8 | int ans = high+1; 9 | 10 | while (low <= high) { 11 | int mid = (low + high)/2; 12 | if (array[mid] == 1) { 13 | ans = mid; 14 | high = mid-1; 15 | } 16 | else { 17 | low = mid+1; 18 | } 19 | } 20 | 21 | return (array.size()) - ans; 22 | } -------------------------------------------------------------------------------- /Solutions/Sorting/Exercise_61.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector sortTheArray(vector A) { 5 | int zero = 0; 6 | int two = 0; 7 | 8 | for (int i = 0; i < A.size(); i++) { 9 | if (A[i] == 0) zero++; 10 | if (A[i] == 2) two++; 11 | } 12 | int i,j; 13 | for (i = 0; i < A.size(); i++) { 14 | if (zero == 0) break; 15 | A[i] = 0; 16 | zero--; 17 | } 18 | 19 | for (j = A.size() - 1; j >= 0; j--) { 20 | if (two == 0) break; 21 | A[j] = 2; 22 | two--; 23 | } 24 | while (i <= j) { 25 | A[i] = 1; 26 | i++; 27 | } 28 | return A; 29 | } -------------------------------------------------------------------------------- /Solutions/Sorting/Exercise_62.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int search(int *nums,int n,int target) { 5 | 6 | int start = 0, end = n- 1; 7 | 8 | while (start <= end) { 9 | int mid = start + ((end - start)>>1); 10 | 11 | if (nums[mid] == target) { 12 | return mid; 13 | } 14 | else if (nums[mid] > target) { 15 | end = mid - 1; 16 | } 17 | else { 18 | start = mid + 1; 19 | } 20 | } 21 | 22 | return -1; 23 | } -------------------------------------------------------------------------------- /Solutions/Sorting/Exercise_63.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int first(int *arr, int n,int x) 5 | { 6 | int low = 0, high = n - 1, res = -1; 7 | while (low <= high) 8 | { 9 | int mid = (low + high) / 2; 10 | if (arr[mid] > x) 11 | high = mid - 1; 12 | else if (arr[mid] < x) 13 | low = mid + 1; 14 | else 15 | { 16 | res = mid; 17 | high = mid - 1; 18 | } 19 | } 20 | return res; 21 | } 22 | 23 | int last(int *arr,int n,int x) 24 | { 25 | int low = 0, high = n - 1, res = -1; 26 | while (low <= high) 27 | { 28 | int mid = (low + high) / 2; 29 | if (arr[mid] > x) 30 | high = mid - 1; 31 | else if (arr[mid] < x) 32 | low = mid + 1; 33 | else 34 | { 35 | res = mid; 36 | low = mid + 1; 37 | } 38 | } 39 | return res; 40 | } 41 | 42 | void firstAndLastOccurence(int *nums,int n,int tar) { 43 | cout << first(nums,n,tar) << " " << last(nums,n,tar); 44 | } -------------------------------------------------------------------------------- /Solutions/Sorting/Exercise_64.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int binarySearch(int* arr,int n, int target, bool leftmost) { 5 | int lo = 0; 6 | int hi = n - 1; 7 | int idx = -1; 8 | while (lo <= hi) { 9 | int mid = (lo + hi) / 2; 10 | 11 | if (target > arr[mid]) { 12 | lo = mid + 1; 13 | } else if (target < arr[mid]) { 14 | hi = mid - 1; 15 | } else { 16 | idx = mid; 17 | if (leftmost) { 18 | hi = mid - 1; 19 | } else { 20 | lo = mid + 1; 21 | } 22 | } 23 | } 24 | return idx; 25 | } 26 | 27 | int getCount(int *nums,int n,int tar) { 28 | int left = binarySearch(nums,n, tar, true); 29 | if (left < 0) return 0; 30 | int right = binarySearch(nums,n, tar, false); 31 | return right - left + 1; 32 | } -------------------------------------------------------------------------------- /Solutions/Sorting/Exercise_65.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | void swap(vector &nums, int to, int from) 5 | { 6 | int temp = nums[to]; 7 | nums[to] = nums[from]; 8 | nums[from] = temp; 9 | } 10 | 11 | void sort012(vector &nums) { 12 | int lo = 0; 13 | int hi = nums.size() - 1; 14 | int mid = 0; 15 | while (mid <= hi) { 16 | switch (nums[mid]) { 17 | case 0: { 18 | swap(nums, lo, mid); 19 | lo++; 20 | mid++; 21 | break; 22 | } 23 | case 1: 24 | mid++; 25 | break; 26 | case 2: { 27 | swap(nums, hi, mid); 28 | hi--; 29 | break; 30 | } 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /Solutions/Sorting/Exercise_66.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | void column_sum(vector>matrix, int n) 5 | { 6 | int i, j, sum = 0; 7 | for (i = 0; i < n; ++i) { 8 | for (j = 0; j < n; ++j) { 9 | sum = sum + matrix[j][i]; 10 | } 11 | cout << sum << " " ; 12 | sum = 0; 13 | } 14 | } 15 | 16 | 17 | void row_sum(vector> matrix, int n) 18 | { 19 | int i, j, sum = 0; 20 | for (i = 0; i < n; ++i) { 21 | for (j = 0; j < n; ++j) { 22 | sum = sum + matrix[i][j]; 23 | } 24 | cout << sum << " " ; 25 | sum = 0; 26 | } 27 | } 28 | 29 | void printRowColSum(vector> matrix) { 30 | int n = matrix.size(); 31 | if (n == 0) return; 32 | row_sum(matrix, n); 33 | cout << "\n" ; 34 | column_sum(matrix, n); 35 | } -------------------------------------------------------------------------------- /Solutions/Strings/Exercise_79.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | bool isAnagram(string s, string t) { 5 | 6 | int alphabet[26]={0} ; 7 | for (int i = 0; i < s.size(); i++) alphabet[s[i] - 'a']++; 8 | for (int i = 0; i < t.size(); i++) alphabet[t[i] - 'a']--; 9 | for (int i : alphabet) if (i != 0) return false; 10 | return true; 11 | } -------------------------------------------------------------------------------- /Solutions/Strings/Exercise_80.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int isPalindrome(string s) { 5 | 6 | int n = s.size(); 7 | 8 | if ((n&1)) { 9 | string s1 = s.substr(0, n/2); 10 | string s2 = s.substr(n/2+1); 11 | reverse(s2.begin(), s2.end()); 12 | if (s1 == s2) { 13 | return 1; 14 | } 15 | 16 | return 0; 17 | } 18 | else { 19 | string s1 = s.substr(0, n/2); 20 | string s2 = s.substr(n/2); 21 | reverse(s2.begin(), s2.end()); 22 | if (s1 == s2) { 23 | return 1; 24 | } 25 | 26 | return 0; 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /Solutions/Strings/Exercise_81.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | void upperToLower(string s) 5 | { 6 | int size = s.length(); 7 | for (int i = 0; i < size; ++i) 8 | { 9 | cout << (char)(tolower(s[i])); 10 | } 11 | } -------------------------------------------------------------------------------- /Solutions/Strings/Exercise_82.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | char largestFrequencyCharacter(string s) { 5 | 6 | unordered_map< char, int> freq; 7 | 8 | for (int i = 0; i < int (s.size()); i++) { 9 | freq[s[i]]++; 10 | } 11 | 12 | int maxFreq = INT_MIN; 13 | char maxFreqChar = '#'; 14 | 15 | for (auto i : freq) { 16 | if (i.second > maxFreq) { 17 | maxFreq = i.second; 18 | maxFreqChar = i.first; 19 | } 20 | } 21 | 22 | return maxFreqChar; 23 | } -------------------------------------------------------------------------------- /Solutions/Strings/Exercise_83.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int allAlphabetsPresentOrNot(string s) { 5 | 6 | int check[26] = {0}; 7 | 8 | for (int i = 0; i < int(s.size()); i++) { 9 | check[int(s[i]-'a')]++; 10 | } 11 | 12 | for (int i = 0; i < 26; i++) { 13 | if (check[i] == 0) { 14 | return 0; 15 | } 16 | } 17 | 18 | return 1; 19 | } -------------------------------------------------------------------------------- /Solutions/Strings/Exercise_84.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int canBeFormedOrNot(string s1, string s2) { 5 | int freq1[26] = {0}; 6 | int freq2[26] = {0}; 7 | 8 | for (int i = 0; i < int(s1.size()); i++) { 9 | freq1[int(s1[i]-'a')]++; 10 | } 11 | for (int i = 0; i < int(s2.size()); i++) { 12 | freq2[int(s2[i]-'a')]++; 13 | } 14 | 15 | for (int j = 0; j < 26; j++) { 16 | if (freq2[j] > freq1[j]) { 17 | return 0; 18 | } 19 | } 20 | 21 | return 1; 22 | } -------------------------------------------------------------------------------- /Solutions/Strings/Exercise_85.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sumOfElement(vector> matrix) { 5 | 6 | int sum = 0; 7 | 8 | for (int i = 0; i < matrix.size(); i++) { 9 | int check = 0; 10 | for (int j = 0; j < matrix[i].size(); j++) { 11 | if (i == j) { 12 | check = 1; 13 | } 14 | if (check == 0) { 15 | sum += matrix[i][j]; 16 | } 17 | } 18 | } 19 | return sum; 20 | } -------------------------------------------------------------------------------- /Solutions/Strings/Exercise_86.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | int romanToInt(string s) { 5 | int sol = 0, num = 0; 6 | for (int k = s.length()-1; k>=0; k--) { 7 | switch(s[k]) { 8 | case 'I': num = 1; break; 9 | case 'V': num = 5; break; 10 | case 'X': num = 10; break; 11 | case 'L': num = 50; break; 12 | case 'C': num = 100; break; 13 | case 'D': num = 500; break; 14 | case 'M': num = 1000; break; 15 | } 16 | if (4 * num < sol) sol -= num; 17 | else sol += num; 18 | } 19 | return sol; 20 | } -------------------------------------------------------------------------------- /Solutions/Strings/Exercise_87.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getNumberOfWords(string sentence) 5 | { 6 | int count=0; 7 | 8 | // char ch[]= new char[sentence.length()]; 9 | int n = sentence.size() ; 10 | char ch[n] ; 11 | for(int i=0; i0)&&(ch[i]!=' ')&&(ch[i-1]==' ')) || ((ch[0]!=' ')&&(i==0)) ) 15 | count++; 16 | } 17 | return count; 18 | } -------------------------------------------------------------------------------- /Solutions/Strings/Exercise_88.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | string longestCommonPrefix(vector &ar) { 5 | 6 | int n=ar.size() ; 7 | 8 | if (n == 0) 9 | return ""; 10 | 11 | if (n == 1) 12 | return ar[0]; 13 | 14 | sort(ar.begin(), ar.end()); 15 | 16 | 17 | int en = min(ar[0].size(), 18 | ar[n - 1].size()); 19 | 20 | 21 | string first = ar[0], last = ar[n - 1]; 22 | int i = 0; 23 | while (i < en && first[i] == last[i]) 24 | i++; 25 | 26 | string pre = first.substr(0, i); 27 | return pre; 28 | } -------------------------------------------------------------------------------- /Solutions/Strings/Exercise_89.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | string removeDuplicates(string s) { 5 | 6 | for(int i = 1; i < s.length();i++){ 7 | if(s[i] == s[i-1]){ 8 | s.erase(i-1,2); 9 | i= i-2 < 0 ? 0 : i - 2; 10 | } 11 | } 12 | return s; 13 | } -------------------------------------------------------------------------------- /Solutions/Strings/Exercise_90.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isSubSequence(string a, string b) { 5 | int j = 0, m = a.length(), n = b.length(); 6 | 7 | for (int i = 0; i < n && j < m; i++) 8 | if (a[j] == b[i]) 9 | j++; 10 | 11 | return (j == m); 12 | } -------------------------------------------------------------------------------- /Solutions/Strings/Exercise_91.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int binaryToDecimal(string s) { 5 | return stoi(s, 0, 2); 6 | } -------------------------------------------------------------------------------- /Solutions/VariablesDatatypes/Exercise_4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | int x,y; 6 | cin >>x>>y ; 7 | cout < 2 | #include 3 | using namespace std; 4 | 5 | void mainFn() { 6 | float a,b,c,d,e; 7 | cin >>a >> b >> c >> d >> e ; 8 | float t=a+b+c+d+e ; 9 | float ans=t/5; 10 | cout << fixed << setprecision(2) << ans ; 11 | } -------------------------------------------------------------------------------- /Solutions/VariablesDatatypes/Exercise_6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void mainFn() { 6 | float a; 7 | cin >> a ; 8 | cout << ceil(a) ; 9 | } -------------------------------------------------------------------------------- /Solutions/VariablesDatatypes/Exercise_7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void mainFn() { 6 | float a; 7 | cin >> a ; 8 | cout << floor(a) ; 9 | } -------------------------------------------------------------------------------- /Solutions/VariablesDatatypes/Exercise_8.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mainFn() { 5 | float p,r,t; 6 | cin >> p >> r >> t; 7 | int ans = (p*r*t)/100; 8 | cout << ans; 9 | } -------------------------------------------------------------------------------- /Solutions/Vectors/Exercise_92.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | void sortList(vector &nums) 5 | { 6 | // write your code here 7 | if (nums.size() == 0) return; 8 | 9 | sort(nums.begin(),nums.end()) ; 10 | for (int i : nums) 11 | cout << i << " "; 12 | } -------------------------------------------------------------------------------- /Solutions/Vectors/Exercise_93.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | void mergeSortedLists(vector &list1,vector &list2) 5 | { 6 | int index2=0 ; 7 | int size2=list2.size() ; 8 | int size1=list1.size() ; 9 | for (int index1 = 0; index2 < size2; index1++) { 10 | if (index1 == size1 || list1[index1] > list2[index2]) { 11 | list1.insert(list1.begin()+index1, list2[index2++]); 12 | } 13 | } 14 | for (int num : list1) cout << num << " "; 15 | } -------------------------------------------------------------------------------- /Solutions/Vectors/Exercise_94.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | void sortList(vector list){ 5 | if (list.size() == 0) return; 6 | 7 | sort(list.begin(),list.end()); 8 | for (auto element : list) 9 | cout << element << " "; 10 | } -------------------------------------------------------------------------------- /Solutions/Vectors/Exercise_95.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | void getMaximumInEachRow(vector> &nums) 5 | { 6 | if (nums.size() == 0) return; 7 | 8 | for (auto list: nums) 9 | { 10 | int maxe = -(int)(1e9); 11 | for (auto i : list) maxe = max(maxe, i); 12 | cout << maxe << " " ; 13 | } 14 | } -------------------------------------------------------------------------------- /Solutions/Vectors/Exercise_96.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void setZeroes(vector> matrix) { 5 | bool row = false, col = false; 6 | for(int i = 0; i < matrix.size(); i++){ 7 | for(int j = 0; j < matrix[0].size(); j++){ 8 | if(matrix[i][j] == 0) { 9 | if(i == 0) row = true; 10 | if(j == 0) col = true; 11 | matrix[0][j] = matrix[i][0] = 0; 12 | } 13 | } 14 | } 15 | for(int i = 1; i < matrix.size(); i++){ 16 | for(int j = 1; j < matrix[0].size(); j++){ 17 | if(matrix[i][0] == 0 || matrix[0][j] == 0) matrix[i][j] = 0; 18 | } 19 | } 20 | if(col){ 21 | for(int i = 0; i < matrix.size(); i++) matrix[i][0] = 0; 22 | } 23 | if(row){ 24 | for(int j = 0; j < matrix[0].size(); j++) matrix[0][j] = 0; 25 | } 26 | 27 | for (auto row : matrix) 28 | { 29 | for (auto element : row) 30 | cout << element << " "; 31 | } 32 | } -------------------------------------------------------------------------------- /Solutions/description: -------------------------------------------------------------------------------- 1 | This folder contains Solutions to all Coding Exercises. 2 | --------------------------------------------------------------------------------