Dados tres números enteros, C0, C1 y C2 frecuencias de 0s, 1s y 2s en un grupo S . La tarea es encontrar el número máximo de grupos que tienen la suma divisible por 3 , la condición es que la suma (S) sea divisible por 3 y la unión de todos los grupos debe ser igual a S
Ejemplos:
Entrada : C0 = 2, C1 = 4, C2 = 1
Salida : 4
Explicación : puede dividir el grupo S = {0, 0, 1, 1, 1, 1, 2} en cuatro grupos {0}, {0} , {1, 1, 1}, {1, 2}. Se puede probar que 4 es la máxima respuesta posible.Entrada : C0 = 250, C1 = 0, C2 = 0
Salida : 250
Enfoque: Este problema se puede resolver usando el Algoritmo Codicioso . siga los pasos que se indican a continuación para resolver el problema.
- Inicialice una variable maxAns , digamos 0, para almacenar la cantidad máxima de grupos.
- Agregue C0 a maxAns , porque cada {0} puede ser un grupo tal que la suma sea divisible por 3 .
- Inicialice una variable k , digamos min(C1, C2) , y agréguela a maxAns, porque se puede crear al menos k , {1, 2} grupo.
- Agregue abs(C1-C2) /3 a maxAns , contribuirá con los 1 o 2 restantes .
- Devuelve maxAns.
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ program for above approach #include <bits/stdc++.h> using namespace std; int maxGroup(int c0, int c1, int c2) { // Initializing to store maximum number of groups int maxAns = 0; // Adding C0 maxAns += c0; // Taking Minimum of c1, c2 as minimum number of // pairs must be minimum of c1, c2 int k = min(c1, c2); maxAns += k; // If there is any remaining element in c1 or c2 // then it must be the absolute difference of c1 and // c2 and dividing it by 3 to make it one pair maxAns += abs(c1 - c2) / 3; return maxAns; } int main() { int C0 = 2, C1 = 4, C2 = 1; cout << maxGroup(C0, C1, C2); return 0; } // This code is contributed by maddler
Java
// Java program for above approach import java.io.*; class GFG { // Function to calculate maximum number of groups public static int maxGroup(int c0, int c1, int c2) { // Initializing to store maximum number of groups int maxAns = 0; // Adding C0 maxAns += c0; // Taking Minimum of c1, c2 as minimum number of // pairs must be minimum of c1, c2 int k = Math.min(c1, c2); maxAns += k; // If there is any remaining element in c1 or c2 // then it must be the absolute difference of c1 and // c2 and dividing it by 3 to make it one pair maxAns += Math.abs(c1 - c2) / 3; return maxAns; } // Driver Code public static void main(String[] args) { // Given Input int C0 = 2, C1 = 4, C2 = 1; // Function Call System.out.println(maxGroup(C0, C1, C2)); } }
Python3
# python 3 program for above approach def maxGroup(c0, c1, c2): # Initializing to store maximum number of groups maxAns = 0 # Adding C0 maxAns += c0 # Taking Minimum of c1, c2 as minimum number of # pairs must be minimum of c1, c2 k = min(c1, c2) maxAns += k # If there is any remaining element in c1 or c2 # then it must be the absolute difference of c1 and # c2 and dividing it by 3 to make it one pair maxAns += abs(c1 - c2) // 3 return maxAns # Driver code if __name__ == '__main__': C0 = 2 C1 = 4 C2 = 1 print(maxGroup(C0, C1, C2)) # This code is contributed by ipg2016107.
C#
// C# program for above approach using System; class GFG{ // Function to calculate maximum number of groups public static int maxGroup(int c0, int c1, int c2) { // Initializing to store maximum number // of groups int maxAns = 0; // Adding C0 maxAns += c0; // Taking Minimum of c1, c2 as minimum number // of pairs must be minimum of c1, c2 int k = Math.Min(c1, c2); maxAns += k; // If there is any remaining element // in c1 or c2 then it must be the // absolute difference of c1 and c2 // and dividing it by 3 to make it one pair maxAns += Math.Abs(c1 - c2) / 3; return maxAns; } // Driver Code static public void Main() { // Given Input int C0 = 2, C1 = 4, C2 = 1; // Function Call Console.WriteLine(maxGroup(C0, C1, C2)); } } // This code is contributed by maddler
Javascript
<script> // JavaScript program for the above approach; function maxGroup(c0, c1, c2) { // Initializing to store maximum number of groups let maxAns = 0; // Adding C0 maxAns += c0; // Taking Minimum of c1, c2 as minimum number of // pairs must be minimum of c1, c2 let k = Math.min(c1, c2); maxAns += k; // If there is any remaining element in c1 or c2 // then it must be the absolute difference of c1 and // c2 and dividing it by 3 to make it one pair maxAns += Math.abs(c1 - c2) / 3; return maxAns; } let C0 = 2, C1 = 4, C2 = 1; document.write(maxGroup(C0, C1, C2)); // This code is contributed by Potta Lokesh </script>
4
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por rajatdubey179 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA