Queries to calculate the Sum of Array elements in the range [L, R] having indices as multiple of K
Last Updated :
22 Feb, 2023
Given an array arr[] consisting of N integers, and a matrix Q[][] consisting of queries of the form (L, R, K), the task for each query is to calculate the sum of array elements from the range [L, R] which are present at indices(0- based indexing) which are multiples of K and
Examples:
Input: arr[]={1, 2, 3, 4, 5, 6}, Q[][]={{2, 5, 2}, {0, 5, 1}}
Output:
8
21
Explanation:
Query1: Indexes (2, 4) are multiple of K(= 2) from the range [2, 5]. Therefore, required Sum = 3+5 = 8.
Query2: Since all indices are a multiple of K(= 1), therefore, the required sum from the range [0, 5] = 1 + 2 + 3 + 4 + 5 + 6 = 21
Input: arr[]={4, 3, 5, 1, 9}, Q[][]={{1, 4, 1}, {3, 4, 3}}
Output:
18
1
Approach: The problem can be solved using Prefix Sum Array and Range sum query technique. Follow the steps below to solve the problem:
- Initialize a matrix of size prefixSum[][] such that prefixSum[i][j] stores the sum of elements present in indices which are a multiple of i up to jth index.
- Traverse the array and precompute the prefix sums.
- Traverse each query, and print the result of prefixSum[K][R] – prefixSum[K][L – 1].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int L;
int R;
int K;
};
int kMultipleSum( int arr[], Node Query[],
int N, int Q)
{
int prefixSum[N + 1][N];
for ( int i = 1; i <= N; i++) {
prefixSum[i][0] = arr[0];
for ( int j = 0; j < N; j++) {
if (j % i == 0) {
prefixSum[i][j]
= arr[j] + prefixSum[i][j - 1];
}
else {
prefixSum[i][j]
= prefixSum[i][j - 1];
}
}
}
for ( int i = 0; i < Q; i++) {
int last
= prefixSum[Query[i].K][Query[i].R];
int first;
if (Query[i].L == 0) {
first
= prefixSum[Query[i].K][Query[i].L];
}
else {
first
= prefixSum[Query[i].K][Query[i].L - 1];
}
cout << last - first << endl;
}
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
int Q = 2;
Node Query[Q];
Query[0].L = 2, Query[0].R = 5, Query[0].K = 2;
Query[1].L = 3, Query[1].R = 5, Query[1].K = 5;
kMultipleSum(arr, Query, N, Q);
}
|
Java
import java.util.*;
class GFG{
static class Node
{
int L;
int R;
int K;
};
static void kMultipleSum( int arr[], Node Query[],
int N, int Q)
{
int prefixSum[][] = new int [N + 1 ][N];
for ( int i = 1 ; i <= N; i++)
{
prefixSum[i][ 0 ] = arr[ 0 ];
for ( int j = 0 ; j < N; j++)
{
if (j % i == 0 )
{
if (j != 0 )
prefixSum[i][j] = arr[j] +
prefixSum[i][j - 1 ];
}
else
{
prefixSum[i][j] = prefixSum[i][j - 1 ];
}
}
}
for ( int i = 0 ; i < Q; i++)
{
int last = prefixSum[Query[i].K][Query[i].R];
int first;
if (Query[i].L == 0 )
{
first = prefixSum[Query[i].K][Query[i].L];
}
else
{
first = prefixSum[Query[i].K][Query[i].L - 1 ];
}
System.out.print(last - first + "\n" );
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 };
int N = arr.length;
int Q = 2 ;
Node Query[] = new Node[Q];
for ( int i = 0 ; i < Q; i++)
Query[i] = new Node();
Query[ 0 ].L = 2 ;
Query[ 0 ].R = 5 ;
Query[ 0 ].K = 2 ;
Query[ 1 ].L = 3 ;
Query[ 1 ].R = 5 ;
Query[ 1 ].K = 5 ;
kMultipleSum(arr, Query, N, Q);
}
}
|
Python3
class GFG :
class Node :
L = 0
R = 0
K = 0
@staticmethod
def kMultipleSum( arr, Query, N, Q) :
prefixSum = [[ 0 ] * (N) for _ in range (N + 1 )]
i = 1
while (i < = N) :
prefixSum[i][ 0 ] = arr[ 0 ]
j = 0
while (j < N) :
if (j % i = = 0 ) :
if (j ! = 0 ) :
prefixSum[i][j] = arr[j] + prefixSum[i][j - 1 ]
else :
prefixSum[i][j] = prefixSum[i][j - 1 ]
j + = 1
i + = 1
i = 0
while (i < Q) :
last = prefixSum[Query[i].K][Query[i].R]
first = 0
if (Query[i].L = = 0 ) :
first = prefixSum[Query[i].K][Query[i].L]
else :
first = prefixSum[Query[i].K][Query[i].L - 1 ]
print ( str (last - first) + "\n" , end = "")
i + = 1
@staticmethod
def main( args) :
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
N = len (arr)
Q = 2
Query = [ None ] * (Q)
i = 0
while (i < Q) :
Query[i] = GFG.Node()
i + = 1
Query[ 0 ].L = 2
Query[ 0 ].R = 5
Query[ 0 ].K = 2
Query[ 1 ].L = 3
Query[ 1 ].R = 5
Query[ 1 ].K = 5
GFG.kMultipleSum(arr, Query, N, Q)
if __name__ = = "__main__" :
GFG.main([])
|
C#
using System;
class GFG{
class Node
{
public int L;
public int R;
public int K;
};
static void kMultipleSum( int []arr, Node []Query,
int N, int Q)
{
int [,]prefixSum = new int [N + 1, N];
for ( int i = 1; i <= N; i++)
{
prefixSum[i, 0] = arr[0];
for ( int j = 0; j < N; j++)
{
if (j % i == 0)
{
if (j != 0)
prefixSum[i, j] = arr[j] +
prefixSum[i, j - 1];
}
else
{
prefixSum[i, j] = prefixSum[i, j - 1];
}
}
}
for ( int i = 0; i < Q; i++)
{
int last = prefixSum[Query[i].K,Query[i].R];
int first;
if (Query[i].L == 0)
{
first = prefixSum[Query[i].K,Query[i].L];
}
else
{
first = prefixSum[Query[i].K,Query[i].L - 1];
}
Console.Write(last - first + "\n" );
}
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5, 6 };
int N = arr.Length;
int Q = 2;
Node []Query = new Node[Q];
for ( int i = 0; i < Q; i++)
Query[i] = new Node();
Query[0].L = 2;
Query[0].R = 5;
Query[0].K = 2;
Query[1].L = 3;
Query[1].R = 5;
Query[1].K = 5;
kMultipleSum(arr, Query, N, Q);
}
}
|
Javascript
class Node {
constructor(L, R, K) {
this .L = L;
this .R = R;
this .K = K;
}
}
function kMultipleSum(arr, Query, N, Q)
{
let prefixSum = new Array(N + 1);
prefixSum[0] = new Array(N);
for (let i = 1; i <= N; i++)
{
prefixSum[i] = new Array(N);
prefixSum[i][0] = arr[0];
for (let j = 1; j < N; j++)
{
if (j % i === 0) {
prefixSum[i][j] = arr[j] + prefixSum[i][j - 1];
} else {
prefixSum[i][j] = prefixSum[i][j - 1];
}
}
}
for (let i = 0; i < Q; i++) {
last = prefixSum[Query[i].K][Query[i].R];
if (Query[i].L === 1) {
first = prefixSum[Query[i].K][0];
} else {
first = prefixSum[Query[i].K][Query[i].L -1];
}
console.log(last - first);
}
}
let arr = [ 1, 2, 3, 4, 5, 6 ];
let N = arr.length;
let Q = 2;
let Query=[];
Query.push( new Node(2,5,2));
Query.push( new Node(3,5,5));
kMultipleSum(arr, Query, N, Q);
|
Time Complexity: O(N2 + O(Q)), Computing the prefix sum array requires O(N2) computational complexity and each query requires O(1) computational complexity.
Auxiliary Space: O(N2)
Method 2:-
- Just traverse all the queries.
- For all the queries traverse the array from L or R.
- Check if the index is divisible by K or not. If yes then and the element into answer.
- In the end of each query print the answer
Solution for the above Approach:-
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int L;
int R;
int K;
};
void kMultipleSum( int arr[], Node Query[],
int N, int Q)
{
for ( int j=0;j<Q;j++){
int start = Query[j].L;
int end = Query[j].R;
int answer=0;
for ( int i=start;i<=end;i++)
{
if (i%Query[j].K==0)answer+=arr[i];
}
cout<<answer<<endl;
}
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
int Q = 2;
Node Query[Q];
Query[0].L = 2, Query[0].R = 5, Query[0].K = 2;
Query[1].L = 3, Query[1].R = 5, Query[1].K = 5;
kMultipleSum(arr, Query, N, Q);
}
|
Java
import java.io.*;
class Node {
int L, R, K;
Node( int L, int R, int K)
{
this .L = L;
this .R = R;
this .K = K;
}
}
class GFG {
static void kMultipleSum( int [] arr, Node[] Query, int N,
int Q)
{
for ( int j = 0 ; j < Q; j++) {
int start = Query[j].L;
int end = Query[j].R;
int answer = 0 ;
for ( int i = start; i <= end; i++) {
if (i % Query[j].K == 0 )
answer += arr[i];
}
System.out.println(answer);
}
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 , 6 };
int N = arr.length;
int Q = 2 ;
Node[] Query = new Node[Q];
Query[ 0 ] = new Node( 2 , 5 , 2 );
Query[ 1 ] = new Node( 3 , 5 , 5 );
kMultipleSum(arr, Query, N, Q);
}
}
|
Python3
class Node:
def __init__( self , L, R, K):
self .L = L
self .R = R
self .K = K
def kMultipleSum(arr, Query, N, Q):
for j in range (Q):
start = Query[j].L
end = Query[j].R
answer = 0
for i in range (start, end + 1 ):
if i % Query[j].K = = 0 :
answer + = arr[i]
print (answer)
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
N = len (arr)
Q = 2
Query = [Node( 2 , 5 , 2 ), Node( 3 , 5 , 5 )]
kMultipleSum(arr, Query, N, Q)
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG
{
class Node {
public int L, R, K;
public Node( int L, int R, int K)
{
this .L=L;
this .R=R;
this .K=K;
}
}
static void kMultipleSum( int [] arr, Node[] Query,
int N, int Q)
{
for ( int j=0;j<Q;j++){
int start = Query[j].L;
int end = Query[j].R;
int answer=0;
for ( int i=start;i<=end;i++)
{
if (i%Query[j].K==0)
answer+=arr[i];
}
Console.WriteLine(answer);
}
}
static public void Main()
{
int [] arr = { 1, 2, 3, 4, 5, 6 };
int N = arr.Length;
int Q = 2;
Node[] Query= new Node[Q];
Query[0]= new Node(2,5,2);
Query[1]= new Node(3,5,5);
kMultipleSum(arr, Query, N, Q);
}
}
|
Javascript
class Node {
constructor(L,R,K)
{
this .L=L;
this .R=R;
this .K=K;
}
}
function kMultipleSum(arr, Query, N, Q)
{
for (let j=0;j<Q;j++){
let start = Query[j].L;
let end = Query[j].R;
let answer=0;
for (let i=start;i<=end;i++)
{
if (i%Query[j].K==0)
answer+=arr[i];
}
console.log(answer);
}
}
let arr = [ 1, 2, 3, 4, 5, 6 ];
let N = arr.length;
let Q = 2;
let Query=[];
Query.push( new Node(2,5,2));
Query.push( new Node(3,5,5));
kMultipleSum(arr, Query, N, Q);
|
Time Complexity:- O(Q*N)
Auxiliary Space:- O(1)
Share your thoughts in the comments
Please Login to comment...