Skip to content
Related Articles

Related Articles

Find all numbers in range whose digits are increasing decreasing alternatively

Improve Article
Save Article
  • Last Updated : 01 Feb, 2022
Improve Article
Save Article

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.

C++




// C++ code to implement the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Utility function to check if
// the digits of the current
// integer forms a wave pattern
bool check(int N)
{
    // Convert the number to a string
    string S = to_string(N);
 
    // Loop to iterate over digits
    for (int i = 0; i < S.size(); i++) {
        if (i == 0) {
 
            // Next character of
            // the number
            int next = i + 1;
 
            // Current character is
            // not a local minimum
            if (next < S.size()) {
                if (S[i] >= S[next]) {
 
                    return false;
                }
            }
        }
 
        else if (i == S.size() - 1) {
 
            // Previous character of
            // the number
            int prev = i - 1;
            if (prev >= 0) {
 
                // Character is a
                // local maximum
                if (i & 1) {
 
                    // Character is not
                    // a local maximum
                    if (S[i] <= S[prev]) {
                        return false;
                    }
                }
                else {
                     
                    // Character is a
                    // local minimum
                    if (S[i] >= S[prev]) {
                        return false;
                    }
                }
            }
        }
        else {
            int prev = i - 1;
            int next = i + 1;
            if (i & 1) {
 
                // Character is a
                // local maximum
                if ((S[i] > S[prev]) &&
                    (S[i] > S[next])) {
                }
                else {
                    return false;
                }
            }
            else {
                 
                // Character is a
                // local minimum
                if ((S[i] < S[prev]) &&
                    (S[i] < S[next])) {
                }
                else {
                    return false;
                }
            }
        }
    }
    return true;
}
 
// Function to find the numbers
void findNumbers(int L, int R)
{
    for (int i = L; i <= R; i++)
        if (check(i))
            cout << i << " ";
}
 
// Driver Code
int main()
{
    int L = 60, R = 100;
    findNumbers(L, R);
    return 0;
}


Java




// Java code to implement the above approach
 
class GFG
{
 
  // Utility function to check if
  // the digits of the current
  // integer forms a wave pattern
  static boolean check(int N)
  {
 
    // Convert the number to a string
    String S = Integer.toString(N);
 
    // Loop to iterate over digits
    for (int i = 0; i < S.length(); i++) {
      if (i == 0) {
 
        // Next character of
        // the number
        int next = i + 1;
 
        // Current character is
        // not a local minimum
        if (next < S.length()) {
          if (S.charAt(i) >= S.charAt(next)) {
 
            return false;
          }
        }
      }
 
      else if (i == S.length() - 1) {
 
        // Previous character of
        // the number
        int prev = i - 1;
        if (prev >= 0) {
 
          // Character is a
          // local maximum
          if ((i & 1) > 0) {
 
            // Character is not
            // a local maximum
            if (S.charAt(i) <= S.charAt(prev)) {
              return false;
            }
          } else {
 
            // Character is a
            // local minimum
            if (S.charAt(i) >= S.charAt(prev)) {
              return false;
            }
          }
        }
      } else {
        int prev = i - 1;
        int next = i + 1;
        if ((i & 1) > 0) {
 
          // Character is a
          // local maximum
          if ((S.charAt(i) > S.charAt(prev)) &&
              (S.charAt(i) > S.charAt(next))) {
          } else {
            return false;
          }
        } else {
 
          // Character is a
          // local minimum
          if ((S.charAt(i) < S.charAt(prev)) &&
              (S.charAt(i) < S.charAt(next))) {
          } else {
            return false;
          }
        }
      }
    }
    return true;
  }
 
  // Function to find the numbers
  static void findNumbers(int L, int R) {
    for (int i = L; i <= R; i++)
      if (check(i))
        System.out.print(i + " ");
  }
 
  // Driver Code
  public static void main(String args[]) {
    int L = 60, R = 100;
    findNumbers(L, R);
  }
}
 
// This code is contributed by gfgking.


Python3




# Python code for the above approach
 
# Utility function to check if
# the digits of the current
# integer forms a wave pattern
def check(N):
 
    # Convert the number to a string
    S = str(N);
 
    # Loop to iterate over digits
    for i in range(len(S)):
        if (i == 0):
 
            # Next character of
            # the number
            next = i + 1;
 
            # Current character is
            # not a local minimum
            if (next < len(S)):
                if (ord(S[i]) >= ord(S[next])):
                    return False;
 
        elif (i == len(S) - 1):
 
            # Previous character of
            # the number
            prev = i - 1;
            if (prev >= 0):
 
                # Character is a
                # local maximum
                if (i & 1):
 
                    # Character is not
                    # a local maximum
                    if (ord(S[i]) <= ord(S[prev])):
                        return False;
                else:
 
                    # Character is a
                    # local minimum
                    if (ord(S[i]) >= ord(S[prev])):
                        return False;
        else:
            prev = i - 1;
            next = i + 1;
            if (i & 1):
 
                # Character is a
                # local maximum
                if (ord(S[i]) > ord(S[prev])) and (ord(S[i]) > ord(S[next])):
                    print("", end="")
                else:
                    return False;
            else:
 
                # Character is a
                # local minimum
                if (ord(S[i]) < ord(S[prev])) and (ord(S[i]) < ord(S[next])):
                    print("", end="")
                else:
                    return False;
    return True;
 
# Function to find the numbers
def findNumbers(L, R):
    for i in range(L, R + 1):
        if (check(i)):
            print(i, end= " ")
 
# Driver Code
L = 60
R = 100;
findNumbers(L, R);
 
# This code is contributed by Saurabh Jaiswal


C#




// C# code to implement the above approach
using System;
class GFG
{
 
  // Utility function to check if
  // the digits of the current
  // integer forms a wave pattern
  static bool check(int N)
  {
 
    // Convert the number to a string
    string S = N.ToString();
 
    // Loop to iterate over digits
    for (int i = 0; i < S.Length; i++) {
      if (i == 0) {
 
        // Next character of
        // the number
        int next = i + 1;
 
        // Current character is
        // not a local minimum
        if (next < S.Length) {
          if (S[i] >= S[next]) {
 
            return false;
          }
        }
      }
 
      else if (i == S.Length - 1) {
 
        // Previous character of
        // the number
        int prev = i - 1;
        if (prev >= 0) {
 
          // Character is a
          // local maximum
          if ((i & 1) > 0) {
 
            // Character is not
            // a local maximum
            if (S[i] <= S[prev]) {
              return false;
            }
          }
          else {
 
            // Character is a
            // local minimum
            if (S[i] >= S[prev]) {
              return false;
            }
          }
        }
      }
      else {
        int prev = i - 1;
        int next = i + 1;
        if ((i & 1) > 0) {
 
          // Character is a
          // local maximum
          if ((S[i] > S[prev]) &&
              (S[i] > S[next])) {
          }
          else {
            return false;
          }
        }
        else {
 
          // Character is a
          // local minimum
          if ((S[i] < S[prev]) &&
              (S[i] < S[next])) {
          }
          else {
            return false;
          }
        }
      }
    }
    return true;
  }
 
  // Function to find the numbers
  static void findNumbers(int L, int R)
  {
    for (int i = L; i <= R; i++)
      if (check(i))
        Console.Write(i + " ");
  }
 
  // Driver Code
  public static void Main()
  {
    int L = 60, R = 100;
    findNumbers(L, R);
  }
}
 
// This code is contributed by Samim Hossain Monda


Javascript




<script>
       // JavaScript code for the above approach
 
       // Utility function to check if
       // the digits of the current
       // integer forms a wave pattern
       function check(N)
       {
        
           // Convert the number to a string
           let S = N.toString();
 
           // Loop to iterate over digits
           for (let i = 0; i < S.length; i++)
           {
               if (i == 0)
               {
 
                   // Next character of
                   // the number
                   let next = i + 1;
 
                   // Current character is
                   // not a local minimum
                   if (next < S.length)
                   {
                       if (S[i].charCodeAt(0) >= S[next].charCodeAt(0))
                       {
                           return false;
                       }
                   }
               }
 
               else if (i == S.length - 1)
               {
 
                   // Previous character of
                   // the number
                   let prev = i - 1;
                   if (prev >= 0) {
 
                       // Character is a
                       // local maximum
                       if (i & 1) {
 
                           // Character is not
                           // a local maximum
                           if (S[i].charCodeAt(0) <= S[prev].charCodeAt(0)) {
                               return false;
                           }
                       }
                       else {
 
                           // Character is a
                           // local minimum
                           if (S[i].charCodeAt(0) >= S[prev].charCodeAt(0)) {
                               return false;
                           }
                       }
                   }
               }
               else {
                   let prev = i - 1;
                   let next = i + 1;
                   if (i & 1) {
 
                       // Character is a
                       // local maximum
                       if ((S[i].charCodeAt(0) > S[prev].charCodeAt(0)) &&
                           (S[i].charCodeAt(0) > S[next].charCodeAt(0))) {
                       }
                       else {
                           return false;
                       }
                   }
                   else {
 
                       // Character is a
                       // local minimum
                       if ((S[i].charCodeAt(0) < S[prev].charCodeAt(0)) &&
                           (S[i].charCodeAt(0) < S[next].charCodeAt(0))) {
                       }
                       else {
                           return false;
                       }
                   }
               }
           }
           return true;
       }
 
       // Function to find the numbers
       function findNumbers(L, R) {
           for (let i = L; i <= R; i++)
               if (check(i))
                   document.write(i + " ")
       }
 
       // Driver Code
       let L = 60, R = 100;
       findNumbers(L, R);
 
        // This code is contributed by Potta Lokesh
   </script>


 
 

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)

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!