给定一个数组arr[] ,任务是找到将数组转换为斐波那契数列所需的最小增量或减量 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,那么它已经是一个斐波那契数列。
- 否则,尝试前两个元素的所有排列:
- 对于每个排列,计算数组其余元素的操作次数:
- 如果在最多一次操作中无法更改元素,则数组无法转换为斐波那契数列。
- 否则,更新所需的操作次数。
- 用操作次数更新答案。
- 对于每个排列,计算数组其余元素的操作次数:
- 返回答案。
下面是我们方法的实现:
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
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。