Absolute Difference of all pairwise consecutive elements in an array - GeeksforGeeks

26 Oct, 2021
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-2Examples:  
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++
Java
Python 3
C#
PHP
Javascript
// C++ program to print the absolute// difference of the consecutive elements#include <iostream>using namespace std; // Function to print pairwise absolute// difference of consecutive elementsvoid 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 Codeint main(){    int arr[] = { 4, 10, 15, 5, 6 };    int n = sizeof(arr) / sizeof(arr[0]);     pairwiseDifference(arr, n);     return 0;}
// Java program to print the absolute// difference of the consecutive elements class GFG{// Function to print pairwise absolute// difference of consecutive elementsstatic 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 Codepublic 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 program to print the absolute# difference of the consecutive elements # Function to print pairwise absolute# difference of consecutive elementsdef 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 Codeif __name__=="__main__":    arr = [ 4, 10, 15, 5, 6 ]    n = len(arr)     pairwiseDifference(arr, n) # This code is contributed# by ChitraNayal
// C# program to print the absolute// difference of the consecutive elementsusing System; class GFG{// Function to print pairwise absolute// difference of consecutive elementsstatic 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 Codepublic static void Main(String[] args){    int []arr = { 4, 10, 15, 5, 6 };    int n = arr.Length;     pairwiseDifference(arr, n);}}
<?php// PHP program to print the absolute// difference of the consecutive elements // Function to print pairwise absolute// difference of consecutive elementsfunction 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?>
<script>// javascript program to print the absolute// difference of the consecutive elements    // Function to print pairwise absolute    // difference of consecutive elements    function pairwiseDifference(arr , n) {        var diff;        for (i = 0; i < n - 1; i++) {             // absolute difference between            // consecutive numbers            diff = Math.abs(arr[i] - arr[i + 1]);            document.write(diff + " ");        }    }     // Driver Code             var arr = [ 4, 10, 15, 5, 6 ];        var n = arr.length;         pairwiseDifference(arr, n); // This code contributed by umadevi9616</script>
6 5 10 1
 
Time complexity : O(n) 
Mithun Kumar
ukasp
ankita_saini
VishalBachchas
umadevi9616
Kirti_Mangal
Technical Scripter 2018
Arrays
Mathematical
School Programming
Technical Scripter
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Program for Fibonacci numbers
C++ Data Types
Write a program to print all permutations of a given string
Set in C++ Standard Template Library (STL)
Coin Change | DP-7