├── Level-2 ├── Math │ ├── gcd.cpp │ ├── trailingZerosInFact.cpp │ ├── rearrangeArray.cpp │ ├── excelColumnNumber.cpp │ ├── gridUniquePathsConstantSpace.cpp │ ├── reverseInteger.cpp │ ├── verifyPrime.cpp │ ├── gridUniquePaths.cpp │ ├── palindromeInteger.cpp │ ├── PrimeSum.cpp │ ├── primeNumbers.cpp │ └── powOf2int.cpp ├── Arrays │ ├── kthRowOfPascal.cpp │ ├── findDuplicate.cpp │ ├── waveArray.cpp │ ├── antiDiagonals.cpp │ ├── maxSumKadane.cpp │ ├── minSteps.cpp │ ├── addOne.cpp │ ├── firstMissingNumb.cpp │ ├── LargestNumber.cpp │ ├── maxAbsDiff.cpp │ ├── repeatAndMissingNum.cpp │ ├── pascalTriangleRow.cpp │ ├── flip.cpp │ ├── setMatrixZeroConstSpace.cpp │ ├── maxNonNegSubarray.cpp │ ├── spiralOrderMatrix.cpp │ ├── maxsumDandC.cpp │ └── mergeOverlappingIntervals.cpp └── level2checkpointPrettyprint.cpp ├── Level-7 ├── Greedy │ ├── highestProduct.cpp │ ├── bulbs.cpp │ ├── assignMice.cpp │ ├── majorityEleNaive.cpp │ ├── majorityEleVotersAlgo.cpp │ └── canCompleteCircuit.cpp └── DynamicProgramming │ ├── Stairs.cpp │ ├── buyAndSell.cpp │ ├── maxSumWithoutAdjElements.cpp │ └── jumpGame.cpp ├── Level-3 ├── Bit-Manipulation │ ├── numOfBits.cpp │ ├── singleNumber.cpp │ ├── reverseBits.cpp │ └── singleNumber2.cpp ├── Two-Pointers │ ├── removeEleFromArray.cpp │ ├── removeDuplicates.cpp │ ├── DiffEqualsK.cpp │ ├── containerWithMostWater.cpp │ ├── removeDupFromSortedArray.cpp │ ├── intersectionOfSorted.cpp │ └── sortByColorAp1.cpp ├── Strings │ ├── PalindromeString.cpp │ ├── longestComPrefix.cpp │ ├── lengthOfLastword.cpp │ ├── romanToInteger.cpp │ ├── naiveStringMatching.cpp │ ├── atoi.cpp │ ├── longestPalinNsqSpace.cpp │ ├── cmpVersionNum.cpp │ └── LongestPalinConstSpace.cpp └── Binary-Search │ ├── squareRootOfN.cpp │ ├── powerMod.cpp │ ├── matrixSearch.cpp │ └── rotatedSortedSearch.cpp ├── Level-6 ├── Trees │ ├── maxDepthBinTree.cpp │ ├── invertBinTree.cpp │ ├── identicalBinTrees.cpp │ ├── minDepth.cpp │ ├── pathSum.cpp │ ├── isSymmetric.cpp │ ├── balancedBinTree.cpp │ ├── flattenBTtoLL.cpp │ ├── rootToLeafPathSum.cpp │ ├── populateNextRPointerExtraMemo.cpp │ ├── LCAnaive.cpp │ ├── populateNextRightPointers.cpp │ ├── kthSmallestBst.cpp │ ├── LCAstandard.cpp │ ├── inorderTraversal.cpp │ └── preorderTraversal.cpp ├── Heaps-And-Maps │ ├── distinctNumbersInWindow.cpp │ ├── mergeKSortedList.cpp │ └── mergeKsortedListsP_QUEUE.cpp └── checkpointInversions.cpp ├── Level-5 ├── Hashing │ ├── DiffK2.cpp │ ├── anagrams.cpp │ ├── longestSubstrNoRep.cpp │ ├── substringConcate.cpp │ ├── longestConsSequence.cpp │ ├── pointsOnLine2.cpp │ ├── fraction.cpp │ └── pointsOnTheLine.cpp └── Backtracking │ ├── generateAllParen.cpp │ └── subsets2.cpp ├── Level-4 ├── Stacks-And-Queues │ ├── nearestSmallerElement.cpp │ ├── rainWaterTrapped.cpp │ ├── redundantBraces.cpp │ ├── minStack.cpp │ └── simplifyDirectoryPath.cpp └── Linked-List │ ├── removeDup.cpp │ ├── removeNthFromLast.cpp │ ├── detectCycle.cpp │ ├── reverseLinkedList2.cpp │ └── partitionList.cpp ├── Level-8 └── Graphs │ ├── blackShapes.cpp │ └── levelOrder.cpp └── README.md /Level-2/Math/gcd.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/greatest-common-divisor/ 2 | 3 | int Solution::gcd(int A, int B) { 4 | if(A==0) 5 | return B; 6 | else 7 | return gcd(B%A, A); 8 | } 9 | -------------------------------------------------------------------------------- /Level-7/Greedy/highestProduct.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/highest-product/ 2 | int Solution::maxp3(vector &A) { 3 | sort(A.begin(), A.end(), greater()); 4 | return max(A[0]*A[1]*A[2], A[0]*A[A.size()-1]*A[A.size()-2]); 5 | } 6 | -------------------------------------------------------------------------------- /Level-3/Bit-Manipulation/numOfBits.cpp: -------------------------------------------------------------------------------- 1 | //https://www.interviewbit.com/problems/number-of-1-bits/ 2 | int Solution::numSetBits(unsigned int A) { 3 | int ans =0; 4 | while(A){ 5 | A = A &(A-1); 6 | ans++; 7 | } 8 | return ans; 9 | } 10 | -------------------------------------------------------------------------------- /Level-3/Bit-Manipulation/singleNumber.cpp: -------------------------------------------------------------------------------- 1 | https://www.interviewbit.com/problems/single-number/ 2 | int Solution::singleNumber(const vector &A) { 3 | int ans = 0; 4 | 5 | for(int i=0;i=powOf5){ 8 | ans+=A/powOf5; 9 | powOf5*=5; 10 | } 11 | return ans; 12 | } 13 | -------------------------------------------------------------------------------- /Level-2/Math/rearrangeArray.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/rearrange-array/ 2 | 3 | void Solution::arrange(vector &A) { 4 | for(int i=0; i=0; i--){ 7 | ans += (A[i]-'A' + 1)*multiplier; 8 | multiplier*=26; 9 | } 10 | return ans; 11 | } 12 | -------------------------------------------------------------------------------- /Level-7/Greedy/bulbs.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/bulbs/ 2 | int Solution::bulbs(vector &A) { 3 | int stateLookingFor=0, ans=0; 4 | 5 | for(int i =0; i Solution::getRow(int A) { 3 | vector ans(A+1); 4 | ans[0]=1; 5 | ans[A]=1; 6 | 7 | for(int i=1;i<=A/2;i++){ 8 | ans[i] = (ans[i-1]* (A-i+1))/i ; 9 | ans[A-i] = ans[i]; 10 | } 11 | 12 | return ans; 13 | } 14 | -------------------------------------------------------------------------------- /Level-2/Math/gridUniquePathsConstantSpace.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/grid-unique-paths/ 2 | 3 | int Solution::uniquePaths(int A, int B) { 4 | int ans = 1; 5 | int x = min (B, A); // if we dont use the min value, ans may overflow. 6 | 7 | for(int i=1;i INT_MAX || reverse < INT_MIN) 11 | return 0; 12 | 13 | return reverse; 14 | } 15 | -------------------------------------------------------------------------------- /Level-2/Math/verifyPrime.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/verify-prime/ 2 | 3 | int Solution::isPrime(int A) { 4 | 5 | if(A==0 || A==1) 6 | return false; 7 | 8 | if(A==2) 9 | return true; 10 | 11 | for(int i=2; i*i<=A;i++){ 12 | if(A%i==0) 13 | return false; 14 | } 15 | 16 | return true; 17 | } 18 | -------------------------------------------------------------------------------- /Level-7/Greedy/assignMice.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/assign-mice-to-holes/ 2 | int Solution::mice(vector &A, vector &B) { 3 | sort(A.begin(), A.end()); 4 | sort(B.begin(), B.end()); 5 | int ans = INT_MIN; 6 | 7 | for(int i=0; i &A, int B) { 4 | 5 | int writeAt = 0; 6 | 7 | for(int i=0;i &A) { 4 | vector v(A.size()); 5 | fill(v.begin(), v.end(), true); 6 | 7 | for(int i=0;i Solution::wave(vector &A) { 4 | sort(A.begin(), A.end()); 5 | 6 | vector ans; 7 | 8 | for(int j=1; j memo(1000); 4 | int Solution::climbStairs(int A) { 5 | memo.clear(); 6 | 7 | if(A<=2){ 8 | return A; 9 | } 10 | 11 | if(memo[A]!=0) 12 | return memo[A]; 13 | 14 | 15 | else{ 16 | memo[A] = climbStairs(A-1) +climbStairs(A-2); 17 | return memo[A]; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Level-3/Two-Pointers/removeDuplicates.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/remove-duplicates-from-sorted-array/ 2 | 3 | int Solution::removeDuplicates(vector &A) { 4 | int writeAt = 0; 5 | 6 | for(int i=0;i &A) { 4 | unordered_map M; 5 | int ans; 6 | int floo = A.size()/2; 7 | 8 | 9 | for(int i=0; ifloo){ 12 | ans = A[i]; 13 | break; 14 | } 15 | } 16 | 17 | return ans; 18 | } 19 | -------------------------------------------------------------------------------- /Level-3/Bit-Manipulation/reverseBits.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/reverse-bits/ 2 | void swap(unsigned int &A, int i, int j); 3 | unsigned int Solution::reverse(unsigned int A) { 4 | 5 | for (int i=0, j=31; i < j; i++, j-- ){ 6 | swap(A,i,j); 7 | } 8 | 9 | return A; 10 | } 11 | 12 | void swap(unsigned int &A, int i, int j){ 13 | if( (A>>i & 1 ) != (A>>j & 1) ){ 14 | A = A ^ (1<left), maxDepth(A->right)); 16 | } 17 | -------------------------------------------------------------------------------- /Level-3/Bit-Manipulation/singleNumber2.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/single-number-ii/ 2 | int Solution::singleNumber(const vector &A) { 3 | int ans =0; 4 | 5 | for(int i=0;i<32;i++){ 6 | 7 | int value = (1< &A, int B) { 4 | 5 | for(int i=0, j=i+1; iB){ 10 | i++; 11 | } 12 | 13 | else 14 | j++; 15 | 16 | if(i==j){ 17 | j++; 18 | } 19 | } 20 | 21 | return false; 22 | } 23 | -------------------------------------------------------------------------------- /Level-3/Strings/PalindromeString.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/palindrome-string/ 2 | int Solution::isPalindrome(string A) { 3 | int i=0, j= A.size()-1; 4 | 5 | while(i &A) { 4 | int largestArea = 0; 5 | int i=0, j=A.size()-1; 6 | 7 | while(i=0 ){ 8 | largestArea = max(largestArea, abs( (j-i) * min( A[j], A[i] ) ) ); 9 | 10 | if(A[i] > Solution::diagonal(vector > &A) { 3 | vector >v; 4 | 5 | for(int i=0;i<2*A[0].size()-1;i++){ 6 | vector temp; 7 | 8 | for(int k=0,j=i;k<2*A[0].size() && j>=0;k++,j--){ 9 | if(k=0 && j &A) { 4 | int ans = INT_MIN; 5 | int cumulative = 0; 6 | int maxval = INT_MIN; 7 | 8 | for(int i=0; i &X, vector &Y) { 6 | int result = 0; 7 | 8 | for(int i=1;i &A) { 15 | string ans=A[0]; 16 | 17 | for(int i=1;i > v(A, vector(B) ); // A vectors with vector(B) value. 5 | 6 | for(int i=0;i=0 && ! ( ( A[i]>= 'A' && A[i]<='Z') || (A[i]>='a' && A[i]<='z') ) ){ 7 | i--; 8 | } 9 | 10 | for(;i>=0;i--){ 11 | if( (A[i]>= 'A' && A[i]<='Z') || (A[i]>='a' && A[i]<='z') ){ 12 | count++; 13 | } 14 | else 15 | break; 16 | } 17 | 18 | return count; 19 | } 20 | -------------------------------------------------------------------------------- /Level-5/Hashing/DiffK2.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/diffk-ii/ 2 | 3 | int Solution::diffPossible(const vector &A, int B) { 4 | unordered_map M; 5 | 6 | for(int i=0;i &A) { 4 | int writeAt = 0; 5 | int i=0; 6 | 7 | for(int i=0;i=0){ 10 | 11 | if( (A % 10) != A / (int)pow(10,j)){ 12 | return false; 13 | } 14 | 15 | A = A % ((int)pow(10,j)); 16 | A = A - A%10; 17 | A = A /10; 18 | 19 | i++; 20 | j=j-2; 21 | 22 | } 23 | return true; 24 | } 25 | -------------------------------------------------------------------------------- /Level-7/Greedy/majorityEleVotersAlgo.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/majority-element/ 2 | // http://www.cs.utexas.edu/~moore/best-ideas/mjrty/index.html 3 | int Solution::majorityElement(const vector &A) { 4 | int ans = A[0]; 5 | int count = 1; 6 | 7 | for(int i = 1; i Solution::prevSmaller(vector &A) { 4 | stack s; 5 | 6 | for(int i=0;i=A[i]){ 10 | s.pop(); 11 | } 12 | 13 | if(s.empty()) 14 | A[i]=-1; 15 | 16 | else 17 | A[i]=s.top(); 18 | 19 | s.push(temp); 20 | } 21 | 22 | return A; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Level-3/Strings/romanToInteger.cpp: -------------------------------------------------------------------------------- 1 | //https://www.interviewbit.com/problems/roman-to-integer/ 2 | int Solution::romanToInt(string A) { 3 | 4 | map M; 5 | 6 | M['I']=1; 7 | M['V']=5; 8 | M['X']=10; 9 | M['L']=50; 10 | M['C']=100; 11 | M['D']=500; 12 | M['M']=1000; 13 | 14 | int ans = M[A[A.size()-1]]; 15 | 16 | for(int i=A.size()-2;i>=0;i--){ 17 | if(M[A[i]] &A) { 4 | int ans = 0; 5 | vector dp(A.size()); 6 | 7 | if(A.size()==0) 8 | return 0; 9 | 10 | dp[0]=0; 11 | 12 | int lowest = A[0]; 13 | 14 | for(int i=1; ileft, root->right); 17 | 18 | inv(root->left); 19 | inv(root->right); 20 | } 21 | 22 | TreeNode* Solution::invertTree(TreeNode* root) { 23 | inv(root); 24 | return root; 25 | } 26 | -------------------------------------------------------------------------------- /Level-2/Arrays/addOne.cpp: -------------------------------------------------------------------------------- 1 | https://www.interviewbit.com/problems/add-one-to-number/ 2 | vector Solution::plusOne(vector &A) { 3 | A[A.size()-1]++; 4 | int i = (int)(A.size()-1); 5 | 6 | while(i>0 && A[i]==10){ 7 | A[i]=0; 8 | A[i-1]++; 9 | i--; 10 | } 11 | 12 | if(A[0]==10){ 13 | A[0]=1; 14 | A.push_back(0); 15 | } 16 | 17 | int j=0; 18 | 19 | while(A[j]==0 && jS; 23 | 24 | for(int i=j;i &A) { 3 | int writeAt = 0; 4 | 5 | for(int i=0;i0){ 7 | swap(A[i],A[writeAt]); 8 | writeAt++; 9 | } 10 | } 11 | 12 | for(int i=0;i0 ){ 14 | A[abs(A[i])] = -A[abs(A[i])]; 15 | } 16 | } 17 | 18 | for(int i=1;i0) 20 | return i; 21 | } 22 | 23 | return writeAt+1; 24 | } 25 | -------------------------------------------------------------------------------- /Level-6/Trees/identicalBinTrees.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/identical-binary-trees/ 2 | 3 | /** 4 | * Definition for binary tree 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | int Solution::isSameTree(TreeNode* A, TreeNode* B) { 13 | if(A==NULL && B==NULL) 14 | return true; 15 | 16 | if(A==NULL || B==NULL) 17 | return false; 18 | 19 | if(A->val == B->val) 20 | return isSameTree(A->left, B->left) && isSameTree(A->right, B->right); 21 | 22 | return false; 23 | } 24 | -------------------------------------------------------------------------------- /Level-2/Math/PrimeSum.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/prime-sum/ 2 | vector Solution::primesum(int A) { 3 | vector v(A+1); 4 | fill(v.begin(), v.end(), true); 5 | v[0]=v[1]=false; 6 | 7 | for(int i=2;i*i<=v.size();i++){ 8 | if(!v[i]) 9 | continue; 10 | for(int j=i*i;jtemp; 18 | temp.push_back(i); 19 | temp.push_back(A-i); 20 | return temp; 21 | } 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Level-2/Arrays/LargestNumber.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/largest-number/ 2 | string sort(vector A); 3 | int comp(string a, string b){ 4 | return a+b > b+a; 5 | } 6 | 7 | string Solution::largestNumber(const vector &A) { 8 | vector B; 9 | bool Allzero = true; 10 | for(int i=0;i0){ 14 | if(n2 & 1) 15 | ans = ( (ans%d2)*(x2%d2) )%d2; 16 | 17 | x2= ( (x2%d2)*(x2%d2) )%d2 ; 18 | n2= n2>>1; 19 | } 20 | 21 | if (isNegAndPowOdd){ 22 | ans = d2 - ans; 23 | } 24 | 25 | return (int)ans; 26 | } 27 | -------------------------------------------------------------------------------- /Level-3/Strings/naiveStringMatching.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/implement-strstr/ 2 | int Solution::strStr(const string &haystack, const string &needle) { 3 | if(needle =="" || haystack.length()next && temp->val == temp->next->val ){ 19 | temp->next = temp->next->next; 20 | } 21 | 22 | else{ 23 | temp = temp->next; 24 | } 25 | } 26 | 27 | return A; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Level-5/Backtracking/generateAllParen.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/generate-all-parentheses-ii/ 2 | 3 | void paranthesis(vector&ans, string x, int open, int close, int A){ 4 | if(open==A && close==A){ 5 | ans.push_back(x); 6 | } 7 | 8 | else{ 9 | if(open Solution::generateParenthesis(int A) { 20 | vector ans; 21 | paranthesis(ans ,"", 0, 0, A); 22 | return ans; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Level-6/Trees/minDepth.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/min-depth-of-binary-tree/ 2 | /** 3 | * Definition for binary tree 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 | int Solution::minDepth(TreeNode* A) { 12 | if(A==NULL) 13 | return 0; 14 | 15 | if(A->left == NULL && A->right==NULL) 16 | return 1; 17 | 18 | if(A->left == NULL || A->right == NULL) 19 | return max(minDepth(A->left), minDepth(A->right)) +1; 20 | 21 | return min( minDepth(A->left), minDepth(A->right) ) + 1; 22 | } 23 | -------------------------------------------------------------------------------- /Level-6/Trees/pathSum.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/path-sum/ 2 | 3 | /** 4 | * Definition for binary tree 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | int sum =0; 13 | 14 | bool solver(TreeNode* A, int B){ 15 | if(A==NULL) 16 | return false; 17 | 18 | if(A->val==B && A->left==NULL && A->right == NULL) 19 | return true; 20 | 21 | return solver(A->left, B - A->val) || solver(A->right, B - A->val); 22 | } 23 | 24 | int Solution::hasPathSum(TreeNode* A, int B) { 25 | return solver(A, B); 26 | } 27 | -------------------------------------------------------------------------------- /Level-7/DynamicProgramming/maxSumWithoutAdjElements.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/max-sum-without-adjacent-elements/ 2 | 3 | int Solution::adjacent(vector > &A) { 4 | int len = A[0].size(); 5 | vector dp(len); // dp[i] will represent the answer till ith element. 6 | 7 | if(len==0) 8 | return 0; 9 | 10 | dp[0] = max(max(A[0][0],0), A[1][0]); 11 | 12 | if(len==1) 13 | return dp[0]; 14 | 15 | dp[1] = max(dp[0], max(A[0][1], A[1][1]) ); 16 | 17 | for(int i=2; i Solution::intersect(const vector &A, const vector &B) { 4 | 5 | vector ans; 6 | 7 | int i=0, j=0; 8 | 9 | while( iB[j] && j &A) { 4 | int countZero=0, countOne=0, countTwo=0; 5 | 6 | for(int i=0;i &A) { 4 | vector left(A.size()); 5 | vector right(A.size()); 6 | 7 | int leftMax=A[0], rightMax = A[A.size()-1] ; 8 | int ans=0; 9 | 10 | for(int i=0;i=0; i--){ 16 | rightMax=max(A[i],rightMax); 17 | right[i]=rightMax; 18 | } 19 | 20 | for(int i=0 ; i &A) { 4 | int ans = INT_MIN; 5 | vector arr1(A.size()+1), arr2(A.size()+1); 6 | 7 | for(int i=0; i s; 5 | 6 | for(int i=0; i > &A, int B) { 4 | int numOfRows = A.size(); 5 | int numOfColumns = A[0].size(); 6 | int low = 0, high = numOfRows*numOfColumns-1; 7 | 8 | while(low<=high){ 9 | int mid = (low+high)/2; 10 | int midX = mid / numOfColumns; 11 | int midY = mid % numOfColumns; 12 | 13 | if(A[midX][midY]==B) 14 | return 1; 15 | 16 | else if(A[midX][midY]>B){ 17 | high = mid-1; 18 | } 19 | 20 | else{ 21 | low = mid+1; 22 | } 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Level-7/Greedy/canCompleteCircuit.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/gas-station/ 2 | // http://stackoverflow.com/a/37391395/3578017 3 | int Solution::canCompleteCircuit(const vector &gas, const vector &cost) { 4 | int ansind = 0; 5 | int net = 0; 6 | int total = 0; 7 | 8 | for(int i=0; i=0 would imply that the circuit can be completed! 17 | } 18 | 19 | if(total>=0) 20 | return ansind; 21 | 22 | return -1; 23 | 24 | return ansind; 25 | } 26 | -------------------------------------------------------------------------------- /Level-2/Arrays/repeatAndMissingNum.cpp: -------------------------------------------------------------------------------- 1 | //https://www.interviewbit.com/problems/repeat-and-missing-number-array/ 2 | vector Solution::repeatedNumber(const vector &A) { 3 | long long int len = A.size(); 4 | long long int sumOfN = (len * (len+1) ) /2, sumOfNsq = (len * (len +1) *(2*len +1) )/6; 5 | long long int missingNumber1=0, missingNumber2=0; 6 | 7 | for(int i=0;i ans; 15 | ans.push_back(missingNumber2); 16 | ans.push_back(missingNumber1); 17 | return ans; 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Level-5/Hashing/anagrams.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/anagrams/ 2 | 3 | vector > Solution::anagrams(const vector &A) { 4 | 5 | map< string, vector > M; 6 | 7 | vector >v; 8 | 9 | for(int i=0;i >::iterator it=M.begin(); it!=M.end(); it++ ){ 16 | vector temp; 17 | 18 | for(vector::iterator i=it->second.begin();isecond.end();i++){ 19 | temp.push_back(*i); 20 | } 21 | 22 | v.push_back(temp); 23 | } 24 | 25 | return v; 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Level-6/Trees/isSymmetric.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/symmetric-binary-tree/ 2 | 3 | /** 4 | * Definition for binary tree 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | 13 | bool isMirror(TreeNode* A, TreeNode* B){ 14 | 15 | if(A == NULL && B == NULL) 16 | return true; 17 | 18 | if(A == NULL || B == NULL) 19 | return false; 20 | 21 | if(A->val == B->val) { 22 | return isMirror(A->left, B->right) && isMirror(A->right, B->left); 23 | } 24 | 25 | return false; 26 | } 27 | 28 | int Solution::isSymmetric(TreeNode* A) { 29 | return isMirror(A, A); 30 | } 31 | -------------------------------------------------------------------------------- /Level-4/Linked-List/removeNthFromLast.cpp: -------------------------------------------------------------------------------- 1 | 2 | // https://www.interviewbit.com/problems/remove-nth-node-from-list-end/ 3 | /** 4 | * Definition for singly-linked list. 5 | * struct ListNode { 6 | * int val; 7 | * ListNode *next; 8 | * ListNode(int x) : val(x), next(NULL) {} 9 | * }; 10 | */ 11 | ListNode* Solution::removeNthFromEnd(ListNode* A, int B) { 12 | int len = 0; 13 | ListNode* temp = A; 14 | 15 | while(temp){ 16 | temp = temp->next; 17 | len++; 18 | } 19 | 20 | 21 | if(B>=len) 22 | return A->next; 23 | 24 | int toMove = len - B - 1; 25 | 26 | temp = A; 27 | 28 | while(toMove--){ 29 | temp = temp->next; 30 | } 31 | 32 | temp ->next = temp->next->next; 33 | return A; 34 | } 35 | -------------------------------------------------------------------------------- /Level-5/Hashing/longestSubstrNoRep.cpp: -------------------------------------------------------------------------------- 1 | //https://www.interviewbit.com/problems/longest-substring-without-repeat/ 2 | 3 | int Solution::lengthOfLongestSubstring(string A) { 4 | 5 | if(A.size()==0) 6 | return 0; 7 | 8 | int anscount=1; 9 | 10 | for(int j=0, i=1 ; i M; 12 | M[A[j]]=j; 13 | int count = 1; 14 | 15 | while( i=A.size()) 24 | break; 25 | 26 | j=M[A[i]]+1; 27 | i=j+1; 28 | 29 | } 30 | 31 | return anscount; 32 | } 33 | -------------------------------------------------------------------------------- /Level-3/Strings/atoi.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/atoi/ 2 | 3 | int Solution::atoi(const string &A) { 4 | long long int num = 0; 5 | int i=0; 6 | bool isNeg = false; 7 | 8 | while(i='0' && A[i]<='9') ) || (isspace(A[i]) ) ){ 21 | break; 22 | } 23 | 24 | if( (num*10 + (A[i]-'0') ) >INT_MAX ) 25 | return isNeg? INT_MIN : INT_MAX; 26 | 27 | num = num*10 + (A[i]-'0'); 28 | } 29 | 30 | return isNeg? -1*num : num; 31 | } 32 | -------------------------------------------------------------------------------- /Level-5/Hashing/substringConcate.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/substring-concatenation/ 2 | vector Solution::findSubstring(string A, const vector &B) { 3 | vector ans; 4 | map M; 5 | int countr=0; 6 | 7 | for(int j=0; j F; 15 | int count =0; 16 | 17 | for(int j=i; j+B[0].length()<=A.size() && countleft), Ht(A->right)) +1; 16 | } 17 | bool HeightCheck(TreeNode *A){ 18 | if(A==NULL) 19 | return true; 20 | 21 | if(abs ( Ht(A->left) - Ht(A->right) ) <=1) 22 | return HeightCheck(A->left) && HeightCheck(A->right); 23 | 24 | else 25 | return false; 26 | } 27 | 28 | int Solution::isBalanced(TreeNode* A) { 29 | return HeightCheck(A); 30 | } 31 | -------------------------------------------------------------------------------- /Level-7/DynamicProgramming/jumpGame.cpp: -------------------------------------------------------------------------------- 1 | 2 | // https://www.interviewbit.com/problems/jump-game-array/ 3 | 4 | int Solution::canJump(vector &A) { 5 | vector dp(A.size()-1); // how far can we go. 6 | if(A.size()<=1) 7 | return 1; 8 | 9 | if(A[0]<=0) 10 | return 0; 11 | 12 | int fuel =A[0]; 13 | dp[0]=A[0]; 14 | 15 | for(int i=1; ifuel){ 19 | fuel = A[i]; 20 | dp[i] = A[i]; 21 | } 22 | 23 | else{ 24 | dp[i]=dp[i-1]; 25 | } 26 | 27 | if(fuel<=0){ 28 | return 0; 29 | } 30 | } 31 | 32 | return 1; 33 | } 34 | -------------------------------------------------------------------------------- /Level-2/Arrays/pascalTriangleRow.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/pascal-triangle-rows/ 2 | vector > Solution::generate(int A) { 3 | vector >v; 4 | vector temp; 5 | 6 | if(A<1) 7 | return v; 8 | 9 | temp.push_back(1); 10 | v.push_back(temp); 11 | 12 | if(A==1) 13 | return v; 14 | 15 | temp.push_back(1); 16 | v.push_back(temp); 17 | 18 | if(A==2) 19 | return v; 20 | 21 | for(int i=2;i temp2; 23 | temp2.push_back(1); 24 | 25 | for(int j=1;j > Solution::prettyPrint(int A) { 3 | int dimension = A*2 -1, start =A ; 4 | vector > v; 5 | 6 | for(int i=0;i<=dimension/2;i++){ 7 | vector temp; 8 | 9 | // 0 1 2 3 5/2 = 2 (0 1 2) 10 | for(int j=0;j<=dimension/2;j++){ 11 | int val=A-min(i,j); 12 | temp.push_back(val); 13 | } 14 | 15 | v.push_back(temp); 16 | 17 | for(int j=dimension/2-1;j>=0;j--){ 18 | int val=v[i][j]; 19 | v[i].push_back(val); 20 | } 21 | 22 | } 23 | 24 | for(int k = dimension/2-1; k>=0; k--){ 25 | v.push_back(v[k]); 26 | } 27 | 28 | 29 | return v; 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Level-5/Hashing/longestConsSequence.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/longest-consecutive-sequence/ 2 | 3 | int ans(const vector &A) { 4 | map M; 5 | map::iterator it; 6 | 7 | 8 | for(int i=0; ifirst-1 ){ 16 | curConsecutiveness=1; 17 | } 18 | 19 | else if(prev== it->first-1 ){ 20 | curConsecutiveness++; 21 | } 22 | 23 | ans = max(curConsecutiveness, ans); 24 | prev=it->first; 25 | } 26 | 27 | return ans; 28 | } 29 | int Solution::longestConsecutive(const vector &A) { 30 | return ans(A); 31 | } 32 | -------------------------------------------------------------------------------- /Level-4/Stacks-And-Queues/minStack.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/min-stack/ 2 | 3 | stack minAns; 4 | stack values; 5 | 6 | MinStack::MinStack() { 7 | while(!minAns.empty()) 8 | minAns.pop(); 9 | 10 | while(!values.empty()) 11 | values.pop(); 12 | } 13 | 14 | void MinStack::push(int x) { 15 | if( minAns.empty() || x<=minAns.top() ) 16 | minAns.push(x); 17 | values.push(x); 18 | } 19 | 20 | void MinStack::pop() { 21 | if(values.empty()) 22 | return; 23 | 24 | if ( values.top() == minAns.top() ){ 25 | values.pop(); 26 | minAns.pop(); 27 | } 28 | else 29 | values.pop(); 30 | } 31 | 32 | int MinStack::top() { 33 | return values.empty() ? -1 : values.top(); 34 | } 35 | 36 | int MinStack::getMin() { 37 | return minAns.empty() ? -1 : minAns.top(); 38 | } 39 | -------------------------------------------------------------------------------- /Level-6/Heaps-And-Maps/distinctNumbersInWindow.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/distinct-numbers-in-window/ 2 | vector Solution::dNums(vector &A, int B) { 3 | vector ans; 4 | 5 | if(B>A.size()) 6 | return ans; 7 | 8 | map M; 9 | 10 | int count = 0; 11 | 12 | for(int i=0;inext==NULL ){ 13 | return A; 14 | } 15 | 16 | ListNode *slow =A, *fast=A, *temp=A; 17 | 18 | while(slow && fast){ 19 | slow=slow->next; 20 | 21 | if(fast && fast->next){ 22 | fast = fast->next->next; 23 | } 24 | 25 | else 26 | return NULL; 27 | 28 | if(slow==fast){ 29 | break; 30 | } 31 | } 32 | 33 | while(slow && temp!=slow){ 34 | temp=temp->next; 35 | slow=slow->next; 36 | } 37 | 38 | return slow; 39 | } 40 | -------------------------------------------------------------------------------- /Level-2/Math/primeNumbers.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/prime-numbers/ 2 | 3 | vector Solution::sieve(int A) { 4 | vector v ( sqrt(A)+1 ); 5 | fill(v.begin(), v.end(), true); 6 | v[0]=v[1]=false; 7 | 8 | vector ans; 9 | 10 | for(int i=2; i*i<=A; i++ ){ 11 | if(!v[i]) 12 | continue; 13 | 14 | ans.push_back(i); 15 | 16 | for(int j=i*i ; j*j<=A; j=i+j){ 17 | v[j]=false; 18 | } 19 | } 20 | 21 | for(int i=sqrt(A)+1;i<=A;i++){ 22 | bool prime = true; 23 | 24 | for(int j=0; jleft!=NULL && A->right==NULL){ 19 | swap(A->left, A->right); 20 | } 21 | 22 | else if(A->left!=NULL && A->right != NULL){ 23 | swap(A->left, A->right); 24 | TreeNode *temp = A->right; 25 | 26 | while(temp->right!=NULL){ 27 | temp= temp->right; 28 | } 29 | 30 | temp->right = A->left; 31 | A->left = NULL; 32 | } 33 | 34 | flat(A->right); 35 | 36 | } 37 | 38 | TreeNode* Solution::flatten(TreeNode* A) { 39 | flat(A); 40 | return A; 41 | } 42 | -------------------------------------------------------------------------------- /Level-5/Backtracking/subsets2.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/subsets-ii/ 2 | 3 | vector > Solution::subsetsWithDup(vector &A) { 4 | vector > ans; 5 | sort(A.begin(), A.end()); 6 | 7 | for(long long int i=0; i temp; 9 | long long int val = i; 10 | long long int k = 0; 11 | 12 | while(val>0){ 13 | if (val&1) 14 | temp.push_back(A[k]); 15 | 16 | k++; 17 | val = val>>1; 18 | } 19 | 20 | ans.push_back(temp); 21 | } 22 | 23 | sort(ans.begin(), ans.end()); 24 | 25 | vector > ans2; 26 | 27 | for(int i=0;i temp, vector > &v, int sum){ 14 | if(root==NULL){ 15 | return ; 16 | } 17 | 18 | temp.push_back(root->val); 19 | 20 | if(root->left == NULL && root->right == NULL && sum==root->val){ 21 | v.push_back(temp); 22 | temp.clear(); 23 | return; 24 | } 25 | 26 | aPath(root->left, temp, v, sum - root->val); 27 | aPath(root->right, temp, v, sum - root->val); 28 | 29 | } 30 | 31 | vector > Solution::pathSum(TreeNode* root, int sum) { 32 | vector > v; 33 | vector temp; 34 | aPath(root, temp, v, sum); 35 | return v; 36 | } 37 | -------------------------------------------------------------------------------- /Level-2/Math/powOf2int.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/power-of-two-integers/ 2 | // if there is only one unique factor, that would be prime. i.ie the number is of the form prime^n 3 | // if the factor is composite then we must check for the largest composite prime, hence traversal in other direction. 4 | // example 15*15*15 = 3375, sqrt(3375) = 58. 5 | bool Solution::isPower(int A) { 6 | 7 | int num = A; 8 | for(int i=sqrt(A); i>1;i--){ 9 | if(A%i==0){ 10 | while(A>=1 && A%i==0){ 11 | A/=i; 12 | } 13 | 14 | break; 15 | } 16 | } 17 | 18 | if(A==1) 19 | return true; 20 | 21 | A = num; 22 | 23 | for(int i=2; i*i<=A;i++){ 24 | if(A%i==0){ 25 | 26 | while(A>=1 && A%i==0){ 27 | A/=i; 28 | } 29 | 30 | break; 31 | } 32 | } 33 | 34 | if(A==1) 35 | return true; 36 | return false; 37 | } 38 | -------------------------------------------------------------------------------- /Level-6/Heaps-And-Maps/mergeKSortedList.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/merge-k-sorted-lists/ 2 | /** 3 | * Definition for singly-linked list. 4 | * struct ListNode { 5 | * int val; 6 | * ListNode *next; 7 | * ListNode(int x) : val(x), next(NULL) {} 8 | * }; 9 | */ 10 | 11 | ListNode* merge2(ListNode* A, ListNode* B){ 12 | if(A==NULL) 13 | return B; 14 | 15 | if(B==NULL) 16 | return A; 17 | 18 | ListNode* temp; 19 | 20 | if(A->val < B->val){ 21 | temp = A; 22 | temp->next = merge2(A->next, B); 23 | } 24 | 25 | else{ 26 | temp = B; 27 | temp->next = merge2(A, B->next); 28 | } 29 | 30 | return temp; 31 | } 32 | 33 | ListNode* Solution::mergeKLists(vector &A) { 34 | if(A.size()==0) 35 | return NULL; 36 | 37 | ListNode* ans = A[0]; 38 | 39 | for(int i=0; inext; 21 | curr= curr->next; 22 | } 23 | 24 | while(curr && n--){ 25 | after = curr ->next; 26 | curr->next = pre; 27 | head = curr; 28 | pre = curr; 29 | curr = after; 30 | } 31 | 32 | if(mInitially!=1) 33 | prevHead2->next = pre; 34 | 35 | prevHead->next = after; 36 | 37 | if(mInitially!=1) 38 | return A; 39 | 40 | return head; 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Level-8/Graphs/blackShapes.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/black-shapes/ 2 | 3 | void bfs (int i, int j, vector &A){ 4 | if(i>0 && A[i-1][j]=='X'){ 5 | A[i-1][j]='O'; 6 | bfs(i-1, j, A); 7 | } 8 | 9 | if(i0 && A[i][j-1]=='X'){ 15 | A[i][j-1]='O'; 16 | bfs(i, j-1, A); 17 | } 18 | 19 | if(j &A) { 28 | int len = A.size(); 29 | int lenvec = A[0].size(); 30 | int ans = 0; 31 | 32 | for(int i=0; i Solution::flip(string A) { 3 | vector ZeroOrOne(A.size()); // if zero then 1, if one then -1. 4 | vector ans; 5 | 6 | for(int i = 0; imaxSum){ 26 | leftAns = left; 27 | rightAns = i; 28 | maxSum=cumulative; 29 | } 30 | } 31 | 32 | if(maxSum<=0){ 33 | return ans; 34 | } 35 | 36 | else{ 37 | ans.push_back(leftAns+1); 38 | ans.push_back(rightAns+1); 39 | } 40 | 41 | return ans; 42 | } 43 | -------------------------------------------------------------------------------- /Level-4/Linked-List/partitionList.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/partition-list/ 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * struct ListNode { 6 | * int val; 7 | * ListNode *next; 8 | * ListNode(int x) : val(x), next(NULL) {} 9 | * }; 10 | */ 11 | 12 | ListNode* Solution::partition(ListNode* A, int B) { 13 | if(A==NULL || A->next==NULL) 14 | return A; 15 | 16 | ListNode* fakeHead = new ListNode(0), *temp=A, *last=A; 17 | fakeHead->next = A; 18 | A = fakeHead; 19 | int n =0; 20 | 21 | while(temp){ 22 | temp=temp->next; 23 | n++; 24 | } 25 | 26 | while(last->next){ 27 | last=last->next; 28 | } 29 | 30 | while(n--){ 31 | 32 | if(fakeHead->next->val next; 34 | } 35 | 36 | else{ 37 | last->next = new ListNode(fakeHead->next->val); 38 | last = last->next; 39 | fakeHead->next = fakeHead->next->next; 40 | } 41 | } 42 | 43 | return A->next; 44 | } 45 | -------------------------------------------------------------------------------- /Level-6/Trees/populateNextRPointerExtraMemo.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/populate-next-right-pointers-tree/ 2 | /** 3 | * Definition for binary tree with next pointer. 4 | * struct TreeLinkNode { 5 | * int val; 6 | * TreeLinkNode *left, *right, *next; 7 | * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} 8 | * }; 9 | */ 10 | void Solution::connect(TreeLinkNode* A) { 11 | deque q; 12 | 13 | if(A==NULL) 14 | return ; 15 | 16 | q.push_back(A); 17 | 18 | while(!q.empty()){ 19 | int count = q.size(); 20 | 21 | for(int i = 0; inext = q[i+1]; 23 | } 24 | 25 | for(int i=0; ileft) 27 | q.push_back(q.front()->left); 28 | 29 | if(q.front()->left) 30 | q.push_back(q.front()->right); 31 | 32 | q.pop_front(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Level-2/Arrays/setMatrixZeroConstSpace.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/set-matrix-zeros/ 2 | void Solution::setZeroes(vector > &A) { 3 | int r = A.size(); 4 | int c = A[0].size(); 5 | bool firstRowHasZero = false, firstColHasZero = false; 6 | 7 | for(int j=0;j Solution::maxset(vector &A) { 3 | long long int start=0, end=0, ansStart=0, length=0, ansLength=0, ansEnd=-1, sumTillNow = INT_MIN, maxTillNow=INT_MIN; 4 | int i=0; 5 | 6 | while(i=0){ 8 | start = i; 9 | sumTillNow =0; 10 | length =0; 11 | 12 | while(A[i]>=0 && imaxTillNow ) || ( sumTillNow==maxTillNow && end - start +1 < ansLength ) ){ 19 | ansStart = start; 20 | ansEnd = end; 21 | ansLength = start + end -1 ; 22 | maxTillNow = sumTillNow; 23 | } 24 | } 25 | i++; 26 | 27 | } 28 | vector ans; 29 | for(int i=ansStart; i<=ansEnd;i++){ 30 | ans.push_back(A[i]); 31 | } 32 | 33 | return ans; 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Level-2/Arrays/spiralOrderMatrix.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/spiral-order-matrix-ii/ 2 | 3 | vector > Solution::generateMatrix(int A) { 4 | vector > ans(A, vector(A)); 5 | 6 | if(A==0) 7 | return ans; 8 | 9 | int xStart =0; 10 | int yStart =0; 11 | int xEnd = A-1; 12 | int yEnd = A-1; 13 | int n=1; 14 | 15 | while(n<=(A*A)){ 16 | 17 | for(int i= yStart; i<=yEnd; i++){ 18 | ans[xStart][i]=n; 19 | n++; 20 | } 21 | 22 | xStart++; 23 | 24 | for(int i= xStart; i<=xEnd ; i++){ 25 | ans[i][yEnd]=n; 26 | n++; 27 | } 28 | 29 | yEnd--; 30 | 31 | for(int i= yEnd ; i>=yStart; i--){ 32 | ans[xEnd][i]=n; 33 | n++; 34 | } 35 | 36 | xEnd--; 37 | 38 | for(int i= xEnd; i>=xStart; i--){ 39 | ans[i][yStart]=n; 40 | n++; 41 | } 42 | 43 | yStart++; 44 | } 45 | 46 | return ans; 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Level-5/Hashing/pointsOnLine2.cpp: -------------------------------------------------------------------------------- 1 | int Solution::maxPoints(vector &A, vector &B) { 2 | 3 | if(A.size()<=2) 4 | return A.size(); 5 | 6 | int result=0; 7 | 8 | for(int i=0;i M; 13 | 14 | for(int j=i+1;j::iterator it=M.begin() ;it!=M.end(); it++){ 37 | result=max(result,it->second+dup); 38 | } 39 | 40 | result = max(result, dup+vert); // as vert can be considered as another slope values. 41 | 42 | } 43 | return result; 44 | } 45 | -------------------------------------------------------------------------------- /Level-3/Strings/longestPalinNsqSpace.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/longest-palindromic-substring/ 2 | string Solution::longestPalindrome(string A) { 3 | 4 | vector < vector > Table (A.size(), vector(A.size()+1)); 5 | string ans=A.substr(0,1); 6 | int ansLength = 1; 7 | 8 | for(int i=0;iansLength){ 28 | ansLength = lenToCheck; 29 | ans = A.substr(i,lenToCheck); 30 | } 31 | } 32 | } 33 | } 34 | 35 | return ans; 36 | } 37 | -------------------------------------------------------------------------------- /Level-2/Arrays/maxsumDandC.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/max-sum-contiguous-subarray/ 2 | 3 | int midMax(vector A, int start, int mid, int end); 4 | int recurseAns(vector A, int start, int end); 5 | 6 | int Solution::maxSubArray(const vector &A) { 7 | return recurseAns(A, 0, A.size() -1); 8 | } 9 | 10 | int midMax(vector A, int start, int mid, int end){ 11 | int ans=INT_MIN; 12 | int cumulativeSum = 0; 13 | 14 | for(int i=mid;i>=start;i--){ 15 | cumulativeSum += A[i]; 16 | 17 | if(cumulativeSum > ans) 18 | ans = cumulativeSum; 19 | } 20 | 21 | cumulativeSum =0; 22 | int ans2=INT_MIN; 23 | for(int j=mid+1;j<=end;j++){ 24 | cumulativeSum += A[j]; 25 | 26 | if(cumulativeSum > ans2) 27 | ans2 = cumulativeSum; 28 | } 29 | 30 | return ans+ans2; 31 | } 32 | 33 | int recurseAns(vector A, int start, int end){ 34 | int mid = ( end + start )/2; 35 | if(end==start) 36 | return A[end]; 37 | 38 | return max ( max ( recurseAns(A, start, mid), 39 | recurseAns(A,mid+1, end) ), midMax(A, start, mid, end) ); 40 | } 41 | -------------------------------------------------------------------------------- /Level-6/Trees/LCAnaive.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/least-common-ancestor/ 2 | /** 3 | * Definition for binary tree 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 | 12 | bool isIn(TreeNode *A, int val){ 13 | if(A==NULL) 14 | return false; 15 | 16 | if(A->val == val) 17 | return true; 18 | 19 | return isIn(A->left, val) || isIn(A->right, val); 20 | } 21 | 22 | int Solution::lca(TreeNode* A, int val1, int val2) { 23 | if(A==NULL) 24 | return -1; 25 | 26 | if( (A->val == val1 && isIn(A, val2) ) || (A->val == val2 && isIn(A, val1) ) ) 27 | return A->val; 28 | 29 | if(isIn(A->left, val1) && isIn(A->right, val2)) 30 | return A->val; 31 | 32 | if(isIn(A->right, val1) && isIn(A->left, val2)) 33 | return A->val; 34 | 35 | if(isIn(A->left, val1) && isIn(A->left, val2)) 36 | return lca(A->left, val1, val2); 37 | 38 | if(isIn(A->right, val1) && isIn(A->right, val2)) 39 | return lca(A->right, val1, val2); 40 | 41 | return -1; 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Level-6/Trees/populateNextRightPointers.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/populate-next-right-pointers-tree/ 2 | void Solution::connect(TreeLinkNode* A) { 3 | if(A==NULL) 4 | return; 5 | 6 | TreeLinkNode *cur =A, *leftWall =NULL, *prev =NULL; 7 | 8 | while(cur){ 9 | 10 | while(cur){ 11 | if(cur->left){ 12 | if(prev!=NULL){ 13 | prev->next = cur->left; 14 | 15 | } 16 | 17 | else{ 18 | leftWall = cur->left; 19 | } 20 | 21 | prev = cur->left; 22 | } 23 | 24 | if(cur->right){ 25 | if(prev!=NULL){ 26 | prev->next = cur->right; 27 | } 28 | 29 | else{ 30 | leftWall = cur->right; 31 | } 32 | prev = cur->right; 33 | 34 | } 35 | 36 | cur = cur->next; 37 | } 38 | 39 | cur = leftWall; 40 | leftWall = NULL; 41 | prev = NULL; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Level-6/Trees/kthSmallestBst.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/kth-smallest-element-in-tree/ 2 | // http://algorithmsandme.in/2013/08/find-kth-smallest-element-in-binary-search-tree/ 3 | /** 4 | * Definition for binary tree 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | 13 | int sizeTree(TreeNode *root){ 14 | if(root==NULL) 15 | return 0; 16 | 17 | return 1 + sizeTree(root->left) + sizeTree(root->right); 18 | } 19 | 20 | int Solution::kthsmallest(TreeNode* root, int k) { 21 | 22 | int leftSize = sizeTree(root->left); 23 | 24 | if(leftSize == k-1) 25 | return root->val; 26 | 27 | if(leftSize > k-1) 28 | return kthsmallest(root->left, k); 29 | 30 | return kthsmallest(root->right, k - leftSize - 1); 31 | 32 | } 33 | /* 34 | Using inOrder Traversal 35 | int findKthNode(Node * root, int *n, int K){ 36 | if(!root) return -1; 37 | 38 | findKthNode(root->left, n, K); 39 | (*n)++; 40 | if(K == *n){ 41 | return root->value; 42 | } 43 | findKthNode(root->right, n, K); 44 | } 45 | 46 | */ 47 | -------------------------------------------------------------------------------- /Level-3/Strings/cmpVersionNum.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/compare-version-numbers/ 2 | 3 | int Solution::compareVersion(string A, string B) { 4 | int i = 0, j = 0; 5 | 6 | while(i 0 ? 1: -1; 31 | return x.length()>y.length() ? 1:-1; 32 | } 33 | 34 | i++; 35 | j++; 36 | } 37 | 38 | while(i=A.length() && j>=B.length()) 47 | return 0; 48 | 49 | return i>j ? 1 : -1; 50 | } 51 | -------------------------------------------------------------------------------- /Level-6/Trees/LCAstandard.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/least-common-ancestor/ 2 | /** 3 | * Definition for binary tree 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 | 12 | TreeNode* ans(TreeNode* root, int val1, int val2){ 13 | if(root==NULL) 14 | return NULL; 15 | 16 | if(root->val == val1 || root->val == val2){ 17 | return root; 18 | } 19 | 20 | TreeNode* L = ans(root->left, val1, val2); 21 | TreeNode* R = ans(root->right, val1, val2); 22 | 23 | if(L&&R) 24 | return root; 25 | 26 | return L ? L : R; 27 | 28 | } 29 | 30 | bool find(TreeNode* root, int val){ 31 | if(root==NULL) 32 | return false; 33 | 34 | if(root->val == val) 35 | return true; 36 | 37 | return find(root->left, val) || find(root->right, val) ; 38 | } 39 | 40 | int Solution::lca(TreeNode* A, int val1, int val2) { 41 | if(find(A, val1) ==NULL || find(A, val2) ==NULL) 42 | return -1; 43 | 44 | if(ans(A, val1, val2) !=NULL){ 45 | return ans(A, val1, val2)->val; 46 | } 47 | 48 | return -1; 49 | } 50 | -------------------------------------------------------------------------------- /Level-2/Arrays/mergeOverlappingIntervals.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/merge-overlapping-intervals/ 2 | /** 3 | * Definition for an interval. 4 | * struct Interval { 5 | * int start; 6 | * int end; 7 | * Interval() : start(0), end(0) {} 8 | * Interval(int s, int e) : start(s), end(e) {} 9 | * }; 10 | */ 11 | 12 | bool comp(Interval a, Interval b){ 13 | if(a.start == b.start) 14 | return a.end < a.end; 15 | return a.start < b.start; 16 | } 17 | 18 | vector Solution::merge(vector &A) { 19 | 20 | vector ans; 21 | 22 | sort(A.begin(),A.end(), comp); 23 | 24 | ans.push_back(A[0]); 25 | 26 | for(int i=0; i=A[i].start && A[i+1].start<=A[i].end ){ 30 | ans[ans.size()-1].start = A[i+1].start = min(A[i].start,A[i+1].start); 31 | ans[ans.size()-1].end = A[i+1].end = max(A[i].end,A[i+1].end); 32 | } 33 | 34 | else{ 35 | x.start = A[i+1].start; 36 | x.end = A[i+1].end; 37 | ans.push_back(x); 38 | } 39 | 40 | } 41 | 42 | return ans; 43 | } 44 | -------------------------------------------------------------------------------- /Level-6/checkpointInversions.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/inversions/ 2 | int Merge(vector&A, int start, int start2, int end){ 3 | vector temp(end-start+1); 4 | int k= 0; 5 | int i = start; 6 | int j = start2; 7 | int count =0 ; 8 | 9 | while(i<=start2-1 && j<=end){ 10 | if(A[i]<=A[j]){ 11 | temp[k]=A[i]; 12 | i++; 13 | } 14 | 15 | else{ 16 | temp[k]=A[j]; 17 | j++; 18 | 19 | count += start2 - i; 20 | } 21 | 22 | k++; 23 | } 24 | 25 | while(i<=start2-1){ 26 | temp[k]=A[i]; 27 | k++; 28 | i++; 29 | } 30 | 31 | while(j<=end){ 32 | temp[k]=A[j]; 33 | k++; 34 | j++; 35 | } 36 | 37 | for(int i =start ; i<=end; i++){ 38 | A[i] = temp[i-start]; 39 | } 40 | 41 | return count; 42 | } 43 | 44 | int countMerge(vector &A, int start, int end){ 45 | if(start>=end) 46 | return 0; 47 | 48 | int mid = (start+end)/2; 49 | 50 | return countMerge(A, start, mid ) + countMerge(A, mid+1, end ) + Merge(A, start, mid+1, end); 51 | } 52 | 53 | 54 | int Solution::countInversions(vector &A) { 55 | return countMerge(A, 0, A.size()-1); 56 | } 57 | -------------------------------------------------------------------------------- /Level-6/Heaps-And-Maps/mergeKsortedListsP_QUEUE.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/merge-k-sorted-lists/ 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * struct ListNode { 6 | * int val; 7 | * ListNode *next; 8 | * ListNode(int x) : val(x), next(NULL) {} 9 | * }; 10 | */ 11 | void pushNode(int val, ListNode** root){ 12 | ListNode *node = new ListNode(val); 13 | node->next = NULL; 14 | ListNode *temp= *root; 15 | 16 | if(temp==NULL){ 17 | *root = node; 18 | return; 19 | } 20 | 21 | while(temp->next){ 22 | temp = temp->next; 23 | } 24 | 25 | temp->next = node; 26 | } 27 | 28 | ListNode* Solution::mergeKLists(vector &A) { 29 | priority_queue < pair , vector< pair >, greater > > q; 30 | 31 | vector::iterator x; 32 | 33 | for(x=A.begin(); x!=A.end(); x++){ 34 | q.push(make_pair ( (*x)->val, *x)); 35 | } 36 | 37 | ListNode* ans = NULL; 38 | 39 | while(q.empty()!=true){ 40 | pushNode((q.top()).first, &ans); 41 | ListNode* forNext = q.top().second->next; 42 | q.pop(); 43 | 44 | if(forNext) 45 | q.push(make_pair ( forNext->val, forNext)); 46 | } 47 | 48 | return ans; 49 | } 50 | -------------------------------------------------------------------------------- /Level-6/Trees/inorderTraversal.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/inorder-traversal/ 2 | 3 | /** 4 | * Definition for binary tree 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | 13 | /* 14 | pseudo 15 | func(TreeNode* A) 16 | if(A==NULL) 17 | return 18 | 19 | func(A->left) 20 | data 21 | func(A->right) 22 | 23 | */ 24 | vector Solution::inorderTraversal(TreeNode* A) { 25 | stack > s; //bool will say if left happened or not. 26 | vector ans; 27 | 28 | if(A==NULL) 29 | return ans; 30 | 31 | s.push(make_pair(A, false) ); 32 | 33 | while(!s.empty()){ 34 | 35 | if((s.top().first)->left!=NULL && s.top().second==false ){ 36 | s.top().second = true; 37 | s.push(make_pair((s.top().first)->left, false) ); 38 | } 39 | 40 | else{ 41 | ans.push_back((s.top().first)->val); 42 | TreeNode *temp = (s.top().first); 43 | s.pop(); 44 | 45 | if(temp->right!=NULL) 46 | s.push(make_pair(temp->right, false)); 47 | } 48 | 49 | } 50 | 51 | return ans; 52 | } 53 | -------------------------------------------------------------------------------- /Level-8/Graphs/levelOrder.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/level-order/ 2 | 3 | /** 4 | * Definition for binary tree 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | vector > Solution::levelOrder(TreeNode* A) { 13 | vector > v; 14 | vector value; 15 | queue Q; 16 | 17 | if(A==NULL) 18 | return v; 19 | 20 | value.push_back(A->val); 21 | 22 | 23 | Q.push(A); 24 | v.push_back(value); 25 | 26 | long long int maxNode = 1; 27 | 28 | while(Q.empty()!=true){ 29 | vector temp; 30 | int count = Q.size(); 31 | 32 | while(count){ 33 | 34 | if(Q.front()->left){ 35 | Q.push(Q.front()->left); 36 | temp.push_back(Q.front()->left->val); 37 | } 38 | 39 | 40 | if(Q.front()->right){ 41 | Q.push(Q.front()->right); 42 | temp.push_back(Q.front()->right->val); 43 | } 44 | 45 | Q.pop(); 46 | count--; 47 | } 48 | 49 | if(!temp.empty()) 50 | v.push_back(temp); 51 | 52 | } 53 | 54 | return v; 55 | } 56 | -------------------------------------------------------------------------------- /Level-3/Strings/LongestPalinConstSpace.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/longest-palindromic-substring/ 2 | 3 | string Solution::longestPalindrome(string A) { 4 | 5 | /*odd case*/ 6 | int ansLen=1; 7 | string ans=A.substr(0,1); 8 | 9 | for(int i=1;i=0 && high<=A.size()-1 ){ 13 | 14 | if(high-low+1 > ansLen){ 15 | ansLen = high-low +1; 16 | ans = A.substr(low,ansLen); 17 | } 18 | 19 | low--, high++; 20 | } 21 | 22 | } 23 | /*even case*/ 24 | for(int i=1;i=0 && high<=A.size()-1 ){ 28 | if(high-low+1 > ansLen){ 29 | ansLen = high-low +1; 30 | ans = A.substr(low,ansLen); 31 | } 32 | low--, high++; 33 | } 34 | 35 | 36 | 37 | } 38 | 39 | 40 | return ans; 41 | // Do not write main() function. 42 | // Do not read input, instead use the arguments to the function. 43 | // Do not print the output, instead return values as specified 44 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 45 | } 46 | -------------------------------------------------------------------------------- /Level-4/Stacks-And-Queues/simplifyDirectoryPath.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/simplify-directory-path/ 2 | 3 | string Solution::simplifyPath(string A) { 4 | stack s; 5 | 6 | for(int i=0;i1 && ans==".."){ 20 | int slashCount = 0; 21 | 22 | while(!s.empty() &&slashCount!=2){ 23 | if(s.top()=='/') 24 | slashCount++; 25 | s.pop(); 26 | } 27 | s.push('/'); 28 | } 29 | 30 | else{ 31 | 32 | for(int j=0; j Solution::preorderTraversal(TreeNode* A) { 12 | // stack < pair < TreeNode*, bool > > s; // bool isLeft. 13 | // vector ans; 14 | 15 | // if(A==NULL) 16 | // return ans; 17 | 18 | // s.push(make_pair(A, false) ); 19 | 20 | // while(!s.empty()){ 21 | // ans.push_back(s.top().first->val); 22 | // TreeNode *temp = s.top().first; 23 | 24 | // s.top().second = false; 25 | 26 | // while(temp->left){ 27 | // s.push(make_pair (temp->left, true) ); 28 | // temp = temp->left; 29 | // } 30 | 31 | // if(temp->right && s.top().second){ 32 | // s.push(make_pair (temp->right, false) ); 33 | // } 34 | 35 | // } 36 | stack < TreeNode* > s; //bool will say if left happened or not. 37 | vector ans; 38 | 39 | if(A==NULL) 40 | return ans; 41 | 42 | s.push(A); 43 | 44 | while(!s.empty()){ 45 | ans.push_back(s.top()->val); 46 | TreeNode *temp = s.top(); 47 | 48 | s.pop(); 49 | 50 | if(temp->right) 51 | s.push(temp->right); 52 | 53 | if(temp->left) 54 | s.push(temp->left); 55 | } 56 | 57 | return ans; 58 | } 59 | -------------------------------------------------------------------------------- /Level-3/Binary-Search/rotatedSortedSearch.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/rotated-sorted-array-search/ 2 | 3 | int Solution::search(const vector &A, int B) { 4 | int low =0, high = A.size()-1; 5 | int start2 =0; 6 | bool linear = false; 7 | 8 | while(lowA[high]){ 18 | low = mid+1; 19 | } 20 | 21 | else { 22 | high =mid; 23 | } 24 | } 25 | 26 | if(linear){ 27 | for(int i=0;iB){ 44 | high2=mid-1; 45 | } 46 | 47 | else{ 48 | low2=mid+1; 49 | } 50 | } 51 | 52 | low = high; 53 | high = A.size()-1; 54 | 55 | while(low<=high){ 56 | int mid = (low+high)/2; 57 | 58 | if(A[mid]==B){ 59 | return mid; 60 | } 61 | 62 | if(A[mid]>B){ 63 | high=mid-1; 64 | } 65 | 66 | else{ 67 | low=mid+1; 68 | } 69 | } 70 | } 71 | 72 | return -1; 73 | } 74 | -------------------------------------------------------------------------------- /Level-5/Hashing/fraction.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/fraction/ 2 | long long int gcd(long long int x, long long int y){ 3 | if(y==0) 4 | return x; 5 | 6 | return gcd(y, x%y); 7 | } 8 | 9 | string Solution::fractionToDecimal(int numerator, int denominator) { 10 | map numOccured; 11 | long long int num = numerator, deno = denominator; 12 | 13 | if(num == 0 || deno == 0) 14 | return "0"; 15 | 16 | bool isNegNum = num<0; 17 | bool isNegDen = deno<0; 18 | num = abs(num); 19 | deno = abs(deno); 20 | 21 | long long int gcdND = gcd(num, deno); 22 | num = num/gcdND; 23 | deno = deno/gcdND; 24 | string ans = ""; 25 | 26 | if(num>=deno){ 27 | ans += to_string(num/deno); 28 | num = num%deno; 29 | } 30 | 31 | else 32 | ans += "0"; 33 | 34 | string rep=""; 35 | int i = 0; 36 | int j = 0; 37 | 38 | if(num!=0){ 39 | ans+="."; 40 | 41 | while(1){ 42 | num*=10; 43 | 44 | if(num && numOccured.count(num)==0){ 45 | numOccured[num]=i; 46 | rep += to_string(num/deno); 47 | num = num%deno; 48 | i++; 49 | } 50 | 51 | 52 | else if(num==0){ 53 | ans+=rep; 54 | break; 55 | } 56 | 57 | else if(numOccured.count(num)!=0){ 58 | j = numOccured[num]; 59 | ans+= rep.substr(0,j) + "(" + rep.substr(j,rep.size()-j) + ")"; 60 | break; 61 | } 62 | 63 | } 64 | } 65 | 66 | if(isNegNum ^ isNegDen) 67 | return "-" + ans; 68 | 69 | return ans; 70 | } 71 | -------------------------------------------------------------------------------- /Level-5/Hashing/pointsOnTheLine.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/points-on-the-straight-line/ 2 | // This solution gives Runtime error, maybe because its slow, but definitely more accurate. As no precision related error may crept in. 3 | 4 | int gcd(int x, int y){ 5 | if(y==0) 6 | return x; 7 | 8 | return gcd(y, x%y); 9 | 10 | } 11 | 12 | int Solution::maxPoints(vector &A, vector &B) { 13 | 14 | if(A.size()<=2) 15 | return A.size(); 16 | 17 | int result=0; 18 | 19 | for(int i=0;i > v; 22 | 23 | for(int j=0;j=0) 50 | v.push_back(make_pair(-y,-x)); 51 | 52 | else 53 | v.push_back(make_pair(y,x)); 54 | 55 | } 56 | } 57 | 58 | 59 | sort(v.begin(), v.end()); 60 | int pp = 1; //points on plane 61 | 62 | if(v.size()==0) 63 | pp = 0; 64 | 65 | for(int k=1;k
10 | **Example:** Given **[3, 30, 34, 5, 9]**, the largest formed number is ***9534330***. 11 |

12 | **Note:** The result may be very large, so you need to return a string instead of an integer. 13 |

14 | [Solution](https://github.com/sidgupta234/InterviewBit/blob/master/Level-2/Arrays/LargestNumber.cpp) 15 |
16 |
17 |
18 | #### [Add one to number](https://www.interviewbit.com/problems/add-one-to-number/) 19 | Given a non-negative number represented as an array of digits, add 1 to the number ( increment the number represented by the digits ). 20 | The digits are stored such that the most significant digit is at the head of the list. 21 |

22 | **Example:** If the vector has **[1, 2, 3]** the returned vector should be ***[1, 2, 4]*** 23 | as 123 + 1 = 124. 24 |

25 | **Note**: The result may be very large, so you need to return a string instead of an integer. 26 |

27 | [Solution](https://github.com/sidgupta234/InterviewBit/blob/master/Level-2/Arrays/addOne.cpp) 28 |
29 |
30 |
31 | #### [Anti diagonals](https://www.interviewbit.com/problems/anti-diagonals/) 32 | Given a NxN square matrix, return an array of its anti-diagonals. Look at the example for more details. 33 |

34 | **Examples:** 35 |
36 | For, **[1, 2, 3, 4, 5, 6, 7, 8, 9]** you should return, ***[ [1], [2, 4], [3, 5, 7], [6, 8], [9] ]*** 37 |
38 | For, **[1, 2, 3, 4]** you should return, ***[ [1], [2, 3], [4] ]*** 39 |

40 | [Solution](https://github.com/sidgupta234/InterviewBit/blob/master/Level-2/Arrays/antiDiagonals.cpp) 41 |
42 |
43 |
44 | #### [Find duplicate in array](https://www.interviewbit.com/problems/find-duplicate-in-array/) 45 | Given a read only array of n + 1 integers between 1 and n, find one number that repeats in linear time using less than O(n) space and traversing the stream sequentially O(1) times. 46 |

47 | **Example:** For **[3, 4, 1, 4, 1]**, you should return ***1***. 48 |

49 | **Note**: If there are multiple possible answers ( like in the sample case above ), output any one. 50 |

51 | [Solution](https://github.com/sidgupta234/InterviewBit/blob/master/Level-2/Arrays/findDuplicate.cpp) 52 |
53 |
54 |
55 | #### [First missing integer](https://www.interviewbit.com/problems/first-missing-integer/) 56 | Given an unsorted integer array, find the first missing positive integer. 57 |

58 | **Example:** For **[1,2,0]** return ***3***, **[3,4,-1,1]** return ***2***, **[-8, -7, -6]** returns ***1*** 59 |

60 | **Note**: Your algorithm should run in O(n) time and use constant space. 61 |

62 | [Solution](https://github.com/sidgupta234/InterviewBit/blob/master/Level-2/Arrays/firstMissingNumb.cpp) 63 |
64 |
65 |
66 | #### [Kth row of pascal's triangle](https://www.interviewbit.com/problems/kth-row-of-pascals-triangle/) 67 | Given an index **k**, return the **kth** row of the Pascal’s triangle. 68 |

69 | **Example:** For **k = 3**, return ***[1,3,3,1]*** 70 |

71 | **Note:** k is 0 based. k = 0, corresponds to the row [1]. Could you optimize your algorithm to use only O(k) extra space? 72 |

73 | [Solution](https://github.com/sidgupta234/InterviewBit/blob/master/Level-2/Arrays/kthRowOfPascal.cpp) 74 |
75 |
76 |
77 | #### [Max non-negative subarray](https://www.interviewbit.com/problems/max-non-negative-subarray/) 78 | Find out the maximum sub-array of non negative numbers from an array. The sub-array should be continuous. That is, a sub-array created by choosing the second and fourth element and skipping the third element is invalid. 79 |

80 | Maximum sub-array is defined in terms of the sum of the elements in the sub-array. Sub-array A is greater than sub-array B if sum(A) > sum(B). 81 |

82 | **Example:** For **[1, 2, 5, -7, 2, 3]**, their are two sub-arrays that follow the constraint; **[1, 2, 5]** and **[2, 3]**. The answer is ***[1, 2, 5]*** as its sum is larger than [2, 3]. 83 |

84 | **Note:** If there is a tie, then compare with segment's length and return segment which has maximum length. 85 |
86 | **Note 2:** If there is still a tie, then return the segment with minimum starting index. 87 |

88 | [Solution](https://github.com/sidgupta234/InterviewBit/blob/master/Level-2/Arrays/maxNonNegSubarray.cpp) 89 |
90 |
91 |
92 | #### [Max sum contiguous subarray](https://www.interviewbit.com/problems/max-sum-contiguous-subarray/) 93 | Find the contiguous subarray within an array (containing at least one number) which has the largest sum. 94 |

95 | **Example:** For **[-2,1,-3,4,-1,2,1,-5,4]**, the contiguous subarray **[4,-1,2,1]** has the largest sum, which is ***6***. 96 |

97 | For this problem, return the maximum sum. 98 |

99 | [Solution](https://github.com/sidgupta234/InterviewBit/blob/master/Level-2/Arrays/maxSumKadane.cpp) 100 |
101 |
102 |
103 | #### [Merge overlapping intervals](https://www.interviewbit.com/problems/merge-overlapping-intervals/) 104 | Given a collection of intervals, merge all overlapping intervals. 105 |

106 | **Example:** Given **[1,3], [2,6], [8,10], [15,18]**, return ***[1,6] [8,10] [15,18]***. 107 |

108 | **Note:** Make sure the returned intervals are sorted. 109 |

110 | [Solution](https://github.com/sidgupta234/InterviewBit/blob/master/Level-2/Arrays/mergeOverlappingIntervals.cpp) 111 |
112 |
113 |
114 | #### [Min steps in infinite grid](https://www.interviewbit.com/problems/min-steps-in-infinite-grid/) 115 | You are in an infinite 2D grid where you can move in any of the 8 directions : 116 |
117 | (x,y) to
118 | (x+1, y),
119 | (x - 1, y),
120 | (x, y+1),
121 | (x, y-1),
122 | (x-1, y-1),
123 | (x+1,y+1),
124 | (x-1,y+1),
125 | (x+1,y-1)

126 | You are given a sequence of points and the order in which you need to cover the points. Give the minimum number of steps in which you can achieve it. You start from the first point. 127 |

128 | **Example:** For **[(0, 0), (1, 1), (1, 2)]**, return ***2***. 129 |
130 | It takes 1 step to move from (0, 0) to (1, 1). It takes one more step to move from (1, 1) to (1, 2). 131 |

132 | [Solution](https://github.com/sidgupta234/InterviewBit/blob/master/Level-2/Arrays/minSteps.cpp) 133 |
134 |
135 |
136 | #### [Pascal triangle rows](https://www.interviewbit.com/problems/pascal-triangle-rows/) 137 | Given numRows, generate the first numRows of Pascal’s triangle. 138 |

139 | **Example:** For numRows = **5**, Return ***[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]*** 140 |

141 | [Solution](https://github.com/sidgupta234/InterviewBit/blob/master/Level-2/Arrays/pascalTriangleRow.cpp) 142 |
143 |
144 |
145 | #### [Repeat and missing number array](https://www.interviewbit.com/problems/repeat-and-missing-number-array/) 146 | You are given a read only array of n integers from 1 to n. 147 | Each integer appears exactly once except A which appears twice and B which is missing. Return A and B. 148 |

149 | **Note:** Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 150 |
151 | **Note 2:** that in your output A should precede B. 152 |

153 | **Example:** For ***[3, 1, 2, 5, 3]*** return ***[3, 4]*** 154 |

155 | [Solution](https://github.com/sidgupta234/InterviewBit/blob/master/Level-2/Arrays/repeatAndMissingNum.cpp) 156 |
157 |
158 |
159 | #### [Set matrix zeros](https://www.interviewbit.com/problems/set-matrix-zeros/) 160 | Given an m x n matrix of 0s and 1s, if an element is 0, set its entire row and column to 0. Do it in place. 161 |

162 | **Example:** For a given array A as **[ [1, 0 ,1], [1, 1, 1], [1, 1, 1,] ]**, on returning, the array A should be ***[ [0, 0 ,0], [1, 0, 1], [1, 0, 1] ]*** 163 |

164 | **Note:** Try to minimize the space and time complexity. 165 |

166 | [Solution](https://github.com/sidgupta234/InterviewBit/blob/master/Level-2/Arrays/setMatrixZeroConstSpace.cpp) 167 |
168 |
169 |
170 | #### [Wave array](https://www.interviewbit.com/problems/wave-array/) 171 | Given an array of integers, sort the array into a wave like array and return it, In other words, arrange the elements into a sequence such that a1 >= a2 <= a3 >= a4 <= a5..... 172 |

173 | **Example:** Given **[1, 2, 3, 4]**, possible answers could be ***[2, 1, 4, 3]*** or ***[4, 1, 3, 2]***. 174 |

175 | **Note:** Multiple answers are possible, return the one that is lexicographically smallest. 176 |

177 | [Solution](https://github.com/sidgupta234/InterviewBit/blob/master/Level-2/Arrays/waveArray.cpp) 178 |
179 |
180 |
181 | --------------------------------------------------------------------------------