├── All paths from root to leaf.txt ├── All possible permutations.txt ├── ArmstrongNumber.cpp ├── Boundary_Traversal_BST.cpp ├── Buy and Sell Stock – II.java ├── Buy and sell stocks - || ├── Combination sum.txt ├── Count BST nodes that lie in a given range ├── Delete and earn ├── EggDrop standard problem soln ├── FakePlasticTreesDpCF.cpp ├── Fibonacci Iterative ├── InsertionSort.cpp ├── Kth bit in binary string ├── LargestRectangle.cpp ├── LeftViewofBinaryTree.py ├── Lobb_Number.cpp ├── Longest_common_increasing_subsequence(LCIS)_2000_RATED_ON_CF.cpp ├── Make_equal_by_deleting.cpp ├── MaximumSumOfProductsCF.cpp ├── Min Cost Path.cpp ├── Minimum path sum ├── Non_Adjacent_Maximum_Sum.cpp ├── Non_adjacent_combination_sum_check.cpp ├── Number of subsequences with given sum.txt ├── Palindrome linked list ├── Palindrome-Partitioning - II.txt ├── Parsing a boolean expression ├── Partition Equal Subset Sum.cpp ├── PartitionSet.cpp ├── PolyAdd.cpp ├── Print all subsequences.txt ├── Print all substring of a string.txt ├── README.md ├── Rod cutting DP ├── Segment tree template ├── Segment_tree.cpp ├── Space optimized solution of lcs ├── Subsequence sum equals to K (Bool).txt ├── Subsequence sum equals to K.txt ├── Subset_sum.cpp ├── TowerOfHanoi.cpp ├── Triangle ├── Unbounded Knapsack.txt ├── Unique paths ├── WildcardMatching.java ├── bubbleSort.cpp ├── dice_rolls_k_faces_target_sum.cpp ├── editdistance.txt ├── longest_palindrome_subsequence.cpp ├── matrix chain multiplocation.txt ├── merge.cpp ├── no_subset_of_given_difference.cpp ├── reverseofanumber.java ├── sorted_array.cpp ├── sqrt_decoposition.cpp └── uniquePathsIII.cpp /All paths from root to leaf.txt: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | vectorv; 5 | void recur(TreeNode* &root,string s) 6 | { 7 | 8 | if (root!=NULL) s+=to_string(root->val); 9 | if (root->left==NULL&&root->right==NULL) 10 | {v.push_back(s); return;} 11 | if (root->left!=NULL||root->right!=NULL) s+="->"; 12 | if (root->left) recur(root->left,s); 13 | if (root->right) recur(root->right,s); 14 | } 15 | 16 | vector binaryTreePaths(TreeNode* root) { 17 | string s=""; 18 | recur(root,s); 19 | return v; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /All possible permutations.txt: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | void recur(vector nums,vectords,vector>&v,mapmpp) 5 | { 6 | if (ds.size()==nums.size()) 7 | { 8 | v.push_back(ds); 9 | return; 10 | } 11 | for(int i=0;i> permute(vector& nums) { 25 | mapmpp; 26 | vectords; 27 | vector>v; 28 | 29 | recur(nums,ds,v,mpp); 30 | return v; 31 | } 32 | }; -------------------------------------------------------------------------------- /ArmstrongNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int num, originalNum, remainder, result = 0; 6 | cout << "Enter a three-digit integer: "; 7 | cin >> num; 8 | originalNum = num; 9 | 10 | while (originalNum != 0) { 11 | // remainder contains the last digit 12 | remainder = originalNum % 10; 13 | 14 | result += remainder * remainder * remainder; 15 | 16 | // removing last digit from the orignal number 17 | originalNum /= 10; 18 | } 19 | 20 | if (result == num) 21 | cout << num << " is an Armstrong number."; 22 | else 23 | cout << num << " is not an Armstrong number."; 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Boundary_Traversal_BST.cpp: -------------------------------------------------------------------------------- 1 | struct Node 2 | { 3 | int data; 4 | Node* left, * right; 5 | }; 6 | 7 | class Solution { 8 | public: 9 | bool isLeaf(Node *root) 10 | { 11 | return !root->left && !root->right; 12 | } 13 | void addLeftBoundary(Node *root, vector &res) 14 | { 15 | Node *curr = root->left; 16 | while (curr) 17 | { 18 | if (!isLeaf(curr)) 19 | res.push_back(curr->data); 20 | if (curr->left) 21 | curr = curr->left; 22 | else 23 | curr = curr->right; 24 | } 25 | } 26 | 27 | void addRightBoundary(Node *root, vector &res) 28 | { 29 | Node *curr = root->right; 30 | vector temp; 31 | while (curr) 32 | { 33 | if (!isLeaf(curr)) 34 | temp.push_back(curr->data); 35 | if (curr->right) 36 | curr = curr->right; 37 | else 38 | curr = curr->left; 39 | } 40 | 41 | for (int i = temp.size() - 1; i >= 0; i--) 42 | { 43 | res.push_back(temp[i]); 44 | } 45 | } 46 | 47 | void addLeaves(Node *root, vector &res) 48 | { 49 | if (isLeaf(root)) 50 | { 51 | res.push_back(root->data); 52 | return; 53 | } 54 | if (root->left) 55 | addLeaves(root->left, res); 56 | if (root->right) 57 | addLeaves(root->right, res); 58 | } 59 | vector boundary(Node *root) 60 | { 61 | //Your code here 62 | vector res; 63 | if (!root) 64 | return res; 65 | if (!isLeaf(root)) 66 | res.push_back(root->data); 67 | addLeftBoundary(root, res); 68 | addLeaves(root, res); 69 | addRightBoundary(root, res); 70 | 71 | return res; 72 | } 73 | }; 74 | -------------------------------------------------------------------------------- /Buy and Sell Stock – II.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class stock{ 4 | static long getMaximumProfit(long Arr[], int n) 5 | { 6 | //Write your code here 7 | 8 | long dp[][]=new long[n+1][2]; 9 | for(long row[]: dp) 10 | Arrays.fill(row,-1); 11 | 12 | //base condition 13 | dp[n][0] = dp[n][1] = 0; 14 | 15 | long profit=0; 16 | 17 | for(int ind= n-1; ind>=0; ind--){ 18 | for(int buy=0; buy<=1; buy++){ 19 | if(buy==0){// We can buy the stock 20 | profit = Math.max(0+dp[ind+1][0], -Arr[ind] + dp[ind+1][1]); 21 | } 22 | 23 | if(buy==1){// We can sell the stock 24 | profit = Math.max(0+dp[ind+1][1], Arr[ind] + dp[ind+1][0]); 25 | } 26 | 27 | dp[ind][buy] = profit; 28 | } 29 | } 30 | return dp[0][0]; 31 | } 32 | 33 | public static void main(String args[]) { 34 | 35 | int n =6; 36 | long Arr[] = {7,1,5,3,6,4}; 37 | 38 | System.out.println("The maximum profit that can be generated is "+getMaximumProfit(Arr, n)); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Buy and sell stocks - ||: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long int dp[30004][2]; 4 | int rec(int i,int store,vector&nums) 5 | { 6 | if (i>=nums.size()) return 0; 7 | int buy=0,sell=0,nien=0; 8 | if(dp[i][store] != -1 ) return dp[i][store]; 9 | if (store) 10 | { 11 | nien = rec(i+1,1,nums); 12 | sell = (-nums[i]) + rec(i+1,0,nums); 13 | } 14 | else 15 | { 16 | nien = rec(i+1,0,nums); 17 | buy = nums[i]+rec(i+1,1,nums); 18 | } 19 | // cout<& prices) { 23 | memset(dp,-1,sizeof(dp)); 24 | // vector>dp(prices.size(),vector(2,-1)); 25 | return rec(0,1,prices); 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Combination sum.txt: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | set>s; 4 | void recur(vector& c,vector&ds,int i, int tar) 5 | { 6 | if (i>=c.size()) 7 | { 8 | if (tar==0) s.insert(ds); return; 9 | } 10 | // pick up condition 11 | if (c[i]<=tar) 12 | { 13 | ds.push_back(c[i]); 14 | recur(c,ds,i,tar-c[i]); 15 | ds.pop_back(); 16 | } 17 | // har element ko tab tak lena h jab tak tar 0 na ho jaye 18 | recur(c,ds,i+1,tar); 19 | } 20 | vector> combinationSum(vector& candidates, int tar) { 21 | vectorsp; 22 | vector> vop; 23 | recur(candidates,sp,0,tar); 24 | for(auto i:s) 25 | { 26 | vop.push_back(i); 27 | } 28 | return vop; 29 | } 30 | }; -------------------------------------------------------------------------------- /Count BST nodes that lie in a given range: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node *left; 9 | struct Node *right; 10 | }; 11 | // Utility function to create a new Tree Node 12 | Node* newNode(int val) 13 | { 14 | Node* temp = new Node; 15 | temp->data = val; 16 | temp->left = NULL; 17 | temp->right = NULL; 18 | 19 | return temp; 20 | } 21 | // Function to Build Tree 22 | Node* buildTree(string str) 23 | { 24 | // Corner Case 25 | if (str.length() == 0 || str[0] == 'N') 26 | return NULL; 27 | 28 | // Creating vector of strings from input 29 | // string after spliting by space 30 | vector ip; 31 | 32 | istringstream iss(str); 33 | for (string str; iss >> str; ) 34 | ip.push_back(str); 35 | 36 | // Create the root of the tree 37 | Node* root = newNode(stoi(ip[0])); 38 | 39 | // Push the root to the queue 40 | queue queue; 41 | queue.push(root); 42 | 43 | // Starting from the second element 44 | int i = 1; 45 | while (!queue.empty() && i < ip.size()) { 46 | 47 | // Get and remove the front of the queue 48 | Node* currNode = queue.front(); 49 | queue.pop(); 50 | 51 | // Get the current node's value from the string 52 | string currVal = ip[i]; 53 | 54 | // If the left child is not null 55 | if (currVal != "N") { 56 | 57 | // Create the left child for the current node 58 | currNode->left = newNode(stoi(currVal)); 59 | 60 | // Push it to the queue 61 | queue.push(currNode->left); 62 | } 63 | 64 | // For the right child 65 | i++; 66 | if (i >= ip.size()) 67 | break; 68 | currVal = ip[i]; 69 | 70 | // If the right child is not null 71 | if (currVal != "N") { 72 | 73 | // Create the right child for the current node 74 | currNode->right = newNode(stoi(currVal)); 75 | 76 | // Push it to the queue 77 | queue.push(currNode->right); 78 | } 79 | i++; 80 | } 81 | 82 | return root; 83 | } 84 | 85 | 86 | // } Driver Code Ends 87 | class Solution{ 88 | public: 89 | int getCount(Node *root, int l, int h) 90 | { 91 | // your code goes here 92 | int c = 0; 93 | if(root-> left != NULL) 94 | c += getCount(root->left , l, h); 95 | if((root->data > l && root->data < h) ||( (root->data == l) || root->data == h) ) 96 | c++; 97 | if(root-> right != NULL) 98 | c += getCount(root->right , l, h); 99 | return c; 100 | } 101 | }; 102 | 103 | 104 | //{ Driver Code Starts. 105 | 106 | int main() 107 | { 108 | 109 | int t; 110 | scanf("%d ", &t); 111 | while (t--) 112 | { 113 | string s; 114 | getline(cin >> ws, s); 115 | int l, r; 116 | cin >> l >> r; 117 | Solution ob; 118 | Node* root = buildTree(s); 119 | cout << ob.getCount(root, l, r) << endl; 120 | 121 | } 122 | return 1; 123 | } 124 | 125 | // } Driver Code Ends 126 | -------------------------------------------------------------------------------- /Delete and earn: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | //Good Problem you have to just think if you have to take this one or next one forget about what happened earlier . 5 | 6 | int dp[500001]; 7 | int rec(int ind,vector&n) 8 | { 9 | if (ind>=n.size()) return 0; 10 | if (dp[ind]!=-1) return dp[ind]; 11 | int pick = (n[ind]) + rec(ind+2,n); 12 | int ntpick = rec(ind+1,n); 13 | return dp[ind] = max(pick,ntpick); 14 | } 15 | int deleteAndEarn(vector& nums) 16 | { 17 | memset(dp,-1,sizeof(dp)); 18 | vector sums(10001, 0); 19 | sort(nums.begin(), nums.end()); 20 | 21 | for(int i=0; i 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | { 8 | public: 9 | //Function to find minimum number of attempts needed in 10 | //order to find the critical floor. 11 | 12 | int dp[205][205]; 13 | 14 | int f(int n , int k) 15 | { 16 | // your code here 17 | if(n == 1 || n == 0) 18 | return k; 19 | 20 | if(k == 0 || k == 1) 21 | return k; 22 | 23 | 24 | if(dp[n][k] != -1) return dp[n][k]; 25 | 26 | int ans =INT_MAX; 27 | 28 | for(int i = 1; i <= k; i++) 29 | { 30 | 31 | int temp = 1 + max( f(n-1,i-1),f(n,k-i)); 32 | ans = min(ans , temp) ; 33 | } 34 | 35 | return dp[n][k] = ans; 36 | } 37 | 38 | 39 | int eggDrop(int n, int k) 40 | { 41 | 42 | memset(dp,-1,sizeof(dp)); 43 | return f(n,k); 44 | 45 | } 46 | 47 | }; 48 | 49 | 50 | //{ Driver Code Starts. 51 | int main() 52 | { 53 | //taking total testcases 54 | int t; 55 | cin>>t; 56 | while(t--) 57 | { 58 | //taking eggs and floors count 59 | int n, k; 60 | cin>>n>>k; 61 | Solution ob; 62 | //calling function eggDrop() 63 | cout< 2 | using namespace std; 3 | #define int long long 4 | 5 | int n, steps; vector> adj; vector L,R; 6 | 7 | int dfs(int s) { 8 | int here = 0; 9 | for(int i: adj[s]) { 10 | here += dfs(i); 11 | } 12 | if(here < L[s]) {steps++; return R[s];} 13 | return min(R[s], here); 14 | } 15 | 16 | signed main() { 17 | int t; cin >> t; 18 | while(t--) { 19 | steps = 0; 20 | cin >> n; adj.assign(n,{}); L.assign(n, 0); R.assign(n, 0); 21 | for(int i = 1; i < n; i++) { 22 | int p; cin >> p; 23 | adj[--p].push_back(i); 24 | } 25 | for(int i = 0; i < n; i++) cin >> L[i] >> R[i]; 26 | dfs(0); cout << steps << endl; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Fibonacci Iterative: -------------------------------------------------------------------------------- 1 | int fibo(int n) { 2 | if (n == 0) return 0; 3 | if (n == 1) return 1; 4 | int x = 0; 5 | int y = 1; 6 | for (int i = 2; i < n; i++) { 7 | int tmp = x + y; 8 | x = y; 9 | y = tmp; 10 | } 11 | return x + y; 12 | } 13 | -------------------------------------------------------------------------------- /InsertionSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printArray(int array[], int size) { 5 | for (int i = 0; i < size; i++) { 6 | cout << array[i] << " "; 7 | } 8 | cout << endl; 9 | } 10 | 11 | void insertionSort(int array[], int size) { 12 | for (int step = 1; step < size; step++) { 13 | int key = array[step]; 14 | int j = step - 1; 15 | while (key < array[j] && j >= 0) { 16 | array[j + 1] = array[j]; 17 | --j; 18 | } 19 | array[j + 1] = key; 20 | } 21 | } 22 | 23 | int main() { 24 | int data[] = {9, 5, 1, 4, 3}; 25 | int size = sizeof(data) / sizeof(data[0]); 26 | insertionSort(data, size); 27 | cout << "Sorted array in ascending order:\n"; 28 | printArray(data, size); 29 | } 30 | -------------------------------------------------------------------------------- /Kth bit in binary string: -------------------------------------------------------------------------------- 1 | def findKthBit(self, n, k): 2 | flip = 0 3 | l = 2 ** n - 1 4 | while k > 1: 5 | if k == l / 2 + 1: 6 | return str(1 ^ flip) 7 | if k > l / 2: 8 | k = l + 1 - k 9 | flip = 1 - flip 10 | l /= 2 11 | return str(flip) 12 | -------------------------------------------------------------------------------- /LargestRectangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX = 100; 5 | 6 | // This function basically finds largest 0 7 | // sum subarray in temp[0..n-1]. If 0 sum 8 | // does't exist, then it returns false. Else 9 | // it returns true and sets starting and 10 | // ending indexes as starti and endj. 11 | bool sumZero(int temp[], int* starti, 12 | int* endj, int n) 13 | { 14 | // Map to store the previous sums 15 | map presum; 16 | int sum = 0; // Initialize sum of elements 17 | 18 | // Initialize length of sub-array with sum 0 19 | int max_length = 0; 20 | 21 | // Traverse through the given array 22 | for (int i = 0; i < n; i++) 23 | { 24 | // Add current element to sum 25 | sum += temp[i]; 26 | 27 | if (temp[i] == 0 && max_length == 0) 28 | { 29 | *starti = i; 30 | *endj = i; 31 | max_length = 1; 32 | } 33 | if (sum == 0) 34 | { 35 | if (max_length < i + 1) 36 | { 37 | *starti = 0; 38 | *endj = i; 39 | } 40 | max_length = i + 1; 41 | } 42 | 43 | // Look for this sum in Hash table 44 | if (presum.find(sum) != presum.end()) 45 | { 46 | // store previous max_length so 47 | // that we can check max_length 48 | // is updated or not 49 | int old = max_length; 50 | 51 | // If this sum is seen before, 52 | // then update max_len 53 | max_length = max(max_length, i - presum[sum]); 54 | 55 | if (old < max_length) 56 | { 57 | // If max_length is updated then 58 | // enter and update start and end 59 | // point of array 60 | *endj = i; 61 | *starti = presum[sum] + 1; 62 | } 63 | } 64 | else 65 | 66 | // Else insert this sum with 67 | // index in hash table 68 | presum[sum] = i; 69 | } 70 | 71 | // Return true if max_length is non-zero 72 | return (max_length != 0); 73 | } 74 | 75 | // The main function that finds Largest rectangle 76 | // sub-matrix in a[][] whose sum is 0. 77 | void sumZeroMatrix(int a[][MAX], int row, int col) 78 | { 79 | int temp[row]; 80 | 81 | // Variables to store the final output 82 | int fup = 0, fdown = 0, fleft = 0, fright = 0; 83 | int sum; 84 | int up, down; 85 | int maxl = INT_MIN; 86 | 87 | // Set the left column 88 | for (int left = 0; left < col; left++) 89 | { 90 | // Initialize all elements of temp as 0 91 | memset(temp, 0, sizeof(temp)); 92 | 93 | // Set the right column for the left column 94 | // set by outer loop 95 | for (int right = left; right < col; right++) 96 | { 97 | // Calculate sum between current left 98 | // and right for every row 'i' 99 | for (int i = 0; i < row; i++) 100 | temp[i] += a[i][right]; 101 | 102 | // Find largest subarray with 0 sum in 103 | // temp[]. The sumZero() function also 104 | // sets values of start and finish. So 105 | // 'sum' is sum of rectangle between (start, 106 | // left) and (finish, right) which is 107 | // boundary columns strictly as left and right. 108 | bool sum = sumZero(temp, &up, &down, row); 109 | int ele = (down - up + 1) * (right - left + 1); 110 | 111 | // Compare no. of elements with previous 112 | // no. of elements in sub-Matrix. 113 | // If new sub-matrix has more elements 114 | // then update maxl and final boundaries 115 | // like fup, fdown, fleft, fright 116 | if (sum && ele > maxl) 117 | { 118 | fup = up; 119 | fdown = down; 120 | fleft = left; 121 | fright = right; 122 | maxl = ele; 123 | } 124 | } 125 | } 126 | 127 | // If there is no change in boundaries 128 | // than check if a[0][0] is 0 129 | // If it not zero then print 130 | // that no such zero-sum sub-matrix exists 131 | if (fup == 0 && fdown == 0 && fleft == 0 && 132 | fright == 0 && a[0][0] != 0) { 133 | cout << "No zero-sum sub-matrix exists"; 134 | return; 135 | } 136 | 137 | // Print final values 138 | for (int j = fup; j <= fdown; j++) 139 | { 140 | for (int i = fleft; i <= fright; i++) 141 | cout << a[j][i] << " "; 142 | cout << endl; 143 | } 144 | } 145 | 146 | // Driver program to test above functions 147 | int main() 148 | { 149 | int a[][MAX] = { { 9, 7, 16, 5 }, { 1, -6, -7, 3 }, 150 | { 1, 8, 7, 9 }, { 7, -2, 0, 10 } }; 151 | 152 | int row = 4, col = 4; 153 | sumZeroMatrix(a, row, col); 154 | return 0; 155 | } 156 | -------------------------------------------------------------------------------- /LeftViewofBinaryTree.py: -------------------------------------------------------------------------------- 1 | #User function Template for python3 2 | 3 | 4 | ''' 5 | # Node Class: 6 | class Node: 7 | def _init_(self,val): 8 | self.data = val 9 | self.left = None 10 | self.right = None 11 | ''' 12 | 13 | #Function to return a list containing elements of left view of the binary tree. 14 | def LeftView(root): 15 | 16 | # code here 17 | if not root: return [] 18 | left = LeftView(root.left) 19 | right = LeftView(root.right) 20 | return [root.data] + left + right[len(left):] 21 | 22 | 23 | #{ 24 | # Driver Code Starts 25 | #Contributed by Sudarshan Sharma 26 | from collections import deque 27 | # Tree Node 28 | class Node: 29 | def __init__(self, val): 30 | self.right = None 31 | self.data = val 32 | self.left = None 33 | 34 | # Function to Build Tree 35 | def buildTree(s): 36 | #Corner Case 37 | if(len(s)==0 or s[0]=="N"): 38 | return None 39 | 40 | # Creating list of strings from input 41 | # string after spliting by space 42 | ip=list(map(str,s.split())) 43 | 44 | # Create the root of the tree 45 | root=Node(int(ip[0])) 46 | size=0 47 | q=deque() 48 | 49 | # Push the root to the queue 50 | q.append(root) 51 | size=size+1 52 | 53 | # Starting from the second element 54 | i=1 55 | while(size>0 and i=len(ip)): 76 | break 77 | currVal=ip[i] 78 | 79 | # If the right child is not null 80 | if(currVal!="N"): 81 | 82 | # Create the right child for the current node 83 | currNode.right=Node(int(currVal)) 84 | 85 | # Push it to the queue 86 | q.append(currNode.right) 87 | size=size+1 88 | i=i+1 89 | return root 90 | 91 | 92 | if __name__=="__main__": 93 | t=int(input()) 94 | for _ in range(0,t): 95 | s=input() 96 | root=buildTree(s) 97 | result = LeftView(root) 98 | for value in result: 99 | print(value,end=" ") 100 | print() 101 | 102 | # } Driver Code Ends -------------------------------------------------------------------------------- /Lobb_Number.cpp: -------------------------------------------------------------------------------- 1 | // CPP Program to find Ln, m Lobb Number. 2 | #include 3 | #define MAXN 109 4 | using namespace std; 5 | 6 | int binomialCoeff(int n, int k) 7 | { 8 | int C[n + 1][k + 1]; 9 | 10 | 11 | for (int i = 0; i <= n; i++) { 12 | for (int j = 0; j <= min(i, k); j++) { 13 | 14 | if (j == 0 || j == i) 15 | C[i][j] = 1; 16 | 17 | 18 | else 19 | C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; 20 | } 21 | } 22 | 23 | return C[n][k]; 24 | } 25 | 26 | 27 | int lobb(int n, int m) 28 | { 29 | return ((2 * m + 1) * binomialCoeff(2 * n, m + n)) / (m + n + 1); 30 | } 31 | 32 | 33 | int main() 34 | { 35 | int n = 5, m = 3; 36 | cout << lobb(n, m) << endl; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Longest_common_increasing_subsequence(LCIS)_2000_RATED_ON_CF.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include // Including tree_order_statistics_node_update 13 | #define MOD 1000000007 14 | #define INF 1e18 15 | #define ff first 16 | #define ss second 17 | #define PI 3.141592653589793238462 18 | #define set_bits __builtin_popcountll 19 | #define sz(x) ((int)(x).size()) 20 | #define all(x) (x).begin(), (x).end() 21 | #define small 0 22 | #define MAXSIZE 100000 23 | const int N = 200005; 24 | #define endl "\n" 25 | using namespace std; 26 | using namespace __gnu_pbds; 27 | #define debug(x) cout << #x << " " << x << endl; 28 | #define debug1(x) cout << #x << " " << x << " "; 29 | #define debug2(x) cout<<"["<<#x<<" = "<, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key 47 | 48 | const int MAXN = (int)((1e8) + 100); 49 | ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;} 50 | int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a);} 51 | int max(int a, int b) {if (a > b) return a; else return b;} 52 | int min(int a, int b) {if (a < b) return a; else return b;} 53 | vector sieve(int n) {int*arr = new int[n + 1](); vector vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;} 54 | 55 | 56 | void precision(int a) 57 | { 58 | cout << setprecision(a) << fixed; 59 | } 60 | 61 | // { 62 | // reference 63 | // cout << "0th element: " << *A.find_by_order(0) << endl; 64 | // cout << "No. of elems smaller than 6: " << A.order_of_key(6) << endl; // 2 65 | // cout << "Lower Bound of 6: " << *A.lower_bound(6) << endl; 66 | // cout << "Upper Bound of 6: " << *A.upper_bound(6) << endl; 67 | // } 68 | 69 | //** pritishcf307 **// 70 | //**------------------------------------------------------------------------------------------------------**// 71 | ll index=0; 72 | ll valod=0; 73 | vector construct(vector v1,vector v2){ 74 | ll n1=sz(v1); 75 | ll n2=sz(v2); 76 | vector dp(n2+1),helper(n2+1); 77 | for(ll i=1;i<=n1;i++){ 78 | ll current=0; 79 | for(ll j=1;j<=n2;j++){ 80 | if(v1[i]==v2[j]){ 81 | dp[j]=dp[current]+1; 82 | helper[j]=current; 83 | } 84 | else if(v2[j]dp[current]){ 85 | current=j; 86 | } 87 | } 88 | } 89 | ll val2=0; 90 | for(ll i=1;i<=n2;i++){ 91 | if(dp[i] > dp[val2]){ 92 | val2=i; 93 | index=i; 94 | } 95 | } 96 | valod=dp[val2]; 97 | vector ans; 98 | while(val2){ 99 | ans.push_back(v2[val2]); 100 | val2=helper[val2]; 101 | } 102 | return ans; 103 | } 104 | vector lcs(vector v1,vector v2){ 105 | vector helper; 106 | ll n1=sz(v1); 107 | ll n2=sz(v2); 108 | ll dp[n1][n2]; 109 | for(ll i=0;i 0 && p2 > 0){ 130 | if(v1[p1-1]==v2[p2-1]){ 131 | helper.push_back(v1[p1-1]); 132 | p1--; 133 | p2--; 134 | } 135 | else{ 136 | if(dp[p1-1][p2] > dp[p1][p2-1]){ 137 | p1--; 138 | } 139 | else{ 140 | p2--; 141 | } 142 | } 143 | } 144 | reverse(all(helper)); 145 | return helper; 146 | } 147 | vector lis(vector v){ 148 | set s; 149 | for(auto i:v){ 150 | s.insert(i); 151 | } 152 | vector helper; 153 | for(auto i:s){ 154 | helper.push_back(i); 155 | } 156 | vector helper1=lcs(v,helper); 157 | return helper1; 158 | } 159 | vector common(vector v1,vector v2){ 160 | vector helperl; 161 | set s1; 162 | for(auto i:v1){ 163 | s1.insert(i); 164 | } 165 | for(auto i:v2){ 166 | if(s1.count(i)){ 167 | helperl.push_back(i); 168 | } 169 | } 170 | return helperl; 171 | } 172 | void solve(){ 173 | ll n1; 174 | cin >> n1; 175 | vector v1(n1+1); 176 | for(ll i=0;i> v1[i+1]; 178 | } 179 | ll n2; 180 | cin >> n2; 181 | vector v2(n2+1); 182 | for(ll i=0;i> v2[i+1]; 184 | } 185 | vector ans3=construct(v1,v2); 186 | if(ans3.size() > 0){ 187 | ll val1=*max_element(all(ans3)); 188 | } 189 | vector ans1=common(v1,v2); 190 | vector ans2=common(v2,v1); 191 | vector ans4=lis(ans1); 192 | vector ans5=lis(ans2); 193 | cout << valod << endl; 194 | reverse(all(ans3)); 195 | for(auto i:ans3){ 196 | cout << i << " "; 197 | } 198 | } 199 | 200 | int main(){ 201 | init(); 202 | IOS; 203 | ll t=1; 204 | // cin >> t; 205 | while(t--){ 206 | solve(); 207 | } 208 | return 0; 209 | } 210 | -------------------------------------------------------------------------------- /Make_equal_by_deleting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int dp[1001][1001]; 5 | int rec(string s1, string s2, int n, int m) 6 | { 7 | if (m == 0 || n == 0) 8 | return 0; 9 | if (dp[n][m] != -1) return dp[n][m]; 10 | if (s1[n - 1] == s2[m - 1]) 11 | { 12 | return dp[n][m] = 1 + rec(s1, s2, n - 1, m - 1); 13 | } 14 | return dp[n][m] = max(rec(s1, s2, n - 1, m), rec(s1, s2, n, m -1)); 15 | } 16 | 17 | int main() 18 | { 19 | memset(dp, -1, sizeof(dp)); 20 | string a, b; 21 | cin >> a >> b; 22 | 23 | int ans = a.size() + b.size() - 2 * rec(a, b, a.size(), b.size()); 24 | 25 | cout << ans << endl; 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /MaximumSumOfProductsCF.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long int 4 | int modpowIter(int a, int b, int c) 5 | { 6 | int ans = 1; 7 | while (b != 0) 8 | { 9 | if (b % 2 == 1) 10 | ans = (ans * a) % c; 11 | 12 | a = (a * a) % c; 13 | b /= 2; 14 | } 15 | return ans; 16 | } 17 | vectora(5000); 18 | vectorb(5000); 19 | ll dp[5000][5000]; 20 | ll compute(int i,int j) 21 | { 22 | if(dp[i][j]!=-1) 23 | return dp[i][j]; 24 | if(i==j) 25 | return dp[i][j]=a[i]*b[i]; 26 | if(abs(i-j)==1) 27 | { 28 | return dp[i][j]=a[i]*b[j]+a[j]*b[i]; 29 | } 30 | return dp[i][j]=a[i]*b[j]+b[i]*a[j]+compute(i+1,j-1); 31 | } 32 | int main() 33 | { 34 | 35 | 36 | ll n; 37 | cin>>n; 38 | memset(dp,-1,sizeof(dp)); 39 | for(int i=0;i>a[i]; 42 | } 43 | for(int i=0;i>b[i]; 46 | } 47 | vectorpref(n); 48 | vectorsuff(n); 49 | pref[0]=0; 50 | suff[n-1]=0; 51 | for(int i=1;i=0;i--) 56 | { 57 | suff[i]=a[i+1]*b[i+1]+suff[i+1]; 58 | } 59 | ll ans=0; 60 | for(int i=0;i 3 | #include 4 | #define R 3 5 | #define C 3 6 | using namespace std; 7 | int min(int a, int b, int c); 8 | 9 | int minCost(int cost[R][C], int m, int n) 10 | { 11 | 12 | int tc[R][C]; 13 | 14 | tc[0][0] = cost[0][0]; 15 | 16 | 17 | for (int i = 1; i <= m; i++) 18 | tc[i][0] = tc[i - 1][0] + cost[i][0]; 19 | 20 | 21 | for (int j = 1; j <= n; j++) 22 | tc[0][j] = tc[0][j - 1] + cost[0][j]; 23 | 24 | for (int i = 1; i <= m; i++) 25 | for (int j = 1; j <= n; j++) 26 | tc[i][j] = min(tc[i - 1][j - 1], 27 | tc[i - 1][j], 28 | tc[i][j - 1]) + cost[i][j]; 29 | 30 | return tc[m][n]; 31 | } 32 | 33 | int min(int a, int b, int c) 34 | { 35 | if (a < b) 36 | return (a < c)? a : c; 37 | else 38 | return (b < c)? b : c; 39 | } 40 | 41 | 42 | int main() 43 | { 44 | int cost[R][C] = { {21, 22, 23}, 45 | {44, 82, 22}, 46 | {22, 45, 24} }; 47 | cout << " " << minCost(cost, 22, 22); 48 | return 0; 49 | } 50 | 51 | // This code is contributed by Moksh Gupta 52 | -------------------------------------------------------------------------------- /Minimum path sum: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dp[201][201]; 4 | int rec(int i,int j,vector>& grid) 5 | { 6 | if (i==0&&j==0) return grid[0][0]; 7 | if (i<0||j<0) return 100000; 8 | if (dp[i][j]!=-1) return dp[i][j]; 9 | int pick = grid[i][j] + rec(i-1,j,grid); 10 | int picky = grid[i][j] + rec(i,j-1,grid); 11 | return dp[i][j]=min(pick,picky); 12 | } 13 | int minPathSum(vector>& grid) { 14 | memset(dp,-1,sizeof(dp)); 15 | // if (grid.size()==1) 16 | return rec(grid.size()-1,grid[0].size()-1,grid); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Non_Adjacent_Maximum_Sum.cpp: -------------------------------------------------------------------------------- 1 | RECURSION 2 | int maxSum(int n, vector &vec, vector &dp) 3 | { 4 | if (n <= 0) 5 | return 0; 6 | 7 | int pick = vec[ind] + maxSum(ind - 2, vec, dp); 8 | int not_pick = maxSum(n - 1, vec, dp); 9 | 10 | return max(pick, not_pick); 11 | } 12 | 13 | MEMORIZATION 14 | 15 | vectordp(n,-1); 16 | int maxSum(int n, vector&vec, vector&dp){ 17 | if(n<=0)return 0; 18 | if(dp[n]!=-1)return dp[n]; 19 | 20 | int pick = vec[ind]+maxSum(ind-2,vec,dp); 21 | int not_pick = maxSum(n-1,vec,dp); 22 | 23 | return dp[n] = max(pick,not_pick); 24 | } 25 | 26 | TABULATION 27 | int maxSum(int n, vector&vec){ 28 | 29 | vectordp(n); 30 | dp[0]=vec[0]; 31 | dp[1]=max(dp[0],vec[1]); 32 | 33 | for(int i=2;i&vec){ 43 | int prev2 = vec[0]; 44 | int prev1 = max(vec[1],prev2); 45 | 46 | for(int i=2;i n * k <= 100,000 8 | 9 | */ 10 | 11 | #include 12 | using namespace std; 13 | 14 | vector> dp; // as the constraints are given for n * k and not for n & k seperately we used a vector instead of a 2D array for memoisation to save space and time. 15 | bool rec(vector nums, int k, int i, int cur) 16 | { 17 | if (cur > k) return 0; 18 | 19 | if (cur == k) return 1; 20 | 21 | if (i >= nums.size()) return 0; 22 | 23 | if (dp[i][cur] != -1) return dp[i][cur]; 24 | 25 | return dp[i][cur] = rec(nums, k, i + 2, cur + nums[i]) || rec(nums, k, i + 1, cur); // if we choose ith element we move pointer to (i + 2)th element and to (i + 1)th otherwise. 26 | } 27 | 28 | int main() 29 | { 30 | int n, k; 31 | cin >> n >> k; 32 | 33 | vector nums; 34 | 35 | for (int i = 0; i < n; i++) 36 | { 37 | int temp; 38 | cin >> temp; 39 | 40 | nums.push_back(temp); 41 | } 42 | 43 | dp.resize(n + 5); 44 | 45 | for (auto &it : dp) 46 | { 47 | it.resize(k + 5); 48 | } 49 | for (auto &it1: dp) 50 | { 51 | for (auto &it: it1) 52 | { 53 | it = -1; 54 | } 55 | } 56 | bool ans = rec(nums, k, 0, 0); 57 | if (ans) cout << "Possible" << endl; 58 | else cout << "Not possible" << endl; 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Number of subsequences with given sum.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #define pin cin 3 | #define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 4 | #define ll long long int 5 | #define pl cout<<"\n"; 6 | #define mp make_pair 7 | #define pb push_back 8 | #define vll vector 9 | #define all(x) (x).begin(),(x).end() 10 | #define f first 11 | #define s second 12 | #define prv(v) for(auto x:v) cout<=0;i--)//order_of_key 15 | #define floop(i,l,n)for(int i=l;i<=n;i++)//find_by_order 16 | #define for1(i,n) for(ll i=1;i<=n;i++) //pop_back() 17 | using namespace std; 18 | int print(int arr[],int i,int a,int sum,int curr) 19 | { 20 | if (i>=a) 21 | { 22 | if (curr==sum) 23 | { 24 | // conditiion satisfied 25 | return 1; 26 | } 27 | //condition not satisfied 28 | return 0; 29 | } 30 | //take condition , the element is added in our subsequence 31 | curr+=arr[i]; 32 | ll left=print(arr,i+1,a,sum,curr); 33 | //not take condition , the element is not added in our subsequence 34 | 35 | curr-=arr[i]; 36 | ll right=print(arr,i+1,a,sum,curr); 37 | return left+right; 38 | 39 | } 40 | void solve() 41 | { 42 | ll ans=0,sta=0,res=0; 43 | mapmpp; 44 | setsp; 45 | int arr[]={1,2,1}; 46 | int sum=2; 47 | // vectords; 48 | cout<>t; 55 | for1(i,t) 56 | { 57 | // cout<<"Case #"<val+'0');head=head->next;} 6 | string p=s; 7 | reverse(p.begin(),p.end()); 8 | return s==p; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /Palindrome-Partitioning - II.txt: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCut(string s) { 4 | int n = s.size(); 5 | vector v(n,0); 6 | for(int i=0;i> dp(n,vector(n,false)); 10 | for(int i=0;i=n) 17 | break; 18 | } 19 | } 20 | for(int i=0;i=n) 28 | break; 29 | } 30 | } 31 | for(int i=0;i& nums, int sum){ 4 | int n = nums.size(); 5 | int t[n+1][sum+1]; 6 | for(int i = 0; i < n+1; i++){ 7 | for(int j = 0; j < sum+1; j++){ 8 | if(i == 0) 9 | t[i][j] = false; 10 | if(j == 0) 11 | t[i][j] = true; 12 | } 13 | } 14 | for(int i = 1; i < n+1; i++){ 15 | for(int j = 1; j < sum+1; j++){ 16 | t[i][j] = t[i-1][j]; 17 | if(nums[i-1] <= j) 18 | t[i][j] |= t[i-1][j-nums[i-1]]; 19 | } 20 | } 21 | return t[n][sum]; 22 | } 23 | bool canPartition(vector& nums) { 24 | int n = nums.size(); 25 | long sum = 0; 26 | for(int i = 0; i < n; i++) 27 | sum = sum + nums[i]; 28 | if(sum % 2 != 0) 29 | return false; 30 | 31 | return subsetSum(nums, sum/2); 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /PartitionSet.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int minSubsetSumDifference(vector < int > & arr, int n) { 6 | int totSum = 0; 7 | 8 | for (int i = 0; i < n; i++) { 9 | totSum += arr[i]; 10 | } 11 | 12 | vector < bool > prev(totSum + 1, false); 13 | 14 | prev[0] = true; 15 | 16 | if (arr[0] <= totSum) 17 | prev[arr[0]] = true; 18 | 19 | for (int ind = 1; ind < n; ind++) { 20 | vector < bool > cur(totSum + 1, false); 21 | cur[0] = true; 22 | for (int target = 1; target <= totSum; target++) { 23 | bool notTaken = prev[target]; 24 | 25 | bool taken = false; 26 | if (arr[ind] <= target) 27 | taken = prev[target - arr[ind]]; 28 | 29 | cur[target] = notTaken || taken; 30 | } 31 | prev = cur; 32 | } 33 | 34 | int mini = 1e9; 35 | for (int i = 0; i <= totSum; i++) { 36 | if (prev[i] == true) { 37 | int diff = abs(i - (totSum - i)); 38 | mini = min(mini, diff); 39 | } 40 | } 41 | return mini; 42 | } 43 | 44 | int main() { 45 | 46 | vector arr = {1,2,3,4}; 47 | int n = arr.size(); 48 | 49 | cout << "The minimum absolute difference is: " << minSubsetSumDifference(arr, n); 50 | } 51 | -------------------------------------------------------------------------------- /PolyAdd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int coef; 6 | int pow; 7 | struct Node *next; 8 | }; 9 | 10 | void node_creation(int x, int y, struct Node **temp) 11 | { 12 | struct Node *a, *r; 13 | a = *temp; 14 | if(a == NULL) 15 | { 16 | r =(struct Node*)malloc(sizeof(struct Node)); 17 | r->coef = x; 18 | r->pow = y; 19 | *temp = r; 20 | r->next = (struct Node*)malloc(sizeof(struct Node)); 21 | r = r->next; 22 | r->next = NULL; 23 | } 24 | else 25 | { 26 | r->coef = x; 27 | r->pow = y; 28 | r->next = (struct Node*)malloc(sizeof(struct Node)); 29 | r = r->next; 30 | r->next = NULL; 31 | } 32 | } 33 | 34 | //polynomial addition 35 | void add(struct Node *p1, struct Node *p2, struct Node *result) 36 | { 37 | while(p1->next && p2->next) 38 | { 39 | if(p1->pow > p2->pow) 40 | { 41 | result->pow = p1->pow; 42 | result->coef = p1->coef; 43 | p1 = p1->next; 44 | } 45 | else if(p1->pow < p2->pow) 46 | { 47 | result->pow = p2->pow; 48 | result->coef = p2->coef; 49 | p2 = p2->next; 50 | } 51 | else 52 | { 53 | result->pow = p1->pow; 54 | result->coef = p1->coef+p2->coef; 55 | p1 = p1->next; 56 | p2 = p2->next; 57 | } 58 | result->next = (struct Node *)malloc(sizeof(struct Node)); 59 | result = result->next; 60 | result->next = NULL; 61 | } 62 | while(p1->next || p2->next) 63 | { 64 | if(p1->next) 65 | { 66 | result->pow = p1->pow; 67 | result->coef = p1->coef; 68 | p1 = p1->next; 69 | } 70 | if(p2->next) 71 | { 72 | result->pow = p2->pow; 73 | result->coef = p2->coef; 74 | p2 = p2->next; 75 | } 76 | result->next = (struct Node *)malloc(sizeof(struct Node)); 77 | result = result->next; 78 | result->next = NULL; 79 | } 80 | } 81 | 82 | //resultant polynomial 83 | void display(struct Node *node) 84 | { 85 | while(node->next != NULL) 86 | { 87 | printf("%dx^%d", node->coef, node->pow); 88 | node = node->next; 89 | if(node->next != NULL) 90 | printf(" + "); 91 | } 92 | } 93 | int main() 94 | { 95 | struct Node *p1 = NULL, *p2 = NULL, *result = NULL; 96 | //creating polynomial1 p1 and polynomial2 p2 97 | node_creation(2,3,&p1); 98 | node_creation(1,1,&p1); 99 | node_creation(5,0,&p1); 100 | node_creation(3,4,&p2); 101 | node_creation(2,2,&p2); 102 | node_creation(8,1,&p2); 103 | printf("polynomial 1: "); 104 | display(p1); 105 | printf("\npolynomial 2: "); 106 | display(p2); 107 | result = (struct Node *)malloc(sizeof(struct Node)); 108 | add(p1, p2, result); 109 | printf("\nresultant polynomial: "); 110 | display(result); 111 | return 0; 112 | } 113 | -------------------------------------------------------------------------------- /Print all subsequences.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #define pin cin 3 | #define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 4 | #define ll long long int 5 | #define pl cout<<"\n"; 6 | #define mp make_pair 7 | #define pb push_back 8 | #define vll vector 9 | #define all(x) (x).begin(),(x).end() 10 | #define f first 11 | #define s second 12 | #define prv(v) for(auto x:v) cout<=0;i--)//order_of_key 15 | #define floop(i,l,n)for(int i=l;i<=n;i++)//find_by_order 16 | #define for1(i,n) for(ll i=1;i<=n;i++) //pop_back() 17 | using namespace std; 18 | void print(vector&ds,int arr[],int i,int a) 19 | { 20 | if (i>=a) 21 | { 22 | if (ds.size()==0) cout<<"{}"<<'\n'; 23 | else { 24 | prv(ds); 25 | pl 26 | } 27 | return; 28 | } 29 | //take condition , the element is added in our subsequence 30 | ds.pb(arr[i]); 31 | // call the function 32 | print(ds,arr,i+1,a); 33 | //not take condition , the element is not added in our subsequence 34 | ds.pop_back(); 35 | // call the function 36 | print(ds,arr,i+1,a); 37 | // for getting reverse order just put not take condition in first 38 | } 39 | void solve() 40 | { 41 | ll ans=0,sta=0,res=0; 42 | mapmpp; 43 | setsp; 44 | int arr[]={3,1,2}; 45 | vectords; 46 | print(ds,arr,0,3); 47 | } 48 | int main() 49 | { 50 | ios 51 | ll t=1; 52 | // cin>>t; 53 | for1(i,t) 54 | { 55 | // cout<<"Case #"< 2 | #define pin cin 3 | #define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 4 | #define ll long long int 5 | #define pl cout<<"\n"; 6 | #define mp make_pair 7 | #define pb push_back 8 | #define vll vector 9 | #define all(x) (x).begin(),(x).end() 10 | #define f first 11 | #define s second 12 | #define prv(v) for(auto x:v) cout<=0;i--)//order_of_key 15 | #define floop(i,l,n)for(int i=l;i<=n;i++)//find_by_order 16 | #define for1(i,n) for(ll i=1;i<=n;i++) //pop_back() 17 | using namespace std; 18 | void print(string s,string p,int curr) 19 | { 20 | if (curr>=s.length()) 21 | { 22 | if(p.length()==0) cout<<"ppppp"; 23 | cout<mpp; 35 | setsp; 36 | // int sum=2; 37 | // vectords; 38 | string s="abcd",p=""; 39 | print(s,p,0); 40 | } 41 | int main() 42 | { 43 | ios 44 | ll t=1; 45 | // cin>>t; 46 | for1(i,t) 47 | { 48 | // cout<<"Case #"< 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | // User function Template for C++ 10 | 11 | 12 | 13 | class Solution{ 14 | public: 15 | int dp[1005][1005]; 16 | 17 | int help(int price[], vector &v, int w ,int n ) 18 | { 19 | 20 | if(n<=0) 21 | return 0; 22 | 23 | if(dp[n][w]!=-1) 24 | return dp[n][w]; 25 | 26 | if(v[n-1]<=w) 27 | { 28 | return dp[n][w]=max(help(price,v,w-v[n-1],n)+price[n-1], help(price,v,w,n-1)); 29 | } 30 | 31 | else 32 | return dp[n][w]= help(price, v,w,n-1); 33 | 34 | 35 | } 36 | 37 | int cutRod(int price[], int n) { 38 | //code here 39 | memset(dp,-1,sizeof(dp)); 40 | vectorv(n); 41 | 42 | for(int i=0;i> t; 56 | while (t--) { 57 | int n; 58 | cin >> n; 59 | int a[n]; 60 | for (int i = 0; i < n; i++) 61 | cin >> a[i]; 62 | 63 | Solution ob; 64 | 65 | cout << ob.cutRod(a, n) << endl; 66 | } 67 | return 0; 68 | } 69 | // } Driver Code Ends 70 | -------------------------------------------------------------------------------- /Segment tree template: -------------------------------------------------------------------------------- 1 | class SGTree { 2 | vector seg; 3 | public: 4 | SGTree(int n) { 5 | seg.resize(4 * n + 1); 6 | } 7 | 8 | void build(int ind, int low, int high, int arr[]) { 9 | if (low == high) { 10 | seg[ind] = arr[low]; 11 | return; 12 | } 13 | 14 | int mid = (low + high) / 2; 15 | build(2 * ind + 1, low, mid, arr); 16 | build(2 * ind + 2, mid + 1, high, arr); 17 | seg[ind] = min(seg[2 * ind + 1], seg[2 * ind + 2]); 18 | } 19 | 20 | int query(int ind, int low, int high, int l, int r) { 21 | // no overlap 22 | // l r low high or low high l r 23 | if (r < low || high < l) return INT_MAX; 24 | 25 | // complete overlap 26 | // [l low high r] 27 | if (low >= l && high <= r) return seg[ind]; 28 | 29 | int mid = (low + high) >> 1; 30 | int left = query(2 * ind + 1, low, mid, l, r); 31 | int right = query(2 * ind + 2, mid + 1, high, l, r); 32 | return min(left, right); 33 | } 34 | void update(int ind, int low, int high, int i, int val) { 35 | if (low == high) { 36 | seg[ind] = val; 37 | return; 38 | } 39 | 40 | int mid = (low + high) >> 1; 41 | if (i <= mid) update(2 * ind + 1, low, mid, i, val); 42 | else update(2 * ind + 2, mid + 1, high, i, val); 43 | seg[ind] = min(seg[2 * ind + 1], seg[2 * ind + 2]); 44 | } 45 | }; 46 | -------------------------------------------------------------------------------- /Segment_tree.cpp: -------------------------------------------------------------------------------- 1 | //If we have to find a sum of all elements of the given range of a subarray 2 | //and also we have to update some of the values ,for this there are generally 2 methods 3 | //1-> Naive updation in value ,then calculating the sum of the given range 4 | // Time Complexity updation->O(1) QueryProcessing->O(n) ie {range of the query} 5 | // 2->Using Prefix Sum ,but here updation of a value in the array is costly 6 | // Time Complexity updation->O(n) QueryProcessing->O(1) ie {pref[r]-pre[l-1]} l and r are the range of the query 7 | // 8 | // Now if suppose there are large no of queries and Updation ,then also we can solve 9 | // the question using Segment tree method in the Time Complexity of O(N(logN)) both the 10 | // operations 11 | // Concept used is divide and Conqueor 12 | //Time Complexity updation->O(N(logN)) QueryProcessing->O(N(logN)) ie {range of the query} 13 | //https://youtu.be/c6GDAgiX5Yw 14 | 15 | 16 | #include 17 | using namespace std; 18 | const int N=1e5+2; 19 | int segment[4*N],arr[N]; 20 | 21 | void Segmentation(int node,int start,int end) 22 | { 23 | if(start==end){ 24 | segment[node]=arr[start]; 25 | return;} 26 | else 27 | { 28 | int mid=(start+end)/2; 29 | Segmentation(2*node,start,mid); 30 | Segmentation(2*node+1,mid+1,end); 31 | segment[node]=segment[2*node]+segment[2*node+1]; 32 | } 33 | 34 | } 35 | int solve(int node,int start,int end,int a,int b) 36 | { 37 | if(a>end || b=end) 40 | return segment[node]; 41 | else 42 | { 43 | int mid=(start+end)/2; 44 | int l=solve(2*node,start,mid,a,b); 45 | int r=solve(2*node+1,mid+1,end,a,b); 46 | return l+r; 47 | } 48 | 49 | } 50 | 51 | 52 | int main () 53 | { 54 | int i,q,j,k,n; 55 | cout<<"Enter the no of elements\n"; 56 | cin>>n; 57 | cout<<"Enter the value of elements\n"; 58 | 59 | for(i=0;i>arr[i]; 61 | Segmentation(1,0,n-1); 62 | cout<<"enter the no. of queries\n"; 63 | cin>>q; 64 | while(q--) 65 | { 66 | int a,b; 67 | cout<<"enter the queries"<<": "; 68 | cin>>a>>b; 69 | cout<<"\n"; 70 | a--,b--; 71 | cout< 4 | using namespace std; 5 | 6 | // Returns length of LCS 7 | // for X[0..m-1], Y[0..n-1] 8 | int lcs(string &X, string &Y) 9 | { 10 | 11 | // Find lengths of two strings 12 | int m = X.length(), n = Y.length(); 13 | 14 | int L[2][n + 1]; 15 | 16 | // Binary index, used to 17 | // index current row and 18 | // previous row. 19 | bool bi; 20 | 21 | for (int i = 0; i <= m; i++) 22 | { 23 | 24 | // Compute current 25 | // binary index 26 | bi = i & 1; 27 | 28 | for (int j = 0; j <= n; j++) 29 | { 30 | if (i == 0 || j == 0) 31 | L[bi][j] = 0; 32 | 33 | else if (X[i-1] == Y[j-1]) 34 | L[bi][j] = L[1 - bi][j - 1] + 1; 35 | 36 | else 37 | L[bi][j] = max(L[1 - bi][j], 38 | L[bi][j - 1]); 39 | } 40 | } 41 | 42 | // Last filled entry contains 43 | // length of LCS 44 | // for X[0..n-1] and Y[0..m-1] 45 | return L[bi][n]; 46 | } 47 | 48 | // Driver code 49 | int main() 50 | { 51 | string X = "AGGTAB"; 52 | string Y = "GXTXAYB"; 53 | 54 | printf("Length of LCS is %d\n", lcs(X, Y)); 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Subsequence sum equals to K (Bool).txt: -------------------------------------------------------------------------------- 1 | #include 2 | #define pin cin 3 | #define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 4 | #define ll long long int 5 | #define pl cout<<"\n"; 6 | #define mp make_pair 7 | #define pb push_back 8 | #define vll vector 9 | #define all(x) (x).begin(),(x).end() 10 | #define f first 11 | #define s second 12 | #define prv(v) for(auto x:v) cout<=0;i--)//order_of_key 15 | #define floop(i,l,n)for(int i=l;i<=n;i++)//find_by_order 16 | #define for1(i,n) for(ll i=1;i<=n;i++) //pop_back() 17 | using namespace std; 18 | bool print(vector&ds,int arr[],int i,int a,int sum,int curr) 19 | { 20 | if (i>=a) 21 | { 22 | if (curr==sum) 23 | { 24 | // conditiion satisfied 25 | prv(ds); 26 | pl 27 | return true; 28 | } 29 | //condition not satisfied 30 | return false; 31 | } 32 | //take condition , the element is added in our subsequence 33 | ds.push_back(arr[i]); 34 | curr+=arr[i]; 35 | if (print(ds,arr,i+1,a,sum,curr)==true) return true; 36 | //not take condition , the element is not added in our subsequence 37 | ds.pop_back(); 38 | curr-=arr[i]; 39 | if (print(ds,arr,i+1,a,sum,curr)==true) return true; 40 | return false; 41 | 42 | } 43 | void solve() 44 | { 45 | ll ans=0,sta=0,res=0; 46 | mapmpp; 47 | setsp; 48 | int arr[]={1,2,1}; 49 | int sum=2; 50 | vectords; 51 | print(ds,arr,0,3,sum,0); 52 | } 53 | int main() 54 | { 55 | ios 56 | ll t=1; 57 | // cin>>t; 58 | for1(i,t) 59 | { 60 | // cout<<"Case #"< 2 | #define pin cin 3 | #define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 4 | #define ll long long int 5 | #define pl cout<<"\n"; 6 | #define mp make_pair 7 | #define pb push_back 8 | #define vll vector 9 | #define all(x) (x).begin(),(x).end() 10 | #define f first 11 | #define s second 12 | #define prv(v) for(auto x:v) cout<=0;i--)//order_of_key 15 | #define floop(i,l,n)for(int i=l;i<=n;i++)//find_by_order 16 | #define for1(i,n) for(ll i=1;i<=n;i++) //pop_back() 17 | using namespace std; 18 | void print(vector&ds,int arr[],int i,int a,int sum,int curr) 19 | { 20 | if (i>=a) 21 | { 22 | if (curr==sum) 23 | { 24 | prv(ds); 25 | pl 26 | return; 27 | } 28 | return ; 29 | } 30 | //take condition , the element is added in our subsequence 31 | ds.push_back(arr[i]); 32 | curr+=arr[i]; 33 | print(ds,arr,i+1,a,sum,curr); 34 | //not take condition , the element is not added in our subsequence 35 | ds.pop_back(); 36 | curr-=arr[i]; 37 | print(ds,arr,i+1,a,sum,curr); 38 | 39 | } 40 | void solve() 41 | { 42 | ll ans=0,sta=0,res=0; 43 | mapmpp; 44 | setsp; 45 | int arr[]={1,2,1}; 46 | int sum=2; 47 | vectords; 48 | print(ds,arr,0,3,sum,0); 49 | } 50 | int main() 51 | { 52 | ios 53 | ll t=1; 54 | // cin>>t; 55 | for1(i,t) 56 | { 57 | // cout<<"Case #"< 5 | using namespace std; 6 | 7 | 8 | 9 | //memoization 10 | int dp[101][100001]; 11 | class Solution{ 12 | public: 13 | 14 | int solve(int n,vectorarr,int req_sum) 15 | { 16 | 17 | if(req_sum==0) 18 | { 19 | return 1; 20 | } 21 | if(n==0||req_sum<0) 22 | { 23 | 24 | return 0; 25 | } 26 | if(dp[n][req_sum]!=-1) 27 | return dp[n][req_sum]; 28 | else 29 | return dp[n][req_sum]=( solve(n-1 , arr,req_sum-arr[n-1])||solve(n-1, arr,req_sum)); 30 | 31 | 32 | } 33 | bool isSubsetSum(vectorarr, int sum){ 34 | memset(dp,-1,sizeof(dp)); 35 | int n=arr.size(); 36 | return solve(n,arr,sum) ; 37 | } 38 | }; 39 | 40 | 41 | int main() 42 | { 43 | int t; 44 | cin>>t; 45 | while(t--) 46 | { 47 | int N, sum; 48 | cin >> N; 49 | vector arr(N); 50 | for(int i = 0; i < N; i++){ 51 | cin >> arr[i]; 52 | } 53 | cin >> sum; 54 | 55 | Solution ob; 56 | cout << ob.isSubsetSum(arr, sum) << endl; 57 | } 58 | return 0; 59 | } 60 | 61 | 62 | // class Solution{ 63 | // public: 64 | // top-down approach 65 | // bool isSubsetSum(vectorarr, int sum){ 66 | // int i,j,k,sz=arr.size(); 67 | // bool dp[sz+1][sum+1]; 68 | // for(j=0;j<=sum;j++) 69 | // dp[0][j]=false; 70 | // for(i=0;i<=sz;i++) 71 | // dp[i][0]=true; 72 | // for(i=1;i<=sz;i++) 73 | // { 74 | // for(j=1;j<=sum;j++) 75 | // { 76 | // if(arr[i-1]>j) 77 | // dp[i][j]=dp[i-1][j]; 78 | // else if(arr[i-1]<=j) 79 | // dp[i][j]=(dp[i-1][j]|| dp[i-1][j-arr[i-1]]); 80 | // } 81 | // } 82 | // return dp[sz][sum]; 83 | 84 | // } 85 | // }; 86 | 87 | -------------------------------------------------------------------------------- /TowerOfHanoi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void towerOfHanoi(int n, char src, char dest, char extra) 5 | { 6 | if (n == 0) 7 | { 8 | return; 9 | } 10 | towerOfHanoi(n - 1, src, extra, dest); 11 | cout << "Move disk " << n << " from rod " << src << " to rod " << dest << endl; 12 | towerOfHanoi(n - 1, extra, dest, src); 13 | } 14 | 15 | int main() 16 | { 17 | int n; 18 | cin>>n; 19 | // A, B and C are names of rods 20 | towerOfHanoi(n, 'A', 'C', 'B'); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Triangle: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dp[201][201]; 4 | int rec(int i,int j,vector>& triangle) 5 | { 6 | if (j==triangle.size()) return 0; 7 | if (i>=triangle[j].size()||j>=triangle.size()) return 10000; 8 | if (dp[i][j]!=-1) return dp[i][j]; 9 | int pick1 = triangle[j][i]+rec(i,j+1,triangle); 10 | int pick2 = triangle[j][i]+rec(i+1,j+1,triangle); 11 | return dp[i][j]=min(pick1,pick2); 12 | } 13 | int minimumTotal(vector>& triangle) { 14 | memset(dp,-1,sizeof(dp)); 15 | return rec(0,0,triangle); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Unbounded Knapsack.txt: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int mini=1000000,dp[10001]; 4 | int recur(int i,vector&v,int sum) 5 | { 6 | if(sum<0||i>=v.size()) return 1000000000; 7 | if(sum==0) {return 0;} 8 | int l=1e9; 9 | if (dp[sum]!=-1) return dp[sum]; 10 | for(int i=0;iv; 20 | while(a<=100) 21 | { 22 | v.push_back(a*a); 23 | a++; 24 | } 25 | return recur(0,v,n); 26 | } 27 | }; -------------------------------------------------------------------------------- /Unique paths: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dp[101][101]; 4 | int rec(int i, int j,vector> o) 5 | { 6 | if (i<0||j<0) return 0; 7 | if (i==0&&j==0) return 1; 8 | if (o[i][j]==1) return 0; 9 | if (dp[i][j]!=-1) return dp[i][j]; 10 | int up=rec(i-1,j,o); 11 | int down=rec(i,j-1,o); 12 | return dp[i][j] = up+down; 13 | } 14 | int uniquePathsWithObstacles(vector>& o) { 15 | if (o[0][0]==1) return 0; 16 | memset(dp,-1,sizeof(dp)); 17 | return rec(o.size()-1,o[0].size()-1,o); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /WildcardMatching.java: -------------------------------------------------------------------------------- 1 | public class Matching { 2 | 3 | // Function that matches input str with 4 | // given wildcard pattern 5 | static boolean strmatch(String str, String pattern, 6 | int n, int m) 7 | { 8 | // empty pattern can only match with 9 | // empty string 10 | if (m == 0) 11 | return (n == 0); 12 | 13 | // lookup table for storing results of 14 | // subproblems 15 | boolean[][] lookup = new boolean[n + 1][m + 1]; 16 | 17 | // initialize lookup table to false 18 | for (int i = 0; i < n + 1; i++) 19 | Arrays.fill(lookup[i], false); 20 | 21 | // empty pattern can match with empty string 22 | lookup[0][0] = true; 23 | 24 | // Only '*' can match with empty string 25 | for (int j = 1; j <= m; j++) 26 | if (pattern.charAt(j - 1) == '*') 27 | lookup[0][j] = lookup[0][j - 1]; 28 | 29 | // fill the table in bottom-up fashion 30 | for (int i = 1; i <= n; i++) 31 | { 32 | for (int j = 1; j <= m; j++) 33 | { 34 | // Two cases if we see a '*' 35 | // a) We ignore '*'' character and move 36 | // to next character in the pattern, 37 | // i.e., '*' indicates an empty 38 | // sequence. 39 | // b) '*' character matches with ith 40 | // character in input 41 | if (pattern.charAt(j - 1) == '*') 42 | lookup[i][j] = lookup[i][j - 1] 43 | || lookup[i - 1][j]; 44 | 45 | // Current characters are considered as 46 | // matching in two cases 47 | // (a) current character of pattern is '?' 48 | // (b) characters actually match 49 | else if (pattern.charAt(j - 1) == '?' 50 | || str.charAt(i - 1) 51 | == pattern.charAt(j - 1)) 52 | lookup[i][j] = lookup[i - 1][j - 1]; 53 | 54 | // If characters don't match 55 | else 56 | lookup[i][j] = false; 57 | } 58 | } 59 | 60 | return lookup[n][m]; 61 | } 62 | 63 | 64 | // Driver code 65 | public static void main(String args[]) 66 | { 67 | String str = "baaabab"; 68 | String pattern = "*****ba*****ab"; 69 | // String pattern = "ba*****ab"; 70 | // String pattern = "ba*ab"; 71 | // String pattern = "a*ab"; 72 | // String pattern = "a*****ab"; 73 | // String pattern = "*a*****ab"; 74 | // String pattern = "ba*ab****"; 75 | // String pattern = "****"; 76 | // String pattern = "*"; 77 | // String pattern = "aa?ab"; 78 | // String pattern = "b*b"; 79 | // String pattern = "a*a"; 80 | // String pattern = "baaabab"; 81 | // String pattern = "?baaabab"; 82 | // String pattern = "*baaaba*"; 83 | 84 | if (strmatch(str, pattern, str.length(), 85 | pattern.length())) 86 | System.out.println("Yes"); 87 | else 88 | System.out.println("No"); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /bubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool compare(int a,int b){ 6 | return a > b; 7 | } 8 | 9 | //Sort the elements in increasing order 10 | void bubble_sort_rec(int a[],int n){ 11 | 12 | //base case 13 | if(n==1){ 14 | return; 15 | } 16 | 17 | for(int j=0;ja[j+1]){ 19 | swap(a[j],a[j+1]); 20 | } 21 | } 22 | bubble_sort_rec(a,n-1); 23 | } 24 | 25 | void bubble_sort_rec_2(int a[],int n,int j){ 26 | 27 | //base case 28 | if(n==1 || n==0){ 29 | return; 30 | } 31 | if(j==n-1){ 32 | //reduce the problem size, and reset j to 0 33 | bubble_sort_rec_2(a,n-1,0); 34 | return; 35 | } 36 | if(a[j]>a[j+1]){ 37 | swap(a[j],a[j+1]); 38 | } 39 | bubble_sort_rec_2(a,n,j+1); 40 | } 41 | 42 | 43 | 44 | 45 | int main(){ 46 | int arr[] = {-2,3,4,-1,5,-12,6,1,3}; 47 | int n = sizeof(arr)/sizeof(int); 48 | 49 | bubble_sort_rec_2(arr,n,0); 50 | 51 | for(int i=0;i> dp(d + 1, vector(target + 1, 0)); 7 | dp[0][0] = 1; 8 | for (int i = 1; i <= d; i++) 9 | { 10 | for (int j = 1; j <= target; j++) 11 | { 12 | for (int k = 1; k <= f; k++) 13 | { 14 | if (k <= j) 15 | dp[i][j] = ((dp[i][j] % m) + (dp[i - 1][j - k] % m)) % m; 16 | } 17 | } 18 | } 19 | return dp[d][target]; 20 | } -------------------------------------------------------------------------------- /editdistance.txt: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDistance(string word1, string word2) 4 | { 5 | int n = word1.size(),m = word2.size(); 6 | vector> dp(n+1,vector(m+1)); 7 | for(int i=0;i<=n;i++) 8 | dp[i][0]=i; 9 | for(int i=0;i<=m;i++) 10 | dp[0][i]=i; 11 | for(int i=1;i<=n;i++) 12 | { 13 | for(int j=1;j<=m;j++) 14 | { 15 | if(word1[i-1]==word2[j-1]) 16 | dp[i][j]=dp[i-1][j-1]; 17 | else 18 | { 19 | dp[i][j]=min(min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1])+1; 20 | } 21 | 22 | } 23 | } 24 | return dp[n][m]; 25 | } 26 | }; -------------------------------------------------------------------------------- /longest_palindrome_subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | string lps(int n,int m, string &s1,){ 5 | string s2 = s1; 6 | reverse(s2.begin(),s2.end()); 7 | vector>dp(n+1,vector(m+1,0)); 8 | 9 | for (int ind1 = 1; ind1 <= n; ind1++) 10 | { 11 | for (int ind2 = 1; ind2 <= m; ind2++) 12 | { 13 | if (s1[ind1 - 1] == s2[ind2 - 1]) 14 | dp[ind1][ind2] = 1 + dp[ind1 - 1][ind2 - 1]; 15 | else 16 | dp[ind1][ind2] = max(dp[ind1 - 1][ind2], dp[ind1][ind2 - 1]); 17 | } 18 | } 19 | 20 | int i=n; 21 | int j=m; 22 | 23 | string ans=""; 24 | while(j>0&&i>0){ 25 | if(s1[i-1]==s2[j-1]){ 26 | ans = s1[i-1]+ans; 27 | i--; 28 | j--; 29 | } 30 | else if(dp[i-1][j]>dp[i][j-1])i--; 31 | else j--; 32 | } 33 | 34 | return ans; 35 | 36 | // return dp[n][m]; -> will return the length of longest palindrome subsequence 37 | } 38 | 39 | int main() 40 | { 41 | string s1 = "bbabcbcab"; 42 | string s2 = s1; 43 | reverse(s2.begin(),s2.end()); 44 | cout << "The Longest Common Subsequence is: " << lps(s1.size(), s2.size(), s1, s2) << endl; 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /matrix chain multiplocation.txt: -------------------------------------------------------------------------------- 1 | // Initial Template for C++ 2 | 3 | #include 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | // User function Template for C++ 8 | 9 | class Solution{ 10 | public: 11 | int dp[110][110]; 12 | 13 | int help(int arr[] , int i , int j) 14 | { 15 | if(i == j ) 16 | return 0; 17 | 18 | if(dp[i][j] != -1) 19 | return dp[i][j]; 20 | 21 | int ans = INT_MAX; 22 | 23 | for(int k = i; k < j ; k++) 24 | { 25 | int t = help(arr, i , k) ; 26 | int t3 =help( arr, k+1 , j) ; 27 | 28 | dp[i][k] = t; 29 | dp[k+1][j] = t3; 30 | 31 | int t1 = arr[i-1]*arr[k]*arr[j]; 32 | 33 | t += t1; 34 | t += t3; 35 | 36 | ans = min(ans, t); 37 | 38 | } 39 | return dp[i][j] = ans; 40 | 41 | } 42 | 43 | int matrixMultiplication(int N, int arr[]) 44 | { 45 | // code here 46 | memset(dp,-1,sizeof(dp)); 47 | return help(arr,1,N-1); 48 | } 49 | }; 50 | 51 | //{ Driver Code Starts. 52 | 53 | int main(){ 54 | int t; 55 | cin>>t; 56 | while(t--){ 57 | int N; 58 | cin>>N; 59 | int arr[N]; 60 | for(int i = 0;i < N;i++) 61 | cin>>arr[i]; 62 | 63 | Solution ob; 64 | cout< 4 | using namespace std; 5 | 6 | // Merge two subarrays L and M into arr 7 | void merge(int arr[], int p, int q, int r) { 8 | 9 | // Create L ← A[p..q] and M ← A[q+1..r] 10 | int n1 = q - p + 1; 11 | int n2 = r - q; 12 | 13 | int L[n1], M[n2]; 14 | 15 | for (int i = 0; i < n1; i++) 16 | L[i] = arr[p + i]; 17 | for (int j = 0; j < n2; j++) 18 | M[j] = arr[q + 1 + j]; 19 | 20 | // Maintain current index of sub-arrays and main array 21 | int i, j, k; 22 | i = 0; 23 | j = 0; 24 | k = p; 25 | 26 | // Until we reach either end of either L or M, pick larger among 27 | // elements L and M and place them in the correct position at A[p..r] 28 | while (i < n1 && j < n2) { 29 | if (L[i] <= M[j]) { 30 | arr[k] = L[i]; 31 | i++; 32 | } else { 33 | arr[k] = M[j]; 34 | j++; 35 | } 36 | k++; 37 | } 38 | 39 | // When we run out of elements in either L or M, 40 | // pick up the remaining elements and put in A[p..r] 41 | while (i < n1) { 42 | arr[k] = L[i]; 43 | i++; 44 | k++; 45 | } 46 | 47 | while (j < n2) { 48 | arr[k] = M[j]; 49 | j++; 50 | k++; 51 | } 52 | } 53 | 54 | // Divide the array into two subarrays, sort them and merge them 55 | void mergeSort(int arr[], int l, int r) { 56 | if (l < r) { 57 | // m is the point where the array is divided into two subarrays 58 | int m = l + (r - l) / 2; 59 | 60 | mergeSort(arr, l, m); 61 | mergeSort(arr, m + 1, r); 62 | 63 | // Merge the sorted subarrays 64 | merge(arr, l, m, r); 65 | } 66 | } 67 | 68 | // Print the array 69 | void printArray(int arr[], int size) { 70 | for (int i = 0; i < size; i++) 71 | cout << arr[i] << " "; 72 | cout << endl; 73 | } 74 | 75 | // Driver program 76 | int main() { 77 | int arr[] = {6, 5, 12, 10, 9, 1}; 78 | int size = sizeof(arr) / sizeof(arr[0]); 79 | 80 | mergeSort(arr, 0, size - 1); 81 | 82 | cout << "Sorted array: \n"; 83 | printArray(arr, size); 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /no_subset_of_given_difference.cpp: -------------------------------------------------------------------------------- 1 | // C++ code to implement the approach 2 | 3 | #include 4 | using namespace std; 5 | int dp[1001][10000]; 6 | // // top-down approach 7 | // void solve(vector& vp, int n,int sum) 8 | // { 9 | // int i,j,k; 10 | // // initialization 11 | // for(i=0;i<=sum;i++) 12 | // { 13 | // dp[0][i]=0; 14 | // } 15 | // for(i=0;i<=n;i++) 16 | // { 17 | // dp[i][0]=1; 18 | // } 19 | 20 | // // calculation 21 | // for(i=1;i<=n;i++) 22 | // { 23 | // for(j=1;j<=sum;j++) 24 | // { 25 | // if(j& vp, int n, int diff) 40 | // { 41 | // int i,j,k,sum=0,count=0; 42 | // if((diff+sum)%2) 43 | // return 0; 44 | // for(auto itr:vp) 45 | // sum+=itr; 46 | // solve(vp,n,sum); 47 | 48 | // return dp[n][(diff+sum)/2]; 49 | 50 | 51 | // } 52 | 53 | // Driver code 54 | // memoization 55 | int solve(vector& vp, int n,int cap ) 56 | { 57 | int i,j,k; 58 | // base conditions 59 | if(cap==0) 60 | return 1; 61 | if(n==0) 62 | return 0; 63 | // pre calculated 64 | if(dp[n][cap]!=-1) 65 | return dp[n][cap]; 66 | 67 | // main function call 68 | if(vp[n-1]>cap) 69 | return dp[n][cap]=solve(vp,n-1,cap); 70 | else 71 | return dp[n][cap]=solve(vp,n-1,cap)+solve(vp,n-1,cap-vp[n-1]); 72 | 73 | } 74 | 75 | int countSub(vector& vp, int n, int diff) 76 | { 77 | int i,j,k,sum=0,count=0; 78 | 79 | for(auto itr:vp) 80 | sum+=itr; 81 | if((diff+sum)%2) 82 | return 0; 83 | memset(dp,-1,sizeof(dp)); 84 | solve(vp,n,(diff+sum)/2); 85 | for(i=0;i<=n;i++) 86 | { 87 | for(j=0;j<=sum;j++) 88 | cout< arr = { 1, 2, 3, 1, 2 }; 100 | int diff = 1; 101 | 102 | 103 | // Function call 104 | cout << countSub(arr, N, diff) << endl; 105 | return 0; 106 | } 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- /reverseofanumber.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | /** 4 | * Simple Java program to reverse a number in Java using loop and operator 5 | * This program also shows example of using division operator(/) and Remainder Operator(%) 6 | */ 7 | public class ReverseNumberExample { 8 | 9 | public static void main(String args[]) { 10 | //input number to reverse 11 | System.out.println("Please enter number to be reversed using Java program: "); 12 | int number = new Scanner(System.in).nextInt(); 13 | 14 | int reverse = reverse(number); 15 | System.out.println("Reverse of number: " + number + " is " + reverse(number)); 16 | 17 | } 18 | 19 | /* 20 | * reverse a number in Java using iteration 21 | * @return reverse of number 22 | */ 23 | public static int reverse(int number){ 24 | int reverse = 0; 25 | int remainder = 0; 26 | do{ 27 | remainder = number%10; 28 | reverse = reverse*10 + remainder; 29 | number = number/10; 30 | 31 | }while(number > 0); 32 | 33 | return reverse; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /sorted_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isSorted(int arr[],int n){ 5 | //base case 6 | if(n==1 or n==0){ 7 | return true; 8 | } 9 | //rec case 10 | if(arr[0] 14 | #include 15 | #include 16 | using namespace std; 17 | void sqrt_decomposition(vector&v1,int arr[],int n,int len) 18 | { 19 | int i,j,k; 20 | for(i=0;i>n; 32 | int arr[n]; 33 | len=(int)sqrt(n); 34 | cout<v1(len); 38 | cout<<"Enter the value of elements\n"; 39 | for(i=0;i>arr[i]; 41 | sqrt_decomposition(v1,arr,n,len); 42 | cout<<"Enter the number of queries\n"; 43 | cin>>q; 44 | while(q--) 45 | { 46 | int a,b,sum=0; 47 | cout<<"Enter the range of the query\n"; 48 | cin>>a>>b; 49 | a--,b--; 50 | for(i=a;i<=b;) 51 | { 52 | if(i%len==0 && i+len-1<=b) 53 | { 54 | sum+=v1[i/len]; 55 | i+=len; 56 | } 57 | else 58 | { 59 | sum+=arr[i]; 60 | i++; 61 | 62 | } 63 | } 64 | cout< dir = {-1, 0, 1, 0, -1}; 5 | 6 | int solve(vector>& grid, int i, int j, int cnt) 7 | { 8 | if (i < 0 || j < 0 || i >= grid.size() || j >= grid[0].size() || grid[i][j] == -1) 9 | { 10 | return 0; 11 | } 12 | if (grid[i][j] == 2) 13 | { 14 | if (cnt == 0) 15 | { 16 | return 1; 17 | } 18 | return 0; 19 | } 20 | if (cnt == 0 && grid[i][j] != 1) 21 | { 22 | return 0; 23 | } 24 | int ans = 0; 25 | if (grid[i][j] == 0) 26 | { 27 | grid[i][j] = -1; 28 | for (int k = 0; k < 4; k++) 29 | { 30 | ans = ans + solve(grid, i + dir[k], j + dir[k + 1], cnt - 1); 31 | } 32 | grid[i][j] = 0; 33 | } 34 | else 35 | { 36 | grid[i][j] = -1; 37 | for (int k = 0; k < 4; k++) 38 | { 39 | ans = ans + solve(grid, i + dir[k], j + dir[k + 1], cnt); 40 | } 41 | grid[i][j] = 1; 42 | } 43 | return ans; 44 | } 45 | 46 | int uniquePathsIII(vector>& grid) { 47 | 48 | int cnt = 0, m = grid.size(), n = grid[0].size(); 49 | 50 | for (int i = 0; i < m; i++) 51 | { 52 | for (int j = 0; j < n; j++) 53 | { 54 | if (grid[i][j] == 0) 55 | { 56 | cnt++; 57 | } 58 | } 59 | } 60 | 61 | for (int i = 0; i < m; i++) 62 | { 63 | for (int j = 0; j < n; j++) 64 | { 65 | if (grid[i][j] == 1) 66 | { 67 | return solve(grid, i, j, cnt); 68 | } 69 | } 70 | } 71 | return 0; 72 | } 73 | }; 74 | --------------------------------------------------------------------------------