Nth number made up of odd digits only
Given an integer N, the task is to find the Nth number made up of odd digits (1, 3, 5, 7, 9) only.
The first few numbers made up of odd digits are 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 31, …
Examples:
Input: N = 7
Output: 13
1, 3, 5, 7, 9, 11, 13
13 is the 7th number in the series
Input: N = 10
Output: 19
Approach 1 (Simple) : Starting from 1, keep checking if the number is made up of only odd digits (1, 3, 5, 7, 9) and stop when nth such number is found.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findNthOddDigitNumber( int n)
{
int count = 0;
for ( int i = 1;; i++) {
int num = i;
bool isMadeOfOdd = true ;
while (num != 0) {
if (num % 10 == 0
|| num % 10 == 2
|| num % 10 == 4
|| num % 10 == 6
|| num % 10 == 8) {
isMadeOfOdd = false ;
break ;
}
num = num / 10;
}
if (isMadeOfOdd == true )
count++;
if (count == n)
return i;
}
}
int main()
{
int n = 10;
cout << findNthOddDigitNumber(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findNthOddDigitNumber( int n)
{
int count = 0 ;
for ( int i = 1 ;; i++) {
int num = i;
boolean isMadeOfOdd = true ;
while (num != 0 ) {
if (num % 10 == 0
|| num % 10 == 2
|| num % 10 == 4
|| num % 10 == 6
|| num % 10 == 8 ) {
isMadeOfOdd = false ;
break ;
}
num = num / 10 ;
}
if (isMadeOfOdd == true )
count++;
if (count == n)
return i;
}
}
public static void main (String[] args) {
int n = 10 ;
System.out.println (findNthOddDigitNumber(n));
}
}
|
Python3
def findNthOddDigitNumber(n) :
count = 0
i = 1
while True :
num = i
isMadeOfOdd = True
while num ! = 0 :
if (num % 10 = = 0 or num % 10 = = 2 or
num % 10 = = 4 or num % 10 = = 6 or
num % 10 = = 8 ) :
isMadeOfOdd = False
break
num / / = 10
if isMadeOfOdd = = True :
count + = 1
if count = = n :
return i
i + = 1
if __name__ = = "__main__" :
n = 10
print (findNthOddDigitNumber(n))
|
C#
using System;
class GFG
{
static int findNthOddDigitNumber( int n)
{
int count = 0;
for ( int i = 1;; i++)
{
int num = i;
bool isMadeOfOdd = true ;
while (num != 0)
{
if (num % 10 == 0 || num % 10 == 2 ||
num % 10 == 4 || num % 10 == 6 ||
num % 10 == 8)
{
isMadeOfOdd = false ;
break ;
}
num = num / 10;
}
if (isMadeOfOdd == true )
count++;
if (count == n)
return i;
}
}
static public void Main ()
{
int n = 10;
Console.WriteLine(findNthOddDigitNumber(n));
}
}
|
PHP
<?php
function findNthOddDigitNumber( $n )
{
$count = 0;
$i = 1;
while (true)
{
$num = $i ;
$isMadeOfOdd = true;
while ( $num != 0)
{
if ( $num % 10 == 0 or $num % 10 == 2 or
$num % 10 == 4 or $num % 10 == 6 or
$num % 10 == 8)
{
$isMadeOfOdd = false;
break ;
}
$num = (int)( $num / 10);
}
if ( $isMadeOfOdd == true)
$count += 1;
if ( $count == $n )
return $i ;
$i += 1;
}
}
$n = 10;
print (findNthOddDigitNumber( $n ));
?>
|
Javascript
<script>
function findNthOddDigitNumber(n)
{
let count = 0;
for (let i = 1;; i++)
{
let num = i;
let isMadeOfOdd = true ;
while (num != 0)
{
if (num % 10 == 0 ||
num % 10 == 2 ||
num % 10 == 4 ||
num % 10 == 6 ||
num % 10 == 8)
{
isMadeOfOdd = false ;
break ;
}
num = Math.floor(num / 10);
}
if (isMadeOfOdd == true )
count++;
if (count === n)
return i;
}
}
let n = 10;
document.write(findNthOddDigitNumber(n));
</script>
|
Time Complexity: O(n log10n), where N represents the size integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Approach 2 (Queue Based):
The idea is to generate all numbers (smaller than n) containing odd digits only. How to generate all numbers smaller than n with odd digits? We use queue for this. Initially, we push ‘1’, ‘3’, ‘5’, ‘7’ and ‘9’ to the queue. Then we run a loop while count of processed elements is smaller than n.
We pop an item one by one and for every popped item x, we generate next numbers x*10 + 1, x*10 + 3, x*10 + 5, x*10 + 7 and x*10 + 9. We enqueue these new numbers.
Time complexity of this approach is O(n)
Count of Binary Digit numbers smaller than N
Approach 3 (Bit Manipulation)
If we observe the pattern then it is similar to the pattern of consecutive bits in base 5 then it is similar to the pattern which we find in this problem.
Base 5: 0 1 2 3 4
Required: 1 3 5 7 9
So we can store 1, 3, 5, 7, 9 in an array and find the bit required in base 5 representation and substitute the required number in place of that.
But we need to take care of one more thing that whenever N is divisible by 5 then last place should be 9 and after dividing the number by 5 we need to decrement it by 1 in this case. Hence array should be in the form {9, 1, 3, 5, 7}
- Create an array v[] for the values to be replaced.
- Initialize a variable ans = 0.
- Do the following until N is greater than 0
- Find the remainder of N with 5. If the remainder is 0
- If true, add v[0] to the answer and divide N by 5 and reduce it by 1.
- Otherwise, simply add v[remainder] in the ans and divide N by 5.
- Now all that is left is to reverse the number and we get the required number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long findNumber( long long N)
{
vector< int > v = { 9, 1, 3, 5, 7 };
long long ans = 0;
while (N > 0) {
ans = ans * 10 + v[N % 5];
if (N % 5 == 0) {
N = N / 5;
N--;
}
else
N /= 5;
}
queue< int > q;
while (ans > 0) {
q.push(ans % 10);
ans /= 10;
}
while (!q.empty()) {
ans = ans * 10 + q.front();
q.pop();
}
return ans;
}
int main()
{
int N = 7;
cout << findNumber(N) << endl;
return 0;
}
|
Java
import java.util.*;
public class gfg2 {
static long findNumber( int N)
{
int [] v = { 9 , 1 , 3 , 5 , 7 };
int ans = 0 ;
while (N > 0 ) {
ans = ans * 10 + v[N % 5 ];
if (N % 5 == 0 ) {
N = N / 5 ;
N--;
}
else
N /= 5 ;
}
Queue<Integer> q = new ArrayDeque<>();
while (ans > 0 ) {
q.add(ans % 10 );
ans /= 10 ;
}
while (!q.isEmpty()) {
ans = ans * 10 + q.peek();
q.remove();
}
return ans;
}
public static void main(String[] args)
{
int N = 7 ;
System.out.println(findNumber(N));
}
}
|
Python3
def findNumber(N):
v = [ 9 , 1 , 3 , 5 , 7 ]
ans = 0
while (N > 0 ):
ans = ans * 10 + v[N % 5 ]
if (N % 5 = = 0 ):
N = N / / 5
N = N - 1
else :
N = N / / 5
q = []
while (ans > 0 ):
q.append(ans % 10 )
ans = ans / / 10
while ( len (q) > 0 ):
ans = ans * 10 + q[ 0 ]
q.pop( 0 )
return ans
N = 7
print (findNumber(N))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static long findNumber( int N)
{
int [] v = new int [5] { 9, 1, 3, 5, 7 };
int ans = 0;
while (N > 0) {
ans = ans * 10 + v[N % 5];
if (N % 5 == 0) {
N = N / 5;
N--;
}
else
N /= 5;
}
List< int > q = new List< int >();
while (ans > 0) {
q.Add(ans % 10);
ans /= 10;
}
while (q.Count != 0) {
ans = ans * 10 + q[0];
q.RemoveAt(0);
}
return ans;
}
public static void Main( string [] args)
{
int N = 7;
Console.WriteLine(findNumber(N));
}
}
|
Javascript
function findNumber(N) {
let v = [9, 1, 3, 5, 7]
let ans = 0
while (N > 0) {
ans = (ans * 10) + v[N % 5]
if (N % 5 == 0) {
N = Math.floor(N / 5);
N--;
}
else
N = Math.floor(N / 5);
}
let q = [];
while (ans > 0) {
q.push(ans % 10);
ans = Math.floor(ans / 10);
}
while (q.length != 0) {
ans = ans * 10 + q[0];
q.shift();
}
return ans;
}
N = 7
console.log(findNumber(N))
|
Time Complexity: O(log5N)
Auxiliary Space: O(log5N)
Last Updated :
06 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...