
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Count Subarrays with Consecutive Elements Differing by 1 in C++
We are given an array arr[] containing integers. The goal is to count all subarrays of arr[] such that consecutive elements in each subarray differ by 1 only. If the array is [1,2,3] .Subarrays will be [1,2], [2,3], [1,2,3] only.
Let us understand with examples.
Input − arr[] = { 4,3,2,1 };
Output − Count of Subarrays with Consecutive elements differing by 1 is − 6
Explanation − Subaarays will be −
[4,3], [3,2], [2,1], [4,3,2], [3,2,1], [4,3,2,1]. Total 6.
Input − arr[] = { 1,5,6,7,9,11 };
Output − Count of Subarrays with Consecutive elements differing by 1 is − 3
Explanation − Subaarays will be −
[5,6], [6,7], [5,6,7]. Total 3
The approach used in the below program is as follows
We will traverse the array using a for a loop. From i=0 to i<size. Then check if any element differs by its adjacent element by 1. If yes store index as first. If not then take the number of elements in the subarray as a temp ( first-last +1 ). Arrays between indexes first and last have all consecutive elements differing by 1. So total subarrays will be temp*(temp-1)/2. Add this to count. Update indexes first=last=i for next array with all consecutive elements.
Take an array arr[] of numbers.
Function sub_ele_diff_one(int arr[], int size) takes the array and returns a count of subarrays with consecutive elements differing by 1.
Take the initial count as 0.
We will traverse the array using a for loops from i=0 to I <size.
Take two variables first, last as 0 for the indexes up to which all elements are consecutive and differ by 1.
Check if arr[i-1]-arr[i] ==1 OR arr[i]-arr[i-1]==1. (elements differ by 1). If true, increment first.
If the previous condition is false, then the total elements in the array that satisfy this condition is temp=first-last+1. Subarrays possible is total=temp*(temp-1)/2.
Now add this subarray count total to count.
Update indexes first and last with current I (index at which the consecutive element condition fails.
At the end of for loop if first!=last. This means the remaining array satisfies the condition. Apply the same steps and add total to count.
At the end of both loops, return count as result.
Example
#include <iostream> using namespace std; int sub_ele_diff_one(int arr[], int size){ int count = 0, first = 0, last = 0; for (int i = 1; i < size; i++){ if (arr[i] - arr[i - 1] == 1 || arr[i-1] - arr[i] == 1){ first++; } else{ int temp = first - last + 1; int total = temp * (temp - 1) / 2; count = count + total; first = i; last = i; } } if (first != last){ int temp = first - last + 1; int total = temp * (temp - 1) / 2; count = count + total; } return count; } int main(){ int arr[] = { 1, 2, 4, 3 }; int size = sizeof(arr) / sizeof(arr[0]); cout<<"Count of Subarrays with Consecutive elements differing by 1 are: "<<sub_ele_diff_one(arr, size); return 0; }
Output
If we run the above code it will generate the following output −
Count of Subarrays with Consecutive elements differing by 1 are: 2