Question

In: Computer Science

Jojo is given N integers, A 1, A 2, ..., A N by his teacher. His...

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.

Solutions

Expert Solution

#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.


Related Solutions

Jojo is given N integers, A1, A2, ..., AN by his teacher. His teacher also give...
Jojo is given N integers, A1, A2, ..., AN by his teacher. His teacher also give him 2M integers, L1,L2,...,LM and R1,R2,...,RM. For each i from 1 to M, his teacher asked him to calculate the sum of odd index numbers from index Li to Ri. For example if Li = 3 and Ri = 7, then he has to calculate the value of (A3 + A5 + A7). Help him by making the program to calculate it quickly! Format...
prove 2 is a factor of (n+1)(n+2) for all positive integers
prove 2 is a factor of (n+1)(n+2) for all positive integers
Prove that for all integers n ≥ 2, the number p(n) − p(n − 1) is...
Prove that for all integers n ≥ 2, the number p(n) − p(n − 1) is equal to the number of partitions of n in which the two largest parts are equal.
An array A[0..n - 2] contains n-1 integers from 1 to n in increasing order. (Thus...
An array A[0..n - 2] contains n-1 integers from 1 to n in increasing order. (Thus one integer in this range is missing.) Design an algorithm in ​(Theta(log n)) to find the missing integer. Your algorithm should be given in pseudo code. For example, the array A could be {1, 2, 3, 4, 6, 7, 8, 9, 10} in which 5 is missing.
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
Given an array A[1..n] of integers - all of whose numbers are in the range [0,...
Given an array A[1..n] of integers - all of whose numbers are in the range [0, n^3 − 1] give an algorithm which sorts them in O(n) time.
Given an array A[1..n] of integers such that A[j] ≥ A[i] − d for every j...
Given an array A[1..n] of integers such that A[j] ≥ A[i] − d for every j > i. That means any inversion pair in A differs in value by at most d. Give an O(n + d) algorithm to sort this array.
Given an array A of n integers, describe an O(n log n) algorithm to decide if...
Given an array A of n integers, describe an O(n log n) algorithm to decide if the elements of A are all distinct. Why does your algorithm run in O(n log n) time?
For all integers n > 2, show that the number of integer partitions of n in...
For all integers n > 2, show that the number of integer partitions of n in which each part is greater than one is given by p(n)-p(n-1), where p(n) is the number of integer partitions of n.
Let X1 and X2 be uniform on the consecutive integers -n, -(n+1), ... , n-1, n....
Let X1 and X2 be uniform on the consecutive integers -n, -(n+1), ... , n-1, n. Use convolution to find the mass function for X1 + X2.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT