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)
Similar Reads
Count Subarrays With At Most K Distinct Elements Given an array arr[] of integers and a positive integer k, the goal is to count the total number of subarrays that contain at most k distinct (unique) elements.Examples:Input: arr[] = [1, 2, 2, 3], k = 2 Output: 9Explanation: Subarrays with at most 2 distinct elements are: [1], [2], [2], [3], [1, 2]
9 min read
Count Subarrays With Exactly K Distinct Elements Given an array arr[] and an integer k, the task is to find the count of subarrays such that each subarray has exactly k distinct elements.Examples:Input: arr[] = [1, 2, 2, 3], k = 2 Output: 4 Explanation: Subarrays with exactly 2 distinct elements are: [1, 2], [1, 2, 2] and [2, 3].Input: arr[] = [3,
10 min read
Count subarrays having total distinct elements same as original array Given an array of n integers. Count the total number of sub-arrays having total distinct elements, the same as that of the total distinct elements of the original array. Examples: Input : arr[] = {2, 1, 3, 2, 3} Output : 5 Total distinct elements in array is 3 Total sub-arrays that satisfy the condi
11 min read
Construct a distinct elements array with given size, sum and element upper bound Given N, size of the original array, SUM total sum of all elements present in the array and K such that no element in array is greater than K, construct the original array where all elements in the array are unique. If there is no solution, print "Not Possible". Note: All elements should be positive
10 min read
Count array elements whose all distinct digits appear in K Given an array arr[] consisting of N positive integers and a positive integer K, the task is to find the count of array elements whose distinct digits are a subset of the digits of K. Examples: Input: arr[] = { 1, 12, 1222, 13, 2 }, K = 12Output: 4Explanation: Distinct Digits of K are { 1, 2 } Disti
15 min read
Count subarrays having a single distinct element that can be obtained from a given array Given an array arr[] of size N, the task is to count the number of subarrays consisting of a single distinct element that can be obtained from a given array. Examples: Input: N = 4, arr[] = { 2, 2, 2, 2 }Output: 7Explanation: All such subarrays {{2}, {2}, {2}, {2}, {2, 2}, {2, 2, 2}, {2, 2, 2, 2}}.
5 min read