diff --git a/BinaryNumberUsingQueue.cpp b/BinaryNumberUsingQueue.cpp new file mode 100644 index 0000000..dfa13c7 --- /dev/null +++ b/BinaryNumberUsingQueue.cpp @@ -0,0 +1,34 @@ +// CPP function to generate all binary number +// from 0 to given number n +#include +#include +using namespace std; + +// Maximum length of generated binary number +const int MAX = 100; + +// CPP function to generate all binary number +// for given number n +char binaryGenerator(int n) +{ + char a[MAX]; + + for (int i = 0; i <= n; i++) { + + // use define function itoa() to convert + // in given base + // a is char[] array where value store + // 2 is base, which convert. + itoa(i, a, 2); + + cout << a << endl; + } +} + +// Driven program +int main() +{ + int n = 10; + binaryGenerator(n); + return 0; +} diff --git a/Bubble_sort.cpp b/Bubble_sort.cpp new file mode 100644 index 0000000..f735886 --- /dev/null +++ b/Bubble_sort.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; +void bubble_sort(int arr[],int n){ + for(int round=1;roundarr[i+1]){ + swap(arr[i],arr[i+1]); + } + } + } +} +int main(){ + int n; cin>>n; + int arr[n]; + for(int i=0;i>arr[i]; + + bubble_sort(arr,n); + + for(int i=0;i + +using namespace std; + +void Insertionsort(int a[],int n) +{ + for(int i=1;i=0 && a[j]>key) + { + a[j+1]=a[j]; + j--; + } + a[j+1] = key; + } +} + +void BucketSort(int a[], int n, int k) +{ + int max = 0; + for (int i = 0; i < n; i++) + if (max < a[i]) + max = a[i]; + max++; + + int bucket[k][n]; + int size[k] = {0}; + + for (int i = 0; i < n; i++) + { + int index = k * a[i] / max; + bucket[index][size[index]] = a[i]; + size[index]++; + } + for (int i = 0; i < k; i++) + { + Insertionsort(bucket[i], size[i]); + } + for (int i = 0, j = 0; j < k; j++) + { + for (int l = 0; l < size[j]; l++) + a[i++] = bucket[j][l]; + } +} + +int main() +{ + int n; + cout << "\nEnter No of elements in the array : "; + cin >> n; + int arr[n]; + for (int i = 0; i < n; i++) //input + cin >> arr[i]; + BucketSort(arr, n, 5); + cout << "\nSorted Array : "; + for (int i = 0; i < n; i++) //output + cout << arr[i] << " "; +} \ No newline at end of file diff --git a/Counting Divisors(using Sieve).cpp b/Counting Divisors(using Sieve).cpp new file mode 100644 index 0000000..7cb3cd4 --- /dev/null +++ b/Counting Divisors(using Sieve).cpp @@ -0,0 +1,95 @@ +#include +using namespace std; + +#define ll long long + + + +//Seive Approach - Generating a array containing prime numbers +vector PrimeSieve(int *p, int n) { + + //first all odd numbers as prime as they can be potential prime + for (int i = 3; i <= 1000000; i += 2) { + p[i] = 1; + } + //Sieve + for (ll i = 3; i <= 1000000; i += 2) { + //if current number is not marked (it is prime) + if (p[i] == 1) { + //mark all the multiples of i as not prime + for (ll j = i * i; j <= 1000000; j = j + i) { + p[j] = 0; + } + } + } + //Special cases + p[2] = 1; + p[1] = p[0] = 0; + + vector primes; + primes.push_back(2); + + for (int i = 3; i <= n; i += 2) { + if (p[i] == 1) + primes.push_back(i); + + } + + return primes; + + +} + +int no_of_divisors(int m, vector &primes) { + + int i = 0; + int p = primes[0]; + int ans = 1; + + while (p * p <= m) { + + if (m % p == 0) { + int count = 0; + + while (m % p == 0) { + count++; + m = m / p; + } + ans = ans * (count + 1); + } + // go to next position + i++; + p = primes[i]; + } + +//if m is not reduced to 1 , it means m also a prime number + if (m != 1) { + ans = ans * 2; + } + return ans; + +} + + + + +int main() { + + + int p[1000000] = {0}; + vector primes = PrimeSieve(p, 100000); + + int t; cin >> t; + while (t--) { + int no; cin >> no; + int divs = no_of_divisors(no, primes); + cout << divs << endl; + + + + } + + return 0; + + +} \ No newline at end of file diff --git a/Counting Sort.cpp b/Counting Sort.cpp new file mode 100644 index 0000000..db721ca --- /dev/null +++ b/Counting Sort.cpp @@ -0,0 +1,36 @@ +#include +using namespace std; + +void Countsort(int a[], int n) +{ + int k = 0; //max element + for (int i = 0; i < n; i++) + if (a[i] > k) + k = a[i]; + int count[k+1] = {0}, temp[n]; + for (int i = 0; i < n; i++) + count[a[i]]++; + for (int i = 1; i <= k; i++) + count[i] = count[i - 1] + count[i]; + for (int i = n - 1, j = k - 1; i >= 0; i--) + { + temp[count[a[i]]-1] = a[i]; + count[a[i]]--; + } + for (int i = 0; i < n; i++) + a[i] = temp[i]; +} + +int main() +{ + int n; + cout << "\nEnter No of elements in the array : "; + cin >> n; + int arr[n]; + for (int i = 0; i < n; i++)//input + cin >> arr[i]; + Countsort(arr, n); + cout << "\nSorted Array : "; + for (int i = 0; i < n; i++)//output + cout << arr[i] << " "; +} diff --git a/Factorial of BIG INTEGER.cpp b/Factorial of BIG INTEGER.cpp new file mode 100644 index 0000000..7d94175 --- /dev/null +++ b/Factorial of BIG INTEGER.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; +int main() +{ + int n; + cin>>n; + + int q=2; + int arr[100000] = {0}; + arr[0] = 1; + int len = 1; + int x = 0; + int num = 0; + while(q<=n) + { + x=0; + num =0; + while(x=0) + { + cout< +using namespace std; + +int gcd(int a, int b) { + + return b == 0 ? a : gcd(b, a % b); +} + + + +int main() { + int n1, n2; + cin >> n1 >> n2; + + cout << gcd(n1, n2) << endl; +} \ No newline at end of file diff --git a/Insertion Sort.cpp b/Insertion Sort.cpp new file mode 100644 index 0000000..f8abd48 --- /dev/null +++ b/Insertion Sort.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; +void insertion_sort(int [], int ); + +int main() { + int n; cin >> n; + int arr[n]; + for (int i = 0; i < n; i++) { + cin >> arr[i]; + } + + insertion_sort(arr, n); + for (int f = 0; f < n; f++) cout << arr[f] << " "; + +} + + +void insertion_sort(int arr[], int n) { + int i, j, temp; + for (i = 1; i < n; i++) { + temp = arr[i]; + for (j = i - 1; j >= 0 && temp < arr[j]; j--) + { arr[j + 1] = arr[j]; } + arr[j + 1] = temp; + } +} diff --git a/LCS_using_DP.txt b/LCS_using_DP.txt new file mode 100644 index 0000000..ac2b8bf --- /dev/null +++ b/LCS_using_DP.txt @@ -0,0 +1,202 @@ +#include +#include +using namespace std; + + +#define f(i,n) for(int i=0;i +#define vi vector +#define mii map +#define pqb priority_queue +#define pqs priority_queue > +#define setbits(x) __builtin_popcountll(x) +#define zrobits(x) __builtin_ctzll(x) +#define mod 1000000007 +#define inf 1e18 +#define ps(x,y) fixed<>x; while(x--) +#define all(x) x.begin(),x.end() +#define rt return +#define br break +#define ct continue +#define elif else if +#define arrin(a,n) for(int i=0;i>a[i] +#define arrout(a,n) for(int i=0;i>= 1; + //cout<> a, vector> b) { +// return a.second < b.second; +// } + + +int getMinDifferenceSubsetSumArrayPartition(int arr[], int n) { + if (n == 0) { + return -1; + } + + int sumOfArray = 0; + for (int i = 0; i < n; i++) { + sumOfArray = sumOfArray + arr[i]; + } + + int sum = sumOfArray / 2; + + // bool[][] mat = new boolean[n][sum + 1]; + bool mat[n][sum + 1]; + + + for (int i = 0; i < n; i++) { + mat[i][0] = true; + } + + for (int j = 0; j <= sum; j++) { + if (j == arr[0]) { + mat[0][j] = true; + } + } + + for (int i = 1; i < n; i++) { + for (int j = 1; j <= sum; j++) { + + if (mat[i - 1][j]) { + mat[i][j] = true; + } else { + if (j >= arr[i]) { + mat[i][j] = mat[i - 1][j - arr[i]]; + } + } + } + } + + int lastRow = n - 1; + int firstPartitionSum = -1; + + for (int j = sum; j >= 0; j--) { + if (mat[lastRow][j]) { + firstPartitionSum = j; + break; + } + } + + int secondPartitionSum = sumOfArray - firstPartitionSum; + + return abs(firstPartitionSum - secondPartitionSum); + +} + + + + + +int32_t main() +{ + //c_p_c(); + string a, b; cin >> a >> b; + int m = a.length(); + int n = b.length(); + int arr[m + 1][n + 1]; + f(i, m+1) { + arr[i][0] = 0; + } + f(i, n+1) { + arr[0][i] = 0; + } + a.insert(0, "0"); + b.insert(0, "0"); + + string ans=""; + + for (int i = 1; i <= m; i++) { + for (int j = 1; j <= n; j++) { + + if (a.at(i) == b.at(j)) { + arr[i][j] = arr[i - 1][j - 1] + 1; + // ans += a.at(i); + } + + else { + arr[i][j] = max(arr[i - 1][j], arr[i][j - 1]); + } + + + + } + } + + + + int len= arr[m][n]; + int x=n; + int y=m; + + while(x>0 and y>0){ + if(a.at(y)==b.at(x)) + { + + char sub=a.at(y); + ans+=sub; + y--; + x--; + } + else if(arr[y-1][x]>arr[y][x-1]){ + y--; + } + else{ + x--; + } + } + reverse(ans.begin(),ans.end()); + cout< +using namespace std; + +#define ll long long + + + +//Seive Approach - Generating a array containing prime numbers +vector PrimeSieve(int *p, int n) { + + //first all odd numbers as prime as they can be potential prime + for (int i = 3; i <= 1000000; i += 2) { + p[i] = 1; + } + //Sieve + for (ll i = 3; i <= 1000000; i += 2) { + //if current number is not marked (it is prime) + if (p[i] == 1) { + //mark all the multiples of i as not prime + for (ll j = i * i; j <= 1000000; j = j + i) { + p[j] = 0; + } + } + } + //Special cases + p[2] = 1; + p[1] = p[0] = 0; + + vector primes; + primes.push_back(2); + + for (int i = 3; i <= n; i += 2) { + if (p[i] == 1) + primes.push_back(i); + + } + + return primes; + + +} + +int no_of_divisors(int m, vector &primes) { + + int i = 0; + int p = primes[0]; + int ans = 1; + + while (p * p <= m) { + + if (m % p == 0) { + int count = 0; + + while (m % p == 0) { + count++; + m = m / p; + } + ans = ans * (count + 1); + } + // go to next position + i++; + p = primes[i]; + } + +//if m is not reduced to 1 , it means m also a prime number + if (m != 1) { + ans = ans * 2; + } + return ans; + +} + + + + +int main() { + + + int p[1000000] = {0}; + vector primes = PrimeSieve(p, 100000); + + int t; cin >> t; + while (t--) { + int no; cin >> no; + int divs = no_of_divisors(no, primes); + cout << divs << endl; + + + + } + + return 0; + + +} \ No newline at end of file diff --git a/PrintingAllSubsetsOfString(Recursion).cpp b/PrintingAllSubsetsOfString(Recursion).cpp new file mode 100644 index 0000000..e67fe20 --- /dev/null +++ b/PrintingAllSubsetsOfString(Recursion).cpp @@ -0,0 +1,35 @@ +// CPP program to generate power set +#include +using namespace std; + +// str : Stores input string +// curr : Stores current subset +// index : Index in current subset, curr +void powerSet(string str, int index = 0, + string curr = "") +{ + int n = str.length(); + + // base case + if (index == n) { + cout << curr << endl; + return; + } + + // Two cases for every character + // (i) We consider the character + // as part of current subset + // (ii) We do not consider current + // character as part of current + // subset + powerSet(str, index + 1, curr + str[index]); + powerSet(str, index + 1, curr); +} + +// Driver code +int main() +{ + string str = "abc"; + powerSet(str); + return 0; +} \ No newline at end of file diff --git a/Queue using 2 stacks - Hackerrank.cpp b/Queue using 2 stacks - Hackerrank.cpp new file mode 100644 index 0000000..5354e1c --- /dev/null +++ b/Queue using 2 stacks - Hackerrank.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int main() { + int t; cin>>t; + stack s1; + stack s2; + +while(t--) + { + int num; + cin>>num; + + switch(num) + { + case 1: + { + int insert; + cin>>insert; + s1.push(insert); + break; + } + case 2: + { + if(s2.empty()) + { + while(!s1.empty()) + { + s2.push(s1.top()); + s1.pop(); + } + } + + s2.pop(); + break; + } + case 3: + { + + if(!s2.empty()) + { + cout< +using namespace std; + +int partition(int *a,int s,int e){ + // Inplace (cant take extra array) + int i=s-1; + int j=s; + + int pivot=a[e]; + + for(;j<=e-1;j++){ + if(a[j]<=pivot){ + i++; + swap(a[i],a[j]); + } + + + } + swap(a[i+1],a[e]); + + return i+1; +} + +void quick_sort(int *a,int s,int e){ + // base case + if(s>=e) + return ; + + // recursive case + int p=partition(a,s,e); + // left part + quick_sort(a,s,p-1); + // right part + quick_sort(a,p+1,e); +} + + +int main(){ + int n; cin>>n; + int a[n]; + for(int i=0;i>a[i]; + + quick_sort(a,0,n-1); + + for(int i=0;i +#include +using namespace std; + +void Countsort(int a[], int n, int e) +{ + int k=9;//0-9 + // int k = 0; //max element + // for (int i = 0; i < n; i++) + // if (a[i] > k) + // k = a[i]; + int count[k+1] = {0}, temp[n]; + for (int i = 0; i < n; i++) + count[(a[i]/e)%10]++; + for (int i = 1; i <= k; i++) + count[i] = count[i - 1] + count[i]; + for (int i = n - 1, j = k - 1; i >= 0; i--) + { + temp[count[(a[i]/e)%10]-1] = a[i]; + count[(a[i]/e)%10]--; + } + for (int i = 0; i < n; i++) + a[i] = temp[i]; +} + +void Radixsort(int a[], int n) +{ + int m = 0; //max element + for (int i = 0; i < n; i++) + if (a[i] > m) + m = a[i]; + int d = log10(m); // no. of max digits + for(int i=0;i> n; + int arr[n]; + for (int i = 0; i < n; i++)//input + cin >> arr[i]; + Radixsort(arr, n); + cout << "\nSorted Array : "; + for (int i = 0; i < n; i++)//output + cout << arr[i] << " "; +} diff --git a/Randomized Select.cpp b/Randomized Select.cpp new file mode 100644 index 0000000..b7379f5 --- /dev/null +++ b/Randomized Select.cpp @@ -0,0 +1,91 @@ +// determines the kth smallest element of an input array using median of median method +#include +#include +using namespace std; + +int findMedian(int arr[], int n) +{ + sort(arr, arr + n); // Sort the array + return arr[n / 2]; // Return middle element +} + +void swapp(int &a, int &b) +{ + int temp = a; + a = b; + b = temp; +} + +int partition(int a[], int f, int l, int r) +{ + for (int y = f; y <= l; y++) //to find index of r + if (a[y] == r) + { + r = y; + break; + } + swapp(a[l], a[r]); + + int pivot = a[l]; + int i = f - 1; + + for (int j = f; j < l; j++) + { + if (a[j] <= pivot) + swapp(a[++i], a[j]); + } + swapp(a[++i], a[l]); + return i; +} + +// low high +int Rselect(int a[], int l, int h, int k) // determines the kth smallest element of an input array +{ + int n = h - l + 1; //size of array + + if (k > 0 && k <= n) + { + int i, median[(n + 4) / 5]; //(n+4)/5 returns ceiling(n/5); + + for (i = 0; i < n / 5; i++) + { + median[i] = findMedian(a + l + i * 5, 5); + } + + if (i * 5 < n) + { + median[i] = findMedian(a + l + i * 5, n % 5); + } + + int medOfmed = (i == 0) ? median[i] : Rselect(median, 0, i, (i + 1) / 2); + + int p = partition(a, l, h, medOfmed); + + if (p - l == k - 1) + return a[p]; + else if (p - l > k - 1) + return Rselect(a, l, p - 1, k); + else + return Rselect(a, p + 1, h, k - (p - l + 1)); + } + + return INT32_MAX; +} + +int main() +{ + int n, k; + cout << "\nEnter No of elements in the array : "; + cin >> n; + int arr[n]; + for (int i = 0; i < n; i++) //input + cin >> arr[i]; + while (1) + { + cout << "\nEnter the order statistic : "; + cin >> k; + cout << '\n' + << "The " << k << " order statistic is " + << Rselect(arr, 0, n - 1, k); + } +} \ No newline at end of file diff --git a/Recursively_Reverse a Linked List.cpp b/Recursively_Reverse a Linked List.cpp new file mode 100644 index 0000000..f35e408 --- /dev/null +++ b/Recursively_Reverse a Linked List.cpp @@ -0,0 +1,53 @@ +#include +using namespace std; + +node* recReverse(node*head) { + // Time Complexity O(N2) + // Space Complexity O(N) for recursion stack + + + + + // smallest linked list + if (head->next == NULL or head == NULL) { + return head; + } + + // recursive case + node* shead = recReverse(head->next); + + node* temp = shead; + while (temp->next != NULL) { + temp = temp->next; + } + + head->next = NULL; + temp->next = head; + // retuning the head of the reversed linked list + return shead; + +} + +node* recReverse_optimised(node*head) { + // Time Complexity O(N) + // Space Complexity O(N) for recursion stack + + + + + // smallest linked list + if (head->next == NULL or head == NULL) { + return head; + } + + // recursive case + node* shead = recReverse(head->next); + + node* temp = head->next; + head->next->next = head; + head->next = NULL; + + // retuning the head of the reversed linked list + return shead; + +} diff --git a/Rod_Cutting using Recursion.cpp b/Rod_Cutting using Recursion.cpp new file mode 100644 index 0000000..bb45ebf --- /dev/null +++ b/Rod_Cutting using Recursion.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +int rodCutting(int n, int a, int b, int c) +{ + if(n == 0) + return 0; + if(n < 0) + return -1; + int res = max(max(rodCutting(n-a, a, b, c), rodCutting(n-b, a, b, c)), + rodCutting(n-c, a, b, c)); + + if(res== -1) + return -1; + return 1+res; +} + +int main() +{ + int n = 25; + int a = 11, b = 12, c = 13; + cout << rodCutting(n, a, b, c); +} \ No newline at end of file diff --git a/RotateArrayInO(n).cpp b/RotateArrayInO(n).cpp new file mode 100644 index 0000000..9eeb22d --- /dev/null +++ b/RotateArrayInO(n).cpp @@ -0,0 +1,81 @@ +// { Driver Code Starts +#include +using namespace std; + + + // } Driver Code Ends + + + +void rotateArr(int arr[], int d, int n){ + + + // HINT -> + // reverse(a, a+d) Reverse array from beginning till D + // reverse(a+d, a+n) Reverse array from D till N + // reverse(a, a+n) Reverse the whole array + + int l=0; + int r=d-1; + while(l> t; + + while(t--){ + int n, d; + cin >> n >> d; + + int arr[n]; + + for(int i = 0; i < n; i++){ + cin >> arr[i]; + } + + rotateArr(arr, d,n); + + for(int i =0;i +#include +#define int long long +using namespace std; + +int min(int A[],int k,int N){ + int j,LOC,MIN; + MIN=A[k]; + LOC=k; + for(j=k+1;j<=N;j++) + if(MIN>A[j]) + { MIN=A[j]; + LOC=j; + } + + return (LOC); +} + +int32_t main(){ + int n; cin>>n; + int A[n]; + for(int i=0;i>A[i]; + int LOC,k,temp; + + for(k=0;k +#include +using namespace std; + +int profit(int wines[],int i,int j,int y){ + // Base Case + if(i>j) + return 0; + + // Recursive Case + int opt1= wines[i]*y + profit(wines,i+1,j,y+1); + int opt2= wines[j]*y + profit(wines,i,j-1,y+1); + return max(opt1,opt2); + +} + + +int main(){ + + int wines[]={2,3}; + int n =sizeof(wines)/sizeof(int); + int y=1; + cout< +#include +using namespace std; + + +int main() { + + int arr[] = {1, 10, 11, 9, 100}; + int n = sizeof(arr) / sizeof(int); + //Search --> find + + int key ; + cin >> key; + auto it = find(arr, arr + n, key); + int index = it - arr; + if (index == n) { + cout << key << " not present"; + } + else { + cout << "Present at index " << index; + } + + + return 0; +} diff --git a/algo_stl_02_binary_search_lower_upper.cpp b/algo_stl_02_binary_search_lower_upper.cpp new file mode 100644 index 0000000..60e9230 --- /dev/null +++ b/algo_stl_02_binary_search_lower_upper.cpp @@ -0,0 +1,37 @@ +#include +#include +using namespace std; + + +int main() { + + int arr[] = {20, 30, 40, 40, 40, 50, 100, 1100}; + int n = sizeof(arr) / sizeof(int); + //Search in a sorted array + int key ; + cin >> key; + + bool present = binary_search(arr, arr + n, key); //logN + if (present) { + cout << "Present"; + } + else { + cout << "Absent!"; + } + + //Two more things + // Get the index of the element + // lower_bound(s,e,key) and upper_bound(s,e,key) + + auto lb = lower_bound(arr, arr + n, 41); + cout << endl << "Lower bound of 40 is" << (lb - arr) << endl; + + + //uppper bound method + auto ub = upper_bound(arr, arr + n, 40); + cout << endl << "Upper bound of 40 is" << (ub - arr) << endl; + + cout << "Occ Freq of 40 is " << (ub - lb) << endl; + + return 0; +} diff --git a/algo_stl_06_next_permuation.cpp b/algo_stl_06_next_permuation.cpp new file mode 100644 index 0000000..fa551ab --- /dev/null +++ b/algo_stl_06_next_permuation.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +using namespace std; +int main() { + + int arr[] = {10, 20, 30, 40, 50}; + int n = sizeof(arr) / sizeof(int); + rotate(arr, arr + 2, arr + n); + + for (int i = 0; i < n; i++) { + cout << arr[i] << " "; + } + cout << endl; + //Apply the same on a vector + vector v{1, 2, 3}; + + //rotate(v.begin(),v.begin()+3,v.end()); + + for (int i = 0; i < v.size(); i++) { + cout << v[i] << " "; + } + + //Next_permuation + next_permutation(v.begin(), v.end()); + next_permutation(v.begin(), v.end()); + cout << endl; + //for each loop + for (int x : v) { + cout << x << " "; + } + + + + + return 0; +} diff --git a/algo_stl_money_change_problem.cpp b/algo_stl_money_change_problem.cpp new file mode 100644 index 0000000..50a9dd0 --- /dev/null +++ b/algo_stl_money_change_problem.cpp @@ -0,0 +1,32 @@ +#include +#include +using namespace std; + +int main() { + + int a = 10; + int b = 20; + + swap(a, b); + cout << a << " and " << b << endl; + + cout << max(a, b) << endl; + cout << min(a, b) << endl; + + int arr[] = {1, 2, 3, 4, 4, 5}; + reverse(arr, arr + 4); + + swap(arr[0], arr[1]); + + int n = sizeof(arr) / sizeof(int); + for (int i = 0; i < n; i++) { + cout << arr[i] << " "; + } + next_permutation(arr, arr + n); + cout << endl; + for (int i = 0; i < n; i++) { + cout << arr[i] << " "; + } + + return 0; +} diff --git a/money.cpp b/money.cpp new file mode 100644 index 0000000..2f7b17d --- /dev/null +++ b/money.cpp @@ -0,0 +1,44 @@ +#include +#include +using namespace std; + + + +int indian_currency[] = {1, 2, 5, 10, 20, 50, 100, 200, 500, 2000}; +int n = sizeof(indian_currency) / sizeof(int); + +bool compare(int a, int b) { + return a <= b; +} + +void count_notes(int money) { + + // Base Case + if (money == 0) { + return; + } + //Recursive Case + // lower_bound will return an iterator + // Log (T) where T is the type of coins you have! + + int largest_idx = lower_bound(indian_currency, indian_currency + n, money, compare) - indian_currency - 1; + int m = indian_currency[largest_idx]; + cout << m << " " << endl; + + //Recursive Call + count_notes(money - m); + + return; + +} + +int main() { + + int money; + cin >> money; + + count_notes(money); + + + return 0 ; +} \ No newline at end of file diff --git a/other_methods.cpp b/other_methods.cpp new file mode 100644 index 0000000..50a9dd0 --- /dev/null +++ b/other_methods.cpp @@ -0,0 +1,32 @@ +#include +#include +using namespace std; + +int main() { + + int a = 10; + int b = 20; + + swap(a, b); + cout << a << " and " << b << endl; + + cout << max(a, b) << endl; + cout << min(a, b) << endl; + + int arr[] = {1, 2, 3, 4, 4, 5}; + reverse(arr, arr + 4); + + swap(arr[0], arr[1]); + + int n = sizeof(arr) / sizeof(int); + for (int i = 0; i < n; i++) { + cout << arr[i] << " "; + } + next_permutation(arr, arr + n); + cout << endl; + for (int i = 0; i < n; i++) { + cout << arr[i] << " "; + } + + return 0; +} diff --git a/pair_stl.cpp b/pair_stl.cpp new file mode 100644 index 0000000..a4cb37e --- /dev/null +++ b/pair_stl.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; + + +int main(){ + + + //Pair + pair p; + p.first = 10; + p.second = 'B'; + + //Another way + pair p2(p); + + cout<< p2.first< p3 = make_pair(100,"Audi"); + cout<>a>>b; + + pair p4 = make_pair(a,b); + //Array of Pairs + //Vector of Pairs + pair, string> car; + car.second = "Audi"; + car.first.first = 10; + car.first.second = 20; + + cout< +#include +#include +#include +#include +using namespace std; +// Use of comparator +//comparing function only sorts if string size is equal and keeps the larger integgers at last. +bool myfunction (string i,string j) +{ + int n=i.length(); + int m=j.length(); + if(n==m) + return (i>n; + vector arr(n); + for(int i=0;i>arr[i]; + + + sort(arr.begin(),arr.end(),myfunction); + + for(int i=0;i