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
3 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

Applications of Artificial Intelligence in Business
Softwares

Applications of Artificial Intelligence in Business

by admin
June 4, 2025
How to Add Custom Style Variations to WordPress Blocks — Speckyboy
Softwares

How to Add Custom Style Variations to WordPress Blocks — Speckyboy

by admin
June 2, 2025
Smart software replaces expensive sensors for glass wall detection with 96% accuracy
Softwares

Smart software replaces expensive sensors for glass wall detection with 96% accuracy

by admin
June 1, 2025
User Guide For UnoPim PDF Generator
Softwares

User Guide For UnoPim PDF Generator

by admin
May 31, 2025
Infragistics Ultimate 25.1 includes updates across several of its UI toolkit components
Softwares

Infragistics Ultimate 25.1 includes updates across several of its UI toolkit components

by admin
May 29, 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
I Tried Calocurb For 90 Days. Here’s My Review.

I Tried Calocurb For 90 Days. Here’s My Review.

January 8, 2025
How to Build a JavaScript Search [Article]

How to Build a JavaScript Search [Article]

August 30, 2022
Advancement in predicting software vulnerabilities

Advancement in predicting software vulnerabilities

May 21, 2022
Product Information Management Trends (PIM)

Product Information Management Trends (PIM)

February 4, 2022
15 Best Movies Like Parasite

15 Best Movies Like Parasite

February 20, 2022
Credit Scoring Software Development Guide

Credit Scoring Software Development Guide

May 13, 2022
What Are the Best User Provisioning Practices for SaaS Apps

What Are the Best User Provisioning Practices for SaaS Apps

September 9, 2022
Neal Schon Teases Potential Return of Co-Founding Journey Member

Neal Schon Teases Potential Return of Co-Founding Journey Member

November 28, 2022
With Their New Single, 2ŁØT and Aloe Blacc Inspire Us to Become ‘Immortal’

With Their New Single, 2ŁØT and Aloe Blacc Inspire Us to Become ‘Immortal’

June 5, 2025
Has The Survivors Season 2 Been Canceled or Renewed?

Has The Survivors Season 2 Been Canceled or Renewed?

June 5, 2025
Turning Disaster Into Relief: The Purpose-Driven Growth of Insurance Claim HQ

Turning Disaster Into Relief: The Purpose-Driven Growth of Insurance Claim HQ

June 5, 2025
Rogue Traders singer Natalie Bassingthwaighte celebrates second anniversary with partner Pip Loth

Rogue Traders singer Natalie Bassingthwaighte celebrates second anniversary with partner Pip Loth

June 5, 2025
X Adds Verification Explainers To Avoid EU Fines

X Adds Verification Explainers To Avoid EU Fines

June 5, 2025
Leah Remini Addresses Rumors of a Rift with Jennifer Lopez

Leah Remini Addresses Rumors of a Rift with Jennifer Lopez

June 5, 2025
Eminem’s Publishing Company Battles Meta in $109 Million Lawsuit

Eminem’s Publishing Company Battles Meta in $109 Million Lawsuit

June 4, 2025
TESmart thunderbolt KVM switch review – great as long as you’re fine with 4k60FPS or 2K screens

TESmart thunderbolt KVM switch review – great as long as you’re fine with 4k60FPS or 2K screens

June 4, 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

  • With Their New Single, 2ŁØT and Aloe Blacc Inspire Us to Become ‘Immortal’
  • Has The Survivors Season 2 Been Canceled or Renewed?
  • Turning Disaster Into Relief: The Purpose-Driven Growth of Insurance Claim HQ
  • 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.

New Self New Life