📜  给定三元组上可能减少的最大对数

📅  最后修改于: 2021-04-17 19:25:31             🧑  作者: Mango

给定一个三元组整数(A,B,C) ,任务是计算可以对给定三元组的正对执行的最大减1数。

例子:

方法:想法是使用贪婪方法解决问题。请按照以下步骤解决问题:

  • 将三元组存储在数组中。
  • 初始化一个变量,例如count ,以存储可以在三元组上执行的最大可能减少量。
  • 迭代循环,直到前两个数组元素减少为0并执行以下操作:
    • 按升序对数组进行排序。
    • 选择两个最大数组元素,并将它们减少1
    • 增量计数1
  • count的值打印为必需的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the maximum
// number of pair reductions
// possible on a given triplet
void maxOps(int a, int b, int c)
{
 
    // Convert them into an array
    int arr[] = { a, b, c };
 
    // Stores count of operations
    int count = 0;
 
    while (1) {
 
        // Sort the array
        sort(arr, arr + 3);
 
        // If the first two array
        // elements reduce to 0
        if (!arr[0] && !arr[1])
            break;
 
        // Apply the operations
        arr[1] -= 1;
        arr[2] -= 1;
 
        // Increment count
        count += 1;
    }
 
    // Print the maximum count
    cout << count;
}
 
// Driver Code
int main()
{
    // Given triplet
    int a = 4, b = 3, c = 2;
    maxOps(a, b, c);
    return 0;
}
 
// This code is contributed by subhammahato348.


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
  // Function to count the maximum
  // number of pair reductions
  // possible on a given triplet
  static void maxOps(int a, int b, int c)
  {
 
    // Convert them into an array
    int arr[] = { a, b, c };
 
    // Stores count of operations
    int count = 0;
    while (1 != 0)
    {
 
      // Sort the array
      Arrays.sort(arr);
 
      // If the first two array
      // elements reduce to 0
      if (arr[0] == 0 && arr[1] == 0)
        break;
 
      // Apply the operations
      arr[1] -= 1;
      arr[2] -= 1;
 
      // Increment count
      count += 1;
    }
 
    // Print the maximum count
    System.out.print(count);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
     
    // Given triplet
    int a = 4, b = 3, c = 2;
    maxOps(a, b, c);
  }
}
 
// This code is contributed by code_hunt.


Python3
# Python3 program for the above approach
 
# Function to count the maximum
# number of pair reductions
# possible on a given triplet
def maxOps(a, b, c):
   
  # Convert them into an array
  arr = [a, b, c]
   
  # Stores count of operations
  count = 0
 
  while True:
     
    # Sort the array
    arr.sort()
     
    # If the first two array
    # elements reduce to 0
    if not arr[0] and not arr[1]:
      break
       
    # Apply the operations
    arr[1] -= 1
    arr[2] -= 1
     
    # Increment count
    count += 1
     
  # Print the maximum count
  print(count)
 
# Given triplet
a, b, c = 4, 3, 2
maxOps(a, b, c)


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to count the maximum
  // number of pair reductions
  // possible on a given triplet
  static void maxOps(int a, int b, int c)
  {
 
    // Convert them into an array
    int[] arr = { a, b, c };
 
    // Stores count of operations
    int count = 0;
    while (1 != 0)
    {
 
      // Sort the array
      Array.Sort(arr);
 
      // If the first two array
      // elements reduce to 0
      if (arr[0] == 0 && arr[1] == 0)
        break;
 
      // Apply the operations
      arr[1] -= 1;
      arr[2] -= 1;
 
      // Increment count
      count += 1;
    }
 
    // Print the maximum count
    Console.WriteLine(count);
  }
 
// Driver Code
public static void Main(String[] args)
{
    // Given triplet
    int a = 4, b = 3, c = 2;
    maxOps(a, b, c);
}
}
 
// This code is contributed by susmitakundugoaldanga.


输出:
4

时间复杂度: O(max(arr [i])*(3 * log(3)))
辅助空间: O(1)