Construct an Array having K Subarrays with all distinct elements
Last Updated :
13 Dec, 2022
Given integers N and K, the task is to construct an array arr[] of size N using numbers in the range [1, N] such that it has K sub-arrays whose all the elements are distinct.
Note: If there are multiple possible answers return any of them.
Examples:
Input: N = 5, K = 8
Output: {1, 2, 3, 3, 3}
Explanation: This array has the distinct sub-arrays as {1}, {2}, {3}, {3}, {3}, {1, 2}, {2, 3}, {1, 2, 3}
Input: N = 6, K = 21
Output: {1, 2, 3, 4, 5, 6}
Explanation: This array has the 21 distinct sub-arrays.
Approach: The idea to solve the problem is based on the following mathematical observation:
- N elements will definitely form N subarrays of size 1 having unique elements.
- Now the remaining task is to form array such that (N-K subarrays) of size more than 1 have all distinct elements.
- Also it is known number of subarrays of size more than 1 from X elements are (X*(X+1))/2 - X = X*(X-1)/2.
- If X elements are distinct all these subarrays have all distinct elements.
So to form the array there is a need for such X distinct elements such that X*(X-1)/2 = K-N.
So in each step, add a distinct element until the above condition is satisfied. After that repeat the last element till the array size becomes N (because if the last element is repeated it will not effect count of subarrays with all distinct elements).
Follow these steps to solve the problem:
- Decrement K by K - N because every integer contributes to a distinct subarray of size 1.
- Now Initialize a variable num = 0 to keep track of integers that are being added to the array and the number of subarrays formed.
- From K, decrement the number of distinct subarrays formed after adding (num + 1) to the new array. (till num <= K).
- Check if array size has reached N. If not add the num - K repeated times till the array fills.
Below is the implementation for the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to construct the array
// with k distinct subarrays
vector<int> construct_array(int n, int k)
{
// Each individual integers
// contributes to one subarray
k = k - n;
// Initialize a variable to keep
// track of integers that are
// adding to the array
int num = 0;
// Initialize the res to
// store the array
vector<int> res;
// Push as many possible distinct
// integers into the vector
while (k >= num) {
res.push_back(num + 1);
// Decrement the count of
// distinct subarrays incrementing
k -= num;
num++;
}
// If still it is not possible to
// get the array of size n then
// adding n-k at the end make num-k
// more distinct subarrays
while (res.size() < n) {
res.push_back(num - k);
}
// Return the array
return res;
}
// Driver code
int main()
{
int N = 5;
int K = 8;
// Function call
vector<int> ans = construct_array(N, K);
for (int x : ans)
cout << x << " ";
return 0;
}
Java
// JAVA program for the above approach
import java.util.*;
class GFG {
// Function to construct the array
// with k distinct subarrays
public static ArrayList<Integer> construct_array(int n,
int k)
{
// Each individual integers
// contributes to one subarray
k = k - n;
// Initialize a variable to keep
// track of integers that are
// adding to the array
int num = 0;
// Initialize the res to
// store the array
ArrayList<Integer> res = new ArrayList<Integer>();
// Push as many possible distinct
// integers into the vector
while (k >= num) {
res.add(num + 1);
// Decrement the count of
// distinct subarrays incrementing
k -= num;
num++;
}
// If still it is not possible to
// get the array of size n then
// adding n-k at the end make num-k
// more distinct subarrays
while (res.size() < n) {
res.add(num - k);
}
// Return the array
return res;
}
// Driver code
public static void main(String[] args)
{
int N = 5;
int K = 8;
// Function call
ArrayList<Integer> ans = construct_array(N, K);
for (int x = 0; x < ans.size(); x++)
System.out.print(ans.get(x) + " ");
}
}
// This code is contributed by Taranpreet
Python3
# Python3 code to implement the approach
# Function to construct the array
# with k distinct subarrays
def construct_array(n, k):
# Each individual integers
# contributes to one subarray
k -= n
# // Initialize a variable to keep
# track of integers that are
# adding to the array
num = 0
# Initialize the res to
# store the array
res = []
# Push as many possible distinct
# integers into the vector
while k >= num:
res.append(num + 1)
# Decrement the count of
# distinct subarrays incrementing
k -= num
num += 1
# If still it is not possible to
# get the array of size n then
# adding n-k at the end make num-k
# more distinct subarrays
while len(res) < n:
res.append(num - k)
return res
# Driver code
N = 5
K = 8
# function call
ans = construct_array(N, K)
print(" ".join(list(map(str, ans))))
# This code is contributed by phasing17
C#
// C# program for the above approach
using System;
using System.Collections;
public class GFG{
// Function to construct the array
// with k distinct subarrays
public static ArrayList construct_array(int n,
int k)
{
// Each individual integers
// contributes to one subarray
k = k - n;
// Initialize a variable to keep
// track of integers that are
// adding to the array
int num = 0;
// Initialize the res to
// store the array
ArrayList res = new ArrayList();
// Push as many possible distinct
// integers into the vector
while (k >= num) {
res.Add(num + 1);
// Decrement the count of
// distinct subarrays incrementing
k -= num;
num++;
}
// If still it is not possible to
// get the array of size n then
// adding n-k at the end make num-k
// more distinct subarrays
while (res.Count < n) {
res.Add(num - k);
}
// Return the array
return res;
}
// Driver code
static public void Main (){
int N = 5;
int K = 8;
// Function call
ArrayList ans = construct_array(N, K);
foreach(int x in ans)
Console.Write(x + " ");
}
}
// This code is contributed by hrithikgarg03188.
JavaScript
<script>
// JavaScript program for the above approach
// Function to construct the array
// with k distinct subarrays
const construct_array = (n, k) => {
// Each individual integers
// contributes to one subarray
k = k - n;
// Initialize a variable to keep
// track of integers that are
// adding to the array
let num = 0;
// Initialize the res to
// store the array
let res = [];
// Push as many possible distinct
// integers into the vector
while (k >= num) {
res.push(num + 1);
// Decrement the count of
// distinct subarrays incrementing
k -= num;
num++;
}
// If still it is not possible to
// get the array of size n then
// adding n-k at the end make num-k
// more distinct subarrays
while (res.length < n) {
res.push(num - k);
}
// Return the array
return res;
}
// Driver code
let N = 5;
let K = 8;
// Function call
let ans = construct_array(N, K);
for (let x in ans)
document.write(`${ans[x]} `);
// This code is contributed by rakeshsahni
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem