📌  相关文章
📜  通过重复从串联为 3 的倍数的对中删除元素来最大化数组的总和

📅  最后修改于: 2021-09-06 05:55:20             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[] ,任务是在重复删除串联是3的倍数的两个元素中的任何一个后,找到剩余数组元素的最大可能总和。

例子:

方法:给定的问题可以通过使用以下事实来解决:一个数的余数除以3 时,等于其数字之和除以3时的余数。 请按照以下步骤解决问题:

  • 初始化三个变量,比如maxRem0maxRem1maxRem2 ,分别存储余数为012的元素。
  • 遍历给定的数组arr[]并执行以下步骤:
    • 初始化变量digitSum以存储数字总和。
    • 如果digitSum % 3 == 0 ,则将maxRem0的值更新为max(maxRem0, arr[i])
    • 否则,如果余数是12 ,则
  • 完成上述步骤后,打印maxRem0maxRem1maxRem2的最大值之和作为结果。

下面是上述方法的实现:

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