Given an integer n, find the nth magic number. A magic number is a positive integer that can be represented as the sum of distinct powers of 5. In other words, every power of 5 can be used at most once in the representation.
Examples:
Input: n = 1
Output: 5
Explanation: 1'st Magic number is 5.Input: n = 2
Output: 25
Explanation: 2'nd Magic number is 25.
Try It Yourself
Table of Content
[Naive Approach] By Generate Magic Number In Increasing Order - O(n log n) Time and O(n) Space
The idea for a magic number is formed by adding distinct powers of 5. Generate magic numbers in increasing order and count until we reach the nth magic number.
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int nthMagicNo(int n)
{
vector<int> magicNumbers;
// Generate magic numbers from 1 to n.
for (int num = 1; num <= n; num++)
{
int power = 1;
int res = 0;
int x = num;
// Construct the magic number corresponding to num.
while (x > 0)
{
power *= 5;
// Include the current power of 5 if the bit is set.
if (x & 1)
{
res += power;
}
x >>= 1;
}
magicNumbers.push_back(res);
}
return magicNumbers[n - 1];
}
int main()
{
cout << nthMagicNo(1) << endl;
cout << nthMagicNo(2) << endl;
return 0;
}
import java.util.ArrayList;
import java.util.List;
public class GFG {
public static int nthMagicNo(int n) {
List<Integer> magicNumbers = new ArrayList<>();
// Generate magic numbers from 1 to n.
for (int num = 1; num <= n; num++) {
int power = 1;
int res = 0;
int x = num;
// Construct the magic number corresponding to num.
while (x > 0) {
power *= 5;
// Include the current power of 5 if the bit is set.
if ((x & 1)!= 0) {
res += power;
}
x >>= 1;
}
magicNumbers.add(res);
}
return magicNumbers.get(n - 1);
}
public static void main(String[] args) {
System.out.println(nthMagicNo(1));
System.out.println(nthMagicNo(2));
}
}
def nthMagicNo(n):
magicNumbers = []
# Generate magic numbers from 1 to n.
for num in range(1, n + 1):
power = 1
res = 0
x = num
# Construct the magic number corresponding to num.
while x > 0:
power *= 5
# Include the current power of 5 if the bit is set.
if x & 1:
res += power
x >>= 1
magicNumbers.append(res)
return magicNumbers[n - 1]
if __name__ == '__main__':
print(nthMagicNo(1))
print(nthMagicNo(2))
using System;
using System.Collections.Generic;
public class GFG {
public static int nthMagicNo(int n) {
List<int> magicNumbers = new List<int>();
// Generate magic numbers from 1 to n.
for (int num = 1; num <= n; num++) {
int power = 1;
int res = 0;
int x = num;
// Construct the magic number corresponding to num.
while (x > 0) {
power *= 5;
// Include the current power of 5 if the bit is set.
if ((x & 1)!= 0) {
res += power;
}
x >>= 1;
}
magicNumbers.Add(res);
}
return magicNumbers[n - 1];
}
public static void Main() {
Console.WriteLine(nthMagicNo(1));
Console.WriteLine(nthMagicNo(2));
}
}
function nthMagicNo(n) {
let magicNumbers = [];
// Generate magic numbers from 1 to n.
for (let num = 1; num <= n; num++) {
let power = 1;
let res = 0;
let x = num;
// Construct the magic number corresponding to num.
while (x > 0) {
power *= 5;
// Include the current power of 5 if the bit is set.
if (x & 1) {
res += power;
}
x >>= 1;
}
magicNumbers.push(res);
}
return magicNumbers[n - 1];
}
// Driver Code
console.log(nthMagicNo(1));
console.log(nthMagicNo(2));
Output
5 25
[Expected Approach] Binary Representation - O(log n) Time and O(1) Space
The idea for find magic number is based on below observation:
- 1st magic number = 5 -> binary 1
- 2nd magic number = 25 -> binary 10
- 3rd magic number = 30 -> binary 11
- 4th magic number = 125 -> binary 100
The binary representation of n tells us which powers of 5 should be included. For every set bit in n, add the corresponding power of 5.
#include <iostream>
using namespace std;
int nthMagicNo(int n)
{
int power = 1;
int res = 0;
while (n > 0) {
// Generate next power of 5
power = (power * 5);
// If current bit is set, include this power
if (n & 1)
{
res = (res + power);
}
n >>= 1;
}
return res;
}
int main()
{
cout << nthMagicNo(1) << endl;
cout << nthMagicNo(2) << endl;
return 0;
}
public class GFG {
int nthMagicNo(int n) {
int power = 1;
int res = 0;
while (n > 0) {
// Generate next power of 5
power = (power * 5);
// If current bit is set, include this power
if ((n & 1)!= 0) {
res = (res + power);
}
n >>= 1;
}
return res;
}
public static void main(String[] args) {
System.out.println(nthMagicNo(1));
System.out.println(nthMagicNo(2));
}
}
def nthMagicNo(n):
power = 1
res = 0
while n > 0:
# Generate next power of 5
power = (power * 5)
# If current bit is set, include this power
if n & 1:
res = (res + power)
n >>= 1
return res
if __name__ == '__main__':
print(nthMagicNo(1))
print(nthMagicNo(2))
using System;
public class GFG
{
public int nthMagicNo(int n)
{
int power = 1;
int res = 0;
while (n > 0) {
// Generate next power of 5
power = (power * 5);
// If current bit is set, include this power
if ((n & 1)!= 0)
{
res = (res + power);
}
n >>= 1;
}
return res;
}
public static void Main()
{
Program obj = new Program();
Console.WriteLine(obj.nthMagicNo(1));
Console.WriteLine(obj.nthMagicNo(2));
}
}
function nthMagicNo(n) {
let power = 1;
let res = 0;
while (n > 0) {
// Generate next power of 5
power = (power * 5);
// If current bit is set, include this power
if (n & 1) {
res = (res + power);
}
n >>= 1;
}
return res;
}
// Driver Code
console.log(nthMagicNo(1));
console.log(nthMagicNo(2));
Output
5 25