Encuentre M para los cuales los números A, B, C forman un AP si alguno se divide por M

Dados tres números enteros positivos A , B y C , la tarea es averiguar que, si dividimos cualquiera de ellos por cualquier número entero M(m>0 ), ¿pueden formar un AP (progresión aritmética) en el mismo orden dado? . Si hay varios valores posibles, imprímalos todos y si no es posible ningún valor, imprima -1.

Ejemplos:

Entrada:   A = 25, B = 10, C = 15
Salida:   5
Explicación: Si A(25) se divide por 5 entonces los tres enteros son 5, 10, 15 que forman un AP
con diferencia común 5.

Entrada:   A = 18, B = 4, C = 2
Salida:   3.
Explicación : Si A(18) se divide por 3 entonces los tres enteros son 6, 4, 2 que forman un AP
con diferencia común -2.

Entrada:  A = 7, B = 11, C = 13
Salida:   -1
Explicación : Se puede probar que no existe ningún entero positivo que al dividir con 
cualquiera de los tres números pueda formar un AP

 

Acercarse: 

Los tres números A, B y C están en AP si-

segundo – un = do – segundo = re

Aquí, 
A, B y C son los tres números
d es la diferencia común

Usando la propiedad de diferencia común anterior de AP, puede haber tres fórmulas para tres casos:

  • Cuando A se divide por un entero m1-

Para que A/m1, B, C formen un AP, tenemos 

B – A / m1 = C – B

Así, m1 = a / (2 * B – C)

  • Cuando B se divide por un número entero m2-

Para que A, B/m2, C formen un AP, tenemos 

B/m2 – A = C – B/m2

Así, m2 = 2 * B/ (C+ A)

  • Cuando C se divide por un número entero m3-

Para que A, B, C/m3 formen un AP, tenemos 

B – A = C / m3 – B

Así, m3 = C / (2 * B – A)

  • Ahora tenemos posibles valores de M para cada caso, luego verifique para cada uno de los tres valores si alguno de ellos es un número entero positivo, entonces ese valor es la respuesta requerida.
  • Si no existe tal valor posible, imprima -1.

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ code to implement the given approach
#include <bits/stdc++.h>
using namespace std;
 
// Utility function to check
// if given argument is an integer or not
bool ifint(double x)
{
    int a = x;
 
    if (x - a > 0)
        return false;
    else
        return true;
}
 
// Function to find any integer M if exists
void findVal(int A, int B, int C)
{
    double m1 = (double)(A / (2 * B - C));
    double m2 = (double)(2 * B / (C + A));
    double m3 = (double)(C / (2 * B - A));
 
    // Checks if it is both
    // positive and an integer
    if (m1 > 1 && ifint(m1))
        cout << m1;
 
    else if (m2 > 1 && ifint(m2))
        cout << m2;
 
    else if (m3 > 1 && ifint(m3))
        cout << m3;
 
    else
        cout << "-1";
}
 
// Driver code
int main()
{
    int A = 2;
    int B = 4;
    int C = 18;
 
    findVal(A, B, C);
    return 0;
}

Java

// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Utility function to check
  // if given argument is an integer or not
  static Boolean ifint(double x)
  {
    int a = (int)x;
 
    if (x - a > 0)
      return false;
    else
      return true;
  }
 
  // Function to find any integer M if exists
  static void findVal(int A, int B, int C)
  {
    double m1 = (double)(A / (2 * B - C));
    double m2 = (double)(2 * B / (C + A));
    double m3 = (double)(C / (2 * B - A));
 
    // Checks if it is both
    // positive and an integer
    if (m1 > 1 && ifint(m1)){
      int M1 = (int)m1;
      System.out.print(M1);
    }
 
    else if (m2 > 1 && ifint(m2)){
      int M2 = (int)m2;
      System.out.print(M2);
    }
 
    else if (m3 > 1 && ifint(m3)){
      int M3 = (int)m3;
      System.out.print(M3);
    }
 
    else
      System.out.print("-1");
  }
 
  // Driver code
  public static void main (String[] args) {
    int A = 2;
    int B = 4;
    int C = 18;
 
    findVal(A, B, C);   
  }
}
 
// This code is contributed by hrithikgarg03188/

Python

# Python code to implement the given approach
 
# Utility function to check
# if given argument is an integer or not
def ifint(x):
    a = x
 
    if (x - a > 0):
        return False
    else:
        return True
 
# Function to find any integer M if exists
def findVal(A, B, C):
     
    m1 = (A / (2 * B - C))
    m2 = (2 * B / (C + A))
    m3 = (C / (2 * B - A));
 
    # Checks if it is both
    # positive and an integer
    if (m1 > 1 and ifint(m1)):
        print(m1)
 
    elif (m2 > 1 and ifint(m2)):
        print(m2)
 
    elif (m3 > 1 and ifint(m3)):
        print(m3)
 
    else:
        print(-1)
 
# Driver code
A = 2
B = 4
C = 18
 
findVal(A, B, C)
 
# This code is contributed by Samim Hossain Mondal.

C#

// C# code to implement the given approach
using System;
class GFG {
    // Utility function to check
    // if given argument is an integer or not
    static bool ifint(double x)
    {
        double a = x;
 
        if (x - a > 0)
            return false;
        else
            return true;
    }
 
    // Function to find any integer M if exists
    static void findVal(int A, int B, int C)
    {
        double m1 = Convert.ToDouble(A / (2 * B - C));
        double m2 = Convert.ToDouble(2 * B / (C + A));
        double m3 = Convert.ToDouble(C / (2 * B - A));
 
        // Checks if it is both
        // positive and an integer
        if (m1 > 1 && ifint(m1))
            Console.Write(m1);
 
        else if (m2 > 1 && ifint(m2))
            Console.Write(m2);
 
        else if (m3 > 1 && ifint(m3))
            Console.Write(m3);
 
        else
            Console.Write(-1);
    }
 
    // Driver code
    public static int Main()
    {
        int A = 2;
        int B = 4;
        int C = 18;
 
        findVal(A, B, C);
        return 0;
    }
}
 
// This code is contributed by Taranpreet

Javascript

<script>
        // JavaScript code for the above approach
 
        // Utility function to check
        // if given argument is an integer or not
        function ifint(x) {
            let a = x;
 
            if (x - a > 0)
                return false;
            else
                return true;
        }
 
        // Function to find any integer M if exists
        function findVal(A, B, C) {
            let m1 = (A / (2 * B - C));
            let m2 = (2 * B / (C + A));
            let m3 = (C / (2 * B - A));
 
            // Checks if it is both
            // positive and an integer
            if (m1 > 1 && ifint(m1))
                document.write(m1);
 
            else if (m2 > 1 && ifint(m2))
                document.write(m2);
 
            else if (m3 > 1 && ifint(m3))
                document.write(m3);
 
            else
                document.write("-1");
        }
 
        // Driver code
        let A = 2;
        let B = 4;
        let C = 18;
 
        findVal(A, B, C);
 
       // This code is contributed by Potta Lokesh
    </script>
Producción

3

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

C++

// C++ code to implement the given approach
#include <bits/stdc++.h>
using namespace std;
 
// Utility function to check
// if given argument is an integer or not
bool ifint(double x)
{
    int a = x;
 
    if (x - a > 0)
        return false;
    else
        return true;
}
 
// Function to find any integer M if exists
void findVal(int A, int B, int C)
{
    double m1 = (double)(A / (2 * B - C));
    double m2 = (double)(2 * B / (C + A));
    double m3 = (double)(C / (2 * B - A));
 
    // Checks if it is both
    // positive and an integer
    if (m1 > 1 && ifint(m1))
        cout << m1;
 
    else if (m2 > 1 && ifint(m2))
        cout << m2;
 
    else if (m3 > 1 && ifint(m3))
        cout << m3;
 
    else
        cout << "-1";
}
 
// Driver code
int main()
{
    int A = 2;
    int B = 4;
    int C = 18;
 
    findVal(A, B, C);
    return 0;
}

Java

// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Utility function to check
  // if given argument is an integer or not
  static Boolean ifint(double x)
  {
    int a = (int)x;
 
    if (x - a > 0)
      return false;
    else
      return true;
  }
 
  // Function to find any integer M if exists
  static void findVal(int A, int B, int C)
  {
    double m1 = (double)(A / (2 * B - C));
    double m2 = (double)(2 * B / (C + A));
    double m3 = (double)(C / (2 * B - A));
 
    // Checks if it is both
    // positive and an integer
    if (m1 > 1 && ifint(m1)){
      int M1 = (int)m1;
      System.out.print(M1);
    }
 
    else if (m2 > 1 && ifint(m2)){
      int M2 = (int)m2;
      System.out.print(M2);
    }
 
    else if (m3 > 1 && ifint(m3)){
      int M3 = (int)m3;
      System.out.print(M3);
    }
 
    else
      System.out.print("-1");
  }
 
  // Driver code
  public static void main (String[] args) {
    int A = 2;
    int B = 4;
    int C = 18;
 
    findVal(A, B, C);   
  }
}
 
// This code is contributed by hrithikgarg03188/

Python3

# Python code for the above approach
 
# Utility function to check
# if given argument is an integer or not
def ifint(x):
    a = x;
 
    if (x - a > 0):
        return False;
    else:
        return True;
 
# Function to find any integer M if exists
def findVal(A, B, C):
    m1 = (A / (2 * B - C));
    m2 = (2 * B / (C + A));
    m3 = (C / (2 * B - A));
 
    # Checks if it is both
    # positive and an integer
    if (m1 > 1 and ifint(m1)):
        print(int(m1))
 
    elif (m2 > 1 and ifint(m2)):
        print(int(m2))
 
    elif (m3 > 1 and ifint(m3)):
        print(int(m3))
 
    else:
        print("-1");
 
# Driver code
A = 2;
B = 4;
C = 18;
 
findVal(A, B, C);
 
# This code is contributed by Saurabh Jaiswal

C#

// C# code to implement the given approach
using System;
class GFG
{
   
    // Utility function to check
    // if given argument is an integer or not
    static bool ifint(double x)
    {
        double a = x;
 
        if (x - a > 0)
            return false;
        else
            return true;
    }
 
    // Function to find any integer M if exists
    static void findVal(int A, int B, int C)
    {
        double m1 = Convert.ToDouble(A / (2 * B - C));
        double m2 = Convert.ToDouble(2 * B / (C + A));
        double m3 = Convert.ToDouble(C / (2 * B - A));
 
        // Checks if it is both
        // positive and an integer
        if (m1 > 1 && ifint(m1))
            Console.Write(m1);
 
        else if (m2 > 1 && ifint(m2))
            Console.Write(m2);
 
        else if (m3 > 1 && ifint(m3))
            Console.Write(m3);
 
        else
            Console.Write(-1);
    }
 
    // Driver code
    public static int Main()
    {
        int A = 2;
        int B = 4;
        int C = 18;
 
        findVal(A, B, C);
        return 0;
    }
}
 
// This code is contributed by Taranpreet

Javascript

<script>
        // JavaScript code for the above approach
 
        // Utility function to check
        // if given argument is an integer or not
        function ifint(x) {
            let a = x;
 
            if (x - a > 0)
                return false;
            else
                return true;
        }
 
        // Function to find any integer M if exists
        function findVal(A, B, C) {
            let m1 = (A / (2 * B - C));
            let m2 = (2 * B / (C + A));
            let m3 = (C / (2 * B - A));
 
            // Checks if it is both
            // positive and an integer
            if (m1 > 1 && ifint(m1))
                document.write(m1);
 
            else if (m2 > 1 && ifint(m2))
                document.write(m2);
 
            else if (m3 > 1 && ifint(m3))
                document.write(m3);
 
            else
                document.write("-1");
        }
 
        // Driver code
        let A = 2;
        let B = 4;
        let C = 18;
 
        findVal(A, B, C);
 
       // This code is contributed by Potta Lokesh
    </script>

Publicación traducida automáticamente

Artículo escrito por harshverma28 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 *