Find the summation of the product of Array elements in range [L, R]

• Last Updated : 21 Jan, 2022

Given an array arr[] and two integers L and R. The task is to find the sum of the product of all the pairs (i, j) in the range [L, R], such that i ≤ j.

Input: arr[] = { 1, 3, 5, 8 }, L = 0, R = 2
Output: 58
Explanation: As 1*1 + 1*3 + 1*5 + 3*3 + 3*5 + 5*5 = 58

Input: arr[] = { 2, 1, 4, 5, 3, 2, 1 }, L = 1, R = 5
Output: 140

Naive Approach: The brute force approach can be directly implemented by multiplying the indices using two nested loops and storing the sum in a variable.

Below is the implementation of the above approach:

C++

 // C++ implementation of the above approach#include using namespace std; // Function to return the sum// of (arr[i]*arr[j]) for all i and j// between the index L and Rint sum_of_products(int arr[], int N, int L,                    int R){    int sum = 0;     for (int i = L; i <= R; i++) {        for (int j = i; j <= R; j++) {            sum += arr[i] * arr[j];        }    }    return sum;} // Driver codeint main(){    int arr[] = { 1, 3, 5, 8 };    int N = sizeof(arr) / sizeof(arr);    int L = 0;    int R = 2;    cout << sum_of_products(arr, N, L, R);    return 0;}

Java

 // Java implementation of the above approachimport java.util.*;public class GFG {   // Function to return the sum  // of (arr[i]*arr[j]) for all i and j  // between the index L and R  static int sum_of_products(int[] arr, int N, int L,                             int R)  {    int sum = 0;     for (int i = L; i <= R; i++) {      for (int j = i; j <= R; j++) {        sum += arr[i] * arr[j];      }    }    return sum;  }   // Driver code  public static void main(String args[])  {    int[] arr = { 1, 3, 5, 8 };    int N = arr.length;    int L = 0;    int R = 2;    System.out.println(sum_of_products(arr, N, L, R));  }} // This code is contributed by Samim Hossain Mondal.

C#

 // C# implementation of the above approachusing System;class GFG {   // Function to return the sum  // of (arr[i]*arr[j]) for all i and j  // between the index L and R  static int sum_of_products(int[] arr, int N, int L,                             int R)  {    int sum = 0;     for (int i = L; i <= R; i++) {      for (int j = i; j <= R; j++) {        sum += arr[i] * arr[j];      }    }    return sum;  }   // Driver code  public static void Main()  {    int[] arr = { 1, 3, 5, 8 };    int N = arr.Length;    int L = 0;    int R = 2;    Console.WriteLine(sum_of_products(arr, N, L, R));  }} // This code is contributed by ukasp.

Javascript



Output
58

Time complexity: O(N2)
Auxiliary Space: O(1)

Efficient Approach: This problem can be efficiently solved by using the Prefix sum technique. In this method, store the prefix sum in pre-calculation and then iterate a single loop from L to R and multiply the corresponding prefix sum from that index to the last index.

Basically 1*1+1*3+1*5+3*3+3*5+5*5 can be written as 1*(1+3+5)+3*(3+5)+5*(5) = 1*(prefix_sum from 1 to 5)+3*(prefix_sum from 3 to 5)+5*(prefix sum from 5 to 5)

Below is the implementation of the above approach.

C++

 // C++ code to implement above approach#include using namespace std; // Function to return the sum of// (arr[i]*arr[j]) for all i and j// between the index L and Rint sum_of_products(int arr[], int n, int L,                    int R){    int sum = 0;    // Pre-calculating Prefix sum    int prefix_sum[n];    prefix_sum = arr;    for (int i = 1; i < n; i++) {        prefix_sum[i] = prefix_sum[i - 1]                        + arr[i];    }    // Using prefix sum to find    // summation of products    for (int i = L; i <= R; i++) {         // if-else for i==0 case        // in prefix sum        if (i != 0)            sum += arr[i]                   * (prefix_sum[R]                      - prefix_sum[i - 1]);        else            sum += arr[i] * (prefix_sum[R]);    }    return sum;} // Driver codeint main(){    int arr[] = { 1, 3, 5, 8 };    int N = sizeof(arr) / sizeof(arr);    int L = 0;    int R = 2;    cout << sum_of_products(arr, N, L, R);    return 0;}

Output
58

Time complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up