Haciendo tres números iguales con las operaciones dadas

Dados cuatro enteros positivos A , B , C y K . La tarea es verificar si es posible igualar los tres enteros A , B y C con la ayuda de K y hacer que K sea igual a 0 . En una operación, puede restar cualquier valor de K (si sigue siendo mayor que 0 después de la resta) y agregar el nuevo valor a cualquiera de los tres enteros A , B o C .
Ejemplos: 
 

Entrada: A = 6, B = 3, C = 2, K = 7 
Salida: Sí 
Operación 1: Sumar 3 a B y restar 3 a K. 
A = 6, B = 6, C = 2 y K = 4 
Operación 2 : Sume 4 a C y reste 4 de K. 
A = 6, B = 6, C = 6 y K = 0
Entrada: A = 10, B = 20, C = 17, K = 15 
Salida: No 
 

Enfoque: Verifique si es posible igualar los tres números ordenando los tres números y restando el valor de K por la suma de la diferencia del 3er y 2do elemento y el 3er y 1er elemento . Si K sigue siendo mayor que 0 y se puede dividir por igual entre los tres elementos, entonces solo se pueden hacer iguales los tres elementos y K se puede hacer igual a 0 . A continuación se muestra la implementación del enfoque anterior: 

 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function that returns true if a, b and c can
// be made equal with the given operations
bool canBeEqual(int a, int b, int c, int k)
{
    int arr[3];
    arr[0] = a;
    arr[1] = b;
    arr[2] = c;
 
    // Sort the three numbers
    sort(arr, arr + 3);
 
    // Find the sum of difference of the 3rd and
    // 2nd element and the 3rd and 1st element
    int diff = 2 * arr[2] - arr[1] - arr[0];
 
    // Subtract the difference from k
    k = k - diff;
 
    // Check the required condition
    if (k < 0 || k % 3 != 0)
        return false;
 
    return true;
}
 
// Driver code
int main()
{
    int a1 = 6, b1 = 3, c1 = 2, k1 = 7;
 
    if (canBeEqual(a1, b1, c1, k1))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}

Java

// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function that returns true if a, b and c can
// be made equal with the given operations
static boolean canBeEqual(int a, int b, int c, int k)
{
    int []arr = new int[3];
    arr[0] = a;
    arr[1] = b;
    arr[2] = c;
 
    // Sort the three numbers
    Arrays.sort(arr);
 
    // Find the sum of difference of the 3rd and
    // 2nd element and the 3rd and 1st element
    int diff = 2 * arr[2] - arr[1] - arr[0];
 
    // Subtract the difference from k
    k = k - diff;
 
    // Check the required condition
    if (k < 0 || k % 3 != 0)
        return false;
 
    return true;
}
 
// Driver code
public static void main(String[] args)
{
    int a1 = 6, b1 = 3, c1 = 2, k1 = 7;
 
    if (canBeEqual(a1, b1, c1, k1))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by PrinciRaj1992

Python3

# Python3 implementation of the approach
 
# Function that returns true if a, b and c can
# be made equal with the given operations
def canBeEqual(a, b, c, k) :
 
    arr = [0] * 3;
    arr[0] = a;
    arr[1] = b;
    arr[2] = c;
 
    # Sort the three numbers
    arr.sort()
 
    # Find the sum of difference of the 3rd and
    # 2nd element and the 3rd and 1st element
    diff = 2 * arr[2] - arr[1] - arr[0];
 
    # Subtract the difference from k
    k = k - diff;
 
    # Check the required condition
    if (k < 0 or k % 3 != 0) :
        return False;
 
    return True;
 
# Driver code
if __name__ == "__main__" :
 
    a1 = 6; b1 = 3; c1 = 2; k1 = 7;
 
    if (canBeEqual(a1, b1, c1, k1)) :
        print("Yes");
    else :
        print("No");
 
# This code is contributed by AnkitRai01

C#

// C# implementation of the approach
using System;
 
class GFG
{
 
// Function that returns true if a, b and c can
// be made equal with the given operations
static bool canBeEqual(int a, int b, int c, int k)
{
    int []arr = new int[3];
    arr[0] = a;
    arr[1] = b;
    arr[2] = c;
 
    // Sort the three numbers
    Array.Sort(arr);
 
    // Find the sum of difference of the 3rd and
    // 2nd element and the 3rd and 1st element
    int diff = 2 * arr[2] - arr[1] - arr[0];
 
    // Subtract the difference from k
    k = k - diff;
 
    // Check the required condition
    if (k < 0 || k % 3 != 0)
        return false;
 
    return true;
}
 
// Driver code
public static void Main(String[] args)
{
    int a1 = 6, b1 = 3, c1 = 2, k1 = 7;
 
    if (canBeEqual(a1, b1, c1, k1))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by 29AjayKumar

Javascript

<script>
 
// Javascript implementation of the approach
 
// Function that returns true if a, b and c can
// be made equal with the given operations
function canBeEqual(a, b, c, k)
{
    var arr = Array(3);
    arr[0] = a;
    arr[1] = b;
    arr[2] = c;
 
    // Sort the three numbers
    arr.sort((a,b)=> a-b);
 
    // Find the sum of difference of the 3rd and
    // 2nd element and the 3rd and 1st element
    var diff = 2 * arr[2] - arr[1] - arr[0];
 
    // Subtract the difference from k
    k = k - diff;
 
    // Check the required condition
    if (k < 0 || k % 3 != 0)
        return false;
 
    return true;
}
 
// Driver code
var a1 = 6, b1 = 3, c1 = 2, k1 = 7;
if (canBeEqual(a1, b1, c1, k1))
    document.write( "Yes");
else
    document.write( "No");
 
// This code is contributed by rrrtnx.
</script>
Producción: 

Yes

 

Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por AmanGupta65 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *