Find the closest and smaller tidy number

Tidy Number are those number whose digits are arranged in non-decreasing order. Here, we are given a number and we have to find another number that is smaller but closest to the given number and also that number should be tidy, i.e., their digit should be in non-decreasing order.

Examples:

Input  : 91234
Output : 89999
Tidy property is violated by appearing 1 after 9. 
So, we will reduce 9 by 1 and the number right
to it will be replaced by 9. So, generated tidy
number is 89999.

Input  : 45000
Output : 44999

The idea is to traverse from end. Whenever tidy property is violated, we reduce digit by one and make all subsequent digits 9.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find closest
// tidy number smaller than the 
// given number
#include<bits/stdc++.h>
using namespace std;
  
char* tidyNum(char str[], int len)
{
    for (int i = len-2; i >= 0; i--)
    {
        // check whether string violates tidy property
        if (str[i] > str[i+1])
        {
            // if string violates tidy property, then
            // decrease the value stored at that index by 1
            // and replace all the value stored right to
            // that index by 9
            (char)str[i]--;
            for (int j=i+1; j<len; j++)
                str[j] = '9';
        }
    }
    return str;
}
  
// Driver code
int main()
{
    char str[] = "11333445538";
    int len = strlen(str);
      
    // num will store closest tidy number
    char *num = tidyNum(str, len);
    printf("%s\n", num);
    return 0;

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find closest
// tidy number smaller than the 
// given number
import java.io.*;
class GFG
{
static String tidyNum(String str1,
                      int len)
{
    char[] str = str1.toCharArray();
    for (int i = len - 2; i >= 0; i--)
    {
        // check whether string 
        // violates tidy property
        if (str[i] > str[i + 1])
        {
            // if string violates tidy 
            // property, then decrease the
            // value stored at that index 
            // by 1 and replace all the value 
            // stored right to that index by 9
            str[i]--;
            for (int j = i + 1; j < len; j++)
                str[j] = '9';
        }
    }
    return String.valueOf(str);
}
  
// Driver code
public static void main(String[] args)
{
    String str = "11333445538";
    int len = str.length();
      
    // num will store closest tidy number
    System.out.println(tidyNum(str, len));
}
  
// This code is contributed by mits

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find closest
# tidy number smaller than the 
# given number
  
def tidyNum(str, len):
  
    for i in range(len-2, -1, -1):
      
        # check whether string 
        # violates tidy property
        if (str[i] > str[i+1]):
          
            # if string violates tidy 
            # property, then decrease the
            # value stored at that index by 1
            # and replace all the value 
            # stored right to that index by 9
            str[i] -= 1
            for j in range(i+1, len):
                str[j] = 9
          
    return str
  
# Driver code
str = [1,1,3,3,3,4,4,5,5,3,8
len = len(str
      
# num will store closest tidy number
num = tidyNum(str, len
  
for i in range(0,len):
    print(str[i], end = "")
  
# This code is contributed by
# Smitha Dinesh Semwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find closest
// tidy number smaller than the 
// given number
using System;
  
class GFG
{
static String tidyNum(String str1, int len)
{
    char[] str = str1.ToCharArray();
    for (int i = len - 2; i >= 0; i--)
    {
        // check whether string 
        // violates tidy property
        if (str[i] > str[i + 1])
        {
            // if string violates tidy 
            // property, then decrease the
            // value stored at that index 
            // by 1 and replace all the value 
            // stored right to that index by 9
            str[i]--;
            for (int j = i + 1; j < len; j++)
                str[j] = '9';
        }
    }
    string s = new string(str);
    return s;
}
  
// Driver code
static void Main()
{
    String str = "11333445538";
    int len = str.Length;
      
    // num will store closest tidy number
    Console.WriteLine(tidyNum(str, len));
}
  
// This code is contributed by mits

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find closest
// tidy number smaller than the 
// given number
  
function tidyNum($str, $len)
{
    for ($i = $len - 2; $i >= 0; $i--)
    {
        // check whether string
        // violates tidy property
        if ($str[$i] > $str[$i + 1])
        {
            // if string violates tidy
            // property, then decrease
            // the value stored at that
            // index by 1 and replace all 
            // the value stored right to
            // that index by 9
            $x = ord($str[$i]);
            $x--;
            $str[$i] = chr($x);
            for ($j = $i + 1; $j < $len; $j++)
                $str[$j] = '9';
        }
    }
    return $str;
}
  
// Driver code
$str = "11333445538";
$len = strlen($str);
  
// num will store 
// closest tidy number
$num = tidyNum($str, $len);
echo $num;
  
// This code is contributed by mits
?>

chevron_right



Output:

11333444999

References :

This question is asked in Google Code Jam 2017 qualification round.

This article is contributed by Aditya kumar. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Mithun Kumar



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.