Home > Backend Development > C++ > ups and downs numbers

ups and downs numbers

WBOY
Release: 2023-08-28 13:41:06
forward
1303 people have browsed it

ups and downs numbers

In this article, we will learn what a fluctuating number is and introduce our method of checking whether a given number is a fluctuating number, using a Boolean function to check the fluctuating number.

Problem Statement

We will be given a number and our task is to check if the given number is fluctuating.

Let us first understand the fluctuation number;

A fluctuating number is a number that consists of only two types of numbers, and every other number is the same.

We can say that a fluctuating number is of the form "PQPQPQ", where P and Q are two different numbers in the number system.

The first and second digits of the fluctuation number can never be the same, that is, 11111 is not a fluctuation number.

We usually regard non-trivial fluctuating numbers as fluctuating numbers only, which means that fluctuating numbers need to be composed of at least 3 digits. That is, we can't just use two numbers to form a fluctuating number.

Let us now consider some examples of fluctuating 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 more.

Some high value fluctuating numbers are - 1212121212, 3838383838, 57575757575757, etc.

For any d digit number, where d>=3 (d contains at least 3 digits), we can have 9 * 9 = 81 fluctuating digits, since the first (number from 1 to 9) value has 9 options, similarly to the 9 options (numbers from 0 to 9, except for the first digit).

solution

We have a number and our task is to find out whether it fluctuates or not.

There are some restrictions on numbers −

  • It contains only two types of numbers.

  • Two numbers cannot be the same.

  • Contains at least 3 digits

  • Adjacent digits in the numbers are not the same.

Example

Given Number : Num = 252
Result: Yes, the number is undulating
Given Number : Num = 64664
Result: =No, the number is not undulating
Copy after login

Example

In the following example, we check if the given number is a fluctuating number. We demonstrated using a number that was not a fluctuating number. You can try different numbers to check if the number is a fluctuating number.

#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 ";
}
Copy after login

Output

When you run the above C program, it will produce the following output -

No, the number is not undulating
Copy after login

Time Complexity - For n digits, the time complexity is O(N).

Space complexity - Since no external space is used, the auxiliary space complexity is O(N).

In this article, we learn in detail what is a fluctuating number and the code solution to check if a given number is fluctuating.

The above is the detailed content of ups and downs numbers. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template