Wednesday, July 3, 2024

Undulating numbers

An undulating number is a number that has only two types of digits and alternate digits are same, i.e., it is of the form “ababab….”. It is sometimes restricted to non-trivial undulating numbers which are required to have at least 3 digits and a is not equal to b. 
 

The first few such numbers are: 101, 121, 131, 141, 151, 161, 171, 181, 191, 202, 212, 232, 242, 252, 262, 272, 282, 292, 303, 313, 323, 343, 353, 363, 373, 383, 393, 404, 414, 424, 434, 454, 464, 474, 484, 494, … 
Some higher undulating numbers are: 6363, 80808, 1717171.

 
 

  1. For any n >= 3, there are 9 × 9 = 81 non-trivial n-digit undulating numbers, since the first digit can have 9 values (it cannot be 0), and the second digit can have 9 values when it must be different from the first.

Given a number, check if it is Undulating numbers considering the definition of alternating digits, at least 3 digits and adjacent digits not same.
Examples : 

Input : n = 121
Output : Yes

Input : n = 1991
Output : No

 

 

C++




// C++ program to check whether a number
// is undulating or not
#include <bits/stdc++.h>
using namespace std;
 
bool isUndulating(string n)
{
    // Considering the definition
    // with restriction that there
    // should be at least 3 digits
    if (n.length() <= 2)
       return false;
 
    // Check if all alternate digits are
    // same or not.
    for (int i = 2; i < n.length(); i++)
        if (n[i - 2] != n[i])
           false;
 
    return true;
}
 
int main()
{
    string n = "1212121";
    if (isUndulating(n))
        cout << "Yes";
    else
        cout << "No";
}


Java




// Java program to check whether a number
// is undulating or not
import java.util.*;
 
class GFG {
     
    public static boolean isUndulating(String n)
    {
         
        // Considering the definition
        // with restriction that there
        // should be at least 3 digits
            if (n.length() <= 2)
                return false;
     
        // Check if all alternate digits are
        // same or not.
        for (int i = 2; i < n.length(); i++)
            if (n.charAt(i-2) != n.charAt(i))
                return false;
     
        return true;
    }
     
     
    // Driver code
    public static void main (String[] args)
    {
         
        String n = "1212121";
         
        if (isUndulating(n)==true)
            System.out.println("yes");
        else
            System.out.println("no");
    }
}
 
// This code is contributed by akash1295.


Python3




# Python3 program to check whether a
# number is undulating or not
 
def isUndulating(n):
 
    # Considering the definition
    # with restriction that there
    # should be at least 3 digits
    if (len(n) <= 2):
        return False
 
    # Check if all alternate digits
    # are same or not.
    for i in range(2, len(n)):
        if (n[i - 2] != n[i]):
            return False
 
    return True
 
# Driver Code
n = "1212121"
if (isUndulating(n)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Smitha Dinesh Semwal.


C#




// C# program to check whether a number
// is undulating or not
using System;
 
class GFG {
     
    public static bool isUndulating(string n)
    {
         
        // Considering the definition
        // with restriction that there
        // should be at least 3 digits
            if (n.Length <= 2)
                return false;
     
        // Check if all alternate digits are
        // same or not.
        for (int i = 2; i < n.Length; i++)
            if (n[i-2] != n[i])
                return false;
     
        return true;
    }
     
    // Driver code
    public static void Main ()
    {
         
        string n = "1212121";
         
        if (isUndulating(n)==true)
            Console.WriteLine("yes");
        else
            Console.WriteLine("no");
    }
}
 
// This code is contributed by Vt_m.


PHP




<?php
// PHP program to check whether a
// number is undulating or not
 
function isUndulating($n)
{
     
    // Considering the definition
    // with restriction that there
    // should be at least 3 digits
    if (strlen($n) <= 2)
        return false;
 
    // Check if all alternate
    // digits are same or not.
    for ($i = 2; $i < strlen($n); $i++)
        if ($n[$i - 2] != $n[$i])
            false;
 
    return true;
}
 
// Driver code
$n = "1212121";
if (isUndulating($n))
    echo("Yes");
else
    echo("No");
 
// This code is contributed by Ajit.
?>


Javascript




<script>
 
// JavaScript program to check whether a number
// is undulating or not
 
    function isUndulating(n)
    {
           
        // Considering the definition
        // with restriction that there
        // should be at least 3 digits
            if (n.length <= 2)
                return false;
       
        // Check if all alternate digits are
        // same or not.
        for (let i = 2; i < n.length; i++)
            if (n[i-2] != n[i])
                return false;
       
        return true;
    }
 
 
// Driver Code
 
        let n = "1212121";
           
        if (isUndulating(n)==true)
            document.write("Yes");
        else
            document.write("No");
   
</script>


Output: 

Yes

 

Time complexity: O(N) where N is no of digits of given number 

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Ted Musemwa
As a software developer I’m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments