Skip to content

Commit 7195822

Browse files
committed
🚀 08-Jul-2020
1 parent 77a7ed6 commit 7195822

File tree

8 files changed

+384
-0
lines changed

8 files changed

+384
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Given a non-negative number represented as an array of digits,
2+
3+
add 1 to the number ( increment the number represented by the digits ).
4+
5+
The digits are stored such that the most significant digit is at the head of the list.
6+
7+
Example:
8+
9+
If the vector has [1, 2, 3]
10+
11+
the returned vector should be [1, 2, 4]
12+
13+
as 123 + 1 = 124.
14+
15+
NOTE: Certain things are intentionally left unclear in this question which you should practice asking the interviewer.
16+
For example, for this problem, following are some good questions to ask :
17+
Q : Can the input have 0’s before the most significant digit. Or in other words, is 0 1 2 3 a valid input?
18+
A : For the purpose of this question, YES
19+
Q : Can the output have 0’s before the most significant digit? Or in other words, is 0 1 2 4 a valid output?
20+
A : For the purpose of this question, NO. Even if the input has zeroes before the most significant digit.
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
vector<int> Solution::plusOne(vector<int> &a) {
33+
int n=a.size();
34+
vector<int>res;
35+
if(n==0){
36+
res.push_back(1); return res;
37+
}
38+
int carry=1; // initially adding one
39+
for(int i=n-1;i>=0;i--){
40+
int tmp=a[i]+carry;
41+
if(tmp>=10){
42+
carry=1;
43+
tmp%=10;
44+
} else carry=0;
45+
res.push_back(tmp);
46+
}
47+
if(carry) res.push_back(1);
48+
for(int i=res.size()-1; i>=0;i--){
49+
if(res[i]==0){
50+
res.erase(res.begin()+i);
51+
} else break;
52+
}
53+
reverse(res.begin(), res.end());
54+
return res;
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Find the contiguous subarray within an array, A of length N which has the largest sum.
2+
3+
Input Format:
4+
5+
The first and the only argument contains an integer array, A.
6+
Output Format:
7+
8+
Return an integer representing the maximum possible sum of the contiguous subarray.
9+
Constraints:
10+
11+
1 <= N <= 1e6
12+
-1000 <= A[i] <= 1000
13+
For example:
14+
15+
Input 1:
16+
A = [1, 2, 3, 4, -10]
17+
18+
Output 1:
19+
10
20+
21+
Explanation 1:
22+
The subarray [1, 2, 3, 4] has the maximum possible sum of 10.
23+
24+
Input 2:
25+
A = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
26+
27+
Output 2:
28+
6
29+
30+
Explanation 2:
31+
The subarray [4,-1,2,1] has the maximum possible sum of 6.
32+
33+
34+
35+
36+
37+
38+
int Solution::maxSubArray(const vector<int> &a) {
39+
int n=a.size();
40+
// kadanes algo
41+
int max_sum=INT_MIN, max_here=0;
42+
for(int i=0;i<n;i++){
43+
max_here+=a[i];
44+
if(max_here> max_sum) max_sum=max_here;
45+
if(max_here<0) max_here=0;
46+
}
47+
return max_sum;
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
You are given an array of N integers, A1, A2 ,…, AN. Return maximum value of f(i, j) for all 1 ≤ i, j ≤ N.
2+
f(i, j) is defined as |A[i] - A[j]| + |i - j|, where |x| denotes absolute value of x.
3+
4+
For example,
5+
6+
A=[1, 3, -1]
7+
8+
f(1, 1) = f(2, 2) = f(3, 3) = 0
9+
f(1, 2) = f(2, 1) = |1 - 3| + |1 - 2| = 3
10+
f(1, 3) = f(3, 1) = |1 - (-1)| + |1 - 3| = 4
11+
f(2, 3) = f(3, 2) = |3 - (-1)| + |2 - 3| = 5
12+
13+
So, we return 5.
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
// O(n^2)
28+
29+
int Solution::maxArr(vector<int> &a) {
30+
int n=a.size();
31+
int maxi=INT_MIN;
32+
for(int i=0;i<n;i++){
33+
for(int j=i+1;j<n;j++){
34+
int tmp=abs(a[i]-a[j])+abs(i-j);
35+
if(tmp>maxi) maxi=tmp;
36+
}
37+
}
38+
return maxi;
39+
}
40+
41+
42+
43+
44+
45+
int Solution::maxArr(vector<int> &a) {
46+
int n=a.size();
47+
int max1=INT_MIN, min1=INT_MAX, max2=INT_MIN, min2=INT_MAX;
48+
for(int i=0;i<n;i++){
49+
max1=max(max1, a[i]+i);
50+
min1=min(min1, a[i]+i);
51+
max2=max(max2, a[i]-i);
52+
min2=min(min2, a[i]-i);
53+
}
54+
return max(max1-min1, max2-min2);
55+
}
56+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Problem Description
2+
3+
You are in an infinite 2D grid where you can move in any of the 8 directions
4+
5+
(x,y) to
6+
(x+1, y),
7+
(x - 1, y),
8+
(x, y+1),
9+
(x, y-1),
10+
(x-1, y-1),
11+
(x+1,y+1),
12+
(x-1,y+1),
13+
(x+1,y-1)
14+
You are given a sequence of points and the order in which you need to cover the points.. Give the minimum number of steps in which you can achieve it. You start from the first point.
15+
16+
NOTE: This question is intentionally left slightly vague. Clarify the question by trying out a few cases in the “See Expected Output” section.
17+
18+
19+
20+
Input Format
21+
Given two integer arrays A and B, where A[i] is x coordinate and B[i] is y coordinate of ith point respectively.
22+
23+
24+
25+
Output Format
26+
Return an Integer, i.e minimum number of steps.
27+
28+
29+
30+
Example Input
31+
Input 1:
32+
33+
A = [0, 1, 1]
34+
B = [0, 1, 2]
35+
36+
37+
Example Output
38+
Output 1:
39+
40+
2
41+
42+
43+
Example Explanation
44+
Explanation 1:
45+
46+
Given three points are: (0, 0), (1, 1) and (1, 2).
47+
It takes 1 step to move from (0, 0) to (1, 1). It takes one more step to move from (1, 1) to (1, 2).
48+
49+
50+
51+
52+
53+
54+
55+
int Solution::coverPoints(vector<int> &a, vector<int> &b) {
56+
int n=a.size();
57+
int steps=0;
58+
for(int i=0;i<n-1;i++){
59+
steps+=max(abs(a[i]-a[i+1]), abs(b[i]-b[i+1]));
60+
}
61+
return steps;
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Following code tries to figure out if a number is prime ( Wiki )
2+
However, it has a bug in it.
3+
Please correct the bug and then submit the code.
4+
5+
6+
7+
8+
// Return 1 if A is prime, else 0
9+
int Solution::isPrime(int n) {
10+
if(n<=1) return 0;
11+
if(n<=3) return 1;
12+
if(n%2==0 || n%3==0) return 0;
13+
for(int i=5;i*i<n;i+=6){
14+
if(n%i==0 || n%(i+2)==0) return 0;
15+
}
16+
return 1;
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Given a non negative integer A,
2+
following code tries to find all pair of integers (a, b) such that
3+
4+
a and b are positive integers
5+
a <= b, and
6+
a2 + b2 = A.
7+
0 <= A <= 100000
8+
However, the code has a small bug. Correct the bug and submit the code
9+
10+
11+
12+
13+
14+
15+
16+
17+
vector<vector<int> > Solution::squareSum(int A) {
18+
vector<vector<int> > ans;
19+
for (int a = 0; a * a < A; a++) {
20+
for (int b = a; b * b < A; b++) {
21+
if (a * a + b * b == A) {
22+
vector<int> newEntry;
23+
newEntry.push_back(a);
24+
newEntry.push_back(b);
25+
ans.push_back(newEntry);
26+
}
27+
}
28+
}
29+
return ans;
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water.
2+
3+
Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
4+
5+
The island doesn't have "lakes" (water inside that isn't connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
6+
7+
8+
9+
Example:
10+
11+
Input:
12+
[[0,1,0,0],
13+
[1,1,1,0],
14+
[0,1,0,0],
15+
[1,1,0,0]]
16+
17+
Output: 16
18+
19+
Explanation: The perimeter is the 16 yellow stripes in the image below:
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
class Solution {
30+
public:
31+
bool isValid(int i, int j, int m, int n){
32+
return ( i>=0 && i<m && j>=0 && j<n);
33+
}
34+
int islandPerimeter(vector<vector<int>>& grid) {
35+
int m=grid.size();
36+
int n=grid[0].size();
37+
int peri=0;
38+
for(int i=0;i<m;i++){
39+
for(int j=0;j<n;j++){
40+
if(grid[i][j]==1){ // land
41+
// top
42+
if(isValid(i-1, j, m, n)==false) peri+=1;
43+
else if(grid[i-1][j]==0) peri+=1;
44+
// right
45+
if(isValid(i, j+1, m, n)==false) peri+=1;
46+
else if(grid[i][j+1]==0) peri+=1;
47+
// down
48+
if(isValid(i+1, j, m, n)==false) peri+=1;
49+
else if(grid[i+1][j]==0) peri+=1;
50+
// left
51+
if(isValid(i, j-1, m, n)==false) peri+=1;
52+
else if(grid[i][j-1]==0) peri+=1;
53+
}
54+
}
55+
}
56+
return peri;
57+
}
58+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water.
2+
3+
Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
4+
5+
The island doesn't have "lakes" (water inside that isn't connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
6+
7+
8+
9+
Example:
10+
11+
Input:
12+
[[0,1,0,0],
13+
[1,1,1,0],
14+
[0,1,0,0],
15+
[1,1,0,0]]
16+
17+
Output: 16
18+
19+
Explanation: The perimeter is the 16 yellow stripes in the image below:
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
class Solution {
30+
public:
31+
bool isValid(int i, int j, int m, int n){
32+
return ( i>=0 && i<m && j>=0 && j<n);
33+
}
34+
int islandPerimeter(vector<vector<int>>& grid) {
35+
int m=grid.size();
36+
int n=grid[0].size();
37+
int peri=0;
38+
for(int i=0;i<m;i++){
39+
for(int j=0;j<n;j++){
40+
if(grid[i][j]==1){ // land
41+
// top
42+
if(isValid(i-1, j, m, n)==false) peri+=1;
43+
else if(grid[i-1][j]==0) peri+=1;
44+
// right
45+
if(isValid(i, j+1, m, n)==false) peri+=1;
46+
else if(grid[i][j+1]==0) peri+=1;
47+
// down
48+
if(isValid(i+1, j, m, n)==false) peri+=1;
49+
else if(grid[i+1][j]==0) peri+=1;
50+
// left
51+
if(isValid(i, j-1, m, n)==false) peri+=1;
52+
else if(grid[i][j-1]==0) peri+=1;
53+
}
54+
}
55+
}
56+
return peri;
57+
}
58+
};

0 commit comments

Comments
 (0)