Open In App

Find the Nth number in the merged and sorted lists of given ranges

Improve
Improve
Like Article
Like
Save
Share
Report

Given two integer array, L and R and an integer N. Each range from the given array denote that every number in the range [L[i], R[i]] is present. The task is to calculate the N-th(0-based indexing) element when numbers from given ranges are ordered in their sorted order.

Examples

Input: L = {1, 5}, R = {3, 7}, N = 4
Output: 6
Explanation: The numbers present are {1, 2, 3, 5, 6, 7}. Therefore 4-th element(0-indexed) is 6.

Input: L = {1, 3}, R = {4, 5}, N = 3
Output: 3
Explanation: The numbers present are {1, 2, 3, 4, 3, 4, 5} and their sorted order is {1, 2, 3, 3, 4, 4, 5}. Therefore 3-rd element(0-indexed) is 3.

Approach: The task can be solved with the help of a binary search over the answer. 
Follow the below steps to solve the problem:

  • Calculate two variables min and max which store the minimum element from the L array and maximum element from the R array.
  • The range of the binary search will be [min, max].
  • For each mid = (min + max) / 2 calculate the position of the current element.
  • To calculate the position, iterate over all ranges and make a variable t = 0 to store the position. Check below two conditions if L[i] >= mid
    • If mid <= R[i], update t += mid – L[i] + 1.
    • else t += R[i] – L[i] + 1
  • The binary search range can be updated as
    • if(t > n) max = mid – 1.
    • else min = mid + 1.
  • The final answer will be stored in the variable min.

Below is the implementation of the above approach: 

C++




// C++ implementation for the above approach
#include <bits/stdc++.h>
using namespace std;
int nthElement(vector<int> L, vector<int> R, int n)
{
    // Store the size of the ranges
    int K = L.size();
 
    // Calculate the max and min values
    long long min = 2000000000, max = -2000000000;
    for (int i = 0; i < K; i++) {
        if (L[i] < min)
            min = L[i];
        if (R[i] > max)
            max = R[i];
    }
 
    // Do a binary search over answer
    while (min <= max) {
        long long mid = (min + max) / 2;
        long long t = 0;
 
        for (int i = 0; i < K; i++) {
            if (mid >= L[i]) {
                if (mid <= R[i]) {
                    t += mid - L[i] + 1;
                }
                else {
                    t += R[i] - L[i] + 1;
                }
            }
        }
 
        // Update the binary Search range.
        if (t > n) {
            max = mid - 1;
        }
        else {
            min = mid + 1;
        }
    }
    return min;
}
 
// Driver Code
int main()
{
    vector<int> L = { 1, 5 }, R = { 3, 7 };
    int N = 4;
    cout << nthElement(L, R, N);
}


Java




// Java implementation for the above approach
 
class GFG {
 
    public static long nthElement(int[] L, int[] R, int n) {
        // Store the size of the ranges
        int K = L.length;
 
        // Calculate the max and min values
        long min = 2000000000, max = -2000000000;
        for (int i = 0; i < K; i++) {
            if (L[i] < min)
                min = L[i];
            if (R[i] > max)
                max = R[i];
        }
 
        // Do a binary search over answer
        while (min <= max) {
            long mid = (min + max) / 2;
            long t = 0;
 
            for (int i = 0; i < K; i++) {
                if (mid >= L[i]) {
                    if (mid <= R[i]) {
                        t += mid - L[i] + 1;
                    } else {
                        t += R[i] - L[i] + 1;
                    }
                }
            }
 
            // Update the binary Search range.
            if (t > n) {
                max = mid - 1;
            } else {
                min = mid + 1;
            }
        }
        return min;
    }
 
    // Driver Code
    public static void main(String args[]) {
        int[] L = { 1, 5 }, R = { 3, 7 };
        int N = 4;
        System.out.println(nthElement(L, R, N));
    }
 
}
 
// This code is contributed by gfgking


Python3




# Python implementation for the above approach
 
def nthElement(L, R, n):
  # Store the size of the ranges
  K = len(L)
 
  # Calculate the max and min values
  min = 2000000000
  max = -2000000000;
  for i in range(K):
    if (L[i] < min):
      min = L[i]
    if (R[i] > max):
      max = R[i];
   
 
  # Do a binary search over answer
  while (min <= max):
    mid = (min + max) // 2;
    t = 0;
 
    for i in range(K):
      if (mid >= L[i]):
        if (mid <= R[i]):
          t += mid - L[i] + 1;
        else:
          t += R[i] - L[i] + 1;
 
    # Update the binary Search range.
    if (t > n):
      max = mid - 1;
    else:
      min = mid + 1;
     
  return min;
 
# Driver Code
 
L = [1, 5]
R = [3, 7];
N = 4;
print(nthElement(L, R, N));
 
# This code is contributed by gfgking


C#




// C# implementation for the above approach
 
using System;
class GFG {
 
    public static long nthElement(int[] L, int[] R, int n)
    {
       
        // Store the size of the ranges
        int K = L.Length;
 
        // Calculate the max and min values
        long min = 2000000000, max = -2000000000;
        for (int i = 0; i < K; i++) {
            if (L[i] < min)
                min = L[i];
            if (R[i] > max)
                max = R[i];
        }
 
        // Do a binary search over answer
        while (min <= max) {
            long mid = (min + max) / 2;
            long t = 0;
 
            for (int i = 0; i < K; i++) {
                if (mid >= L[i]) {
                    if (mid <= R[i]) {
                        t += mid - L[i] + 1;
                    } else {
                        t += R[i] - L[i] + 1;
                    }
                }
            }
 
            // Update the binary Search range.
            if (t > n) {
                max = mid - 1;
            } else {
                min = mid + 1;
            }
        }
        return min;
    }
 
    // Driver Code
    public static void Main() {
        int[] L = { 1, 5 }, R = { 3, 7 };
        int N = 4;
        Console.Write(nthElement(L, R, N));
    }
 
}
 
// This code is contributed by gfgking


Javascript




<script>
// Javascript implementation for the above approach
 
function nthElement(L, R, n) {
  // Store the size of the ranges
  let K = L.length;
 
  // Calculate the max and min values
  let min = 2000000000, max = -2000000000;
  for (let i = 0; i < K; i++) {
    if (L[i] < min)
      min = L[i];
    if (R[i] > max)
      max = R[i];
  }
 
  // Do a binary search over answer
  while (min <= max) {
    let mid = Math.floor((min + max) / 2);
    let t = 0;
 
    for (let i = 0; i < K; i++) {
      if (mid >= L[i]) {
        if (mid <= R[i]) {
          t += mid - L[i] + 1;
        }
        else {
          t += R[i] - L[i] + 1;
        }
      }
    }
 
    // Update the binary Search range.
    if (t > n) {
      max = mid - 1;
    }
    else {
      min = mid + 1;
    }
  }
  return min;
}
 
// Driver Code
 
let L = [1, 5]
let R = [3, 7];
let N = 4;
document.write(nthElement(L, R, N));
 
// This code is contributed by gfgking
 
</script>


Output

6

Time Complexity: O(N*log(max – min))
Auxiliary Space: O(N)



Last Updated : 15 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads