VOOZH about

URL: https://www.geeksforgeeks.org/dsa/precomputation-technique-on-arrays/

⇱ PreComputation Technique on Arrays - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

PreComputation Technique on Arrays

Last Updated : 23 Jul, 2025

Precomputation refers to the process of pre-calculating and storing the results of certain computations or data structures(array in this case) in advance, in order to speed up the execution time of a program. This can be useful in situations where the same calculations are needed multiple times, as it avoids the need to recalculate them every time.

This technique relies on fast retreival of already stored data rather than recalculation.

Some Common Pre-Computation Techniques used on Array:

Though pre-computation can be done in various ways over a wide range of questions, in this section we will look at some of the popular techniques of pre-computation:

Prefix/Suffix on 1-D Array:

A prefix array PRE[] is defined on an array ARR[], such that for each index 'i' PRE[i] stores the information about the values from ARR[0] to ARR[i]. This information can be in the form of prefix sum, prefix max, prefix gcd, etc.

Let's construct prefix array for ARR[] = {4, 2, 1, -1, 3}, to do this we can simply iterate ARR[] from 0 to size-1, and store PRE[i]=ARR[i]+PRE[i-1], after doing this we have PRE[] = {4, 6, 7, 6, 9}

Why do we need this Prefix Array?

  • For simplicity consider the example for Prefix Sum Array. Suppose we have an array ARR[] of size N, and Q queries and for each query, we have to output the sum of values between the index L to R.
  • The Brute Force way of doing this will result in a time complexity of O(N*Q) where we iterate from L to R in each query to know the sum. In order to improve this time complexity we can use Pre-Computation i.e. calculate the Prefix sum array before processing the queries, and then for each query simply return PRE[R]-PRE[L-1] as the result in O(1), the overall complexity then would be O(max(N, Q))
  • The Suffix array is similar to the Prefix array the only difference is that the Suffix array stores the information about the values from index 'i' to the end of the array rather that start of the array i..e. SUFFIX[i] stores information about values from ARR[i] to ARR[size-1].

Below is the code to construct a Prefix Sum and Suffix Sum array:


Output
Given Array: 4 2 1 -1 3 
Prefix Sum: 4 6 7 6 9 
Suffix Sum: 9 5 3 2 3 

Prefix on 2-D Array:

The above idea of Prefix on 1-D array can be expanded to 2-D array, where we have a 2-D array ARR[][] of size NxM and PRE[i][j] will store the information about the values from row 0 to 'i' and and column 0 to 'j' . Let's say we want to retrieve the sum of values of submatrice [i1, j1] to [i2, j2], we can simply use our 2-D prefix sum array to achieve this by simple Inclusion Exclusion principle PRE[i2][j2] - PRE[i2][j1-1] - PRE[i1-1][j2] + PRE[i1-1][j1-1].

Below is the code construct prefix sum for 2-D array:


Output
Give Array:
1 1 1 1 
1 1 1 1 
1 1 1 1 
1 1 1 1 
Prefix Sum Array:
1 2 3 4 
2 4 6 8 
3 6 9 12 
4 8 12 16 

Hashing the Arrays data:

There are times when Hashing can be used for pre-computation and drastically reduce the time complexity of program. Suppose for various query we would require the first index of an element 'e' in the array. Instead of calculating this again and again, we can simply iterate on array once and hash the element to the first index it occurs to and finally retrieve this value in O(1) for each query.

Moreover Hashing can be combined with other precomputation techniques like prefix_sum in order to enhance the usability.

Practice Problems:

Problem

Difficulty

Queries for the product of first N factorials

Easy

Range sum queries without updates

Easy

Range Queries for Frequencies of array elements

Easy

Count Primes in Ranges

Easy

Check in binary array the number represented by a subarray is odd or even

Easy

GCDs of given index ranges in an array

Easy

Mean of range in array

Medium

Difference Array | Range update query in O(1)

Medium

Range sum query using Sparse Table

Medium

Number whose sum of XOR with given array range is maximum

Medium

Number of primes in a subarray (with updates)

Medium

Sum of Interval and Update with Number of Divisors

Medium

Print modified array after multiple array range increment operations

Medium

Sparse Table

Hard

Range Minimum Query (Square Root Decomposition and Sparse Table)

Hard

Range Query on array whose each element is XOR of index value and previous element

Hard

Queries for GCD of all numbers of an array except elements in a given range

Hard

Queries of nCr%p in O(1) time complexity

Hard

Comment
Article Tags:
Article Tags: