VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-all-numbers-in-range-whose-digits-are-increasing-decreasing-alternatively/

⇱ Find all numbers in range whose digits are increasing decreasing alternatively - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find all numbers in range whose digits are increasing decreasing alternatively

Last Updated : 1 Feb, 2022

Given integers L and R, find all numbers in range L to R whose digits are increasing-decreasing alternatively i.e. if the digits in the current number are d1, d2, d3, d4, d5 . . . then d1 < d2 > d3 < d4. . . must hold true.

Examples:

Input: L = 60, R = 100
Output: 67 68 69 78 79 89
Explanation: These numbers follow the increasing decreasing manner of digits

Input: L = 4, R = 12
Output: 4 5 6 7 8 9 12

Approach: Traverse all numbers in range L to R and find the numbers with given pattern of digits. Follow the steps mentioned below:

  • Traverse each digit in the number
  • Check if the character on two index ahead is increasing from current character,
  • Else check if character on two index ahead is decreasing from current character
  • If both the case is false, break and check for next number
  • If all cases are true, print the number

Below is the implementation of the above approach.

 
 


Output
67 68 69 78 79 89 


 

Time Complexity: O((R-L) * D) where D is the number of digits in R
Auxiliary Space: O(D)


 

Comment