Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Questions From interview bit remaining of two pointer math and Hashing #83

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 54 additions & 22 deletions Arrays/LargestNumber.cpp
Original file line number Diff line number Diff line change
@@ -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<int> &A) {
Expand All @@ -19,18 +10,59 @@ string Solution::largestNumber(const vector<int> &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<int> 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";
vector<string>s(n,"");
for(int i=0;i<n;i++)
s[i]+to_string(A[i]);
sort(s.begin(),s.end(),comp);
string ans="";
for(int i=0;i<n;i++)
ans+=s[i];
return ans;
}

// // 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;

sort(sol.begin(), sol.end(), checkLarger);
// if(st > 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<int> &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<int> 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;
// }
81 changes: 81 additions & 0 deletions Arrays/Partitions.cpp
Original file line number Diff line number Diff line change
@@ -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 <numeric>
int Solution::solve(int A, vector<int> &B) {
int count = 0;
int sum = std::accumulate(B.begin(), B.end(), 0);

if(sum%3 == 0) {
vector<int> prefix(A);
vector<int> 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;
}
33 changes: 33 additions & 0 deletions Hashing/LongestSubArrayLength.cpp
Original file line number Diff line number Diff line change
@@ -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<int> &A) {
unordered_map<int , int>map;
int maxlen=0,cont=0;
for(int i=0;i<A.size();i++){
cont+=(A[i]==0?-1:1);
if(cont==1){
maxlen=i+1;
}else if(map.find(cont)==map.end()){
map[cont]=i;
}
if(map.find(cont-1)!=map.end()){
maxlen=max(maxlen,i-map[cont-1]);
}
}
return maxlen;



}
42 changes: 42 additions & 0 deletions Math/FindNthFabbonaciSeries.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*https://www.interviewbit.com/problems/find-nth-fibonacci/

Find Nth Fibonacci
Asked in:
Facebook

*/




// 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
// St
vector<vector<long long>> multiply(vector<vector<long long>>a,vector<vector<long long>>b){
vector<vector<long long>>m(2, vector<long long>(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<vector<long long>>PS= {{1, 1},{1, 0}};
vector<vector<long long>>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
31 changes: 31 additions & 0 deletions Two-Pointers/CountingSubarrays.cpp
Original file line number Diff line number Diff line change
@@ -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<int> &A, int B) {
int n=A.size();

int cont=0,window=0;
int sum=0;
int i=0;
while(i<n && window<n ){
if(sum+A[i]<B){
sum+=A[i];
cont+=i-window+1;
i++;
}
else {
sum-=A[window];
window++;
}

}
return cont;
}
30 changes: 30 additions & 0 deletions Two-Pointers/MaxSumContigousSubarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

//Counting Subarrays! Many Time Asked in these
/*Facebook
Paypal
Yahoo
Microsoft
LinkedIn
Amazon
Goldman Sachs*/


//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::maxSubArray(const vector<int> &A) {
int max=INT_MIN;
int sum=0;
for(int i=0;i<A.size();i++){
sum+=A[i];
if(sum<0)
sum=0;
else if(sum>max)
max=sum;

}
return max;
}