C++ program to find the sum of elements between two given indices in array

Finding the sum of elements between two given indices in an array is a common operation in programming. In C++, there are multiple ways to calculate the sum of elements between two given indices in C++. In this article, we are going to discuss various approaches to calculating the sum of elements between two indices in an array using C++.

How to Find the Sum of Elements Between Two Indices?

In this problem, we are given an array and two indices, L and R, and we have to find the sum of elements from index L and R (both L and R indices are included). The formula for calculating this sum is:

sum = arr[L] + arr[L+1] + arr[L + 2] +... + arr[R]

Example 1

  • Input:
    arr= [1, 2, 3, 4, 5, 6, 7, 8]
    L = 3 , R = 6
  • Output: 22

Explanation:

The sum of elements from index 3 to 6 is: 4 + 5 + 6 + 7 = 22

Example 2

  • Input:
    arr = [10, 20, 30, 40, 50, 60]
    L = 2 , R = 4
  • Output: 120

Explanation:

The sum of elements from index 2 to 4 is: 30 + 40 + 50 = 120

Method 1: Using a Simple Loop

This is the simplest and most direct approach. In this approach, we iterate through the array using a for loop from index L to R and sum up the elements. After calculating the sum of the elements, we return the sum.

Algorithm

  • Take input for the array and the indices L and R.
  • Initialize a variable sum to store the sum.
  • Loop through the array from index L to R, adding each element to sum.
  • Output the result.

Example

#include<iostream>
using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
    int L = 3, R = 6;
    int sum = 0;
    
    for (int i = L; i <= R; i++) {
        sum += arr[i];
    }
    
    cout << "The sum of elements from index " << L << " to " << R << " is: " << sum;
    return 0;
}
The sum of elements from index 3 to 6 is: 22

Time Complexity: O(N)
Space Complexity: O(1)

Method 2: Using Prefix Sum Array

In this approach, we optimize the addition process by precomputing the prefix sum array. Using this approach, we can reduce query time to constant time complexity, i.e., O(1).

Algorithm

  • Create a prefix sum array where prefix[i] stores the sum of elements from index 0 to i.
  • For range [L, R], calculate sum using: Sum = prefix[R] - prefix[L-1]
  • If L = 0, then sum is just prefix[R].
Original Array: 1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 Prefix Sum Array: 1 3 6 10 15 21 28 36 L = 3, R = 6 Sum = prefix[6] - prefix[2] = 28 - 6 = 22 Range [L, R] Used in calculation

Example

#include<iostream>
using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
    int n = 8;
    int prefix[n];
    
    // Compute prefix sum array
    prefix[0] = arr[0];
    for (int i = 1; i < n; i++) {
        prefix[i] = prefix[i - 1] + arr[i];
    }
    
    // Query the sum between indices L and R
    int L = 3, R = 6;
    int sum = prefix[R] - (L > 0 ? prefix[L - 1] : 0);
    
    cout << "The sum of elements from index " << L << " to " << R << " is: " << sum;
    return 0;
}
The sum of elements from index 3 to 6 is: 22

Time Complexity:
O(N) for building the prefix sum array.
O(1) for each sum query.
Space Complexity: O(N) for storing the prefix sum array.

Comparison

Method Time Complexity Space Complexity Best For
Simple Loop O(N) per query O(1) Single queries
Prefix Sum O(N) preprocessing, O(1) per query O(N) Multiple queries

Conclusion

For single queries, use the simple loop approach for better space efficiency. For multiple range sum queries on the same array, prefix sum provides optimal O(1) query time after O(N) preprocessing.

Updated on: 2026-03-15T23:19:01+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements