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++
#include <bits/stdc++.h>
using namespace std;
vector< int > construct_array( int n, int k)
{
k = k - n;
int num = 0;
vector< int > res;
while (k >= num) {
res.push_back(num + 1);
k -= num;
num++;
}
while (res.size() < n) {
res.push_back(num - k);
}
return res;
}
int main()
{
int N = 5;
int K = 8;
vector< int > ans = construct_array(N, K);
for ( int x : ans)
cout << x << " " ;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static ArrayList<Integer> construct_array( int n,
int k)
{
k = k - n;
int num = 0 ;
ArrayList<Integer> res = new ArrayList<Integer>();
while (k >= num) {
res.add(num + 1 );
k -= num;
num++;
}
while (res.size() < n) {
res.add(num - k);
}
return res;
}
public static void main(String[] args)
{
int N = 5 ;
int K = 8 ;
ArrayList<Integer> ans = construct_array(N, K);
for ( int x = 0 ; x < ans.size(); x++)
System.out.print(ans.get(x) + " " );
}
}
|
Python3
def construct_array(n, k):
k - = n
num = 0
res = []
while k > = num:
res.append(num + 1 )
k - = num
num + = 1
while len (res) < n:
res.append(num - k)
return res
N = 5
K = 8
ans = construct_array(N, K)
print ( " " .join( list ( map ( str , ans))))
|
C#
using System;
using System.Collections;
public class GFG{
public static ArrayList construct_array( int n,
int k)
{
k = k - n;
int num = 0;
ArrayList res = new ArrayList();
while (k >= num) {
res.Add(num + 1);
k -= num;
num++;
}
while (res.Count < n) {
res.Add(num - k);
}
return res;
}
static public void Main (){
int N = 5;
int K = 8;
ArrayList ans = construct_array(N, K);
foreach ( int x in ans)
Console.Write(x + " " );
}
}
|
Javascript
<script>
const construct_array = (n, k) => {
k = k - n;
let num = 0;
let res = [];
while (k >= num) {
res.push(num + 1);
k -= num;
num++;
}
while (res.length < n) {
res.push(num - k);
}
return res;
}
let N = 5;
let K = 8;
let ans = construct_array(N, K);
for (let x in ans)
document.write(`${ans[x]} `);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...