Group a sorted array based on the difference between current and previous elements in JavaScript


In the given problem statement we have to group a sorted array based on the difference between the current and previous elements with the help of Javascript functionalities. To group a sorted array based on the difference between the current and previous items we can iterate over the array and create a new array of groups.

Understanding the Problem Statement

The above problem statement states that we have to find out the group of elements based on the difference between the current and previous elements in the array. As we have given a sorted array so we need to group its elements based on the difference between every element and its previous element. Simply we can say that we have to create a new group whenever the difference between consecutive elements is greater than 1. The output should be an array of groups in which every group contains elements with a difference of 1 and less between consecutive elements.

For example, suppose we have an array like [1, 2, 3, 5, 7, 9], so the function we will create should give the output as [ [ 1, 2, 3 ], [ 5, 7, 9 ] ]. Here we can see the difference between 1 and 2 is 1 and similarly the difference between 2 and 3 is also 1. So keeping in mind the same difference we will make a group of them as [1, 2, 3].

Algorithm

Step 1: To solve the given problem of grouping a sorted array as per the difference between the current and previous elements we have to create an empty array to store the groups.

Step 2: We will use a variable to keep track of the current group.

Step 3: And iterate over the elements of the sorted array and for each item we will calculate the difference between the current and previous items.

Step 4: If the difference is larger than one then create a new group and include the current item in the existing group.

Example

//function to get the group array by difference
function groupByDiff(arr) {
   //Check if the provided array is empty 
   if (arr.length === 0) {
      return [];
   }
   //Initialize a variable to store the result
   const result = [];
   let currGroup = [arr[0]];

   for (let i = 1; i < arr.length; i++) {
      const diff = arr[i] - arr[i - 1];

      if (diff <= 1) {
         currGroup.push(arr[i]);
      } else {
         result.push(currGroup);
         currGroup = [arr[i]];
      }
   }

   result.push(currGroup);

   return result;
}

const sortedArray = [1, 2, 3, 5, 7, 9, 10, 14, 16, 17, 20, 21, 23];
const groupedArray = groupByDiff(sortedArray);

console.log(groupedArray);

Output

[
    [ 1, 2, 3 ], [ 5 ],
    [ 7 ],       [ 9, 10 ],
    [ 14 ],      [ 16, 17 ],
    [ 20, 21 ],  [ 23 ]
]

Complexity

The time complexity of the provided code is O(n). Here n is the number of elements in the array. The reason for this complexity is that the function traverses over the array once and does constant time operation for every element.

The space complexity for the code is O(m) in the worst case. Because all the elements in the input array have a difference greater than 1 and each element is in a separate group so resulting in m equal to the size of the input array. And if the difference between elements is generally small the number of groups will be smaller. Here m is the number of groups in the resulting array.

Conclusion

In the conclusion the code effectively groups a sorted array as per the difference between consecutive elements. Iterating over the array once it creates groups whenever the difference between elements is greater than 1. The code provides a correct grouping of elements and has a linear time complexity and makes the code effective.

Updated on: 14-Aug-2023

333 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements