Given an integer n, check whether it is a tidy number. A number is called tidy if its digits are in non-decreasing order from left to right.
Examples:
Input: n = 1234
Output: true
Explanation: The digits 1, 2, 3 and 4 are in non-decreasing order.Input: n = 1243
Output: false
Explanation: Since 4 > 3, the digits are not in non-decreasing order.
Table of Content
[Naive Approach] Using String - O(log n) Time and O(log n) Space
The idea is to convert the given integer n into a string and check whether its digits are in non-decreasing order from left to right.
- After converting the number into a string, compare every pair of adjacent digits.
- If the current digit is greater than the next digit, the digits are not in non-decreasing order, so return false.
- If all adjacent pairs satisfy the condition, return true.
#include <bits/stdc++.h>
using namespace std;
bool isTidy(int n) {
string s = to_string(n);
// Traverse the string and check adjacent digits.
for (int i = 0; i + 1 < s.size(); i++) {
// Digits are not in non-decreasing order.
if (s[i] > s[i + 1])
return false;
}
return true;
}
int main() {
int n = 1234;
cout << boolalpha << isTidy(n);
return 0;
}
class GFG {
public static boolean isTidy(int n) {
String s = Integer.toString(n);
// Traverse the string and check adjacent digits.
for (int i = 0; i + 1 < s.length(); i++) {
// Digits are not in non-decreasing order.
if (s.charAt(i) > s.charAt(i + 1))
return false;
}
return true;
}
public static void main(String[] args) {
int n = 1234;
System.out.println(isTidy(n));
}
}
def isTidy(n):
s = str(n)
# Traverse the string and check adjacent digits.
for i in range(len(s) - 1):
# Digits are not in non-decreasing order.
if s[i] > s[i + 1]:
return False
return True
if __name__ == "__main__":
n = 1234
print(isTidy(n))
using System;
class GFG {
public static bool isTidy(int n) {
string s = n.ToString();
// Traverse the string and check adjacent digits.
for (int i = 0; i + 1 < s.Length; i++) {
// Digits are not in non-decreasing order.
if (s[i] > s[i + 1])
return false;
}
return true;
}
public static void Main() {
int n = 1234;
Console.WriteLine(isTidy(n));
}
}
function isTidy(n)
{
let s = n.toString();
// Traverse the string and check adjacent digits.
for (let i = 0; i + 1 < s.length; i++) {
// Digits are not in non-decreasing order.
if (s[i] > s[i + 1])
return false;
}
return true;
}
// Driver code
let n = 1234;
console.log(isTidy(n));
Output
true
[Expected Approach] Using Arithmetic Operations - O(log n) Time and O(1) Space
The idea is to check the digits directly using arithmetic operations instead of converting the number into a string.
Since the digits need to be non-decreasing from left to right, we can process them from right to left. In this direction, the digits must be in non-increasing order.
So, while traversing from right to left, the current digit must be less than or equal to the previously processed digit.
- Initialize prev = 10. Since 10 is greater than every decimal digit from 0 to 9, the first extracted digit will always satisfy the comparison.
- Extract the last digit using n % 10.
- If digit > prev, return false.
- Update prev = digit and remove the last digit using n /= 10.
- If all digits are processed without a violation, return true.
Consider: n = 1234.
The digits are processed from right to left:
- digit = 4, prev = 10 -> 4 <= 10
- digit = 3, prev = 4 -> 3 <= 4
- digit = 2, prev = 3 -> 2 <= 3
- digit = 1, prev = 2 -> 1 <= 2
No violation is found, so the number is tidy and the answer is true.
#include <bits/stdc++.h>
using namespace std;
bool isTidy(int n) {
// Previous digit while traversing from right to left.
int prev = 10;
while (n > 0) {
int digit = n % 10;
// Digits are not in non-decreasing order.
if (digit > prev)
return false;
prev = digit;
n /= 10;
}
return true;
}
int main() {
int n = 1234;
cout << boolalpha << isTidy(n);
return 0;
}
class GFG {
public static boolean isTidy(int n) {
// Previous digit while traversing from right to left.
int prev = 10;
while (n > 0) {
int digit = n % 10;
// Digits are not in non-decreasing order.
if (digit > prev)
return false;
prev = digit;
n /= 10;
}
return true;
}
public static void main(String[] args) {
int n = 1234;
System.out.println(isTidy(n));
}
}
def isTidy(n):
# Previous digit while traversing from right to left.
prev = 10
while n > 0:
digit = n % 10
# Digits are not in non-decreasing order.
if digit > prev:
return False
prev = digit
n //= 10
return True
if __name__ == "__main__":
n = 1234
print(isTidy(n))
using System;
class GFG {
public static bool isTidy(int n) {
// Previous digit while traversing from right to left.
int prev = 10;
while (n > 0) {
int digit = n % 10;
// Digits are not in non-decreasing order.
if (digit > prev)
return false;
prev = digit;
n /= 10;
}
return true;
}
public static void Main() {
int n = 1234;
Console.WriteLine(isTidy(n));
}
}
function isTidy(n)
{
// Previous digit while traversing from right to left.
let prev = 10;
while (n > 0) {
let digit = n % 10;
// Digits are not in non-decreasing order.
if (digit > prev)
return false;
prev = digit;
n = Math.floor(n / 10);
}
return true;
}
// Driver code
let n = 1234;
console.log(isTidy(n));
Output
true