diff --git a/Arrays/LargestNumber.cpp b/Arrays/LargestNumber.cpp index 8ef75b0..2a3f864 100644 --- a/Arrays/LargestNumber.cpp +++ b/Arrays/LargestNumber.cpp @@ -1,16 +1,7 @@ // https://www.interviewbit.com/problems/largest-number/ -bool checkLarger(int a, int b){ - string aa = to_string(a); - string bb = to_string(b); - string st = aa + bb; - string rev = bb + aa; - - if(st > rev){ - return true; - } - - return false; +bool comp(string &s1,string &s2){ + return (s1+s2)>=(s2+s1); } string Solution::largestNumber(const vector &A) { @@ -19,18 +10,59 @@ string Solution::largestNumber(const vector &A) { // Do not print the output, instead return values as specified // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details - vector sol = A; + + int n=A.size(); + if(n==1) + return to_string(A[0]); + int count=0; + for(int x : A) + if(x==0) + count++; + if(count==n) + return "0"; + vectors(n,""); + for(int i=0;i rev){ +// return true; +// } - string ans = ""; - for(int i = 0; i < sol.size(); i++){ - ans = ans + to_string(sol[i]); - } +// return false; +// } + +// string Solution::largestNumber(const vector &A) { +// // Do not write main() function. +// // Do not read input, instead use the arguments to the function. +// // Do not print the output, instead return values as specified +// // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details - if(ans[0] == '0'){ - return "0"; - } +// vector sol = A; - return ans; -} +// sort(sol.begin(), sol.end(), checkLarger); + +// string ans = ""; +// for(int i = 0; i < sol.size(); i++){ +// ans = ans + to_string(sol[i]); +// } + +// if(ans[0] == '0'){ +// return "0"; +// } + +// return ans; +// } diff --git a/Arrays/Partitions.cpp b/Arrays/Partitions.cpp new file mode 100644 index 0000000..25a162f --- /dev/null +++ b/Arrays/Partitions.cpp @@ -0,0 +1,81 @@ +/* +Link: https://www.interviewbit.com/problems/partitions/ +Problem Description + +You are given a 1D integer array B containing A integers. + +Count the number of ways to split all the elements of the array into 3 contiguous parts so that the sum of elements in each part is the same. + +Such that : sum(B[1],..B[i]) = sum(B[i+1],...B[j]) = sum(B[j+1],...B[n]) + + + +Problem Constraints +1 <= A <= 105 + +-109 <= B[i] <= 109 + + + +Input Format +First argument is an integer A. + +Second argument is an 1D integer array B of size A. + + + +Output Format +Return a single integer denoting the number of ways to split the array B into three parts with the same sum. + + + +Example Input +Input 1: + + A = 5 + B = [1, 2, 3, 0, 3] +Input 2: + + A = 4 + B = [0, 1, -1, 0] + + +Example Output +Output 1: + + 2 +Output 2: + + 1 + + +*/ +#include +int Solution::solve(int A, vector &B) { + int count = 0; +int sum = std::accumulate(B.begin(), B.end(), 0); + +if(sum%3 == 0) { + vector prefix(A); + vector suffix(A); + int runningSum = 0; + for(int i = 0; i < A; i++) { + suffix[i] = sum - runningSum; + runningSum += B[i]; + prefix[i] = runningSum; + } + + const int val = sum/3; + for(int i = 0; i < A; i++) { + if( prefix[i] == val) { + for(int j = i+2; j < A; j++) { + if( suffix[j] == val) { + count++; + } + } + } + } + +} +return count; +} diff --git a/Hashing/LongestSubArrayLength.cpp b/Hashing/LongestSubArrayLength.cpp new file mode 100644 index 0000000..1aedaf7 --- /dev/null +++ b/Hashing/LongestSubArrayLength.cpp @@ -0,0 +1,33 @@ +/*https://www.interviewbit.com/problems/longest-subarray-length/ +Longest Subarray Length +Asked in: +DE Shaw +*/ + + + + + // Do not write main() function. + // Do not read input, instead use the arguments to the function. + // Do not print the output, instead return values as specified + // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details + +int Solution::solve(vector &A) { + unordered_mapmap; + int maxlen=0,cont=0; + for(int i=0;i> multiply(vector>a,vector>b){ + vector>m(2, vector(2)); + + for(int i=0;i<2;i++){ + for(int j=0;j<2;j++){ + m[i][j]=0; + for(int k=0;k<2;k++){ + m[i][j]+=((a[i][k]%1000000007)*(b[k][j]%1000000007))%1000000007; + } + } + } +return m; +} +int Solution::solve(int n) { + vector>PS= {{1, 1},{1, 0}}; + vector>P= {{1, 0},{0, 1}}; + while(n>0){ + if ((n%2)==1) P=multiply(P, PS); + n=n/2; PS =multiply(PS, PS); + } + +return P[0][1]%1000000007; +} + +// This Approch is based on Simple technique but may or may not pass the all testcases but an correct approch for leetcode and other platform + +//And This 3rd approch will give a O(1) time complexity 100 % faster on leet code but may not pass some cases on interview bit diff --git a/Two-Pointers/CountingSubarrays.cpp b/Two-Pointers/CountingSubarrays.cpp new file mode 100644 index 0000000..f875f65 --- /dev/null +++ b/Two-Pointers/CountingSubarrays.cpp @@ -0,0 +1,31 @@ + +//Counting Subarrays! 2 Times Asked in Uber + + +//Question: https://www.interviewbit.com/problems/counting-subarrays/ + // Do not write main() function. + // Do not read input, instead use the arguments to the function. + // Do not print the output, instead return values as specified + // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details + + +int Solution::solve(vector &A, int B) { + int n=A.size(); + + int cont=0,window=0; + int sum=0; + int i=0; + while(i &A) { + int max=INT_MIN; + int sum=0; + for(int i=0;imax) + max=sum; + + } + return max; +}