Dados 3 números enteros A, B y C , y un número entero N , la tarea es distribuir N entre los otros 3 números de modo que al final A = B = C . Si la distribución es posible, imprima «Sí», de lo contrario, emita «No».
Ejemplos:
Entrada: A = 5, B = 3, C = 2, N = 8
Salida: Sí
Explicación:
Podemos distribuir N = 8 sumando 1 a A, 3 a B y 4 a C para obtener todos ellos como 6. Por lo tanto la distribución es posible.
Entrada: A = 10, B = 20, C = 15, N = 14
Salida: No
Explicación:
No es posible distribuir N entre los tres enteros para que sean iguales.
Enfoque:
Para resolver el problema mencionado anteriormente, debemos seguir los pasos que se detallan a continuación:
- Encuentre el máximo de los tres enteros A, B y C. Sea el entero K
- Multiplique el entero K por 3 y luego réstelo por la suma de los tres enteros.
- Comprueba si la diferencia de ese número y N es divisible por 3 o no.
- Si es así, la salida es «Sí», de lo contrario, no es posible distribuir el número.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to distribute integer N // among A, B, C such that they become equal #include<bits/stdc++.h> using namespace std; void distributeN(int A, int B, int C, int n) { // Find maximum among the three elements int max1 = max(A, B); int max2 = max(B, C); int maximum = max(max1, max2); // Summation of three elements int sum = A + B + C; int p = (3 * maximum) - sum; int diff = n - p; // Check if difference is divisible by 3 if (diff < 0 || diff % 3) cout << "No"; else cout << "Yes"; } // Driver code int main() { int A = 10, B = 20; int C = 15, n = 14; distributeN(A, B, C, n); return 0; } // This code is contributed by PratikBasu
Java
// Java program to distribute integer N // among A, B, C such that they become equal class GFG{ static void distributeN(int A, int B, int C, int n) { // Find maximum among the three elements int max1 = Math.max(A, B); int max2 = Math.max(B, C); int maximum = Math.max(max1, max2); // Summation of three elements int sum = A + B + C; int p = (3 * maximum) - sum; int diff = n - p; // Check if difference is divisible by 3 if (diff < 0 || diff % 3 == 0) System.out.print("No"); else System.out.print("Yes"); } // Driver code public static void main(String[] args) { int A = 10, B = 20; int C = 15, n = 14; distributeN(A, B, C, n); } } // This code is contributed by Rajput-Ji
Python3
# Python3 Program to Distribute integer N # among A, B, C such that they become equal def distributeN(A, B, C, n): # find maximum among the three elements maximum = max(A, B, C) # summation of three elements sum = A + B+C p = (3 * maximum)-sum diff = n-p # check if difference is divisible by 3 if diff < 0 or diff % 3: print "No" else: print "Yes" # Driver code A = 10 B = 20 C = 15 n = 14 distributeN(A, B, C, n)
C#
// C# program to distribute integer N // among A, B, C such that they become equal using System; class GFG{ static void distributeN(int A, int B, int C, int n) { // Find maximum among the three elements int max1 = Math.Max(A, B); int max2 = Math.Max(B, C); int maximum = Math.Max(max1, max2); // Summation of three elements int sum = A + B + C; int p = (3 * maximum) - sum; int diff = n - p; // Check if difference is divisible by 3 if (diff < 0 || diff % 3 == 0) Console.Write("No"); else Console.Write("Yes"); } // Driver code public static void Main(String[] args) { int A = 10, B = 20; int C = 15, n = 14; distributeN(A, B, C, n); } } // This code is contributed by Rajput-Ji
Javascript
<script> // JavaScript program to distribute integer N // among A, B, C such that they become equal function distributeN(A, B, C, n) { // Find maximum among the three elements let max1 = Math.max(A, B); let max2 = Math.max(B, C); let maximum = Math.max(max1, max2); // Summation of three elements let sum = A + B + C; let p = (3 * maximum) - sum; let diff = n - p; // Check if difference is divisible by 3 if (diff < 0 || diff % 3) document.write("No"); else document.write("Yes"); } // Driver code let A = 10, B = 20; let C = 15, n = 14; distributeN(A, B, C, n); // This code is contributed by Surbhi Tyagi. </script>
No
Complejidad de tiempo: O(1)
Publicación traducida automáticamente
Artículo escrito por vermashivani543 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA