Maximum Sum of Exactly Three Non-Adjacent Elements
Updated
Problem statement
Given an integer array nums, choose exactly three indices i < j < k such that j - i >= 2 and k - j >= 2.
Return the largest possible sum nums[i] + nums[j] + nums[k]. You must choose exactly three elements, even when all values are negative.
Function
maximumThreeSum(nums: int[]) → longExamples
Example 1
nums = [4,100,7,100,9]return = 20The only valid triple uses indices 0, 2 and 4, giving 4 + 7 + 9 = 20.
Example 2
nums = [-5,-1,-2,-1,-3,-2]return = -4Choose indices 1, 3 and 5: -1 + -1 + -2 = -4. Exactly three elements are required.
Example 3
nums = [5,1,10,1,5,1,10]return = 25Indices 2, 4 and 6 give 10 + 5 + 10 = 25.
Constraints
- 5 <= nums.length <= 100000.
- -1000000000 <= nums[i] <= 1000000000.
More LSEG Online Assessment questions
- Count Times Using at Most Two DigitsOA · Seen Jul 2025
- Maximum Word Count in a SentenceOA · Seen Jul 2025
- Minimum Digit Swaps for the Smallest DifferenceOA · Seen Jul 2025
- Smallest String After at Most One DeletionOA · Seen Jul 2025
- Delete One Digit 5 to Maximize the NumberOA · Seen Feb 2025
- Three-Part Partition with Minimum Largest RangeOA · Seen Feb 2025
- Transform a String with at Most One OperationOA · Seen Feb 2025
- Price Equity Options and ForwardsOA · Seen Dec 2024
- Maximum Sum of Non-Adjacent ElementsOA · Seen Sep 2024
- Return the Sentence with the Most WordsOA · Seen Sep 2024
Similar questions at other companies
- 0/1 KnapsackConfluent · OAConfluent interview questions
- Minimum Cost to Tile a Strip with Widths 1, 2, and 3Infosys · OAInfosys interview questions
- Maximum Escape Game ScoreMicrosoft · OAMicrosoft interview questions
- Maximum Subarray SumStraive · OA
- Longest Increasing Price SubsequenceOpenAI · OAOpenAI interview questions
- Lowest-Cost Shirt Color ScheduleElevenLabs · OAElevenLabs interview questions
- Maximum Game Board ScoreJP Morgan Chase · OAJP Morgan Chase interview questions
- API Bandwidth AllocationArista Networks · OAArista Networks interview questions
- Maximum Points by Deleting ElementsIBM · OAIBM interview questions
- House RobberWalmart · OAWalmart interview questions