-
Notifications
You must be signed in to change notification settings - Fork 0
/
q5_GS_ugly_number.cpp
52 lines (44 loc) · 1.07 KB
/
q5_GS_ugly_number.cpp
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
/*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, …
shows the first 11 ugly numbers. By convention, 1 is included. Write a program to find Nth Ugly Number.*/
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
// } Driver Code Ends
//User function template for C++
class Solution{
public:
// #define ull unsigned long long
/* Function to get the nth ugly number*/
ull getNthUglyNo(int n) {
// code here
ull result = 0;
set<ull> s;
s.insert(1);
n--;
while(n--){
auto itr = s.begin();
ull val = *itr;
s.erase(itr);
s.insert(val * 2);
s.insert(val * 3);
s.insert(val * 5);
}
result = *s.begin();
return result;
}
};
// { Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
Solution ob;
auto ans = ob.getNthUglyNo(n);
cout << ans << "\n";
}
return 0;
}
// } Driver Code Ends