Advertisement

Greatest Sum Divisible by Three - LeetCode 1262 Solution

Greatest Sum Divisible by Three - Complete Solution Guide

Greatest Sum Divisible by Three is LeetCode problem 1262, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an integer array nums , return the maximum possible sum of elements of the array such that it is divisible by three . Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3). C

Detailed Explanation

The problem asks us to find the maximum possible sum of elements from a given integer array `nums` such that the sum is divisible by 3. We are allowed to pick any combination of numbers from the array. If no such combination exists (i.e., no subset sums to a multiple of 3), we should return 0.

Solution Approach

The solution uses dynamic programming to efficiently track the maximum possible sums for each remainder (0, 1, and 2) when dividing by 3. We initialize a `dp` array of size 3 with all elements set to 0. `dp[i]` will store the maximum possible sum seen so far with remainder `i` when divided by 3. We iterate through the input array `nums`. For each number, we update the `dp` array by considering including the current number in the sums and calculating the new remainders. At the end, `dp[0]` will contain the maximum possible sum that is divisible by 3.

Step-by-Step Algorithm

  1. Step 1: Initialize a `dp` array of size 3 to [0, 0, 0]. `dp[i]` represents the maximum sum with remainder `i` when divided by 3.
  2. Step 2: Iterate through each number `num` in the input array `nums`.
  3. Step 3: Create a copy of the `dp` array (e.g., `dp_copy`) to avoid modifying it directly during the inner loop. This is crucial because updates to `dp` based on previous values in `dp` are performed.
  4. Step 4: Iterate through each element `s` in `dp_copy`.
  5. Step 5: Calculate the new sum: `new_sum = s + num`.
  6. Step 6: Calculate the remainder when `new_sum` is divided by 3: `remainder = new_sum % 3`.
  7. Step 7: Update `dp[remainder]` to the maximum of its current value and `new_sum`: `dp[remainder] = max(dp[remainder], new_sum)`.
  8. Step 8: After iterating through all numbers in `nums`, return `dp[0]`, which represents the maximum sum divisible by 3.

Key Insights

  • Insight 1: We only care about the remainders when dividing each number by 3. A number can only have remainders 0, 1, or 2 when divided by 3.
  • Insight 2: Dynamic programming can be used to keep track of the maximum sum achievable for each possible remainder (0, 1, 2) when iterating through the array.
  • Insight 3: We don't need to store all intermediate sums; we only need to store the largest sum seen so far for each remainder (0, 1, 2).

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Dynamic Programming, Greedy, Sorting.

Companies

Asked at: DE Shaw.