Find Earliest Month π
Learn this problemProblem statement
The interns at Amazon were asked to review the company's stock value over a period. Given the stock prices of n months, the net price change for the ith month is defined as the absolute difference between the average of stock prices for the first i months and for the remaining (n - i) months where 1 β€ i < n. Note that these averages are rounded down to an integer.
Given an array of stock prices, find the month at which the net price change is minimum. If there are several such months, return the earliest month.
Note: The average of a set of integers here is defined as the sum of integers divided by the number of integers, rounded down to the nearest integer. For example, the average of [1, 2, 3, 4] is the floor of (1 + 2 + 3 + 4) / 4 = 2.5 and the floor of 2.5 is 2.
Function
findEarliestMonth(stockPrice: int[]) β int
Complete the function findEarliestMonth in the editor.
findEarliestMonth has the following parameter:
int[] stockPrice: an array of integers representing the stock prices
Returns
int: the earliest month at which the net price change is minimum
Examples
Example 1
stockPrice = [1, 3, 2, 3]return = 2
Example 2
stockPrice = [1, 3, 2, 4, 5]return = 2[1] and [3, 2, 4, 5], their respective averages, rounded down = 1 and 3, net price change = 2[1, 3] and [2, 4, 5], averages = 2 and 3, net price change = 1[1, 3, 2] and [4, 5], averages = 2 and 4, net price change = 2[1, 3, 2, 4] and [5], averages = 2 and 5, net price change = 31, and it occurs at month 2 :D.More Amazon problems
- Drone Delivery RouteOA Β· Seen Aug 2026
- Package Dependency OrderPHONE SCREEN Β· ONSITE INTERVIEW Β· Seen Aug 2026
- Unfulfilled Customers by Inventory PriorityOA Β· Seen Aug 2026
- Calculate Beauty ValuesOA Β· Seen Aug 2026
- Maximize Distance to the Closest Occupied SeatONSITE INTERVIEW Β· Seen Aug 2026
- Package Delivery SystemOA Β· Seen Aug 2026
- Select Least Resource TasksOA Β· Seen Aug 2026
- Maximum Length-K Window Sum over Sparse SegmentsOA Β· Seen Aug 2026