Max Profit Amazon OA 2023 Solution
An Amazon seller is deciding which of their products to invest in for the next quarter to maximize their profits. They have each of their products listed as segments of a circle. Due to varying market conditions, the products do not sell consistently. The seller wants to achieve maximum profit using limited resources for investment. The product list is segmented into a number of equal segments, and a projected profit is calculated for each segment. The projected profit is the cost to invest versus the sale price of the product. The seller has chosen to invest in a number of contiguous segments along with those opposite. Determine the maximum profit the seller can achieve using this approach.

For example, the product list Is divided into n = 6 sections and will select k = 2 contiguous sections and those opposite to invest in. The profit estimates are profit = [1, 5, 1, 3, 7, -3] respectively. The diagrams below show the possible choices with profits[0] at the 9 o’clock position and filling counterclockwise.
The profit levels, from left to right, are 1+5+7+3=16, 5+1+7+-3=10, and 1+3+-3+1=2. The maximum profit is 16.
Input
- k: an integer that denotes half of the needed number of products within the list
- profit: an array of integers that denote the profit from investing in each of the products
Output
the maximum profit possible
Examples
Example 1:
Input:
1k = 2
2profit = [1, 5, 1, 3, 7 -3]
Output: 16
Explanation:
The profit levels, from left to right, are 1+5+7+3=16, 5+1+7+-3=10, and 1+3+-3+1=2. The maximum profit is 16.
Example 2:
Input:
1k = 1
2profit = 3 -5
Output: -2
Explanation:
Here k=1 and n=2. The seller will choose 2*k=2 products. In this case, it is the entire list, so overall profit is 3+-5=-2.
Constraints
- 1 <= k <= n/2
- 2 <= n <= 10^5
- n is even
- 0 <= |profit[i]| <= 10^9

SOLUTION
Program: Max Profit Amazon OA Solution in Python
Python Approach – the question gives you a lot of unnecessary information. The main thing we need to be concerned about is the edge case of getting the array to be “circular”. There are some cases my code might not pass (i.e if k is larger than 2 times the size of the array), but this is the general idea of it.
Time: O(n^2) – basically doing a double for loop for a window of the array
Space: O(1)
def contprofit(arr, k):
profits = 0
for i in range(len(arr) - k):
window = sum(arr[i : i + k])
for j in range(i + k, len(arr)):
nextwindow = sum(arr[j : j + k])
if j + k > len(arr) - 1:
diff = (j + k) - len(arr)
nextwindow += sum(arr[0:diff])
profits = max(profits, window + nextwindow)
return profits
Program: Max Profit Amazon OA Solution in C++
#include<bits/stdc++.h>
using namespace std;
void solve() {
int n, k;
cin >> n >> k;
vector<int>a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int i = 0, j = 0, sum = 0, p = INT_MIN;
while (j < n) {
sum += a[j] + a[(n / 2 + j) % n];
while (j - i + 1 == k) {
p = max(p, sum);
sum -= (a[i] + a[(i + n / 2 ) % n]);
i++;
}
j++;
}
cout << p << endl;
}
int main() {
#ifndef ONLINE_JUDGE
//for getting input from input.txt
freopen("input1.txt", "r", stdin);
// for writing output to output.txt
freopen("output1.txt", "w", stdout);
#endif
int t ;
cin >> t;
while (t--) {
solve();
}
}
Program: Max Profit Amazon OA Solution in C++
Time Complexity – O(n)
Space Complexity – O(n)
Let me know your thoughts on this
// efficient harvest
int getQuerySum(vector<int> &prefixSum, int a, int b) // function to get interval sum in constant time
{
if(a <= b)
return prefixSum[b] - (a >= 1 ? prefixSum[a-1] : 0);
return getQuerySum(prefixSum, a, prefixSum.size()-1) + getQuerySum(prefixSum, 0, b);
}
int findMaxProfit(vector<int> profit, int n, int k)
{
vector<int> prefixSum;
int sum = 0, maxProfit = INT_MIN, l = (n >> 1);
for(auto &x : profit)
{
sum += x;
prefixSum.push_back(sum);
}
for(int i = 0;i < (n >> 1); i++)
{
int temp = getQuerySum(prefixSum, i, i + k - 1) + getQuerySum(prefixSum, (i + l) % n, (i + l + k - 1) % n);
maxProfit = max(maxProfit, temp);
}
return maxProfit;
}
int main()
{
cout<<findMaxProfit({1, 5, 1, 3, 7, -3}, 6, 2)<<"\n"; // 16
cout<<findMaxProfit({-3, -6, 3, 6}, 4, 1)<<"\n"; // 0
cout<<findMaxProfit({3, -5}, 2, 1)<<"\n"; // -2
return 0;
}
Program: Max Profit Amazon OA Solution in JavaScript
TC: O(n + k)
(function main() {
var k = 2;
var profit = [7, 3, 1, 5, 1, -3];
console.log(maxProfit(k, profit));
k = 1;
profit = [6, 3, -6, -3];
console.log(maxProfit(k, profit));
k = 1;
profit = [-5, 3];
console.log(maxProfit(k, profit));
}());
function maxProfit(k, profit) {
var length = profit.length;
var half_length = length / 2;
var max = 0;
//get the profit for index 0;
for (var i = 0; i < k; i ++) {
max += profit[i];
max += profit[(i+half_length) % length];
}
var current_profit = max;
for (var i = 1; i < length/2; i ++) {
current_profit -= profit[i - 1];
current_profit -= profit[(i - 1 + half_length) % length]
current_profit += profit[i + k - 1]
current_profit += profit[(i + k - 1 + half_length) % length]
max = Math.max(max, current_profit);
}
return max;
}
Program: Max Profit Amazon OA Solution in Golang
Solution 1: O(N*K)
The key is how to get the oppsite index:
for circle 6: 0->3; 1->4;2->5;3->0; 5->2;
opIndex:=(curIndex+len(profits)/2)%len(profits).
size, res := len(profits), math.MinInt32
for i := 0; i < size/2; i++ { // try each!
var sum int
for j := i; j < i+k; j++ {sum += profits[j] + profits[(j+size/2)%size]}
if sum > res { res = sum}
}
Solution 2: O(N) with preSum
size := len(profits)
preSum := make([]int, 2*size+1)
for i := 1; i < len(preSum); i++ {preSum[i] = preSum[i-1] + profits[i%size]}
res := math.MinInt32
for i := 0; i < size/2; i++ {
thisSum := preSum[i+k] - preSum[i]
oppSum := preSum[i+size/2+k] - preSum[i+size/2]
if thisSum+oppSum > res { res = thisSum + oppSum}
}
Program: Max Profit Amazon OA Solution in Java
public class EffecientHarvest {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] table = {3,-5};
int k = 1;
int n = table.length;
System.out.println(effecient(table, k, n));
}
private static int effecient(int[] table, int k, int n) {
// TODO Auto-generated method stub
n=n/2;
if(k>n) {
return 0;
}
int i=0;
int max = Integer.MIN_VALUE;
while(i<n) {
int sum=0;
for(int j=i; j<i+k; j++) {
int opp;
if(j<n) {
opp = table[j+n];
}
else {
opp = table[j+n-(n*2)];
}
sum+=opp+table[j];
}
if(sum>max) {
max=sum;
}
i++;
}
return max;
}
}
Amazon OA 2023 Questions with Solution
- Shopping Patterns Solution Amazon OA 2023
- Reorder Data in Log Files Solution Amazon OA 2023
- Top K Frequent Words Solution Amazon OA 2023
- Trees Height Solution Amazon OA SDE 2023
- Counting Binary Substrings Amazon OA 2023
- Grid Connections Amazon OA 2023
- Shipment Imbalance Amazon OA 2023
- Find Lowest Price Amazon OA 2023
- Decode String Frequency Amazon OA 2023
- Simple Cipher Amazon OA 2023
- Valid Discount Coupons Amazon OA 2023 Solution
- Count Maximum Teams Amazon OA 2023
- Minimum Coin Flips Amazon OA 2023
- Max Average Stock Price Amazon OA 2023 Solution
- Robot Bounded In Circle Amazon OA 2023
- Shopping Options Amazon OA 2023 Solution
- Fill The Truck Maximum Units on a Truck Amazon OA Solution
- Maximize Score After N Operations Number Game Solution Amazon OA 2023
- Slowest Key Amazon OA 2023 Solution
- Five Star Seller Maximum Average Pass Ratio Amazon OA 2023
- Split String Into Unique Primes Amazon OA 2023 Solution
- Storage Optimization Amazon OA 2023 Solution
- Minimum Difficulty of a Job Schedule Amazon OA 2023 Solution
- Autoscale Policy Utilization Check Amazon OA 2023
- Optimal Utilization Solution Amazon OA 2023
- Merge Two Sorted Lists Solution Amazon OA 2023
- Two Sum Unique Pairs Solution Amazon OA 2023
- Amazon Music Pairs Amazon OA 2023 Solution
- Class Grouping Amazon OA 2023 Solution
- Find Max products Amazon OA 2023 Solution
- Get encrypted number Amazon OA 2023 Solution
- Find Total Imbalance Amazon OA 2023 Solution
- Find Total Power Amazon OA 2023 Solution