Absolute Difference of all pairwise consecutive elements in an array
Last Updated :
07 Jan, 2024
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, 5Input: 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;
}
C
// C program to print the absolute
// difference of the consecutive elements
#include <stdio.h>
int abs(int a)
{
int abs = a;
if(abs < 0)
abs = abs * (-1);
return abs;
}
// 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]);
printf("%d ",diff);
}
}
// Driver Code
int main()
{
int arr[] = { 4, 10, 15, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
pairwiseDifference(arr, n);
return 0;
}
// This code is contributed by kothavvsaakash.
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);
}
}
JavaScript
<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>
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
?>
Complexity Analysis:
- Time complexity: O(n),
- Auxiliary Space: O(1)
Similar Reads
Count of all pairs in an Array with minimum absolute difference Given an integer array arr[] of size N, the task is to count the total number of distinct pairs having minimum absolute difference. Examples: Input: arr[] = {4, 2, 1, 3} Output: 3 Explanation: The minimum absolute difference between the pairs {1, 2}, {2, 3}, {3, 4} is 1.Input: arr[] = {1, 3, 8, 10,
5 min read
Modulus of all pairwise consecutive elements in an Array Given an array of N elements. The task is to print the modulus of all of the pairwise consecutive elements. That is for all pair of consecutive elements say ((a[i], a[i+1])), print (a[i] % a[i+1]). Note: Consecutive pairs of an array of size N are (a[i], a[i+1]) for all i ranging from 0 to N-2. Exam
4 min read
Product of all pairwise consecutive elements in an Array Given an array of integers of N elements. The task is to print the product 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 : 40, 20, 12, 45, 300 Input
4 min read
Sum of minimum difference between consecutive elements of an array Given an array of pairs where each pair represents a range, the task is to find the sum of the minimum difference between the consecutive elements of an array where the array is filled in the below manner: Each element of an array lies in the range given at its corresponding index in the range array
10 min read
Sum of absolute differences of all pairs in a given array Given a sorted array of distinct elements, the task is to find the summation of absolute differences of all pairs in the given array. Examples: Input : arr[] = {1, 2, 3, 4} Output: 10 Sum of |2-1| + |3-1| + |4-1| + |3-2| + |4-2| + |4-3| = 10 Input : arr[] = {1, 8, 9, 15, 16} Output: 74 Input : arr[]
11 min read
Creating Array of Absolute Position Differences for Equal Elements Given an array arr[] of size N. The task is to create a new array result[] of the same size, where result[i] = â |i - j| for all j such that arr[i] = arr[j]. Examples: Input: arr = {2, 1, 4, 1, 2, 4, 4}Output: {4, 2, 7, 2, 4, 4, 5}Explanation:=> 0th Index: Another 2 is found at index 4. |0 - 4| =
9 min read