Given an integer array arr of size N. The task is to find the maximum number of splits such that each split has sum divisible by 3. It is not necessary that all splits are divisible by 3, the task is to just maximize the number of splits which are divisible by 3.
Examples:
Input: arr [] = [2, 36, 1, 9, 2, 0, 1, 8, 1]
Output: 4
Explanation
The array can be splited into 4 parts:
[2, 36, 1] Sum = 39
[9] Sum = 9
[2, 0, 1] Sum = 3
[8, 1] Sum = 9
All splits are divisible by 3 and there cannot be more than 4 splits which are divisible by 3.Input: arr [] = [40, 40, 40, 5]
Output: 1
Explanation:
Array can be splits into only two parts
[40, 40] Sum = 80.
[40, 5] Sum = 45.
The sum of the second split is divisible by 3 hence only one split is divisible by 3 so the output is 1.
Approach
One observation that can be made easily is that it is easier to work with the array if we take modulo of every element by 3. Because it would not have any effect in the splits. Now the problem can be solved using Dynamic Programming .
- Let dp[i] signifies maximum number of splits at position i .
- Then calculate the prefix sums modulo 3.
- So if a segment has sum divisible by 3 and its left and right prefix sum will also be same.
- Another thing to notice is that prefix sums modulo 3 can be either 0, 1 or 2 . So if the current prefix sum modulo 3 is 1. Choose the left pointer as the right most index which has prefix sum 1. Or ignore the segment and move on.
dp[i] = max( dp[right most index from 0 to i with prefix sum same as i] + 1, dp[i-1])
dp[i-1] means we are not considering i as right pointer of some segment .
dp[right most index from 0 to i with prefix sum same as i]+1 means i is a right pointer of the segment and total number of splits will be the total number of splits at the left pointer of the segment + 1 (for this segment) .
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include<bits/stdc++.h> using namespace std; int calculate_maximum_splits(int arr[], int N) { // Prefix array storing right most // index with prefix sums 0, 1, 2 int pre[] = { 0, -1, -1 }; // dp array int dp[N]; memset(dp, 0, sizeof(dp)); // Current prefix sum int C = 0; for(int i = 0; i < N; i++) { C = C + arr[i]; // Calculating the prefix sum modulo 3 C = C % 3; // We dont have a left pointer // with prefix sum C if (pre[C] == -1) { dp[i] = dp[i - 1]; // We cannot consider i as // a right pointer of any segment } else { // We have a left pointer // pre[C] with prefix sum C dp[i] = max(dp[i - 1], dp[pre[C]] + 1); } // i is the rightmost index of // prefix sum C pre[C] = i; } return dp[N - 1]; } // Driver code int main() { int arr[] = { 2, 36, 1, 9, 2, 0, 1, 8, 1 }; int N = sizeof(arr) / sizeof(arr[0]); cout << (calculate_maximum_splits(arr, N)); } // This code is contributed by chitranayal |
Java
// Java implementation of the above appraoch import java.util.*; class GFG{ static int calculate_maximum_splits(int arr[], int N) { // Prefix array storing right most // index with prefix sums 0, 1, 2 int pre[] = { 0, -1, -1 }; // dp array int[] dp = new int[N]; Arrays.fill(dp, 0); // Current prefix sum int C = 0; for(int i = 0; i < N; i++) { C = C + arr[i]; // Calculating the prefix sum modulo 3 C = C % 3; // We dont have a left pointer // with prefix sum C if (pre[C] == -1) { if(1 <= i) dp[i] = dp[i - 1]; // We cannot consider i as // a right pointer of any segment } else { // We have a left pointer // pre[C] with prefix sum C dp[i] = Math.max(dp[i - 1], dp[pre[C]] + 1); } // i is the rightmost index of // prefix sum C pre[C] = i; } return dp[N - 1]; } // Driver code public static void main(String[] args) { int arr[] = { 2, 36, 1, 9, 2, 0, 1, 8, 1 }; int N = arr.length; System.out.println(calculate_maximum_splits(arr, N)); } } // This code is contributed by offbeat |
Python3
# Python3 program for above approach def calculate_maximum_splits(arr, N): # prefix array storing right most # index with prefix sums 0, 1, 2 pre =[0, -1, -1] # dp array dp =[0 for i in range(N)] # current prefix sum C = 0 for i in range(N): C = C + arr[i] # Calculating the prefix sum modulo 3 C = C % 3 # We dont have a left pointer # with prefix sum C if pre[C]==-1: dp[i]= dp[i-1] # We cannot consider i as # a right pointer of any segment else: # We have a left pointer # pre[C] with prefix sum C dp[i]= max(dp[i-1], dp[pre[C]]+1) # i is the rightmost index of # prefix sum C pre[C]= i return dp[N-1] # Driver code arr = [2, 36, 1, 9, 2, 0, 1, 8, 1] N = len(arr) print(calculate_maximum_splits(arr, N)) |
C#
// C# implementation of the above appraoch using System; class GFG{ static int calculate_maximum_splits(int []arr, int N) { // Prefix array storing right most // index with prefix sums 0, 1, 2 int []pre = { 0, -1, -1 }; // dp array int[] dp = new int[N]; for(int i = 0; i < N; i++) { dp[i] = 0; } // Current prefix sum int C = 0; for(int i = 0; i < N; i++) { C = C + arr[i]; // Calculating the prefix sum modulo 3 C = C % 3; // We dont have a left pointer // with prefix sum C if (pre[C] == -1) { if(1 <= i) dp[i] = dp[i - 1]; // We cannot consider i as // a right pointer of any segment } else { // We have a left pointer // pre[C] with prefix sum C dp[i] = Math.Max(dp[i - 1], dp[pre[C]] + 1); } // i is the rightmost index of // prefix sum C pre[C] = i; } return dp[N - 1]; } // Driver code public static void Main(string[] args) { int []arr = { 2, 36, 1, 9, 2, 0, 1, 8, 1 }; int N = arr.Length; Console.Write(calculate_maximum_splits(arr, N)); } } // This code is contributed by rutvik_56 |
4
Time Complexity: O (N)
Auxiliary Space: O (N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Maximum splits in binary string such that each substring is divisible by given odd number
- Count possible splits of sum N into K integers such that the minimum is at least P
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Maximum number of splits of a binary number
- Minimum splits required to convert a number into prime segments
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Rearrange Array to maximize number having Array elements as digits based on given conditions
- Maximize count of pairs (i, j) from two arrays having element from first array not exceeding that from second array
- Maximize the number of sum pairs which are divisible by K
- Count number of pairs in array having sum divisible by K | SET 2
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Maximize subsequences having array elements not exceeding length of the subsequence
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Count all sub-arrays having sum divisible by k
- Count sub-matrices having sum divisible 'k'
- Largest rectangular sub-matrix having sum divisible by k
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

