FastPrepMaximum Alternating-Index Packet Value

Maximum Alternating-Index Packet Value

Infosys logoInfosys● MediumNEW GRADOA
Learn

Problem statement

You are given nonnegative packet values and an integer budget. Select exactly budget packets. The selected packets must alternate between original 0-based even and odd indices; the first selected packet may come from either parity.

The selected values may be arranged in any order that satisfies the parity alternation. Return the maximum possible total value.

Function

maximumAlternatingPacketValue(packetValues: int[], budget: int) → long

Examples

Example 1

packetValues = [8,1,7,5,4,9]budget = 4return = 29

Choose even-index values 8 and 7 plus odd-index values 9 and 5.

Example 2

packetValues = [8,1,7,5,4,9]budget = 5return = 33

Starting with an even index permits three even-index and two odd-index packets.

Constraints

  • 1 <= packetValues.length <= 200000
  • 0 <= packetValues[i] <= 10^9
  • 1 <= budget <= packetValues.length

Source note: Interview prompt excerpt preserved as source evidence.

More Infosys problems

See Infosys hiring insights
public long maximumAlternatingPacketValue(int[] packetValues, int budget) {
    // Write your code here.
}
packetValues[8,1,7,5,4,9]
budget4
expected29
Checking account…