Maximum Sum of Three Numbers Divisible by Three

You are given an integer array nums.

Your task is to choose exactly three integers from nums such that their sum is divisible by three.

Return the maximum possible sum of such a triplet. If no such triplet exists, return 0.

Example 1
Inputnums = [4,2,3,1]
Output9
The valid triplets divisible by 3 have sums 9 and 6, so the maximum is 9.
Example 2
Inputnums = [2,1,5]
Output0
No triplet forms a sum divisible by 3, so the answer is 0.

Constraints

  • 3 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^5

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate