A Sum Array Puzzle in C++



In this article, we will solve a sum array puzzle, where we have an array with n elements. We have to create another array of n elements such that the 'ith' position of the second array will hold the sum of all elements of the first array except the 'ith' element. We have one constraint, we cannot use the subtraction operator in this problem.

Example

Here is an example of calculating the sum of array elements except for the ith element:

Input:
arr[] = {5, 6, 7, 8}

Output:
res[] = {21, 20, 19, 18}

Here is the explanation of the above example:

for i = 0, res[0] = 6 + 7 + 8 = 21
for i = 1, res[1] = 5 + 7 + 8 = 20
for i = 2, res[2] = 5 + 6 + 8 = 19
for i = 3, res[3] = 5 + 6 + 7 = 18

The approaches for solving this sum array puzzle are given below:

Sum Array Puzzle Using Nested for Loop

In this approach, we have used a nested for loop for getting the sum array of elements. Here are the steps:

  • The outer for loop selects an element at index 'i' from the given array.
  • For each index at 'i', the inner for loop iterates over the array and compares the index at (i) with each array element.
  • If both index i and j are different, then we compute the sum of the array elements and store it in the sum.
  • Using the above three steps we calculate the sum of all elements present in the array except the selected element in the outer for loop.
  • We repeat the first three steps until we calculate the sum of all elements except for the selected elements in each iteration.

Example

The following example implements the above steps using nested for loop:

#include <iostream>
using namespace std;
void printArray(int arr[], int n)
{
   for (int i = 0; i < n; i++)
   {
      cout << arr[i] << " ";
   }
   cout << endl;
}

void sumArray(int arr[], int res[], int n)
{
   for (int i = 0; i < n; i++)
   {
      int sum = 0;
      for (int j = 0; j < n; j++)
      {
         if (i != j)
         {
            sum += arr[j];
         }
      }
      res[i] = sum;
   }
}
int main()
{
   int arr[7] = {5, 4, 7, 6, 9, 2, 3};
   int res[7];
   cout << "Given array is: ";
   printArray(arr, 7);

   sumArray(arr, res, 7);
   cout << "Result: ";
   printArray(res, 7);
}

The output of the above code is as follows:

Given array is: 5 4 7 6 9 2 3 
Result: 31 32 29 30 27 34 33

Sum Array Puzzle Using Prefix Suffix Technique

The following approach computes the sum of the array in two steps:

  • The first step calculates the sum of all elements before the selected element.

    For example: arr = {4, 2, 1, 6}. 
    sum[0] = 0, sum[1] = 4, sum[2] = 6, sum[3] = 7
            
  • The second step calculates the sum of all elements at the right side of the selected elements and then both the sums are added.

Example

In this example, we have used the above steps to calculate the sum array sum[i] from the array elements arr[i], except the ith element:

#include <iostream>
using namespace std;

void sum(int arr[], int n, int sum[])
{
   if (n <= 1)
   {
      if (n == 1)
         sum[0] = 0;
      return;
   }

   sum[0] = 0;
   for (int i = 1; i < n; i++)
   {
      sum[i] = sum[i - 1] + arr[i - 1];
   }

   int sumRight = 0;
   for (int i = n - 1; i >= 0; i--)
   {
      sum[i] = sum[i] + sumRight;
      sumRight = sumRight + arr[i];
   }
}

void printArray(int arr[], int n)
{
   for (int i = 0; i < n; i++)
   {
      cout << arr[i];
      if (i < n - 1)
         cout << " ";
   }
   cout << endl;
}

int main()
{
   int arr1[] = {1, 2, 3, 4, 5};
   int n1 = 5;
   int sum1[5];
   cout << "Input:  ";
   printArray(arr1, n1);

   sum(arr1, n1, sum1);
   cout << "Output: ";
   printArray(sum1, n1);

   return 0;
}

The output of the above code is as follows:

Input:  1 2 3 4 5
Output: 14 13 12 11 10

Complexity Comparison

Here is a comparison of the time and space complexity of all the above approaches.

Approach Time Complexity Space Complexity
Nested for Loop O(n^2) O(n)
Prefix Suffix Technique O(n) O(n)
Updated on: 2025-06-11T18:41:02+05:30

199 Views

Kickstart Your Career

Get certified by completing the course

Get Started