Question: Cognizant Python Technical Cluster | Recent OA Feb 2026 | Athlete's Altitude Readings | Array Manipulation & Segment Splitting
0
Entering edit mode

Question: Athlete's Altitude Readings (Maximal Non-Decreasing Runs)

Problem Statement:

You are charting an athlete's altitude readings during a jog, represented by R. It is stored in an integer array A where the route naturally forms plateaus where altitude never decreases for a while and then drops.

You have to split array A into maximal non-decreasing runs (plateaus). For each plateau, add the (last - first) value of that run to a total. Your task is to find and return an integer value representing the sum of all maximal non-decreasing runs of (last_value - first_value).

Note: A "run" is a maximal segment you can't extend without breaking the non-decreasing property; i.e., within a run a[i] <= a[i+1], and the next element after the run is smaller (or the array ends).

Input Specification:

  • input1: An integer value R, representing the number of readings.
  • input2: An integer array A, representing the altitude readings.

Output Specification:

  • Return an integer value representing the sum of all maximal non-decreasing runs of (last_value - first_value)

Example 1:

Input:

input1 : 7

input2 : {2, 2, 5, 4, 4, 6, 1}

Output:

5

Explanation:

Here, R = 7 and A = {2, 2, 5, 4, 4, 6, 1}. Plateaus (maximal non-decreasing runs) are as below:

  • [2, 2, 5] 5 - 2 = 3
  • [4, 4, 6] 6 - 4 = 2
  • [1] 1 - 1 = 0

Total = 3 + 2 + 0 = 5. Hence, 5 is returned as the output.

 

ADD COMMENTlink 6 hours ago Sarthak • 0

Login before adding your answer.

Similar Posts
Loading Similar Posts