FastPrepMaximum Sum with K-Element Skipping
Problems/Maximum Sum with K-Element Skipping

Maximum Sum with K-Element Skipping

PhonePe logoPhonePe● MediumFULLTIMEONSITE INTERVIEW

Updated Dec 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a PhonePe interview for a software engineer with 5 to 7 years of experience. It was the first of two coding questions in the second round, a problem-solving round that came after a machine-coding round. The report states the task in full: pick elements of an array for the largest sum, where each pick rules out the next k elements, and return both the sum and the picked elements. Example 1 is the example from the report.

The report gives no limits and does not say what to return when several selections tie or when every value is negative. We added limits, allowed an empty selection with total 0, set a tie rule that skips an element on a tie, and chose a two-row array as the return shape. Examples 2 to 4 are our own.

Our version is about 90% the same as the reported question.

You are given an integer array arr and an integer k. Go through the array from left to right. At each index you either select the element there or skip it:

  • If you select the element at index i, its value is added to your total, and you must skip the next k elements, indices i + 1 through i + k.
  • If you skip an element, you just move on to the next index.

Find the largest total you can get, and the elements you selected to get it. You may select no elements at all, which gives a total of 0. If k is at least the length of arr, at most one element can be selected.

Return an array with two rows. Row 0 holds only the maximum total. Row 1 holds the selected elements in the order of their indices.

If several selections give the maximum total, use this rule: go from left to right, and select the element at index i only if selecting it (and then choosing the rest as well as possible) gives a strictly larger total than skipping it. On a tie, skip it. So an element that is 0 or negative is never selected.

Function

maxSumWithSkips(arr: int[], k: int) → int[][]

Examples

Example 1

arr = [5,1,2,10,6,2]k = 1return = [[17],[5,10,2]]

Select 5 at index 0 (index 1 is skipped), 10 at index 3 (index 4 is skipped) and 2 at index 5. The total is 5 + 10 + 2 = 17, and no other selection reaches 17.

Example 2

arr = [1,8,3,9,4,6]k = 2return = [[14],[8,6]]

Selecting 8 at index 1 blocks indices 2 and 3. After that, 6 at index 5 is better than 4 at index 4: 8 + 6 = 14. A selection with 9 at index 3 can add only 1 at index 0, for a total of 10.

Example 3

arr = [3,5,2]k = 1return = [[5],[5]]

Both 3 and 2 (indices 0 and 2) and 5 alone (index 1) give a total of 5. At index 0, selecting 3 gives 3 + 2 = 5 and skipping it also gives 5. That is a tie, so index 0 is skipped, and then 5 is selected.

Example 4

arr = [-4,0,-1]k = 0return = [[0],[]]

Every element is 0 or negative, so the best total is 0, reached by selecting nothing. Selecting the 0 would only tie with skipping it, so it is skipped too.

Constraints

  • 1 <= arr.length <= 10^4
  • -10^4 <= arr[i] <= 10^4
  • 0 <= k <= 10^4

More PhonePe interview questions

  • Minimum Number of Days to Make m BouquetsONSITE INTERVIEW · Seen Dec 2026
  • Evaluate DivisionONSITE INTERVIEW · Seen Oct 2026
  • K-th Smallest Value in a Sorted MatrixONSITE INTERVIEW · Seen Oct 2026
  • Identify Two Failing Tests From an Oracle TranscriptONSITE INTERVIEW · Seen Sep 2026
  • Count Bowl SubarraysONSITE INTERVIEW · Seen Jul 2026
  • Reconstruct ItineraryONSITE INTERVIEW · Seen Jul 2026
  • Constrained Jump SequencesOA · Seen Aug 2026
  • Maximum Payout with Context SwitchesOA · Seen Aug 2026
  • Substrings Without a Target SubsequenceOA · Seen Aug 2026
  • Luna and the Colorful SocksOA · Seen Jul 2026

Similar questions at other companies

  • Assign Escalations to TeamsAmazon · ONSITE INTERVIEW
  • Distinct Subset SumsGoogle · ONSITE INTERVIEWGoogle interview questions
  • Count Subsets With a Target SumSafe Security · ONSITE INTERVIEW
  • Maximum Subarray Sum with One DeletionOracle · ONSITE INTERVIEWOracle interview questions
  • Best Time to Buy and Sell Stock IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
  • Coin ChangeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Maximum SubarrayUpstart · ONSITE INTERVIEWUpstart interview questions
  • Smallest Sufficient TeamExpedia · ONSITE INTERVIEWExpedia interview questions
  • Maximum Product SubarrayHartford Financial Services · ONSITE INTERVIEWHartford Financial Services interview questions
  • Word BreakMoveworks · ONSITE INTERVIEWMoveworks interview questions
All PhonePe interview questionsSee PhonePe hiring insights
Saved
class Solution {
    public int[][] maxSumWithSkips(int[] arr, int k) {
      // Write your code here.
    }

}
Checking account…