给定一个整数m和n的范围(例如[a,b])是相交和重叠的。任务是查找不属于任何给定范围的范围内的所有数字。
例子:
Input: m = 6, ranges = {{1, 2}, {4, 5}}
Output: 3 6
As only 3 and 6 are missing from the given ranges.
Input: m = 5, ranges = {{2, 4}}
Output: 1 5
方法1:由于我们有n个范围,因此如果范围不重叠且不相交,请遵循此处描述的方法。
但是这里是重叠和相交的范围,因此首先合并所有范围,以使没有重叠或相交的范围。
合并完成后,从每个范围进行迭代并找到缺失的数字。
下面是上述方法的实现:
时间复杂度:O(nlogn)
C++
// C++ implementation of the approach
#include
#define ll long long int
using namespace std;
// function to find the missing
// numbers from the given ranges
void findNumbers(vector > ranges, int m)
{
vector ans;
// prev is use to store end of last range
int prev = 0;
// j is used as counter for range
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 range
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] << " ";
}
// function to return the ranges after merging
vector > mergeRanges(
vector > ranges, int m)
{
// sort all the ranges
sort(ranges.begin(), ranges.end());
vector > ans;
ll prevFirst = ranges[0].first,
prevLast = ranges[0].second;
// merging of overlapping ranges
for (int i = 0; i < m; i++) {
ll start = ranges[i].first;
ll last = ranges[i].second;
// ranges do not overlap
if (start > prevLast) {
ans.push_back({ prevFirst, prevLast });
prevFirst = ranges[i].first;
prevLast = ranges[i].second;
}
else
prevLast = last;
if (i == m - 1)
ans.push_back({ prevFirst, prevLast });
}
return ans;
}
// Driver code
int main()
{
// vector of pair to store the ranges
vector > ranges;
ranges.push_back({ 1, 2 });
ranges.push_back({ 4, 5 });
int n = ranges.size();
int m = 6;
// this function returns merged ranges
vector > mergedRanges
= mergeRanges(ranges, n);
// this function is use to find
// missing numbers upto m
findNumbers(mergedRanges, m);
return 0;
}
Java
// Java implementation of the approach
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 the missing
// numbers from the given ranges
static void findNumbers(ArrayList ranges,
int m)
{
ArrayList ans = new ArrayList<>();
// prev is use to store end of last range
int prev = 0;
// j is used as counter for range
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 range
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) + " ");
}
// Function to return the ranges after merging
static ArrayList mergeRanges(ArrayList ranges,
int m)
{
// Sort all the 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;
}
});
ArrayList ans = new ArrayList<>();
int prevFirst = ranges.get(0).first,
prevLast = ranges.get(0).second;
// Merging of overlapping ranges
for(int i = 0; i < m; i++)
{
int start = ranges.get(i).first;
int last = ranges.get(i).second;
// Ranges do not overlap
if (start > prevLast)
{
ans.add(new Pair(prevFirst, prevLast));
prevFirst = ranges.get(i).first;
prevLast = ranges.get(i).second;
}
else
prevLast = last;
if (i == m - 1)
ans.add(new Pair(prevFirst, prevLast));
}
return ans;
}
// Driver code
public static void main(String[] args)
{
// Vector of pair to store the ranges
ArrayList ranges = new ArrayList<>();
ranges.add(new Pair(1, 2));
ranges.add(new Pair(4, 5));
int n = ranges.size();
int m = 6;
// This function returns merged ranges
ArrayList mergedRanges = mergeRanges(ranges, n);
// This function is use to find
// missing numbers upto m
findNumbers(mergedRanges, m);
}
}
// This code is contributed by sanjeev2552
Python3
# Python3 implementation of the approach
# function to find the missing
# numbers from the given ranges
def findNumbers(ranges, m):
ans = []
# prev is use to store
# end of last range
prev = 0
# j is used as counter for range
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 range
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 = ' ')
# Function to return the ranges
# after merging
def mergeRanges(ranges, m):
# sort all the ranges
ranges.sort()
ans = []
prevFirst = ranges[0][0]
prevLast = ranges[0][1]
# Merging of overlapping ranges
for i in range(m):
start = ranges[i][0]
last = ranges[i][1]
# Ranges do not overlap
if (start > prevLast):
ans.append([prevFirst, prevLast])
prevFirst = ranges[i][0]
prevLast = ranges[i][1]
else:
prevLast = last;
if (i == m - 1):
ans.append([prevFirst, prevLast])
return ans
# Driver code
if __name__=="__main__":
# Vector of pair to store the ranges
ranges = []
ranges.append([ 1, 2 ]);
ranges.append([ 4, 5 ]);
n = len(ranges)
m = 6
# This function returns merged ranges
mergedRanges = mergeRanges(ranges, n);
# This function is use to find
# missing numbers upto m
findNumbers(mergedRanges, m);
# This code is contributed by rutvik_56
C#
// C# implementation to check
// if the year is a leap year
// using macros
using System;
using System.Collections.Generic;
class GFG {
// function to find the missing
// numbers from the given ranges
static void findNumbers(List> ranges, int m)
{
List ans = new List();
// prev is use to store end of last range
int prev = 0;
// j is used as counter for range
for (int j = 0; j < ranges.Count; j++) {
int start = ranges[j].Item1;
int end = ranges[j].Item2;
for (int i = prev + 1; i < start; i++)
ans.Add(i);
prev = end;
}
// for last range
for (int i = prev + 1; i <= m; i++)
ans.Add(i);
// finally print all answer
for (int i = 0; i < ans.Count; i++)
if (ans[i] <= m)
Console.Write(ans[i] + " ");
}
// function to return the ranges after merging
static List> mergeRanges(
List> ranges, int m)
{
// sort all the ranges
ranges.Sort();
List> ans =
new List>();
int prevFirst = ranges[0].Item1,
prevLast = ranges[0].Item2;
// merging of overlapping ranges
for (int i = 0; i < m; i++)
{
int start = ranges[i].Item1;
int last = ranges[i].Item2;
// ranges do not overlap
if (start > prevLast)
{
ans.Add(new Tuple(prevFirst, prevLast ));
prevFirst = ranges[i].Item1;
prevLast = ranges[i].Item2;
}
else
prevLast = last;
if (i == m - 1)
ans.Add(new Tuple(prevFirst, prevLast ));
}
return ans;
}
// Driver code
static void Main()
{
// vector of pair to store the ranges
List> ranges = new List>();
ranges.Add(new Tuple(1, 2));
ranges.Add(new Tuple(4, 5));
int n = ranges.Count;
int m = 6;
// this function returns merged ranges
List> mergedRanges = mergeRanges(ranges, n);
// this function is use to find
// missing numbers upto m
findNumbers(mergedRanges, m);
}
}
// This code is contributed by divyesh072019.
C++
// C++ implementation of the approach
#include
using namespace std;
// function to find the missing
// numbers from the given ranges
void findNumbers(vector > ranges, int m)
{
//Intialized the array of size m+1 with zero
int r[m+1];
memset(r,0,sizeof(int)*(m+1));
//for each range [L,R] do array[L]++ and array[R+1]--
for(int i=0;i > ranges;
ranges.push_back({ 1, 2 });
ranges.push_back({ 4, 5 });
int n = ranges.size();
int m = 6;
// this function is use to find
// missing numbers upto m
findNumbers(ranges, m);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
public class GFG
{
static class Point
{
int x;
int y;
}
// function to find the missing
// numbers from the given ranges
static void findNumbers(Point ranges[], int m)
{
// Intialized the array of size m+1 with zero
int r[] = new int[m + 1];
Arrays.fill(r, 0);
// for each range [L,R] do array[L]++ and array[R+1]--
for(int i = 0; i < 2; i++)
{
if(ranges[i].x <= m)
r[ranges[i].x]++;// array[L]++
if(ranges[i].y + 1 <= m)
r[ranges[i].y + 1]--;// array[R+1]--
}
// Now iterate array and take the sum
// if sum = x i.e, that particular number
// comes under x ranges
// thats means if sum = 0 so that number does not
// include in any of ranges(print these numbers)
int sum = 0;
for(int i = 1; i <= m; i++)
{
sum += r[i];
if(sum == 0)
{
System.out.print(i + " ");
}
}
}
// Driver code
public static void main(String[] args)
{
// vector of pair to store the ranges
Point ranges[] = new Point[2];
ranges[0] = new Point();
ranges[0].x = 1;
ranges[0].y = 2;
ranges[1] = new Point();
ranges[1].x = 4;
ranges[1].y = 5;
int n = 2;
int m = 6;
// this function is use to find
// missing numbers upto m
findNumbers(ranges, m);
}
}
// This code is contributed by divyeshrabadiya07.
Python3
# Python3 implementation of
# the above approach
# Function to find the missing
# numbers from the given ranges
def findNumbers(ranges, m):
# Intialized the array
# of size m+1 with zero
r = [0] * (m + 1)
# For each range [L,R] do
# array[L]++ and array[R+1]--
for i in range (len(ranges)):
if(ranges[i][0] <= m):
# array[L]++
r[ranges[i][0]] += 1
if(ranges[i][1] + 1 <= m):
# array[R+1]--
r[ranges[i][1] + 1] -= 1
# Now iterate array and
# take the sum if sum = x
# i.e, that particular number
# comes under x ranges
# thats means if sum = 0 so
# that number does not include
# in any of ranges(print these numbers)
sum = 0
for i in range(1, m + 1):
sum += r[i]
if(sum == 0):
print(i, end = " ")
# Driver Code
if __name__ == "__main__":
# Vector of pair to
# store the ranges
ranges = []
ranges.append([1, 2])
ranges.append([4, 5])
n = len(ranges)
m = 6
# This function is use
# to find missing numbers
# upto m
findNumbers(ranges, m)
# This code is contributed by Chitranayal
C#
// C# implementation of the approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
// function to find the missing
// numbers from the given ranges
static void findNumbers(ArrayList ranges, int m)
{
// Intialized the array of size m+1 with zero
int []r = new int[m + 1];
Array.Fill(r, 0);
// for each range [L,R] do array[L]++ and array[R+1]--
for(int i = 0; i < ranges.Count; i++)
{
if(((KeyValuePair)ranges[i]).Key <= m)
r[((KeyValuePair)ranges[i]).Key]++;// array[L]++
if(((KeyValuePair)ranges[i]).Value + 1 <= m)
r[((KeyValuePair)ranges[i]).Value + 1]--;// array[R+1]--
}
// Now iterate array and take the sum
// if sum = x i.e, that particular number
// comes under x ranges
// thats means if sum = 0 so that number does not
// include in any of ranges(print these numbers)
int sum = 0;
for(int i = 1; i <= m; i++)
{
sum += r[i];
if(sum == 0)
{
Console.Write(i + " ");
}
}
}
// Driver Code
static void Main(string []arg)
{
// vector of pair to store the ranges
ArrayList ranges = new ArrayList();
ranges.Add(new KeyValuePair(1, 2));
ranges.Add(new KeyValuePair(4, 5));
int n = ranges.Count;
int m = 6;
// this function is use to find
// missing numbers upto m
findNumbers(ranges, m);
}
}
// This code is contributed by pratham76
输出:
3 6
方法二:
制作一个大小为m的数组,并用零初始化。对每个范围{L,R}进行array [L] ++和array [R + 1] –。现在在求和时迭代数组,如果sum = x在索引i处,则表示数字i在求和范围内,例如,如果在索引2处sum = 3的值,则意味着数字2在3个范围内。因此,当总和为0表示给定数字不在任何给定范围内时,请打印这些数字
下面是上述方法2的实现。
C++
// C++ implementation of the approach
#include
using namespace std;
// function to find the missing
// numbers from the given ranges
void findNumbers(vector > ranges, int m)
{
//Intialized the array of size m+1 with zero
int r[m+1];
memset(r,0,sizeof(int)*(m+1));
//for each range [L,R] do array[L]++ and array[R+1]--
for(int i=0;i > ranges;
ranges.push_back({ 1, 2 });
ranges.push_back({ 4, 5 });
int n = ranges.size();
int m = 6;
// this function is use to find
// missing numbers upto m
findNumbers(ranges, m);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
public class GFG
{
static class Point
{
int x;
int y;
}
// function to find the missing
// numbers from the given ranges
static void findNumbers(Point ranges[], int m)
{
// Intialized the array of size m+1 with zero
int r[] = new int[m + 1];
Arrays.fill(r, 0);
// for each range [L,R] do array[L]++ and array[R+1]--
for(int i = 0; i < 2; i++)
{
if(ranges[i].x <= m)
r[ranges[i].x]++;// array[L]++
if(ranges[i].y + 1 <= m)
r[ranges[i].y + 1]--;// array[R+1]--
}
// Now iterate array and take the sum
// if sum = x i.e, that particular number
// comes under x ranges
// thats means if sum = 0 so that number does not
// include in any of ranges(print these numbers)
int sum = 0;
for(int i = 1; i <= m; i++)
{
sum += r[i];
if(sum == 0)
{
System.out.print(i + " ");
}
}
}
// Driver code
public static void main(String[] args)
{
// vector of pair to store the ranges
Point ranges[] = new Point[2];
ranges[0] = new Point();
ranges[0].x = 1;
ranges[0].y = 2;
ranges[1] = new Point();
ranges[1].x = 4;
ranges[1].y = 5;
int n = 2;
int m = 6;
// this function is use to find
// missing numbers upto m
findNumbers(ranges, m);
}
}
// This code is contributed by divyeshrabadiya07.
Python3
# Python3 implementation of
# the above approach
# Function to find the missing
# numbers from the given ranges
def findNumbers(ranges, m):
# Intialized the array
# of size m+1 with zero
r = [0] * (m + 1)
# For each range [L,R] do
# array[L]++ and array[R+1]--
for i in range (len(ranges)):
if(ranges[i][0] <= m):
# array[L]++
r[ranges[i][0]] += 1
if(ranges[i][1] + 1 <= m):
# array[R+1]--
r[ranges[i][1] + 1] -= 1
# Now iterate array and
# take the sum if sum = x
# i.e, that particular number
# comes under x ranges
# thats means if sum = 0 so
# that number does not include
# in any of ranges(print these numbers)
sum = 0
for i in range(1, m + 1):
sum += r[i]
if(sum == 0):
print(i, end = " ")
# Driver Code
if __name__ == "__main__":
# Vector of pair to
# store the ranges
ranges = []
ranges.append([1, 2])
ranges.append([4, 5])
n = len(ranges)
m = 6
# This function is use
# to find missing numbers
# upto m
findNumbers(ranges, m)
# This code is contributed by Chitranayal
C#
// C# implementation of the approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
// function to find the missing
// numbers from the given ranges
static void findNumbers(ArrayList ranges, int m)
{
// Intialized the array of size m+1 with zero
int []r = new int[m + 1];
Array.Fill(r, 0);
// for each range [L,R] do array[L]++ and array[R+1]--
for(int i = 0; i < ranges.Count; i++)
{
if(((KeyValuePair)ranges[i]).Key <= m)
r[((KeyValuePair)ranges[i]).Key]++;// array[L]++
if(((KeyValuePair)ranges[i]).Value + 1 <= m)
r[((KeyValuePair)ranges[i]).Value + 1]--;// array[R+1]--
}
// Now iterate array and take the sum
// if sum = x i.e, that particular number
// comes under x ranges
// thats means if sum = 0 so that number does not
// include in any of ranges(print these numbers)
int sum = 0;
for(int i = 1; i <= m; i++)
{
sum += r[i];
if(sum == 0)
{
Console.Write(i + " ");
}
}
}
// Driver Code
static void Main(string []arg)
{
// vector of pair to store the ranges
ArrayList ranges = new ArrayList();
ranges.Add(new KeyValuePair(1, 2));
ranges.Add(new KeyValuePair(4, 5));
int n = ranges.Count;
int m = 6;
// this function is use to find
// missing numbers upto m
findNumbers(ranges, m);
}
}
// This code is contributed by pratham76
输出:
3 6
时间复杂度:O(n)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。