Minimize array elements required to be incremented or decremented to convert given array into a Fibonacci Series
Given an array arr[], the task is to find the minimum number increments or decrements by 1 required to convert the array into a Fibonacci Series. If it is not possible, then print -1.
Note: Each array element can be incremented or decremented only once.
Examples:
Input: arr[] = {4, 8, 9, 17, 21}
Output: 3
Explanation:
The array can be converted into a Fibonacci Series in three moves:
Convert 4 to 3, arr[] = {3, 8, 9, 17, 21}
Convert 8 to 7, arr[] = {3, 7, 9, 17, 21}
Convert 9 to 10, arr[] = {3, 7, 10, 17, 21}
Input: arr[] = {3, 8, 7, 2}
Output: -1
Explanation:
The given array cannot be converted into a Fibonacci Series.
Approach: The idea to solve the problem is to use the fact that the first two elements of a Fibonacci Series are enough to calculate the common difference of the Fibonacci Series and hence the subsequent elements. So check all permutations of operations on the first two numbers and calculate the minimum moves to convert the rest of the elements into Fibonacci Series.
Follow the steps below to implement the above approach:
- If the number of elements in the array is less than 3, then it’s already a Fibonacci series.
- Otherwise, try all permutations of the first two elements:
- For each permutation, calculate the number of operations for the rest of the elements of the array:
- If it’s not possible to change the element in at most one operation, then the array cannot be converted into a Fibonacci Series.
- Otherwise, update the required number of operations.
- Update the answer with the number of operations.
- Return the answer.
Below is the implementation of our approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minMoves(vector< int > arr)
{
int N = arr.size();
if (N <= 2)
return 0;
int ans = INT_MAX;
for ( int i = -1; i <= 1; i++) {
for ( int j = -1; j <= 1; j++) {
int num1 = arr[0] + i;
int num2 = arr[1] + j;
int flag = 1;
int moves = abs (i) + abs (j);
for ( int idx = 2; idx < N; idx++) {
int num = num1 + num2;
if ( abs (arr[idx] - num) > 1)
flag = 0;
else
moves += abs (arr[idx] - num);
num1 = num2;
num2 = num;
}
if (flag)
ans = min(ans, moves);
}
}
if (ans == INT_MAX)
return -1;
return ans;
}
int main()
{
vector< int > arr = { 4, 8, 9, 17, 27 };
cout << minMoves(arr) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minMoves( int []arr)
{
int N = arr.length;
if (N <= 2 )
return 0 ;
int ans = Integer.MAX_VALUE;
for ( int i = - 1 ; i <= 1 ; i++)
{
for ( int j = - 1 ; j <= 1 ; j++)
{
int num1 = arr[ 0 ] + i;
int num2 = arr[ 1 ] + j;
int flag = 1 ;
int moves = Math.abs(i) + Math.abs(j);
for ( int idx = 2 ; idx < N; idx++)
{
int num = num1 + num2;
if (Math.abs(arr[idx] - num) > 1 )
flag = 0 ;
else
moves += Math.abs(arr[idx] - num);
num1 = num2;
num2 = num;
}
if (flag > 0 )
ans = Math.min(ans, moves);
}
}
if (ans == Integer.MAX_VALUE)
return - 1 ;
return ans;
}
public static void main(String[] args)
{
int []arr = { 4 , 8 , 9 , 17 , 27 };
System.out.print(minMoves(arr));
}
}
|
Python3
import sys
def minMoves(arr):
N = len (arr)
if (N < = 2 ):
return 0
ans = sys.maxsize
for i in range ( - 1 , 2 ):
for j in range ( - 1 , 2 ):
num1 = arr[ 0 ] + i
num2 = arr[ 1 ] + j
flag = 1
moves = abs (i) + abs (j)
for idx in range ( 2 , N):
num = num1 + num2
if ( abs (arr[idx] - num) > 1 ):
flag = 0
else :
moves + = abs (arr[idx] - num)
num1 = num2
num2 = num
if (flag):
ans = min (ans, moves)
if (ans = = sys.maxsize):
return - 1
return ans
if __name__ = = "__main__" :
arr = [ 4 , 8 , 9 , 17 , 27 ]
print (minMoves(arr))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int minMoves(List< int > arr)
{
int N = arr.Count;
if (N <= 2)
return 0;
int ans = Int32.MaxValue;
for ( int i = -1; i <= 1; i++)
{
for ( int j = -1; j <= 1; j++)
{
int num1 = arr[0] + i;
int num2 = arr[1] + j;
int flag = 1;
int moves = Math.Abs(i) + Math.Abs(j);
for ( int idx = 2; idx < N; idx++)
{
int num = num1 + num2;
if (Math.Abs(arr[idx] - num) > 1)
flag = 0;
else
moves += Math.Abs(arr[idx] - num);
num1 = num2;
num2 = num;
}
if (flag != 0)
ans = Math.Min(ans, moves);
}
}
if (ans == Int32.MaxValue)
return -1;
return ans;
}
public static void Main()
{
List< int > arr = new List< int >(){ 4, 8, 9, 17, 27 };
Console.WriteLine(minMoves(arr));
}
}
|
Javascript
<script>
function minMoves(arr)
{
let N = arr.length;
if (N <= 2)
return 0;
let ans = Number.MAX_VALUE;
for (let i = -1; i <= 1; i++)
{
for (let j = -1; j <= 1; j++)
{
let num1 = arr[0] + i;
let num2 = arr[1] + j;
let flag = 1;
let moves = Math.abs(i) + Math.abs(j);
for (let idx = 2; idx < N; idx++)
{
let num = num1 + num2;
if (Math.abs(arr[idx] - num) > 1)
flag = 0;
else
moves += Math.abs(arr[idx] - num);
num1 = num2;
num2 = num;
}
if (flag > 0)
ans = Math.min(ans, moves);
}
}
if (ans == Number.MAX_VALUE)
return -1;
return ans;
}
let arr = [ 4, 8, 9, 17, 27 ];
document.write(minMoves(arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
04 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...