Table of Contents
Problem Statement
solution
Example
Output
Home Backend Development C++ ups and downs numbers

ups and downs numbers

Aug 28, 2023 pm 01:41 PM
variable cycle random

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

A guide to using Windows 11 and 10 environment variables for profiling A guide to using Windows 11 and 10 environment variables for profiling Nov 01, 2023 pm 08:13 PM

Environment variables are the path to the location (or environment) where applications and programs run. They can be created, edited, managed or deleted by the user and come in handy when managing the behavior of certain processes. Here's how to create a configuration file to manage multiple variables simultaneously without having to edit them individually on Windows. How to use profiles in environment variables Windows 11 and 10 On Windows, there are two sets of environment variables – user variables (apply to the current user) and system variables (apply globally). However, using a tool like PowerToys, you can create a separate configuration file to add new and existing variables and manage them all at once. Here’s how: Step 1: Install PowerToysPowerTo

Strict mode for variables in PHP7: how to reduce potential bugs? Strict mode for variables in PHP7: how to reduce potential bugs? Oct 19, 2023 am 10:01 AM

Strict mode was introduced in PHP7, which can help developers reduce potential errors. This article will explain what strict mode is and how to use strict mode in PHP7 to reduce errors. At the same time, the application of strict mode will be demonstrated through code examples. 1. What is strict mode? Strict mode is a feature in PHP7 that can help developers write more standardized code and reduce some common errors. In strict mode, there will be strict restrictions and detection on variable declaration, type checking, function calling, etc. Pass

Generate random numbers and strings in JavaScript Generate random numbers and strings in JavaScript Sep 02, 2023 am 08:57 AM

The ability to generate random numbers or alphanumeric strings comes in handy in many situations. You can use it to spawn enemies or food at different locations in the game. You can also use it to suggest random passwords to users or create filenames to save files. I wrote a tutorial on how to generate random alphanumeric strings in PHP. I said at the beginning of this post that few events are truly random, and the same applies to random number or string generation. In this tutorial, I'll show you how to generate a pseudo-random alphanumeric string in JavaScript. Generating Random Numbers in JavaScript Let’s start by generating random numbers. The first method that comes to mind is Math.random(), which returns a float

What are instance variables in Java What are instance variables in Java Feb 19, 2024 pm 07:55 PM

Instance variables in Java refer to variables defined in the class, not in the method or constructor. Instance variables are also called member variables. Each instance of a class has its own copy of the instance variable. Instance variables are initialized during object creation, and their state is saved and maintained throughout the object's lifetime. Instance variable definitions are usually placed at the top of the class and can be declared with any access modifier, which can be public, private, protected, or the default access modifier. It depends on what we want this to be

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

The difference between random and pseudo-random The difference between random and pseudo-random Oct 10, 2023 am 09:27 AM

The difference between random and pseudo-random is predictability, reproducibility, uniformity and security. Detailed introduction: 1. Predictability. Random numbers cannot be predicted. Even if the past results are known, future results cannot be accurately predicted. Pseudo-random numbers can be predicted because they are generated by algorithms. As long as you know the algorithm and seed, you can regenerate the same sequence or sequence; 2. Reproducibility, random numbers are not reproducible, and the results generated each time are independent, while pseudo-random numbers are reproducible. Yes, just use the same algorithm and seeds etc.

Deep understanding of const in C language Deep understanding of const in C language Feb 18, 2024 pm 12:56 PM

Detailed explanation and code examples of const in C In C language, the const keyword is used to define constants, which means that the value of the variable cannot be modified during program execution. The const keyword can be used to modify variables, function parameters, and function return values. This article will provide a detailed analysis of the use of the const keyword in C language and provide specific code examples. const modified variable When const is used to modify a variable, it means that the variable is a read-only variable and cannot be modified once it is assigned a value. For example: constint

See all articles