Given two integers n, m and an array arr[], where arr[i] represent a subarray from index arr[i][0] to arr[i][1]. The task is to construct an array ans[] of size n, such that the minimum MEX among the given m subarrays is as large as possible.
Note: There may exist multiple ans[], return one of them.
Examples:
Input: n=5, m=3, arr = [ [1,3], [2,5], [4,5] ]
Output: [1, 0, 2, 1, 0]
Explanation: MEX of first subarray ([1, 0, 2]) is 3, MEX of second subarray ([0, 2, 1, 0]) is 3 and MEX of third subarray ([0,1]) is 2. Hence, the minimum MEX is 2 which is the maximum possible.Input: n=4, m=2, arr=[ [1,4], [2,4] ]
Output: [3, 0, 1, 2]
Explanation: MEX of first subarray ([3, 0, 1, 2]) is 4, and MEX of second subarray ([1, 2, 0]) is 3 . Hence, the minimum MEX is 3 which is the maximum possible.
Approach:
It can be observed that the answer cannot be greater than the size of the smallest subarrays among the given subarrays because the MEX of the array of size n cannot be greater than. Let ans denote the size of the smallest subarray. So we can simple construct the array ans [0, 1, 2, .... ans-1, 0, 1, .... ]. Now since each of the given subarray has least ans size, so each of the subrarray will contain all the numbers from 0 to ans-1.
Follow the steps to solve the given problem:
- Iterate over m subarrays.
- Determine the minimum size subarray and stores its size in a variable.
- Run a loop from i is equal to 0 to n-1, and output print (i % ans), for each i.
Below is the implementation of above approach:
#include <bits/stdc++.h>
using namespace std;
void solve(int n, int m, vector<vector<int> >& subarrays)
{
// Initialize ans with the maximum possible value of n.
int ans = n;
// Iterate over the subarrays to find the minimum size
// among them.
for (int i = 0; i < m; i++) {
int x = subarrays[i][0];
int y = subarrays[i][1];
int diff = y - x;
// Update ans with the minimum size among subarrays.
ans = min(ans, diff + 1);
}
// Construct the array to maximize the minimum MEX.
for (int i = 0; i < n; i++) {
cout << (i % ans) << " ";
}
}
// Driver Code
int main()
{
int n=4, m=2;
vector<vector<int>> subarrays={{1,4}, {2,4}} ;
solve(n,m,subarrays);
}
// Java Implmentation
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
int n = 4;
int m = 2;
List<List<Integer>> subarrays = new ArrayList<>();
subarrays.add(List.of(1, 4));
subarrays.add(List.of(2, 4));
solve(n, m, subarrays);
}
public static void solve(int n, int m, List<List<Integer>> subarrays) {
// Initialize ans with the maximum possible value of n.
int ans = n;
// Iterate over the subarrays to find the minimum size among them.
for (List<Integer> subarray : subarrays) {
int x = subarray.get(0);
int y = subarray.get(1);
int diff = y - x;
// Update ans with the minimum size among subarrays.
ans = Math.min(ans, diff + 1);
}
// Construct the array to maximize the minimum MEX.
for (int i = 0; i < n; i++) {
System.out.print(i % ans + " ");
}
}
}
// This code is contributed by Sakshi
def construct_array(n, m, subarrays):
ans = n
for i in range(m):
x, y = subarrays[i]
diff = y - x + 1
ans = min(ans, diff)
result = []
for i in range(n):
result.append(i % ans)
return result
# Driver Code
if __name__ == "__main__":
n = 5
m = 3
subarrays = [[1, 3], [2, 5], [4, 5]]
ans = construct_array(n, m, subarrays)
print(ans)
// C# Implmentation
using System;
using System.Collections.Generic;
class GFG {
public static void Main()
{
int n = 4;
int m = 2;
List<List<int>> subarrays = new List<List<int>>();
subarrays.Add(new List<int>() { 1, 4 });
subarrays.Add(new List<int>() { 2, 4 });
solve(n, m, subarrays);
}
static void solve(int n, int m,
List<List<int> > subarrays)
{
// Initialize ans with the maximum possible
// value of n.
int ans = n;
// Iterate over the subarrays to find the minimum
// size among them.
foreach(var subarray in subarrays)
{
int x = subarray[0];
int y = subarray[1];
int diff = y - x;
// Update ans with the minimum size among
// subarrays.
ans = Math.Min(ans, diff + 1);
}
// Construct the array to maximize the minimum MEX.
for (int i = 0; i < n; i++) {
Console.Write((i % ans) + " ");
}
}
}
// This code is contributed by ragul21
function solve(n, m, subarrays) {
// Initialize ans with the maximum possible value of n.
let ans = n;
// Iterate over the subarrays to find the minimum size
// among them.
for (let i = 0; i < m; i++) {
let x = subarrays[i][0];
let y = subarrays[i][1];
let diff = y - x;
// Update ans with the minimum size among subarrays.
ans = Math.min(ans, diff + 1);
}
// Construct the array to maximize the minimum MEX.
for (let i = 0; i < n; i++) {
process.stdout.write((i % ans) + " ");
}
}
// Driver Code
function main() {
let n = 4, m = 2;
let subarrays = [[1, 4], [2, 4]];
solve(n, m, subarrays);
}
main();
Output
0 1 0 1 0
Time Complexity: O(n)
Auxiliary Space: O(1)