- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Undulating Numbers
In this article, we will learn what an undulating number is and our approaches to check whether a given number is undulating or not using a boolean function to check undulating numbers.
Problem statement
We will be given a number and our task is to check whether the given number is undulating.
Let us first learn about an undulating number;
An undulating number is a number that only consists of two types of digits and every second digit is the same.
We can say an undulating number is of the form “PQPQPQ” where P and Q are two different digits of the number system.
The first digit and second digit of an undulating number can never be the same i.e., 11111 is not an undulating number.
We often consider non-trivial undulating numbers only as undulating numbers which means an undulating number needs to be formed using 3 digits at least. i.e., we cannot have an undulating number of 2 digits only.
Let us now consider a few examples of undulating numbers −
494, 484, 474, 464, 454, 434, 424, 414, 404, 393, 383, 373, 363, 353, 343, 323, 313, 303, 101, 121, 131, 141, 151, 161, 171, 181, 191, 202 and many more.
Some highly valued undulating numbers are- 1212121212, 3838383838, 57575757575757 etc.
For any d digit number where d>=3 (d contains minimum 3 digits), we can have 9 * 9 = 81 undulating numbers as there are 9 options for the first (digits from 1 to 9) value and similarly 9 options (digits from 0 to 9 except first digit).
Solution
We have a number and our task is to find out whether it is undulating or not.
There are a few restrictions on the number −
It Contains only two types of digits.
Both digits cannot be the same.
It contains at least 3 digits
Adjacent digits in the number are not the same.
Examples
Given Number : Num = 252 Result: Yes, the number is undulating Given Number : Num = 64664 Result: =No, the number is not undulating
Example
In the below example, we check if a given number is an undulating number or not. We demonstrated using a number that is not an undulating number. You can try with different numbers to check if the number is undulating or not.
#include <bits/stdc++.h> using namespace std; // boolean function that checks // is the number undulating bool Is_number_undulating(string num){ // important to check // if first and second digit // are equal if (num.length() <= 2 || num[0]==num[1]) return false; for (int iterator = 2; iterator < num.length(); iterator++) if (num[iterator - 2] != num[iterator]) false; return true; } int main(){ string num = "111111"; if (Is_number_undulating(num)) cout << " Yes the number is undulating "; else cout << " No, the number is not undulating "; }
Output
When you run the above C++ program, it will produce the following output −
No, the number is not undulating
Time complexity − For an n digit number, the time complexity is O(N).
Space complexity − As no external space is used, the auxiliary space complexity is O(N).
In this article we learn in detail what is an undulating number and code solution to check whether the given number is undulating or not.