├── .gitattributes ├── Array ├── Add One To Number ├── Anti Diagonals ├── Find Duplicate in Array ├── Find Missing Integer ├── Flip ├── Hotel Bookings Possible ├── Intersecting Chords in a Circle ├── Jump Game Array ├── Kth Row of Pascal's Triangle ├── Largest Number ├── Max Distance ├── Max Non Negative SubArray ├── Max Sum Contiguous Subarray ├── Maximum Absolute difference ├── Maximum Unsorted Subarray ├── Merge Intervals ├── Merge Overlapping Intervals ├── Min Steps in Infinite Grid ├── Noble Integer ├── Pascal triangle rows ├── README.md ├── Repeat Number ├── Repeat and missing number array ├── Rotate Matrix ├── Set Matrix Zeros ├── Spiral Order Matrix I ├── Spiral Order Matrix II ├── Stairs ├── Wave array └── Ways to Decode ├── Binary Search ├── Allocate books ├── Count Element Occurence ├── Implement power function ├── Matrix Median ├── Matrix Search ├── Painter's Partition Problem ├── Rotated Array ├── Search for a range ├── Sorted Insert Position └── Square Root Of Integer ├── DynamicProgramming ├── Intersecting Chords in a Circle ├── Jump Game Array ├── Largest area of rectangle with permutations ├── Length of Longest Subsequence ├── Longest Arithmetic Progression ├── Max Sum Without Adjacent Elements ├── Min jumps array ├── N digit numbers with digit sum S ├── Stairs └── Ways to decode ├── Greedy ├── Assign mice to holes ├── Bulbs ├── Distribute Candy ├── Gas Station ├── Highest Product ├── Majority Element └── seats ├── LinkedList ├── Add two numbers as lists ├── Insertion Sort List ├── Intersection Of Linked Lists ├── List Cycle ├── Merge Two Sorted Lists ├── Palindrome List ├── Partition List ├── README.md ├── Remove Duplicates from Sorted List II ├── Remove Duplicates from Sorted list ├── Remove Nth Node from List End ├── Reorder List ├── Reverse A Linked List ├── Reverse Link List II ├── Rotate List ├── Swap List Nodes in Pairs └── k Reverse Linked List ├── Math ├── Excel Coloumn Title ├── Excel Column Number ├── FizzBuzz ├── Greatest Common Divisor ├── Grid Unique Paths ├── Largest Coprime Divisor ├── Palindrome Integer ├── Power of two integers ├── Prime Sum ├── Rearrange array ├── Reverse Integer ├── Sum of pairwise hamming distance └── Trailing Zeroes in Factorial └── String └── Palindrome String /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto -------------------------------------------------------------------------------- /Array/Add One To Number: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Given a non-negative number represented as an array of digits, 4 | 5 | add 1 to the number ( increment the number represented by the digits ). 6 | 7 | The digits are stored such that the most significant digit is at the head of the list. 8 | 9 | Example: 10 | 11 | If the vector has [1, 2, 3] 12 | 13 | the returned vector should be [1, 2, 4] 14 | 15 | as 123 + 1 = 124. 16 | 17 | NOTE: Certain things are intentionally left unclear in this question which you should practice asking the interviewer. 18 | For example, for this problem, following are some good questions to ask : 19 | Q : Can the input have 0’s before the most significant digit. Or in other words, is 0 1 2 3 a valid input? 20 | A : For the purpose of this question, YES 21 | Q : Can the output have 0’s before the most significant digit? Or in other words, is 0 1 2 4 a valid output? 22 | A : For the purpose of this question, NO. Even if the input has zeroes before the most significant digit. 23 | 24 | */ 25 | 26 | vector Solution::plusOne(vector &A) { 27 | // Do not write main() function. 28 | // Do not read input, instead use the arguments to the function. 29 | // Do not print the output, instead return values as specified 30 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 31 | int n=A.size(),carry=0; 32 | vectorres; 33 | res.push_back((1+A[n-1])%10); 34 | carry=(1+A[n-1])/10; 35 | for(int i=n-2;i>=0;i--){ 36 | res.push_back((carry+A[i])%10); 37 | carry=(carry+A[i])/10; 38 | } 39 | if(carry) 40 | res.push_back(carry); 41 | int x=res.size(); 42 | for(int i=0;i > Solution::diagonal(vector > &A) { 40 | int x=A.size(); 41 | vector >res(2*x-1); 42 | int count=0; 43 | for(int i=0;i=0){ 46 | res[count].push_back(A[a][y]); 47 | a++; 48 | y--; 49 | } 50 | count++; 51 | } 52 | for(int i=1;i=0){ 55 | res[count].push_back(A[a][y]); 56 | a++; 57 | y--; 58 | } 59 | count++; 60 | } 61 | return res; 62 | } 63 | -------------------------------------------------------------------------------- /Array/Find Duplicate in Array: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 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. 4 | 5 | Sample Input: 6 | 7 | [3 4 1 4 1] 8 | Sample Output: 9 | 10 | 1 11 | If there are multiple possible answers ( like in the sample case above ), output any one. 12 | 13 | If there is no duplicate, output -1 14 | 15 | */ 16 | 17 | int Solution::repeatedNumber(const vector &A) { 18 | // Do not write main() function. 19 | // Do not read input, instead use the arguments to the function. 20 | // Do not print the output, instead return values as specified 21 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 22 | 23 | int slow = A[0]; 24 | int fast = A[A[0]]; 25 | while (slow != fast) { 26 | slow = A[slow]; 27 | fast = A[A[fast]]; 28 | } 29 | 30 | fast = 0; 31 | while (slow != fast) { 32 | slow = A[slow]; 33 | fast = A[fast]; 34 | } 35 | if(slow==0) return -1; 36 | return slow; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Array/Find Missing Integer: -------------------------------------------------------------------------------- 1 | /* 2 | Given an unsorted integer array, find the first missing positive integer. 3 | 4 | Example: 5 | 6 | Given [1,2,0] return 3, 7 | 8 | [3,4,-1,1] return 2, 9 | 10 | [-8, -7, -6] returns 1 11 | 12 | Your algorithm should run in O(n) time and use constant space. 13 | */ 14 | 15 | int Solution::firstMissingPositive(vector &A) { 16 | // Do not write main() function. 17 | // Do not read input, instead use the arguments to the function. 18 | // Do not print the output, instead return values as specified 19 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 20 | int n=A.size(); 21 | int j=0; 22 | for(int i=0;i=0&&abs(A[i])+j-10) 31 | A[abs(A[i])+j-1]=-A[abs(A[i])+j-1]; 32 | } 33 | } 34 | for(int i=j;i0) 37 | return (i-j+1); 38 | } 39 | return (n-j+1); 40 | } 41 | -------------------------------------------------------------------------------- /Array/Flip: -------------------------------------------------------------------------------- 1 | vector Solution::flip(string A) { 2 | vectorc; 3 | int i=0; 4 | while(A[i]=='1'){ 5 | i++; 6 | } 7 | if(i==A.length()) return c; 8 | int n=A.length(); 9 | int b[n]; 10 | for(i=0;ic; 33 | 34 | return c; 35 | } 36 | -------------------------------------------------------------------------------- /Array/Hotel Bookings Possible: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | A hotel manager has to process N advance bookings of rooms for the next season. His hotel has K rooms. Bookings contain an arrival date and a departure date. He wants to find out whether there are enough rooms in the hotel to satisfy the demand. Write a program that solves this problem in time O(N log N) . 4 | 5 | Input: 6 | 7 | 8 | First list for arrival time of booking. 9 | Second list for departure time of booking. 10 | Third is K which denotes count of rooms. 11 | 12 | Output: 13 | 14 | A boolean which tells whether its possible to make a booking. 15 | Return 0/1 for C programs. 16 | O -> No there are not enough rooms for N booking. 17 | 1 -> Yes there are enough rooms for N booking. 18 | Example : 19 | 20 | Input : 21 | Arrivals : [1 3 5] 22 | Departures : [2 6 8] 23 | K : 1 24 | 25 | Return : False / 0 26 | 27 | At day = 5, there are 2 guests in the hotel. But I have only one room. 28 | 29 | */ 30 | 31 | bool Solution::hotel(vector &arrive, vector &depart, int k) { 32 | vector >v; 33 | int n=arrive.size(); 34 | for(int i=0;ik) 43 | return false; 44 | } 45 | return true; 46 | } 47 | -------------------------------------------------------------------------------- /Array/Intersecting Chords in a Circle: -------------------------------------------------------------------------------- 1 | int Solution::chordCnt(int n){ 2 | long long int dp[n+1]; 3 | // if(n%2!=0) return 0; 4 | if(n==0||n==1) 5 | return 1; 6 | else if(n==2) 7 | return 2; 8 | else if(n>2){ 9 | dp[0]=1; 10 | dp[1]=1; 11 | dp[2]=2; 12 | for(int i=3;i<=n;i++){ 13 | dp[i]=0; 14 | for(int k=0;k &A) { 2 | // Do not write main() function. 3 | // Do not read input, instead use the arguments to the function. 4 | // Do not print the output, instead return values as specified 5 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 6 | int i,n=A.size(); 7 | int dp[n-1]; 8 | for(i=0;i=0;i--){ 22 | if(A[i]!=0){ 23 | for(int k=i+A[i];k>=i+1;k--) 24 | dp[i]=max(dp[i],dp[k]); 25 | } 26 | } 27 | /*for(int i=0;i=n-1) return 1; 31 | else return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Array/Kth Row of Pascal's Triangle: -------------------------------------------------------------------------------- 1 | //https://www.interviewbit.com/problems/kth-row-of-pascals-triangle/ 2 | 3 | /* 4 | 5 | Given an index k, return the kth row of the Pascal’s triangle. 6 | 7 | Pascal’s triangle : To generate A[C] in row R, sum up A’[C] and A’[C-1] from previous row R - 1. 8 | 9 | Example: 10 | 11 | Input : k = 3 12 | 13 | Return : [1,3,3,1] 14 | NOTE : k is 0 based. k = 0, corresponds to the row [1]. 15 | Note:Could you optimize your algorithm to use only O(k) extra space? 16 | 17 | */ 18 | vector Solution::getRow(int k) { 19 | // Do not write main() function. 20 | // Do not read input, instead use the arguments to the function. 21 | // Do not print the output, instead return values as specified 22 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 23 | vector v(k+1); 24 | if(k==0){ 25 | v[0]=1; 26 | return v; 27 | } 28 | int c=1; 29 | v[0]=c; 30 | for(int j=1;j<=k;j++){ 31 | v[j]=c*(k-j+1)/j; 32 | c=v[j]; 33 | } 34 | return v; 35 | } 36 | -------------------------------------------------------------------------------- /Array/Largest Number: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Given a list of non negative integers, arrange them such that they form the largest number. 4 | 5 | For example: 6 | 7 | Given [3, 30, 34, 5, 9], the largest formed number is 9534330. 8 | 9 | Note: The result may be very large, so you need to return a string instead of an integer. 10 | 11 | */ 12 | 13 | int myCompare(string X, string Y) 14 | { 15 | // first append Y at the end of X 16 | string XY = X.append(Y); 17 | 18 | // then append X at the end of Y 19 | string YX = Y.append(X); 20 | 21 | // Now see which of the two formed numbers is greater 22 | return XY.compare(YX) > 0 ? 1: 0; 23 | } 24 | string Solution::largestNumber(const vector &A) { 25 | // Do not write main() function. 26 | // Do not read input, instead use the arguments to the function. 27 | // Do not print the output, instead return values as specified 28 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 29 | vector b; 30 | for(int i=0;i &A) { 16 | // Do not write main() function. 17 | // Do not read input, instead use the arguments to the function. 18 | // Do not print the output, instead return values as specified 19 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 20 | int n=A.size(); 21 | // if(n==1) return 0; 22 | vector >v; 23 | for(int i=0;i=0;i--){ 30 | ans=max(ans,rmax-v[i].second); 31 | rmax=max(rmax,v[i].second); 32 | } 33 | return ans; 34 | } 35 | -------------------------------------------------------------------------------- /Array/Max Non Negative SubArray: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Find out the maximum sub-array of non negative numbers from an array. 4 | 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. 5 | 6 | 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). 7 | 8 | Example: 9 | 10 | A : [1, 2, 5, -7, 2, 3] 11 | The two sub-arrays are [1, 2, 5] [2, 3]. 12 | The answer is [1, 2, 5] as its sum is larger than [2, 3] 13 | NOTE: If there is a tie, then compare with segment's length and return segment which has maximum length 14 | NOTE 2: If there is still a tie, then return the segment with minimum starting index 15 | 16 | */ 17 | 18 | vector Solution::maxset(vector &A) { 19 | // Do not write main() function. 20 | // Do not read input, instead use the arguments to the function. 21 | // Do not print the output, instead return values as specified 22 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 23 | int n=A.size(); 24 | int i=0,maxm=0,count=0,start=0,end=-1; 25 | int fstart=-1,fend=-1; 26 | long long int sum=0,maxsum=0; 27 | vector res; 28 | while(i=0){ 30 | sum+=A[i]; 31 | count++; 32 | end++; 33 | } 34 | if(sum>maxsum){ 35 | maxsum=sum; 36 | fstart=start; 37 | fend=end; 38 | } 39 | else if(sum==maxsum&&count>maxm){ 40 | maxm=count; 41 | fstart=start; 42 | fend=end; 43 | } 44 | if(A[i]<0){ 45 | count=0; 46 | start=i+1;end=i; 47 | sum=0; 48 | } 49 | i++; 50 | } 51 | if(fstart!=-1&&fend!=-1){ 52 | for(int i=fstart;i<=fend;i++) 53 | res.push_back(A[i]); 54 | } 55 | return res; 56 | } 57 | -------------------------------------------------------------------------------- /Array/Max Sum Contiguous Subarray: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Find the contiguous subarray within an array (containing at least one number) which has the largest sum. 4 | 5 | For example: 6 | 7 | Given the array [-2,1,-3,4,-1,2,1,-5,4], 8 | 9 | the contiguous subarray [4,-1,2,1] has the largest sum = 6. 10 | 11 | For this problem, return the maximum sum. 12 | 13 | */ 14 | 15 | int Solution::maxSubArray(const vector &A) { 16 | // Do not write main() function. 17 | // Do not read input, instead use the arguments to the function. 18 | // Do not print the output, instead return values as specified 19 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 20 | 21 | int max_till_now=A[0],max_ending_here=A[0]; 22 | for(int i=1;i &A) { 20 | int max1=INT_MIN,min1=INT_MAX,max2=INT_MIN,min2=INT_MAX; 21 | for(int i=0;i Solution::subUnsort(vector &A) { 25 | int n=A.size(); 26 | vectorres; 27 | int s=-1,e=-1; 28 | for(int i=0;iA[i+1]){ 30 | s=i; 31 | break; 32 | } 33 | } 34 | if(s==-1){ 35 | res.push_back(-1); 36 | return res; 37 | } 38 | for(int i=n-1;i>0;i--){ 39 | if(A[i]maxm) 50 | maxm=A[i]; 51 | } 52 | for(int i=0;iminm){ 54 | s=i; 55 | break; 56 | } 57 | } 58 | for(int i=n-1;i>e;i--){ 59 | if(A[i] Solution::insert(vector &A, Interval newInterval) { 34 | // Do not write main() function. 35 | // Do not read input, instead use the arguments to the function. 36 | // Do not print the output, instead return values as specified 37 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 38 | A.push_back(newInterval); 39 | sort(A.begin(),A.end(),mycomp); 40 | vector res; 41 | int n=A.size(); 42 | res.push_back(A[0]); 43 | for(int i=1;i Solution::merge(vector &A) { 26 | // Do not write main() function. 27 | // Do not read input, instead use the arguments to the function. 28 | // Do not print the output, instead return values as specified 29 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 30 | sort(A.begin(),A.end(),mycomp); 31 | vector res; 32 | int n=A.size(); 33 | res.push_back(A[0]); 34 | for(int i=1;i &x, vector &y) { 26 | int n=x.size(),ans=0; 27 | for(int i=0;i &A) { 10 | int n=A.size(); 11 | sort(A.begin(),A.end()); 12 | for(int i=0;i > Solution::generate(int A) { 27 | // Do not write main() function. 28 | // Do not read input, instead use the arguments to the function. 29 | // Do not print the output, instead return values as specified 30 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 31 | vector >res(A); 32 | for(int i=0;i > Solution::generate(int A) { 49 | // Do not write main() function. 50 | // Do not read input, instead use the arguments to the function. 51 | // Do not print the output, instead return values as specified 52 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 53 | vector >v(A); 54 | int c=1; 55 | for(int i=0;i &A) { 17 | int n=A.size(); 18 | int e1=INT_MAX,e2=INT_MAX; 19 | int count1=0,count2=0; 20 | for(int i=0;i0 && A[i]==e1){ 22 | count1+=1; 23 | } 24 | else if(count2>0 && A[i]==e2){ 25 | count2+=1; 26 | } 27 | else if(count1==0){ 28 | e1=A[i]; 29 | count1=1; 30 | } 31 | else if(count2==0){ 32 | e2=A[i]; 33 | count2=1; 34 | } 35 | else{ 36 | count1--; 37 | count2--; 38 | } 39 | } 40 | if(count1==0 && count2==0) 41 | return -1; 42 | int freq=0; 43 | if(count1){ 44 | for(int i=0;in/3) 49 | return e1; 50 | } 51 | freq=0; 52 | if(count2){ 53 | for(int i=0;in/3) 58 | return e2; 59 | } 60 | return -1; 61 | } 62 | -------------------------------------------------------------------------------- /Array/Repeat and missing number array: -------------------------------------------------------------------------------- 1 | /* 2 | You are given a read only array of n integers from 1 to n. 3 | 4 | Each integer appears exactly once except A which appears twice and B which is missing. 5 | 6 | Return A and B. 7 | 8 | Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 9 | 10 | Note that in your output A should precede B. 11 | 12 | Example: 13 | 14 | Input:[3 1 2 5 3] 15 | 16 | Output:[3, 4] 17 | 18 | A = 3, B = 4 19 | */ 20 | 21 | //First approach: using xor 22 | 23 | vector Solution::repeatedNumber(const vector &A) { 24 | // Do not write main() function. 25 | // Do not read input, instead use the arguments to the function. 26 | // Do not print the output, instead return values as specified 27 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 28 | int n=A.size(); 29 | int xo=0; 30 | for(int i=0;i res(2); 46 | for(int i=0;i,n;i++){ 47 | if(A[i]==x){ 48 | res[0]=x; 49 | res[1]=y; 50 | break; 51 | } 52 | else if(A[i]==y){ 53 | res[0]=y; 54 | res[1]=x; 55 | break; 56 | } 57 | } 58 | return res; 59 | } 60 | 61 | 62 | 63 | //Second approach: using sum of squares 64 | 65 | vector Solution::repeatedNumber(const vector &A) { 66 | // Do not write main() function. 67 | // Do not read input, instead use the arguments to the function. 68 | // Do not print the output, instead return values as specified 69 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 70 | long long int len = A.size(); 71 | long long int sumOfN = (len * (len+1) ) /2, sumOfNsq = (len * (len +1) *(2*len +1) )/6; 72 | long long int missingNumber1=0, missingNumber2=0; 73 | 74 | for(int i=0;i ans; 82 | ans.push_back(missingNumber2); 83 | ans.push_back(missingNumber1); 84 | return ans; 85 | } 86 | -------------------------------------------------------------------------------- /Array/Rotate Matrix: -------------------------------------------------------------------------------- 1 | //https://www.interviewbit.com/problems/rotate-matrix/ 2 | /* 3 | You are given an n x n 2D matrix representing an image. 4 | 5 | Rotate the image by 90 degrees (clockwise). 6 | 7 | You need to do this in place. 8 | 9 | Note that if you end up using an additional array, you will only receive partial score. 10 | 11 | Example: 12 | 13 | If the array is 14 | 15 | [ 16 | [1, 2], 17 | [3, 4] 18 | ] 19 | Then the rotated array becomes: 20 | 21 | [ 22 | [3, 1], 23 | [4, 2] 24 | ] 25 | */ 26 | 27 | void Solution::rotate(vector > &A) { 28 | int n=A.size(); 29 | int k=n*n/4; 30 | int i=0,j=0,a; 31 | while(k){ 32 | int y=A[i][j],a=4; 33 | int p=i,q=j; 34 | while(a){ 35 | int x=A[q][n-p-1]; 36 | A[q][n-p-1]=y; 37 | y=x; 38 | a--; 39 | int t=p; 40 | p=q; 41 | q=n-t-1; 42 | } 43 | if(j > &A) { 24 | // Do not write main() function. 25 | // Do not read input, instead use the arguments to the function. 26 | // Do not print the output, instead return values as specified 27 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 28 | int m=A.size(),n=A[0].size(); 29 | int r1=0,c1=0; 30 | for(int i=0;i Solution::spiralOrder(const vector > &A) { 21 | vector res; 22 | int t=0,b=A.size()-1,l=0,r=A[0].size()-1; 23 | int flag=1; 24 | int n=A.size()*A[0].size(); 25 | while(t<=b&&l<=r){ 26 | if(flag==1){ 27 | for(int j=l;j<=r;j++) 28 | res.push_back(A[t][j]); 29 | t++; 30 | flag=2; 31 | } 32 | else if(flag==2){ 33 | for(int j=t;j<=b;j++) 34 | res.push_back(A[j][r]); 35 | r--; 36 | flag=3; 37 | } 38 | else if(flag==3){ 39 | for(int j=r;j>=l;j--) 40 | res.push_back(A[b][j]); 41 | b--; 42 | flag=4; 43 | } 44 | else if(flag==4){ 45 | for(int j=b;j>=t;j--) 46 | res.push_back(A[j][l]); 47 | l++; 48 | flag=1; 49 | } 50 | /*for(int i=0;i > Solution::generateMatrix(int n) { 15 | // Do not write main() function. 16 | // Do not read input, instead use the arguments to the function. 17 | // Do not print the output, instead return values as specified 18 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 19 | int t=0,l=0,r=n-1,b=n-1; 20 | int d=0,count=1; 21 | vector >a(n,vector(n)); 22 | while(t<=b&&l<=r){ 23 | if(d==0){ 24 | for(int i=l;i<=r;i++){ 25 | a[t][i]=count; 26 | count++; 27 | } 28 | d=1; 29 | t++; 30 | } 31 | else if(d==1){ 32 | for(int i=t;i<=b;i++){ 33 | a[i][r]=count; 34 | count++; 35 | } 36 | d=2; 37 | r--; 38 | } 39 | else if(d==2){ 40 | for(int i=r;i>=l;i--){ 41 | a[b][i]=count; 42 | count++; 43 | } 44 | d=3; 45 | b--; 46 | } 47 | else if(d==3){ 48 | for(int i=b;i>=t;i--){ 49 | a[i][l]=count; 50 | count++; 51 | } 52 | d=0; 53 | l++; 54 | } 55 | } 56 | return a; 57 | } 58 | -------------------------------------------------------------------------------- /Array/Stairs: -------------------------------------------------------------------------------- 1 | int Solution::climbStairs(int n) { 2 | // Do not write main() function. 3 | // Do not read input, instead use the arguments to the function. 4 | // Do not print the output, instead return values as specified 5 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 6 | int dp[n]; 7 | if(n==0) return 1; 8 | dp[0]=1; 9 | dp[1]=2; 10 | for(int i=2;i= a2 <= a3 >= a4 <= a5..... 4 | 5 | Example 6 | 7 | Given [1, 2, 3, 4] 8 | 9 | One possible answer : [2, 1, 4, 3] 10 | Another possible answer : [4, 1, 3, 2] 11 | NOTE : If there are multiple answers possible, return the one thats lexicographically smallest. 12 | So, in example case, you will return [2, 1, 13 | */ 14 | 15 | //using sorting, O(nlogn) complexity 16 | 17 | vector Solution::wave(vector &A) { 18 | // Do not write main() function. 19 | // Do not read input, instead use the arguments to the function. 20 | // Do not print the output, instead return values as specified 21 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 22 | int n=A.size(); 23 | sort(A.begin(),A.end()); 24 | for(int i=1;ia; 8 | for(int i=0;i2||A[i-1]-'0'<1) 12 | return 0; 13 | else 14 | a[i-1]=(A[i-1]-'0')*10+(A[i]-'0'); 15 | } 16 | } 17 | n=a.size(); 18 | /* for(int i=0;i &A,long long int X,int B){ 33 | int n=A.size(); 34 | long long int sum=X; 35 | int stdcnt=1,i=0; 36 | while(iB) return false; 47 | } 48 | return true; 49 | } 50 | 51 | int Solution::books(vector &A, int B) { 52 | // Do not write main() function. 53 | // Do not read input, instead use the arguments to the function. 54 | // Do not print the output, instead return values as specified 55 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 56 | int n=A.size(); 57 | if(n &A, int B) { 12 | // Do not write main() function. 13 | // Do not read input, instead use the arguments to the function. 14 | // Do not print the output, instead return values as specified 15 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 16 | int n=A.size(); 17 | int low=0,high=n-1; 18 | int mid=low+(high-low)/2; 19 | int first=-1,last=-1; 20 | while(low<=high){ 21 | mid=low+(high-low)/2; 22 | if(A[mid]==B){ 23 | first=mid; 24 | high=mid-1; 25 | } 26 | else if(A[mid]0){ 26 | if(expo&1) 27 | ans=((ans)*y)%m; 28 | y=(y*y)%m; 29 | if(y<0) y+=m; 30 | expo=expo>>1; 31 | } 32 | if(ans<0){ 33 | ans=(m-abs(ans)%m); 34 | return ans; 35 | } 36 | return ans%m; 37 | } 38 | -------------------------------------------------------------------------------- /Binary Search/Matrix Median: -------------------------------------------------------------------------------- 1 | /* 2 | Given a N cross M matrix in which each row is sorted, find the overall median of the matrix. Assume N*M is odd. 3 | 4 | For example, 5 | 6 | Matrix= 7 | [1, 3, 5] 8 | [2, 6, 9] 9 | [3, 6, 9] 10 | 11 | A = [1, 2, 3, 3, 5, 6, 6, 9, 9] 12 | 13 | Median is 5. So, we return 5. 14 | Note: No extra memory is allowed. 15 | */ 16 | 17 | int Solution::findMedian(vector > &A) { 18 | int r=A.size(),c=A[0].size(); 19 | int req=(r*c+1)/2; 20 | int minm=INT_MAX,maxm=INT_MIN; 21 | for(int i=0;i > &A, int x) { 24 | // Do not write main() function. 25 | // Do not read input, instead use the arguments to the function. 26 | // Do not print the output, instead return values as specified 27 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 28 | int r=A.size(),c=A[0].size(); 29 | int row=-1; 30 | for(int i=0;i=A[i][0]&&x<=A[i][c-1]){ 32 | row=i; 33 | break; 34 | } 35 | } 36 | if(row==-1) return 0; 37 | int low=0,high=c-1; 38 | while(low<=high){ 39 | int mid=low+(high-low)/2; 40 | if(A[row][mid]==x) return 1; 41 | if(A[row][mid] &C,long long int X){ 28 | int n=C.size(); 29 | long long int t=X; 30 | int i=0,cnt=1; 31 | while(iA) 33 | return false; 34 | if(C[i]>t){ 35 | cnt++; 36 | t=X; 37 | } 38 | else{ 39 | t=t-C[i]; 40 | i++; 41 | } 42 | } 43 | return true; 44 | } 45 | 46 | int Solution::paint(int A, int B, vector &C) { 47 | // Do not write main() function. 48 | // Do not read input, instead use the arguments to the function. 49 | // Do not print the output, instead return values as specified 50 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 51 | int n=C.size(); 52 | long long int sum=0; 53 | for(int i=0;i &A) { 13 | // Do not write main() function. 14 | // Do not read input, instead use the arguments to the function. 15 | // Do not print the output, instead return values as specified 16 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 17 | int n=A.size(); 18 | int low=0,high=n-1; 19 | int mid=low+(high-low)/2; 20 | while(low<=high){ 21 | mid=low+(high-low)/2; 22 | if((mid>0&&A[mid-1]>A[mid])) 23 | return A[mid]; 24 | if(midA[mid+1]) 25 | return A[mid+1]; 26 | if(A[mid] Solution::searchRange(const vector &A, int B) { 18 | // Do not write main() function. 19 | // Do not read input, instead use the arguments to the function. 20 | // Do not print the output, instead return values as specified 21 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 22 | int n=A.size(); 23 | int low=0,high=n-1; 24 | int mid=low+(high-low)/2; 25 | vectorv(2); 26 | v[0]=-1;v[1]=-1; 27 | while(low<=high){ 28 | mid=low+(high-low)/2; 29 | if(A[mid]==B){ 30 | v[0]=mid; 31 | high=mid-1; 32 | } 33 | else if(A[mid] &A, int x) { 16 | // Do not write main() function. 17 | // Do not read input, instead use the arguments to the function. 18 | // Do not print the output, instead return values as specified 19 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 20 | int n=A.size(); 21 | int low=0,high=n-1; 22 | int mid=low+(high-low)/2; 23 | while(low<=high){ 24 | mid=low+(high-low)/2; 25 | if(A[mid]==x||(mid==0&&x0&&xA[mid-1])) 26 | return mid; 27 | if(x>A[mid]) low=mid+1; 28 | else high=mid-1; 29 | } 30 | return n; 31 | } 32 | -------------------------------------------------------------------------------- /Binary Search/Square Root Of Integer: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Implement int sqrt(int x). 4 | 5 | Compute and return the square root of x. 6 | 7 | If x is not a perfect square, return floor(sqrt(x)) 8 | 9 | Example : 10 | 11 | Input : 11 12 | Output : 3 13 | 14 | */ 15 | 16 | #define ll long long int 17 | int Solution::sqrt(int A) { 18 | // Do not write main() function. 19 | // Do not read input, instead use the arguments to the function. 20 | // Do not print the output, instead return values as specified 21 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 22 | if(A==1) return 1; 23 | ll low=0,high=A/2; 24 | ll mid=low+(high-low)/2; 25 | while(low<=high){ 26 | mid=low+(high-low)/2; 27 | if(mid*mid<=A&&(mid+1)*(mid+1)>A) 28 | return mid; 29 | else if(mid*mid>A) high=mid-1; 30 | else low=mid+1; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /DynamicProgramming/Intersecting Chords in a Circle: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Given a number N, return number of ways you can draw N chords in a circle with 2*N points such that no 2 chords intersect. 4 | Two ways are different if there exists a chord which is present in one way and not in other. 5 | 6 | For example, 7 | 8 | N=2 9 | If points are numbered 1 to 4 in clockwise direction, then different ways to draw chords are: 10 | {(1-2), (3-4)} and {(1-4), (2-3)} 11 | 12 | So, we return 2. 13 | Notes: 14 | 15 | 1 ≤ N ≤ 1000 16 | Return answer modulo 109+7. 17 | 18 | */ 19 | 20 | //The pattern gives Catalan numbers 1,1,2,5,14... 21 | //So the solution is to find nth catalan number. 22 | 23 | int Solution::chordCnt(int n){ 24 | long long int dp[n+1]; 25 | // if(n%2!=0) return 0; 26 | if(n==0||n==1) 27 | return 1; 28 | else if(n==2) 29 | return 2; 30 | else if(n>2){ 31 | dp[0]=1; 32 | dp[1]=1; 33 | dp[2]=2; 34 | for(int i=3;i<=n;i++){ 35 | dp[i]=0; 36 | for(int k=0;k &A) { 17 | // Do not write main() function. 18 | // Do not read input, instead use the arguments to the function. 19 | // Do not print the output, instead return values as specified 20 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 21 | int n=A.size(); 22 | int dp[n]; 23 | dp[n-1]=0; 24 | for(int i=n-2;i>=0;i--){ 25 | if(i+A[i]>n-1) { 26 | dp[i]=n-1; 27 | continue; 28 | } 29 | dp[i]=i+A[i]; 30 | for(int j=i+1;j<=i+A[i];j++) 31 | dp[i]=max(dp[i],dp[j]); 32 | } 33 | if(dp[0]==n-1) 34 | return 1; 35 | else return 0; 36 | } 37 | -------------------------------------------------------------------------------- /DynamicProgramming/Largest area of rectangle with permutations: -------------------------------------------------------------------------------- 1 | /* 2 | Given a binary grid i.e. a 2D grid only consisting of 0’s and 1’s, find the area of the largest rectangle inside the grid 3 | such that all the cells inside the chosen rectangle should have 1 in them. You are allowed to permutate the columns matrix i.e. 4 | you can arrange each of the column in any order in the final grid. Please follow the below example for more clarity. 5 | 6 | Lets say we are given a binary grid of 3 * 3 size. 7 | 1 0 1 8 | 0 1 0 9 | 1 0 0 10 | 11 | At present we can see that max rectangle satisfying the criteria mentioned in the problem is of 1 * 1 = 1 area i.e either of 12 | the 4 cells which contain 1 in it. Now since we are allowed to permutate the columns of the given matrix, we can take column 1 13 | and column 3 and make them neighbours. One of the possible configuration of the grid can be: 14 | 1 1 0 15 | 0 0 1 16 | 1 0 0 17 | 18 | Now In this grid, first column is column 1, second column is column 3 and third column is column 2 from the original given grid. 19 | Now, we can see that if we calculate the max area rectangle, we get max area as 1 * 2 = 2 which is bigger than the earlier case. 20 | Hence 2 will be the answer in this case. 21 | */ 22 | 23 | int Solution::solve(vector > &A) { 24 | int n=A.size(),m=A[0].size(); 25 | int count[n][m]; 26 | for(int i=0;i &A) { 15 | int n=A.size(); 16 | int lis[n]; 17 | int lds[n]; 18 | for(int i=0;iA[j]&&lis[i]=0;i--){ 29 | for(int j=n-1;j>i;j--){ 30 | if(A[i]>A[j]&&lds[i] &a) { 19 | int n=a.size(); 20 | if(n<=2) return n; 21 | int A[n]; 22 | for(int i=0;i0;j--){ 32 | int i=j-1,k=j+1; 33 | while(i>=0&&k > &A) { 20 | // Do not write main() function. 21 | // Do not read input, instead use the arguments to the function. 22 | // Do not print the output, instead return values as specified 23 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 24 | int n=A[0].size(); 25 | int dp[2][n]; 26 | if(n==0) return 0; 27 | if(n==1) return max(A[0][0],A[1][0]); 28 | int ans=INT_MIN; 29 | for(int i=0;i<2;i++){ 30 | dp[i][n-1]=A[i][n-1]; 31 | ans=max(ans,dp[i][n-1]); 32 | } 33 | dp[0][n-2]=A[0][n-2]; 34 | dp[1][n-2]=A[1][n-2]; 35 | ans=max(ans,max(dp[0][n-2],dp[1][n-2])); 36 | 37 | int i=1; 38 | for(int j=n-3;j>=0;j--){ 39 | int maxfromright=INT_MIN; 40 | for(int k=j+2;k &A) { 18 | // Do not write main() function. 19 | // Do not read input, instead use the arguments to the function. 20 | // Do not print the output, instead return values as specified 21 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 22 | int n=A.size(); 23 | if(n==1) return 0; 24 | int max_reach=A[0],step=A[0],jump=1; 25 | for(int i=1;i=n-1) return jump; 35 | return -1; 36 | } 37 | -------------------------------------------------------------------------------- /DynamicProgramming/N digit numbers with digit sum S: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Find out the number of N digit numbers, whose digits on being added equals to a given number S. Note that a 4 | valid number starts from digits 1-9 except the number 0 itself. i.e. leading zeroes are not allowed. 5 | 6 | Since the answer can be large, output answer modulo 1000000007 7 | 8 | **** 9 | 10 | N = 2, S = 4 11 | Valid numbers are {22, 31, 13, 40} 12 | Hence output 4. 13 | */ 14 | 15 | int Solution::solve(int n, int s) { 16 | long long int dp[n][s]; 17 | if(s<1||s>9*n) return 0; 18 | 19 | for(int i=0;i=9) k=j-9; 31 | for(;k<=j;k++) 32 | dp[i][j]=(dp[i][j]+dp[i-1][k])%1000000007; 33 | 34 | } 35 | } 36 | return dp[n-1][s-1]; 37 | } 38 | -------------------------------------------------------------------------------- /DynamicProgramming/Stairs: -------------------------------------------------------------------------------- 1 | /* 2 | You are climbing a stair case. It takes n steps to reach to the top. 3 | 4 | Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 5 | 6 | Example : 7 | 8 | Input : 3 9 | Return : 3 10 | 11 | Steps : [1 1 1], [1 2], [2 1] 12 | */ 13 | 14 | int Solution::climbStairs(int n) { 15 | // Do not write main() function. 16 | // Do not read input, instead use the arguments to the function. 17 | // Do not print the output, instead return values as specified 18 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 19 | int dp[n]; 20 | if(n==0) return 1; 21 | dp[0]=1; 22 | dp[1]=2; 23 | for(int i=2;i 1 6 | 'B' -> 2 7 | ... 8 | 'Z' -> 26 9 | Given an encoded message containing digits, determine the total number of ways to decode it. 10 | 11 | Example : 12 | 13 | Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12). 14 | 15 | The number of ways decoding "12" is 2. 16 | 17 | */ 18 | 19 | int Solution::numDecodings(string A) { 20 | // Do not write main() function. 21 | // Do not read input, instead use the arguments to the function. 22 | // Do not print the output, instead return values as specified 23 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 24 | int n=A.length(); 25 | vectorv; 26 | for(int i=0;i0&&x<27) 37 | dp[i]=dp[i-1]; 38 | else 39 | return 0; 40 | } 41 | else if(x>0&&x<27&&x!=v[i]) 42 | dp[i]=dp[i-1]+dp[i-2]; 43 | else 44 | dp[i]=dp[i-1]; 45 | } 46 | return dp[n-1]; 47 | } 48 | -------------------------------------------------------------------------------- /Greedy/Assign mice to holes: -------------------------------------------------------------------------------- 1 | int Solution::mice(vector &A, vector &B) { 2 | // Do not write main() function. 3 | // Do not read input, instead use the arguments to the function. 4 | // Do not print the output, instead return values as specified 5 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 6 | sort(A.begin(),A.end()); 7 | sort(B.begin(),B.end()); 8 | int n=A.size(); 9 | int ans=abs(A[0]-B[0]); 10 | for(int i=1;i &A) { 2 | int ans=0,n=A.size(); 3 | if(n==0) return 0; 4 | for(int i=0;i &ratings) { 2 | // Do not write main() function. 3 | // Do not read input, instead use the arguments to the function. 4 | // Do not print the output, instead return values as specified 5 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 6 | int n=ratings.size(); 7 | vectorcandies(n); 8 | for(int i=0;iratings[i-1]&&candies[i]<=candies[i-1]) 15 | candies[i]=candies[i-1]+1; 16 | else{ 17 | int j=i-1; 18 | while(j>=0&&ratings[j]>ratings[j+1]&candies[j]<=candies[j+1]){ 19 | candies[j]=candies[j+1]+1; 20 | j--; 21 | } 22 | } 23 | } 24 | int ans=candies[0]; 25 | for(int i=1;i &gas, const vector &cost) { 2 | // Do not write main() function. 3 | // Do not read input, instead use the arguments to the function. 4 | // Do not print the output, instead return values as specified 5 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 6 | int n=gas.size(); 7 | if(n==1) return 0; 8 | for(int i=0;i &A) { 2 | // Do not write main() function. 3 | // Do not read input, instead use the arguments to the function. 4 | // Do not print the output, instead return values as specified 5 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 6 | int n=A.size(); 7 | sort(A.begin(),A.end()); 8 | int ans=max(A[0]*A[1]*A[n-1],A[n-3]*A[n-2]*A[n-1]); 9 | return ans; 10 | } 11 | -------------------------------------------------------------------------------- /Greedy/Majority Element: -------------------------------------------------------------------------------- 1 | int Solution::majorityElement(const vector &B) { 2 | // Do not write main() function. 3 | // Do not read input, instead use the arguments to the function. 4 | // Do not print the output, instead return values as specified 5 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 6 | int n=B.size(); 7 | vectorA(n); 8 | for(int i=0;i=n/2) 22 | return ans; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Greedy/seats: -------------------------------------------------------------------------------- 1 | int Solution::seats(string A) { 2 | // Do not write main() function. 3 | // Do not read input, instead use the arguments to the function. 4 | // Do not print the output, instead return values as specified 5 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 6 | vectorpos; 7 | for(int i=0;i=0;i--){ 15 | ans=(ans+pos[mid]-k-pos[i])%10000003; 16 | k++; 17 | } 18 | k=1; 19 | for(int i=mid+1;i 4 -> 3) + (5 -> 6 -> 4) 5 | Output: 7 -> 0 -> 8 6 | 7 | 342 + 465 = 807 8 | Make sure there are no trailing zeros in the output list 9 | So, 7 -> 0 -> 8 -> 0 is not a valid response even though the value is still 807. 10 | */ 11 | 12 | /** 13 | * Definition for singly-linked list. 14 | * struct ListNode { 15 | * int val; 16 | * ListNode *next; 17 | * ListNode(int x) : val(x), next(NULL) {} 18 | * }; 19 | */ 20 | ListNode* Solution::addTwoNumbers(ListNode* A, ListNode* B) { 21 | // Do not write main() function. 22 | // Do not read input, instead use the arguments to the function. 23 | // Do not print the output, instead return values as specified 24 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 25 | ListNode *res=new ListNode(0); 26 | int carry=0; 27 | int x=A->val+B->val+carry; 28 | res->val=x%10; 29 | carry=x/10; 30 | ListNode *temp=res; 31 | A=A->next; 32 | B=B->next; 33 | while(A!=NULL||B!=NULL){ 34 | if(A==NULL) x=B->val+carry; 35 | else if(B==NULL) x=A->val+carry; 36 | else x=A->val+B->val+carry; 37 | 38 | temp->next=new ListNode(x%10); 39 | carry=x/10; 40 | temp=temp->next; 41 | A=A->next; 42 | B=B->next; 43 | } 44 | if(carry){ 45 | temp->next=new ListNode(carry); 46 | } 47 | return res; 48 | } 49 | -------------------------------------------------------------------------------- /LinkedList/Insertion Sort List: -------------------------------------------------------------------------------- 1 | /* 2 | Sort a linked list using insertion sort. 3 | 4 | We have explained Insertion Sort at Slide 7 of Arrays 5 | 6 | Insertion Sort Wiki has some details on Insertion Sort as well. 7 | 8 | Example : 9 | 10 | Input : 1 -> 3 -> 2 11 | 12 | Return 1 -> 2 -> 3 13 | */ 14 | 15 | /** 16 | * Definition for singly-linked list. 17 | * struct ListNode { 18 | * int val; 19 | * ListNode *next; 20 | * ListNode(int x) : val(x), next(NULL) {} 21 | * }; 22 | */ 23 | ListNode* Insert(ListNode* A,ListNode *x){ 24 | if(A==NULL){ 25 | A=x; 26 | return A; 27 | } 28 | if(x->valval){ 29 | x->next=A; 30 | A=x; 31 | return A; 32 | } 33 | ListNode* temp=A; 34 | while(temp!=NULL&&temp->next!=NULL&&temp->next->valval) 35 | temp=temp->next; 36 | 37 | ListNode* next=NULL; 38 | if(temp!=NULL) next=temp->next; 39 | temp->next=x; 40 | x->next=next; 41 | 42 | return A; 43 | } 44 | ListNode* Solution::insertionSortList(ListNode* A) { 45 | // Do not write main() function. 46 | // Do not read input, instead use the arguments to the function. 47 | // Do not print the output, instead return values as specified 48 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 49 | ListNode* B=NULL,*x=A,*save=A->next; 50 | while(x!=NULL){ 51 | x->next=NULL; 52 | B=Insert(B,x); 53 | 54 | x=save; 55 | if(x!=NULL) save=x->next; 56 | } 57 | return B; 58 | } 59 | -------------------------------------------------------------------------------- /LinkedList/Intersection Of Linked Lists: -------------------------------------------------------------------------------- 1 | /* 2 | Problem Statement: Write a program to find the node at which the intersection of two singly linked lists begins. 3 | 4 | For example, the following two linked lists: 5 | 6 | 7 | A: a1 → a2 8 | ↘ 9 | c1 → c2 → c3 10 | ↗ 11 | B: b1 → b2 → b3 12 | 13 | begin to intersect at node c1. 14 | */ 15 | 16 | /** 17 | * Definition for singly-linked list. 18 | * struct ListNode { 19 | * int val; 20 | * ListNode *next; 21 | * ListNode(int x) : val(x), next(NULL) {} 22 | * }; 23 | */ 24 | int findLength(ListNode *temp){ 25 | int l=0; 26 | while(temp!=NULL){ 27 | l++; 28 | temp=temp->next; 29 | } 30 | return l; 31 | } 32 | ListNode* findMerge(ListNode *h1,ListNode *h2,int l1,int l2){ 33 | ListNode* t1=h1; 34 | ListNode *t2=h2; 35 | for(int i=0;inext; 37 | //cout<val<next; 42 | t2=t2->next; 43 | } 44 | } 45 | ListNode* Solution::getIntersectionNode(ListNode* A, ListNode* B) { 46 | // Do not write main() function. 47 | // Do not read input, instead use the arguments to the function. 48 | // Do not print the output, instead return values as specified 49 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 50 | int l1=findLength(A); 51 | int l2=findLength(B); 52 | //cout<l2){ 55 | ans=findMerge(A,B,l1,l2); 56 | } 57 | else 58 | ans=findMerge(B,A,l2,l1); 59 | return ans; 60 | } 61 | -------------------------------------------------------------------------------- /LinkedList/List Cycle: -------------------------------------------------------------------------------- 1 | /* 2 | Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 3 | 4 | Try solving it using constant additional space. 5 | 6 | Example : 7 | 8 | Input : 9 | 10 | ______ 11 | | | 12 | \/ | 13 | 1 -> 2 -> 3 -> 4 14 | 15 | Return the node corresponding to node 3. 16 | */ 17 | 18 | /** 19 | * Definition for singly-linked list. 20 | * struct ListNode { 21 | * int val; 22 | * ListNode *next; 23 | * ListNode(int x) : val(x), next(NULL) {} 24 | * }; 25 | */ 26 | ListNode* Solution::detectCycle(ListNode* A) { 27 | // Do not write main() function. 28 | // Do not read input, instead use the arguments to the function. 29 | // Do not print the output, instead return values as specified 30 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 31 | ListNode* slow=A,*fast=A; 32 | while(slow!=NULL&&fast!=NULL){ 33 | slow=slow->next; 34 | 35 | if(slow==NULL||fast->next==NULL) 36 | return NULL; 37 | 38 | fast=fast->next->next; 39 | 40 | if(slow==fast) 41 | break; 42 | } 43 | if(slow==NULL||fast==NULL) return NULL; 44 | // cout<val<<" "<val<next; 46 | int count=1; 47 | while(temp!=slow){ 48 | count++; 49 | temp=temp->next; 50 | } 51 | temp=A; 52 | for(int i=0;inext; 54 | 55 | slow=A; 56 | fast=temp; 57 | while(slow!=fast){ 58 | slow=slow->next; 59 | fast=fast->next; 60 | } 61 | return slow; 62 | } 63 | -------------------------------------------------------------------------------- /LinkedList/Merge Two Sorted Lists: -------------------------------------------------------------------------------- 1 | /* 2 | Merge two sorted linked lists and return it as a new list. 3 | The new list should be made by splicing together the nodes of the first two lists, and should also be sorted. 4 | 5 | For example, given following linked lists : 6 | 7 | 5 -> 8 -> 20 8 | 4 -> 11 -> 15 9 | The merged list should be : 10 | 11 | 4 -> 5 -> 8 -> 11 -> 15 -> 20 12 | */ 13 | 14 | /** 15 | * Definition for singly-linked list. 16 | * struct ListNode { 17 | * int val; 18 | * ListNode *next; 19 | * ListNode(int x) : val(x), next(NULL) {} 20 | * }; 21 | */ 22 | /*void print(ListNode* h){ 23 | while(h!=NULL){ 24 | cout<val<<" "; 25 | h=h->next; 26 | } 27 | cout<val<=c2->val){ 39 | res=c1; 40 | c1=c1->next; 41 | res->next=NULL; 42 | } 43 | else{ 44 | res=c2; 45 | c2=c2->next; 46 | res->next=NULL; 47 | } 48 | // print(res); 49 | ListNode *temp=res; 50 | while(c1!=NULL&&c2!=NULL){ 51 | // cout<val<<" "<val<val<=c2->val){ 53 | temp->next=c1; 54 | c1=c1->next; 55 | temp=temp->next; 56 | temp->next=NULL; 57 | } 58 | else{ 59 | temp->next=c2; 60 | c2=c2->next; 61 | temp=temp->next; 62 | temp->next=NULL; 63 | } 64 | // print(res); 65 | } 66 | if(c1==NULL&&c2!=NULL){ 67 | temp->next=c2; 68 | return res; 69 | } 70 | else if(c1!=NULL&&c2==NULL){ 71 | temp->next=c1; 72 | return res; 73 | } 74 | else 75 | return res; 76 | } 77 | -------------------------------------------------------------------------------- /LinkedList/Palindrome List: -------------------------------------------------------------------------------- 1 | /* 2 | Given a singly linked list, determine if its a palindrome. Return 1 or 0 denoting if its a palindrome or not, respectively. 3 | 4 | Notes: 5 | - Expected solution is linear in time and constant in space. 6 | 7 | For example, 8 | 9 | List 1-->2-->1 is a palindrome. 10 | List 1-->2-->3 is not a palindrome. 11 | */ 12 | 13 | /** 14 | * Definition for singly-linked list. 15 | * struct ListNode { 16 | * int val; 17 | * ListNode *next; 18 | * ListNode(int x) : val(x), next(NULL) {} 19 | * }; 20 | */ 21 | int findLength(ListNode *temp){ 22 | int l=0; 23 | while(temp!=NULL){ 24 | l++; 25 | temp=temp->next; 26 | } 27 | return l; 28 | } 29 | 30 | int Solution::lPalin(ListNode* A) { 31 | int len=findLength(A); 32 | ListNode *mid=A; 33 | int half; 34 | if(len&1){ 35 | half=len/2+1; 36 | } 37 | else half=len/2; 38 | ListNode *prev=NULL; 39 | for(int i=0;inext; 42 | } 43 | prev->next=NULL; 44 | 45 | prev=NULL; 46 | ListNode *curr=mid; 47 | ListNode *next=NULL; 48 | while(curr!=NULL){ 49 | next=curr->next; 50 | curr->next=prev; 51 | prev=curr; 52 | curr=next; 53 | } 54 | 55 | ListNode* t1=A; 56 | ListNode*t2=prev; 57 | while(t1!=NULL&&t2!=NULL){ 58 | if(t1->val!=t2->val) 59 | return 0; 60 | t1=t1->next; 61 | t2=t2->next; 62 | } 63 | return 1; 64 | } 65 | -------------------------------------------------------------------------------- /LinkedList/Partition List: -------------------------------------------------------------------------------- 1 | /* 2 | Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. 3 | 4 | You should preserve the original relative order of the nodes in each of the two partitions. 5 | 6 | For example, 7 | Given 1->4->3->2->5->2 and x = 3, 8 | return 1->2->2->4->3->5. 9 | */ 10 | 11 | /** 12 | * Definition for singly-linked list. 13 | * struct ListNode { 14 | * int val; 15 | * ListNode *next; 16 | * ListNode(int x) : val(x), next(NULL) {} 17 | * }; 18 | */ 19 | ListNode* Solution::partition(ListNode* A, int B) { 20 | // Do not write main() function. 21 | // Do not read input, instead use the arguments to the function. 22 | // Do not print the output, instead return values as specified 23 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 24 | ListNode* c1=NULL,*c2=NULL; 25 | ListNode* l1=c1,*l2=c2; 26 | ListNode* temp=A; 27 | while(temp!=NULL){ 28 | if(temp->valval); 31 | l1=c1; 32 | } 33 | else{ 34 | l1->next=new ListNode(temp->val); 35 | l1=l1->next; 36 | } 37 | } 38 | else{ 39 | if(c2==NULL){ 40 | c2=new ListNode(temp->val); 41 | l2=c2; 42 | } 43 | else{ 44 | l2->next=new ListNode(temp->val); 45 | l2=l2->next; 46 | } 47 | } 48 | ListNode* x=temp; 49 | temp=temp->next; 50 | free(x); 51 | A=temp; 52 | } 53 | if(c1==NULL) return c2; 54 | l1->next=c2; 55 | return c1; 56 | } 57 | -------------------------------------------------------------------------------- /LinkedList/README.md: -------------------------------------------------------------------------------- 1 | # InterviewBit-LinkedList 2 | Solutions of Linked List problems on InterviewBit 3 | -------------------------------------------------------------------------------- /LinkedList/Remove Duplicates from Sorted List II: -------------------------------------------------------------------------------- 1 | /* 2 | Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. 3 | 4 | For example, 5 | Given 1->2->3->3->4->4->5, return 1->2->5. 6 | Given 1->1->1->2->3, return 2->3. 7 | */ 8 | 9 | /** 10 | * Definition for singly-linked list. 11 | * struct ListNode { 12 | * int val; 13 | * ListNode *next; 14 | * ListNode(int x) : val(x), next(NULL) {} 15 | * }; 16 | */ 17 | ListNode* Solution::deleteDuplicates(ListNode* A) { 18 | // Do not write main() function. 19 | // Do not read input, instead use the arguments to the function. 20 | // Do not print the output, instead return values as specified 21 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 22 | ListNode *temp=A; 23 | ListNode *prev=NULL; 24 | int val=INT_MAX; 25 | while(temp!=NULL){ 26 | if(temp->next!=NULL&&temp->next->val==temp->val){ 27 | val=temp->val; 28 | ListNode* x=temp->next; 29 | temp->next=x->next; 30 | free(x); 31 | } 32 | else{ 33 | if(val!=INT_MAX&&temp->val==val){ 34 | if(prev==NULL){ 35 | ListNode *y=temp; 36 | A=temp->next; 37 | temp=temp->next; 38 | free(y); 39 | } 40 | else{ 41 | ListNode *y=temp; 42 | prev->next=temp->next; 43 | temp=temp->next; 44 | free(y); 45 | } 46 | } 47 | else{ 48 | prev=temp; 49 | temp=temp->next; 50 | } 51 | } 52 | } 53 | return A; 54 | } 55 | -------------------------------------------------------------------------------- /LinkedList/Remove Duplicates from Sorted list: -------------------------------------------------------------------------------- 1 | /* 2 | Given a sorted linked list, delete all duplicates such that each element appear only once. 3 | 4 | For example, 5 | Given 1->1->2, return 1->2. 6 | Given 1->1->2->3->3, return 1->2->3. 7 | 8 | */ 9 | 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 | ListNode* Solution::deleteDuplicates(ListNode* A) { 19 | // Do not write main() function. 20 | // Do not read input, instead use the arguments to the function. 21 | // Do not print the output, instead return values as specified 22 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 23 | ListNode *temp=A; 24 | while(temp!=NULL&&temp->next!=NULL){ 25 | if(temp->next->val==temp->val){ 26 | ListNode* x=temp->next; 27 | temp->next=x->next; 28 | free(x); 29 | } 30 | else 31 | temp=temp->next; 32 | } 33 | return A; 34 | } 35 | -------------------------------------------------------------------------------- /LinkedList/Remove Nth Node from List End: -------------------------------------------------------------------------------- 1 | /* 2 | Given a linked list, remove the nth node from the end of list and return its head. 3 | 4 | For example, 5 | Given linked list: 1->2->3->4->5, and n = 2. 6 | After removing the second node from the end, the linked list becomes 1->2->3->5. 7 | 8 | Note: 9 | * If n is greater than the size of the list, remove the first node of the list. 10 | Try doing it using constant additional space. 11 | */ 12 | 13 | /** 14 | * Definition for singly-linked list. 15 | * struct ListNode { 16 | * int val; 17 | * ListNode *next; 18 | * ListNode(int x) : val(x), next(NULL) {} 19 | * }; 20 | */ 21 | ListNode* Solution::removeNthFromEnd(ListNode* A, int n) { 22 | // Do not write main() function. 23 | // Do not read input, instead use the arguments to the function. 24 | // Do not print the output, instead return values as specified 25 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 26 | int l=0; 27 | ListNode *temp=A; 28 | while(temp!=NULL){ 29 | l++; 30 | temp=temp->next; 31 | } 32 | if(n>=l){ 33 | ListNode* x=A; 34 | A=A->next; 35 | free(x); 36 | return A; 37 | } 38 | temp=A; 39 | for(int i=0;ival<next; 42 | } 43 | ListNode* y=temp->next; 44 | temp->next=y->next; 45 | free(y); 46 | return A; 47 | } 48 | -------------------------------------------------------------------------------- /LinkedList/Reorder List: -------------------------------------------------------------------------------- 1 | /* 2 | Given a singly linked list 3 | 4 | L: L0 → L1 → … → Ln-1 → Ln, 5 | reorder it to: 6 | 7 | L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → … 8 | You must do this in-place without altering the nodes’ values. 9 | 10 | For example, 11 | Given {1,2,3,4}, reorder it to {1,4,2,3}. 12 | */ 13 | 14 | int findLength(ListNode *temp){ 15 | int l=0; 16 | while(temp!=NULL){ 17 | l++; 18 | temp=temp->next; 19 | } 20 | return l; 21 | } 22 | void print(ListNode *temp){ 23 | while(temp!=NULL){ 24 | cout<val<<" "; 25 | temp=temp->next; 26 | } 27 | cout<next; 42 | } 43 | prev->next=NULL; 44 | 45 | prev=NULL; 46 | ListNode *curr=mid; 47 | ListNode *next=NULL; 48 | while(curr!=NULL){ 49 | next=curr->next; 50 | curr->next=prev; 51 | prev=curr; 52 | curr=next; 53 | } 54 | 55 | ListNode* t1=A; 56 | ListNode* t2=prev; 57 | // cout<data<<" "<data; 58 | // print(t1); 59 | //print(t2); 60 | while(t1!=NULL&&t2!=NULL){ 61 | ListNode* temp1=t1->next; 62 | ListNode* temp2=t2->next; 63 | 64 | t1->next=t2; 65 | t2->next=temp1; 66 | 67 | t2=temp2; 68 | t1=temp1; 69 | } 70 | return A; 71 | } 72 | -------------------------------------------------------------------------------- /LinkedList/Reverse A Linked List: -------------------------------------------------------------------------------- 1 | /* 2 | Reverse a linked list. Do it in-place and in one-pass. 3 | 4 | For example: 5 | Given 1->2->3->4->5->NULL, 6 | 7 | return 5->4->3->2->1->NULL. 8 | 9 | */ 10 | 11 | /** 12 | * Definition for singly-linked list. 13 | * struct ListNode { 14 | * int val; 15 | * ListNode *next; 16 | * ListNode(int x) : val(x), next(NULL) {} 17 | * }; 18 | */ 19 | ListNode* Solution::reverseList(ListNode* A) { 20 | // Do not write main() function. 21 | // Do not read input, instead use the arguments to the function. 22 | // Do not print the output, instead return values as specified 23 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 24 | ListNode *prev=NULL; 25 | ListNode *curr=A; 26 | ListNode *next=NULL; 27 | while(curr!=NULL){ 28 | next=curr->next; 29 | curr->next=prev; 30 | prev=curr; 31 | curr=next; 32 | } 33 | return prev; 34 | } 35 | -------------------------------------------------------------------------------- /LinkedList/Reverse Link List II: -------------------------------------------------------------------------------- 1 | /* 2 | Reverse a linked list from position m to n. Do it in-place and in one-pass. 3 | 4 | For example: 5 | Given 1->2->3->4->5->NULL, m = 2 and n = 4, 6 | 7 | return 1->4->3->2->5->NULL. 8 | 9 | Note: 10 | Given m, n satisfy the following condition: 11 | 1 ≤ m ≤ n ≤ length of list. 12 | */ 13 | 14 | /** 15 | * Definition for singly-linked list. 16 | * struct ListNode { 17 | * int val; 18 | * ListNode *next; 19 | * ListNode(int x) : val(x), next(NULL) {} 20 | * }; 21 | */ 22 | ListNode* rev(ListNode *A){ 23 | ListNode *prev=NULL; 24 | ListNode *curr=A; 25 | ListNode *next=NULL; 26 | while(curr!=NULL){ 27 | next=curr->next; 28 | curr->next=prev; 29 | prev=curr; 30 | curr=next; 31 | } 32 | return prev; 33 | } 34 | ListNode* Solution::reverseBetween(ListNode* A, int m, int n) { 35 | // Do not write main() function. 36 | // Do not read input, instead use the arguments to the function. 37 | // Do not print the output, instead return values as specified 38 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 39 | if(m==n) return A; 40 | ListNode *temp=A,*first=A,*last=A,*prev=NULL; 41 | int i=1; 42 | for(;inext; 45 | } 46 | 47 | first=temp; 48 | 49 | for(;inext; 51 | last=temp; 52 | 53 | ListNode* next=last->next; 54 | last->next=NULL; 55 | 56 | first=rev(first); 57 | 58 | if(prev!=NULL)prev->next=first; 59 | else A=first; 60 | temp=first; 61 | while(temp->next!=NULL) 62 | temp=temp->next; 63 | 64 | temp->next=next; 65 | return A; 66 | } 67 | -------------------------------------------------------------------------------- /LinkedList/Rotate List: -------------------------------------------------------------------------------- 1 | /* 2 | Given a list, rotate the list to the right by k places, where k is non-negative. 3 | 4 | For example: 5 | 6 | Given 1->2->3->4->5->NULL and k = 2, 7 | return 4->5->1->2->3->NULL. 8 | */ 9 | 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 | int findLen(ListNode *temp){ 19 | int l=0; 20 | while(temp!=NULL){ 21 | l++; 22 | temp=temp->next; 23 | } 24 | return l; 25 | } 26 | ListNode* Solution::rotateRight(ListNode* A, int k) { 27 | // Do not write main() function. 28 | // Do not read input, instead use the arguments to the function. 29 | // Do not print the output, instead return values as specified 30 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 31 | int l=findLen(A); 32 | if(k%l==0) return A; 33 | ListNode* temp=A; 34 | for(int i=0;inext; 36 | } 37 | ListNode* next=temp->next; 38 | temp->next=NULL; 39 | 40 | ListNode *last=next; 41 | while(last->next!=NULL) 42 | last=last->next; 43 | 44 | last->next=A; 45 | A=next; 46 | return A; 47 | } 48 | -------------------------------------------------------------------------------- /LinkedList/Swap List Nodes in Pairs: -------------------------------------------------------------------------------- 1 | /* 2 | Given a linked list, swap every two adjacent nodes and return its head. 3 | 4 | For example, 5 | Given 1->2->3->4, you should return the list as 2->1->4->3. 6 | 7 | Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed. 8 | */ 9 | 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 | ListNode* Solution::swapPairs(ListNode* A) { 19 | // Do not write main() function. 20 | // Do not read input, instead use the arguments to the function. 21 | // Do not print the output, instead return values as specified 22 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 23 | ListNode *temp=A; 24 | while(temp!=NULL&&temp->next!=NULL){ 25 | int t=temp->val; 26 | temp->val=temp->next->val; 27 | temp->next->val=t; 28 | 29 | temp=temp->next->next; 30 | } 31 | return A; 32 | } 33 | -------------------------------------------------------------------------------- /LinkedList/k Reverse Linked List: -------------------------------------------------------------------------------- 1 | /* 2 | Given a singly linked list and an integer K, reverses the nodes of the 3 | 4 | list K at a time and returns modified linked list. 5 | 6 | NOTE : The length of the list is divisible by K 7 | Example : 8 | 9 | Given linked list 1 -> 2 -> 3 -> 4 -> 5 -> 6 and K=2, 10 | 11 | You should return 2 -> 1 -> 4 -> 3 -> 6 -> 5 12 | 13 | Try to solve the problem using constant extra space. 14 | 15 | */ 16 | 17 | /** 18 | * Definition for singly-linked list. 19 | * struct ListNode { 20 | * int val; 21 | * ListNode *next; 22 | * ListNode(int x) : val(x), next(NULL) {} 23 | * }; 24 | */ 25 | int findLen(ListNode *temp){ 26 | int l=0; 27 | while(temp!=NULL){ 28 | l++; 29 | temp=temp->next; 30 | } 31 | return l; 32 | } 33 | 34 | ListNode* rev(ListNode *A){ 35 | ListNode *prev=NULL; 36 | ListNode *curr=A; 37 | ListNode *next=NULL; 38 | while(curr!=NULL){ 39 | next=curr->next; 40 | curr->next=prev; 41 | prev=curr; 42 | curr=next; 43 | } 44 | return prev; 45 | } 46 | ListNode* reverseBetween(ListNode* A, int m, int n) { 47 | if(m==n) return A; 48 | ListNode *temp=A,*first=A,*last=A,*prev=NULL; 49 | int i=1; 50 | for(;inext; 53 | } 54 | 55 | first=temp; 56 | 57 | for(;inext; 59 | last=temp; 60 | 61 | ListNode* next=last->next; 62 | last->next=NULL; 63 | 64 | first=rev(first); 65 | 66 | if(prev!=NULL)prev->next=first; 67 | else A=first; 68 | temp=first; 69 | while(temp->next!=NULL) 70 | temp=temp->next; 71 | 72 | temp->next=next; 73 | return A; 74 | } 75 | ListNode* Solution::reverseList(ListNode* A, int k) { 76 | ListNode *res=A; 77 | int n=findLen(A); 78 | int x=1; 79 | while(x<=n-k+1){ 80 | res=reverseBetween(res,x,x+k-1); 81 | x+=k; 82 | } 83 | return res; 84 | } 85 | -------------------------------------------------------------------------------- /Math/Excel Coloumn Title: -------------------------------------------------------------------------------- 1 | /* 2 | Given a positive integer, return its corresponding column title as appear in an Excel sheet. 3 | 4 | For example: 5 | 6 | 1 -> A 7 | 2 -> B 8 | 3 -> C 9 | ... 10 | 26 -> Z 11 | 27 -> AA 12 | 28 -> AB 13 | 14 | */ 15 | 16 | string Solution::convertToTitle(int n) { 17 | // Do not write main() function. 18 | // Do not read input, instead use the arguments to the function. 19 | // Do not print the output, instead return values as specified 20 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 21 | string ans=""; 22 | while(n){ 23 | int rem=n%26; 24 | if(rem){ 25 | char c=rem-1+'A'; 26 | string s; 27 | s.push_back(c); 28 | ans.insert(0,s); 29 | n=n/26; 30 | } 31 | else{ 32 | ans.insert(0,"Z"); 33 | n=n/26-1; 34 | } 35 | } 36 | return ans; 37 | } 38 | -------------------------------------------------------------------------------- /Math/Excel Column Number: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Given a column title as appears in an Excel sheet, return its corresponding column number. 4 | 5 | Example: 6 | 7 | A -> 1 8 | 9 | B -> 2 10 | 11 | C -> 3 12 | 13 | ... 14 | 15 | Z -> 26 16 | 17 | AA -> 27 18 | 19 | AB -> 28 20 | 21 | */ 22 | 23 | int Solution::titleToNumber(string s) { 24 | // Do not write main() function. 25 | // Do not read input, instead use the arguments to the function. 26 | // Do not print the output, instead return values as specified 27 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 28 | int n=s.size(); 29 | int res=0,j=n-1; 30 | for(int i=0;i Solution::fizzBuzz(int n) { 12 | vectorv(n); 13 | for(int i=1;i<=n;i++){ 14 | if(i%3==0&&i%5==0) 15 | v[i-1]="FizzBuzz"; 16 | else if(i%3==0) 17 | v[i-1]="Fizz"; 18 | else if(i%5==0) 19 | v[i-1]="Buzz"; 20 | else 21 | v[i-1]=to_string(i); 22 | } 23 | return v; 24 | } 25 | -------------------------------------------------------------------------------- /Math/Greatest Common Divisor: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Given 2 non negative integers m and n, find gcd(m, n) 4 | 5 | GCD of 2 integers m and n is defined as the greatest integer g such that g is a divisor of both m and n. 6 | Both m and n fit in a 32 bit signed integer. 7 | 8 | Example 9 | 10 | m : 6 11 | n : 9 12 | 13 | GCD(m, n) : 3 14 | NOTE : DO NOT USE LIBRARY FUNCTIONS 15 | 16 | */ 17 | 18 | int Solution::gcd(int x, int y) { 19 | // Do not write main() function. 20 | // Do not read input, instead use the arguments to the function. 21 | // Do not print the output, instead return values as specified 22 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 23 | int dividend=x>y?x:y; 24 | int divisor=x>y?y:x; 25 | while(divisor!=0){ 26 | int rem=dividend%divisor; 27 | dividend=divisor; 28 | divisor=rem; 29 | } 30 | return dividend; 31 | } 32 | -------------------------------------------------------------------------------- /Math/Grid Unique Paths: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | A robot is located at the top-left corner of an A x B grid (marked ‘Start’ in the diagram below). 4 | The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of 5 | the grid (marked ‘Finish’ in the diagram below). 6 | 7 | How many possible unique paths are there? 8 | 9 | Note: A and B will be such that the resulting answer fits in a 32 bit signed integer. 10 | 11 | Example : 12 | 13 | Input : A = 2, B = 2 14 | Output : 2 15 | 16 | 2 possible routes : (0, 0) -> (0, 1) -> (1, 1) 17 | OR : (0, 0) -> (1, 0) -> (1, 1) 18 | */ 19 | 20 | int Solution::uniquePaths(int m, int n) { 21 | // Do not write main() function. 22 | // Do not read input, instead use the arguments to the function. 23 | // Do not print the output, instead return values as specified 24 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 25 | int dp[m][n]; 26 | for(int i=0;iy?x:y; 18 | int divisor=x>y?y:x; 19 | while(divisor!=0){ 20 | int rem=dividend%divisor; 21 | dividend=divisor; 22 | divisor=rem; 23 | } 24 | return dividend; 25 | } 26 | 27 | int Solution::cpFact(int A, int B) { 28 | while(gcd(A,B)!=1){ 29 | A=A/gcd(A,B); 30 | } 31 | return A; 32 | } 33 | -------------------------------------------------------------------------------- /Math/Palindrome Integer: -------------------------------------------------------------------------------- 1 | /* 2 | Determine whether an integer is a palindrome. Do this without extra space. 3 | 4 | A palindrome integer is an integer x for which reverse(x) = x where reverse(x) is x with its digit reversed. 5 | Negative numbers are not palindromic. 6 | 7 | Example : 8 | 9 | Input : 12121 10 | Output : True 11 | 12 | Input : 123 13 | Output : False 14 | 15 | */ 16 | 17 | bool Solution::isPalindrome(int n) { 18 | // Do not write main() function. 19 | // Do not read input, instead use the arguments to the function. 20 | // Do not print the output, instead return values as specified 21 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 22 | if(n<0) return false; 23 | int x=n,s=0; 24 | while(x){ 25 | s++; 26 | x/=10; 27 | } 28 | int j=s-1; 29 | while(n){ 30 | int y=n/(pow(10,j)); 31 | if(y!=(n%10)) 32 | return false; 33 | n=n-y*pow(10,j); 34 | n=n/10; 35 | j-=2; 36 | } 37 | return true; 38 | } 39 | 40 | //Solution 2: by reversing the integer 41 | 42 | bool Solution::isPalindrome(int n) { 43 | // Do not write main() function. 44 | // Do not read input, instead use the arguments to the function. 45 | // Do not print the output, instead return values as specified 46 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 47 | if(n<0) return false; 48 | int x=n; 49 | long long res=0; 50 | while(x){ 51 | res=res*10+x%10; 52 | x=x/10; 53 | } 54 | if(res==n) return 1; 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Math/Power of two integers: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Given a positive integer which fits in a 32 bit signed integer, find if it can be expressed as A^P 4 | where P > 1 and A > 0. A and P both should be integers. 5 | 6 | Example 7 | 8 | Input : 4 9 | Output : True 10 | as 2^2 = 4. 11 | 12 | */ 13 | 14 | bool Solution::isPower(int n) { 15 | if(n==1||n==0) return 1; 16 | int p=2; 17 | float x=pow(n,1.0/p); 18 | while((int)x>1){ 19 | //cout< Solution::primesum(int n) { 24 | vector isprime(n+1); 25 | //memset(isprime,1,sizeof(isprime)); 26 | for(int i=0;i<=n;i++) 27 | isprime[i]=1; 28 | isprime[0]=0; 29 | isprime[1]=0; 30 | for(int i=2;i*i<=n;i++){ 31 | if(isprime[i]){ 32 | for(int j=i*2;j<=n;j+=i) 33 | isprime[j]=0; 34 | } 35 | } 36 | vectorans(2); 37 | for(int i=2;i<=n/2;i++){ 38 | if(isprime[i]&&isprime[n-i]){ 39 | ans[0]=i; 40 | ans[1]=n-i; 41 | return ans; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Math/Rearrange array: -------------------------------------------------------------------------------- 1 | /* 2 | Rearrange a given array so that Arr[i] becomes Arr[Arr[i]] with O(1) extra space. 3 | 4 | Example: 5 | 6 | Input : [1, 0] 7 | Return : [0, 1] 8 | Lets say N = size of the array. Then, following holds true : 9 | * All elements in the array are in the range [0, N-1] 10 | * N * N does not overflow for a signed integer 11 | 12 | */ 13 | 14 | //Solution 1: Using extra space 15 | 16 | void Solution::arrange(vector &A) { 17 | // Do not write main() function. 18 | // Do not read input, instead use the arguments to the function. 19 | // Do not print the output, instead return values as specified 20 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 21 | int n=A.size(); 22 | vectorv(n); 23 | for(int i=0;i &A) { 35 | // Do not write main() function. 36 | // Do not read input, instead use the arguments to the function. 37 | // Do not print the output, instead return values as specified 38 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 39 | int n=A.size(); 40 | for(int i=0;iINT_MAX||res &A) { 28 | long long int ans=0,n=A.size(); 29 | long long int count; 30 | for(int i=0;i<31;i++){ 31 | count=0; 32 | for(int j=0;j='0'&&c<='9')||(c>='a'&&c<='z')||(c>='A'&&c<='Z')) 16 | return true; 17 | return false; 18 | } 19 | int Solution::isPalindrome(string A) { 20 | // Do not write main() function. 21 | // Do not read input, instead use the arguments to the function. 22 | // Do not print the output, instead return values as specified 23 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 24 | int i=0,len=0; 25 | while(A[len]!='\0'){ 26 | len++; 27 | } 28 | int j=len-1; 29 | while(i<=j){ 30 | // cout<