-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUglyNumber.cpp
More file actions
85 lines (81 loc) · 1.38 KB
/
UglyNumber.cpp
File metadata and controls
85 lines (81 loc) · 1.38 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<iostream>
using namespace std;
int divBy(int n,int x)
{
while(n%x==0)
{
n/=x;
}
return n;
}
bool isUgly(int n)
{
n=divBy(n,2);
n=divBy(n,3);
n=divBy(n,5);
if(n==1)
{
return true;
}
else
{
return false;
}
}
// Time Complexity O(n) and Space Complexity O(1)
int uglyNumber(int n)
{
int c=0;
int i=0;
while(c!=n)
{
i++;
if (!isUgly(i))
continue;
c++;
}
return i;
}
//Time Complexity O(n) and Time Complexity O(n)
int uglyNumberDp(int n)
{
int *ar=new int[n];
int i2=0;
int i3=0;
int i5=0;
int next2=2;
int next3=3;
int next5=5;
ar[0]=1;
for(int i=1;i<n;i++)
{
int minDiv=min(next2,min(next3,next5));
ar[i]=minDiv;
if(minDiv==next2)
{
i2++;
next2=2*ar[i2];
}
if(minDiv==next3)
{
i3++;
next3=3*ar[i3];
}
if(minDiv==next5)
{
i5++;
next5=5*ar[i5];
}
}
return ar[n-1];
}
int main()
{
int n;
cout<<"Enter the value of n:"<<endl;
cin>>n;
cout<<"Using Simple Approach :\n";
cout<<uglyNumber(n)<<endl;
cout<<"Using DP :\n";
cout<<uglyNumberDp(n)<<endl;
}