forked from Amisha-here/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuglyno.cpp
More file actions
47 lines (38 loc) · 1.06 KB
/
uglyno.cpp
File metadata and controls
47 lines (38 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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;
}