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: 13Explanation: Following are the conversion of elements required to make all array elements as Fibonacci Numbers:
Convert arr[0](= 56) to 55. Cost = | 56 – 55| = 1.
Convert arr[1](= 34) to 34. Cost = |34 – 34| = 0.
Convert arr[2](= 23) to 21. Cost = |23 – 21| = 2.
Convert arr[3](= 98) to 89. Cost = |98 – 89| = 9.
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++
#include <bits/stdc++.h>
using namespace std;
int nthFibo(int n)
{
double a = (pow(5, 0.5) + 1) / 2;
double b = (-1*(pow(5, 0.5) ) + 1) / 2;
double r = pow(5, 0.5);
double ans = (pow(a, n) - pow(b, n)) / r;
return int(ans);
}
int nearFibo(int X)
{
double a = (pow(5, 0.5) + 1) / 2;
int n = int(log((pow(5, 0.5)) * X) / log(a));
int nth = nthFibo(n);
int nplus = nthFibo(n + 1);
if (abs(X - nth) < abs(X - nplus))
return nth;
else
return nplus;
}
int getCost(int arr[], int n)
{
int cost = 0;
for(int i = 0; i < n; i++)
{
int fibo = nearFibo(arr[i]);
cost += abs(arr[i] - fibo);
}
return cost;
}
int main()
{
int arr[] = { 56, 34, 23, 98, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << (getCost(arr, n));
}
Java
import java.util.*;
import java.lang.*;
class GFG
{
static int nthFibo(int n)
{
double a = (Math.pow(5, 0.5) + 1) / 2;
double b = (-1*(Math.pow(5, 0.5) ) + 1) / 2;
double r = Math.pow(5, 0.5);
double ans = (Math.pow(a, n) - Math.pow(b, n)) / r;
return (int)ans;
}
static int nearFibo(int X)
{
double a = (Math.pow(5, 0.5) + 1) / 2;
int n = (int)(Math.log((Math.pow(5, 0.5)) * X) / Math.log(a));
int nth = nthFibo(n);
int nplus = nthFibo(n + 1);
if (Math.abs(X - nth) < Math.abs(X - nplus))
return nth;
else
return nplus;
}
static int getCost(int arr[], int n)
{
int cost = 0;
for(int i = 0; i < n; i++)
{
int fibo = nearFibo(arr[i]);
cost += Math.abs(arr[i] - fibo);
}
return cost;
}
public static void main (String[] args)
{
int arr[] = { 56, 34, 23, 98, 7 };
int n = arr.length;
System.out.print(getCost(arr, n));
}
}
Python3
import math
def nthFibo(n):
a = (5**(1 / 2) +1)/2
b = (-5**(1 / 2) +1)/2
r = 5**(1 / 2)
ans = (a**n - b**n)/r
return int(ans)
def nearFibo(X):
a = (5**(1 / 2)+1)/2
n = int(math.log((5**(1 / 2))*X) / math.log(a))
nth = nthFibo(n)
nplus = nthFibo(n + 1)
if abs(X - nth) < abs(X - nplus):
return nth
else:
return nplus
def getCost(arr):
cost = 0
for i in arr:
fibo = nearFibo(i)
cost += abs(i-fibo)
return cost
arr = [56, 34, 23, 98, 7]
print(getCost(arr))
C#
using System;
class GFG{
static int nthFibo(int n)
{
double a = (Math.Pow(5, 0.5) + 1) / 2;
double b = (-1*(Math.Pow(5, 0.5) ) + 1) / 2;
double r = Math.Pow(5, 0.5);
double ans = (Math.Pow(a, n) -
Math.Pow(b, n)) / r;
return (int)ans;
}
static int nearFibo(int X)
{
double a = (Math.Pow(5, 0.5) + 1) / 2;
int n = (int)(Math.Log((Math.Pow(5, 0.5)) * X) /
Math.Log(a));
int nth = nthFibo(n);
int nplus = nthFibo(n + 1);
if (Math.Abs(X - nth) < Math.Abs(X - nplus))
return nth;
else
return nplus;
}
static int getCost(int []arr, int n)
{
int cost = 0;
for(int i = 0; i < n; i++)
{
int fibo = nearFibo(arr[i]);
cost += Math.Abs(arr[i] - fibo);
}
return cost;
}
public static void Main(String []args)
{
int []arr = { 56, 34, 23, 98, 7 };
int n = arr.Length;
Console.WriteLine(getCost(arr, n));
}
}
Javascript
<script>
function nthFibo(n)
{
let a = (Math.pow(5, 0.5) + 1) / 2;
let b = (-1*(Math.pow(5, 0.5) ) + 1) / 2;
let r = Math.pow(5, 0.5);
let ans = (Math.pow(a, n) - Math.pow(b, n)) / r;
return Math.floor(ans);
}
function nearFibo(X)
{
let a = (Math.pow(5, 0.5) + 1) / 2;
let n = Math.floor(Math.log((Math.pow(5, 0.5)) * X) / Math.log(a));
let nth = nthFibo(n);
let nplus = nthFibo(n + 1);
if (Math.abs(X - nth) < Math.abs(X - nplus))
return nth;
else
return nplus;
}
function getCost(arr, n)
{
let cost = 0;
for(let i = 0; i < n; i++)
{
let fibo = nearFibo(arr[i]);
cost += Math.abs(arr[i] - fibo);
}
return cost;
}
let arr = [ 56, 34, 23, 98, 7 ];
let n = arr.length;
document.write(getCost(arr, n));
</script>
Time Complexity: O(N)Auxiliary Space: O(1), since no extra space has been taken.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule. Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
20 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...