Consecutive difference of elements in an Array
Tags: codenewbie, beginners, cpp, programming
Published: 2021-05-22 16:39:07

Hi👋 guys, In this post we gonna check a most basic but most important programming problem in which we have to find the consecutive difference of elements in a given Array. This problem I came across when I was giving a coding👨‍💻 test🧪 during my placement 👨‍✈️ season.

### Problem description
You are given with a circular array. Your task is calculate the difference between two consecutive number. And if difference is greater than ‘k’, print 1 else print 0

Input Description: You are given two numbers ‘n’, ’m’. Next line contains n space separated integers.

Output Description: Print 1 if the difference is greater than ‘m’.

*Sample Input :*
5 15
50 65 85 98 35

*Sample Output :*
 0 1 0 1 0
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ey26wxo1vv0r2oxkt6rg.png)
### Logic to follow to come-up with the solution :
<ol>
<li>Declare the required sets of variables to use in the code.</li>


<li>Input the user input size of the array and its elements.</li>

<li>Now iterate from initialization as 0 till the second last element.</li>

<li>And inside it finds the absolute difference of two consecutive numbers, also if the difference is greater than the inputted value then prints 1 or in other case print 0.</li>

(Absolute convert the -ve computed value into +ve. Ex. abs (-15) to (15)

<li>Now iterate from second last element till the last element, this is done to compute the difference of last and first element of the array.</li>

<li>Similarly, inside it find the absolute difference of two consecutive numbers, also if the difference is greater than the inputted value then prints 1 or in other case print 0.</li>

<li>At last we get the required set of the 1’s and 0’s by computing the absolute difference.</li></ol>

### Now let's come to the coding part of the problem👨‍💻

**Code ✍**
```
#include <iostream>
using namespace std;
int main() {
    int n,k;
    cin>>n>>k;
    int a[n];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(int i=0;i<n-1;i++)
    {
        if(abs(a[i]-a[i+1])>k)
        {
            cout<<"1 ";
        }
        if(abs(a[i]-a[i+1])<=k)
        {
            cout<<"0 ";
        }
    }
      for(int i=n-1;i<n;i++)
    {
        if(abs(a[n-1]-a[0])>k)
        {
            cout<<"1";
        }
        if(abs(a[n-1]-a[0])<=k)
        {
            cout<<"0";
        }
    }
    return 0;
}
```
**Sample Input**
```
5 15
50 65 85 98 35
```
**Sample Output**
```
0 1 0 1 0
```
Hence with the required set of problem we came across to know one of the important problem in Array and we can make the concept array strong with practicing such kinds of problem as much as we can.

Hope with this you learned and acquired some basic knowledge on C++ Programming.

**Drop a Love❤** if you liked👍 this post, then share 🤝this with your friends and if anything is confusing or incorrect then let me know in the comment section.

Thanks from my side, this is Mayank, keep learning and exploring !!

Support Me to write great articles by Clicking👇
<a href="https://www.buymeacoffee.com/mayankpathak" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>

Meet you in the next article......till than see ya🤚