Dada una array ordenada arr[] , la tarea es encontrar los elementos mínimos necesarios para insertar en la array de modo que la array forme una Progresión aritmética .
Ejemplos:
Entrada: arr[] = {1, 6, 8, 10, 14, 16}
Salida: 10
Explicación:
Los elementos mínimos necesarios para formar AP son 10.
Array transformada después de la inserción de los elementos.
array[] = {1, 2 , 3 , 4 , 5 , 6, 7 , 8,
9 , 10, 11 , 12 , 13 , 14, 15 , 16}
Entrada: array[] = {1, 3, 5, 7, 11}
Salida: 1
Explicación:
Los elementos mínimos necesarios para formar AP son 1.
Array transformada después de la inserción de los elementos.
array[] = {1, 3, 5, 7,9 , 11}
Enfoque: La idea es encontrar la diferencia de elementos consecutivos de la array ordenada y luego encontrar el máximo común divisor de todas las diferencias. El MCD de las diferencias será la diferencia común de la progresión aritmética que se puede formar. A continuación se muestra la ilustración de los pasos:
- Encuentre la diferencia entre los elementos consecutivos de la array y guárdela en diff[] .
- Ahora el GCD de la array diff[] dará la diferencia común entre los elementos de la array ordenada dada.
Por ejemplo:
Given array be {1, 5, 7} Difference of Consecutive elements will be - Difference(1, 5) = |5 - 1| = 4 Difference(5, 7) = |7 - 5| = 2 Then, GCD of the Differences will be gcd(4, 2) = 2 This means there can be A.P. formed with common-difference as 2. That is - {1, 3, 5, 7}
- Si la diferencia entre los elementos consecutivos de la array ordenada arr[] es mayor que el GCD calculado anteriormente, entonces el número mínimo de elementos necesarios para insertar en la array dada para formar el elemento de la array en Progresión aritmética viene dado por:
Number of possible elements = (Difference / Common Difference) - 1
- Agregue el recuento de elementos necesarios para insertar para todo el conjunto de elementos consecutivos en la array ordenada dada.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to find the // minimum elements required to // be inserted into an array to // form an arithmetic progression #include <bits/stdc++.h> using namespace std; // Function to find the greatest // common divisor of two numbers int gcdFunc(int a, int b) { if (b == 0) return a; return gcdFunc(b, a % b); } // Function to find the minimum // the minimum number of elements // required to be inserted into array int findMinimumElements(int* a, int n) { int b[n - 1]; // Difference array of consecutive // elements of the array for (int i = 1; i < n; i++) { b[i - 1] = a[i] - a[i - 1]; } int gcd = b[0]; // GCD of the difference array for (int i = 0; i < n - 1; i++) { gcd = gcdFunc(gcd, b[i]); } int ans = 0; // Loop to calculate the minimum // number of elements required for (int i = 0; i < n - 1; i++) { ans += (b[i] / gcd) - 1; } return ans; } // Driver Code int main() { int arr1[] = { 1, 6, 8, 10, 14, 16 }; int n1 = sizeof(arr1)/sizeof(arr1[0]); // Function calling cout << findMinimumElements(arr1, n1) << endl; }
Java
// Java implementation to find the // minimum elements required to // be inserted into an array to // form an arithmetic progression class GFG{ // Function to find the greatest // common divisor of two numbers static int gcdFunc(int a, int b) { if (b == 0) return a; return gcdFunc(b, a % b); } // Function to find the minimum // the minimum number of elements // required to be inserted into array static int findMinimumElements(int[] a, int n) { int[] b = new int[n - 1]; // Difference array of consecutive // elements of the array for (int i = 1; i < n; i++) { b[i - 1] = a[i] - a[i - 1]; } int gcd = b[0]; // GCD of the difference array for (int i = 0; i < n - 1; i++) { gcd = gcdFunc(gcd, b[i]); } int ans = 0; // Loop to calculate the minimum // number of elements required for (int i = 0; i < n - 1; i++) { ans += (b[i] / gcd) - 1; } return ans; } // Driver Code public static void main(String[] args) { int arr1[] = { 1, 6, 8, 10, 14, 16 }; int n1 = arr1.length; // Function calling System.out.print(findMinimumElements(arr1, n1) +"\n"); } } // This code is contributed by Rajput-Ji
Python3
# Python3 implementation to find the # minimum elements required to # be inserted into an array to # form an arithmetic progression # Function to find the greatest # common divisor of two numbers def gcdFunc(a, b): if (b == 0): return a return gcdFunc(b, a % b) # Function to find the minimum # the minimum number of elements # required to be inserted into array def findMinimumElements(a, n): b = [0]*(n - 1) # Difference array of consecutive # elements of the array for i in range(1,n): b[i - 1] = a[i] - a[i - 1] gcd = b[0] # GCD of the difference array for i in range(n-1): gcd = gcdFunc(gcd, b[i]) ans = 0 # Loop to calculate the minimum # number of elements required for i in range(n-1): ans += (b[i] // gcd) - 1 return ans # Driver Code arr1 = [1, 6, 8, 10, 14, 16] n1 = len(arr1) # Function calling print(findMinimumElements(arr1, n1)) # This code is contributed by shubhamsingh10
C#
// C# implementation to find the // minimum elements required to // be inserted into an array to // form an arithmetic progression using System; class GFG{ // Function to find the greatest // common divisor of two numbers static int gcdFunc(int a, int b) { if (b == 0) return a; return gcdFunc(b, a % b); } // Function to find the minimum // the minimum number of elements // required to be inserted into array static int findMinimumElements(int[] a, int n) { int[] b = new int[n - 1]; // Difference array of consecutive // elements of the array for (int i = 1; i < n; i++) { b[i - 1] = a[i] - a[i - 1]; } int gcd = b[0]; // GCD of the difference array for (int i = 0; i < n - 1; i++) { gcd = gcdFunc(gcd, b[i]); } int ans = 0; // Loop to calculate the minimum // number of elements required for (int i = 0; i < n - 1; i++) { ans += (b[i] / gcd) - 1; } return ans; } // Driver Code static public void Main () { int[] arr1 = new int[] { 1, 6, 8, 10, 14, 16 }; int n1 = arr1.Length; // Function calling Console.WriteLine(findMinimumElements(arr1, n1)); } } // This code is contributed by shivanisingh
Javascript
<script> // Javascript implementation to find the // minimum elements required to // be inserted into an array to // form an arithmetic progression // Function to find the greatest // common divisor of two numbers function gcdFunc(a, b) { if (b == 0) return a; return gcdFunc(b, a % b); } // Function to find the minimum // the minimum number of elements // required to be inserted into array function findMinimumElements(a, n) { let b = new Array(n - 1); // Difference array of consecutive // elements of the array for (let i = 1; i < n; i++) { b[i - 1] = a[i] - a[i - 1]; } let gcd = b[0]; // GCD of the difference array for (let i = 0; i < n - 1; i++) { gcd = gcdFunc(gcd, b[i]); } let ans = 0; // Loop to calculate the minimum // number of elements required for (let i = 0; i < n - 1; i++) { ans += (b[i] / gcd) - 1; } return ans; } // Driver Code let arr1 = [ 1, 6, 8, 10, 14, 16 ]; let n1 = arr1.length; // Function calling document.write(findMinimumElements(arr1, n1) + "<br>"); // This code is contributed by Mayank Tyagi </script>
10
Complejidad de tiempo: O(N * log(MAX)), donde N es el número de elementos en la array y MAX es el elemento máximo en la array.
Espacio auxiliar : O(N + log(MAX))
Publicación traducida automáticamente
Artículo escrito por AmanGupta65 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA