Skip to content

Commit 4d55a80

Browse files
Add files via upload
1 parent e720be6 commit 4d55a80

File tree

88 files changed

+4803
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+4803
-0
lines changed

Arrays/AddOneToNumber.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Given a non-negative number represented as an array of digits,
3+
4+
add 1 to the number ( increment the number represented by the digits ).
5+
6+
The digits are stored such that the most significant digit is at the head of the list.
7+
8+
Example:
9+
10+
If the vector has [1, 2, 3]
11+
12+
the returned vector should be [1, 2, 4]
13+
14+
as 123 + 1 = 124.
15+
16+
LINK: https://www.interviewbit.com/problems/add-one-to-number/
17+
*/
18+
19+
vector<int> Solution::plusOne(vector<int> &A)
20+
{
21+
int c=1;
22+
23+
for(int i=A.size()-1;i>=0;i--)
24+
{
25+
int num = A[i]+c;
26+
c = num/10;
27+
A[i] = num%10;
28+
}
29+
30+
vector<int> v;
31+
if(c==1)
32+
{
33+
v.push_back(1);
34+
for(int i=0;i<A.size();i++)
35+
v.push_back(A[i]);
36+
}
37+
else
38+
{
39+
int i=0;
40+
while(i<A.size())
41+
{
42+
if(A[i]==0)
43+
i++;
44+
else
45+
break;
46+
}
47+
for(;i<A.size();i++)
48+
v.push_back(A[i]);
49+
}
50+
return v;
51+
}

Arrays/AntiDiagonals.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Give a N*N square matrix, return an array of its anti-diagonals. Look at the example for more details.
3+
4+
Example:
5+
6+
7+
Input:
8+
9+
1 2 3
10+
4 5 6
11+
7 8 9
12+
13+
Return the following :
14+
15+
[
16+
[1],
17+
[2, 4],
18+
[3, 5, 7],
19+
[6, 8],
20+
[9]
21+
]
22+
23+
24+
Input :
25+
1 2
26+
3 4
27+
28+
Return the following :
29+
30+
[
31+
[1],
32+
[2, 3],
33+
[4]
34+
]
35+
36+
LINK: https://www.interviewbit.com/problems/anti-diagonals/
37+
*/
38+
39+
vector<vector<int> > Solution::diagonal(vector<vector<int> > &A)
40+
{
41+
vector<vector<int>> v;
42+
43+
int n = A.size();
44+
45+
for(int i=0;i<n;i++)
46+
{
47+
vector<int> vv;
48+
for(int j=i,k=0;k<=i;j--,k++)
49+
vv.push_back(A[k][j]);
50+
v.push_back(vv);
51+
}
52+
53+
for(int i=1;i<n;i++)
54+
{
55+
vector<int> vv;
56+
for(int j=n-1,k=i;k<n;j--,k++)
57+
vv.push_back(A[k][j]);
58+
v.push_back(vv);
59+
}
60+
61+
return v;
62+
}

Arrays/FindDuplicateinArray.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
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.
3+
4+
Sample Input:
5+
6+
[3 4 1 4 1]
7+
Sample Output:
8+
9+
1
10+
If there are multiple possible answers ( like in the sample case above ), output any one.
11+
12+
If there is no duplicate, output -1
13+
14+
LINK: https://www.interviewbit.com/problems/find-duplicate-in-array/
15+
*/
16+
17+
int Solution::repeatedNumber(const vector<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+
23+
int valueRange = A.size()-1;
24+
int range = sqrt(valueRange);
25+
26+
if(range*range < valueRange)
27+
range++;
28+
29+
int count[range+1];
30+
memset(count, 0, sizeof(count));
31+
32+
for(int i=0;i<A.size();i++)
33+
{
34+
count[(A[i]-1)/range]++;
35+
}
36+
37+
int repeatedRange = -1;
38+
int numRanges = ((valueRange-1)/range)+1;
39+
40+
for(int i=0;i<numRanges && repeatedRange==-1;i++)
41+
{
42+
if(i<numRanges-1 || valueRange%range==0)
43+
{
44+
if(count[i]>range)
45+
repeatedRange = i;
46+
}
47+
else
48+
if(count[i] > valueRange%range)
49+
repeatedRange = i;
50+
}
51+
52+
if(repeatedRange == -1)
53+
return -1;
54+
55+
memset(count, 0, sizeof(count));
56+
57+
for(int i=0;i<A.size();i++)
58+
{
59+
if((A[i]-1)/range == repeatedRange)
60+
count[(A[i]-1)%range]++;
61+
}
62+
63+
for(int i=0;i<range;i++)
64+
{
65+
if(count[i]>1)
66+
return repeatedRange*range+i+1;
67+
}
68+
69+
return -1;
70+
}

Arrays/FindPermutation.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Given a positive integer n and a string s consisting only of letters D or I, you have to find any permutation of first n positive integer that satisfy the given input string.
3+
4+
D means the next number is smaller, while I means the next number is greater.
5+
6+
Notes
7+
8+
Length of given string s will always equal to n - 1
9+
Your solution should run in linear time and space.
10+
Example :
11+
12+
Input 1:
13+
14+
n = 3
15+
16+
s = ID
17+
18+
Return: [1, 3, 2]
19+
20+
LINK: https://www.interviewbit.com/problems/find-permutation/
21+
*/
22+
23+
vector<int> Solution::findPerm(const string s, int n)
24+
{
25+
vector<int> v(n);
26+
int len = s.length();
27+
28+
int k=1;
29+
30+
for(int i=0;i<len;i++)
31+
{
32+
if(s[i]=='I')
33+
{
34+
v[i] = k;
35+
k++;
36+
}
37+
}
38+
39+
v[n-1] = k++;
40+
41+
for(int i=len-1;i>=0;i--)
42+
{
43+
if(s[i]=='D')
44+
{
45+
v[i] = k;
46+
k++;
47+
}
48+
}
49+
50+
return v;
51+
}

Arrays/FirstMissingInteger.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
LINK: https://www.interviewbit.com/problems/first-missing-integer/
15+
*/
16+
17+
int Solution::firstMissingPositive(vector<int> &A)
18+
{
19+
int n = A.size();
20+
21+
int j=0;
22+
23+
for(int i=0;i<n;i++)
24+
{
25+
if(A[i]<=0)
26+
{
27+
swap(A[i],A[j]);
28+
j++;
29+
}
30+
}
31+
32+
int size=n-j;
33+
34+
for(int i=j;i<n;i++)
35+
{
36+
if(abs(A[i])-1+j < n && A[abs(A[i])-1+j]>0)
37+
A[abs(A[i])-1+j] = -A[abs(A[i])-1+j];
38+
}
39+
40+
for(int i=j;i<n;i++)
41+
if(A[i]>0)
42+
return (i-j+1);
43+
44+
return (n-j+1);
45+
}

Arrays/Flip.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
You are given a binary string(i.e. with characters 0 and 1) S consisting of characters S1, S2, …, SN. In a single operation, you can choose two indices L and R such that 1 ≤ L ≤ R ≤ N and flip the characters SL, SL+1, …, SR. By flipping, we mean change character 0 to 1 and vice-versa.
3+
4+
Your aim is to perform ATMOST one operation such that in final string number of 1s is maximised. If you don’t want to perform the operation, return an empty array. Else, return an array consisting of two elements denoting L and R. If there are multiple solutions, return the lexicographically smallest pair of L and R.
5+
6+
Notes:
7+
8+
Pair (a, b) is lexicographically smaller than pair (c, d) if a < c or, if a == c and b < d.
9+
For example,
10+
11+
S = 010
12+
13+
Pair of [L, R] | Final string
14+
_______________|_____________
15+
[1 1] | 110
16+
[1 2] | 100
17+
[1 3] | 101
18+
[2 2] | 000
19+
[2 3] | 001
20+
21+
We see that two pairs [1, 1] and [1, 3] give same number of 1s in final string. So, we return [1, 1].
22+
Another example,
23+
24+
If S = 111
25+
26+
No operation can give us more than three 1s in final string. So, we return empty array [].
27+
28+
LINK: https://www.interviewbit.com/problems/flip/
29+
*/
30+
31+
vector<int> Solution::flip(string A)
32+
{
33+
vector<int> v;
34+
35+
int sum=0, msum=0, st=0, end=0;
36+
37+
int rs = -1, re = -1;
38+
39+
for(int i=0;i<A.length();i++)
40+
{
41+
if(A[i]=='0')
42+
sum++;
43+
else
44+
sum--;
45+
46+
if(sum<0)
47+
{
48+
sum=0;
49+
st = i+1;
50+
}
51+
52+
if(sum>msum)
53+
{
54+
msum = sum;
55+
rs = st;
56+
re = end = i;
57+
}
58+
}
59+
60+
if(rs!=-1 && re!=-1)
61+
{
62+
v.push_back(rs+1);
63+
v.push_back(re+1);
64+
}
65+
66+
return v;
67+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy