Minimum elements inserted in a sorted array to form an Arithmetic progression
Given a sorted array arr[], the task is to find minimum elements needed to be inserted in the array such that array forms an Arithmetic Progression.
Examples:
Input: arr[] = {1, 6, 8, 10, 14, 16}
Output: 10
Explanation:
Minimum elements required to form A.P. is 10.
Transformed array after insertion of the elements.
arr[] = {1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16}
Input: arr[] = {1, 3, 5, 7, 11}
Output: 1
Explanation:
Minimum elements required to form A.P. is 1.
Transformed array after insertion of the elements.
arr[] = {1, 3, 5, 7, 9, 11}
Approach: The idea is to find the difference of consecutive elements of the sorted array and then find the greatest common divisor of all the differences. The GCD of the differences will be the common-difference for the arithmetic progression that can be formed. Below is the illustration of the steps:
- Find the difference between consecutive elements of the array and store in it diff[].
- Now the GCD of the diff[] array will give the common difference between the elements of given sorted array.
For Example:
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}
-
- If the difference between consecutive elements of the sorted array arr[] is greater than the GCD calculated above, then the minimum number of elements needed to be inserted in the given array to make the element of the array in Arithmetic Progression is given by:
Number of possible elements =
(Difference / Common Difference) - 1
-
- Add the count of element needed to be insert for all set of consecutive elements in the given sorted array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcdFunc( int a, int b)
{
if (b == 0)
return a;
return gcdFunc(b, a % b);
}
int findMinimumElements( int * a, int n)
{
int b[n - 1];
for ( int i = 1; i < n; i++) {
b[i - 1] = a[i] - a[i - 1];
}
int gcd = b[0];
for ( int i = 0; i < n - 1; i++) {
gcd = gcdFunc(gcd, b[i]);
}
int ans = 0;
for ( int i = 0; i < n - 1; i++) {
ans += (b[i] / gcd) - 1;
}
return ans;
}
int main()
{
int arr1[] = { 1, 6, 8, 10, 14, 16 };
int n1 = sizeof (arr1)/ sizeof (arr1[0]);
cout << findMinimumElements(arr1, n1)
<< endl;
}
|
Java
class GFG{
static int gcdFunc( int a, int b)
{
if (b == 0 )
return a;
return gcdFunc(b, a % b);
}
static int findMinimumElements( int [] a, int n)
{
int [] b = new int [n - 1 ];
for ( int i = 1 ; i < n; i++) {
b[i - 1 ] = a[i] - a[i - 1 ];
}
int gcd = b[ 0 ];
for ( int i = 0 ; i < n - 1 ; i++) {
gcd = gcdFunc(gcd, b[i]);
}
int ans = 0 ;
for ( int i = 0 ; i < n - 1 ; i++) {
ans += (b[i] / gcd) - 1 ;
}
return ans;
}
public static void main(String[] args)
{
int arr1[] = { 1 , 6 , 8 , 10 , 14 , 16 };
int n1 = arr1.length;
System.out.print(findMinimumElements(arr1, n1)
+ "\n" );
}
}
|
Python3
def gcdFunc(a, b):
if (b = = 0 ):
return a
return gcdFunc(b, a % b)
def findMinimumElements(a, n):
b = [ 0 ] * (n - 1 )
for i in range ( 1 ,n):
b[i - 1 ] = a[i] - a[i - 1 ]
gcd = b[ 0 ]
for i in range (n - 1 ):
gcd = gcdFunc(gcd, b[i])
ans = 0
for i in range (n - 1 ):
ans + = (b[i] / / gcd) - 1
return ans
arr1 = [ 1 , 6 , 8 , 10 , 14 , 16 ]
n1 = len (arr1)
print (findMinimumElements(arr1, n1))
|
C#
using System;
class GFG{
static int gcdFunc( int a, int b)
{
if (b == 0)
return a;
return gcdFunc(b, a % b);
}
static int findMinimumElements( int [] a, int n)
{
int [] b = new int [n - 1];
for ( int i = 1; i < n; i++) {
b[i - 1] = a[i] - a[i - 1];
}
int gcd = b[0];
for ( int i = 0; i < n - 1; i++) {
gcd = gcdFunc(gcd, b[i]);
}
int ans = 0;
for ( int i = 0; i < n - 1; i++) {
ans += (b[i] / gcd) - 1;
}
return ans;
}
static public void Main ()
{
int [] arr1 = new int [] { 1, 6, 8, 10, 14, 16 };
int n1 = arr1.Length;
Console.WriteLine(findMinimumElements(arr1, n1));
}
}
|
Javascript
<script>
function gcdFunc(a, b)
{
if (b == 0)
return a;
return gcdFunc(b, a % b);
}
function findMinimumElements(a, n)
{
let b = new Array(n - 1);
for (let i = 1; i < n; i++) {
b[i - 1] = a[i] - a[i - 1];
}
let gcd = b[0];
for (let i = 0; i < n - 1; i++) {
gcd = gcdFunc(gcd, b[i]);
}
let ans = 0;
for (let i = 0; i < n - 1; i++) {
ans += (b[i] / gcd) - 1;
}
return ans;
}
let arr1 = [ 1, 6, 8, 10, 14, 16 ];
let n1 = arr1.length;
document.write(findMinimumElements(arr1, n1)
+ "<br>" );
</script>
|
Time Complexity: O(N * log(MAX)), where N is the number of elements in the array and MAX is the maximum element in the array.
Auxiliary Space: O(N + log(MAX))
Last Updated :
17 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...