Skip to content

Sum of All Numbers in the Interval

Edited by Charles

Easy · Python · Array

Problem

Original source

Question:

Given an array of integers, calculate the sum of numbers in the input interval.

The input and output sample:

Input Sample.png

Output Sample.png

The first row is the length of the integer array, n. The following n rows are elements of the array. Then, the interval start and end indices in the array are given. For example, (0, 1) → [1, 2] → 3; (1, 3) → [2, 3, 4] → 9.

Explanation, examples & other approaches

Core idea

Given an array of integers, calculate the sum of numbers in the input interval.

The problem statement is available in the Problem block above.

Study record
Field Value
Attempts 4
Mastery ❚❚❚❚
The Latest Attempt January 3, 2026

Python solution

import sys
import sys.stdin.read

def IntervalSum():
    # Input length of the array
    data = input().split()
    index = 0
    # Input elements of the array
    n = int(data[index])
    index += 1
    vec = []
    for i in range(n):
        vec.append(int(data[index + i]))

    # Input the intervals for calculation
    index += n

    presum = [0] * n
    temp = 0
    for i in range(n):
        temp += vec[i]
        presum[i] = temp

    results = []
    while index < len(data):
        start = int(data[index])
        end = int(data[index + 1])
        index += 2

        if start == 0:
            temp_sum = presum[end]
        else:
            temp_sum = presum[end] - presum[start - 1]

        results.append(temp_sum)

    for result in results:
        print(result)

Back to Day 2 Array II · All Algorithm notes