Dados dos números enteros A , B que son dos términos cualesquiera de una serie de progresión aritmética y un número entero N , la tarea es minimizar el término N de esa progresión aritmética.
Nota: Todos los elementos de una serie AP deben ser positivos.
Ejemplos:
Entrada: A = 1, B = 6, N = 3
Salida: 11
Explicaciones: Los primeros tres términos de la progresión aritmética dada son: {1, 6, 11}.
Por lo tanto, la salida requerida es 11.Entrada: A = 20, B = 50, N = 5
Salida: 50
Explicaciones: Los primeros cinco términos de la progresión aritmética dada son: {10, 20, 30, 40, 50}.
Por lo tanto, la salida requerida es 50.
Enfoque: el problema se puede resolver colocando A y B en todas las posiciones posibles en una progresión aritmética y verificando cuál genera el menor término N posible . Considerando que las posiciones de A y B son i y j respectivamente, se deben realizar los siguientes cálculos:
Diferencia común (D) de un AP = (B – A)/(j – i)
Primer término de un AP = A – (i – 1) * D N-
ésimo término de un AP = Primer término + (N – 1)* D
Finalmente, devuelve el N -ésimo término más pequeño obtenido.
Siga los pasos a continuación para resolver el problema:
- Inicialice una variable, digamos res para almacenar el valor más pequeño posible del término N de progresión aritmética requerido .
- Usando dos bucles anidados, coloque A y B en todas las posiciones posibles en un AP y calcule el término N usando los cálculos anteriores. Continúe actualizando res para almacenar el valor mínimo del término N obtenido .
- Finalmente, imprima el valor de res como la respuesta requerida.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find the smallest // Nth term of an AP possible int smallestNth(int A, int B, int N) { // Stores the smallest Nth term int res = INT_MAX; for (int i = 1; i < N; i++) { for (int j = N; j > i; j--) { // Check if common difference // of AP is an integer if ((B - A) % (j - i) == 0) { // Store the common // difference int D = (B - A) / (j - i); // Store the First Term // of that AP int FirstTerm = A - (i - 1) * D; // Store the Nth term of // that AP int NthTerm = FirstTerm + (N - 1) * D; // Check if all elements of // an AP are positive if (FirstTerm > 0) res = min(res, NthTerm); } } } // Return the least // Nth term obtained return res; } // Driver Code int main() { int N = 3; int A = 1; int B = 6; cout << smallestNth(A, B, N); }
Java
// Java program to implement // the above approach import java.io.*; class GFG{ // Function to find the smallest // Nth term of an AP possible static int smallestNth(int A, int B, int N) { // Stores the smallest Nth term int res = Integer.MAX_VALUE; for(int i = 1; i < N; i++) { for(int j = N; j > i; j--) { // Check if common difference // of AP is an integer if ((B - A) % (j - i) == 0) { // Store the common // difference int D = (B - A) / (j - i); // Store the First Term // of that AP int FirstTerm = A - (i - 1) * D; // Store the Nth term of // that AP int NthTerm = FirstTerm + (N - 1) * D; // Check if all elements of // an AP are positive if (FirstTerm > 0) res = Math.min(res, NthTerm); } } } // Return the least // Nth term obtained return res; } // Driver Code public static void main (String[] args) { int N = 3; int A = 1; int B = 6; System.out.print(smallestNth(A, B, N)); } } // This code is contributed by code_hunt
Python3
# Python3 program to implement # the above approach import sys # Function to find the smallest # Nth term of an AP possible def smallestNth(A, B, N): # Stores the smallest Nth term res = sys.maxsize for i in range(1, N): for j in range(N, i, -1): # Check if common difference # of AP is an integer if ((B - A) % (j - i) == 0): # Store the common # difference D = (B - A) // (j - i) # Store the First Term # of that AP FirstTerm = A - (i - 1) * D # Store the Nth term of # that AP NthTerm = FirstTerm + (N - 1) * D # Check if all elements of # an AP are positive if (FirstTerm > 0): res = min(res, NthTerm) # Return the least # Nth term obtained return res # Driver Code if __name__ == '__main__': N = 3 A = 1 B = 6 print(smallestNth(A, B, N)) # This code is contributed by mohit kumar 29
C#
// C# program to implement // the above approach using System; class GFG{ // Function to find the smallest // Nth term of an AP possible static int smallestNth(int A, int B, int N) { // Stores the smallest Nth term int res = Int32.MaxValue; for(int i = 1; i < N; i++) { for(int j = N; j > i; j--) { // Check if common difference // of AP is an integer if ((B - A) % (j - i) == 0) { // Store the common // difference int D = (B - A) / (j - i); // Store the First Term // of that AP int FirstTerm = A - (i - 1) * D; // Store the Nth term of // that AP int NthTerm = FirstTerm + (N - 1) * D; // Check if all elements of // an AP are positive if (FirstTerm > 0) res = Math.Min(res, NthTerm); } } } // Return the least // Nth term obtained return res; } // Driver Code public static void Main () { int N = 3; int A = 1; int B = 6; Console.Write(smallestNth(A, B, N)); } } // This code is contributed by code_hunt
Javascript
<script> // JavaScript program to implement // the above approach // Function to find the smallest // Nth term of an AP possible function smallestNth(A, B, N) { // Stores the smallest Nth term let res = Number.MAX_VALUE; for(let i = 1; i < N; i++) { for(let j = N; j > i; j--) { // Check if common difference // of AP is an integer if ((B - A) % (j - i) == 0) { // Store the common // difference let D = (B - A) / (j - i); // Store the First Term // of that AP let FirstTerm = A - (i - 1) * D; // Store the Nth term of // that AP let NthTerm = FirstTerm + (N - 1) * D; // Check if all elements of // an AP are positive if (FirstTerm > 0) res = Math.min(res, NthTerm); } } } // Return the least // Nth term obtained return res; } // Driver code let N = 3; let A = 1; let B = 6; document.write(smallestNth(A, B, N)); // This code is contributed by target_2. </script>
11
Complejidad de Tiempo: O(N 2 )
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por hrithikmalik86 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA