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.
Given a number n, the task is to find n’th Ugly number.
Examples:
Input : n = 7
Output : 8
Input : n = 10
Output : 12
Input : n = 15
Output : 24
Input : n = 150
Output : 5832
Method 1 (Simple)
Loop for all positive integers till the ugly number count is smaller than n, if an integer is ugly then increment ugly number count.
To check if a number is ugly, divide the number by greatest divisible powers of 2, 3 and 5, if the number becomes 1 then it is an ugly number otherwise not.
For example, let us see how to check for 300 is ugly or not. Greatest divisible power of 2 is 4, after dividing 300 by 4 we get 75. Greatest divisible power of 3 is 3, after dividing 75 by 3 we get 25. Greatest divisible power of 5 is 25, after dividing 25 by 25 we get 1. Since we get 1 finally, 300 is ugly number.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int maxDivide( int a, int b)
{
while (a % b == 0)
a = a / b;
return a;
}
int isUgly( int no)
{
no = maxDivide(no, 2);
no = maxDivide(no, 3);
no = maxDivide(no, 5);
return (no == 1) ? 1 : 0;
}
int getNthUglyNo( int n)
{
int i = 1;
int count = 1;
while (n > count)
{
i++;
if (isUgly(i))
count++;
}
return i;
}
int main()
{
unsigned no = getNthUglyNo(150);
cout << "150th ugly no. is " << no;
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
int maxDivide( int a, int b)
{
while (a % b == 0)
a = a / b;
return a;
}
int isUgly( int no)
{
no = maxDivide(no, 2);
no = maxDivide(no, 3);
no = maxDivide(no, 5);
return (no == 1) ? 1 : 0;
}
int getNthUglyNo( int n)
{
int i = 1;
int count = 1;
while (n > count) {
i++;
if (isUgly(i))
count++;
}
return i;
}
int main()
{
unsigned no = getNthUglyNo(150);
printf ( "150th ugly no. is %d " , no);
getchar ();
return 0;
}
|
Java
import java.io.*;
public class GFG {
static int maxDivide( int a, int b)
{
while (a % b == 0 )
a = a / b;
return a;
}
static int isUgly( int no)
{
no = maxDivide(no, 2 );
no = maxDivide(no, 3 );
no = maxDivide(no, 5 );
return (no == 1 ) ? 1 : 0 ;
}
static int getNthUglyNo( int n)
{
int i = 1 ;
int count = 1 ;
while (n > count) {
i++;
if (isUgly(i) == 1 )
count++;
}
return i;
}
public static void main(String args[])
{
int no = getNthUglyNo( 150 );
System.out.println( "150th ugly "
+ "no. is " + no);
}
}
|
Python3
def maxDivide(a, b):
while a % b = = 0 :
a = a / b
return a
def isUgly(no):
no = maxDivide(no, 2 )
no = maxDivide(no, 3 )
no = maxDivide(no, 5 )
return 1 if no = = 1 else 0
def getNthUglyNo(n):
i = 1
count = 1
while n > count:
i + = 1
if isUgly(i):
count + = 1
return i
no = getNthUglyNo( 150 )
print ( "150th ugly no. is " , no)
|
C#
using System;
class GFG {
static int maxDivide( int a, int b)
{
while (a % b == 0)
a = a / b;
return a;
}
static int isUgly( int no)
{
no = maxDivide(no, 2);
no = maxDivide(no, 3);
no = maxDivide(no, 5);
return (no == 1) ? 1 : 0;
}
static int getNthUglyNo( int n)
{
int i = 1;
int count = 1;
while (n > count) {
i++;
if (isUgly(i) == 1)
count++;
}
return i;
}
public static void Main()
{
int no = getNthUglyNo(150);
Console.WriteLine( "150th ugly"
+ " no. is " + no);
}
}
|
Javascript
<script>
function maxDivide(a , b) {
while (a % b == 0)
a = a / b;
return a;
}
function isUgly(no) {
no = maxDivide(no, 2);
no = maxDivide(no, 3);
no = maxDivide(no, 5);
return (no == 1) ? 1 : 0;
}
function getNthUglyNo(n)
{
var i = 1;
var count = 1;
while (n > count)
{
i++;
if (isUgly(i) == 1)
count++;
}
return i;
}
var no = getNthUglyNo(150);
document.write( "150th ugly " + "no. is " + no);
</script>
|
PHP
<?php
function maxDivide( $a , $b )
{
while ( $a % $b == 0)
$a = $a / $b ;
return $a ;
}
function isUgly( $no )
{
$no = maxDivide( $no , 2);
$no = maxDivide( $no , 3);
$no = maxDivide( $no , 5);
return ( $no == 1)? 1 : 0;
}
function getNthUglyNo( $n )
{
$i = 1;
$count = 1;
while ( $n > $count )
{
$i ++;
if (isUgly( $i ))
$count ++;
}
return $i ;
}
$no = getNthUglyNo(150);
echo "150th ugly no. is " . $no ;
?>
|
Output
150th ugly no. is 5832
This method is not time efficient as it checks for all integers until ugly number count becomes n, but space complexity of this method is O(1)
Method 2 (Use Dynamic Programming)
Here is a time efficient solution with O(n) extra space. The ugly-number sequence is 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …
because every number can only be divided by 2, 3, 5, one way to look at the sequence is to split the sequence to three groups as below:
(1) 1×2, 2×2, 3×2, 4×2, 5×2, …
(2) 1×3, 2×3, 3×3, 4×3, 5×3, …
(3) 1×5, 2×5, 3×5, 4×5, 5×5, …
We can find that every subsequence is the ugly-sequence itself (1, 2, 3, 4, 5, …) multiply 2, 3, 5. Then we use similar merge method as merge sort, to get every ugly number from the three subsequences. Every step we choose the smallest one, and move one step after.
1 Declare an array for ugly numbers: ugly[n]
2 Initialize first ugly no: ugly[0] = 1
3 Initialize three array index variables i2, i3, i5 to point to
1st element of the ugly array:
i2 = i3 = i5 =0;
4 Initialize 3 choices for the next ugly no:
next_multiple_of_2 = ugly[i2]*2;
next_multiple_of_3 = ugly[i3]*3
next_multiple_of_5 = ugly[i5]*5;
5 Now go in a loop to fill all ugly numbers till 150:
For (i = 1; i < 150; i++ )
{
/* These small steps are not optimized for good
readability. Will optimize them in C program */
next_ugly_no = Min(next_multiple_of_2,
next_multiple_of_3,
next_multiple_of_5);
ugly[i] = next_ugly_no
if (next_ugly_no == next_multiple_of_2)
{
i2 = i2 + 1;
next_multiple_of_2 = ugly[i2]*2;
}
if (next_ugly_no == next_multiple_of_3)
{
i3 = i3 + 1;
next_multiple_of_3 = ugly[i3]*3;
}
if (next_ugly_no == next_multiple_of_5)
{
i5 = i5 + 1;
next_multiple_of_5 = ugly[i5]*5;
}
}/* end of for loop */
6.return next_ugly_no
Example:
Let us see how it works
initialize
ugly[] = | 1 |
i2 = i3 = i5 = 0;
First iteration
ugly[1] = Min(ugly[i2]*2, ugly[i3]*3, ugly[i5]*5)
= Min(2, 3, 5)
= 2
ugly[] = | 1 | 2 |
i2 = 1, i3 = i5 = 0 (i2 got incremented )
Second iteration
ugly[2] = Min(ugly[i2]*2, ugly[i3]*3, ugly[i5]*5)
= Min(4, 3, 5)
= 3
ugly[] = | 1 | 2 | 3 |
i2 = 1, i3 = 1, i5 = 0 (i3 got incremented )
Third iteration
ugly[3] = Min(ugly[i2]*2, ugly[i3]*3, ugly[i5]*5)
= Min(4, 6, 5)
= 4
ugly[] = | 1 | 2 | 3 | 4 |
i2 = 2, i3 = 1, i5 = 0 (i2 got incremented )
Fourth iteration
ugly[4] = Min(ugly[i2]*2, ugly[i3]*3, ugly[i5]*5)
= Min(6, 6, 5)
= 5
ugly[] = | 1 | 2 | 3 | 4 | 5 |
i2 = 2, i3 = 1, i5 = 1 (i5 got incremented )
Fifth iteration
ugly[4] = Min(ugly[i2]*2, ugly[i3]*3, ugly[i5]*5)
= Min(6, 6, 10)
= 6
ugly[] = | 1 | 2 | 3 | 4 | 5 | 6 |
i2 = 3, i3 = 2, i5 = 1 (i2 and i3 got incremented )
Will continue same way till I < 150
C++
#include <bits/stdc++.h>
using namespace std;
unsigned getNthUglyNo(unsigned n)
{
unsigned ugly[n];
unsigned i2 = 0, i3 = 0, i5 = 0;
unsigned next_multiple_of_2 = 2;
unsigned next_multiple_of_3 = 3;
unsigned next_multiple_of_5 = 5;
unsigned next_ugly_no = 1;
ugly[0] = 1;
for ( int i = 1; i < n; i++) {
next_ugly_no = min(
next_multiple_of_2,
min(next_multiple_of_3, next_multiple_of_5));
ugly[i] = next_ugly_no;
if (next_ugly_no == next_multiple_of_2) {
i2 = i2 + 1;
next_multiple_of_2 = ugly[i2] * 2;
}
if (next_ugly_no == next_multiple_of_3) {
i3 = i3 + 1;
next_multiple_of_3 = ugly[i3] * 3;
}
if (next_ugly_no == next_multiple_of_5) {
i5 = i5 + 1;
next_multiple_of_5 = ugly[i5] * 5;
}
}
return next_ugly_no;
}
int main()
{
int n = 150;
cout << getNthUglyNo(n);
return 0;
}
|
C
#include <stdio.h>
int min( int num1, int num2)
{
return (num1 > num2) ? num2 : num1;
}
unsigned getNthUglyNo(unsigned n)
{
unsigned ugly[n];
unsigned i2 = 0, i3 = 0, i5 = 0;
unsigned next_multiple_of_2 = 2;
unsigned next_multiple_of_3 = 3;
unsigned next_multiple_of_5 = 5;
unsigned next_ugly_no = 1;
ugly[0] = 1;
for ( int i = 1; i < n; i++) {
next_ugly_no = min(
next_multiple_of_2,
min(next_multiple_of_3, next_multiple_of_5));
ugly[i] = next_ugly_no;
if (next_ugly_no == next_multiple_of_2) {
i2 = i2 + 1;
next_multiple_of_2 = ugly[i2] * 2;
}
if (next_ugly_no == next_multiple_of_3) {
i3 = i3 + 1;
next_multiple_of_3 = ugly[i3] * 3;
}
if (next_ugly_no == next_multiple_of_5) {
i5 = i5 + 1;
next_multiple_of_5 = ugly[i5] * 5;
}
}
return next_ugly_no;
}
int main()
{
int n = 150;
printf ( "%u" ,getNthUglyNo(n));
return 0;
}
|
Java
import java.lang.Math;
import java.io.*;
class UglyNumber
{
int getNthUglyNo( int n)
{
int ugly[] = new int [n];
int i2 = 0 , i3 = 0 , i5 = 0 ;
int next_multiple_of_2 = 2 ;
int next_multiple_of_3 = 3 ;
int next_multiple_of_5 = 5 ;
int next_ugly_no = 1 ;
ugly[ 0 ] = 1 ;
for ( int i = 1 ; i < n; i++)
{
next_ugly_no
= Math.min(next_multiple_of_2,
Math.min(next_multiple_of_3,
next_multiple_of_5));
ugly[i] = next_ugly_no;
if (next_ugly_no == next_multiple_of_2)
{
i2 = i2 + 1 ;
next_multiple_of_2 = ugly[i2] * 2 ;
}
if (next_ugly_no == next_multiple_of_3)
{
i3 = i3 + 1 ;
next_multiple_of_3 = ugly[i3] * 3 ;
}
if (next_ugly_no == next_multiple_of_5)
{
i5 = i5 + 1 ;
next_multiple_of_5 = ugly[i5] * 5 ;
}
}
return next_ugly_no;
}
public static void main(String args[])
{
int n = 150 ;
UglyNumber obj = new UglyNumber();
System.out.println(obj.getNthUglyNo(n));
}
}
|
Python
def getNthUglyNo(n):
ugly = [ 0 ] * n
ugly[ 0 ] = 1
i2 = i3 = i5 = 0
next_multiple_of_2 = 2
next_multiple_of_3 = 3
next_multiple_of_5 = 5
for l in range ( 1 , n):
ugly[l] = min (next_multiple_of_2,
next_multiple_of_3,
next_multiple_of_5)
if ugly[l] = = next_multiple_of_2:
i2 + = 1
next_multiple_of_2 = ugly[i2] * 2
if ugly[l] = = next_multiple_of_3:
i3 + = 1
next_multiple_of_3 = ugly[i3] * 3
if ugly[l] = = next_multiple_of_5:
i5 + = 1
next_multiple_of_5 = ugly[i5] * 5
return ugly[ - 1 ]
def main():
n = 150
print getNthUglyNo(n)
if __name__ = = '__main__' :
main()
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int getNthUglyNo( int n)
{
int [] ugly = new int [n];
int i2 = 0, i3 = 0, i5 = 0;
int next_multiple_of_2 = 2;
int next_multiple_of_3 = 3;
int next_multiple_of_5 = 5;
int next_ugly_no = 1;
ugly[0] = 1;
for ( int i = 1; i < n; i++)
{
next_ugly_no
= Math.Min(next_multiple_of_2,
Math.Min(next_multiple_of_3,
next_multiple_of_5));
ugly[i] = next_ugly_no;
if (next_ugly_no == next_multiple_of_2)
{
i2 = i2 + 1;
next_multiple_of_2 = ugly[i2] * 2;
}
if (next_ugly_no == next_multiple_of_3)
{
i3 = i3 + 1;
next_multiple_of_3 = ugly[i3] * 3;
}
if (next_ugly_no == next_multiple_of_5)
{
i5 = i5 + 1;
next_multiple_of_5 = ugly[i5] * 5;
}
}
return next_ugly_no;
}
public static void Main()
{
int n = 150;
Console.WriteLine(getNthUglyNo(n));
}
}
|
Javascript
<script>
function getNthUglyNo(n)
{
var ugly = Array.from({length: n}, (_, i) => 0);
var i2 = 0, i3 = 0, i5 = 0;
var next_multiple_of_2 = 2;
var next_multiple_of_3 = 3;
var next_multiple_of_5 = 5;
var next_ugly_no = 1;
ugly[0] = 1;
for (i = 1; i < n; i++)
{
next_ugly_no
= Math.min(next_multiple_of_2,
Math.min(next_multiple_of_3,
next_multiple_of_5));
ugly[i] = next_ugly_no;
if (next_ugly_no == next_multiple_of_2)
{
i2 = i2 + 1;
next_multiple_of_2 = ugly[i2] * 2;
}
if (next_ugly_no == next_multiple_of_3)
{
i3 = i3 + 1;
next_multiple_of_3 = ugly[i3] * 3;
}
if (next_ugly_no == next_multiple_of_5)
{
i5 = i5 + 1;
next_multiple_of_5 = ugly[i5] * 5;
}
}
return next_ugly_no;
}
var n = 150;
document.write(getNthUglyNo(n));
</script>
|
PHP
<?php
function getNthUglyNo( $n )
{
$ugly = array_fill (0, $n , 0);
$i2 = 0;
$i3 = 0;
$i5 = 0;
$next_multiple_of_2 = 2;
$next_multiple_of_3 = 3;
$next_multiple_of_5 = 5;
$next_ugly_no = 1;
$ugly [0] = 1;
for ( $i = 1; $i < $n ; $i ++)
{
$next_ugly_no = min( $next_multiple_of_2 ,
min( $next_multiple_of_3 ,
$next_multiple_of_5 ));
$ugly [ $i ] = $next_ugly_no ;
if ( $next_ugly_no ==
$next_multiple_of_2 )
{
$i2 = $i2 + 1;
$next_multiple_of_2 = $ugly [ $i2 ] * 2;
}
if ( $next_ugly_no ==
$next_multiple_of_3 )
{
$i3 = $i3 + 1;
$next_multiple_of_3 = $ugly [ $i3 ] * 3;
}
if ( $next_ugly_no ==
$next_multiple_of_5 )
{
$i5 = $i5 + 1;
$next_multiple_of_5 = $ugly [ $i5 ] * 5;
}
}
return $next_ugly_no ;
}
$n = 150;
echo getNthUglyNo( $n );
?>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Super Ugly Number (Number whose prime factors are in the given set)
Method 3 (Using SET Data Structure in C++, Javascript and TreeSet in JAVA)
In this method, SET data structure to store the minimum of the 3 generated ugly numbers which will the ith Ugly Number stored at the first position of the set. SET Data Structure as a set stores all the element in ascending order
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int nthUglyNumber( int n)
{
if (n == 1 or n == 2 or n == 3 or n == 4 or n == 5)
return n;
set< long long int > s;
s.insert(1);
n--;
while (n) {
auto it = s.begin();
long long int x = *it;
s.erase(it);
s.insert(x * 2);
s.insert(x * 3);
s.insert(x * 5);
n--;
}
return *s.begin();
}
int main()
{
int n = 150;
cout << nthUglyNumber(n);
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static long nthUglyNumber( int n)
{
TreeSet<Long> t = new TreeSet<>();
t.add(1L);
int i = 1 ;
while (i < n) {
long temp = t.pollFirst();
t.add(temp * 2 );
t.add(temp * 3 );
t.add(temp * 5 );
i++;
}
return t.pollFirst();
}
public static void main(String[] args)
{
int n = 150 ;
System.out.println(nthUglyNumber(n));
}
}
|
Python3
def nthUglyNumber(n):
if (n = = 1 or n = = 2 or n = = 3 or n = = 4 or n = = 5 ):
return n
s = [ 1 ]
n - = 1
while (n):
it = s[ 0 ]
x = it
s = s[ 1 :]
s = set (s)
s.add(x * 2 )
s.add(x * 3 )
s.add(x * 5 )
s = list (s)
s.sort()
n - = 1
return s[ 0 ]
n = 150
print ( nthUglyNumber(n))
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
public class GFG {
static long nthUglyNumber( int n) {
SortedSet< long > t = new SortedSet< long >();
t.Add(1L);
int i = 1;
while (i < n)
{
long temp = t.FirstOrDefault();
t.Remove(temp);
t.Add(temp * 2);
t.Add(temp * 3);
t.Add(temp * 5);
i++;
}
return t.FirstOrDefault();
}
public static void Main(String[] args) {
int n = 150;
Console.WriteLine(nthUglyNumber(n));
}
}
|
Javascript
<script>
function nthUglyNumber(n)
{
if (n == 1 || n == 2 || n == 3 || n == 4 || n == 5)
{
return n;
}
var s = [1];
n = n - 1;
while (n)
{
var it = s[0];
var x = it;
s = s.slice(1);
s = new Set(s);
s.add(x * 2);
s.add(x * 3);
s.add(x * 5);
s = Array.from(s);
s.sort( function (a, b){ return a - b});
n = n - 1;
}
return s[0];
}
var n = 150;
document.write(nthUglyNumber(n));
</script>
|
Time Complexity:- O(N log N)
Auxiliary Space:- O(N)
Method 4(Using Binary Search)
- This method is suitable if you have a max value for n. The no will be of form x=pow(2,p)*pow(3,q)*pow(5,r).
- Search from low=1 to high =21474836647. We are expecting nth ugly no to be in this range.
- So we do a binary search. Now suppose we are at mid now we are going to find the total number of ugly numbers less than mid and put our conditions accordingly.
Below is the rough CPP code:
C++
#include <bits/stdc++.h>
using namespace std;
int nthUglyNumber( int n)
{
int pow [40] = { 1 };
for ( int i = 1; i <= 30; ++i)
pow [i] = pow [i - 1] * 2;
int l = 1, r = 2147483647;
int ans = -1;
while (l <= r) {
int mid = l + ((r - l) / 2);
int cnt = 0;
for ( long long i = 1; i <= mid; i *= 5)
{
for ( long long j = 1; j * i <= mid; j *= 3)
{
cnt += upper_bound( pow , pow + 31,
mid / (i * j)) - pow ;
}
}
if (cnt < n)
l = mid + 1;
else
r = mid - 1, ans = mid;
}
return ans;
}
int main()
{
int n = 150;
cout << nthUglyNumber(n);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG
{
static int upperBound( int [] a, int low,
int high, int element)
{
while (low < high)
{
int middle = low + (high - low)/ 2 ;
if (a[middle] > element)
high = middle;
else
low = middle + 1 ;
}
return low;
}
static int nthUglyNumber( int n)
{
int pow[] = new int [ 40 ];
Arrays.fill(pow, 1 );
for ( int i = 1 ; i <= 30 ; ++i)
pow[i] = pow[i - 1 ] * 2 ;
int l = 1 , r = 2147483647 ;
int ans = - 1 ;
while (l <= r) {
int mid = l + ((r - l) / 2 );
int cnt = 0 ;
for ( long i = 1 ; i <= mid; i *= 5 )
{
for ( long j = 1 ; j * i <= mid; j *= 3 )
{
cnt += upperBound(pow, 0 , 31 ,
( int )(mid / (i * j)));
}
}
if (cnt < n)
l = mid + 1 ;
else {
r = mid - 1 ; ans = mid;
}
}
return ans;
}
public static void main(String[] args)
{
int n = 150 ;
System.out.print(nthUglyNumber(n));
}
}
|
Python3
def upper_bound(a, low, high, element) :
while (low < high) :
middle = low + (high - low) / / 2
if (a[middle] > element) :
high = middle
else :
low = middle + 1
return low
def nthUglyNumber(n) :
pow = [ 1 ] * 40
for i in range ( 1 , 31 ):
pow [i] = pow [i - 1 ] * 2
l = 1
r = 2147483647
ans = - 1
while (l < = r) :
mid = l + ((r - l) / / 2 )
cnt = 0
i = 1
while (i < = mid) :
j = 1
while (j * i < = mid) :
cnt + = upper_bound( pow , 0 , 31 , mid / / (i * j))
j * = 3
i * = 5
if (cnt < n):
l = mid + 1
else :
r = mid - 1
ans = mid
return ans
n = 150
print (nthUglyNumber(n))
|
C#
using System;
public class GFG {
static int upperBound( int [] a, int low,
int high, int element)
{
while (low < high) {
int middle = low + (high - low) / 2;
if (a[middle] > element)
high = middle;
else
low = middle + 1;
}
return low;
}
static int nthUglyNumber( int n) {
int []pow = new int [40];
for ( int i = 0; i <40; ++i)
pow[i] = 1;
for ( int i = 1; i <= 30; ++i)
pow[i] = pow[i - 1] * 2;
int l = 1, r = 2147483647;
int ans = -1;
while (l <= r) {
int mid = l + ((r - l) / 2);
int cnt = 0;
for ( long i = 1; i <= mid; i *= 5)
{
for ( long j = 1; j * i <= mid; j *= 3)
{
cnt += upperBound(pow, 0, 31,
( int ) (mid / (i * j)));
}
}
if (cnt < n)
l = mid + 1;
else {
r = mid - 1;
ans = mid;
}
}
return ans;
}
public static void Main(String[] args) {
int n = 150;
Console.Write(nthUglyNumber(n));
}
}
|
Javascript
<script>
function upperBound(a , low , high , element) {
while (low < high) {
var middle = low + parseInt((high - low) / 2);
if (a[middle] > element)
high = middle;
else
low = middle + 1;
}
return low;
}
function nthUglyNumber(n) {
var pow = Array(40).fill(1);
for (i = 1; i <= 30; ++i)
pow[i] = pow[i - 1] * 2;
var l = 1, r = 2147483647;
var ans = -1;
while (l <= r) {
var mid = l + parseInt((r - l) / 2);
var cnt = 0;
for (i = 1; i <= mid; i *= 5)
{
for (j = 1; j * i <= mid; j *= 3)
{
cnt += upperBound(pow, 0, 31, parseInt( (mid / (i * j))));
}
}
if (cnt < n)
l = mid + 1;
else {
r = mid - 1;
ans = mid;
}
}
return ans;
}
var n = 150;
document.write(nthUglyNumber(n));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Please write comments if you find any bug in the above program or other ways to solve the same problem.
Share your thoughts in the comments
Please Login to comment...