Minimum elements to be inserted in Array to make adjacent differences equal
Last Updated :
04 Jun, 2022
Given an array of integers Arr[]. The elements of the array are sorted in increasing order. The task is to find the minimum number of elements to be inserted in the array so that the differences between any two consecutive elements are the same.
Examples:
Input: Arr[] = {1, 4, 13, 19, 25}
Output: 4
Explanation:
One possible solution is: Arr[] = { 1, 4, 7, 10, 13, 16, 19, 22, 25 }. Here all the consecutive elements has difference of 3, for this 4 elements are inserted.
Input: Arr[] = {1, 5, 8, 10, 12, 16};
Output: 10
Explanation:
10 elements needs to be inserted in order to make the difference equal.
Approach: The idea is to calculate the differences between all consecutive elements. There are N elements in the array so there will be N – 1 such difference.
- Find the GCD(greatest common divisor) of all such differences. This GCD will be the difference between any two consecutive elements of the array after insertion of new elements.
- Let’s suppose the difference between the first and second element is diff. Initialize the answer by 0 and add ((diff / GCD) – 1) to the answer because there are ( diff / GCD – 1 ) elements that are needed to make the difference equal to GCD.
- Perform the same for all consecutive elements of the given array and add to the answer in order to find the minimum number of elements to be inserted to make equal differences between any two consecutive elements of the array.
Below is the implementation of the approach:
C++
#include <iostream>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int minimum_elements( int n, int arr[])
{
if (n < 3)
return 0;
int g, ans = 0, diff, cnt;
diff = arr[1] - arr[0];
g = diff;
for ( int i = 2; i < n; i++) {
diff = arr[i] - arr[i - 1];
g = gcd(g, diff);
}
for ( int i = 1; i < n; i++) {
diff = arr[i] - arr[i - 1];
cnt = diff / g;
ans += (cnt - 1);
}
return ans;
}
int main()
{
int arr[] = { 1, 5, 8, 10, 12, 16 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << minimum_elements(n, arr);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int minimum_elements( int n, int arr[])
{
if (n < 3 )
return 0 ;
int g, ans = 0 , diff, cnt;
diff = arr[ 1 ] - arr[ 0 ];
g = diff;
for ( int i = 2 ; i < n; i++)
{
diff = arr[i] - arr[i - 1 ];
g = gcd(g, diff);
}
for ( int i = 1 ; i < n; i++)
{
diff = arr[i] - arr[i - 1 ];
cnt = diff / g;
ans += (cnt - 1 );
}
return ans;
}
public static void main (String[] args)
{
int arr[] = { 1 , 5 , 8 , 10 , 12 , 16 };
int n = arr.length;
System.out.println(minimum_elements(n, arr));
}
}
|
Python3
def gcd(a, b):
if (b = = 0 ):
return a
return gcd(b, a % b)
def minimum_elements(n, arr):
if (n < 3 ):
return 0
ans = 0
diff = arr[ 1 ] - arr[ 0 ]
g = diff
for i in range ( 2 , n):
diff = arr[i] - arr[i - 1 ]
g = gcd(g, diff)
for i in range ( 1 , n):
diff = arr[i] - arr[i - 1 ]
cnt = diff / / g
ans + = (cnt - 1 )
return ans
arr = [ 1 , 5 , 8 , 10 , 12 , 16 ]
n = len (arr)
print (minimum_elements(n, arr))
|
C#
using System;
class GFG{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int minimum_elements( int n, int [] arr)
{
if (n < 3)
return 0;
int g, ans = 0, diff, cnt;
diff = arr[1] - arr[0];
g = diff;
for ( int i = 2; i < n; i++)
{
diff = arr[i] - arr[i - 1];
g = gcd(g, diff);
}
for ( int i = 1; i < n; i++)
{
diff = arr[i] - arr[i - 1];
cnt = diff / g;
ans += (cnt - 1);
}
return ans;
}
public static void Main ()
{
int [] arr = { 1, 5, 8, 10, 12, 16 };
int n = arr.Length;
Console.WriteLine(minimum_elements(n, arr));
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function minimum_elements(n, arr)
{
if (n < 3)
return 0;
let g, ans = 0, diff, cnt;
diff = arr[1] - arr[0];
g = diff;
for (let i = 2; i < n; i++) {
diff = arr[i] - arr[i - 1];
g = gcd(g, diff);
}
for (let i = 1; i < n; i++) {
diff = arr[i] - arr[i - 1];
cnt = parseInt(diff / g);
ans += (cnt - 1);
}
return ans;
}
let arr = [ 1, 5, 8, 10, 12, 16 ];
let n = arr.length;
document.write(minimum_elements(n, arr));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(1), since no extra space has been taken.
Share your thoughts in the comments
Please Login to comment...