Given a number N, the task is to find the smallest number not less than N, which has all digits odd.
Examples:
Input: N = 1345
Output: 1351
1351 is the smallest number not less than N, whose all digits are odd.
Input: N = 2397
Output: 3111
3111 is the smallest number not less than N, whose all digits are odd.
Naive approach: A naive approach is to keep iterating from N until we find a number with all digits odd.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int check_digits( int n)
{
while (n) {
if ((n % 10) % 2 == 0)
return 0;
n /= 10;
}
return 1;
}
int smallest_number( int n)
{
for ( int i = n;; i++)
if (check_digits(i))
return i;
}
int main()
{
int N = 2397;
cout << smallest_number(N);
return 0;
}
|
Java
import java.io.*;
class Geeks {
static int check_digits( int n)
{
while (n > 0 ) {
if ((n % 10 ) % 2 == 0 )
return 0 ;
n /= 10 ;
}
return 1 ;
}
static int smallest_number( int n)
{
for ( int i = n;; i++)
if (check_digits(i) > 0 )
return i;
}
public static void main(String args[])
{
int N = 2397 ;
System.out.println(smallest_number(N));
}
}
|
Python3
def check_digits(n):
while (n):
if ((n % 10 ) % 2 = = 0 ):
return 0
n = int (n / 10 )
return 1
def smallest_number(n):
i = n
while ( 1 ):
if (check_digits(i)):
return i
i + = 1
if __name__ = = '__main__' :
N = 2397
print (smallest_number(N))
|
C#
using System;
class GFG
{
static int check_digits( int n)
{
while (n != 0)
{
if ((n % 10) % 2 == 0)
return 0;
n /= 10;
}
return 1;
}
static int smallest_number( int n)
{
for ( int i = n;; i++)
if (check_digits(i) == 1)
return i;
}
static void Main()
{
int N = 2397;
Console.WriteLine(smallest_number(N));
}
}
|
PHP
<?php
function check_digits( $n )
{
while ( $n > 1)
{
if (( $n % 10) % 2 == 0)
return 0;
$n = (int) $n / 10;
}
return 1;
}
function smallest_number( $n )
{
for ( $i = $n ;; $i ++)
if (check_digits( $i ))
return $i ;
}
$N = 2397;
echo smallest_number( $N );
?>
|
Javascript
<script>
function check_digits(n)
{
while (n > 1)
{
if ((n % 10) % 2 == 0)
return 0;
n = parseInt(n / 10);
}
return 1;
}
function smallest_number( n)
{
for (i = n;; i++)
if (check_digits(i))
return i;
}
let N = 2397;
document.write( smallest_number(N));
</script>
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Efficient Approach:
We can find the number by increasing the first even digit in N by one and replacing all digits to the right of that odd digit with the smallest odd digit (i.e. 1). If there are no even digits in N, then N is the smallest number itself. For example, consider N = 213. Increment first even digit in N i.e., 2 to 3 and replace all digits right to it by 1. So, our required number will be 311.
Below is the implementation of the efficient approach:
C++
#include <bits/stdc++.h>
using namespace std;
int smallestNumber( int n)
{
int num = 0;
string s = "" ;
int duplicate = n;
while (n) {
s.push_back(((n % 10) + '0' ));
n /= 10;
}
reverse(s.begin(), s.end());
int index = -1;
for ( int i = 0; i < s.length(); i++) {
int digit = s[i] - '0' ;
if ((digit & 1) == 0) {
index = i;
break ;
}
}
if (index == -1)
return duplicate;
for ( int i = 0; i < index; i++)
num = num * 10 + (s[i] - '0' );
num = num * 10 + (s[index] - '0' + 1);
for ( int i = index + 1; i < s.length(); i++)
num = num * 10 + 1;
return num;
}
int main()
{
int N = 2397;
cout << smallestNumber(N);
return 0;
}
|
Java
import java.io.*;
public class GFG {
static int smallestNumber( int n) {
int num = 0 ;
String s = "" ;
int duplicate = n;
while (n > 0 ) {
s = ( char ) (n % 10 + 48 ) + s;
n /= 10 ;
}
int index = - 1 ;
for ( int i = 0 ; i < s.length(); i++) {
int digit = s.charAt(i) - '0' ;
if ((digit & 1 ) == 0 ) {
index = i;
break ;
}
}
if (index == - 1 ) {
return duplicate;
}
for ( int i = 0 ; i < index; i++) {
num = num * 10 + (s.charAt(i) - '0' );
}
num = num * 10 + (s.charAt(index) - '0' + 1 );
for ( int i = index + 1 ; i < s.length(); i++) {
num = num * 10 + 1 ;
}
return num;
}
static public void main(String[] args) {
int N = 2397 ;
System.out.println(smallestNumber(N));
}
}
|
Python 3
def smallestNumber(n):
num = 0
s = ""
duplicate = n
while (n):
s = chr (n % 10 + 48 ) + s
n / / = 10
index = - 1
for i in range ( len ( s)):
digit = ord (s[i]) - ord ( '0' )
if ((digit & 1 ) = = 0 ) :
index = i
break
if (index = = - 1 ):
return duplicate
for i in range ( index):
num = num * 10 + ( ord (s[i]) -
ord ( '0' ))
num = num * 10 + ( ord (s[index]) -
ord ( '0' ) + 1 )
for i in range (index + 1 , len (s)):
num = num * 10 + 1
return num
if __name__ = = "__main__" :
N = 2397
print (smallestNumber(N))
|
C#
using System;
public class GFG{
static int smallestNumber( int n) {
int num = 0;
String s = "" ;
int duplicate = n;
while (n > 0) {
s = ( char ) (n % 10 + 48) + s;
n /= 10;
}
int index = -1;
for ( int i = 0; i < s.Length; i++) {
int digit = s[i] - '0' ;
if ((digit & 1) == 0) {
index = i;
break ;
}
}
if (index == -1) {
return duplicate;
}
for ( int i = 0; i < index; i++) {
num = num * 10 + (s[i] - '0' );
}
num = num * 10 + (s[index] - '0' + 1);
for ( int i = index + 1; i < s.Length; i++) {
num = num * 10 + 1;
}
return num;
}
static public void Main() {
int N = 2397;
Console.WriteLine(smallestNumber(N));
}
}
|
Javascript
<script>
function smallestNumber(n)
{
var num = 0;
var s = "" ;
var duplicate = n;
while (n) {
s = String.fromCharCode(n % 10 + 48) + s;
n = parseInt(n/10);
}
var index = -1;
for ( var i = 0; i < s.length; i++) {
var digit = s[i].charCodeAt(0) - '0' .charCodeAt(0);
if ((digit & 1) == 0) {
index = i;
break ;
}
}
if (index == -1)
return duplicate;
for ( var i = 0; i < index; i++)
num = num * 10 + (s[i].charCodeAt(0) - '0' .charCodeAt(0));
num = num * 10 + (s[index].charCodeAt(0) - '0' .charCodeAt(0) + 1);
for ( var i = index + 1; i < s.length; i++)
num = num * 10 + 1;
return num;
}
var N = 2397;
document.write( smallestNumber(N));
</script>
|
Time Complexity: O(M), where M is the number of digits in N.
Auxiliary Space: O(M)
Another Approach:
Initialize a variable num to n.
If num is even, increment it by 1.
While num contains even digits:
a. Convert num to a string and check each digit.
b. If the current digit is even, add a power of 10 to num such that the resulting number has the same number of digits as num.
Return num.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n = 123456;
int num = n;
if (num % 2 == 0) {
num++;
}
while (1) {
int contains_even = 0;
string str = to_string(num);
int len = str.length();
for ( int i = 0; i < len; i++) {
if ((str[i] - '0' ) % 2 == 0) {
contains_even = 1;
int pow10 = 1;
for ( int j = 0; j < len - i - 1; j++) {
pow10 *= 10;
}
num += pow10;
break ;
}
}
if (!contains_even) {
break ;
}
}
cout << "Smallest odd digits number not less than " << n
<< " is: " << num << endl;
return 0;
}
|
C
#include <stdio.h>
#include <string.h>
int main()
{
int n = 123456;
int num = n;
if (num % 2 == 0) {
num++;
}
while (1) {
int contains_even = 0;
char str[20];
sprintf (str, "%d" , num);
int len = strlen (str);
for ( int i = 0; i < len; i++) {
if ((str[i] - '0' ) % 2 == 0) {
contains_even = 1;
int pow10 = 1;
for ( int j = 0; j < len - i - 1; j++) {
pow10 *= 10;
}
num += pow10;
break ;
}
}
if (!contains_even) {
break ;
}
}
printf ( "Smallest odd digits number not less than %d "
"is: %d\n" ,
n, num);
return 0;
}
|
Python3
def GetSmallestOddDigitsNumber(n):
num = n
if num % 2 = = 0 :
num + = 1
while True :
contains_even = False
str_num = str (num)
len_num = len (str_num)
for i in range (len_num):
if int (str_num[i]) % 2 = = 0 :
contains_even = True
pow10 = 1
for j in range (len_num - i - 1 ):
pow10 * = 10
num + = pow10
break
if not contains_even:
break
print ( "Smallest odd digits number not less than" , n, "is:" , num)
GetSmallestOddDigitsNumber( 123456 )
|
Javascript
let n = 123456;
let num = n;
if (num % 2 == 0) {
num++;
}
while (1) {
let contains_even = 0;
let str = num.toString();
let len = str.length;
for (let i = 0; i < len; i++) {
if ((str[i] - '0' ) % 2 == 0) {
contains_even = 1;
let pow10 = 1;
for (let j = 0; j < len - i - 1; j++) {
pow10 *= 10;
}
num += pow10;
break ;
}
}
if (!contains_even) {
break ;
}
}
console.log( "Smallest odd digits number not less than " + n + " is: " + num);
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class Main {
public static void main(String[] args)
throws java.lang.Exception
{
int n = 123456 ;
int num = n;
if (num % 2 == 0 ) {
num++;
}
while ( true ) {
int contains_even = 0 ;
String str = Integer.toString(num);
int len = str.length();
for ( int i = 0 ; i < len; i++) {
if ((str.charAt(i) - '0' ) % 2 == 0 ) {
contains_even = 1 ;
int pow10 = 1 ;
for ( int j = 0 ; j < len - i - 1 ; j++) {
pow10 *= 10 ;
}
num += pow10;
break ;
}
}
if (contains_even == 0 ) {
break ;
}
}
System.out.println(
"Smallest odd digits number not less than " + n
+ " is: " + num);
}
}
|
C#
using System;
public class Program {
public static void Main()
{
int n = 123456;
int num = n;
if (num % 2 == 0) {
num++;
}
while ( true ) {
bool contains_even = false ;
string str = num.ToString();
int len = str.Length;
for ( int i = 0; i < len; i++) {
if ((str[i] - '0' ) % 2 == 0) {
contains_even = true ;
int pow10 = 1;
for ( int j = 0; j < len - i - 1; j++) {
pow10 *= 10;
}
num += pow10;
break ;
}
}
if (!contains_even) {
break ;
}
}
Console.WriteLine(
"Smallest odd digits number not less than " + n
+ " is: " + num);
}
}
|
Output
Smallest odd digits number not less than 123456 is: 133557
time complexity of O(log N) , Where N is given in the problem statement
space complexity of O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
11 Apr, 2023
Like Article
Save Article