JavaScript Sum odd indexed and even indexed elements separately and return their absolute difference


In this problem we will understand how to get the absolute difference of the sum of odd indexed and even indexed items separately with the help of Javascript functionalities. We will use a simple loop to iterate through all the elements of the array.

Understanding the problem statement

To understand the above problem statement we will break down the problem step by step:

  • So we are given an array of numbers.

  • Our task is to separate the items in the array in two groups: first is odd-indexed items and second is even-indexed items.

  • We have to calculate the sum of the items in every group separately.

  • And finally we have to find the absolute difference between the sum of off-indexed and the sum of even-indexed items.

Let's ee an example:

Suppose we have an array as [1, 2, 3, 4, 5, 6]

Now separating the items with the index, so we will get:

Odd-indexed items: [2, 4, 6] Even-indexed items: [1, 3, 5]

The sum of odd-indexed items is 2 + 4 + 6 = 12.

The sum of even-indexed items is: 1 + 3 + 5 = 9.

So the difference between the above sums is 12 - 9 = 3. So the output for this array will be 3.

Logic for the problem statement

In the given problem statement we have to sum odd indexed and even indexed items separately and give the output as their absolute difference between both sums. So for doing this we will first separate odd indexed and even indexed elements. After separating both types of elements we will sum even and odd elements separately and store it in different variables. Now we have both the sums then we will calculate the difference between these sums which is the essential result.

Algorithm

Step 1: First we will initialize two different variables and give it names like oddSum and evenSum. These variables will keep track of both odd-indexed and even-indexed items respectively. And set values to zero.

Step 2: Iterate through the array and check each element.

Step 3: Inside the loop we will check that the current index is odd or even. We can do this task by checking that the index % 2 === 0.

Step 4: If the index is even then add it to the evenSum variable.

Step 5: If the index is odd then add it to the oddSum variable.

Step 6: When the loop gets finished then calculate the difference between oddSum and evenSum with the help of Math.abs method.

Step 7: At the end we will return the absolute difference as the result.

Example

// Function to get the absolute difference
function calculateDifference(arr) {
  let oddSum = 0;
   let evenSum = 0;

   for (let i = 0; i < arr.length; i++) {
      if (i % 2 === 0) {
         // Even-indexed element
         evenSum += arr[i];
      } else {
         // Odd-indexed element
         oddSum += arr[i];
      }
   }

   return Math.abs(oddSum - evenSum);
}

const array = [1, 2, 3, 4, 5, 6];
const absoluteDifference = calculateDifference(array);
console.log(absoluteDifference);

Output

3

Complexity

The time complexity for the solution is O(n) as n is the length of the input array. Because we have iterated over every item of the array exactly once in a single loop. Besides the size of the array we have performed a constant amount of work for every element like checking the index and adding the item to the respective sum variable. So the time for this functionality is linear. The space complexity for the solution is O(1), this means the code requires a constant amount of additional memory. As we only require a few variables and these variables do not depend on the size of the input array.

Conclusion

In the above solution we have successfully implemented the given problem statement by separating the odd indexed and even indexed elements and by calculating the sum of them separately. This is the efficient way to solve these kinds of problems as it requires O(n) time to execute the task.

Updated on: 14-Aug-2023

937 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements