Home > Backend Development > C++ > Program to calculate bitonality of an array

Program to calculate bitonality of an array

PHPz
Release: 2023-08-29 20:53:06
forward
667 people have browsed it

Program to calculate bitonality of an array

The bitonality of an array is defined as follows:

Find the bitonality of an array based on the array elements:

Bitonicity = 0 , initially arr[0]
i from 0 to n
Bitonicity = Bitonicity+1 ; if arr[i] > arr[i-1]
Bitonicity = Bitonicity-1 ; if arr[i] < arr[i-1]
Bitonicity = Bitonicity ; if arr[i] = arr[i-1]
Copy after login

Example

In the code to find the bitonicity of an array, we use a variable called bitonicity, which changes based on the comparison of the current element of the array with the previous element. The above logic updates the bitonality of the array, and the final bitonality can be found at the end of the array.

#include <iostream>
using namespace std;
int main() {
   int arr[] = { 1, 2, 4, 5, 4, 3 };
   int n = sizeof(arr) / sizeof(arr[0]); int Bitonicity = 0;
   for (int i = 1; i < n; i++) {
      if (arr[i] > arr[i - 1])
         Bitonicity++;
      else if (arr[i] < arr[i - 1]) Bitonicity--;
   }
   cout << "Bitonicity = " << Bitonicity;
   return 0;
}
Copy after login

Output

Bitonicity = 1
Copy after login

The above is the detailed content of Program to calculate bitonality of an array. 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