3780. Maximum Sum of Three Numbers Divisible by Three Medium
1/**
2 * [3780] Maximum Sum of Three Numbers Divisible by Three
3 *
4 * You are given an integer array nums.
5 * Your task is to choose exactly three integers from nums such that their sum is divisible by three.
6 * Return the maximum possible sum of such a triplet. If no such triplet exists, return 0.
7 *
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">nums = [4,2,3,1]</span>
11 * Output: <span class="example-io">9</span>
12 * Explanation:
13 * The valid triplets whose sum is divisible by 3 are:
14 *
15 * (4, 2, 3) with a sum of 4 + 2 + 3 = 9.
16 * (2, 3, 1) with a sum of 2 + 3 + 1 = 6.
17 *
18 * Thus, the answer is 9.
19 * </div>
20 * <strong class="example">Example 2:
21 * <div class="example-block">
22 * Input: <span class="example-io">nums = [2,1,5]</span>
23 * Output: <span class="example-io">0</span>
24 * Explanation:
25 * No triplet forms a sum divisible by 3, so the answer is 0.
26 * </div>
27 *
28 * Constraints:
29 *
30 * 3 <= nums.length <= 10^5
31 * 1 <= nums[i] <= 10^5
32 *
33 */
34pub struct Solution {}
35
36// problem: https://leetcode.com/problems/maximum-sum-of-three-numbers-divisible-by-three/
37// discuss: https://leetcode.com/problems/maximum-sum-of-three-numbers-divisible-by-three/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41impl Solution {
42 pub fn maximum_sum(nums: Vec<i32>) -> i32 {
43 0
44 }
45}
46
47// submission codes end
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52
53 #[test]
54 fn test_3780() {
55 }
56}
57Back
© 2026 bowen.ge All Rights Reserved.