The Wayback Machine - https://web.archive.org/web/20211211093122/https://www.geeksforgeeks.org/absolute-difference-of-all-pairwise-consecutive-elements-in-a-set/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Absolute difference of all pairwise consecutive elements in a Set

  • Last Updated : 10 Dec, 2021

Given a set of integers of N elements. The task is to print the absolute difference of all of the pairwise consecutive elements in a set. Pairwise consecutive pairs of a set of size N are accessed using iterator. 

Example:

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.

Input: s = {8, 5, 4, 3, 15, 20}

Output: 1 1 3 7 5



Explanation:

The set is : 3 4 5 8 15 20
The difference between 4 and 3 is 1
The difference between 5 and 4 is 1
The difference between 8 and 5 is 3
The difference between 15 and 8 is 7
The difference between 20 and 15 is 5

Input: s = {5, 10, 15, 20}

Output: 5 5 5 

Explanation:
The set is : 5 10 15 20
The difference between 10 and 5 is 5
The difference between 15 and 10 is 5
The difference between 20 and 15 is 5

The article Absolute Difference of all pairwise consecutive elements in an array covers the approach to find the absolute difference of all pairwise consecutive elements in an array. 

Approach: This problem can be solved using two pointer algorithm. We will be using iterators as the two pointers to iterate the set and check for a given condition. Follow the steps below to understand the solution to the above problem:

  1. Declare two iterators itr1 and itr2 and both of them point to the beginning element of the set.
  2. Increment itr2 i.e. itr2++ at the beginning of the loop.
  3. Subtract values pointed by itr1 and itr2 i.e. *itr2 – *itr1.
  4. Increment itr1 at the end of the loop, this means *itr1++.
  5. If itr2 reaches the end of the set, then break the loop and exit.

In C++, the set elements are sorted and duplicates are removed before storing in the memory. Therefore, in the below C++ program the difference between the pairwise consecutive elements is computed on the sorted set as explained in the above examples.

Below is the C++ program implementation of the above approach:

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to calculate the
// difference of consecutive pairwise
// elements in a set
void display_difference(set<int> s)
{
    // Declaring the set iterator
    set<int>::iterator itr;
  
    // Printing difference between
    // consecutive elements in a set
    set<int>::iterator itr1 = s.begin();
    set<int>::iterator itr2 = s.begin();
    while (1) {
        itr2++;
        if (itr2 == s.end())
            break;
        cout << (*itr2 - *itr1) << " ";
        itr1++;
    }
}
// Driver code
int main()
{
    // Declaring the set
    set<int> s{ 8, 5, 4, 3, 15, 20 };
  
    // Invoking the display_difference()
    // function
    display_difference(s);
    return 0;
}

Output:

1 1 3 7 5

Time Complexity: O(n)
Auxiliary Space: O(n)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!