├── **kadanes.cpp ├── **minimumsum.cpp ├── *leet_permutationII.cpp ├── .DS_Store ├── Assignments ├── Assignment 1 - Basics of Problem Solving.pdf ├── Assignment 10 - Trees.pdf ├── Assignment 11 - Binary Trees.pdf ├── Assignment 12 - BST.pdf ├── Assignment 13 - Hashtables and Heaps.pdf ├── Assignment 2 - Intro to Java.pdf ├── Assignment 2a - Programming Fundamentals.pdf ├── Assignment 3 - Arrays .pdf ├── Assignment 4 - Arrays and Strings.pdf ├── Assignment 5 - Recursion 1.pdf ├── Assignment 6 - Recursion 2.pdf ├── Assignment 6 - Recursion 3.pdf ├── Assignment 8 - LinkedList.pdf └── Assignment 9 - Stacks and Queues.pdf ├── Graphs ├── .DS_Store ├── articulation copy.cpp ├── articulation.cpp ├── bfs_graph.cpp ├── cycle_bellman_ford_graph.cpp ├── cycle_color.cpp ├── cycle_graph.cpp ├── dfs_graph.cpp ├── dijkstra_graph.cpp ├── find_island.cpp ├── graph_btraversal.cpp ├── hamiltoninan.cpp ├── kosaraju copy.cpp ├── kosaraju.cpp ├── kruskal_graph.cpp ├── mcolouring_graph.cpp ├── minspan_graph.cpp ├── pretotree.cpp └── topological_graph.cpp ├── README.md ├── SUBSETS.cpp ├── anagram.py ├── backtracking ├── chess.c ├── codenation(colour_matrix).cpp ├── geekschess.cpp ├── nqueen.c ├── ratmaze.cpp └── sudoku.c ├── bitwise ├── **numberDigitOne.cpp ├── .DS_Store ├── BITWISE.pdf ├── Error in Competitive programming Online Code.pdf ├── count1_32bit.cpp ├── power4.cpp └── sumInteger(BITWISE).cpp ├── check_frequency.java ├── combinational sum.py ├── cut_rods_max_multiples.cpp ├── flood_fill.c ├── frog.c ├── frogjump.c ├── heap.cpp ├── important questions and notes ├── .DS_Store ├── ARRAY.pdf ├── BITWISE.pdf ├── Dynamic Program and recursion.pdf ├── Error in Competitive programming Online Code .pdf ├── Linklist -.pdf ├── Must do questions.pdf ├── Notes │ ├── 1. Flow Charts.pdf │ ├── 10. Complexity Analysis 2.pdf │ ├── 11. OOPS 1.pdf │ ├── 12. OOPS 2.pdf │ ├── 13. Linked Lists.pdf │ ├── 14. Stacks.pdf │ ├── 15. Queues.pdf │ ├── 16. Trees.pdf │ ├── 17. Binary Trees.pdf │ ├── 18. BST .pdf │ ├── 19. Hashtables and Mixed Problems.pdf │ ├── 2. Programming Fundamentals 1.pdf │ ├── 20. Priority Queues and Heaps.pdf │ ├── 22. Graphs.pdf │ ├── 24. String Matching.pdf │ ├── 3. Programming Fundamentals 2.pdf │ ├── 4. Programming Fundamentals 3.pdf │ ├── 5. Programming Fundamentals 3.pdf │ ├── 6. Recursion.pdf │ ├── 7. Recursion 2.pdf │ ├── 8. Recursion 3.pdf │ ├── 9. Complexity Analysis.pdf │ ├── Assignment 1 - Basics of Problem Solving.pdf │ ├── Assignment 10 - Trees.pdf │ ├── Assignment 11 - Binary Trees.pdf │ ├── Assignment 12 - BST.pdf │ ├── Assignment 13 - Hashtables and Heaps.pdf │ ├── Assignment 2 - Intro to Java.pdf │ ├── Assignment 2a - Programming Fundamentals.pdf │ ├── Assignment 3 - Arrays .pdf │ ├── Assignment 4 - Arrays and Strings.pdf │ ├── Assignment 5 - Recursion 1.pdf │ ├── Assignment 6 - Recursion 2.pdf │ ├── Assignment 6 - Recursion 3.pdf │ ├── Assignment 8 - LinkedList.pdf │ ├── Assignment 9 - Stacks and Queues.pdf │ ├── DP.java │ ├── L10.zip │ ├── L13_LL.zip │ ├── L14.zip │ ├── L15.zip │ ├── L16_Trees.zip │ ├── L17.zip │ ├── L18.zip │ ├── L2.zip │ ├── L20.zip │ ├── L22 (1).zip │ ├── L22.zip │ ├── L23.zip │ ├── L24.zip │ ├── L3_FunctionsAndArrays.zip │ ├── L4.zip │ ├── L5.zip │ ├── L6.zip │ ├── L7.zip │ ├── L8_Recursion.java │ ├── L9_ComplexityAnalysis.java │ ├── OOPS.zip │ ├── OOPSNotes.pdf │ ├── StarPattern.java │ └── _DS_Store ├── Problems:.pdf ├── Screen Shot 2018-03-11 at 4.27.24 PM.png ├── Screen Shot 2018-03-11 at 4.45.29 PM.png └── TREES.pdf ├── java practise ├── *bucket.cpp ├── .DS_Store ├── BitManipulation.java ├── CommonSubsequence.java ├── Knapsack.java ├── LRU.java ├── Pattern Searching │ ├── .DS_Store │ ├── KMPPattern.java │ └── NaivePattern.java ├── Screen Shot 2017-08-06 at 5.01.55 AM.png ├── SmallestWindow.java ├── Sparse.java ├── Subset.java ├── SwapSumSame.java ├── Test.java ├── ToggleBit.java ├── UncommonChar.java ├── WinterComming.java ├── candy.java ├── consecutiveInteger.java ├── javafile.java ├── maxCoins.java ├── minimiseHeight.java ├── minimumOperation.java ├── nMeetings.java ├── repeatSmallIndex.java └── subset_sum.py ├── josephus.c ├── kadanes.cpp ├── leetcode ├── **Maximum_Product_Subarray.cpp ├── **O(n)unsorted_max_consecutive.cpp ├── **bitwiseAND.cpp ├── **canIwin.cpp ├── **coinChangeO(n).cpp ├── **countNodesCompleteTree.cpp ├── **greaterTree.cpp ├── **hIndexxResearchPaper.cpp ├── **inordersuccessor.cpp ├── **integerBreak.cpp ├── **largestDivisibleSubset.cpp ├── **lexographic_sort_numbers.cpp ├── **linkNodes_Second.cpp ├── **majorityVoteAlgorithm.cpp ├── **maxProduct(Two string equal or not).cpp ├── **max_jump_array.cpp ├── **maximalSquare.cpp ├── **mode_bst_nospace.cpp ├── **next_permutation.cpp ├── **nthDigit.cpp ├── **nthPermutation.cpp ├── **numberDigitOne.cpp ├── **pathSumWithoutRoot.cpp ├── **rectArea.cpp ├── **superPow.cpp ├── **swap_consecutive_nodes.cpp ├── **tortroisehareAlgo.cpp ├── **word_break_DP.cpp ├── *combination.cpp ├── *distinctSubsequences(DP).cpp ├── *minimumMOves(n-1)ele.cpp ├── *uglyNumber.cpp ├── .DS_Store ├── 3sum.cpp ├── Longest_Repeating_Character.cpp ├── MaxRepeatedSubArray.cpp ├── MaximumNumberofNon-OverlappingSubarraysWithSumEqualsTarget.cpp ├── Maximum_Sum_Subarray.cpp ├── Palindrome_Partitioning.cpp ├── Screen Shot 2018-02-13 at 12.29.22 AM.png ├── Set_Matrix_Zeroes.cpp ├── Symmetric(Mirror)Tree.cpp ├── TriangleMINSUM.cpp ├── Word_Break.cpp ├── Word_Ladder.cpp ├── addDigits.cpp ├── addString.cpp ├── allPathsofSum.cpp ├── anagram_group.cpp ├── arithmeticSeries.cpp ├── arrangingCoins.cpp ├── assignCookies.cpp ├── badVesrion.cpp ├── balancedTree.cpp ├── base7.cpp ├── battleship.cpp ├── binaryToPathss.cpp ├── binaryWatch.cpp ├── binarytoString.cpp ├── bottomLeftMostTree.cpp ├── bulbSwitcher.cpp ├── bullsCows.cpp ├── buyStock.cpp ├── check_jump_game.cpp ├── circularArray.cpp ├── combination_sum.cpp ├── combination_sum3.cpp ├── comniation_sum2.cpp ├── contain_duplicate.cpp ├── contain_duplicates2.cpp ├── count1_32bit.cpp ├── countPrimes.cpp ├── countUniqueDigits.cpp ├── countingBits.cpp ├── courseSchedule.cpp ├── decodeways.cpp ├── deleteNodeBST.cpp ├── detectCapital.cpp ├── diagonalraverse.cpp ├── diameterBT.cpp ├── dungeonGame.cpp ├── duplicateNumber.cpp ├── duplicatesArray.cpp ├── dutch_national.cpp ├── dynamic_path_matrix.cpp ├── eliminationGame.cpp ├── enumerationPossible.cpp ├── evaluateDivision.cpp ├── every_ele_diagonal.cpp ├── findAllAnagramSubstring.cpp ├── findKpairs.cpp ├── findLargest.cpp ├── findPeak.cpp ├── findRightInterval.cpp ├── firstUnoqueString.cpp ├── first_missing_positive.cpp ├── fizzbuzz.cpp ├── flattenBT.cpp ├── floodFillPixel.cpp ├── gamelife.cpp ├── generateUniqueBST.cpp ├── graycode.cpp ├── guessNumber.cpp ├── hammingDistance.cpp ├── happyNumber.cpp ├── heater.cpp ├── house_robber.cpp ├── houserobberII.cpp ├── in_pre_to_tree.cpp ├── incTriplet.cpp ├── insert_interval.cpp ├── integerReplacement.cpp ├── intersectionArray.cpp ├── intersectionTwoLL.cpp ├── invertTree.cpp ├── isSubsequence.cpp ├── island.cpp ├── islandPerimeter.cpp ├── isomorphicString.cpp ├── keyboard.cpp ├── kthSmallestBST.cpp ├── kthSmallestMatrix.cpp ├── length_last_word.cpp ├── levelOrder.cpp ├── linkListpalindrom.cpp ├── linkNodesTrees.cpp ├── longestIncreasingSubsequence.cpp ├── longestPalindrome.cpp ├── longest_palidromic_string.cpp ├── longest_substring_withoutrepchar.cpp ├── majorityElement.cpp ├── matchStick.cpp ├── matrixsearch.cpp ├── maxDepthTree.cpp ├── max_consecutive_ones.cpp ├── max_rect.cpp ├── max_subtree_sum.cpp ├── median_two_sorted.cpp ├── merge_intervals.cpp ├── merge_two_sorted_array.cpp ├── minBalloons.cpp ├── minDepth.cpp ├── minSubArraySum.cpp ├── min_in_rotateII.cpp ├── min_rotate_array.cpp ├── mindistnodes.cpp ├── mineditdist_words.cpp ├── minimum_array_moves.cpp ├── missinNumber.cpp ├── moveZeroes.cpp ├── netgreaterele.cpp ├── nextGreaterEle.cpp ├── nimGame.cpp ├── no.ofboomerang.cpp ├── no_of_segment.cpp ├── no_of_unique_paths.cpp ├── noofvariable.cpp ├── numberToHexadecimal.cpp ├── ones_zeroes.cpp ├── pacificAtlantic.cpp ├── palindromePartition.cpp ├── partitionList.cpp ├── pascalTriangle.cpp ├── pathSUMBT.cpp ├── pathSum.cpp ├── path_in_matrix.cpp ├── perfectNo.cpp ├── perfectSquare.cpp ├── permutation.cpp ├── permutation2.cpp ├── power.cpp ├── power4.cpp ├── powerThree.cpp ├── product_wothout_self.cpp ├── rabbitsForest.cpp ├── randomPickIndex.cpp ├── randomised.cpp ├── randomisedDuplicates.cpp ├── rangeSumQuerry.cpp ├── rangequerryMutable.cpp ├── ransomNote.cpp ├── rectangle.cpp ├── relativeRank.cpp ├── removeKdigits.cpp ├── removeLinkList.cpp ├── remove_duplicates.cpp ├── remove_duplicates_II.cpp ├── repeat_substring.cpp ├── reverseBits.cpp ├── reverseLinkList.cpp ├── reverseStringII.cpp ├── reverseVowel.cpp ├── reverseWords(MIRROR).cpp ├── reverse_no.cpp ├── rightSide.cpp ├── rotateArray copy.cpp ├── rotateArray.cpp ├── rotate_image.cpp ├── searchMatrix.cpp ├── shortest_cont_subarray.cpp ├── shuffleArray.cpp ├── singleNumberII.cpp ├── small_ciler_into_large_cicles.cpp ├── sortCharByfreq.cpp ├── sorted_array_balacedBST.cpp ├── stairjumps.cpp ├── stairjumps_memoization.cpp ├── stockII.cpp ├── stringCompression.cpp ├── subStringRepeating.cpp ├── subsequences.cpp ├── subset.cpp ├── subsetII.cpp ├── subtractwithoutdivision.cpp ├── sumInteger(BITWISE).cpp ├── sumLeftNodes.cpp ├── sumToROOT.cpp ├── summary_ranges.cpp ├── superUgly.cpp ├── surroundedRegion.cpp ├── targetSum.cpp ├── thirdMax.cpp ├── topKfrequent.cpp ├── totalhammingDistance.cpp ├── trailing_zeroes_factorial.cpp ├── trap_rain_water.cpp ├── trap_water_coordinates.cpp ├── trimBST.cpp ├── twoSumBST.cpp ├── twotreessame.cpp ├── uglyNumberII.cpp ├── uniValuePath.cpp ├── unique_path2.cpp ├── uniquesBST(enumeration).cpp ├── validAnagram.cpp ├── validate_bst.cpp ├── verticalline_waterfill.cpp ├── verufyPreorder.cpp ├── wiggleSequence.cpp ├── wordPattern.cpp ├── word_search.cpp └── word_searchII.cpp ├── maxpathmatrix.c ├── mini-max-tic-tac-toe └── tictactoe.cpp ├── minjump.cpp ├── minswaps.cpp ├── nextGreater.c ├── perm.py ├── permutaion.py ├── permutation.py ├── pranthesis.cpp ├── reachn.cpp ├── removeDuplicate.cpp ├── rep.py ├── reverseDuplicates.cpp ├── reverselinklist.c ├── shortest_s_to_dc.cpp ├── sorting ├── insertionsort.cpp ├── mergesort.cpp ├── qsort.cpp └── quicksort.cpp ├── spiral.cpp ├── subset ├── sub.cpp ├── subset.py ├── subset_sum.png ├── subsetpython.py └── subsets of string.png ├── tomjerry.c ├── tree ├── LCABST.cpp ├── LCABT.cpp ├── pretotree.cpp ├── roottoleaf.cpp └── treediagonal.cpp ├── trie.cpp ├── water.py └── wordboggle.cpp /**kadanes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int maxSubArraySum(int a[], int size) 5 | { 6 | int max_so_far = a[0]; 7 | int curr_max = a[0]; 8 | 9 | for (int i = 1; i < size; i++) 10 | { 11 | cout<<"i:"< 2 | using namespace std; 3 | int a[4]={36 ,7 ,46 ,40}; 4 | int findMinimumSum(int netsum,int sum,int i){ 5 | if(i==3){ 6 | return abs((netsum-sum)-sum); 7 | } 8 | 9 | return min(findMinimumSum(netsum,sum+a[i],i+1),findMinimumSum(netsum,sum,i+1)); 10 | } 11 | int main() 12 | { 13 | int sum=0; 14 | for (int i=0;i<5;i++){ 15 | sum+=a[i]; 16 | } 17 | int k=findMinimumSum(sum,0,0); 18 | cout<<"K:"< 2 | #include 3 | using namespace std; 4 | 5 | class Graph { 6 | int v; 7 | list *adj; 8 | 9 | public: 10 | void addEdge(int v,int w) 11 | { 12 | (this->adj)[v].push_back(w); 13 | } 14 | 15 | Graph(int v){ 16 | this->v=v; 17 | this->adj=new list[v]; 18 | } 19 | void bfs(int v){ 20 | bool *visited=new bool[this->v]; 21 | list queue; 22 | list::iterator i; 23 | list::iterator j; 24 | 25 | for(int i=0;iv;i++){ 26 | visited[i]=false; 27 | } 28 | 29 | visited[v]=true; 30 | queue.push_back(v); 31 | j=queue.begin(); 32 | 33 | while(j!=queue.end()){ 34 | for(i=(this->adj)[*j].begin();i!=(this->adj)[*j].end();i++){ 35 | if(visited[(*i)]==false){ 36 | visited[*i]=true; 37 | queue.push_back(*i); 38 | } 39 | } 40 | j++; 41 | } 42 | 43 | for(j=queue.begin();j!=queue.end();j++){ 44 | cout<<*j<<"\n"; 45 | } 46 | } 47 | }; 48 | 49 | int main(){ 50 | Graph g(4); 51 | g.addEdge(0, 1); 52 | g.addEdge(0, 2); 53 | g.addEdge(1, 2); 54 | g.addEdge(2, 0); 55 | g.addEdge(2, 3); 56 | g.addEdge(3, 3); 57 | g.bfs(0); 58 | return 0; 59 | } -------------------------------------------------------------------------------- /Graphs/dfs_graph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | // #include 5 | using namespace std; 6 | class Graph{ 7 | int v; 8 | list *adj; 9 | public: 10 | Graph(int v){ 11 | this->v=v; 12 | (this->adj)=new list[v]; 13 | } 14 | 15 | void addEdge(int u,int v){ 16 | (this->adj)[u].push_back(v); 17 | } 18 | void dfs(int v){ 19 | bool *visited=new bool[this->v]; 20 | for(int i=0;iv;i++){ 21 | visited[i]=false; 22 | } 23 | stack s; 24 | s.push(v); 25 | visited[v]=true; 26 | while(s.size()!=0){ 27 | int i=s.top(); 28 | cout<:: iterator k; 32 | for(k=this->adj[i].begin();k!=this->adj[i].end();k++){ 33 | if(!visited[*k]){ 34 | s.push(*k); 35 | } 36 | } 37 | } 38 | } 39 | }; 40 | int main(){ 41 | Graph g(4); 42 | g.addEdge(0, 1); 43 | g.addEdge(0, 2); 44 | g.addEdge(1, 2); 45 | g.addEdge(2, 0); 46 | g.addEdge(2, 3); 47 | g.addEdge(3, 3); 48 | g.dfs(2); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Graphs/graph_btraversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Graph { 6 | int v; 7 | list *adj; 8 | 9 | public: 10 | void addEdge(int v,int w) 11 | { 12 | (this->adj)[v].push_back(w); 13 | } 14 | 15 | Graph(int v){ 16 | this->v=v; 17 | this->adj=new list[v]; 18 | } 19 | void bfs(int v){ 20 | bool *visited=new bool[this->v]; 21 | list queue; 22 | list::iterator i; 23 | list::iterator j; 24 | 25 | for(int i=0;iv;i++){ 26 | visited[i]=false; 27 | } 28 | 29 | visited[v]=true; 30 | queue.push_back(v); 31 | j=queue.begin(); 32 | 33 | while(j!=queue.end()){ 34 | for(i=(this->adj)[*j].begin();i!=(this->adj)[*j].end();i++){ 35 | if(visited[(*i)]==false){ 36 | visited[*i]=true; 37 | queue.push_back(*i); 38 | } 39 | } 40 | j++; 41 | } 42 | 43 | for(j=queue.begin();j!=queue.end();j++){ 44 | cout<<*j<<"\n"; 45 | } 46 | } 47 | }; 48 | 49 | int main(){ 50 | Graph g(4); 51 | g.addEdge(0, 1); 52 | g.addEdge(0, 2); 53 | g.addEdge(1, 2); 54 | g.addEdge(2, 0); 55 | g.addEdge(2, 3); 56 | g.addEdge(3, 3); 57 | g.bfs(0); 58 | return 0; 59 | } -------------------------------------------------------------------------------- /Graphs/pretotree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define v 15 3 | using namespace std; 4 | int t=0; 5 | struct tree{ 6 | int data; 7 | struct tree * right; 8 | struct tree * left; 9 | }; 10 | int findRoot(int data,int inorder[v]){ 11 | for(int i=0;iend){ 21 | return NULL; 22 | } 23 | 24 | int k=preorder[t++]; 25 | struct tree *root=new tree(); 26 | root->data=k; 27 | root->left=NULL; 28 | root->right=NULL; 29 | 30 | if(start==end){ 31 | return root; 32 | } 33 | 34 | int l=findRoot(k,inorder); 35 | root->left=makeTree(start,l-1,inorder,preorder); 36 | root->right=makeTree(l+1,end,inorder,preorder); 37 | 38 | return root; 39 | } 40 | void printRoot(struct tree *root){ 41 | if(root==NULL){ 42 | return; 43 | } 44 | printRoot(root->left); 45 | cout<data<<"\n"; 46 | printRoot(root->right); 47 | } 48 | int main(){ 49 | int inorder[v]={8,4,9,2,10,5,11,1,12,6,13,3,14,7,15}; 50 | int preorder[v]={1,2,4,8,9,5,10,11,3,6,12,13,7,14,15}; 51 | struct tree *root=makeTree(0,v-1,inorder,preorder); 52 | printRoot(root); 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Graphs/topological_graph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Graph { 7 | int v; 8 | list *adj; 9 | 10 | public: 11 | void addEdge(int v,int w) 12 | { 13 | (this->adj)[v].push_back(w); 14 | } 15 | 16 | Graph(int v){ 17 | this->v=v; 18 | this->adj=new list[v]; 19 | } 20 | 21 | void topologicalUtil(int v,bool *visited,stack &st){ 22 | visited[v]=true; 23 | list::iterator i; 24 | for(i=(this->adj)[v].begin();i!=(this->adj)[v].end();i++){ 25 | if(!visited[(*i)]){ 26 | topologicalUtil(*i,visited,st); 27 | } 28 | } 29 | st.push(v); 30 | } 31 | 32 | void topological(){ 33 | 34 | bool *visited=new bool[this->v]; 35 | stack st; 36 | 37 | for(int i=0;iv;i++){ 38 | visited[i]=false; 39 | } 40 | 41 | for(int i=0;iv;i++){ 42 | if(!visited[i]){ 43 | topologicalUtil(i,visited,st); 44 | } 45 | } 46 | 47 | while(st.size()>0){ 48 | cout< 2 | using namespace std; 3 | void possibleSubsets(char A[], int N){ 4 | for(int i = 0;i < (1 << N); ++i) 5 | { 6 | for(int j = 0;j < N;++j) 7 | if(i & (1 << j)) 8 | cout< 2 | 3 | int safe(int i,int j,int a[8][8]){ 4 | return(i>=0 && i<8 && j>=0 && j<8 && a[i][j]==-1); 5 | } 6 | 7 | void printChess(int a[8][8]){ 8 | for(int i=0;i<8;i++){ 9 | for(int j=0;j<8;j++){ 10 | printf("%d\t",a[i][j]); 11 | } 12 | printf("\n"); 13 | } 14 | printf("\n#################\n"); 15 | } 16 | 17 | int solveKnightChessUtil(int i,int j,int t,int a[8][8],int x[8],int y[8]){ 18 | int nexti,nextj,k; 19 | if(t==64){ 20 | return 1; 21 | } 22 | for(k=0;k<8;k++){ 23 | nexti=i+x[k]; 24 | nextj=j+y[k]; 25 | if(safe(nexti,nextj,a)){ 26 | a[nexti][nextj]=t; 27 | if(solveKnightChessUtil(nexti,nextj,t+1,a,x,y)==1){ 28 | return 1; 29 | }else{ 30 | a[nexti][nextj]=-1; 31 | } 32 | 33 | } 34 | } 35 | return 0; 36 | } 37 | 38 | void solveKnightChess(){ 39 | int a[8][8]; 40 | for(int i=0;i<8;i++){ 41 | for(int j=0;j<8;j++){ 42 | a[i][j]=-1; 43 | } 44 | } 45 | // int x[8]={2,2,-2,-2,1,-1,1,-1}; 46 | // int y[8]={1,-1,1,-1,2,2,-2,-2}; 47 | 48 | 49 | int x[8] = {2,1,-1,-2,-2,-1,1,2}; 50 | int y[8] = {1,2,2,1,-1,-2,-2,-1 }; 51 | a[0][0]=0; 52 | if(solveKnightChessUtil(0,0,1,a,x,y)==0){ 53 | printf("No Solution\n"); 54 | }else{ 55 | printChess(a); 56 | } 57 | } 58 | 59 | int main(){ 60 | solveKnightChess(); 61 | return 0; 62 | } -------------------------------------------------------------------------------- /backtracking/nqueen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define n 8 3 | int a[n][n]; 4 | int safe(int i,int j){ 5 | 6 | int count=0; 7 | 8 | for(int p=j-1;p>=0;p--){ 9 | if(a[i][p]==1){ 10 | count=1; 11 | break; 12 | } 13 | } 14 | if(count==1){ 15 | return 0; 16 | } 17 | 18 | int p=i-1; 19 | int q=j-1; 20 | count=0; 21 | while(p>=0 && q>=0){ 22 | if(a[p][q]==1){ 23 | count=1; 24 | break; 25 | } 26 | p--; 27 | q--; 28 | } 29 | 30 | if(count==1){ 31 | return 0; 32 | } 33 | 34 | p=i+1; 35 | q=j-1; 36 | count=0; 37 | while(p=0){ 38 | if(a[p][q]==1){ 39 | count=1; 40 | break; 41 | } 42 | p++; 43 | q--; 44 | } 45 | 46 | if(count==1){ 47 | return 0; 48 | } 49 | 50 | return 1; 51 | } 52 | 53 | void printarray(){ 54 | for(int i=0;i=n){ 66 | printarray(); 67 | return; 68 | } 69 | 70 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | #define n 4 5 | vector path; 6 | 7 | void printvector(){ 8 | for(int i=0;i=n || j>=n){ 17 | return; 18 | } 19 | 20 | if(i==j && i==n-1){ 21 | printvector(); 22 | return; 23 | } 24 | 25 | if(dir!='u' && a[i+1][j]==1 && i0){ 38 | // path.push_back('U'); 39 | // ratmaze(a,i-1,j,'u'); 40 | // path.pop_back(); 41 | // } 42 | 43 | // if(dir!='r' && a[i][j-1]==1 && j>0){ 44 | // path.push_back('L'); 45 | // ratmaze(a,i,j-1,'l'); 46 | // path.pop_back(); 47 | // } 48 | } 49 | 50 | int main() 51 | { 52 | int a[n][n]={{1,0,0,0},{1,1,0,1},{1,1,0,0},{0,1,1,1}}; 53 | ratmaze(a,0,0,'d'); 54 | return 0; 55 | } -------------------------------------------------------------------------------- /bitwise/**numberDigitOne.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 233. Number of Digit One 3 | 4 | Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. 5 | 6 | For example: 7 | Given n = 13, 8 | Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 9 | 10 | 11 | */ 12 | class Solution { 13 | public: 14 | int lengthDigit(int n){ 15 | int c=0; 16 | while(n>0){ 17 | c++; 18 | n=n/10; 19 | } 20 | return c; 21 | } 22 | int countDigitOne(int n) { 23 | if(n<=0){ 24 | return 0; 25 | }else if(n<10){ 26 | return 1; 27 | } 28 | int length=lengthDigit(n); 29 | int base=pow(10,length-1); 30 | int answer=n/base; 31 | int remainder=n%base; 32 | int m; 33 | if(answer==1){ 34 | m=n-base+1; 35 | }else{ 36 | m=base; 37 | } 38 | return countDigitOne(base-1)*answer+m+countDigitOne(remainder); 39 | } 40 | }; -------------------------------------------------------------------------------- /bitwise/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/bitwise/.DS_Store -------------------------------------------------------------------------------- /bitwise/BITWISE.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/bitwise/BITWISE.pdf -------------------------------------------------------------------------------- /bitwise/Error in Competitive programming Online Code.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/bitwise/Error in Competitive programming Online Code.pdf -------------------------------------------------------------------------------- /bitwise/count1_32bit.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 191. Number of 1 Bits 3 | 4 | Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). 5 | 6 | For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3. 7 | */ 8 | class Solution { 9 | public: 10 | int hammingWeight(uint32_t n) { 11 | int c=0; 12 | while(n){ 13 | n = n & (n-1); 14 | c++; 15 | } 16 | return c; 17 | } 18 | }; -------------------------------------------------------------------------------- /bitwise/power4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 342. Power of Four 3 | 4 | Given an integer (signed 32 bits), write a function to check whether it is a power of 4. 5 | 6 | Example: 7 | Given num = 16, return true. Given num = 5, return false. 8 | 9 | Follow up: Could you solve it without loops/recursion? 10 | 11 | Credits: 12 | Special thanks to @yukuairoy for adding this problem and creating all test cases. 13 | */ 14 | class Solution { 15 | public: 16 | bool isPowerOfFour(int n) { 17 | if(n==0 || n==INT_MIN){ 18 | return false; 19 | } 20 | int k = n & (n-1); 21 | int k1 = (int)log2(n); 22 | if( k==0 && k1%2==0 ){ 23 | return true; 24 | } 25 | return false; 26 | } 27 | }; -------------------------------------------------------------------------------- /bitwise/sumInteger(BITWISE).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 371. Sum of Two Integers 3 | 4 | Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. 5 | 6 | Example: 7 | Given a = 1 and b = 2, return 3. 8 | 9 | 10 | */ 11 | class Solution { 12 | public: 13 | int getSum(int a, int b) { 14 | if(b==0){ 15 | return a; 16 | } 17 | return getSum(a^b,(a&b)<<1); 18 | } 19 | }; -------------------------------------------------------------------------------- /check_frequency.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.util.Scanner; 3 | import java.lang.*; 4 | 5 | public class check_frequency{ 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | int t=sc.nextInt(); 9 | while(t>0){ 10 | Scanner stsc = new Scanner(System.in); 11 | String str = stsc.nextLine(); 12 | HashMap hash = new HashMap<>(); 13 | for(int i=0;i entry : hash.entrySet()){ 27 | if(min>entry.getValue()){ 28 | min=entry.getValue(); 29 | } 30 | } 31 | int x=0; 32 | int y=0; 33 | for(Map.Entry entry : hash.entrySet()){ 34 | if(min+1==entry.getValue()){ 35 | x=x+1; 36 | } 37 | if(min==entry.getValue()){ 38 | y=y+1; 39 | } 40 | } 41 | if((x==1 && ((x+y)==str.length()-1)) || (y==str.length()-1)){ 42 | System.out.println("1"); 43 | }else{ 44 | System.out.println("0"); 45 | } 46 | t=t-1; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /combinational sum.py: -------------------------------------------------------------------------------- 1 | #COMBINATION SUM 2 | 3 | import sys 4 | 5 | # arrayElements = input("Enter arrayElements: ").split() 6 | # arrayElements = [int(i) for i in arrayElements] 7 | 8 | # finalSum = int(input("Enter finalSum: ")) 9 | 10 | arrayElements = [2,4,6,8] 11 | finalSum = 8 12 | 13 | def combinationSum(tempArray): 14 | 15 | if(sum(tempArray) > finalSum): 16 | return 17 | 18 | if(sum(tempArray) == finalSum): 19 | print(tempArray) 20 | 21 | if(len(tempArray) == len(arrayElements)): 22 | return 23 | 24 | for element in arrayElements: 25 | 26 | tempArray.append(element) 27 | combinationSum(tempArray) 28 | tempArray.pop() 29 | 30 | combinationSum([]) -------------------------------------------------------------------------------- /cut_rods_max_multiples.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int m=1; 4 | 5 | void myfunct(int n){ 6 | if(n==2){ 7 | m=m*2; 8 | return; 9 | } 10 | if(n==3){ 11 | m=m*3; 12 | return; 13 | } 14 | if(n%2==0){ 15 | myfunct(n/2); 16 | myfunct(n/2); 17 | }else{ 18 | myfunct((n-1)/2); 19 | myfunct((n+1)/2); 20 | } 21 | } 22 | 23 | int main() 24 | { 25 | myfunct(23); 26 | cout << "Maximum Product is " << m<<"\n"; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /frog.c: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void findJumps(int *a,int num,int start){ 5 | count<<*a; 6 | //int max=-1; 7 | 8 | // while(start--){ 9 | 10 | // } 11 | } 12 | 13 | int main(int argc, char const *argv[]) 14 | { 15 | int t; 16 | int n; 17 | cin>>t; 18 | int a={1,2,3,4,5}; 19 | findJumps(&a,3,0); 20 | 21 | 22 | // while(t--){ 23 | // cin>>n; 24 | // int a[n]; 25 | // for(int i=0;i>a[i]; 27 | // } 28 | // count<<"Array:\n"; 29 | // for(int i=0;i 2 | 3 | 4 | int totalways(int n){ 5 | if(n<0){ 6 | return 0; 7 | } 8 | 9 | if(n==0){ 10 | return 1; 11 | } 12 | 13 | return totalways(n-1)+totalways(n-2)+totalways(n-3); 14 | } 15 | 16 | int main() 17 | { 18 | int ways; 19 | 20 | ways=totalways(1); 21 | printf("Ways:%d\n",ways); 22 | 23 | 24 | ways=totalways(2); 25 | printf("Ways:%d\n",ways); 26 | 27 | 28 | ways=totalways(3); 29 | printf("Ways:%d\n",ways); 30 | 31 | 32 | ways=totalways(4); 33 | printf("Ways:%d\n",ways); 34 | 35 | 36 | ways=totalways(5); 37 | printf("Ways:%d\n",ways); 38 | 39 | 40 | ways=totalways(6); 41 | printf("Ways:%d\n",ways); 42 | 43 | return 0; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /important questions and notes/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/.DS_Store -------------------------------------------------------------------------------- /important questions and notes/ARRAY.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/ARRAY.pdf -------------------------------------------------------------------------------- /important questions and notes/BITWISE.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/BITWISE.pdf -------------------------------------------------------------------------------- /important questions and notes/Dynamic Program and recursion.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Dynamic Program and recursion.pdf -------------------------------------------------------------------------------- /important questions and notes/Error in Competitive programming Online Code .pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Error in Competitive programming Online Code .pdf -------------------------------------------------------------------------------- /important questions and notes/Linklist -.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Linklist -.pdf -------------------------------------------------------------------------------- /important questions and notes/Must do questions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Must do questions.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /1. Flow Charts.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /1. Flow Charts.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /10. Complexity Analysis 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /10. Complexity Analysis 2.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /11. OOPS 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /11. OOPS 1.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /12. OOPS 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /12. OOPS 2.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /13. Linked Lists.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /13. Linked Lists.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /14. Stacks.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /14. Stacks.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /15. Queues.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /15. Queues.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /16. Trees.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /16. Trees.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /17. Binary Trees.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /17. Binary Trees.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /18. BST .pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /18. BST .pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /19. Hashtables and Mixed Problems.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /19. Hashtables and Mixed Problems.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /2. Programming Fundamentals 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /2. Programming Fundamentals 1.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /20. Priority Queues and Heaps.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /20. Priority Queues and Heaps.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /22. Graphs.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /22. Graphs.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /24. String Matching.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /24. String Matching.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /3. Programming Fundamentals 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /3. Programming Fundamentals 2.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /4. Programming Fundamentals 3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /4. Programming Fundamentals 3.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /5. Programming Fundamentals 3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /5. Programming Fundamentals 3.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /6. Recursion.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /6. Recursion.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /7. Recursion 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /7. Recursion 2.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /8. Recursion 3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /8. Recursion 3.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /9. Complexity Analysis.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /9. Complexity Analysis.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /Assignment 1 - Basics of Problem Solving.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /Assignment 1 - Basics of Problem Solving.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /Assignment 10 - Trees.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /Assignment 10 - Trees.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /Assignment 11 - Binary Trees.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /Assignment 11 - Binary Trees.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /Assignment 12 - BST.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /Assignment 12 - BST.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /Assignment 13 - Hashtables and Heaps.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /Assignment 13 - Hashtables and Heaps.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /Assignment 2 - Intro to Java.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /Assignment 2 - Intro to Java.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /Assignment 2a - Programming Fundamentals.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /Assignment 2a - Programming Fundamentals.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /Assignment 3 - Arrays .pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /Assignment 3 - Arrays .pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /Assignment 4 - Arrays and Strings.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /Assignment 4 - Arrays and Strings.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /Assignment 5 - Recursion 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /Assignment 5 - Recursion 1.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /Assignment 6 - Recursion 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /Assignment 6 - Recursion 2.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /Assignment 6 - Recursion 3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /Assignment 6 - Recursion 3.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /Assignment 8 - LinkedList.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /Assignment 8 - LinkedList.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /Assignment 9 - Stacks and Queues.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /Assignment 9 - Stacks and Queues.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /L10.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /L10.zip -------------------------------------------------------------------------------- /important questions and notes/Notes /L13_LL.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /L13_LL.zip -------------------------------------------------------------------------------- /important questions and notes/Notes /L14.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /L14.zip -------------------------------------------------------------------------------- /important questions and notes/Notes /L15.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /L15.zip -------------------------------------------------------------------------------- /important questions and notes/Notes /L16_Trees.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /L16_Trees.zip -------------------------------------------------------------------------------- /important questions and notes/Notes /L17.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /L17.zip -------------------------------------------------------------------------------- /important questions and notes/Notes /L18.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /L18.zip -------------------------------------------------------------------------------- /important questions and notes/Notes /L2.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /L2.zip -------------------------------------------------------------------------------- /important questions and notes/Notes /L20.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /L20.zip -------------------------------------------------------------------------------- /important questions and notes/Notes /L22 (1).zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /L22 (1).zip -------------------------------------------------------------------------------- /important questions and notes/Notes /L22.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /L22.zip -------------------------------------------------------------------------------- /important questions and notes/Notes /L23.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /L23.zip -------------------------------------------------------------------------------- /important questions and notes/Notes /L24.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /L24.zip -------------------------------------------------------------------------------- /important questions and notes/Notes /L3_FunctionsAndArrays.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /L3_FunctionsAndArrays.zip -------------------------------------------------------------------------------- /important questions and notes/Notes /L4.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /L4.zip -------------------------------------------------------------------------------- /important questions and notes/Notes /L5.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /L5.zip -------------------------------------------------------------------------------- /important questions and notes/Notes /L6.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /L6.zip -------------------------------------------------------------------------------- /important questions and notes/Notes /L7.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /L7.zip -------------------------------------------------------------------------------- /important questions and notes/Notes /OOPS.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /OOPS.zip -------------------------------------------------------------------------------- /important questions and notes/Notes /OOPSNotes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /OOPSNotes.pdf -------------------------------------------------------------------------------- /important questions and notes/Notes /_DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Notes /_DS_Store -------------------------------------------------------------------------------- /important questions and notes/Problems:.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Problems:.pdf -------------------------------------------------------------------------------- /important questions and notes/Screen Shot 2018-03-11 at 4.27.24 PM.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Screen Shot 2018-03-11 at 4.27.24 PM.png -------------------------------------------------------------------------------- /important questions and notes/Screen Shot 2018-03-11 at 4.45.29 PM.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/Screen Shot 2018-03-11 at 4.45.29 PM.png -------------------------------------------------------------------------------- /important questions and notes/TREES.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/important questions and notes/TREES.pdf -------------------------------------------------------------------------------- /java practise/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/java practise/.DS_Store -------------------------------------------------------------------------------- /java practise/Knapsack.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Knapsack{ 4 | 5 | public static void main(String[] args) { 6 | int[] w={10, 20, 30}; 7 | int[] v={60, 100, 120}; 8 | int max_wt=50; 9 | int[][] k = new int[w.length+1][max_wt+1]; 10 | for(int i=0;i<=w.length;i++){ 11 | for(int j=0;j<=max_wt;j++){ 12 | if(i==0 || j==0){ 13 | k[i][j]=0; 14 | }else { 15 | if(jk[i-1][j]){ 20 | k[i][j]=(k[i-1][diff]+v[i-1]); 21 | }else{ 22 | k[i][j]=k[i-1][j]; 23 | } 24 | } 25 | } 26 | } 27 | } 28 | System.out.println("k is:"+k[w.length][max_wt]); 29 | 30 | } 31 | } -------------------------------------------------------------------------------- /java practise/LRU.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class LRU{ 4 | 5 | public static Boolean alreadyPresent(ArrayList k, int value){ 6 | for(int i=0;i k=new ArrayList(); 16 | int fault=0; 17 | for (int i=0;i findBinaryNumber(int n){ 19 | ArrayList stack=new ArrayList(); 20 | int r=-1,q=-1; 21 | while(n!=0){ 22 | r=n%2; 23 | q=n/2; 24 | n=q; 25 | stack.add(r); 26 | } 27 | return stack; 28 | } 29 | 30 | public static void main(String[] args) { 31 | Scanner s= new Scanner(System.in); 32 | System.out.println("Enter a number:"); 33 | int number=s.nextInt(); 34 | 35 | 36 | 37 | // Short way 38 | // System.out.println(isSparse(Integer.toBinaryString(number))); 39 | 40 | //use own fnction to find binary 41 | 42 | ArrayList binary=findBinaryNumber(number); 43 | String binaryString=""; 44 | for(int i=binary.size()-1;i>=0;i--){ 45 | binaryString+=String.valueOf(binary.get(i)); 46 | } 47 | System.out.println(isSparse(binaryString)); 48 | } 49 | } -------------------------------------------------------------------------------- /java practise/Subset.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import static java.lang.Math.pow; 3 | 4 | public class Subset{ 5 | 6 | public static void findSubset(int[] a,int n){ 7 | for (int i=0;i 0){ 11 | // System.out.println("j is: "+j); 12 | System.out.print(a[j] + " "); 13 | } 14 | } 15 | System.out.print("\n"); 16 | } 17 | 18 | } 19 | 20 | public static void main(String[] args) { 21 | int[] a={1,2,3}; 22 | findSubset(a,a.length); 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /java practise/SwapSumSame.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.util.Scanner; 3 | 4 | public class SwapSumSame{ 5 | 6 | public static int sumArray(int [] k){ 7 | int sum=0; 8 | for(int j=0;j toggle(ArrayList k,int l,int r){ 7 | 8 | for(int i=l-1;i<=r-1;i++){ 9 | if(k.get(i) == 1 ){ 10 | k.set(i,0); 11 | }else{ 12 | k.set(i,1); 13 | } 14 | } 15 | return k; 16 | } 17 | 18 | 19 | public static ArrayList findBinaryNumber(int n){ 20 | ArrayList stack=new ArrayList(); 21 | ArrayList reverseStack=new ArrayList(); 22 | int r=-1,q=-1; 23 | while(n!=0){ 24 | r=n%2; 25 | q=n/2; 26 | n=q; 27 | stack.add(r); 28 | } 29 | return stack; 30 | } 31 | 32 | public static void main(String[] args) { 33 | Scanner s= new Scanner(System.in); 34 | int t=s.nextInt(); 35 | 36 | for(int g=0;g binary=findBinaryNumber(number); 42 | ArrayList toggleArray=(toggle(binary,l,r)); 43 | String binaryString=""; 44 | for(int i=toggleArray.size()-1;i>=0;i--){ 45 | binaryString+=String.valueOf(toggleArray.get(i)); 46 | } 47 | System.out.println(Integer.parseInt(binaryString,2)); 48 | 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /java practise/UncommonChar.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.util.Scanner; 3 | 4 | 5 | public class UncommonChar{ 6 | 7 | public static boolean notIn(ArrayList str,char ch){ 8 | for (int i=0;i uncommon){ 17 | 18 | for(int i=0;i uncommon= new ArrayList(); 39 | Scanner s = new Scanner(System.in); 40 | System.out.println("Enter a string"); 41 | 42 | String k1=s.nextLine(); 43 | String k2=s.nextLine(); 44 | 45 | System.out.println("String1 is: "+k1); 46 | System.out.println("String2 is: "+k2); 47 | 48 | uncommon(k1,k2,uncommon); 49 | uncommon(k2,k1,uncommon); 50 | System.out.println("uncommon character is: "+uncommon); 51 | } 52 | } -------------------------------------------------------------------------------- /java practise/WinterComming.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.lang.*; 3 | import java.io.*; 4 | import java.util.Scanner; 5 | 6 | class WinterComming { 7 | 8 | public static boolean isPrime(int ele){ 9 | 10 | if(ele==1){ 11 | return false; 12 | } 13 | 14 | for(int i=2;i<=(ele/2);i++){ 15 | if(ele%i==0){ 16 | return false; 17 | } 18 | } 19 | return true; 20 | } 21 | 22 | 23 | public static int willSave(int[] a,int n, int k){ 24 | int power=k; 25 | for(int i=0;i "+willSave(a,6,3)); 45 | 46 | // Scanner s = new Scanner(System.in); 47 | // int testCases=s.nextInt(); 48 | // for (int i=0; i "+willSave(a,n,k)); 56 | // } 57 | } 58 | } -------------------------------------------------------------------------------- /java practise/consecutiveInteger.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.util.Scanner; 3 | 4 | public class consecutiveInteger{ 5 | 6 | public static int [] sortArray(int [] a){ 7 | 8 | for (int i=0;i1){ 28 | max_count++; 29 | System.out.println(String.valueOf(max_count)); 30 | break; 31 | }else{ 32 | max_count++; 33 | } 34 | } 35 | 36 | } 37 | 38 | public static void main(String[] args) { 39 | 40 | int[] a={2, 6, 1, 9, 4, 5, 3}; 41 | 42 | checkConsecutive(a); 43 | } 44 | } -------------------------------------------------------------------------------- /java practise/maxCoins.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.util.Scanner; 3 | 4 | public class maxCoins{ 5 | 6 | public static int max_near(int [] a,int c ){ 7 | for(int i=0;ic){ 9 | return a[i-1]; 10 | } 11 | } 12 | return 0; 13 | } 14 | 15 | public static void findMaxCoins(int[] a, int c, int max_coins){ 16 | if(c==0){ 17 | System.out.println("Max Coins: "+max_coins); 18 | return; 19 | } 20 | int coinValue=max_near(a,c); 21 | System.out.println(c/coinValue+" Coin needed: "+coinValue); 22 | max_coins=max_coins+(c/coinValue); 23 | c=c%coinValue; 24 | findMaxCoins(a,c,max_coins); 25 | } 26 | 27 | public static void main(String[] args) { 28 | int[] a={1,2,5,10,20,50,100,500,1000}; 29 | int max_coins=0; 30 | Scanner s =new Scanner(System.in); 31 | System.out.println("enter currency:"); 32 | int c=s.nextInt(); 33 | findMaxCoins(a,c,max_coins); 34 | } 35 | } -------------------------------------------------------------------------------- /java practise/minimumOperation.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.util.Scanner; 3 | 4 | public class minimumOperation{ 5 | 6 | public static void minimumOperationNumber(int n,int max_count){ 7 | 8 | if(n<=3){ 9 | max_count=max_count+n; 10 | System.out.println("count: "+max_count); 11 | return; 12 | } 13 | 14 | if(n%2==0){ 15 | max_count=max_count+1; 16 | }else{ 17 | max_count=max_count+2; 18 | } 19 | minimumOperationNumber(n/2,max_count); 20 | 21 | } 22 | 23 | public static void main(String[] args) { 24 | int n=8; 25 | int max_count=0; 26 | minimumOperationNumber(n,max_count); 27 | } 28 | } -------------------------------------------------------------------------------- /java practise/nMeetings.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.util.Scanner; 3 | 4 | public class nMeetings{ 5 | 6 | public static void sort(int [] a,int [] b ){ 7 | for(int i=0;ib[j+1]){ 10 | int t=a[j+1]; 11 | a[j+1]=a[j]; 12 | a[j]=t; 13 | 14 | t=b[j+1]; 15 | b[j+1]=b[j]; 16 | b[j]=t; 17 | } 18 | } 19 | } 20 | } 21 | 22 | public static void findMaxMeetings(int [] a, int [] b ){ 23 | sort(a,b); 24 | int max_count=-1; 25 | for (int i=0;i=finish_prev_time){ 32 | count++; 33 | } 34 | } 35 | if(max_count(j-i)){ 15 | repeatAlpha=ch; 16 | diff=j-i; 17 | } 18 | } 19 | } 20 | } 21 | System.out.println("Repeat character is: "+repeatAlpha); 22 | } 23 | 24 | public static void main(String[] args) { 25 | Scanner s = new Scanner(System.in); 26 | System.out.println("Enter a string"); 27 | 28 | String k=s.nextLine(); 29 | 30 | System.out.println("String is: "+k); 31 | 32 | checkRepeatAlpha(k); 33 | 34 | 35 | } 36 | } -------------------------------------------------------------------------------- /java practise/subset_sum.py: -------------------------------------------------------------------------------- 1 | def subset_sum(a,sumn,i,l): 2 | if(i==l): 3 | print ("sum",sumn) 4 | return 5 | 6 | print ("ele",a[i]) 7 | subset_sum(a,sumn+a[i],i+1,l) 8 | print ("prevsum",sumn) 9 | print ("prevelem",a[i]) 10 | subset_sum(a,sumn,i+1,l) 11 | 12 | 13 | a=[1,2,3] 14 | subset_sum(a,0,0,len(a)) 15 | -------------------------------------------------------------------------------- /josephus.c: -------------------------------------------------------------------------------- 1 | #include 2 | int n,k; 3 | void kill(int i,int arr[],int totalperson,int counter){ 4 | 5 | if(totalperson==1){ 6 | for(int i=1;i<=n;i++){ 7 | if(arr[i]==0){ 8 | printf("%d\n",i); 9 | break; 10 | } 11 | } 12 | return; 13 | } 14 | 15 | if(i>n){ 16 | i=1; 17 | } 18 | 19 | if(counter==k-1 && arr[i]!=-1){ 20 | totalperson=totalperson-1; 21 | arr[i]=-1; 22 | counter=0; 23 | }else if(arr[i]!=-1){ 24 | counter=counter+1; 25 | } 26 | 27 | kill(i+1,arr,totalperson,counter); 28 | 29 | } 30 | 31 | int main() 32 | { 33 | scanf("%d %d",&n,&k); 34 | int arr[n+1]; 35 | for(int i=1;i<=n;i++){ 36 | arr[i]=0; 37 | } 38 | int totalperson=n; 39 | int counter=0; 40 | kill(1,arr,totalperson,counter); 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /kadanes.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to print largest contiguous array sum 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void maxSubArraySum(int a[], int size) 7 | { 8 | int max_so_far = INT_MIN, max_ending_here = 0, 9 | start =0, end = 0, s=0; 10 | 11 | for (int i=0; i< size; i++ ) 12 | { 13 | max_ending_here += a[i]; 14 | 15 | if (max_so_far < max_ending_here) 16 | { 17 | max_so_far = max_ending_here; 18 | start = s; 19 | end = i; 20 | } 21 | 22 | if (max_ending_here < 0) 23 | { 24 | max_ending_here = 0; 25 | s = i + 1; 26 | } 27 | } 28 | cout << "Maximum contiguous sum is " 29 | << max_so_far << endl; 30 | cout << "Starting index "<< start 31 | << endl << "Ending index "<< end << endl; 32 | } 33 | 34 | /*Driver program to test maxSubArraySum*/ 35 | int main() 36 | { 37 | int a[] = {-2, -3, 4, -1, -2, 1, 5, -3}; 38 | int n = sizeof(a)/sizeof(a[0]); 39 | maxSubArraySum(a, n); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /leetcode/**Maximum_Product_Subarray.cpp: -------------------------------------------------------------------------------- 1 | // Find the contiguous subarray within an array (containing at least one number) which has the largest product. 2 | 3 | // For example, given the array [2,3,-2,4], 4 | // the contiguous subarray [2,3] has the largest product = 6. 5 | 6 | 7 | class Solution { 8 | public: 9 | int maxProduct(vector& a) { 10 | int m=a[0]; 11 | int imax=a[0]; 12 | int imin=a[0]; 13 | for(int i=1;i& nums) { 19 | int c=0; 20 | if(nums.size()==0){ 21 | return c; 22 | } 23 | unordered_set myset(nums.begin(),nums.end()); 24 | for(int m : nums){ 25 | if(myset.find(m)==myset.end()){ 26 | continue; 27 | }else{ 28 | myset.erase(m); 29 | int prev=m-1; 30 | int next=m+1; 31 | while(myset.find(prev)!=myset.end()){ 32 | myset.erase(prev--); 33 | } 34 | while(myset.find(next)!=myset.end()){ 35 | myset.erase(next++); 36 | } 37 | c=max(c,next-prev-1); 38 | } 39 | } 40 | return c; 41 | } 42 | }; -------------------------------------------------------------------------------- /leetcode/**bitwiseAND.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 201. Bitwise AND of Numbers Range 4 | 5 | Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. 6 | 7 | For example, given the range [5, 7], you should return 4. 8 | 9 | Credits: 10 | Special thanks to @amrsaqr for adding this problem and creating all test cases. 11 | 12 | class Solution { 13 | public: 14 | int rangeBitwiseAnd(int m, int n) { 15 | int k = m; 16 | m = m + 1; 17 | while(m<=n){ 18 | if(k==0){ 19 | break; 20 | } 21 | k = k & m; 22 | if(m==INT_MAX){ 23 | break; 24 | } 25 | m = m + 1; 26 | } 27 | return k; 28 | } 29 | }; 30 | */ 31 | 32 | class Solution { 33 | public: 34 | int rangeBitwiseAnd(int m, int n) { 35 | if(m==0){ 36 | return 0; 37 | } 38 | int count=0; 39 | while(m!=n){ 40 | m=m>>1; 41 | n=n>>1; 42 | count++; 43 | } 44 | return m<left; 17 | } 18 | return h; 19 | } 20 | int rightdepth(TreeNode* root){ 21 | int h=0; 22 | while(root!=NULL){ 23 | h+=1; 24 | root=root->right; 25 | } 26 | return h; 27 | } 28 | int countNodes(TreeNode* root) { 29 | if(root==NULL){ 30 | return 0; 31 | } 32 | int ld=leftdepth(root); 33 | int rd=rightdepth(root); 34 | if(ld==rd){ 35 | return pow(2,ld)-1; 36 | }else{ 37 | return 1+countNodes(root->left)+countNodes(root->right); 38 | } 39 | return 0; 40 | } 41 | }; -------------------------------------------------------------------------------- /leetcode/**greaterTree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | 538. Convert BST to Greater Tree 4 | 5 | 6 | Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. 7 | 8 | Example: 9 | 10 | Input: The root of a Binary Search Tree like this: 11 | 5 12 | / \ 13 | 2 13 14 | 15 | Output: The root of a Greater Tree like this: 16 | 18 17 | / \ 18 | 20 13 19 | 20 | * Definition for a binary tree node. 21 | * struct TreeNode { 22 | * int val; 23 | * TreeNode *left; 24 | * TreeNode *right; 25 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 26 | * }; 27 | */ 28 | class Solution { 29 | public: 30 | int s=0; 31 | TreeNode* greater(TreeNode* root) { 32 | if(!root){ 33 | return NULL; 34 | } 35 | greater(root->right); 36 | root->val=root->val+s; 37 | s=root->val; 38 | greater(root->left); 39 | return root; 40 | } 41 | TreeNode* convertBST(TreeNode* root) { 42 | if(!root){ 43 | return NULL; 44 | } 45 | return greater(root); 46 | } 47 | }; -------------------------------------------------------------------------------- /leetcode/**integerBreak.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 343. Integer Break 3 | Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. 4 | 5 | For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). 6 | 7 | Note: You may assume that n is not less than 2 and not larger than 58. 8 | */ 9 | class Solution { 10 | public: 11 | int integerBreak(int n) { 12 | if(n==2){ 13 | return 1; 14 | } 15 | if(n==3){ 16 | return 2; 17 | } 18 | int p=1; 19 | while(n>4){ 20 | p*=3; 21 | n=n-3; 22 | } 23 | p*=n; 24 | return p; 25 | } 26 | }; -------------------------------------------------------------------------------- /leetcode/**lexographic_sort_numbers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | // int tointeger(string a){ 4 | // stringstream geek(a); 5 | // int x = 0; 6 | // geek >> x; 7 | // return x; 8 | // } 9 | // vector t; 10 | // void addString(string a,int n){ 11 | // if(tointeger(a)>n){ 12 | // return; 13 | // } 14 | // t.push_back(tointeger(a)); 15 | // for(int i=0;i<=9;i++){ 16 | // a.append(to_string(i)); 17 | // addString(a,n); 18 | // a.pop_back(); 19 | // } 20 | // } 21 | vector lexicalOrder(int n) { 22 | vector ans; 23 | // int p = tointeger(to_string(to_string(n)[0])); 24 | // for(int i=1;i<=9;i++){ 25 | // string a = to_string(i); 26 | // addString(a,n); 27 | // } 28 | int a=1; 29 | for(int i=1;i<=n;i++){ 30 | ans.push_back(a); 31 | if(a*10<=n){ 32 | a=a*10; 33 | }else if(a%10!=9 && a+1<=n){ 34 | a=a+1; 35 | }else{ 36 | while((a/10)%10==9){ 37 | a=a/10; 38 | } 39 | a=a/10+1; 40 | } 41 | } 42 | return ans; 43 | } 44 | }; -------------------------------------------------------------------------------- /leetcode/**max_jump_array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 45. Jump Game II 3 | Given an array of non-negative integers, you are initially positioned at the first index of the array. 4 | 5 | Each element in the array represents your maximum jump length at that position. 6 | 7 | Your goal is to reach the last index in the minimum number of jumps. 8 | 9 | For example: 10 | Given array A = [2,3,1,1,4] 11 | 12 | The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.) 13 | 14 | Note: 15 | You can assume that you can always reach the last index. 16 | */ 17 | class Solution { 18 | public: 19 | int jump(vector& n) { 20 | if(n.size()<=1){ 21 | return 0; 22 | } 23 | if(n[0]==0){ 24 | return -1; 25 | } 26 | int maxReach=n[0]; 27 | int steps=n[0]; 28 | int i=1; 29 | int count=1; 30 | for(i=1;i=n.size()-1){ 32 | return count; 33 | } 34 | maxReach=max(maxReach,n[i]+i); 35 | steps--; 36 | if(steps==0){ 37 | count++; 38 | steps=maxReach-i; 39 | } 40 | } 41 | return count; 42 | } 43 | }; -------------------------------------------------------------------------------- /leetcode/**maximalSquare.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 221. Maximal Square 3 | 4 | Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. 5 | 6 | For example, given the following matrix: 7 | 8 | 1 0 1 0 0 9 | 1 0 1 1 1 10 | 1 1 1 1 1 11 | 1 0 0 1 0 12 | */ 13 | class Solution { 14 | public int maximalSquare(char[][] matrix) { 15 | int rows = matrix.length, cols = rows > 0 ? matrix[0].length : 0; 16 | int[][] dp = new int[rows + 1][cols + 1]; 17 | int maxsqlen = 0; 18 | for (int i = 1; i <= rows; i++) { 19 | for (int j = 1; j <= cols; j++) { 20 | if (matrix[i-1][j-1] == '1'){ 21 | dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1; 22 | maxsqlen = Math.max(maxsqlen, dp[i][j]); 23 | } 24 | } 25 | } 26 | return maxsqlen * maxsqlen; 27 | } 28 | } -------------------------------------------------------------------------------- /leetcode/**numberDigitOne.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 233. Number of Digit One 3 | 4 | Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. 5 | 6 | For example: 7 | Given n = 13, 8 | Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 9 | 10 | 11 | */ 12 | class Solution { 13 | public: 14 | int lengthDigit(int n){ 15 | int c=0; 16 | while(n>0){ 17 | c++; 18 | n=n/10; 19 | } 20 | return c; 21 | } 22 | int countDigitOne(int n) { 23 | if(n<=0){ 24 | return 0; 25 | }else if(n<10){ 26 | return 1; 27 | } 28 | int length=lengthDigit(n); 29 | int base=pow(10,length-1); 30 | int answer=n/base; 31 | int remainder=n%base; 32 | int m; 33 | if(answer==1){ 34 | m=n-base+1; 35 | }else{ 36 | m=base; 37 | } 38 | return countDigitOne(base-1)*answer+m+countDigitOne(remainder); 39 | } 40 | }; -------------------------------------------------------------------------------- /leetcode/**pathSumWithoutRoot.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | 437. Path Sum III 4 | 5 | 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 12 | * }; 13 | */ 14 | class Solution { 15 | public: 16 | int sumUtil(TreeNode* root,int sum,int c){ 17 | if(root==NULL){ 18 | return 0; 19 | } 20 | c=c+root->val; 21 | return (sum==c)+sumUtil(root->left,sum,c) + sumUtil(root->right,sum,c); 22 | } 23 | int pathSum(TreeNode* root, int sum) { 24 | if(!root){ 25 | return 0; 26 | } 27 | return sumUtil(root,sum,0)+pathSum(root->left,sum)+pathSum(root->right,sum); 28 | } 29 | }; -------------------------------------------------------------------------------- /leetcode/**rectArea.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 223. Rectangle Area 3 | 4 | Find the total area covered by two rectilinear rectangles in a 2D plane. 5 | 6 | Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. 7 | 8 | Rectangle Area 9 | */ 10 | class Solution { 11 | public: 12 | int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { 13 | int first_triangle=(C-A)*(D-B); 14 | int second_triangle=(G-E)*(H-F); 15 | int overlap=0; 16 | int l=max(E,A); 17 | int r=min(G,C); 18 | int t=max(F,B); 19 | int b=min(D,H); 20 | if(l& wd){ 4 | for( string st : wd){ 5 | if(s==st){ 6 | return true; 7 | } 8 | } 9 | return false; 10 | } 11 | bool wordBreak(string str, vector& wd) { 12 | 13 | int size = str.size(); 14 | if (size == 0) return true; 15 | bool wb[size+1]; 16 | memset(wb, 0, sizeof(wb)); 17 | for (int i=1; i<=size; i++) 18 | { 19 | if (wb[i] == false && findDict(str.substr(0, i),wd)) 20 | wb[i] = true; 21 | 22 | if (wb[i] == true) 23 | { 24 | if (i == size) 25 | return true; 26 | for (int j = i+1; j <= size; j++) 27 | { 28 | if (wb[j] == false && findDict(str.substr(i, j-i),wd)) 29 | wb[j] = true; 30 | 31 | if (j == size && wb[j] == true) 32 | return true; 33 | } 34 | } 35 | } 36 | return false; 37 | } 38 | }; -------------------------------------------------------------------------------- /leetcode/*combination.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 77. Combinations 3 | Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. 4 | 5 | For example, 6 | If n = 4 and k = 2, a solution is: 7 | 8 | [ 9 | [2,4], 10 | [3,4], 11 | [2,3], 12 | [1,2], 13 | [1,3], 14 | [1,4], 15 | ] 16 | */ 17 | class Solution { 18 | public: 19 | void printvector(vector nr){ 20 | for(int i=0;i nr,int k,vector> &a,int s,vector sm){ 26 | if(sm.size()==k){ 27 | // printvector(sm); 28 | a.push_back(sm); 29 | return; 30 | } 31 | for(int i=s;i> combine(int n, int k) { 41 | vector> a; 42 | vector nr; 43 | for(int i=1;i<=n;i++){ 44 | nr.push_back(i); 45 | } 46 | vector sm; 47 | backtrack(nr,k,a,0,sm); 48 | return a; 49 | } 50 | }; -------------------------------------------------------------------------------- /leetcode/*minimumMOves(n-1)ele.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 453. Minimum Moves to Equal Array Elements 3 | Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. 4 | 5 | Example: 6 | 7 | Input: 8 | [1,2,3] 9 | 10 | Output: 11 | 3 12 | 13 | Explanation: 14 | Only three moves are needed (remember each move increments two elements): 15 | 16 | [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4] 17 | */ 18 | class Solution { 19 | public: 20 | int minMoves(vector& nums) { 21 | int s=0; 22 | int min=nums[0]; 23 | for(int i=0;inums[i]){ 26 | min=nums[i]; 27 | } 28 | } 29 | return s-min*nums.size(); 30 | } 31 | }; -------------------------------------------------------------------------------- /leetcode/*uglyNumber.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 263. Ugly Number 4 | 5 | Write a program to check whether a given number is an ugly number. 6 | 7 | Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. 8 | 9 | Note that 1 is typically treated as an ugly number. 10 | */ 11 | class Solution { 12 | public: 13 | bool isPrime(int t){ 14 | for(int i=2;i<=t/2;i++){ 15 | if(t%i==0){ 16 | return false; 17 | } 18 | } 19 | return true; 20 | } 21 | bool isUgly(int num) { 22 | if(num<0){ 23 | return false; 24 | } 25 | if(num==1){ 26 | return true; 27 | } 28 | if(num==0 || num==INT_MAX){ 29 | return false; 30 | } 31 | while(num%2==0){ 32 | num=num/2; 33 | } 34 | while(num%3==0){ 35 | num=num/3; 36 | } 37 | while(num%5==0){ 38 | num=num/5; 39 | } 40 | if(num==1){ 41 | return true; 42 | } 43 | return false; 44 | } 45 | }; -------------------------------------------------------------------------------- /leetcode/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mayankagg9722/Placement-Preparation/30593f86d1497adecca98e30a439368771c11061/leetcode/.DS_Store -------------------------------------------------------------------------------- /leetcode/MaximumNumberofNon-OverlappingSubarraysWithSumEqualsTarget.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxNonOverlapping(vector& nums, int target) { 4 | int n=nums.size(); 5 | int total=0; 6 | int count=0; 7 | map m; 8 | for(int i=0;i& a) { 4 | int m=a[0]; 5 | int maxtill=a[0]; 6 | for(int i=1;i>& a) { 4 | vector pos; 5 | for(int i=0;i>& triangle) { 21 | int s=INT_MAX; 22 | for(int i=1;i t = triangle[i-1]; 24 | int size = triangle[i].size(); 25 | triangle[i][0]=triangle[i][0]+t[0]; 26 | triangle[i][size-1]=triangle[i][size-1]+t[size-2]; 27 | for(int j=1;jtriangle[triangle.size()-1][j]){ 33 | s=triangle[triangle.size()-1][j]; 34 | } 35 | } 36 | return s; 37 | } 38 | }; -------------------------------------------------------------------------------- /leetcode/addDigits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 258. Add Digits 3 | 4 | Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. 5 | 6 | For example: 7 | 8 | Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. 9 | 10 | Follow up: 11 | Could you do it without any loop/recursion in O(1) runtime? 12 | */ 13 | class Solution { 14 | public: 15 | int count(int a){ 16 | int c=0; 17 | while(a){ 18 | c++; 19 | a=a/10; 20 | } 21 | return c; 22 | } 23 | int add(int a){ 24 | int c=0; 25 | while(a){ 26 | int k=a%10; 27 | c+=k; 28 | a=a/10; 29 | } 30 | return c; 31 | } 32 | int addDigits(int num) { 33 | while(num!=0 && count(num)!=1){ 34 | num=add(num); 35 | } 36 | return num; 37 | } 38 | }; -------------------------------------------------------------------------------- /leetcode/allPathsofSum.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | 113. Path Sum II 4 | 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 11 | * }; 12 | */ 13 | class Solution { 14 | public: 15 | void sumUtil(TreeNode* root,int sum,int c,vector> &a,vector small){ 16 | if(root==NULL){ 17 | return; 18 | } 19 | small.push_back(root->val); 20 | c=c+root->val; 21 | if(root->left==NULL && root->right==NULL){ 22 | if(sum==c){ 23 | a.push_back(small); 24 | } 25 | } 26 | sumUtil(root->left,sum,c,a,small); 27 | sumUtil(root->right,sum,c,a,small); 28 | small.pop_back(); 29 | } 30 | vector> pathSum(TreeNode* root, int sum) { 31 | vector> a; 32 | vector small; 33 | sumUtil(root,sum,0,a,small); 34 | return a; 35 | } 36 | }; -------------------------------------------------------------------------------- /leetcode/arrangingCoins.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 441. Arranging Coins 3 | 4 | You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. 5 | 6 | Given n, find the total number of full staircase rows that can be formed. 7 | 8 | n is a non-negative integer and fits within the range of a 32-bit signed integer. 9 | 10 | Example 1: 11 | 12 | n = 5 13 | 14 | The coins can form the following rows: 15 | ¤ 16 | ¤ ¤ 17 | ¤ ¤ 18 | 19 | Because the 3rd row is incomplete, we return 2. 20 | Example 2: 21 | 22 | n = 8 23 | 24 | The coins can form the following rows: 25 | ¤ 26 | ¤ ¤ 27 | ¤ ¤ ¤ 28 | ¤ ¤ 29 | 30 | Because the 4th row is incomplete, we return 3. 31 | */ 32 | class Solution { 33 | public: 34 | int arrangeCoins(int n) { 35 | return (int)((-1+sqrt(1+(long)8*n))/2); 36 | } 37 | }; -------------------------------------------------------------------------------- /leetcode/badVesrion.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 278. First Bad Version 3 | DescriptionHintsSubmissionsDiscussSolution 4 | You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. 5 | 6 | Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad. 7 | 8 | You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API. 9 | 10 | */ 11 | // Forward declaration of isBadVersion API. 12 | 13 | //remember if the numbers are too large the to find mid dont add up the two nos 14 | //just do s+(e-s)/2 and then find mid also avoid recussion in such cases. 15 | bool isBadVersion(int version); 16 | 17 | class Solution { 18 | public: 19 | int firstBadVersion(int n) { 20 | int s=1; 21 | int e=n; 22 | while(s& n) { 17 | if(n.size()<=1){ 18 | return true; 19 | } 20 | if(n[0]==0){ 21 | return false; 22 | } 23 | int steps=n[0]; 24 | int maxReach=n[0]; 25 | int jumps=1; 26 | int i=1; 27 | for(i=1;i=n.size()-1){ 32 | return true; 33 | } 34 | maxReach=max(maxReach,n[i]+i); 35 | steps--; 36 | if(steps==0){ 37 | jumps++; 38 | steps=maxReach-i; 39 | } 40 | } 41 | return false; 42 | } 43 | }; -------------------------------------------------------------------------------- /leetcode/combination_sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void printsmall(vector small){ 4 | for(int i=0;i& cand, int target,set> &mylist,int sum,vector small){ 10 | if(sum>target){ 11 | return; 12 | } 13 | if(sum==target){ 14 | sort(small.begin(),small.end()); 15 | mylist.insert(small); 16 | } 17 | for(int i=0;i> combinationSum(vector& cand, int target) { 26 | set> mylist; 27 | vector> t; 28 | vector small; 29 | int sum=0; 30 | findComb(cand,target,mylist,sum,small); 31 | set> :: iterator it; 32 | for(it=mylist.begin();it!=mylist.end();it++){ 33 | t.push_back(*it); 34 | } 35 | return t; 36 | } 37 | }; -------------------------------------------------------------------------------- /leetcode/contain_duplicate.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 217. Contains Duplicate 3 | 4 | Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 5 | */ 6 | class Solution { 7 | public: 8 | bool containsDuplicate(vector& n) { 9 | unordered_map m; 10 | for(int i=0;i& nums, int k) { 4 | unordered_set s; 5 | if (k <= 0) return false; 6 | if (k >= nums.size()) k = nums.size() - 1; 7 | for (int i = 0; i < nums.size(); i++) 8 | { 9 | if (i > k){ 10 | s.erase(nums[i - k - 1]); 11 | } 12 | if (s.find(nums[i]) != s.end()){ 13 | return true; 14 | } 15 | s.insert(nums[i]); 16 | } 17 | return false; 18 | } 19 | }; -------------------------------------------------------------------------------- /leetcode/count1_32bit.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 191. Number of 1 Bits 3 | 4 | Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). 5 | 6 | For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3. 7 | */ 8 | class Solution { 9 | public: 10 | int hammingWeight(uint32_t n) { 11 | int c=0; 12 | while(n){ 13 | n = n & (n-1); 14 | c++; 15 | } 16 | return c; 17 | } 18 | }; -------------------------------------------------------------------------------- /leetcode/countPrimes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 204. Count Primes 3 | 4 | Description: 5 | 6 | Count the number of prime numbers less than a non-negative number, n. 7 | */ 8 | class Solution { 9 | public: 10 | int countPrimes(int n) { 11 | vector a; 12 | for(int i=0;i<=n;i++){ 13 | a.push_back(false); 14 | } 15 | int k=1; 16 | for(int i=2;i<=n;i++){ 17 | int t=2; 18 | while((i*t)<=n){ 19 | a[(i*t)]=true; 20 | t++; 21 | } 22 | } 23 | int count=0; 24 | for(int i=2;i a; 16 | if(n>=1){ 17 | a.push_back(10); 18 | } 19 | if(n>=2){ 20 | a.push_back(9*9); 21 | } 22 | int k=8; 23 | while(a.size() countBits(int num) { 12 | vector a; 13 | for(int i=0;i<=num;i++){ 14 | int c=0; 15 | int k=i; 16 | while(k){ 17 | if(k & 1){ 18 | c++; 19 | } 20 | k = k >> 1; 21 | } 22 | a.push_back(c); 23 | } 24 | return a; 25 | } 26 | }; -------------------------------------------------------------------------------- /leetcode/decodeways.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int numDecodings(string s,int n) { 4 | if(n==0 || n==1){ 5 | return 1; 6 | } 7 | int count=0; 8 | if(s[n-1]>'0'){ 9 | count=numDecodings(s,n-1); 10 | } 11 | if((s[n-2]=='1' || s[n-2]=='2') && s[n-1]<'7'){ 12 | count=count+numDecodings(s,n-2); 13 | } 14 | return count; 15 | } 16 | 17 | int main(){ 18 | if(s.length()==0){ 19 | return 0; 20 | } 21 | if(s[0]=='0'){ 22 | return 0; 23 | } 24 | int count=numDecodings("1234",4); 25 | cout< findDiagonalOrder(vector>& m) { 22 | vector ans; 23 | int i=0; 24 | int j=0; 25 | int r=m.size(); 26 | if(r==0){ 27 | return ans; 28 | } 29 | int c=m[0].size(); 30 | vector> a(r+c-1,vector()); 31 | for(int i=0;i b:a){ 38 | if(k!=0 && k%2==0){ 39 | reverse(b.begin(),b.end()); 40 | } 41 | for(int i:b){ 42 | ans.push_back(i); 43 | } 44 | k++; 45 | } 46 | return ans; 47 | } 48 | }; -------------------------------------------------------------------------------- /leetcode/diameterBT.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | int m=0; 13 | int findDia(TreeNode* root){ 14 | if(!root){ 15 | return 0; 16 | } 17 | int a = findDia(root->left); 18 | int b = findDia(root->right); 19 | m=max(m,a+b); 20 | return 1+max(a,b); 21 | } 22 | int diameterOfBinaryTree(TreeNode* root) { 23 | if(!root){ 24 | return 0; 25 | } 26 | findDia(root); 27 | return m; 28 | } 29 | }; -------------------------------------------------------------------------------- /leetcode/dungeonGame.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 174. Dungeon Game 3 | The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess. 4 | 5 | The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately. 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int sethealth(int i){ 11 | if(i<=0){ 12 | return 1; 13 | } 14 | return i; 15 | } 16 | int calculateMinimumHP(vector>& g) { 17 | int m=g.size(); 18 | int n=g[0].size(); 19 | int dp[m][n]; 20 | dp[m-1][n-1]=sethealth(1-g[m-1][n-1]); 21 | for(int i=m-2;i>=0;i--){ 22 | dp[i][n-1]=sethealth(dp[i+1][n-1]-g[i][n-1]); 23 | } 24 | for(int i=n-2;i>=0;i--){ 25 | dp[m-1][i]=sethealth(dp[m-1][i+1]-g[m-1][i]); 26 | } 27 | for(int i=m-2;i>=0;i--){ 28 | for(int j=n-2;j>=0;j--){ 29 | dp[i][j]=sethealth(min(dp[i+1][j],dp[i][j+1])-g[i][j]); 30 | } 31 | } 32 | return dp[0][0]; 33 | } 34 | }; -------------------------------------------------------------------------------- /leetcode/duplicateNumber.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 287. Find the Duplicate Number 3 | Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. 4 | 5 | Note: 6 | You must not modify the array (assume the array is read only). 7 | You must use only constant, O(1) extra space. 8 | Your runtime complexity should be less than O(n2). 9 | 10 | */ 11 | class Solution { 12 | public: 13 | int findDuplicate(vector& n) { 14 | unordered_map mp; 15 | for(int i:n){ 16 | if(mp.find(i)!=mp.end()){ 17 | return i; 18 | } 19 | mp[i]++; 20 | } 21 | return 0; 22 | } 23 | }; -------------------------------------------------------------------------------- /leetcode/duplicatesArray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 442. Find All Duplicates in an Array 3 | 4 | Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. 5 | 6 | Find all the elements that appear twice in this array. 7 | 8 | Could you do it without extra space and in O(n) runtime? 9 | 10 | Example: 11 | Input: 12 | [4,3,2,7,8,2,3,1] 13 | 14 | Output: 15 | [2,3] 16 | 17 | */ 18 | class Solution { 19 | public: 20 | vector findDuplicates(vector& nums) { 21 | vector s; 22 | for(int i=0;i0){ 25 | s.push_back(abs(nums[i])); 26 | } 27 | } 28 | return s; 29 | } 30 | }; -------------------------------------------------------------------------------- /leetcode/dutch_national.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void sortColors(vector& a) { 4 | int i=0; 5 | int j=a.size()-1; 6 | int k=0; 7 | while(k<=j){ 8 | if(a[k]==0 && i!=k){ 9 | int t=a[i]; 10 | a[i]=a[k]; 11 | a[k]=t; 12 | i++; 13 | }else if(a[k]==2 && j!=k){ 14 | int t=a[j]; 15 | a[j]=a[k]; 16 | a[k]=t; 17 | j--; 18 | }else{ 19 | k++; 20 | } 21 | } 22 | } 23 | }; -------------------------------------------------------------------------------- /leetcode/dynamic_path_matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minPathSum(vector>& grid) { 4 | vector> nm=grid; 5 | for(int i=0;i mp; 30 | void util(TreeNode *root,int k){ 31 | if(!root){ 32 | return; 33 | } 34 | if(mp.find(k)!=mp.end()){ 35 | mp[k]=max(mp[k],root->val); 36 | }else{ 37 | mp[k]=root->val; 38 | } 39 | util(root->left,k+1); 40 | util(root->right,k+1); 41 | } 42 | vector largestValues(TreeNode* root) { 43 | util(root,0); 44 | vector a; 45 | for(pair p:mp){ 46 | a.push_back(p.second); 47 | } 48 | return a; 49 | } 50 | }; -------------------------------------------------------------------------------- /leetcode/findPeak.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 162. Find Peak Element 3 | 4 | A peak element is an element that is greater than its neighbors. 5 | 6 | Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. 7 | 8 | The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. 9 | 10 | You may imagine that num[-1] = num[n] = -∞. 11 | 12 | For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2. 13 | */ 14 | class Solution { 15 | public: 16 | int findPeakElement(vector& n) { 17 | int i=0; 18 | int res=0; 19 | int j=n.size()-1; 20 | while(i<=j){ 21 | if(i==0 && n[i]>n[i+1] && n[i]>INT_MIN){ 22 | res=0; 23 | break; 24 | } 25 | if(j==n.size()-1 && n[j]>n[j-1] && n[j]>INT_MIN){ 26 | res=j; 27 | break; 28 | } 29 | if(n[i]>n[i+1] && n[i]>n[i-1]){ 30 | res=i; 31 | break; 32 | } 33 | if(n[j]>n[j+1] && n[j]>n[j-1]){ 34 | res=j; 35 | break; 36 | } 37 | i++; 38 | j--; 39 | } 40 | return res; 41 | } 42 | }; -------------------------------------------------------------------------------- /leetcode/firstUnoqueString.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstUniqChar(string s) { 4 | int a[26]; 5 | for(int i=0;i<26;i++){ 6 | a[i]=0; 7 | } 8 | for(int i : s){ 9 | a[i-97]++; 10 | } 11 | int k=0; 12 | for(int i : s){ 13 | if(a[i-97]==1){ 14 | return k; 15 | } 16 | k++; 17 | } 18 | return -1; 19 | } 20 | }; -------------------------------------------------------------------------------- /leetcode/first_missing_positive.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void putAgain(int s,int i,int val,vector &nums){ 4 | if(val<=0 || nums.size()& nums) { 12 | int n = nums.size(); 13 | if(nums.size()==0){ 14 | return 1; 15 | } 16 | int i=0; 17 | while(inums.size() || nums[i]==nums[nums[i]-1]){ 19 | i++; 20 | }else if(i+1!=nums[i]){ 21 | putAgain(i,nums[i]-1,nums[i],nums); 22 | } 23 | } 24 | for(i=0;i fizzBuzz(int n) { 34 | vector a; 35 | for(int i=1;i<=n;i++){ 36 | if(i%3!=0 && i%5!=0){ 37 | a.push_back(to_string(i)); 38 | }else if(i%3==0 && i%5==0){ 39 | a.push_back("FizzBuzz"); 40 | }else if(i%3==0 && i%5!=0){ 41 | a.push_back("Fizz"); 42 | }else if(i%3!=0 && i%5==0){ 43 | a.push_back("Buzz"); 44 | } 45 | } 46 | return a; 47 | } 48 | }; -------------------------------------------------------------------------------- /leetcode/graycode.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | stack toBinary(int n){ 4 | stack b; 5 | if(n==0){ 6 | b.push(0); 7 | return b; 8 | } 9 | while(n>1){ 10 | int r = n%2; 11 | b.push(r); 12 | n=n/2; 13 | } 14 | b.push(1); 15 | return b; 16 | } 17 | vector grayCode(int n) { 18 | vector finalVect; 19 | for(int i=0;i<(1< a; 21 | stack b = toBinary(i); 22 | while(b.size()>0){ 23 | a.push_back(b.top()); 24 | b.pop(); 25 | } 26 | int binary=0; 27 | int k=a.size()-1; 28 | for(int i=0;i> 1; 36 | y = y >> 1; 37 | } 38 | while(x){ 39 | if(x&1){ 40 | c++; 41 | } 42 | x = x >> 1; 43 | } 44 | while(y){ 45 | if(y&1){ 46 | c++; 47 | } 48 | y = y >> 1; 49 | } 50 | return c; 51 | } 52 | }; -------------------------------------------------------------------------------- /leetcode/houserobberII.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int robUtil(int s,int e,vector nums){ 4 | int odd=0; 5 | int even=0; 6 | for(int i=s;i<=e;i++){ 7 | if(i%2==0){ 8 | even = max(even+nums[i],odd); 9 | }else{ 10 | odd = max(odd+nums[i],even); 11 | } 12 | } 13 | return max(even,odd); 14 | } 15 | int rob(vector& nums) { 16 | if(nums.size()==1){ 17 | return nums[0]; 18 | } 19 | return max(robUtil(0,nums.size()-2,nums),robUtil(1,nums.size()-1,nums)); 20 | } 21 | }; -------------------------------------------------------------------------------- /leetcode/in_pre_to_tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | 105. Construct Binary Tree from Preorder and Inorder Traversal 4 | 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 11 | * }; 12 | */ 13 | class Solution { 14 | public: 15 | int t=0; 16 | int findInorder(vector inorder,int val){ 17 | for(int i=0;i& preorder, vector& inorder){ 25 | if(start>end){ 26 | return NULL; 27 | } 28 | int v = preorder[t++]; 29 | TreeNode* newNode=new TreeNode(v); 30 | int k=findInorder(inorder,v); 31 | newNode->left=buildTreeUtil(start,k-1,preorder,inorder); 32 | newNode->right=buildTreeUtil(k+1,end,preorder,inorder); 33 | return newNode; 34 | } 35 | TreeNode* buildTree(vector& preorder, vector& inorder) { 36 | TreeNode* root=buildTreeUtil(0,inorder.size()-1,preorder,inorder); 37 | return root; 38 | } 39 | }; -------------------------------------------------------------------------------- /leetcode/integerReplacement.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 397. Integer Replacement 3 | 4 | Given a positive integer n and you can do operations as follow: 5 | 6 | If n is even, replace n with n/2. 7 | If n is odd, you can replace n with either n + 1 or n - 1. 8 | What is the minimum number of replacements needed for n to become 1? 9 | 10 | Example 1: 11 | 12 | Input: 13 | 8 14 | 15 | Output: 16 | 3 17 | 18 | Explanation: 19 | 8 -> 4 -> 2 -> 1 20 | Example 2: 21 | 22 | Input: 23 | 7 24 | 25 | Output: 26 | 4 27 | 28 | Explanation: 29 | 7 -> 8 -> 4 -> 2 -> 1 30 | or 31 | 7 -> 6 -> 3 -> 2 -> 1 32 | */ 33 | class Solution { 34 | public: 35 | int findUtil(int n,int ans){ 36 | if(n==1){ 37 | return ans; 38 | } 39 | if(n==INT_MAX){ 40 | return 32; 41 | } 42 | if(n%2==0){ 43 | return findUtil(n>>1,ans+1); 44 | } 45 | return min(findUtil(n+1,ans+1),findUtil(n-1,ans+1)); 46 | } 47 | int integerReplacement(int n) { 48 | return findUtil(n,0); 49 | } 50 | }; -------------------------------------------------------------------------------- /leetcode/intersectionArray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 349. Intersection of Two Arrays 3 | Given two arrays, write a function to compute their intersection. 4 | 5 | Example: 6 | Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. 7 | */ 8 | class Solution { 9 | public: 10 | vector intersection(vector& n1, vector& n2) { 11 | vector a; 12 | unordered_map mp; 13 | for(int i=0;isecond==1){ 18 | mp[n1[i]]=0; 19 | a.push_back(n1[i]); 20 | } 21 | } 22 | return a; 23 | } 24 | }; -------------------------------------------------------------------------------- /leetcode/invertTree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | 226. Invert Binary Tree 3 | 4 | Invert a binary tree. 5 | 4 6 | / \ 7 | 2 7 8 | / \ / \ 9 | 1 3 6 9 10 | to 11 | 4 12 | / \ 13 | 7 2 14 | / \ / \ 15 | 9 6 3 1 16 | 17 | Trivia: 18 | This problem was inspired by this original tweet by Max Howell: 19 | Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off. 20 | 21 | * Definition for a binary tree node. 22 | * struct TreeNode { 23 | * int val; 24 | * TreeNode *left; 25 | * TreeNode *right; 26 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 27 | * }; 28 | */ 29 | class Solution { 30 | public: 31 | TreeNode* invertUtil(TreeNode* root){ 32 | if(!root){ 33 | return NULL; 34 | } 35 | TreeNode* temp = root->right; 36 | root->right=root->left; 37 | root->left=temp; 38 | invertUtil(root->left); 39 | invertUtil(root->right); 40 | return root; 41 | } 42 | TreeNode* invertTree(TreeNode* root) { 43 | if(!root){ 44 | return NULL; 45 | } 46 | return invertUtil(root); 47 | } 48 | }; -------------------------------------------------------------------------------- /leetcode/keyboard.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 500. Keyboard Row 3 | Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below. 4 | 5 | 6 | American keyboard 7 | 8 | 9 | Example 1: 10 | Input: ["Hello", "Alaska", "Dad", "Peace"] 11 | Output: ["Alaska", "Dad"] 12 | */ 13 | class Solution { 14 | public: 15 | int finds(char c){ 16 | string s="qwertyuiop"; 17 | string s1="asdfghjkl"; 18 | string s2="zxcvbnm"; 19 | cout< findWords(vector& words) { 32 | vector ans; 33 | for(string s: words ){ 34 | int k=finds(s[0]); 35 | int f=0; 36 | for(char c: s){ 37 | if(k!=finds(c)){ 38 | f=-1; 39 | break; 40 | } 41 | } 42 | if(f==0){ 43 | ans.push_back(s); 44 | } 45 | } 46 | return ans; 47 | } 48 | }; -------------------------------------------------------------------------------- /leetcode/kthSmallestBST.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. 3 | 4 | Note: 5 | You may assume k is always valid, 1 ≤ k ≤ BST's total elements. 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 12 | * }; 13 | */ 14 | class Solution { 15 | public: 16 | int c=0; 17 | int ele; 18 | void findUtil(TreeNode* root,int k){ 19 | if(!root || c==k){ 20 | return; 21 | } 22 | findUtil(root->left,k); 23 | c++; 24 | if(c==k){ 25 | ele=root->val; 26 | return; 27 | } 28 | findUtil(root->right,k); 29 | } 30 | int kthSmallest(TreeNode* root, int k) { 31 | if(!root){ 32 | return 0; 33 | } 34 | findUtil(root,k); 35 | return ele; 36 | } 37 | }; -------------------------------------------------------------------------------- /leetcode/length_last_word.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 58. Length of Last Word 3 | 4 | Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. 5 | 6 | If the last word does not exist, return 0. 7 | 8 | Note: A word is defined as a character sequence consists of non-space characters only. 9 | */ 10 | class Solution { 11 | public: 12 | int lengthOfLastWord(string s) { 13 | if(s.length()==0){ 14 | return 0; 15 | } 16 | int i=s.length()-1; 17 | int c=0; 18 | while(s[i]==' ' && i>=0){ 19 | i--; 20 | } 21 | while(s[i]!=' ' && i>=0){ 22 | c++; 23 | i--; 24 | } 25 | return c; 26 | } 27 | }; -------------------------------------------------------------------------------- /leetcode/linkListpalindrom.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | 234. Palindrome Linked List 3 | 4 | Given a singly linked list, determine if it is a palindrome. 5 | 6 | * Definition for singly-linked list. 7 | * struct ListNode { 8 | * int val; 9 | * ListNode *next; 10 | * ListNode(int x) : val(x), next(NULL) {} 11 | * }; 12 | */ 13 | class Solution { 14 | public: 15 | long long int a=0; 16 | long long int b=0; 17 | bool isPalindrome(ListNode* head) { 18 | ListNode* temp=head; 19 | while(temp!=NULL){ 20 | a=a*10+(temp->val); 21 | temp=temp->next; 22 | } 23 | temp=NULL; 24 | while(head!=NULL){ 25 | ListNode* r = head->next; 26 | head->next=temp; 27 | temp=head; 28 | head=r; 29 | } 30 | while(temp!=NULL){ 31 | b=b*10+(temp->val); 32 | temp=temp->next; 33 | } 34 | cout<& n) { 16 | vector il; 17 | for(int i=0;i mp; 26 | if(s.length()==0){ 27 | return 0; 28 | } 29 | for(char c:s){ 30 | mp[c]++; 31 | } 32 | int ans=0; 33 | int exist=0; 34 | for(pair p:mp){ 35 | int k = p.second; 36 | if(k%2==0){ 37 | ans+=k; 38 | }else{ 39 | ans+=k-1; 40 | exist=1; 41 | } 42 | } 43 | return ans+exist; 44 | } 45 | }; -------------------------------------------------------------------------------- /leetcode/longest_substring_withoutrepchar.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLongestSubstring(string s) { 4 | vector myChar; 5 | int count=0; 6 | int i=0; 7 | int max=0; 8 | while(i& N) { 15 | if(N.size()==0){ 16 | return 0; 17 | } 18 | if(N.size()==1){ 19 | return N[0]; 20 | } 21 | int times = N.size()/2; 22 | unordered_map myset; 23 | unordered_map:: iterator p; 24 | for(int n:N){ 25 | myset[n]++; 26 | } 27 | for (p = myset.begin(); p != myset.end(); p++){ 28 | if(p->second>times){ 29 | return p->first; 30 | } 31 | } 32 | return 0; 33 | } 34 | }; -------------------------------------------------------------------------------- /leetcode/matrixsearch.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& a, int target) { 4 | for(int i=0;ia[i][p] && targeta[i][q]){ 22 | continue; 23 | } 24 | } 25 | return false; 26 | } 27 | }; -------------------------------------------------------------------------------- /leetcode/maxDepthTree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | 104. Maximum Depth of Binary Tree 3 | * Definition for a binary tree node. 4 | * struct TreeNode { 5 | * int val; 6 | * TreeNode *left; 7 | * TreeNode *right; 8 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | int max= INT_MIN; 14 | void findDepth(TreeNode* root,int k){ 15 | if(root==NULL){ 16 | return; 17 | } 18 | if(root->left==NULL && root->right==NULL){ 19 | if(k>max){ 20 | max=k; 21 | } 22 | } 23 | findDepth(root->left,k+1); 24 | findDepth(root->right,k+1); 25 | } 26 | int maxDepth(TreeNode* root) { 27 | if(root==NULL){ 28 | return 0; 29 | } 30 | findDepth(root,1); 31 | return max; 32 | } 33 | }; -------------------------------------------------------------------------------- /leetcode/max_consecutive_ones.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 485. Max Consecutive Ones 3 | 4 | Given a binary array, find the maximum number of consecutive 1s in this array. 5 | 6 | Example 1: 7 | Input: [1,1,0,1,1,1] 8 | Output: 3 9 | Explanation: The first two digits or the last three digits are consecutive 1s. 10 | The maximum number of consecutive 1s is 3. 11 | Note: 12 | 13 | The input array will only contain 0 and 1. 14 | The length of input array is a positive integer and will not exceed 10,000 15 | 16 | */ 17 | class Solution { 18 | public: 19 | int findMaxConsecutiveOnes(vector& n) { 20 | int s = 0; 21 | int i = 0; 22 | int ans = INT_MIN; 23 | while(i& n1, int m, vector& n2, int n) { 12 | int k = m+n-1; 13 | int i=m-1; 14 | int j=n-1; 15 | while(i>=0 && j>=0){ 16 | if(n1[i]>n2[j]){ 17 | n1[k--]=n1[i--]; 18 | }else{ 19 | n1[k--]=n2[j--]; 20 | } 21 | } 22 | while(j>=0){ 23 | n1[k--]=n2[j--]; 24 | } 25 | while(i>=0){ 26 | n1[k--]=n1[i--]; 27 | } 28 | } 29 | }; -------------------------------------------------------------------------------- /leetcode/minDepth.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | 111. Minimum Depth of Binary Tree 4 | 5 | * Definition for a binary tree node. 6 | * struct TreeNode { 7 | * int val; 8 | * TreeNode *left; 9 | * TreeNode *right; 10 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 11 | * }; 12 | */ 13 | class Solution { 14 | public: 15 | int min= INT_MAX; 16 | void findDepth(TreeNode* root,int k){ 17 | if(root==NULL){ 18 | return; 19 | } 20 | if(root->left==NULL && root->right==NULL){ 21 | if(kleft,k+1); 26 | findDepth(root->right,k+1); 27 | } 28 | int minDepth(TreeNode* root) { 29 | if(root==NULL){ 30 | return 0; 31 | } 32 | findDepth(root,1); 33 | return min; 34 | } 35 | }; -------------------------------------------------------------------------------- /leetcode/minSubArraySum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 209. Minimum Size Subarray Sum 3 | 4 | Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead. 5 | 6 | For example, given the array [2,3,1,2,4,3] and s = 7, 7 | the subarray [4,3] has the minimal length under the problem constraint. 8 | */ 9 | class Solution { 10 | public: 11 | int m=INT_MAX; 12 | int minSubArrayLen(int s, vector& a) { 13 | if(a.size()==0){ 14 | return 0; 15 | } 16 | int sum=0; 17 | int j=0,i=0; 18 | while(i=s){ 21 | m=min(m,i-j+1); 22 | sum-=a[j]; 23 | j+=1; 24 | } 25 | i+=1; 26 | } 27 | return m==INT_MAX ? 0 : m; 28 | } 29 | }; -------------------------------------------------------------------------------- /leetcode/min_in_rotateII.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 154. Find Minimum in Rotated Sorted Array II 3 | 4 | Follow up for "Find Minimum in Rotated Sorted Array": 5 | What if duplicates are allowed? 6 | 7 | Would this affect the run-time complexity? How and why? 8 | 9 | Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. 10 | 11 | (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). 12 | 13 | Find the minimum element. 14 | 15 | The array may contain duplicates. 16 | 17 | 18 | */ 19 | class Solution { 20 | public: 21 | int m=INT_MAX; 22 | void findUtil(vector &n,int s,int e){ 23 | if(s>e){ 24 | return; 25 | } 26 | if(s==e){ 27 | m=min(m,n[s]); 28 | return; 29 | } 30 | int middle =(s+e)/2; 31 | if(n[middle]n[e]){ 34 | findUtil(n,middle+1,e); 35 | }else{ 36 | findUtil(n,s,middle); 37 | findUtil(n,middle+1,e); 38 | } 39 | } 40 | int findMin(vector& n) { 41 | if(n.size()==0){ 42 | return 0; 43 | } 44 | int s = n.size()-1; 45 | findUtil(n,0,s); 46 | return m; 47 | } 48 | }; -------------------------------------------------------------------------------- /leetcode/min_rotate_array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 153. Find Minimum in Rotated Sorted Array 3 | 4 | Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. 5 | 6 | (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). 7 | 8 | Find the minimum element. 9 | 10 | You may assume no duplicate exists in the array. 11 | */ 12 | class Solution { 13 | public: 14 | int m=INT_MAX; 15 | void findUtil(vector &n,int s,int e){ 16 | if(s>e){ 17 | return; 18 | } 19 | if(n[s]<=n[e]){ 20 | m=min(m,n[s]); 21 | }else{ 22 | int middle = (s+e)/2; 23 | findUtil(n,0,middle); 24 | findUtil(n,middle+1,e); 25 | } 26 | } 27 | int findMin(vector& n) { 28 | if(n.size()==0){ 29 | return 0; 30 | } 31 | int s = n.size()-1; 32 | findUtil(n,0,s); 33 | return m; 34 | } 35 | }; -------------------------------------------------------------------------------- /leetcode/mindistnodes.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | int min=INT_MAX; 13 | vector a; 14 | void findMin(TreeNode* root){ 15 | if(root==NULL){ 16 | return; 17 | } 18 | findMin(root->left); 19 | a.push_back(root->val); 20 | findMin(root->right); 21 | } 22 | int minDiffInBST(TreeNode* root) { 23 | findMin(root); 24 | for(int i=0;i(a[i+1]-a[i])){ 26 | min=a[i+1]-a[i]; 27 | } 28 | } 29 | return min; 30 | } 31 | }; -------------------------------------------------------------------------------- /leetcode/mineditdist_words.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findCount(string word1, string word2,int m,int n){ 4 | int dp[m+1][n+1]; 5 | for(int i=0;i<=m;i++){ 6 | for(int j=0;j<=n;j++){ 7 | if(i==0){ 8 | dp[i][j]=j; 9 | continue; 10 | } 11 | if(j==0){ 12 | dp[i][j]=i; 13 | continue; 14 | } 15 | if(word1[i-1]==word2[j-1]){ 16 | dp[i][j]=dp[i-1][j-1]; 17 | }else{ 18 | dp[i][j]=1+min(dp[i-1][j],min(dp[i-1][j-1],dp[i][j-1])); 19 | } 20 | } 21 | } 22 | return dp[m][n]; 23 | } 24 | int minDistance(string word1, string word2) { 25 | if(word1==word2){ 26 | return 0; 27 | } 28 | int k=findCount(word1,word2,word1.length(),word2.length()); 29 | return k; 30 | } 31 | }; -------------------------------------------------------------------------------- /leetcode/minimum_array_moves.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 462. Minimum Moves to Equal Array Elements II 3 | 4 | Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element by 1. 5 | 6 | You may assume the array's length is at most 10,000. 7 | 8 | Example: 9 | 10 | Input: 11 | [1,2,3] 12 | 13 | Output: 14 | 2 15 | 16 | Explanation: 17 | Only two moves are needed (remember each move increments or decrements one element): 18 | 19 | [1,2,3] => [2,2,3] => [2,2,2] 20 | */ 21 | class Solution { 22 | public: 23 | int minMoves2(vector& nums) { 24 | if(nums.size()==1){ 25 | return 0; 26 | } 27 | sort(nums.begin(),nums.end()); 28 | int k = nums.size()/2; 29 | int ele = nums[k]; 30 | int c=0; 31 | cout<<"e:"< findDisappearedNumbers(vector& nums) { 21 | vector s; 22 | for(int i=0;i0){ 24 | nums[abs(nums[i])-1]*=-1; 25 | } 26 | } 27 | for(int i=0;i0){ 29 | s.push_back(i+1); 30 | } 31 | } 32 | return s; 33 | } 34 | 35 | }; -------------------------------------------------------------------------------- /leetcode/moveZeroes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 283. Move Zeroes 3 | 4 | Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. 5 | 6 | For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. 7 | 8 | Note: 9 | You must do this in-place without making a copy of the array. 10 | */ 11 | class Solution { 12 | public: 13 | void moveZeroes(vector& n) { 14 | int i=0; 15 | int j=0; 16 | while(i0){ 36 | ans+=1; 37 | } 38 | return ans; 39 | } 40 | }; -------------------------------------------------------------------------------- /leetcode/no_of_unique_paths.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int uniquePaths(int m, int n) { 4 | vector> nm; 5 | for(int i=0;i sm; 7 | for(int j=0;j 2 | #include 3 | #include 4 | using namespace std; 5 | int main(){ 6 | unsigned int n; 7 | cin>>n; 8 | cin.ignore(); 9 | set myset; 10 | while(n--){ 11 | string s; 12 | getline(cin,s); 13 | int pos; 14 | for(unsigned int i=0;ileft); 32 | int b = findSum(root->right); 33 | myMAX=max(myMAX,max(root->val,max(root->val+a+b,max(root->val+a,root->val+b)))); 34 | return max(0,root->val+max(a,b)); 35 | } 36 | int maxPathSum(TreeNode* root) { 37 | findSum(root); 38 | return myMAX; 39 | } 40 | }; -------------------------------------------------------------------------------- /leetcode/pathSum.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | 112. Path Sum 4 | 5 | 6 | * Definition for a binary tree node. 7 | * struct TreeNode { 8 | * int val; 9 | * TreeNode *left; 10 | * TreeNode *right; 11 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 12 | * }; 13 | */ 14 | class Solution { 15 | public: 16 | bool flag=false; 17 | 18 | void sumUtil(TreeNode* root,int sum,int c){ 19 | if(flag==true){ 20 | return; 21 | } 22 | if(root==NULL){ 23 | return; 24 | } 25 | c=c+root->val; 26 | if(root->left==NULL && root->right==NULL){ 27 | if(sum==c){ 28 | flag=true; 29 | } 30 | } 31 | if(flag==false){ 32 | sumUtil(root->left,sum,c); 33 | sumUtil(root->right,sum,c); 34 | }else{ 35 | return; 36 | } 37 | 38 | } 39 | 40 | bool hasPathSum(TreeNode* root, int sum) { 41 | sumUtil(root,sum,0); 42 | return flag; 43 | } 44 | }; -------------------------------------------------------------------------------- /leetcode/perfectNo.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 507. Perfect Number 3 | 4 | We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself. 5 | 6 | Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not. 7 | Example: 8 | Input: 28 9 | Output: True 10 | Explanation: 28 = 1 + 2 + 4 + 7 + 14 11 | Note: The input number n will not exceed 100,000,000. (1e8) 12 | */ 13 | class Solution { 14 | public: 15 | bool checkPerfectNumber(int num) { 16 | if(num<=1){ 17 | return false; 18 | } 19 | int s=0; 20 | vector p; 21 | for(int i=1;i<=sqrt(num);i++){ 22 | if(num%i==0){ 23 | s+=i; 24 | if(num/inum){ 29 | return false; 30 | } 31 | } 32 | if(s==num){ 33 | return true; 34 | } 35 | return false; 36 | } 37 | }; -------------------------------------------------------------------------------- /leetcode/perfectSquare.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 279. Perfect Squares 3 | 4 | Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. 5 | 6 | For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9. 7 | */ 8 | class Solution { 9 | public: 10 | int numSquares(int n) { 11 | int s = sqrt(n); 12 | int a[s+1][n+1]; 13 | for(int i=1;i<=s;i++){ 14 | for(int j=1;j<=n;j++){ 15 | if(i==1){ 16 | a[i][j]=j; 17 | }else{ 18 | if(j<(i*i)){ 19 | a[i][j]=a[i-1][j]; 20 | }else{ 21 | int c = j-(i*i); 22 | if(c==0){ 23 | a[i][j]=1; 24 | continue; 25 | } 26 | int k = 1+min(a[i][c],a[i-1][c]); 27 | a[i][j]=min(a[i-1][j],k); 28 | } 29 | } 30 | } 31 | } 32 | return a[s][n]; 33 | } 34 | }; -------------------------------------------------------------------------------- /leetcode/permutation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 46. Permutations 3 | */ 4 | class Solution { 5 | public: 6 | void swap(int i,int j,vector &nums){ 7 | int t=nums[i]; 8 | nums[i]=nums[j]; 9 | nums[j]=t; 10 | } 11 | void findPermutation(vector &nums,int s,int l,vector> &p){ 12 | if(s==l){ 13 | p.push_back(nums); 14 | return; 15 | } 16 | for(int k=s;k<=l;k++){ 17 | swap(s,k,nums); 18 | findPermutation(nums,s+1,l,p); 19 | swap(s,k,nums); 20 | } 21 | } 22 | vector> permute(vectoxr& nums) { 23 | vector> p; 24 | findPermutation(nums,0,nums.size()-1,p); 25 | return p; 26 | } 27 | }; -------------------------------------------------------------------------------- /leetcode/permutation2.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void swap(int i,int j,vector &nums){ 4 | int t=nums[i]; 5 | nums[i]=nums[j]; 6 | nums[j]=t; 7 | } 8 | void findPermutation(vector &nums,int s,int l,set> &p){ 9 | if(s==l){ 10 | p.insert(nums); 11 | return; 12 | } 13 | for(int k=s;k<=l;k++){ 14 | swap(s,k,nums); 15 | findPermutation(nums,s+1,l,p); 16 | swap(s,k,nums); 17 | } 18 | } 19 | vector> permuteUnique(vector& nums) { 20 | vector> p; 21 | set> s; 22 | findPermutation(nums,0,nums.size()-1,s); 23 | set> :: iterator it; 24 | for(it=s.begin();it!=s.end();it++){ 25 | p.push_back(*it); 26 | } 27 | return p; 28 | } 29 | }; -------------------------------------------------------------------------------- /leetcode/power.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 50. Pow(x, n) 3 | 4 | Implement pow(x, n). 5 | 6 | 7 | Example 1: 8 | 9 | Input: 2.00000, 10 10 | Output: 1024.00000 11 | Example 2: 12 | 13 | Input: 2.10000, 3 14 | Output: 9.26100 15 | */ 16 | class Solution { 17 | public: 18 | double myPow(double x, int n) { 19 | double a=1; 20 | bool f=false; 21 | if(n<0){ 22 | x=1/x; 23 | n=n*-1; 24 | }else if(n==0){ 25 | return 1.0; 26 | } 27 | if(x<0){ 28 | x=abs(x); 29 | f=true; 30 | } 31 | if(x==1.0 || x==0.0){ 32 | if(f && n%2!=0){ 33 | return -1*x; 34 | } 35 | return x; 36 | } 37 | if(x>1 && n==INT_MAX){ 38 | return INFINITY; 39 | } 40 | if(x>0 && x<1 && (n==INT_MAX || n==INT_MIN)){ 41 | return 0.0; 42 | } 43 | for(int i=0;i=3){ 13 | double k = n/3.0; 14 | int a = k; 15 | if((k-a)!=0){ 16 | return false; 17 | } 18 | n=n/3; 19 | if(n==3 || n==1){ 20 | return true; 21 | } 22 | } 23 | return false; 24 | } 25 | }; -------------------------------------------------------------------------------- /leetcode/randomPickIndex.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 398. Random Pick Index 3 | 4 | Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array. 5 | 6 | Note: 7 | The array size can be very large. Solution that uses too much extra space will not pass the judge. 8 | 9 | Example: 10 | 11 | int[] nums = new int[] {1,2,3,3,3}; 12 | Solution solution = new Solution(nums); 13 | 14 | // pick(3) should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning. 15 | solution.pick(3); 16 | 17 | // pick(1) should return 0. Since in the array only nums[0] is equal to 1. 18 | solution.pick(1); 19 | */ 20 | class Solution { 21 | public: 22 | vector n; 23 | Solution(vector nums) { 24 | n=nums; 25 | } 26 | 27 | int pick(int target) { 28 | vector ind; 29 | int k=0; 30 | for(int i:n){ 31 | if(i==target){ 32 | ind.push_back(k); 33 | } 34 | k++; 35 | } 36 | return ind[rand()%ind.size()]; 37 | } 38 | }; 39 | 40 | /** 41 | * Your Solution object will be instantiated and called as such: 42 | * Solution obj = new Solution(nums); 43 | * int param_1 = obj.pick(target); 44 | */ -------------------------------------------------------------------------------- /leetcode/ransomNote.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 383. Ransom Note 3 | Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false. 4 | 5 | Each letter in the magazine string can only be used once in your ransom note. 6 | 7 | Note: 8 | You may assume that both strings contain only lowercase letters. 9 | 10 | canConstruct("a", "b") -> false 11 | canConstruct("aa", "ab") -> false 12 | canConstruct("aa", "aab") -> true 13 | */ 14 | class Solution { 15 | public: 16 | bool canConstruct(string ransomNote, string magazine) { 17 | unordered_map mp; 18 | for(char s:magazine){ 19 | mp[s]++; 20 | } 21 | for(char s:ransomNote){ 22 | if(mp.find(s)!=mp.end() && mp[s]>0){ 23 | mp[s]--; 24 | }else{ 25 | return false; 26 | } 27 | } 28 | return true; 29 | } 30 | }; -------------------------------------------------------------------------------- /leetcode/rectangle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 492. Construct the Rectangle 3 | 4 | For a web developer, it is very important to know how to design a web page's size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose length L and width W satisfy the following requirements: 5 | 6 | 1. The area of the rectangular web page you designed must equal to the given target area. 7 | 8 | 2. The width W should not be larger than the length L, which means L >= W. 9 | 10 | 3. The difference between length L and width W should be as small as possible. 11 | You need to output the length L and the width W of the web page you designed in sequence. 12 | */ 13 | class Solution { 14 | public: 15 | vector constructRectangle(int t) { 16 | vector a; 17 | int l; 18 | a.push_back(0); 19 | a.push_back(0); 20 | int ans = INT_MAX; 21 | for(int i=1;i<=sqrt(t);i++){ 22 | l = t/i; 23 | if(l*i==t){ 24 | a[0]=l; 25 | a[1]=i; 26 | } 27 | } 28 | return a; 29 | } 30 | }; -------------------------------------------------------------------------------- /leetcode/removeLinkList.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | 203. Remove Linked List Elements 4 | 5 | Remove all elements from a linked list of integers that have value val. 6 | 7 | Example 8 | Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 9 | Return: 1 --> 2 --> 3 --> 4 --> 5 10 | 11 | * Definition for singly-linked list. 12 | * struct ListNode { 13 | * int val; 14 | * ListNode *next; 15 | * ListNode(int x) : val(x), next(NULL) {} 16 | * }; 17 | */ 18 | class Solution { 19 | public: 20 | ListNode* removeElements(ListNode* h, int v) { 21 | ListNode* t1 = NULL; 22 | while(h!=NULL && h->val==v){ 23 | h=h->next; 24 | } 25 | t1=h; 26 | while(h!=NULL){ 27 | ListNode* temp = h->next; 28 | while(temp!=NULL && temp->val==v){ 29 | temp=temp->next; 30 | } 31 | h->next=temp; 32 | h=h->next; 33 | } 34 | return t1; 35 | } 36 | }; -------------------------------------------------------------------------------- /leetcode/remove_duplicates.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 26. Remove Duplicates from Sorted Array 3 | DescriptionHintsSubmissionsDiscussSolution 4 | Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length. 5 | 6 | Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. 7 | 8 | Example: 9 | 10 | Given nums = [1,1,2], 11 | 12 | Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. 13 | It doesn't matter what you leave beyond the new length. 14 | */ 15 | 16 | class Solution { 17 | public: 18 | void removeNumbers(vector &nm,int i){ 19 | if(nm.size()==0){ 20 | return; 21 | } 22 | if(i>=nm.size()-1){ 23 | return; 24 | } 25 | if(nm[i]==nm[i+1]){ 26 | nm.erase(nm.begin()+i); 27 | }else{ 28 | i++; 29 | } 30 | removeNumbers(nm,i); 31 | } 32 | int removeDuplicates(vector& nums) { 33 | removeNumbers(nums,0); 34 | return nums.size(); 35 | } 36 | }; -------------------------------------------------------------------------------- /leetcode/reverseBits.cpp: -------------------------------------------------------------------------------- 1 | 2 | // 190. Reverse Bits 3 | 4 | // Reverse bits of a given 32 bits unsigned integer. 5 | 6 | // For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), 7 | // return 964176192 (represented in binary as 00111001011110000010100101000000). 8 | 9 | class Solution { 10 | public: 11 | uint32_t reverseBits(uint32_t n) { 12 | uint32_t rev = 0; 13 | int l=1; 14 | while(l<32){ 15 | if(n & 1 == 1){ 16 | rev = rev ^ 1; 17 | } 18 | rev = rev << 1; 19 | n = n >> 1; 20 | l=l+1; 21 | } 22 | if(n & 1 == 1){ 23 | rev = rev ^ 1; 24 | } 25 | return rev; 26 | } 27 | }; -------------------------------------------------------------------------------- /leetcode/reverseLinkList.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | 206. Reverse Linked List 3 | 4 | Reverse a singly linked list. 5 | 6 | * Definition for singly-linked list. 7 | * struct ListNode { 8 | * int val; 9 | * ListNode *next; 10 | * ListNode(int x) : val(x), next(NULL) {} 11 | * }; 12 | */ 13 | class Solution { 14 | public: 15 | ListNode* reverseList(ListNode* head) { 16 | ListNode* temp=NULL; 17 | while(head!=NULL){ 18 | ListNode* a=head->next; 19 | head->next=temp; 20 | temp=head; 21 | head=a; 22 | } 23 | return temp; 24 | } 25 | }; -------------------------------------------------------------------------------- /leetcode/reverseStringII.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 541. Reverse String II 3 | 4 | Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original. 5 | Example: 6 | Input: s = "abcdefg", k = 2 7 | Output: "bacdfeg" 8 | Restrictions: 9 | The string consists of lower English letters only. 10 | Length of the given string and k will in the range [1, 10000] 11 | */ 12 | 13 | class Solution { 14 | public: 15 | void reverseUtil(string &s,int i,int k){ 16 | if(i+k>s.length()){ 17 | reverse(s.begin()+i,s.end()); 18 | return; 19 | } 20 | reverse(s.begin()+i,s.begin()+i+k); 21 | reverseUtil(s,i+2*k,k); 22 | } 23 | string reverseStr(string s, int k) { 24 | reverseUtil(s,0,k); 25 | return s; 26 | } 27 | }; -------------------------------------------------------------------------------- /leetcode/reverseVowel.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 345. Reverse Vowels of a String 4 | 5 | Write a function that takes a string as input and reverse only the vowels of a string. 6 | 7 | Example 1: 8 | Given s = "hello", return "holle". 9 | 10 | Example 2: 11 | Given s = "leetcode", return "leotcede". 12 | 13 | Note: 14 | The vowels does not include the letter "y". 15 | */ 16 | class Solution { 17 | public: 18 | bool vowel(char i){ 19 | if(i=='a' || i=='e' || i=='i' || i=='o' || i=='u' || i=='A' || i=='E' || i=='I' || i=='O' || i=='U'){ 20 | return true; 21 | } 22 | return false; 23 | } 24 | string reverseVowels(string s) { 25 | int i=0;int j=s.length()-1; 26 | while(i0){ 7 | int b=n%10; 8 | r=(r*10)+b; 9 | n=n/10; 10 | } 11 | if(r>INT_MAX){ 12 | return 0; 13 | } 14 | if(x<0){ 15 | return -(int)r; 16 | } 17 | return (int)r; 18 | } 19 | }; -------------------------------------------------------------------------------- /leetcode/rotateArray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 189. Rotate Array 3 | 4 | Rotate an array of n elements to the right by k steps. 5 | 6 | For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 7 | */ 8 | class Solution { 9 | public: 10 | void reverse(vector& n,int s,int e){ 11 | while(s& n, int k) { 20 | k=k%(n.size()); 21 | if(k>0){ 22 | reverse(n,0,n.size()-1); 23 | reverse(n,0,k-1); 24 | reverse(n,k,n.size()-1); 25 | } 26 | } 27 | }; -------------------------------------------------------------------------------- /leetcode/rotate_image.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector>& m) { 4 | int s = m.size(); 5 | for(int i=0;i>& m, int t) { 25 | int r=m.size(); 26 | if(r==0){ 27 | return false; 28 | } 29 | int c=m[0].size(); 30 | if(c==0){ 31 | return false; 32 | } 33 | for(int i=0;it){ 35 | break; 36 | } 37 | for(int j=0;jt){ 39 | break; 40 | } 41 | if(t==m[i][j]){ 42 | return true; 43 | } 44 | } 45 | } 46 | return false; 47 | } 48 | }; -------------------------------------------------------------------------------- /leetcode/singleNumberII.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 137. Single Number II 3 | Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one. 4 | 5 | Note: 6 | Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 7 | */ 8 | class Solution { 9 | public: 10 | int singleNumber(vector& nu) { 11 | unordered_set ms; 12 | unordered_set ms1; 13 | for(int n:nu){ 14 | if(ms1.find(n)!=ms1.end()){ 15 | continue; 16 | }else{ 17 | if(ms.find(n)!=ms.end()){ 18 | ms1.insert(n); 19 | ms.erase(n); 20 | }else{ 21 | ms.insert(n); 22 | } 23 | } 24 | } 25 | for(int l:ms){ 26 | return l; 27 | } 28 | return 0; 29 | } 30 | }; -------------------------------------------------------------------------------- /leetcode/small_ciler_into_large_cicles.cpp: -------------------------------------------------------------------------------- 1 | // how many small circles of radius r can exactly fit into large circle of clapital radius R. 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | #define PI 3.14159 7 | void findnumbers(double R,double r,double &c){ 8 | if(r>=R){ 9 | return; 10 | } 11 | 12 | double f=(PI/asin(r/(R-r))); 13 | cout<<"f:"<0){ 12 | return memo[n]; 13 | } 14 | memo[n]=memo[n]+findCount(n-1,memo)+findCount(n-2,memo); 15 | return memo[n]; 16 | // c=c+findCount(n-1,memo)%m+findCount(n-2,memo); 17 | // return c; 18 | } 19 | int climbStairs(int n) { 20 | int memo[n+1]; 21 | for(int i=0;i& prices) { 4 | if(prices.size()==0){ 5 | return 0; 6 | } 7 | int t=0; 8 | for(int i=0;iprices[i]){ 10 | t=t+(prices[i+1]-prices[i]); 11 | } 12 | } 13 | return t; 14 | } 15 | }; -------------------------------------------------------------------------------- /leetcode/subsequences.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 491. Increasing Subsequences 3 | 4 | Given an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2 . 5 | 6 | Example: 7 | Input: [4, 6, 7, 7] 8 | Output: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]] 9 | */ 10 | class Solution { 11 | public: 12 | void findsequencesUtil(vector n,set> &a,int s,vector small){ 13 | if(small.size()>=2){ 14 | a.insert(small); 15 | } 16 | for(int i=s;i=small[small.size()-1]){ 18 | small.push_back(n[i]); 19 | findsequencesUtil(n,a,i+1,small); 20 | small.pop_back(); 21 | } 22 | } 23 | return; 24 | } 25 | vector> findSubsequences(vector& n) { 26 | set> a; 27 | vector s; 28 | findsequencesUtil(n,a,0,s); 29 | set> :: iterator it; 30 | vector> b; 31 | for(it=a.begin();it!=a.end();it++){ 32 | b.push_back(*it); 33 | } 34 | return b; 35 | } 36 | }; -------------------------------------------------------------------------------- /leetcode/subset.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> subsets(vector& nums) { 4 | int n=nums.size(); 5 | vector> sub; 6 | for(int i=0;i<1< a; 8 | for(int j=0;j> subsetsWithDup(vector& nums) { 22 | sort(nums.begin(),nums.end()); 23 | int n = nums.size(); 24 | set> a; 25 | for(int i=0;i<(1< small; 27 | for(int j=0;j> b; 35 | set> :: iterator it; 36 | for(it=a.begin();it!=a.end();it++){ 37 | b.push_back(*it); 38 | } 39 | return b; 40 | } 41 | }; -------------------------------------------------------------------------------- /leetcode/subtractwithoutdivision.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void findAnswer(int dividend,int divisor,int &count){ 4 | while(dividend>divisor){ 5 | count++; 6 | dividend=dividend-divisor; 7 | } 8 | } 9 | int divide(int dividend, int divisor) { 10 | int count=0; 11 | int f=0; 12 | if(divisor==1 || divisor==-1){ 13 | if(divisor==-1){ 14 | int k = -dividend; 15 | return k; 16 | }else{ 17 | return dividend; 18 | } 19 | } 20 | if(divisor==2 || divisor==-2){ 21 | if(divisor==-2){ 22 | return -(dividend>>1); 23 | } 24 | return (dividend>>1); 25 | } 26 | if(dividend<0 && divisor<0){ 27 | findAnswer(abs(dividend),abs(divisor),count); 28 | }else if(dividend<0 || divisor<0){ 29 | f=1; 30 | findAnswer(abs(dividend),abs(divisor),count); 31 | }else{ 32 | findAnswer(abs(dividend),abs(divisor),count); 33 | } 34 | if(f==1){ 35 | return -(count); 36 | } 37 | return count; 38 | } 39 | int main() 40 | { 41 | int c=divide(-2147483647,-1); 42 | cout<left; 32 | if(temp!=NULL && temp->left==NULL && temp->right==NULL){ 33 | s+=temp->val; 34 | } 35 | findUtil(root->left); 36 | findUtil(root->right); 37 | } 38 | int sumOfLeftLeaves(TreeNode* root) { 39 | findUtil(root); 40 | return s; 41 | } 42 | }; -------------------------------------------------------------------------------- /leetcode/sumToROOT.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | 129. Sum Root to Leaf Numbers 4 | 5 | Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. 6 | 7 | An example is the root-to-leaf path 1->2->3 which represents the number 123. 8 | 9 | Find the total sum of all root-to-leaf numbers. 10 | 11 | For example, 12 | 13 | 1 14 | / \ 15 | 2 3 16 | The root-to-leaf path 1->2 represents the number 12. 17 | The root-to-leaf path 1->3 represents the number 13. 18 | 19 | 20 | 21 | * Definition for a binary tree node. 22 | * struct TreeNode { 23 | * int val; 24 | * TreeNode *left; 25 | * TreeNode *right; 26 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 27 | * }; 28 | */ 29 | class Solution { 30 | public: 31 | int s=0; 32 | int finalsum=0; 33 | void findNo(TreeNode* root){ 34 | if(!root){ 35 | return; 36 | } 37 | s=(s*10)+root->val; 38 | if(!root->right && !root->left){ 39 | finalsum+=s; 40 | } 41 | findNo(root->left); 42 | findNo(root->right); 43 | s=(s-root->val)/10; 44 | } 45 | int sumNumbers(TreeNode* root) { 46 | if(!root){ 47 | return 0; 48 | } 49 | findNo(root); 50 | return finalsum; 51 | } 52 | }; -------------------------------------------------------------------------------- /leetcode/summary_ranges.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 228. Summary Ranges 3 | 4 | Given a sorted integer array without duplicates, return the summary of its ranges. 5 | 6 | Example 1: 7 | Input: [0,1,2,4,5,7] 8 | Output: ["0->2","4->5","7"] 9 | Example 2: 10 | Input: [0,2,3,4,6,8,9] 11 | */ 12 | class Solution { 13 | public: 14 | vector summaryRanges(vector& n) { 15 | vector s; 16 | if(n.size()==0){ 17 | return s; 18 | } 19 | int i=0; 20 | int j=0; 21 | while(i"+to_string(n[j-1]); 35 | } 36 | s.push_back(a); 37 | i=j; 38 | } 39 | return s; 40 | } 41 | }; -------------------------------------------------------------------------------- /leetcode/topKfrequent.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 347. Top K Frequent Elements 3 | 4 | Given a non-empty array of integers, return the k most frequent elements. 5 | 6 | For example, 7 | Given [1,1,1,2,2,3] and k = 2, return [1,2]. 8 | 9 | Note: 10 | You may assume k is always valid, 1 ≤ k ≤ number of unique elements. 11 | Your algorithm's time complexity must be better than O(n log n), where n is the array's size. 12 | */ 13 | class Solution { 14 | public: 15 | vector topKFrequent(vector& n, int k) { 16 | vector a; 17 | int s=n.size(); 18 | unordered_map mp; 19 | unordered_map ::iterator it; 20 | for(int i=0;isecond){ 28 | m=it->second; 29 | t=it->first; 30 | } 31 | } 32 | a.push_back(t); 33 | mp.erase(t); 34 | } 35 | return a; 36 | } 37 | }; -------------------------------------------------------------------------------- /leetcode/totalhammingDistance.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 477. Total Hamming Distance 3 | DescriptionHintsSubmissionsDiscussSolution 4 | The Hamming distance between two integers is the number of positions at which the corresponding bits are different. 5 | 6 | Now your job is to find the total Hamming distance between all pairs of the given numbers. 7 | 8 | Example: 9 | Input: 4, 14, 2 10 | 11 | Output: 6 12 | 13 | Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just 14 | showing the four bits relevant in this case). So the answer will be: 15 | HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6. 16 | Note: 17 | Elements of the given array are in the range of 0 to 10^9 18 | Length of the array will not exceed 10^4. 19 | */ 20 | 21 | class Solution { 22 | public: 23 | int totalHammingDistance(vector& nums) { 24 | int s=0; 25 | for(int j=0;j<32;j++){ 26 | int zero=0; 27 | int one=0; 28 | for(int i=0;i>j)& 1; 30 | if(k==1){ 31 | one++; 32 | }else{ 33 | zero++; 34 | } 35 | } 36 | s+=(one*zero); 37 | } 38 | return s; 39 | } 40 | }; -------------------------------------------------------------------------------- /leetcode/trailing_zeroes_factorial.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 172. Factorial Trailing Zeroes 4 | 5 | Given an integer n, return the number of trailing zeroes in n!. 6 | 7 | Note: Your solution should be in logarithmic time complexity. 8 | */ 9 | class Solution { 10 | public: 11 | int trailingZeroes(int n) { 12 | int c2=0; 13 | int c5=0; 14 | long long int i=2; 15 | long long int j=5; 16 | while((n/i)>0){ 17 | c2+=(n/i); 18 | i=i*2; 19 | } 20 | while((n/j)>0){ 21 | cout<& height) { 4 | vector maxfromleft; 5 | vector maxfromright; 6 | int s = height.size(); 7 | int max = -1; 8 | for(int i=0;i=0;i--){ 16 | if(max& height) { 4 | int i=0; 5 | int j=height.size()-1; 6 | int max=-1; 7 | while(i<=j){ 8 | if(height[i]<=height[j]){ 9 | int v = height[i]*(j-i); 10 | if(maxval!=q->val){ 24 | return false; 25 | } 26 | 27 | bool t=isSameTree(p->left,q->left); 28 | bool f=isSameTree(p->right,q->right); 29 | if(!t){ 30 | return false; 31 | } 32 | if(!f){ 33 | return false; 34 | } 35 | return true; 36 | } 37 | 38 | }; -------------------------------------------------------------------------------- /leetcode/uglyNumberII.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 264. Ugly Number II 3 | Write a program to find the n-th ugly number. 4 | 5 | Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. 6 | 7 | Note that 1 is typically treated as an ugly number, and n does not exceed 1690. 8 | */ 9 | class Solution { 10 | public: 11 | int nthUglyNumber(int n) { 12 | vector a; 13 | a.push_back(1); 14 | int i2=0,i3=0,i5=0; 15 | int k; 16 | while(a.size() mp; 24 | for(char i : s){ 25 | mp[i]++; 26 | } 27 | for(char i : t){ 28 | if(mp.count(i)){ 29 | mp[i]--; 30 | }else{ 31 | return false; 32 | } 33 | } 34 | for(pair p : mp){ 35 | if(p.second!=0){ 36 | return false; 37 | } 38 | } 39 | return true; 40 | } 41 | }; -------------------------------------------------------------------------------- /leetcode/verticalline_waterfill.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | int j=height.size()-1; 5 | int i=0; 6 | int max=-1; 7 | while(i 2 | #include 3 | 4 | int a[3][3] = {{1,2, 9},{5, 3, 8},{4, 6, 7}}; 5 | 6 | void findMaxPath(int i,int j,int *k){ 7 | 8 | 9 | if(i<0 || i>2 || j<0 || j>2){ 10 | return; 11 | } 12 | 13 | printf("%d\n",a[i][j]); 14 | *k=*k+1; 15 | 16 | if(a[i+1][j]-a[i][j]==1 && i+1<3){ 17 | findMaxPath(i+1,j,k); 18 | } 19 | 20 | if(a[i-1][j]-a[i][j]==1 && i-1>=0){ 21 | findMaxPath(i-1,j,k); 22 | } 23 | 24 | if(a[i][j+1]-a[i][j]==1 && j+1<3){ 25 | findMaxPath(i,j+1,k); 26 | } 27 | 28 | if(a[i][j-1]-a[i][j]==1 && j-1>=0){ 29 | findMaxPath(i,j-1,k); 30 | } 31 | 32 | } 33 | int main() 34 | { 35 | int max=-1; 36 | for(int i=0;i<3;i++){ 37 | for(int j=0;j<3;j++){ 38 | printf("\n"); 39 | int k=0; 40 | findMaxPath(i,j,&k); 41 | printf("Path Length:%d\n",k); 42 | printf("\n"); 43 | if(k>max){ 44 | max=k; 45 | } 46 | } 47 | } 48 | printf("MAX:%d\n",max); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /minjump.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n; 5 | void findJumps(int a[n],int jumps,int start){ 6 | if(start+a[start]>=n-1){ 7 | jumps=jumps+1; 8 | cout<>t; 28 | 29 | while(t--){ 30 | cin>>n; 31 | int a[n]; 32 | for(int i=0;i>a[i]; 34 | } 35 | findJumps(a,0,0); 36 | } 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /nextGreater.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n=6; 6 | int a[6]={1,5,3,2,4,6}; 7 | int b[n],c[n]; 8 | 9 | 10 | for(int i=0;i0 && (a[i]>a[c[k-1]] )){ 18 | b[c[k-1]]=a[i]; 19 | k=k-1; 20 | } 21 | c[k]=i; 22 | k=k+1; 23 | } 24 | 25 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | void checkFlag(char i ,char j,int *flag){ 6 | if(i=='[' && (j=='}' || j==')')){ 7 | *flag=1; 8 | } 9 | if(i=='{' && (j==']' || j==')')){ 10 | *flag=1; 11 | } 12 | if(i=='(' && (j==']' || j=='}')){ 13 | *flag=1; 14 | } 15 | } 16 | 17 | int main() 18 | { 19 | int t; 20 | cin>>t; 21 | while(t--){ 22 | string s; 23 | cin>>s; 24 | int flag=0; 25 | int count_circ=0; 26 | int count_rect=0; 27 | int count_curl=0; 28 | for(int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | int a=INT_MAX,b=INT_MAX,c=INT_MAX; 6 | int reachn(int n){ 7 | 8 | 9 | if(n==1){ 10 | return 0; 11 | }else if(n%2==0){ 12 | a=1+reachn(n/2); 13 | }else if(n%3==0){ 14 | b=1+reachn(n/3); 15 | }else{ 16 | c=1+reachn(n-1); 17 | } 18 | 19 | return min(a,min(b,c)); 20 | } 21 | int main() 22 | { 23 | int n; 24 | cin>>n; 25 | int k=reachn(n); 26 | cout<<"answer:"< 2 | #include 3 | using namespace std; 4 | void removeDuplicates(string s,int i){ 5 | 6 | if(s[i]=='\0'){ 7 | cout<>s; 31 | int size=s.length(); 32 | removeDuplicates(s,0); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /rep.py: -------------------------------------------------------------------------------- 1 | def printRepeating(arr, size): 2 | 3 | print("The repeating elements are: ") 4 | 5 | for i in range(0, size): 6 | print("index:",i) 7 | print("index element:",arr[i]) 8 | print ("final element:",arr[abs(arr[i])]) 9 | if arr[abs(arr[i])] >= 0: 10 | arr[abs(arr[i])] = -arr[abs(arr[i])] 11 | else: 12 | print ("Ans:",abs(arr[i]), end = " ") 13 | 14 | arr = [1, 2, 3, 1, 3, 6, 6] 15 | arr_size = len(arr) 16 | 17 | printRepeating(arr, arr_size) -------------------------------------------------------------------------------- /reverseDuplicates.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | void removeDuplicates(string s,int i){ 5 | 6 | if(s[i]=='\0'){ 7 | cout<>s; 31 | int size=s.length(); 32 | removeDuplicates(s,0); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /shortest_s_to_dc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int n=3; 4 | int m=4; 5 | int a[3][4]={{1,0,0,0}, 6 | {1,1,0,1}, 7 | {0,1,1,1}}; 8 | 9 | bool visited[3][4]; 10 | int t=0; 11 | bool isSafe(int i,int j){ 12 | if(a[i][j]==1 && i>=0 && i=0 && j 2 | using namespace std; 3 | 4 | void insertionSort(int a[8]){ 5 | for(int i=0;i<8;i++){ 6 | int k=a[i]; 7 | int j=i-1; 8 | while(j>=0 && a[j]>k){ 9 | a[j+1]=a[j]; 10 | j=j-1; 11 | } 12 | a[j+1]=k; 13 | } 14 | } 15 | 16 | int main() 17 | { 18 | int a[8]={9,7,6,15,16,5,10,11}; 19 | insertionSort(a); 20 | for(int i=0;i<8;i++){ 21 | cout< 2 | using namespace std; 3 | 4 | void printArray(int a[8]){ 5 | for(int i=0;i<8;i++){ 6 | cout< 2 | #include 3 | 4 | int compare (const void * a, const void * b) 5 | { 6 | return ( *(long long int*)a - *(long long int*)b ); 7 | } 8 | 9 | int main(int argc, char const *argv[]) 10 | { 11 | int t; 12 | scanf("%d",&t); 13 | while(t--){ 14 | long long int n,k; 15 | scanf("%lld %lld",&n,&k); 16 | if(n>0){ 17 | int flag=0; 18 | long long int a[n]; 19 | for(int i=0;i0){ 29 | k=k-1; 30 | }else{ 31 | flag=1; 32 | printf("%lld\n",i); 33 | break; 34 | } 35 | }else{ 36 | j++; 37 | } 38 | } 39 | if(flag==0){ 40 | if(k>0){ 41 | printf("%lld\n",a[n-1]+k+1); 42 | }else{ 43 | printf("%lld\n",a[n-1]+1); 44 | } 45 | } 46 | } 47 | } 48 | return 0; 49 | } -------------------------------------------------------------------------------- /sorting/quicksort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printArray(int a[8]){ 5 | for(int i=0;i<8;i++){ 6 | cout< 2 | #define n 5 3 | int a[n][n]; 4 | void spiralMatrix(int a[n][n],int p,int q,int s){ 5 | if(s==0){ 6 | return; 7 | } 8 | int j=0; 9 | int i=0; 10 | for (j = q; j < s; j++){ 11 | printf("%d\t", a[p][j]); 12 | } 13 | j=j-1; 14 | printf("\n"); 15 | 16 | for(i=p+1;i=q;j--){ 23 | printf("%d\t", a[i][j]); 24 | } 25 | j=j+1; 26 | printf("\n"); 27 | 28 | for(i=i-1;i>p;i--){ 29 | printf("%d\t", a[i][j]); 30 | } 31 | printf("\n"); 32 | 33 | spiralMatrix(a,i+1,j+1,s-1); 34 | 35 | } 36 | void printArray(int a[n][n]){ 37 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | string data; 10 | int size; 11 | static string ans; 12 | 13 | void print_subsets(string str, int level) // recursive function 14 | { 15 | if(level==size-1) 16 | { 17 | ans= ans +" " + str;return; 18 | } 19 | 20 | print_subsets(str , level+1); 21 | print_subsets(str +data[level], level+1); 22 | } 23 | 24 | 25 | int main() 26 | { 27 | cin>>data; 28 | cin>>size; 29 | print_subsets(data, 0); 30 | cout< 2 | 3 | int main() { 4 | int t; 5 | int n; 6 | scanf("%d",&t); 7 | while(t--){ 8 | scanf("%d",&n); 9 | if(n%2!=0){ 10 | printf("0"); 11 | }else{ 12 | printf("1"); 13 | } 14 | } 15 | return 0; 16 | } -------------------------------------------------------------------------------- /tree/LCABST.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | TreeNode* findLCABST(TreeNode* root, TreeNode* p, TreeNode* q){ 13 | 14 | if(root->val>=p->val && root->valval){ 15 | cout<<"root:"<val; 16 | return root; 17 | } 18 | 19 | if(root->valval && root->valval){ 20 | if(root->right!=NULL) 21 | return findLCABST(root->right,p,q); 22 | }else if(root->val>p->val && root->val>q->val){ 23 | if(root->left!=NULL) 24 | return findLCABST(root->left,p,q); 25 | } 26 | return root; 27 | } 28 | 29 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 30 | TreeNode* data=findLCABST(root,p,q); 31 | return data; 32 | } 33 | }; -------------------------------------------------------------------------------- /tree/LCABT.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 13 | 14 | if(root==NULL){ 15 | return NULL; 16 | } 17 | 18 | if(root==p || root==q){ 19 | return root; 20 | } 21 | 22 | TreeNode* leftNode=lowestCommonAncestor(root->left,p,q); 23 | TreeNode* rightNode=lowestCommonAncestor(root->right,p,q); 24 | 25 | if(leftNode!=NULL && rightNode!=NULL){ 26 | return root; 27 | } 28 | 29 | if(leftNode==NULL && rightNode==NULL){ 30 | return NULL; 31 | } 32 | 33 | if(leftNode!=NULL){ 34 | return leftNode; 35 | } 36 | 37 | if(rightNode!=NULL){ 38 | return rightNode; 39 | } 40 | 41 | return root; 42 | } 43 | }; -------------------------------------------------------------------------------- /tree/pretotree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define v 15 3 | using namespace std; 4 | int t=0; 5 | struct tree{ 6 | int data; 7 | struct tree * right; 8 | struct tree * left; 9 | }; 10 | int findRoot(int data,int inorder[v]){ 11 | for(int i=0;iend){ 21 | return NULL; 22 | } 23 | 24 | int k=preorder[t++]; 25 | struct tree *root=new tree(); 26 | root->data=k; 27 | root->left=NULL; 28 | root->right=NULL; 29 | 30 | if(start==end){ 31 | return root; 32 | } 33 | 34 | int l=findRoot(k,inorder); 35 | root->left=makeTree(start,l-1,inorder,preorder); 36 | root->right=makeTree(l+1,end,inorder,preorder); 37 | 38 | return root; 39 | } 40 | void printRoot(struct tree *root){ 41 | if(root==NULL){ 42 | return; 43 | } 44 | printRoot(root->left); 45 | cout<data<<"\n"; 46 | printRoot(root->right); 47 | } 48 | int main(){ 49 | int inorder[v]={8,4,9,2,10,5,11,1,12,6,13,3,14,7,15}; 50 | int preorder[v]={1,2,4,8,9,5,10,11,3,6,12,13,7,14,15}; 51 | struct tree *root=makeTree(0,v-1,inorder,preorder); 52 | printRoot(root); 53 | return 0; 54 | } -------------------------------------------------------------------------------- /tree/roottoleaf.cpp: -------------------------------------------------------------------------------- 1 | 2 | /* Tree node structure used in the program 3 | struct Node 4 | { 5 | int data; 6 | Node* left, *right; 7 | }; */ 8 | int number(vector list){ 9 | int n=0; 10 | for(int i=0;i list,int &s){ 16 | if(root==NULL){ 17 | return; 18 | } 19 | list.push_back(root->data); 20 | findLeaf(root->left,list,s); 21 | findLeaf(root->right,list,s); 22 | if(root->left==NULL && root->right==NULL){ 23 | int n = number(list); 24 | s=s+n; 25 | list.pop_back(); 26 | } 27 | } 28 | 29 | long long treePathsSum(Node *root){ 30 | vector list; 31 | int s=0; 32 | findLeaf(root,list,s); 33 | return s; 34 | } -------------------------------------------------------------------------------- /trie.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | struct tree{ 7 | char data; 8 | vector vec; 9 | }; 10 | void insert(char t,struct tree **root){ 11 | if((*root)->vec.empty()){ 12 | struct tree *temp=new tree; 13 | temp->data=t; 14 | (*root)->vec.push_back(temp); 15 | *root=temp; 16 | return; 17 | }else{ 18 | int flag=0; 19 | for(int p=0;p<(*root)->vec.size();p++){ 20 | if(t==(*root)->vec[p]->data){ 21 | flag=1; 22 | (*root)=(*root)->vec[p]; 23 | return; 24 | } 25 | } 26 | if(flag==0){ 27 | struct tree *temp=new tree; 28 | temp->data=t; 29 | (*root)->vec.push_back(temp); 30 | *root=temp; 31 | return; 32 | } 33 | } 34 | } 35 | void print(struct tree *root){ 36 | if(root==NULL){ 37 | return; 38 | } 39 | for(int i=0;ivec.size();i++){ 40 | cout<vec[i]->data<<"\n"; 41 | print(root->vec[i]); 42 | } 43 | } 44 | 45 | int main() 46 | { 47 | string s; 48 | struct tree *root=new tree; 49 | root->data='#'; 50 | int n=5; 51 | for(int i=0;i>s; 53 | struct tree *rootTemp=root; 54 | for(int j=0;j1): 10 | w[i][p]=1 11 | fill_water(w,((k-1.0)/2.0),i+1,p) 12 | fill_water(w,((k-1.0)/2.0),i+1,p+1) 13 | else: 14 | w[i][p]=w[i][p]+k 15 | rem=w[i][p]-1 16 | if(w[i][p]>1): 17 | w[i][p]=1 18 | fill_water(w,(rem/2.0),i+1,p) 19 | fill_water(w,(rem/2.0),i+1,p+1) 20 | 21 | 22 | fill_water(w,k,0,0) 23 | print(w) --------------------------------------------------------------------------------