Skip to content

Commit 97c2538

Browse files
committed
Add Max Selling Gap problem
1 parent 9c7cc9b commit 97c2538

File tree

4 files changed

+67
-22
lines changed

4 files changed

+67
-22
lines changed

Maximum Selling Gap/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Max Selling Gap
2+
[Try the problem](https://www.interviewbit.com/problems/max-distance/)
3+
4+
Given an array `A` of integers, find the maximum of `j - i` subjected to the constraint of `A[i] <= A[j]`.
5+
6+
If there is no solution possible, return `-1`.
7+
8+
### Example 1
9+
10+
```
11+
Input: [3,5,4,2]
12+
Output: 2
13+
Explanation: For the pair (3, 4).
14+
```
15+
16+
### Example 2
17+
```
18+
Input: [5,7,1,9,6,3,2]
19+
Output: 4
20+
Explanation: For the pair (1, 2). There could be other answers but they'll have the same maximum gap.
21+
```

Maximum Selling Gap/solution.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <vector>
2-
2+
#include <iostream>
33
using namespace std;
44

55
int maxSellingGap(const vector<int>& price) {
@@ -13,34 +13,31 @@ int maxSellingGap(const vector<int>& price) {
1313
}
1414

1515
int left = 0;
16-
int right = num - 1;
16+
int right = 0;
1717

1818
int maxGap = 0;
19-
int minPrefix = -1;
20-
while (left < num) {
19+
int minPrefix = price[0];
20+
while (left < num && right < num) {
2121
int elem = price[left];
22-
// 5 7 1 9 6 3 2
23-
// 9 9 9 9 6 3 2
24-
// ^
25-
26-
if (minPrefix == -1) {
27-
// first iteration
28-
while (suffix_max[right] < elem) {
29-
--right;
30-
}
31-
} else if (minPrefix > elem) {
32-
// shift the right to right
33-
while (right < num && suffix_max[right] < elem) {
34-
++right;
35-
}
36-
}
37-
minPrefix = min(minPrefix, elem);
22+
// 5 7 1 9 6 3 2
23+
// 5 5 1 1 1 1 1 (minPrefix)
24+
// 9 9 9 9 6 3 2 (suffix_max)
25+
// ^
3826

39-
if (right < num && price[right] >= elem) {
27+
minPrefix = min(minPrefix, elem);
28+
if (minPrefix <= suffix_max[right]) {
4029
maxGap = max(maxGap, right - left);
30+
++right;
31+
} else {
32+
++left;
4133
}
42-
++left;
4334
}
4435

4536
return maxGap;
37+
}
38+
39+
int main () {
40+
vector<int> price = {5,7,1,9,6,3,2};
41+
cout << maxSellingGap(price) << endl;
42+
return 0;
4643
}

Maximum Selling Gap/solution.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Solution
2+
3+
<details>
4+
<summary>
5+
Hint 1
6+
</summary>
7+
The brute force O(N^2) solution is obvious. Try to think if you can use sorting (you might have to also store the index information) to optimize it to O(N log N).
8+
</details>
9+
10+
<details>
11+
<summary>
12+
Hint 2
13+
</summary>
14+
Think if you can further reduce the complexity of your algorithm to O(N). You might need extra space.
15+
Given a index i, we basically want to find the maximum index j, such that A[i] <= A[j].
16+
So, does storing the suffixMax (i.e. maximum starting from a given index till the end) help?
17+
18+
At an index, if the suffixMax at next index is greater than it, definitely we can get a possible value from here. How to get the maximum one?
19+
</details>
20+
21+
<details>
22+
<summary>
23+
Hint 3
24+
</summary>
25+
Now, also think if storing or calculating (while iterating) the prefixMin can help in some way!
26+
</details>

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This repository contains the coding interview problems along with solutions.
2222
| [2 Keys Keyboard](2%20Keys%20Keyboard) | [text](2%20Keys%20Keyboard/solution.txt) | [CPP](2%20Keys%20Keyboard/solution.cpp) | DP, Maths | Medium |
2323
| [Very Hard Queries](Very%20Hard%20Queries) | [Solution article](Very%20Hard%20Queries/solution.md) | [CPP](Very%20Hard%20Queries/solution.cpp) | Queries, Maths | Medium |
2424
| [Deterministic Finite Automaton](Deterministic%20Finite%20Automaton/) | [Solution article](Deterministic%20Finite%20Automaton/solution.md) | [CPP](Deterministic%20Finite%20Automaton/solution.cpp) | Recursion, Dynamic Programming | Medium |
25+
| [Maximum Selling Gap](Maximum%20Selling%20Gap) | [Solution article](Maximum%20Selling%20Gap/solution.md) | [CPP](Maximum%20Selling%20Gap/solution.cpp) | Arrays | Medium |
2526

2627
# CS Fundamentals
2728

0 commit comments

Comments
 (0)