给定数组arr [] ,任务是找到将数组转换为Fibonacci级数所需的最小数量增减1。如果不可能,则打印-1 。
注意:每个数组元素只能递增或递减一次。
例子:
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.
方法:解决问题的想法是利用一个事实,即斐波那契数列的前两个元素足以计算斐波那契数列及其后继元素的共同差异。因此,请检查前两个数字的所有操作排列,并计算将其余元素转换为斐波那契级数的最小移动。
请按照以下步骤实施上述方法:
- 如果数组中的元素数少于3,则已经是斐波那契数列。
- 否则,请尝试前两个元素的所有排列:
- 对于每个排列,计算数组其余元素的操作数:
- 如果最多只能执行一次操作来更改元素,则无法将数组转换为Fibonacci系列。
- 否则,更新所需的操作数。
- 用操作数更新答案。
- 对于每个排列,计算数组其余元素的操作数:
- 返回答案。
下面是我们方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate minimum
// number of moves to make the
// sequence a Fibonacci series
int minMoves(vector arr)
{
int N = arr.size();
// If number of elements
// is less than 3
if (N <= 2)
return 0;
// Initialize the value
// of the result
int ans = INT_MAX;
// Try all permutations of
// the first two elements
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
// Value of first element
// after operation
int num1 = arr[0] + i;
// Value of second element
// after operation
int num2 = arr[1] + j;
int flag = 1;
int moves = abs(i) + abs(j);
// Calculate number of moves
// for rest of the elements
// of the array
for (int idx = 2; idx < N; idx++) {
// Element at idx index
int num = num1 + num2;
// If it is not possible
// to change the element
// in atmost one move
if (abs(arr[idx] - num) > 1)
flag = 0;
// Otherwise
else
moves += abs(arr[idx] - num);
num1 = num2;
num2 = num;
}
// Update the answer
if (flag)
ans = min(ans, moves);
}
}
// Return the answer
if (ans == INT_MAX)
return -1;
return ans;
}
// Driver Code
int main()
{
vector arr = { 4, 8, 9, 17, 27 };
cout << minMoves(arr) << endl;
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to calculate minimum
// number of moves to make the
// sequence a Fibonacci series
static int minMoves(int []arr)
{
int N = arr.length;
// If number of elements
// is less than 3
if (N <= 2)
return 0;
// Initialize the value
// of the result
int ans = Integer.MAX_VALUE;
// Try all permutations of
// the first two elements
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
// Value of first element
// after operation
int num1 = arr[0] + i;
// Value of second element
// after operation
int num2 = arr[1] + j;
int flag = 1;
int moves = Math.abs(i) + Math.abs(j);
// Calculate number of moves
// for rest of the elements
// of the array
for (int idx = 2; idx < N; idx++)
{
// Element at idx index
int num = num1 + num2;
// If it is not possible
// to change the element
// in atmost one move
if (Math.abs(arr[idx] - num) > 1)
flag = 0;
// Otherwise
else
moves += Math.abs(arr[idx] - num);
num1 = num2;
num2 = num;
}
// Update the answer
if (flag > 0)
ans = Math.min(ans, moves);
}
}
// Return the answer
if (ans == Integer.MAX_VALUE)
return -1;
return ans;
}
// Driver Code
public static void main(String[] args)
{
int []arr = { 4, 8, 9, 17, 27 };
System.out.print(minMoves(arr));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
import sys
# Function to calculate minimum
# number of moves to make the
# sequence a Fibonacci series
def minMoves(arr):
N = len(arr)
# If number of elements
# is less than 3
if (N <= 2):
return 0
# Initialize the value
# of the result
ans = sys.maxsize
# Try all permutations of
# the first two elements
for i in range(-1, 2):
for j in range(-1, 2):
# Value of first element
# after operation
num1 = arr[0] + i
# Value of second element
# after operation
num2 = arr[1] + j
flag = 1
moves = abs(i) + abs(j)
# Calculate number of moves
# for rest of the elements
# of the array
for idx in range(2, N):
# Element at idx index
num = num1 + num2
# If it is not possible
# to change the element
# in atmost one move
if (abs(arr[idx] - num) > 1):
flag = 0
# Otherwise
else:
moves += abs(arr[idx] - num)
num1 = num2
num2 = num
# Update the answer
if (flag):
ans = min(ans, moves)
# Return the answer
if (ans == sys.maxsize):
return -1
return ans
# Driver Code
if __name__ == "__main__":
arr = [4, 8, 9, 17, 27]
print(minMoves(arr))
# This code is contributed by chitranayal
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
// Function to calculate minimum
// number of moves to make the
// sequence a Fibonacci series
class GFG{
static int minMoves(List arr)
{
int N = arr.Count;
// If number of elements
// is less than 3
if (N <= 2)
return 0;
// Initialize the value
// of the result
int ans = Int32.MaxValue;
// Try all permutations of
// the first two elements
for(int i = -1; i <= 1; i++)
{
for(int j = -1; j <= 1; j++)
{
// Value of first element
// after operation
int num1 = arr[0] + i;
// Value of second element
// after operation
int num2 = arr[1] + j;
int flag = 1;
int moves = Math.Abs(i) + Math.Abs(j);
// Calculate number of moves
// for rest of the elements
// of the array
for(int idx = 2; idx < N; idx++)
{
// Element at idx index
int num = num1 + num2;
// If it is not possible
// to change the element
// in atmost one move
if (Math.Abs(arr[idx] - num) > 1)
flag = 0;
// Otherwise
else
moves += Math.Abs(arr[idx] - num);
num1 = num2;
num2 = num;
}
// Update the answer
if (flag != 0)
ans = Math.Min(ans, moves);
}
}
// Return the answer
if (ans == Int32.MaxValue)
return -1;
return ans;
}
// Driver Code
public static void Main()
{
List arr = new List(){ 4, 8, 9, 17, 27 };
Console.WriteLine(minMoves(arr));
}
}
// This code is contributed by SURENDRA_GANGWAR
输出:
3
时间复杂度: O(N)
辅助空间: O(1)