Largest number not greater than N all the digits of which are odd
Given a number N, the task is to find the largest number not greater than N which has all of it’s digits as odd.
Examples:
Input: N = 23
Output: 19
19 is the largest number less than 23 which has all odd digits.
Input: N = 7236
Output: 7199
Naive Approach: Iterate from N to 0, and find the first number which has all of its digits as odd. This approach can still be optimized if the even numbers are skipped as every even number will have an even digit on it’s right.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool allOddDigits( int n)
{
while (n) {
if ((n % 10) % 2 == 0)
return false ;
n /= 10;
}
return true ;
}
int largestNumber( int n)
{
if (n % 2 == 0)
n--;
for ( int i = n;; i -= 2)
if (allOddDigits(i))
return i;
}
int main()
{
int N = 23;
cout << largestNumber(N);
return 0;
}
|
Java
public class GFG{
static boolean allOddDigits( int n)
{
while (n != 0 ) {
if ((n % 10 ) % 2 == 0 )
return false ;
n /= 10 ;
}
return true ;
}
static int largestNumber( int n)
{
if (n % 2 == 0 )
n--;
for ( int i = n;; i -= 2 )
if (allOddDigits(i))
return i;
}
public static void main(String []args){
int N = 23 ;
System.out.println(largestNumber(N));
}
}
|
Python3
def allOddDigits(n):
while (n):
if ((n % 10 ) % 2 = = 0 ):
return False
n = int (n / 10 )
return True
def largestNumber(n):
if (n % 2 = = 0 ):
n - = 1
i = n
while ( 1 ):
if (allOddDigits(i)):
return i
i - = 2
if __name__ = = '__main__' :
N = 23
print (largestNumber(N))
|
C#
using System;
class GFG
{
static bool allOddDigits( int n)
{
while (n != 0)
{
if ((n % 10) % 2 == 0)
return false ;
n /= 10;
}
return true ;
}
static int largestNumber( int n)
{
if (n % 2 == 0)
n--;
for ( int i = n;; i -= 2)
if (allOddDigits(i))
return i;
}
public static void Main()
{
int N = 23;
Console.WriteLine(largestNumber(N));
}
}
|
PHP
<?php
function allOddDigits( $n )
{
while ( $n > 1)
{
if (( $n % 10) % 2 == 0)
return false;
$n = (int) $n / 10;
}
return true;
}
function largestNumber( $n )
{
if ( $n % 2 == 0)
$n --;
for ( $i = $n ;; $i = ( $i - 2))
if (allOddDigits( $i ))
return $i ;
}
$N = 23;
echo largestNumber( $N );
?>
|
Javascript
<script>
function allOddDigits(n)
{
while (n != 0)
{
if ((n % 10) % 2 == 0)
return false ;
n = parseInt(n / 10);
}
return true ;
}
function largestNumber(n)
{
if (parseInt(n % 2) == 0)
n--;
for (i = n; i > 0; i -= 2)
if (allOddDigits(i))
return i;
}
var N = 23;
document.write(largestNumber(N));
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(1)
Efficient Approach: We can obtain the required number by decreasing the first even digit in N by one and then replacing all the other digits with the largest odd digit i.e. 9. For example, if N = 24578 then X = 19999. If there are no even digits in N, then N is the number itself.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int largestNumber( int n)
{
string s = "" ;
int duplicate = n;
while (n) {
s = char (n % 10 + 48) + s;
n /= 10;
}
int index = -1;
for ( int i = 0; i < s.length(); i++) {
if (((s[i] - '0' ) % 2 & 1) == 0) {
index = i;
break ;
}
}
if (index == -1)
return duplicate;
int num = 0;
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 + 9;
return num;
}
int main()
{
int N = 24578;
cout << largestNumber(N);
return 0;
}
|
Java
class GFG
{
static int largestNumber( int n)
{
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++)
{
if ((( int )(s.charAt(i) - '0' ) % 2 & 1 ) == 0 )
{
index = i;
break ;
}
}
if (index == - 1 )
return duplicate;
int num = 0 ;
for ( int i = 0 ; i < index; i++)
num = num * 10 + ( int )(s.charAt(i) - '0' );
num = num * 10 + (( int )s.charAt(index) - ( int )( '0' ) - 1 );
for ( int i = index + 1 ; i < s.length(); i++)
num = num * 10 + 9 ;
return num;
}
public static void main (String[] args)
{
int N = 24578 ;
System.out.println(largestNumber(N));
}
}
|
Python3
def largestNumber(n):
s = ""
duplicate = n
while (n):
s = chr (n % 10 + 48 ) + s
n / / = 10
index = - 1
for i in range ( len (s)):
if ((( ord (s[i]) -
ord ( '0' )) % 2 & 1 ) = = 0 ):
index = i
break
if (index = = - 1 ):
return duplicate
num = 0
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 + 9
return num
N = 24578
print (largestNumber(N))
|
C#
using System;
class GFG
{
static int largestNumber( int n)
{
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++)
{
if ((( int )(s[i] - '0' ) % 2 & 1) == 0)
{
index = i;
break ;
}
}
if (index == -1)
return duplicate;
int num = 0;
for ( int i = 0; i < index; i++)
num = num * 10 + ( int )(s[i] - '0' );
num = num * 10 + (( int )s[index] - ( int )( '0' ) - 1);
for ( int i = index + 1; i < s.Length; i++)
num = num * 10 + 9;
return num;
}
static void Main()
{
int N = 24578;
Console.WriteLine(largestNumber(N));
}
}
|
Javascript
<script>
function largestNumber(n)
{
var s = "" ;
var duplicate = n;
while (n > 0)
{
s = String.fromCharCode(n % 10 + 48) + s;
n = parseInt(n/10);
}
var index = -1;
for (i = 0; i < s.length; i++)
{
if (((s.charAt(i).charCodeAt(0) -
'0' .charCodeAt(0)) % 2 & 1) == 0)
{
index = i;
break ;
}
}
if (index == -1)
return duplicate;
var num = 0;
for (i = 0; i < index; i++)
num = num * 10 + (s.charAt(i).charCodeAt(0)
- '0' .charCodeAt(0));
num = num * 10 + (s.charAt(index).charCodeAt(0)
- ( '0' ).charCodeAt(0) - 1);
for (i = index + 1; i < s.length; i++)
num = num * 10 + 9;
return num;
}
var N = 24578;
document.write(largestNumber(N));
</script>
|
Time Complexity: O(M) where M is the number of digits
Auxiliary Space: O(1)
Last Updated :
11 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...