In: Computer Science
Jojo is given N integers, A 1, A 2, ..., A N by his teacher. His teacher also give him an integer K and 2 M integers, L 1, L 2, ..., L M and R 1, R 2, ..., R M. For each i from 1 to M , his teacher asked him to calculate the sum of multiple of K index numbers from index L i to R i . For example if L i = 3 and R i = 10 and K = 3, then he has to calculate the value of ( A 3 + A 6 + A9). Help him by making the program to calculate it quickly!
Format Input:
The first line consist of three integers, N , M, and K. The second line consist of N integers, A 1, A 2, ..., A N . The next M lines consist of two integers, L i and R i.
Format Output:
Output M lines, the answer for L i and R i , where i is an integer from 1 to M.
Constraints
• 1 ≤ N, M ≤ 105
• 1 ≤ A i ≤ 102 • 1 ≤ K ≤ 10
• 1 ≤ L i ≤ R i ≤ N
Sample Input 1 (standard input):
5 3 2
100 3 1 87 6
1 1
1 5
3 4
Sample Output 1 (standard output):
0
90
87
Note: Use C language, don’t use function./recursive/void.
The input is N,M,K not only N &M.
#include <stdio.h>
int main()
{
//N, M and K value
int N, M, K;
//for loop iteration
int i, j;
//to store Li, Ri and sum
int Li[100000], Ri[100000], sum[100000];
//Array A
int A[100000];
//user input for N and M
scanf("%d %d %d", &N, &M, &K);
//user input for Array A
for(i = 0; i < N; i++)
{
scanf("%d", &A[i]);
}
//user input for Li and Ri and calculate sum of them
for(i = 0; i < M; i++)
{
sum[i] = 0; //initially sum is 0
//user input for Li and Ri
scanf("%d %d", &Li[i], &Ri[i]);
//execute till Li to Ri
do{
//if odd index then added to sum
if(Li[i] % K == 0)
{
sum[i] = sum[i] + A[Li[i] - 1];
}
Li[i]++;
}while(Li[i] <= Ri[i]);
}
//to print sum
printf("\n");
for(i =0; i < M; i++)
{
printf("%d\n", sum[i]);
}
return 0;
}
Please refer below screenshot of code for better understanding of indentation.
Sample output is also given in above screenshot.