Not in Range

You are given 

106 boxes that are numbered from 1 to 106. The value of each box is equal to its number. 
There are N ranges and every range consists of two integers L  and R  denoting that the value of box in the range [LR] will turn out to be zero. 
Find the sum of values of all boxes from 1 to 106 .

Note: The ranges may overlap.

Example:

Input:  n = 5, L = [2, 23, 21, 101, 2002], R = [20, 200, 21, 2000, 999998]
Output: 2002023

Approach

C++

#include <bits/stdc++.h>
using namespace std;

long long notInRange(long long nint L[], int R[])
{
    sort(LL + n);
    sort(RR + n);
    long long m = 1000000;
    long long i = 0sum = 0;

    for (i = 0i < ni++)
    {
        if (i == 0)
        {
            sum += ((L[i] - 1) * (L[i])) / 2;
        }
        else
        {
            if (L[i] <= R[i - 1])
                L[i] = R[i - 1] + 1;
            if (L[i] != (R[i - 1] + 1))
                sum += ((L[i] - R[i - 1] - 1) * 
(L[i] + R[i - 1])) / 2;
        }
    }
    sum += ((m - R[i - 1]) * (m + R[i - 1] + 1)) / 2;
    return sum;
}
int main()
{

    long long n = 5;
    int L[n] = {223211012002};
    int R[n] = {20200212000999998};

    cout << notInRange(nLR<< "\n";

    return 0;
}


No comments:

Post a Comment