Question

In: Computer Science

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 Input

The first line consist of two integers, N and M. The second line consist of N integers, A1, A2, ..., AN . The next M lines consist of two integers, Li and Ri.

Format Output

Output M lines, the answer for Li and Ri, where i is an integer from 1 to M. Constraints

• 1 ≤ N, M ≤ 105 • 1≤Ai ≤102
• 1≤Li ≤Ri ≤N

Sample Input 1 (standard input)

53
13 7 1 5 9 11
22
25

Sample Output 1 (standard output)

13 0 10

please use c/c++ language

Solutions

Expert Solution

If you like the solution please give it a thumbs up. And if you have any query please let me know in the comments.

Solution :-

C++ Code :-

#include <iostream>
using namespace std;
  
int main() {
    int n,m;
    cin>>n>>m;

    int a[n+1];
    a[0] = 0;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    
    int sum[n];
    sum[0] = a[0];
    /* sum array is kind of prefix array, sum[i] stores sum of all odd
     index numbers upto ith index */
    for(int i=1;i<=n;i++)
    {
        // if index is odd then only add
        if(i%2 == 1)
            sum[i] = sum[i-1] + a[i];
        else 
            sum[i] = sum[i-1];
    }

    // now simply result for any query (l,r) will sum(r)-sum(l) --> (taking sum upto r index and then excluding upto l)
    for(int i=0;i<m;i++)
    {
        int l,r;
        cin>>l>>r;

        cout<<sum[r]-sum[l-1]<<" ";
    }

}

Sample input :-

6 4
4 12 9 5 31 8
1 4
1 6
1 1
3 6

Sample Output :-

13 44 4 40 

Related Solutions

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...
Let a1 ≥ a2, . . . , an be a sequence of positive integers whose...
Let a1 ≥ a2, . . . , an be a sequence of positive integers whose sum is 2n − 2. Prove that there exists a tree T on n vertices whose vertices have degrees a1, a2, . . . , an. Sketch of solution: Prove that there exist i and j such that ai = 1 and aj ≥ 2. Remove ai, subtract 1 from aj and induct on n.
Question in graph theory: 1. Let (a1,a2,a3,...an) be a sequence of integers. Given that the sum...
Question in graph theory: 1. Let (a1,a2,a3,...an) be a sequence of integers. Given that the sum of all integers = 2(n-1) Write an algorithm that, starting with a sequence (a1,a2,a3,...an) of positive integers, either constructs a tree with this degree sequence or concludes that none is possible.
Prove: If a1 = b1 mod n and a2 = b2 mod n then (1) a1...
Prove: If a1 = b1 mod n and a2 = b2 mod n then (1) a1 + a2 = b1 + b2 mod n, (2) a1 − a2 = b1 − b2 mod n, and (3) a1a2 = b1b2 mod n.
Consider the following algorithm, which takes as input a sequence of ?n integers ?1,?2,…,??a1,a2,…,an and produces...
Consider the following algorithm, which takes as input a sequence of ?n integers ?1,?2,…,??a1,a2,…,an and produces as output a matrix ?={???}M={mij} where ???mij is the minim term in the sequence of integers ??,??+1,…,??ai,ai+1,…,aj for ?≥?j≥i and ???=0mij=0 otherwise. for i := 1 to n for j := 1+1 to n for k:= i+1 to j m[i][j] := min(m[i][j], a[k]) end for end for end for return m a.) Show that this algorithm uses ?(?3)O(n3) comparisons to compute the matrix M....
You are given two arrays A1 and A2, each with n elements sorted in increasing order....
You are given two arrays A1 and A2, each with n elements sorted in increasing order. For simplicity, you may assume that the keys are all distinct from each other. Describe an o(log n) time algorithm to find the (n/2) th smallest of the 2n keys assuming that n is even.
given an array A = {a1, a2, ... , an} find the number of the continuous...
given an array A = {a1, a2, ... , an} find the number of the continuous subarrays of gcd one continuous subarrays of gcd one means gcd(ai, ai+1, ... , aj) = 1 for example {2, 4, 6, 3} or {1} are continuous subarrays of gcd one
1. Give a direct proof that if n is an odd integers, then n3 is also...
1. Give a direct proof that if n is an odd integers, then n3 is also an odd integer. 2. Give a proof by contradiction that the square of any positive single digit decimal integer cannot have more than two decimal digits.
Present an O(n) algorithm that sorts n positive integer numbers a1, a2, . . . ,...
Present an O(n) algorithm that sorts n positive integer numbers a1, a2, . . . , an which are known to be bounded by n 2 (so ai ≤ n 2 , for every i = 1, . . . , n. Use the idea of Radix Sort (discussed in class and presented in Section 8.3 in the textbook). Illustrate your algorithm by showing on paper similar to Fig. 8.3, page 198 in the textbook (make sure you indicate clearly...
Recall the linear search algorithm: procedure linear search (x: integer, a1, a2, ..., an: distinct integers)...
Recall the linear search algorithm: procedure linear search (x: integer, a1, a2, ..., an: distinct integers) i := 1 while (i ≤ n and x 6= ai) i := i + 1 if i ≤ n then location:= i else location:= 0 return location Apply the linear search algorithm to search for the number 3 in the list 1, 5, 3, 9. (a) In this application of the algorithm, what is the integer n? (b) What is the initial value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT