给定一个由N 个正整数组成的数组arr[] ,任务是在重复删除串联是3的倍数的两个元素中的任何一个后,找到剩余数组元素的最大可能总和。
例子:
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
方法:给定的问题可以通过使用以下事实来解决:一个数的余数除以3 时,等于其数字之和除以3时的余数。 请按照以下步骤解决问题:
- 初始化三个变量,比如maxRem0 、 maxRem1和maxRem2 ,分别存储余数为0 、 1和2的元素。
- 遍历给定的数组arr[]并执行以下步骤:
- 初始化变量digitSum以存储数字总和。
- 如果digitSum % 3 == 0 ,则将maxRem0的值更新为max(maxRem0, arr[i]) 。
- 否则,如果余数是1或2 ,则
- 完成上述步骤后,打印maxRem0与maxRem1和maxRem2的最大值之和作为结果。
下面是上述方法的实现:
C++
// C++ approach for the above approach
#include
using namespace std;
// Function to calculate sum
// of digits of an integer
int getSum(int n)
{
int ans = 0;
// char[] arr = (String.valueOf(n)).toCharArray();
string arr = to_string(n);
for(int i = 0; i < arr.length(); i++)
{
ans += int(arr[i]);
}
return ans;
}
// Function to calculate maximum sum
// of array after removing pairs whose
// concatenation is divisible by 3
void getMax(int arr[], int n)
{
// Stores the sum of
// digits of array element
int maxRem0 = 0;
int rem1 = 0;
int rem2 = 0;
for(int i = 0; i < n; i++)
{
// Find the sum of digits
int digitSum = getSum(arr[i]);
// If i is divisible by 3
if (digitSum % 3 == 0)
maxRem0 = max(maxRem0, arr[i]);
// Otherwise, if i modulo 3 is 1
else if (digitSum % 3 == 1)
rem1 += arr[i];
// Otherwise, if i modulo 3 is 2
else
rem2 += arr[i];
}
// Return the resultant
// sum of array elements
cout << (maxRem0 + max(rem1, rem2));
}
// Driver Code
int main()
{
int arr[] = { 23, 12, 43, 3, 56 };
int n = sizeof(arr) / sizeof(arr[0]);
getMax(arr, n);
}
// This code is contributed by ukasp
Java
// Java approach for the above approach
class GFG{
// Function to calculate sum
// of digits of an integer
static int getSum(int n)
{
int ans = 0;
char[] arr = (String.valueOf(n)).toCharArray();
for(char ch : arr)
{
ans += Character.getNumericValue(ch);
}
return ans;
}
// Function to calculate maximum sum
// of array after removing pairs whose
// concatenation is divisible by 3
static void getMax(int[] arr)
{
// Stores the sum of
// digits of array element
int maxRem0 = 0;
int rem1 = 0;
int rem2 = 0;
for(int i:arr)
{
// Find the sum of digits
int digitSum = getSum(i);
// If i is divisible by 3
if (digitSum % 3 == 0)
maxRem0 = Math.max(maxRem0, i);
// Otherwise, if i modulo 3 is 1
else if (digitSum % 3 == 1)
rem1 += i;
// Otherwise, if i modulo 3 is 2
else
rem2 += i;
}
// Return the resultant
// sum of array elements
System.out.print(maxRem0 +
Math.max(rem1, rem2));
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 23, 12, 43, 3, 56 };
getMax(arr);
}
}
// This code is contributed by abhinavjain194
Python3
# Python3 program for the above approach
# Function to calculate sum
# of digits of an integer
def getSum(n):
ans = 0
for i in str(n):
ans += int(i)
return ans
# Function to calculate maximum sum
# of array after removing pairs whose
# concatenation is divisible by 3
def getMax(arr):
# Stores the sum of
# digits of array element
maxRem0 = 0
rem1 = 0
rem2 = 0
for i in arr:
# Find the sum of digits
digitSum = getSum(i)
# If i is divisible by 3
if digitSum % 3 == 0:
maxRem0 = max(maxRem0, i)
# Otherwise, if i modulo 3 is 1
elif digitSum % 3 == 1:
rem1 += i
# Otherwise, if i modulo 3 is 2
else:
rem2 += i
# Return the resultant
# sum of array elements
print( maxRem0 + max(rem1, rem2))
# Driver Code
# Given array
arr = [ 23, 12, 43, 3, 56 ]
getMax(arr)
C#
// C# program for the above approach
using System;
class GFG{
// Function to calculate sum
// of digits of an integer
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;
}
// Function to calculate maximum sum
// of array after removing pairs whose
// concatenation is divisible by 3
static void getMax(int[] arr)
{
// Stores the sum of
// digits of array element
int maxRem0 = 0;
int rem1 = 0;
int rem2 = 0;
foreach(int i in arr)
{
// Find the sum of digits
int digitSum = getSum(i);
// If i is divisible by 3
if (digitSum % 3 == 0)
maxRem0 = Math.Max(maxRem0, i);
// Otherwise, if i modulo 3 is 1
else if (digitSum % 3 == 1)
rem1 += i;
// Otherwise, if i modulo 3 is 2
else
rem2 += i;
}
// Return the resultant
// sum of array elements
Console.WriteLine(maxRem0 +
Math.Max(rem1, rem2));
}
// Driver Code
static void Main()
{
int[] arr = { 23, 12, 43, 3, 56 };
getMax(arr);
}
}
// This code is contributed by souravghosh0416.
Javascript
输出:
91
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live