├── BucketSort.java ├── Closest pair of points - Geometrical Algorithms ├── Count_Occurences_of_Anagrams.cpp ├── Count_Steps_To_one.cpp ├── Dijkstra.java ├── Edit_Distance.cpp ├── Greatest_common_divisor_Algo.cpp ├── HeapSort.java ├── Merge-intervals.cpp ├── MergeSort.java ├── Minimum_Cost.cpp ├── Number_of_islands.cpp ├── Parenthesis-Checker.cpp ├── Peak_element.cpp ├── PowerOfFour.java ├── QuickSort.java ├── README.md ├── RadixSort.java ├── Rat_in_a_maze_Backtracking.cpp ├── Stringcompression.java ├── allocate_minimum_number_of_pages.java ├── almostSorted.cpp ├── carRefueling.cpp ├── divison.cpp ├── fibonacci.cpp ├── floyd_warshall.java ├── gcd.cpp ├── if_else.cpp ├── insertionSortUsingArray.c ├── insertionsort ├── kadane's_algorithm.cpp ├── kthElement.cpp ├── linear_search.cpp ├── linked list using two variables.c ├── matrix multiplication.c ├── matrix_chain_multiplication.java ├── minSpanningTree.py ├── moneyChange.cpp ├── multiply.cpp ├── oneaway.java ├── palindrome_partitioning.java ├── quickSort.cpp ├── reverse_LinkedList.java ├── staircase.cpp ├── string.cpp ├── string2.cpp ├── swap.java ├── switch_statement.cpp ├── top.cpp ├── urlify.java └── zeromatrix.java /BucketSort.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class BucketSort { 4 | public static void main(String args[]) { 5 | 6 | Random rand = new Random(); 7 | int size=6000; 8 | 9 | float[] a = new float[size]; 10 | 11 | double start,duration; 12 | 13 | for (int i = 0; i < size; i++) { 14 | 15 | a[i] = rand.nextFloat(); 16 | } 17 | 18 | start = System.nanoTime(); 19 | bucketsort(a, size); 20 | duration = (System.nanoTime() - start)/1000.0; 21 | 22 | System.out.println("Time is "+ duration); 23 | } 24 | 25 | static void bucketsort(float a[], int size) { 26 | 27 | @SuppressWarnings("unchecked") 28 | ArrayList[] b = new ArrayList[size]; 29 | 30 | for (int i = 0; i < size; i++) { 31 | b[i] = new ArrayList(); 32 | } 33 | 34 | for (int i = 0; i < size; i++) { 35 | int index = (int) (a[i]*size); 36 | b[index].add(a[i]); 37 | } 38 | 39 | for (int i = 0; i < size; i++) { 40 | Collections.sort(b[i]); 41 | } 42 | int index1 = 0; 43 | for (int i = 0; i < size; i++) { 44 | for (int j = 0; j < b[i].size(); j++) { 45 | a[index1++] = b[i].get(j); 46 | } 47 | } 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /Closest pair of points - Geometrical Algorithms: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | struct Point 9 | { 10 | int x, y; 11 | }; 12 | 13 | int compareX(const void* a, const void* b) 14 | { 15 | Point *p1 = (Point *)a, *p2 = (Point *)b; 16 | return (p1->x != p2->x) ? (p1->x - p2->x) : (p1->y - p2->y); 17 | } 18 | 19 | int compareY(const void* a, const void* b) 20 | { 21 | Point *p1 = (Point *)a, *p2 = (Point *)b; 22 | return (p1->y != p2->y) ? (p1->y - p2->y) : (p1->x - p2->x); 23 | } 24 | 25 | 26 | float dist(Point p1, Point p2) 27 | { 28 | return sqrt( (p1.x - p2.x)*(p1.x - p2.x) + 29 | (p1.y - p2.y)*(p1.y - p2.y) 30 | ); 31 | } 32 | 33 | float bruteForce(Point P[], int n) 34 | { 35 | float min = FLT_MAX; 36 | for (int i = 0; i < n; ++i) 37 | for (int j = i+1; j < n; ++j) 38 | if (dist(P[i], P[j]) < min) 39 | min = dist(P[i], P[j]); 40 | return min; 41 | } 42 | 43 | float min(float x, float y) 44 | { 45 | return (x < y)? x : y; 46 | } 47 | 48 | 49 | float stripClosest(Point strip[], int size, float d) 50 | { 51 | float min = d; // Initialize the minimum distance as d 52 | 53 | 54 | for (int i = 0; i < size; ++i) 55 | for (int j = i+1; j < size && (strip[j].y - strip[i].y) < min; ++j) 56 | if (dist(strip[i],strip[j]) < min) 57 | min = dist(strip[i], strip[j]); 58 | 59 | return min; 60 | } 61 | 62 | float closestUtil(Point Px[], Point Py[], int n) 63 | { 64 | 65 | if (n <= 3) 66 | return bruteForce(Px, n); 67 | 68 | 69 | int mid = n/2; 70 | Point midPoint = Px[mid]; 71 | 72 | 73 | Point Pyl[mid]; 74 | Point Pyr[n-mid]; 75 | int li = 0, ri = 0; 76 | for (int i = 0; i < n; i++) 77 | { 78 | if ((Py[i].x < midPoint.x || (Py[i].x == midPoint.x && Py[i].y < midPoint.y)) && li 2 | using namespace std; 3 | #define IO() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) 4 | 5 | int main(){ 6 | 7 | IO(); 8 | 9 | string s = "forxxorfxdofr"; 10 | string anagram = "for"; 11 | int k = anagram.size(); 12 | 13 | unordered_map m; 14 | for(auto &i: anagram){ 15 | m[i]++; 16 | } 17 | int count = m.size(); 18 | 19 | // vector v; 20 | 21 | int i = 0, j = 0, ans = 0; 22 | while(j < s.size()){ 23 | // calculations 24 | if(m.find(s[j]) != m.end()){ 25 | m[s[j]]--; 26 | if(m[s[j]] == 0) count--; 27 | } 28 | 29 | if(j - i + 1 < k ) j++; 30 | 31 | else if(j - i + 1 == k){ 32 | // take answer from calculations 33 | if(count == 0) { 34 | ans++; 35 | // v.push_back(i); 36 | } 37 | 38 | if(m.find(s[i]) != m.end()){ 39 | m[s[i]]++; 40 | if(m[s[i]] == 1) count++; 41 | } 42 | //slide the window 43 | i++; j++; 44 | } 45 | } 46 | cout< 2 | using namespace std; 3 | 4 | //This is brute force 5 | int countMinStepsToOne(int n) 6 | { 7 | if(n<=1) 8 | return 0; 9 | int a=countMinStepsToOne(n-1)+1; 10 | int b=INT_MAX; 11 | if(n%2==0) 12 | b=countMinStepsToOne(n/2)+1; 13 | a=min(a,b); 14 | int c=INT_MAX; 15 | if(n%3==0) 16 | c=countMinStepsToOne(n/3)+1; 17 | a=min(a,c); 18 | return a; 19 | } 20 | 21 | //This is memoization 22 | int helper(int n,int *arr) 23 | { 24 | if(n==1) 25 | return 0; 26 | if(arr[n]!=-1) 27 | return arr[n]; 28 | int a=helper(n-1,arr)+1; 29 | int b=INT_MAX; 30 | if(n%2==0) 31 | b=helper(n/2,arr)+1; 32 | a=min(a,b); 33 | int c=INT_MAX; 34 | if(n%3==0) 35 | c=helper(n/3,arr)+1; 36 | arr[n]=min(a,c); 37 | return arr[n]; 38 | } 39 | int countMinStepsToOne(int n) 40 | { 41 | int *arr=new int[n+1]; 42 | for(int i=0;i 11 | using namespace std; 12 | int editDistance(string s1, string s2) 13 | { 14 | 15 | if(s1.empty()) 16 | return s2.size(); 17 | if(s2.empty()) 18 | return s1.size(); 19 | 20 | if(s1==s2) 21 | return 0; 22 | 23 | int answer=0; 24 | if(s1[0]!=s2[0]) 25 | { 26 | int a=editDistance(s1.substr(1),s2); 27 | int b=editDistance(s1,s2.substr(1)); 28 | int c=editDistance(s1.substr(1),s2.substr(1)); 29 | answer=min(a,min(b,c))+1; 30 | } 31 | else 32 | { 33 | answer= editDistance(s1.substr(1),s2.substr(1)); 34 | } 35 | return answer; 36 | 37 | } 38 | 39 | //Better brute force, is to avoid using substr method 40 | int min(int x, int y, int z) { return min(min(x, y), z); } 41 | 42 | int editDist(string str1, string str2, int m, int n) 43 | { 44 | if (m == 0) 45 | return n; 46 | 47 | 48 | if (n == 0) 49 | return m; 50 | 51 | if (str1[m - 1] == str2[n - 1]) 52 | return editDist(str1, str2, m - 1, n - 1); 53 | 54 | 55 | return 1 56 | + min(editDist(str1, str2, m, n - 1), // Insert 57 | editDist(str1, str2, m - 1, n), // Remove 58 | editDist(str1, str2, m - 1, 59 | n - 1) // Replace 60 | ); 61 | } 62 | 63 | 64 | //*Memoization 65 | 66 | 67 | int helper(string s1,string s2,int **arr) 68 | { 69 | int m=s1.size(); 70 | int n=s2.size(); 71 | if(s1.empty()) 72 | return s2.size(); 73 | if(s2.empty()) 74 | return s1.size(); 75 | if(s1==s2) 76 | return 0; 77 | if(arr[m][n]!=-1) 78 | return arr[m][n]; 79 | if(s1[0]!=s2[0]) 80 | { 81 | int a,b,c; 82 | a=helper(s1.substr(1),s2,arr); 83 | b=helper(s1,s2.substr(1),arr); 84 | c=helper(s1.substr(1),s2.substr(1),arr); 85 | arr[m][n]= min(a,min(b,c))+1; 86 | } 87 | else 88 | arr[m][n]=helper(s1.substr(1),s2.substr(1),arr); 89 | return arr[m][n]; 90 | } 91 | 92 | 93 | int editDistance(string s1, string s2) 94 | { 95 | int m=s1.size(); 96 | int n=s2.size(); 97 | int i,j; 98 | int **arr=new int*[m+1]; 99 | for(i=0;i<=m;i++) 100 | arr[i]=new int[n+1]; 101 | for(i=0;i<=m;i++) 102 | for(j=0;j<=n;j++) 103 | arr[i][j]=-1; 104 | return helper(s1,s2,arr); 105 | 106 | } 107 | 108 | 109 | 110 | 111 | 112 | // * Solution through DP 113 | 114 | int editDistance(string s, string t) { 115 | int m,n,i,j; 116 | m=s.size(); 117 | n=t.size(); 118 | vector> dp(m+1,vector(n+1)); 119 | 120 | 121 | dp[0][0]=0; 122 | 123 | for(i=1;i<=n;i++) 124 | dp[0][i]=i; 125 | 126 | for(i=1;i<=m;i++) 127 | dp[i][0]=i; 128 | 129 | for(i=1;i<=m;i++){ 130 | for(j=1;j<=n;j++){ 131 | 132 | if(s[i-1]==t[j-1]) 133 | dp[i][j]=dp[i-1][j-1]; 134 | else{ 135 | int a = dp[i-1][j]; 136 | int b = dp[i][j-1]; 137 | int c = dp[i-1][j-1]; 138 | dp[i][j]=min(a,min(b,c))+1; 139 | } 140 | } 141 | } 142 | return dp[m][n]; 143 | } 144 | 145 | 146 | -------------------------------------------------------------------------------- /Greatest_common_divisor_Algo.cpp: -------------------------------------------------------------------------------- 1 | // Greatest Common Divisor Algorithm in recursive approach 2 | 3 | #include 4 | using namespace std; 5 | 6 | int gcd(int firstNum, int secondNum) { 7 | // firstNum%secondNum = secondNum; 8 | // when secondNum divides completely to the firstNum 9 | // firstNum%secondNum = remainder 10 | if(firstNum%secondNum==0){ 11 | return secondNum; 12 | } 13 | return gcd(secondNum,firstNum%secondNum); 14 | } 15 | 16 | int main(){ 17 | int firstnum , secondnum; 18 | cin>>firstnum>>secondnum; 19 | cout<= 0; i--) 8 | heapify(arr, n, i); 9 | 10 | for (int i=n-1; i>0; i--) 11 | { 12 | int temp = arr[0]; 13 | arr[0] = arr[i]; 14 | arr[i] = temp; 15 | heapify(arr, i, 0); 16 | } 17 | } 18 | 19 | 20 | public static void heapify(int arr[], int n, int i) 21 | { 22 | int largest = i; 23 | int l = 2*i + 1; 24 | int r = 2*i + 2; 25 | 26 | if (l < n && arr[l] > arr[largest]) 27 | largest = l; 28 | 29 | if (r < n && arr[r] > arr[largest]) 30 | largest = r; 31 | 32 | if (largest != i) 33 | { 34 | int swap = arr[i]; 35 | arr[i] = arr[largest]; 36 | arr[largest] = swap; 37 | heapify(arr, n, largest); 38 | } 39 | } 40 | 41 | 42 | public static void printArray(int arr[]) 43 | { 44 | int n = arr.length; 45 | for (int i=0; i 2 | 3 | using namespace std; 4 | 5 | vector < vector < int >> merge(vector < vector < int >> & intervals) { 6 | vector < vector < int >> ans; 7 | vector < pair < int, int > > s; 8 | stack < pair < int, int >> stack; 9 | 10 | for (int i = 0; i < intervals.size(); i++) { 11 | s.push_back(make_pair(intervals[i][0], intervals[i][1])); 12 | } 13 | 14 | sort(s.begin(), s.end()); 15 | stack.push(s[0]); 16 | for (int i = 0; i < intervals.size(); i++) { 17 | auto it = stack.top(); 18 | if (it.second >= s[i].first) { 19 | stack.pop(); 20 | if (it.second < s[i].second) 21 | stack.push(make_pair(it.first, s[i].second)); 22 | else 23 | stack.push(make_pair(it.first, it.second)); 24 | } else 25 | stack.push(make_pair(s[i].first, s[i].second)); 26 | } 27 | while (stack.empty() != true) { 28 | auto it = stack.top(); 29 | ans.push_back({ 30 | it.first, 31 | it.second 32 | }); 33 | stack.pop(); 34 | } 35 | return ans; 36 | } 37 | int main() { 38 | vector < vector < int >> v; 39 | v.push_back({ 40 | 6, 41 | 8 42 | }); 43 | v.push_back({ 44 | 1, 45 | 9 46 | }); 47 | v.push_back({ 48 | 2, 49 | 4 50 | }); 51 | v.push_back({ 52 | 4, 53 | 7 54 | }); 55 | v = merge(v); 56 | for (int i = 0; i < v.size(); i++) { 57 | cout << v[i][0] << " " << v[i][1] << endl; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /MergeSort.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MergeSort{ 4 | public static void mergesort(int p,int q,int [] arr) 5 | { 6 | if(p 3 | using namespace std; 4 | 5 | int helper(int **input,int m,int n,int i,int j) 6 | { 7 | if(i>=m||j>=n) 8 | return INT_MAX; 9 | if(i==m-1&&j==n-1) 10 | return input[i][j]; 11 | int a1=helper(input,m,n,i,j+1); 12 | int a2=helper(input,m,n,i+1,j); 13 | int a3=helper(input,m,n,i+1,j+1); 14 | return min(a1,min(a2,a3))+input[i][j]; 15 | } 16 | 17 | 18 | 19 | int minCostPath(int **input, int m, int n) 20 | { 21 | return helper(input,m,n,0,0); 22 | } 23 | 24 | 25 | 26 | // This is memoization 27 | 28 | 29 | 30 | int helper(int **input,int m,int n,int i,int j,int **arr) 31 | { 32 | if(i>=m||j>=n) 33 | return INT_MAX; 34 | if(i==m-1&&j==n-1) 35 | return input[i][j]; 36 | if(arr[i][j]!=-1) 37 | return arr[i][j]; 38 | int a1=helper(input,m,n,i,j+1,arr); 39 | int a2=helper(input,m,n,i+1,j,arr); 40 | int a3=helper(input,m,n,i+1,j+1,arr); 41 | arr[i][j]=min(a1,min(a2,a3))+input[i][j]; 42 | return arr[i][j]; 43 | } 44 | 45 | 46 | - 47 | int minCostPath(int **input, int m, int n) 48 | { 49 | int **arr; 50 | arr = new int *[m+1]; 51 | for (int i = 0; i <= m; i++) 52 | { 53 | arr[i] = new int[n+1]; 54 | } 55 | for(int i=0;i<=m;i++) 56 | { 57 | for(int j=0;j<=n;j++) 58 | arr[i][j]=-1; 59 | } 60 | return helper(input,m,n,0,0,arr); 61 | } 62 | 63 | 64 | //Now taking the solution through DP 65 | 66 | 67 | int minCostPath(int **input,int m,int n) 68 | { 69 | int **arr; 70 | arr=new int*[m+1]; 71 | for(int i=0;i<=m;i++) 72 | arr[i]=new int[n+1]; 73 | for(int i=0;i<=m;i++) 74 | { 75 | for(int j=0;j<=n;j++) 76 | arr[i][j]=INT_MAX; 77 | } 78 | arr[m][n]=input[m-1][n-1]; 79 | for(int i=m;i>=1;i--) 80 | { 81 | for(int j=n;j>=1;j--) 82 | { 83 | int a,b,c; 84 | a=b=c=INT_MAX; 85 | //Initially took them as infinity, As infinity represents the index is not valid,so u cannot go there, and its value will be updated if we can go to a particular index 86 | if(i+1<=m&&j+1<=n) 87 | { 88 | a=arr[i+1][j+1]; 89 | b=arr[i][j+1]; 90 | c=arr[i+1][j]; 91 | } 92 | else if(i+1<=m) 93 | { 94 | c=arr[i+1][j]; 95 | } 96 | else if (j+1<=n) 97 | { 98 | b=arr[i][j+1]; 99 | } 100 | //Reason of this if statement is that we are initializing arr[m][n] and we don't want to initialize again,further if we do it then we will get wrong answer,bcoz this is the value which we are using for other ones, but if we try to initialize it then there is no value to use for it,SO a,b,c all will be INT_MAX 101 | if(i==m&&j==n) 102 | continue; 103 | arr[i][j]=min(a,min(b,c))+input[i-1][j-1]; 104 | } 105 | } 106 | return arr[1][1]; 107 | 108 | } -------------------------------------------------------------------------------- /Number_of_islands.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Given a grid consisting of '0's(Water) and '1's(Land). Find the number of islands. 5 | // An island is surrounded by water and is formed by connecting adjacent lands 6 | // horizontally or vertically or diagonally i.e., in all 8 directions. 7 | 8 | 9 | class Solution 10 | { 11 | 12 | 13 | void mark(vector> &matrix,int x,int y,int r,int c) 14 | { 15 | if(x<0 || x>=r || y<0 || y>=c || matrix[x][y]!='1') 16 | return; 17 | 18 | 19 | matrix[x][y] = '2'; 20 | 21 | 22 | mark(matrix,x+1,y,r,c); 23 | mark(matrix,x,y+1,r,c); 24 | mark(matrix,x-1,y,r,c); 25 | mark(matrix,x,y-1,r,c); 26 | mark(matrix,x-1,y-1,r,c); 27 | mark(matrix,x+1,y+1,r,c); 28 | mark(matrix,x+1,y-1,r,c); 29 | mark(matrix,x-1,y+1,r,c); 30 | 31 | } 32 | public: 33 | 34 | int numIslands(vector>& grid) 35 | { 36 | 37 | int row=grid.size(); 38 | if(row==0) 39 | return 0; 40 | int col=grid[0].size(); 41 | 42 | 43 | int ans=0; 44 | for(int i=0;i> tc; 64 | while(tc--){ 65 | int n, m; 66 | cin >> n >> m; 67 | vector>grid(n, vector(m, '#')); 68 | for(int i = 0; i < n; i++){ 69 | for(int j = 0; j < m; j++){ 70 | cin >> grid[i][j]; 71 | } 72 | } 73 | Solution obj; 74 | int ans = obj.numIslands(grid); 75 | cout << ans <<'\n'; 76 | } 77 | return 0; 78 | } -------------------------------------------------------------------------------- /Parenthesis-Checker.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | // Given an expression string x. Examine whether the pairs and the orders of “{“,”}”,”(“,”)”,”[“,”]” are correct in exp. 7 | // For example, the function should return 'true' for exp = “[()]{}{[()()]()}” and 'false' for exp = “[(])”. 8 | bool ispar(string expr) 9 | { 10 | 11 | stack s; 12 | char x; 13 | 14 | for (int i = 0; i < expr.length(); i++) 15 | { 16 | if (expr[i] == '(' || expr[i] == '[' 17 | || expr[i] == '{') 18 | { 19 | s.push(expr[i]); 20 | continue; 21 | } 22 | 23 | if (s.empty()) 24 | return false; 25 | 26 | switch (expr[i]) { 27 | case ')': 28 | 29 | x = s.top(); 30 | s.pop(); 31 | if (x == '{' || x == '[') 32 | return false; 33 | break; 34 | 35 | case '}': 36 | 37 | x = s.top(); 38 | s.pop(); 39 | if (x == '(' || x == '[') 40 | return false; 41 | break; 42 | 43 | case ']': 44 | x = s.top(); 45 | s.pop(); 46 | if (x == '(' || x == '{') 47 | return false; 48 | break; 49 | } 50 | } 51 | 52 | 53 | return (s.empty()); 54 | 55 | } 56 | 57 | 58 | 59 | int main() 60 | { 61 | string s; 62 | cin>>s; 63 | 64 | if(ispar(s)) 65 | cout<<"Balanced"< 2 | #include 3 | 4 | using namespace std; 5 | 6 | // A peak element in an array is the one that is not smaller than its neighbours. 7 | // Given an array arr[] of size N, find the index of any one of its peak elements. 8 | 9 | int findPeakUtil(int arr[], int low, int high, int n) 10 | { 11 | int mid = low + (high - low) / 2; 12 | 13 | if ((mid == 0 || arr[mid - 1] <= arr[mid]) && (mid == n - 1 || arr[mid + 1] <= arr[mid])) 14 | return mid; 15 | 16 | else if (mid > 0 && arr[mid - 1] > arr[mid]) 17 | return findPeakUtil(arr, low, (mid - 1), n); 18 | 19 | else 20 | return findPeakUtil(arr, (mid + 1), high, n); 21 | } 22 | 23 | 24 | int peakElement(int arr[], int n) 25 | { 26 | return findPeakUtil(arr, 0, n - 1, n); 27 | 28 | } 29 | 30 | 31 | int main() 32 | { 33 | int n; 34 | cin>>n; 35 | int arr[n]; 36 | for(int i=0;i>arr[i]; 38 | 39 | cout< p) 13 | p = a[i]; 14 | return p; 15 | } 16 | 17 | static void sorting(int a[], int n, int x) 18 | { 19 | int oArray[] = new int[n]; 20 | int count[] = new int[10]; 21 | int i; 22 | Arrays.fill(count,0); 23 | 24 | for (i = 0; i < n; i++) 25 | count[ (a[i]/x)%10 ]++; 26 | 27 | for (i = 1; i < 10; i++) 28 | count[i] += count[i - 1]; 29 | 30 | 31 | for (i = n - 1; i >= 0; i--) 32 | { 33 | oArray[count[ (a[i]/x)%10 ] - 1] = a[i]; 34 | count[ (a[i]/x)%10 ]--; 35 | } 36 | 37 | for (i = 0; i < n; i++) 38 | a[i] = oArray[i]; 39 | } 40 | 41 | static void radixSort(int a[], int n) 42 | { 43 | 44 | int z = findMaximum(a, n); 45 | 46 | for (int x = 1; z/x > 0; x *= 10) 47 | sorting(a, n, x); 48 | } 49 | public static void main(String[] args) { 50 | Scanner scan = new Scanner(System.in); 51 | Random rand = new Random(); 52 | int n; 53 | long a1=0,b1=0,c1=0; 54 | System.out.print(" size of the array : "); 55 | n = scan.nextInt(); 56 | int[] increasingArray = new int[n]; 57 | int[] decreasingArray = new int[n]; 58 | int[] randomArray = new int[n]; 59 | 60 | for(int i=0;i0) 73 | { 74 | for(int i = 0 ; i < n ; i++) 75 | { 76 | a[i] = increasingArray[i]; 77 | b[i] = decreasingArray[i]; 78 | c[i] = randomArray[i]; 79 | } 80 | 81 | long startA = System.nanoTime(); 82 | radixSort(a,n); 83 | long durationA = (System.nanoTime() - startA); 84 | 85 | long startB = System.nanoTime(); 86 | radixSort(b,n); 87 | long durationB = (System.nanoTime() - startB); 88 | 89 | long startC = System.nanoTime(); 90 | radixSort(c,n); 91 | long durationC = (System.nanoTime() - startC); 92 | a1=a1+durationA; 93 | b1=b1+durationB; 94 | c1=c1+durationC; 95 | 96 | System.out.println("The time required for a[] is : " + durationA/1000 + " nanoseconds."); 97 | System.out.println("The time required for b[] is : " + durationB/1000 + " nanoseconds."); 98 | System.out.println("The time required for c[] is : " + durationC/1000 + " nanoseconds."); 99 | k--; 100 | } 101 | System.out.println("\n\n"); 102 | System.out.println("The time required for a[] is : " + a1/10000 + " nanoseconds."); 103 | System.out.println("The time required for b[] is : " + b1/10000 + " nanoseconds."); 104 | System.out.println("The time required for c[] is : " + c1/10000 + " nanoseconds."); 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /Rat_in_a_maze_Backtracking.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | using namespace std; 5 | 6 | // Rat in a Maze Problem (Backtracking) 7 | 8 | // Consider a rat placed at (0, 0) in a square matrix of order N * N. 9 | // It has to reach the destination at (N - 1, N - 1). 10 | // Find all possible paths that the rat can take to reach from source to destination. 11 | // The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). 12 | // Value 0 at a cell in the matrix represents that it is blocked and rat cannot move to it 13 | // while value 1 at a cell in the matrix represents that rat can be travel through it. 14 | 15 | // Input: 16 | // N = 4 17 | // m[][] = {{1, 0, 0, 0}, 18 | // {1, 1, 0, 1}, 19 | // {1, 1, 0, 0}, 20 | // {0, 1, 1, 1}} 21 | // Output: 22 | // DDRDRR DRDDRR 23 | // Explanation: 24 | // The rat can reach the destination at 25 | // (3, 3) from (0, 0) by two paths - DRDDRR 26 | // and DDRDRR, when printed in sorted order 27 | // we get DDRDRR DRDDRR. 28 | 29 | class Solution{ 30 | public: 31 | 32 | void find(vector> &m,string s,int n,int i,int j,vector &ans) 33 | { 34 | if(i==n-1&&j==n-1) 35 | { 36 | ans.push_back(s); 37 | return; 38 | } 39 | if(i>=0&&j>=0&&i findPath(vector> &m, int n) { 53 | 54 | vector ans; 55 | string s; 56 | if(m[0][0]==0||m[n-1][n-1]==0) 57 | return ans; 58 | 59 | find(m,s,n,0,0,ans); 60 | return ans; 61 | 62 | } 63 | }; 64 | 65 | 66 | 67 | 68 | 69 | 70 | int main() { 71 | int t; 72 | cin >> t; 73 | while (t--) { 74 | int n; 75 | cin >> n; 76 | vector> m(n, vector (n,0)); 77 | for (int i = 0; i < n; i++) { 78 | for (int j = 0; j < n; j++) { 79 | cin >> m[i][j]; 80 | } 81 | } 82 | Solution obj; 83 | vector result = obj.findPath(m, n); 84 | if (result.size() == 0) 85 | cout << -1; 86 | else 87 | for (int i = 0; i < result.size(); i++) cout << result[i] << " "; 88 | cout << endl; 89 | } 90 | return 0; 91 | } -------------------------------------------------------------------------------- /Stringcompression.java: -------------------------------------------------------------------------------- 1 | package development; 2 | import java.util.*; 3 | public class Stringcompression { 4 | 5 | public static void main(String[] args) { 6 | Scanner scan = new Scanner(System.in); 7 | String s; 8 | String z =""; 9 | s = scan.next(); 10 | int count=0; 11 | char t = s.charAt(0); 12 | for(int i=0;i0) 22 | { 23 | z = z+s.charAt(s.length()-1)+count; 24 | } 25 | System.out.println(z.length()>s.length()?s:z); 26 | scan.close(); 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /allocate_minimum_number_of_pages.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | class pages { 4 | public static void main (String[] args) { 5 | Scanner sc=new Scanner(System.in); 6 | 7 | int t=sc.nextInt(); 8 | 9 | while(t-->0) 10 | { 11 | int n=sc.nextInt(); 12 | int a[]=new int[n]; 13 | 14 | for(int i=0;imid) 58 | return false; 59 | if(sum+a[i]>mid) 60 | { s++; 61 | sum = a[i]; 62 | if(s>m) 63 | return false; 64 | } 65 | else 66 | sum+= a[i]; 67 | } 68 | 69 | return true; 70 | } 71 | }; -------------------------------------------------------------------------------- /almostSorted.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long int 4 | #define rep(i,j,n) for(ll i=j;ib?a:b;} 9 | void swapv(ll &a,ll &b){a=a+b;b=a-b;a=a-b;} 10 | ll power(ll a, ll b, ll m=mod){ 11 | if(a==0 || b==1) return a; 12 | if(b==0) return 1; 13 | ll ans=1; 14 | while(b>=1){ if(b&1){b--;ans = (ans*a) % m;}a = (a*a)% m;b = b>>1;} 15 | return ans; 16 | } 17 | ll inv(ll a,ll m){return power(a,m-2,m);} 18 | 19 | int main() { 20 | ios_base::sync_with_stdio(false); 21 | cin.tie(NULL); 22 | #ifndef ONLINE_JUDGE 23 | freopen("input.txt", "r", stdin); 24 | freopen("output.txt", "w", stdout); 25 | #endif 26 | ll t=1; 27 | cin>>t; 28 | ll n,k; 29 | while(t--) 30 | { 31 | cin>>n>>k; 32 | ll a = 1<<(n-1); 33 | if(k>a) {cout<<"-1\n";continue;} 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /carRefueling.cpp: -------------------------------------------------------------------------------- 1 | // You are going to travel to another city that is located 𝑑 miles away from your home city. Your car can travel 2 | // at most 𝑚 miles on a full tank and you start with a full tank. Along your way, there are gas stations at 3 | // distances stop1, stop2, . . . , stop𝑛 from your home city. What is the minimum number of refills needed? 4 | #include 5 | using namespace std; 6 | typedef long long int ll; 7 | 8 | int main() 9 | { 10 | ios_base::sync_with_stdio(false); 11 | cin.tie(NULL); 12 | //d is the distance between the starting point and the destination. 13 | //m is the maximum distance the car can travel with a full tank. 14 | //n is the number of refill stops between the starting point and destination. 15 | ll d, m, n; 16 | cin >> d >> m >> n; 17 | //Array a stores the distance (from the starting point) at which the refill stops are located. 18 | ll a[n + 2]; 19 | a[n + 1] = d; //n+1th position is initiallized to the destination, since it will be a stop. 20 | a[0] = 0; //0th position is the position from where we start. 21 | for (ll i = 1; i <= n; i++) 22 | { 23 | cin >> a[i]; 24 | } 25 | ll currRefill = 0; //currRefill stores the index of the array a i.e. where car is standing 26 | ll numRefill = 0; //numRefill counts the number of refills we make on our way to destination 27 | //it is initialized to 0, since we are at the start 28 | ll flag = 0; //flag is used to check if it's impossible to complete the journey 29 | while (currRefill <= n) 30 | { 31 | ll lastRefill = currRefill; 32 | while ((currRefill <= n) //checking if we haven't reached the destination i.e. index of array a <= n 33 | && ((a[currRefill + 1] - a[lastRefill]) <= m)) //checking if he have enough fuel to travel to the further refill stop 34 | currRefill++; //increasing the index of the array a i.e. we are moving to the next refill stop 35 | 36 | if (currRefill == lastRefill) //checking if the previous refill stop is equal to the current refill stop 37 | //if that's the case, then we don't have enough fuel to move to the next stop 38 | //and hence it is impossible to complete the journey 39 | { 40 | flag = 1; //so we turn the flag to the value 1 41 | break; //and break out since no use of iterating further in the array a i.e. we know we can't move further in our journey 42 | } 43 | else if (currRefill <= n) //if we land here, it means we don't have enough fuel to make up to the next refill stop 44 | //so we refill at the current refill stop 45 | numRefill++; //increasing the numRefill since we make refill at the current refill stop 46 | } 47 | if (flag == 0) 48 | cout << numRefill << endl; //printing minimum number of refills made. 49 | else 50 | cout << "-1" << endl; //printing -1 if it's impossible to complete the journey. 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /divison.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int a = 10; 6 | int b = 5; 7 | int c = a/b; 8 | cout< 2 | using namespace std; 3 | 4 | int fib(int n) 5 | { 6 | if (n <= 1) 7 | return n; 8 | return fib(n-1) + fib(n-2); 9 | } 10 | 11 | int main () 12 | { 13 | int n; 14 | cin>>n; 15 | cout << fib(n); 16 | getchar(); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /floyd_warshall.java: -------------------------------------------------------------------------------- 1 | //Initial template for JAVA 2 | 3 | import java.util.*; 4 | import java.lang.*; 5 | import java.io.*; 6 | class floyd_warshall 7 | { 8 | public static void main(String[] args) throws IOException 9 | { 10 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 11 | int T = Integer.parseInt(br.readLine().trim()); 12 | while(T-->0) 13 | { 14 | int n = Integer.parseInt(br.readLine().trim()); 15 | int[][] matrix = new int[n][n]; 16 | for(int i = 0; i < n; i++){ 17 | String[] s = br.readLine().trim().split(" "); 18 | for(int j = 0; j < n; j++) 19 | matrix[i][j] =Integer.parseInt(s[j]); 20 | } 21 | Solution obj = new Solution(); 22 | obj.shortest_distance(matrix); 23 | for(int i = 0; i < n; i++){ 24 | for(int j = 0; j < n; j++){ 25 | System.out.print(matrix[i][j] + " "); 26 | } 27 | System.out.println(); 28 | } 29 | } 30 | } 31 | } 32 | // } Driver Code Ends 33 | 34 | 35 | //User function template for JAVA 36 | 37 | class Solution 38 | { 39 | public void shortest_distance(int[][] matrix) 40 | { int v = matrix.length; 41 | for(int k = 0;k 2 | using namespace std; 3 | 4 | int gcd(int a, int b) 5 | { 6 | if (a == 0) 7 | return b; 8 | return gcd(b % a, a); 9 | } 10 | 11 | int main() 12 | { 13 | int a = 10, b = 15; 14 | cout << "GCD(" << a << ", " 15 | << b << ") = " << gcd(a, b) 16 | << endl; 17 | a = 35, b = 10; 18 | cout << "GCD(" << a << ", " 19 | << b << ") = " << gcd(a, b) 20 | << endl; 21 | a = 31, b = 2; 22 | cout << "GCD(" << a << ", " 23 | << b << ") = " << gcd(a, b) 24 | << endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /if_else.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n=2; 6 | if(n>0) 7 | cout<<"n is positive"; 8 | else if(n==0) 9 | cout<<"n is zero"; 10 | else 11 | cout<<"n is negative"; 12 | } 13 | -------------------------------------------------------------------------------- /insertionSortUsingArray.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int n; 5 | printf("Enter the size of the array:"); 6 | scanf("%d",&n); 7 | int a[n]; 8 | for(int i=0;i=0 && a[j]>temp) 19 | { 20 | a[j+1]=a[j]; 21 | j--; 22 | } 23 | a[j+1]=temp; 24 | } 25 | printf("Sorted array using insertion sort is:\n"); 26 | for(int i=0;i 2 | using namespace std; 3 | 4 | void printArray(int arr[], int n) 5 | { 6 | int i; 7 | for (i = 0; i < n; i++) 8 | cout << arr[i] << " "; 9 | cout << endl; 10 | } 11 | 12 | void insertionSort(int arr[], int n){ 13 | int i, key, j; 14 | for (i = 1; i < n; i++) 15 | { 16 | key = arr[i]; 17 | j = i - 1; 18 | 19 | /* Move elements of arr[0..i-1], that are 20 | greater than key, to one position ahead 21 | of their current position */ 22 | while (j >= 0 && arr[j] > key) 23 | { 24 | arr[j + 1] = arr[j]; 25 | j = j - 1; 26 | } 27 | arr[j + 1] = key; 28 | } 29 | } 30 | 31 | /* Driver code */ 32 | int main() 33 | { 34 | int arr[] = { 12, 11, 13, 5, 6 }; 35 | int n = sizeof(arr) / sizeof(arr[0]); 36 | 37 | insertionSort(arr, n); 38 | printArray(arr, n); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /kadane's_algorithm.cpp: -------------------------------------------------------------------------------- 1 | // kadane's algorithm 2 | 3 | #include 4 | using namespace std; 5 | 6 | int max_array_sum(int ar[],int a){ 7 | 8 | int till_cur_sum=0 , total_sum=INT_MIN , max_element=INT_MIN; 9 | for(int j=0;j>n; 26 | int arr[n]; 27 | 28 | for(int i=0;i>arr[i]; 30 | } 31 | cout<<" Maximum subarray sum : "<< max_array_sum(arr,n); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /kthElement.cpp: -------------------------------------------------------------------------------- 1 | // K-th element of two sorted Arrays 2 | 3 | /* Question - Given two sorted arrays arr1 and arr2 of size N and M respectively and an element K. The task is to find the element that would be at the k’th position of the final sorted array. */ 4 | #include 5 | using namespace std; 6 | 7 | int kthElement(int arr1[], int arr2[], int n, int m, int k) 8 | { 9 | int i=0,j=0,l=1; 10 | 11 | while(i arr2[j]) 21 | { 22 | if(k==l) 23 | return arr2[j]; 24 | j++; 25 | l++; 26 | } 27 | else 28 | { 29 | 30 | if(k==l) 31 | return arr1[i]; 32 | 33 | if(k==l+1) 34 | return arr1[i]; 35 | 36 | i++; 37 | j++; 38 | l+=2; 39 | } 40 | } 41 | while(i 2 | using namespace std; 3 | int main() 4 | { 5 | int n,key; 6 | cin>>n; 7 | int a[n]; 8 | for(int i=0;i>a[i]; 10 | cin>>key; 11 | for(int i=0;i 2 | #include 3 | 4 | typedef struct node 5 | { 6 | int id; 7 | int data; 8 | struct node* next; 9 | }a; 10 | 11 | int main() 12 | { 13 | int i=1; 14 | a *first=NULL,*t=NULL,*last=NULL,*temp=NULL; 15 | for(;;) 16 | { 17 | int r; 18 | printf("Do you want to enter data:(1-yes/0-no):"); 19 | scanf("%d",&r); 20 | if(r==1) 21 | { 22 | t=(a*)malloc(sizeof(a)); 23 | t->id=i; 24 | i++; 25 | scanf("%d",&t->data); 26 | 27 | t->next= NULL; 28 | 29 | if(first==NULL) 30 | { 31 | first=t; 32 | last=t; 33 | } 34 | else 35 | { 36 | last->next=t; 37 | last=t; 38 | } 39 | } 40 | else 41 | break; 42 | } 43 | int p; 44 | printf("Enter the id of data you want to delete:"); 45 | scanf("%d",&p); 46 | t=first; 47 | if(t->id==p) 48 | { 49 | temp=t; 50 | first=t->next; 51 | } 52 | else 53 | { 54 | while(t->next->id!=p) 55 | { 56 | t=t->next; 57 | } 58 | temp=t->next; 59 | t->next=temp->next; 60 | free(temp); 61 | } 62 | 63 | t=first; 64 | while(t!=NULL) 65 | { 66 | printf("%d\n",t->data); 67 | t=t->next; 68 | } 69 | return 0; 70 | 71 | } 72 | -------------------------------------------------------------------------------- /matrix multiplication.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int m1,n1,m2,n2; 5 | printf("Enter the number of rows of first matrix:"); 6 | scanf("%d",&m1); 7 | printf("Enter the number of columns of first matrix:"); 8 | scanf("%d",&n1); 9 | printf("Enter the number of rows of second matrix:"); 10 | scanf("%d",&m2); 11 | printf("Enter the number of columns of second matrix:"); 12 | scanf("%d",&n2); 13 | 14 | 15 | int a[m1][n1],b[m2][n2],c[m1][n2],i,j,k; 16 | for(i=0;i 0){ 13 | int N = Integer.parseInt(in.readLine()); 14 | String input_line[] = in.readLine().trim().split("\\s+"); 15 | int arr[]= new int[N]; 16 | for(int i = 0; i < N; i++) 17 | arr[i] = Integer.parseInt(input_line[i]); 18 | 19 | Solution ob = new Solution(); 20 | System.out.println(ob.matrixMultiplication(N, arr)); 21 | } 22 | } 23 | } 24 | // } Driver Code Ends 25 | 26 | 27 | //User function Template for Java 28 | 29 | class Solution{ 30 | static int[][] memo = new int[100][100];; 31 | static int matrixMultiplication(int n, int arr[]) 32 | { 33 | 34 | for(int[] row:memo) 35 | Arrays.fill(row,-1); 36 | return solve(arr,1,n-1); 37 | 38 | } 39 | public static int solve(int [] a,int i,int j) 40 | { 41 | if(i>=j) 42 | return 0; 43 | if(memo[i][j]!=-1) 44 | return memo[i][j]; 45 | memo[i][j] = Integer.MAX_VALUE; 46 | for(int k = i;k 0 and mstSet[v] == False and key[v] > self.graph[u][v]: 68 | key[v] = self.graph[u][v] 69 | parent[v] = u 70 | 71 | self.printMST(parent) 72 | 73 | g = Graph(5) 74 | g.graph = [ [0, 2, 0, 6, 0], 75 | [2, 0, 3, 8, 5], 76 | [0, 3, 0, 0, 7], 77 | [6, 8, 0, 0, 9], 78 | [0, 5, 7, 9, 0]] 79 | 80 | g.primMST(); 81 | 82 | # Contributed by Divyanshu Mehta 83 | -------------------------------------------------------------------------------- /moneyChange.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | 5 | main() 6 | { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(NULL); 9 | ll m; 10 | cin>>m; 11 | ll a = m/10; 12 | m -= a * 10; 13 | ll b = m/5; 14 | m -= b * 5; 15 | ll c = m; 16 | ll n = a + b + c; 17 | cout << n << endl; 18 | } -------------------------------------------------------------------------------- /multiply.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int a=10; 6 | int b= 20; 7 | int c = a*b; 8 | cout<<"Multiplication of " + a + " and " + b + " is "+ c <1) { 13 | System.out.println("False"); 14 | } 15 | else { 16 | System.out.println("True"); 17 | } 18 | 19 | } 20 | public static void insert(String b, String a) { 21 | int j=0; 22 | int i=0; 23 | int c=0; 24 | while(i1) { 37 | System.out.println("False"); 38 | } 39 | else { 40 | System.out.println("True"); 41 | } 42 | 43 | } 44 | 45 | public static void main(String[] args) { 46 | Scanner scan = new Scanner(System.in); 47 | String a = scan.next(); 48 | String b = scan.next(); 49 | System.out.println(a); 50 | System.out.println(b); 51 | if(a.length()==b.length()) 52 | { 53 | replace(a,b); 54 | } 55 | else if (a.length()-1 == b.length()) 56 | { 57 | insert(b,a); 58 | } 59 | else if(a.length() == (b.length()-1)) 60 | { 61 | insert(a,b); 62 | } 63 | else { 64 | System.out.println("False"); 65 | } 66 | scan.close(); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /palindrome_partitioning.java: -------------------------------------------------------------------------------- 1 | //Initial Template for Java 2 | 3 | import java.io.*; 4 | import java.util.*; 5 | 6 | class palindrome_partitioning{ 7 | public static void main(String args[])throws IOException 8 | { 9 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 10 | int t = Integer.parseInt(in.readLine()); 11 | while(t-- > 0){ 12 | String str = in.readLine(); 13 | Solution ob = new Solution(); 14 | System.out.println(ob.palindromicPartition(str)); 15 | } 16 | } 17 | }// } Driver Code Ends 18 | 19 | 20 | //User function Template for Java 21 | 22 | class Solution{ 23 | static int[][] dp = new int[501][501]; 24 | static int palindromicPartition(String s) 25 | { 26 | for(int [] rows:dp) 27 | Arrays.fill(rows,-1); 28 | return solve(s,0,s.length()-1); 29 | } 30 | public static int solve(String s,int i ,int j) 31 | { 32 | if(i>=j) 33 | return 0; 34 | if(palindrome(s,i,j)) 35 | return 0; 36 | if(dp[i][j]!=-1) 37 | return dp[i][j]; 38 | dp[i][j] = Integer.MAX_VALUE; 39 | for(int k = i;kj) 59 | return false; 60 | if(i==j) 61 | return true; 62 | while(i 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /reverse_LinkedList.java: -------------------------------------------------------------------------------- 1 | // Java program for reversing the linked list 2 | 3 | class LinkedList { 4 | 5 | static Node head; 6 | 7 | static class Node { 8 | 9 | int data; 10 | Node next; 11 | 12 | Node(int d) 13 | { 14 | data = d; 15 | next = null; 16 | } 17 | } 18 | 19 | /* Function to reverse the linked list */ 20 | Node reverse(Node node) 21 | { 22 | Node prev = null; 23 | Node current = node; 24 | Node next = null; 25 | while (current != null) { 26 | next = current.next; 27 | current.next = prev; 28 | prev = current; 29 | current = next; 30 | } 31 | node = prev; 32 | return node; 33 | } 34 | 35 | // prints content of double linked list 36 | void printList(Node node) 37 | { 38 | while (node != null) { 39 | System.out.print(node.data + " "); 40 | node = node.next; 41 | } 42 | } 43 | 44 | // Driver Code 45 | public static void main(String[] args) 46 | { 47 | LinkedList list = new LinkedList(); 48 | list.head = new Node(85); 49 | list.head.next = new Node(15); 50 | list.head.next.next = new Node(4); 51 | list.head.next.next.next = new Node(20); 52 | 53 | System.out.println("Given Linked list"); 54 | list.printList(head); 55 | head = list.reverse(head); 56 | System.out.println(""); 57 | System.out.println("Reversed linked list "); 58 | list.printList(head); 59 | } 60 | } 61 | 62 | // This code has been contributed by Mayank Jaiswal 63 | -------------------------------------------------------------------------------- /staircase.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Using Memoization 3 | long helper(int n,long *arr) 4 | { 5 | if(n<=1) 6 | return 1; 7 | if(n==2) 8 | return 2; 9 | if(arr[n]!=-1) 10 | return arr[n]; 11 | arr[n]= helper(n-1,arr)+helper(n-2,arr)+helper(n-3,arr); 12 | return arr[n]; 13 | } 14 | 15 | 16 | 17 | 18 | long staircase(int n) 19 | { 20 | long *arr=new long[n+1]; 21 | for(int i=0;i<=n;i++) 22 | arr[i]=-1; 23 | return helper(n,arr); 24 | 25 | } 26 | 27 | 28 | //Using DP 29 | long staircase(int n) 30 | { 31 | long *arr=new long[n+1]; 32 | arr[0]=arr[1]=1; 33 | arr[2]=2; 34 | for(int i=3;i<=n;i++) 35 | { 36 | arr[i]=arr[i-1]+arr[i-2]+arr[i-3]; 37 | } 38 | return arr[n]; 39 | } -------------------------------------------------------------------------------- /string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | cout<<"hello world"< 2 | using namespace std; 3 | int main() 4 | { 5 | cout<<"hello world"; 6 | cout< h 18 | = new HashMap(); 19 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int n=1; 6 | switch(n) 7 | { 8 | case 1:cout<<"value of n is 1"; 9 | break; 10 | default:cout<<"value of n is not equal to 1"; 11 | break; 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /top.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | usingn namespace std; 3 | int main() 4 | { 5 | int a = 10; 6 | int b = 20; 7 | int c = a+b; 8 | cout<<"sum of "+a+" and "+b+" is "+c<0) 27 | { 28 | for(int j=0;j0) 36 | { 37 | for(int j=0;j