Related Articles
Minimize cost of converting all array elements to Fibonacci Numbers
• Last Updated : 05 May, 2021

Given an array arr[] consisting of N integers, the task is to minimize the cost of converting all array elements to a Fibonacci Number, where the cost of converting a number A to B is the absolute difference between A and B.

Examples:

Input: arr[] = {56, 34, 23, 98, 7}
Output: 13
Explanation:
Following are the conversion of elements required to make all array elements as Fibonacci Numbers:

1. Convert arr[0](= 56) to 55. Cost = | 56 – 55| = 1.
2. Convert arr[1](= 34) to 34. Cost = |34 – 34| = 0.
3. Convert arr[2](= 23) to 21. Cost = |23 – 21| = 2.
4. Convert arr[3](= 98) to 89. Cost = |98 – 89| = 9.
5. Convert arr[4](= 7) to 8. Cost = |7 – 8| = 1.

Therefore, the total cost of changing all array elements to Fibonacci numbers = 1 + 0 + 2 + 9 + 1 = 13.

Input: {543, 32, 7, 23, 641}
Output: 103

Approach: The given problem can be solved by replacing each array element with its nearest Fibonacci Number to get the minimum cost for changing all array elements to Fibonacci Number. Follow the step below to solve the given problem:

• Initialize a variable say, cost that stores the minimum cost of changing all array elements to Fibonacci Number.
• Traverse the given array arr[] and perform the following steps:
• To find the Fibonacci number closest to arr[i], first, find the value of N such that Nth Fibonacci Number is arr[i] using the formula
• Now, find the Nth and (N + 1)th Fibonacci Number, say X and Y respectively and add the minimum of absolute values of (X – arr[i]) and (Y – arr[i]) to the cost.
• After completing the above steps, print the value of cost as the resultant minimum cost.

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach#include using namespace std; // Function to find the// N-th Fibonacci Numberint nthFibo(int n){         // Find the value of a, b, and r    double a = (pow(5, 0.5) + 1) / 2;    double b = (-1*(pow(5, 0.5) ) + 1) / 2;    double r = pow(5, 0.5);     // Find the N-th Fibonacci    double ans = (pow(a, n) - pow(b, n)) / r;     // Return the result    return int(ans);} // Function to find the Fibonacci// number which is nearest to Xint nearFibo(int X){    double a = (pow(5, 0.5) + 1) / 2;     // Calculate the value of n for X    int n = int(log((pow(5, 0.5)) * X) / log(a));     int nth = nthFibo(n);    int nplus = nthFibo(n + 1);     // Return the nearest    // Fibonacci Number    if (abs(X - nth) < abs(X - nplus))        return nth;    else        return nplus;} // Function to find the minimum// cost to conevert all array// elements to Fibonacci Numbersint getCost(int arr[], int n){     // Stores the total minimum cost    int cost = 0;     // Traverse the given array arr[]    for(int i = 0; i < n; i++)    {                 // Find the nearest        // Fibonacci Number        int fibo = nearFibo(arr[i]);         // Add the cost        cost += abs(arr[i] - fibo);    }         // Return the final cost    return cost;} // Driver Codeint main(){    int arr[] = { 56, 34, 23, 98, 7 };    int n = sizeof(arr) / sizeof(arr[0]);    cout << (getCost(arr, n));} // This code is contributed by ukasp

## Python3

 # Python program for the above approach import math # Function to find the# N-th Fibonacci Numberdef nthFibo(n):       # Find the value of a, b, and r    a = (5**(1 / 2) +1)/2    b = (-5**(1 / 2) +1)/2    r = 5**(1 / 2)      # Find the N-th Fibonacci    ans = (a**n - b**n)/r     # Return the result    return int(ans) # Function to find the Fibonacci# number which is nearest to Xdef nearFibo(X):       a = (5**(1 / 2)+1)/2         # Calculate the value of n for X    n = int(math.log((5**(1 / 2))*X) / math.log(a))     nth = nthFibo(n)    nplus = nthFibo(n + 1)     # Return the nearest    # Fibonacci Number    if abs(X - nth) < abs(X - nplus):        return nth    else:        return nplus # Function to find the minimum# cost to conevert all array# elements to Fibonacci Numbersdef getCost(arr):       # Stores the total minimum cost    cost = 0         # Traverse the given array arr[]    for i in arr:               # Find the nearest        # Fibonacci Number        fibo = nearFibo(i)                 # Add the cost        cost += abs(i-fibo)     # Return the final cost    return cost # Driver Code arr = [56, 34, 23, 98, 7] print(getCost(arr))
Output:
13

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up