Skip to content

Commit

Permalink
Add nthugly solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Jatin-Goyal5 committed Oct 14, 2020
1 parent 00fe513 commit 0631032
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions DP/nthuglyno.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// ugly no solution
// using Dp
// Jatin Goyal
// problem

//Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 25 … shows some ugly numbers.
//By convention, 1 is included.

#include<bits/stdc++.h>
using namespace std;

int nthUglyNumber(int n) {

vector<int> dp(n);
dp[0]=1;
int multi2= dp[0]*2;
int multi3 = dp[0]*3;
int multi5 = dp[0]*5;
int i2=0,i3=0,i5=0;
for(int i =1 ; i <n; i++ ){
dp[i]= min(multi2, min(multi3,multi5));
if(dp[i] == multi2){
i2+=1;
multi2=dp[i2]*2;

}
if(dp[i] == multi3){
i3+=1;
multi3=dp[i3]*3;

}
if(dp[i] == multi5){
i5+=1;
multi5= dp[i5]*5;

}
}
return dp[n-1];

}
int main(int argc ,char **argv){
int n;
cin>> n;
cout<<nthUglyNumber(n);

return 0;
}

0 comments on commit 0631032

Please sign in to comment.