- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Number of indexes with equal elements in given range in C++

You are given an array, and indexes range. You need to count the total number of adjacent elements that are equal in the given range.

Let's see an example.

**Input**

arr = [1, 2, 2, 2, 3, 3, 4] lower = 1 upper = 5

**Output**

3

## Algorithm

Initialise the array and indexes range.

Write a loop that iterates from the lower index of the range to upper index of the range.

Compare the element the previous or next element.

Increment the count if they are equal.

Return the count.

## Implementation

Following is the implementation of the above algorithm in C++

#include <bits/stdc++.h> using namespace std; int getEqualElementsCount(int arr[], int n, int lower, int upper) { int count = 0; for (int i = lower; i < upper; i++) { if (arr[i] == arr[i + 1]) { count += 1; } } return count; } int main() { int arr[] = { 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5 }; int n = 15; cout << getEqualElementsCount(arr, 15, 1, 15) << endl; return 0; }

## Output

If you run the above code, then you will get the following result.

10

- Related Questions & Answers
- Count subarrays with equal number of occurrences of two given elements in C++
- Count number of smallest elements in given range in C++
- Python Program for Number of elements with odd factors in the given range
- Java Program to get number of elements with odd factors in given range
- Number of elements less than or equal to a given number in a given subarray in C++
- Accessing all elements at given Python list of indexes
- Count number of triplets with product equal to given number in C++
- Construct sum-array with sum of elements in given range in C++
- Count number of triplets with product equal to given number with duplicates allowed in C++
- Python Program to replace list elements within a range with a given number
- Queries for counts of array elements with values in given range in C++
- Count number of subsets of a set with GCD equal to a given number in C++
- Count pairs of natural numbers with GCD equal to given number in C++
- Count of matrices (of different orders) with given number of elements in C++
- Program to find number of pairs where elements square is within the given range in Python

Advertisements