Answer to Question #231619 in C for aman

Question #231619

Harry loves numbers in the range [m1, m2] (m1 and m2 included). carry decides to gift harry an array of numbers for his birthday. harry wants to find the number of pairs of indices [l, r] for which the sum of all the elements in the range [l, r] lies between m1 and m2.  

  1. What if the numbers could be negative as well? Can you think of an O(nlogn) solution in this case? (Hint: use sorting)  




1
Expert's answer
2021-08-31T23:52:07-0400
#include <iostream>


using namespace std;


int n, m1, m2, g[100], a = 0;


int main()
{
    cin >> n >> m1 >> m2;
    for (int i = 0; i < n; i++) cin >> g[i];
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            if (i != j && g[i]+g[j]<=m2 && g[i]+g[j]>=m1) a++;
        }
    }
    cout << a;


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS