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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to print the largest integer
// not greater than N with all odd digits
#include <bits/stdc++.h>
using namespace std;
  
// Function to check if all digits
// of a number are odd
bool allOddDigits(int n)
{
    // iterate for all digits
    while (n) {
  
        // if digit is even
        if ((n % 10) % 2 == 0)
            return false;
        n /= 10;
    }
  
    // all digits are odd
    return true;
}
  
// function to return the largest number
// with all digits odd
int largestNumber(int n)
{
    if (n % 2 == 0)
        n--;
  
    // iterate till we find a
    // number with all digits odd
    for (int i = n;; i -= 2)
        if (allOddDigits(i))
            return i;
}
  
// Driver Code
int main()
{
    int N = 23;
    cout << largestNumber(N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print the largest integer 
// not greater than N with all odd digits 
  
public class GFG{
  
        // Function to check if all digits 
        // of a number are odd 
        static boolean allOddDigits(int n) 
        
            // iterate for all digits 
            while (n != 0) { 
            
                // if digit is even 
                if ((n % 10) % 2 == 0
                    return false
                n /= 10
            
            
            // all digits are odd 
            return true
        
            
        // function to return the largest number 
        // with all digits odd 
        static int largestNumber(int n) 
        
            if (n % 2 == 0
                n--; 
            
            // iterate till we find a 
            // number with all digits odd 
            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));
   
    }
    // This code is contributed by ANKITRAI1
       
}

chevron_right


Python3

# Python 3 program to print the largest
# integer not greater than N with all
# odd digits

# Function to check if all digits
# of a number are odd
def allOddDigits(n):

# iterate for all digits
while (n):

# if digit is even
if ((n % 10) % 2 == 0):
return False
n = int(n / 10)

# all digits are odd
return True

# function to return the largest
# number with all digits odd
def largestNumber(n):
if (n % 2 == 0):
n -= 1

# iterate till we find a
# number with all digits odd
i = n
while(1):
if (allOddDigits(i)):
return i
i -= 2

# Driver Code
if __name__ ==’__main__’:
N = 23
print(largestNumber(N))

# This code is contributed by
# Shashank_Sharma

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print the largest
// integer not greater than N with
// all odd digits 
using System;
  
class GFG
{
  
// Function to check if all 
// digits of a number are odd 
static bool allOddDigits(int n) 
    // iterate for all digits 
    while (n != 0) 
    
  
        // if digit is even 
        if ((n % 10) % 2 == 0) 
            return false
        n /= 10; 
    
  
    // all digits are odd 
    return true
  
// function to return the largest 
// number with all digits odd 
static int largestNumber(int n) 
    if (n % 2 == 0) 
        n--; 
  
    // iterate till we find a 
    // number with all digits odd 
    for (int i = n;; i -= 2) 
        if (allOddDigits(i)) 
            return i; 
  
// Driver Code
public static void Main()
{
    int N = 23; 
  
    Console.WriteLine(largestNumber(N));
}
}
  
// This code is contributed by anuj_67

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to print the largest integer
// not greater than N with all odd digits
  
// Function to check if all digits
// of a number are odd
function allOddDigits($n)
{
    // iterate for all digits
    while ($n > 1) 
    {
  
        // if digit is even
        if (($n % 10) % 2 == 0)
            return false;
        $n = (int)$n / 10;
    }
  
    // all digits are odd
    return true;
}
  
// function to return the largest 
// number with all digits odd
function largestNumber($n)
{
    if ($n % 2 == 0)
        $n--;
  
    // iterate till we find a
    // number with all digits odd
    for ($i = $n;; $i= ($i - 2))
        if (allOddDigits($i))
            return $i;
}
  
// Driver Code
$N = 23;
echo largestNumber($N);
  
// This code is contributed by ajit
?>

chevron_right


Output:

19

Time Complexity: O(N)

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to print the largest integer
// not greater than N with all odd digits
#include <bits/stdc++.h>
using namespace std;
  
// function to return the largest number
// with all digits odd
int largestNumber(int n)
{
    string s = "";
    int duplicate = n;
  
    // convert the number to a string for
    // easy operations
    while (n) {
        s = char(n % 10 + 48) + s;
        n /= 10;
    }
  
    int index = -1;
  
    // find first even digit
    for (int i = 0; i < s.length(); i++) {
        if (((s[i] - '0') % 2 & 1) == 0) {
            index = i;
            break;
        }
    }
  
    // if no even digit, then N is the answer
    if (index == -1)
        return duplicate;
  
    int num = 0;
  
    // till first even digit, add all odd numbers
    for (int i = 0; i < index; i++)
        num = num * 10 + (s[i] - '0');
  
    // decrease 1 from the even digit
    num = num * 10 + (s[index] - '0' - 1);
  
    // add 9 in the rest of the digits
    for (int i = index + 1; i < s.length(); i++)
        num = num * 10 + 9;
  
    return num;
}
  
// Driver Code
int main()
{
    int N = 24578;
  
    cout << largestNumber(N);
  
    return 0;
}

chevron_right


Output:

19999

Time Complexity: O(M) where M is the number of digits



My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Ryuga, vt_m, jit_t, Shashank_Sharma



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.