Maximize sum of array by repeatedly removing an element from pairs whose concatenation is a multiple of 3
Last Updated :
04 Apr, 2023
Given an array arr[] consisting of N positive integers, the task is to find the maximum possible sum of the remaining array elements after repeatedly removing any of the two elements whose concatenation is a multiple of 3.
Examples:
Input: arr[] = {23, 12, 43, 3, 56}
Output: 91
Explanation:
Initially the array is {23, 12, 43, 3, 56}. Following removal of array elements are performed:
- Pair {23, 43}: Concatenation = 2343, which is divisible by 3. Now, removing 43 modifies the array to {23, 12, 3, 56}.
- Pair {12, 3}: Concatenation = 123, which is divisible by 3. Now, removing 3 modifies the array to {23, 12, 56}.
After the above operations, sum of the array elements = 12 + 56 + 23 = 91.
Input: arr[] = {324, 32, 53, 67, 330}
Output: 415
Approach: The given problem can be solved by using the fact that the remainder of a number, when divided by 3, is equal to the remainder of the sum of its digits when divided by 3. Follow the steps below to solve the problem:
- Initialize three variables, say maxRem0, maxRem1, and maxRem2, to store the element having remainder as 0, 1, and 2 respectively.
- Traverse the given array arr[] and perform the following steps:
- Initialize a variable, digitSum to store the digit sum.
- If digitSum % 3 == 0, then update the value of maxRem0 as max(maxRem0, arr[i]).
- Otherwise, if the remainder is 1 or 2, then
- After completing the above steps, print the sum of maxRem0 and the maximum value of maxRem1 and maxRem2 as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getSum( int n)
{
int ans = 0;
string arr = to_string(n);
for ( int i = 0; i < arr.length(); i++)
{
ans += int (arr[i]);
}
return ans;
}
void getMax( int arr[], int n)
{
int maxRem0 = 0;
int rem1 = 0;
int rem2 = 0;
for ( int i = 0; i < n; i++)
{
int digitSum = getSum(arr[i]);
if (digitSum % 3 == 0)
maxRem0 = max(maxRem0, arr[i]);
else if (digitSum % 3 == 1)
rem1 += arr[i];
else
rem2 += arr[i];
}
cout << (maxRem0 + max(rem1, rem2));
}
int main()
{
int arr[] = { 23, 12, 43, 3, 56 };
int n = sizeof (arr) / sizeof (arr[0]);
getMax(arr, n);
}
|
Java
class GFG{
static int getSum( int n)
{
int ans = 0 ;
char [] arr = (String.valueOf(n)).toCharArray();
for ( char ch : arr)
{
ans += Character.getNumericValue(ch);
}
return ans;
}
static void getMax( int [] arr)
{
int maxRem0 = 0 ;
int rem1 = 0 ;
int rem2 = 0 ;
for ( int i:arr)
{
int digitSum = getSum(i);
if (digitSum % 3 == 0 )
maxRem0 = Math.max(maxRem0, i);
else if (digitSum % 3 == 1 )
rem1 += i;
else
rem2 += i;
}
System.out.print(maxRem0 +
Math.max(rem1, rem2));
}
public static void main(String[] args)
{
int [] arr = { 23 , 12 , 43 , 3 , 56 };
getMax(arr);
}
}
|
Python3
def getSum(n):
ans = 0
for i in str (n):
ans + = int (i)
return ans
def getMax(arr):
maxRem0 = 0
rem1 = 0
rem2 = 0
for i in arr:
digitSum = getSum(i)
if digitSum % 3 = = 0 :
maxRem0 = max (maxRem0, i)
elif digitSum % 3 = = 1 :
rem1 + = i
else :
rem2 + = i
print ( maxRem0 + max (rem1, rem2))
arr = [ 23 , 12 , 43 , 3 , 56 ]
getMax(arr)
|
C#
using System;
class GFG{
static int getSum( int n)
{
int ans = 0;
string str = n.ToString();
Char[] arr = str.ToCharArray();
foreach (Char ch in arr)
{
ans += ( int )Char.GetNumericValue(ch);
}
return ans;
}
static void getMax( int [] arr)
{
int maxRem0 = 0;
int rem1 = 0;
int rem2 = 0;
foreach ( int i in arr)
{
int digitSum = getSum(i);
if (digitSum % 3 == 0)
maxRem0 = Math.Max(maxRem0, i);
else if (digitSum % 3 == 1)
rem1 += i;
else
rem2 += i;
}
Console.WriteLine(maxRem0 +
Math.Max(rem1, rem2));
}
static void Main()
{
int [] arr = { 23, 12, 43, 3, 56 };
getMax(arr);
}
}
|
Javascript
<script>
function getSum(n) {
let ans = 0;
let arr = n.toString();
for (let i = 0; i < arr.length; i++)
{
ans += arr[i];
}
return ans;
}
function getMax(arr, n) {
let maxRem0 = 0;
let rem1 = 0;
let rem2 = 0;
for (let i = 0; i < n; i++) {
let digitSum = getSum(arr[i]);
if (digitSum % 3 == 0)
maxRem0 = Math.max(maxRem0, arr[i]);
else if (digitSum % 3 == 1)
rem1 += arr[i];
else
rem2 += arr[i];
}
document.write(maxRem0 + Math.max(rem1, rem2))
}
let arr = [23, 12, 43, 3, 56];
let n = arr.length;
getMax(arr, n);
</script>
|
Time Complexity: O(n * k), where n is the size of the array and k is the maximum number of digits in an array element.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...