Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Maximum number of pair reductions possible on a given triplet

  • Last Updated : 12 May, 2021

Given a triplet of integers (A, B, C), the task is to count the maximum number of decrements by 1 that can be performed on positive pairs of the given triplet.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Input: A = 4, B = 3, C = 2
Output: 4
Explanation:
Operation 1: Reduce the pair (4, 3). Therefore, the triplet reduces to {3, 2, 2}.
Operation 2: Reduce the pair (3, 2). Therefore, the triplet reduces to {2, 1, 2}.
Operation 3: Reduce the pair (2, 2). Therefore, the triplet reduces to {1, 1, 1}.
Operation 3: Reduce the pair (1, 1). Therefore, the triplet reduces to {0, 0, 1}.
No further operations are possible.

Input: A = 7, B = 9, C = 6
Output: 11



 

Approach: The idea is to use Greedy Approach to solve the problem. Follow the steps below to solve the problem:

  • Store the triplet in an array.
  • Initialize a variable, say count, to store the maximum possible reductions that can be performed on the triplet.
  • Iterate a loop until the first two array elements are reduced to 0 and perform the following operations:
  • Print the value of count as the required answer.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
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.

Javascript




<script>
 
// JavaScript program for the above approach
 
// Function to count the maximum
// number of pair reductions
// possible on a given triplet
function maxOps(a, b, c)
{
 
    // Convert them into an array
    let arr = [ a, b, c ];
 
    // Stores count of operations
    let count = 0;
 
    while (1) {
 
        // Sort the array
        arr.sort();
 
        // 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
    document.write(count);
}
 
// Driver Code
    // Given triplet
    let a = 4, b = 3, c = 2;
    maxOps(a, b, c);
 
 
// This code is contributed by Surbhi Tyagi.
 
</script>
Output: 
4

 

Time Complexity: O(max(arr[i])*(3 * log(3))) 
Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :