New Self New Life
No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
New Self New Life
No Result
View All Result
Home Softwares

Minimum powers of P and Q to represent N

by admin
2 years ago
in Softwares
Best Coding Practices For Rest API Design
Share on FacebookShare on Twitter


Enhance Article

Save Article

Like Article

Enhance Article

Save Article

Given integer N and values P and Q, The duty is to calculate the minimal variety of powers of P and Q required to generate N.

Notice: The 0th energy of the values can also be thought-about.

Examples:

Enter: N = 15, P = 2, Q = 3
Output: 3
Clarification: We will make 15 through the use of (8, 4, 3) or (9, 3, 3). Each take 3 numbers.

Enter: N = 19, P = 4, Q = 3
Output: 2
Clarification: Within the second case, we are able to make 19 through the use of (16, 3) which is 2 numbers.

Method: Recursion (Memoization)

The Primary thought is to make use of memoization strategy for this drawback, merely we’ll examine methods to succeed in or to generate N by contemplating each  P and Q powers by making recursive calls.

Pseudo Code: 

To examine the powers being utilized in recursive relation.

‘lengthy lengthy int a=1; 
ans = 1e9;   // to retailer potential reply
  
if(energy = 1){ 
    return n;
}                
whereas(n-a >= 0)
{
    ans = min(ans, dp[n-a]);
    a = a*energy;
}
  
return ans+1;

Observe the steps talked about beneath to implement the thought:

  • Initialize a dp[] array of dimension N+1 and initialize it with 1e9.
  • Set, the bottom instances, dp[0] = 0 and dp[1] = 1.
  • Traverse via 2 to N and discover the methods with powers.
    •  Way1 by contemplating energy of P.
    •  Way2 by contemplating energy of Q.
  • Contemplate dp[i] = min(way1, way2).
  • After traversing return dp[N].  

Under is the implementation of the above strategy.

C++

  

#embody <bits/stdc++.h>

utilizing namespace std;

  

int examine(int n, int energy, vector<int>& dp)

{

    

    lengthy lengthy int a = 1;

    int ans = 1e9;

  

    

    if (energy == 1)

        return n;

  

    

    whereas (n - a >= 0) {

        ans = min(ans, dp[n - a]);

        a = a * energy;

    }

  

    return ans + 1;

}

  

int strikes(int n, int p, int q)

{

    

    vector<int> dp(n + 1, 1e9);

  

    

    dp[0] = 0;

    dp[1] = 1;

  

    for (int i = 2; i <= n; ++i) {

        int way1 = examine(i, p, dp);

        int way2 = examine(i, q, dp);

        dp[i] = min(way1, way2);

    }

  

    

    return dp[n];

}

  

int predominant()

{

    int N = 15, P = 2, Q = 1;

  

    

    cout << strikes(N, P, Q) << endl;

    return 0;

}

Time Complexity: O(N * logN)
Auxiliary House:  O(N * logN)

Associated Articles:



Source link

Tags: Minimumpowersrepresent
Previous Post

5 Twitter Updates that Elon Musk Should Consider to Maximize Revenue at the App

Next Post

What It’s Like to Use Apple’s Emergency SOS Feature

Related Posts

AI updates from the past week: IBM watsonx Orchestrate updates, web search in Anthropic API, and more — May 9, 2025
Softwares

AI updates from the past week: IBM watsonx Orchestrate updates, web search in Anthropic API, and more — May 9, 2025

by admin
May 11, 2025
Unlocking the Future of Finance
Softwares

Unlocking the Future of Finance

by admin
May 8, 2025
Address bar tweaks – Vivaldi Browser snapshot 3683.4
Softwares

Address bar tweaks – Vivaldi Browser snapshot 3683.4

by admin
May 7, 2025
A faster, sleeker JavaScript experience
Softwares

A faster, sleeker JavaScript experience

by admin
May 10, 2025
How WordPress Agencies Can Improve Site Building Efficiency — Speckyboy
Softwares

How WordPress Agencies Can Improve Site Building Efficiency — Speckyboy

by admin
May 6, 2025
Next Post
What It’s Like to Use Apple’s Emergency SOS Feature

What It's Like to Use Apple's Emergency SOS Feature

The Worst Christmas Movies Ever Made

The Worst Christmas Movies Ever Made

  • Trending
  • Comments
  • Latest
Cameron Monaghan Discusses Erotic Thriller

Cameron Monaghan Discusses Erotic Thriller

January 13, 2022
Doctor Strange: 12 Best Comic Issues Of The 1990s

Doctor Strange: 12 Best Comic Issues Of The 1990s

December 11, 2021
Anant Ambani wedding: Celebs, wealthy elite attend lavish billionaire festivities – National

Anant Ambani wedding: Celebs, wealthy elite attend lavish billionaire festivities – National

March 1, 2024
The Best Crime Shows on Netflix

The Best Crime Shows on Netflix

May 27, 2023
New TV & Movie Additions

New TV & Movie Additions

October 1, 2021
Guide for Odoo Website Razorpay Checkout Payment Acquirer

Guide for Odoo Website Razorpay Checkout Payment Acquirer

January 6, 2023
Best travel cameras 2023

Best travel cameras 2023

December 13, 2023
Best Cryptocurrency Wallets Comparison | SCAND Blog

Best Cryptocurrency Wallets Comparison | SCAND Blog

July 30, 2022
Eric Clapton’s ‘Unplugged’ and the Peak Dad Rock Moment

Eric Clapton’s ‘Unplugged’ and the Peak Dad Rock Moment

May 12, 2025
Niall Horan Returning to The Voice as Coach

Niall Horan Returning to The Voice as Coach

May 12, 2025
Kate Middleton makes huge announcement with the help of Prince William in new teaser video

Kate Middleton makes huge announcement with the help of Prince William in new teaser video

May 12, 2025
Apple reportedly plans to hike prices of upcoming iPhones

Apple reportedly plans to hike prices of upcoming iPhones

May 12, 2025
Who Is Shane Lowry’s Wife? Wendy’s Job & Kids

Who Is Shane Lowry’s Wife? Wendy’s Job & Kids

May 12, 2025
The Best Luxury Sportswear Brands for Men in 2025

The Best Luxury Sportswear Brands for Men in 2025

May 12, 2025
The Most Visited Websites in the World [Infographic]

The Most Visited Websites in the World [Infographic]

May 12, 2025
I’m Frustrated With How Many New Characters Played A Critical Role At The End Of This Episode

I’m Frustrated With How Many New Characters Played A Critical Role At The End Of This Episode

May 11, 2025
New Self New Life

Your source for entertainment news, celebrities, celebrity news, and Music, Cinema, Digital Lifestyle and Social Media and More !

Categories

  • Celebrity
  • Cinema
  • Devices
  • Digital Lifestyle
  • Entertainment
  • Music
  • Social Media
  • Softwares
  • Uncategorized

Recent Posts

  • Eric Clapton’s ‘Unplugged’ and the Peak Dad Rock Moment
  • Niall Horan Returning to The Voice as Coach
  • Kate Middleton makes huge announcement with the help of Prince William in new teaser video
  • Home
  • Disclaimer
  • DMCA
  • Privacy Policy
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites. slotsfree  creator solana token

No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites.

slots casino slots