📌  相关文章
📜  在给定的N范围内找到从1到M的缺失元素

📅  最后修改于: 2021-04-25 00:59:20             🧑  作者: Mango

给定N个分段作为范围[L,R],其中范围是不相交且不重叠的。任务是查找1到M之间不属于任何给定范围的所有数字。

例子

Input : N = 2, M = 6
        Ranges:
        [1, 2]
        [4, 5]
Output : 3, 6
Explanation: Only 3 and 6 are missing from
the above ranges.

Input : N = 1, M = 5
        Ranges:
        [2, 4]
Output : 1, 5

方法:假设我们有N个范围,它们是不重叠且不相交的。首先,根据起始值对所有细分进行排序。排序后,从每个细分中进行迭代并找到缺失的数字。

下面是上述方法的实现:

C++
// C++ program to find mssing elements
// from given Ranges
 
#include 
using namespace std;
 
// Function to find mssing elements
// from given Ranges
void findMissingNumber(vector > ranges, int m)
{
    // First of all sort all the given ranges
    sort(ranges.begin(), ranges.end());
 
    // store ans in a different vector
    vector ans;
 
    // prev is use to store end of
    // last range
    int prev = 0;
 
    // j is used as a counter for ranges
    for (int j = 0; j < ranges.size(); j++) {
        int start = ranges[j].first;
        int end = ranges[j].second;
 
        for (int i = prev + 1; i < start; i++)
            ans.push_back(i);
 
        prev = end;
    }
 
    // for last segment
    for (int i = prev + 1; i <= m; i++)
        ans.push_back(i);
 
    // finally print all answer
    for (int i = 0; i < ans.size(); i++) {
        if (ans[i] <= m)
            cout << ans[i] << " ";
    }
}
 
// Driver code
int main()
{
    int N = 2, M = 6;
 
    // Store ranges in vector of pair
    vector > ranges;
    ranges.push_back({ 1, 2 });
    ranges.push_back({ 4, 5 });
 
    findMissingNumber(ranges, M);
 
    return 0;
}


Java
// Java program to find missing elements
// from given Ranges
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
 
class GFG{
 
static class Pair
{
    int first, second;
     
    public Pair(int first, int second)
    {
        this.first = first;
        this.second = second;
    }
}
 
// Function to find mssing elements
// from given Ranges
static void findMissingNumber(ArrayList ranges,
                              int m)
{
     
    // First of all sort all the given ranges
    Collections.sort(ranges, new Comparator()
    {
        public int compare(Pair first, Pair second)
        {
            if (first.first == second.first)
            {
                return first.second - second.second;
            }
            return first.first - second.first;
        }
    });
 
    // Store ans in a different vector
    ArrayList ans = new ArrayList<>();
     
    // prev is use to store end of
    // last range
    int prev = 0;
     
    // j is used as a counter for ranges
    for(int j = 0; j < ranges.size(); j++)
    {
        int start = ranges.get(j).first;
        int end = ranges.get(j).second;
         
        for(int i = prev + 1; i < start; i++)
            ans.add(i);
             
        prev = end;
    }
     
    // For last segment
    for(int i = prev + 1; i <= m; i++)
        ans.add(i);
 
    // Finally print all answer
    for(int i = 0; i < ans.size(); i++)
    {
        if (ans.get(i) <= m)
            System.out.print(ans.get(i) + " ");
    }
}
 
// Driver code
public static void main(String[] args)
{
    int N = 2, M = 6;
     
    // Store ranges in vector of pair
    ArrayList ranges = new ArrayList<>();
    ranges.add(new Pair(1, 2));
    ranges.add(new Pair(4, 5));
 
    findMissingNumber(ranges, M);
}
}
 
// This code is contributed by sanjeev2552


Python3
# Python3 program to find missing
# elements from given Ranges
 
# Function to find mssing elements
# from given Ranges
def findMissingNumber(ranges, m):
 
    # First of all sort all the
    # given ranges
    ranges.sort()
 
    # store ans in a different vector
    ans = []
 
    # prev is use to store end
    # of last range
    prev = 0
 
    # j is used as a counter for ranges
    for j in range(len(ranges)):
        start = ranges[j][0]
        end = ranges[j][1]
 
        for i in range(prev + 1, start):
            ans.append(i)
 
        prev = end
 
    # for last segment
    for i in range(prev + 1, m + 1):
        ans.append(i)
 
    # finally print all answer
    for i in range(len(ans)):
        if ans[i] <= m:
            print(ans[i], end = " ")
     
# Driver Code
if __name__ == "__main__":
     
    N, M = 2, 6
 
    # Store ranges in vector of pair
    ranges = []
    ranges.append([1, 2])
    ranges.append([4, 5])
 
    findMissingNumber(ranges, M)
 
# This code is contributed
# by Rituraj Jain


C#
// C# program to find missing elements
// from given Ranges
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
        
class sortHelper : IComparer
{
   int IComparer.Compare(object a, object b)
   {
      Pair first = (Pair)a;
      Pair second = (Pair)b;
      if (first.first == second.first)
      {
        return first.second - second.second;
      }
         
      return first.first - second.first;
   }
}
 
public class Pair
{
    public int first, second;
     
    public Pair(int first, int second)
    {
        this.first = first;
        this.second = second;
    }
}
 
     
// Function to find mssing elements
// from given Ranges
static void findMissingNumber(ArrayList ranges, int m)
{
    IComparer myComparer = new sortHelper();
    ranges.Sort(myComparer);
     
    // Store ans in a different vector
    ArrayList ans = new ArrayList();
     
    // prev is use to store end of
    // last range
    int prev = 0;
     
    // j is used as a counter for ranges
    for(int j = 0; j < ranges.Count; j++)
    {
        int start = ((Pair)ranges[j]).first;
        int end = ((Pair)ranges[j]).second;
         
        for(int i = prev + 1; i < start; i++)
            ans.Add(i);
             
        prev = end;
    }
     
    // For last segment
    for(int i = prev + 1; i <= m; i++)
        ans.Add(i);
 
    // Finally print all answer
    for(int i = 0; i < ans.Count; i++)
    {
        if ((int)ans[i] <= m)
            Console.Write(ans[i] + " ");
    }
}
 
// Driver code
public static void Main(string[] args)
{
    int  M = 6;
     
    // Store ranges in vector of pair
    ArrayList ranges = new ArrayList();
    ranges.Add(new Pair(1, 2));
    ranges.Add(new Pair(4, 5));
 
    findMissingNumber(ranges, M);
}
}
 
// This code is contributed by rutvik_56


PHP


时间复杂度: O(n * log(n)),其中n是向量的长度

辅助空间: O(n)