Given an array of integers of N elements. The task is to print the absolute difference of all of the pairwise consecutive elements.
Pairwise consecutive pairs of an array of size N are (a[i], a[i+1]) for all i ranging from 0 to N-2
Examples:
Input: arr[] = {8, 5, 4, 3, 15, 20}
Output: 3, 1, 1, 12, 5
Input: arr[] = {5, 10, 15, 20}
Output: 5, 5, 5
Approach: The solution is to traverse the array and calculate and print the absolute difference of every pair (arr[i], arr[i+1]).
Below is the implementation of the above approach:
C++
// C++ program to print the absolute // difference of the consecutive elements #include <iostream> using namespace std; // Function to print pairwise absolute // difference of consecutive elements void pairwiseDifference(int arr[], int n) { int diff; for (int i = 0; i < n - 1; i++) { // absolute difference between // consecutive numbers diff = abs(arr[i] - arr[i + 1]); cout << diff << " "; } } // Driver Code int main() { int arr[] = { 4, 10, 15, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]); pairwiseDifference(arr, n); return 0; } |
Java
// Java program to print the absolute // difference of the consecutive elements class GFG{ // Function to print pairwise absolute // difference of consecutive elements static void pairwiseDifference(int arr[], int n) { int diff; for (int i = 0; i < n - 1; i++) { // absolute difference between // consecutive numbers diff = Math.abs(arr[i] - arr[i + 1]); System.out.print(diff+" "); } } // Driver Code public static void main(String[] args) { int arr[] = { 4, 10, 15, 5, 6 }; int n = arr.length; pairwiseDifference(arr, n); } } // This code is contributed by mits |
Python 3
# Python 3 program to print the absolute # difference of the consecutive elements # Function to print pairwise absolute # difference of consecutive elements def pairwiseDifference(arr, n): for i in range(n - 1) : # absolute difference between # consecutive numbers diff = abs(arr[i] - arr[i + 1]) print(diff , end = " ") # Driver Code if __name__=="__main__": arr = [ 4, 10, 15, 5, 6 ] n = len(arr) pairwiseDifference(arr, n) # This code is contributed # by ChitraNayal |
C#
// C# program to print the absolute // difference of the consecutive elements using System; class GFG{ // Function to print pairwise absolute // difference of consecutive elements static void pairwiseDifference(int []arr, int n) { int diff; for (int i = 0; i < n - 1; i++) { // absolute difference between // consecutive numbers diff = Math.Abs(arr[i] - arr[i + 1]); Console.WriteLine(diff+" "); } } // Driver Code public static void Main(String[] args) { int []arr = { 4, 10, 15, 5, 6 }; int n = arr.Length; pairwiseDifference(arr, n); } } |
PHP
<?php // PHP program to print the absolute // difference of the consecutive elements // Function to print pairwise absolute // difference of consecutive elements function pairwiseDifference($arr, $n) { $diff = 0; for ($i = 0; $i < $n - 1; $i++) { // absolute difference between // consecutive numbers $diff = abs($arr[$i] - $arr[$i + 1]); echo $diff." "; } } // Driver Code $arr = array( 4, 10, 15, 5, 6 ); $n = sizeof($arr); pairwiseDifference($arr, $n); // This code is contributed by mits ?> |
6 5 10 1
Time complexity : O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Product of all pairwise consecutive elements in an Array
- Modulus of all pairwise consecutive elements in an Array
- Smallest subsequence with sum of absolute difference of consecutive elements maximized
- Find the winner by adding Pairwise difference of elements in the array until Possible
- Minimum number of elements to be removed so that pairwise consecutive elements are same
- Subsequence with maximum pairwise absolute difference and minimum size
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Count elements in first Array with absolute difference greater than K with an element in second Array
- k-th smallest absolute difference of two elements in an array
- Absolute Difference of even and odd indexed elements in an Array
- Maximum absolute difference between distinct elements in an Array
- Array value by repeatedly replacing max 2 elements with their absolute difference
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Sort an Array based on the absolute difference of adjacent elements
- Minimum absolute difference of adjacent elements in a circular array
- Find K elements whose absolute difference with median of array is maximum
- Count maximum elements of an array whose absolute difference does not exceed K
- Minimize the maximum absolute difference of adjacent elements in a circular array
- Sort array such that absolute difference of adjacent elements is in increasing order
- Generate original array from difference between every two consecutive elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

