Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,089 views
in Online Assessments by Expert (115,900 points) | 1,089 views

3 Answers

0 like 0 dislike
Best answer

image
imageimage
image

 

Image of the problem..

by Expert (115,900 points)
0 like 0 dislike
Easy and Optimised binary search approach

Time Complexity - O(n*log(n))
#include<bits/stdc++.h>
using namespace std;

vector<long long int> fib;
void generate_fib()
{
    fib.push_back(1);
    fib.push_back(1);
    int c=2;
    
    while(1)
    {
        long long int i=fib[c-1]+fib[c-2];
        
        if(i>1000000000)
            break;
        
        fib.push_back(i);
        c++;
    }
}

int main()
{
    generate_fib();
    int t;
    cin>>t;
    
    while(t--)
    {
        long long int k;
        cin>>k;
        
        int s=0,e=fib.size()-1,pos=-1;
        while(s<=e)
        {
            int mid=s+((e-s)>>1);
            
            if(fib[mid]>k)
                e=mid-1;
            else
            {
                pos=mid;
                s=mid+1;
            }
        }
        
        int ans=0;
        s=0,e=pos,pos=-1;
        
        while(k>0)
        {
            while(s<=e)
            {
                int mid=s+((e-s)>>1);
                
                if(fib[mid]>k)
                    e=mid-1;
                    
                else
                {
                    pos=mid;
                    s=mid+1;
                }
            }
            
            ans++;
            k=k-fib[pos];
            e=pos;
        }
        
        cout<<ans<<"\n";
    }
}
by Expert (115,900 points)
0 like 0 dislike
def findMinimumSteps(tc):
  fib, cnt = [1, 1], [0] * len(tc)
  for i, k in sorted(enumerate(tc), key=operator.itemgetter(1)):
    while (num := fib[-1] + fib[-2]) <= k:
      fib.append(num)
    hi = len(fib)
    while k:
      hi = bisect.bisect(fib, k, hi=hi) - 1
      k -= fib[hi]
      cnt[i] += 1
    return cnt
by Expert (115,900 points)