Construct an Array of length N containing exactly K elements divisible by their positions
Last Updated :
04 Apr, 2022
Given two integers N and K, the task is to construct an array of length N containing exactly K elements divisible by their positions.
Examples:
Input: N = 6, K = 2
Output: {5, 1, 2, 3, 4, 6}
Explanation: Considering the above array:
At Position 1, element 5 is divisible by 1
At Position 2, element 1 is not divisible by 2
At Position 3, element 2 is not divisible by 3
At Position 4, element 3 is not divisible by 4
At Position 5, element 4 is not divisible by 5
At Position 6, element 6 is divisible by 6
Therefore, there are exactly K elements in array divisible by their positions, meeting the required criteria.
Hence the resultant array will be {5 1 2 3 4 6}.
Input: N = 5, K = 5
Output: {1, 2, 3, 4, 5}
Approach: The problem can be solved easily using Greedy approach based on below observations:
For any integer X, we know that:
- X will be divisible by 1 and X always.
- No integer greater than X will ever be able to divide X.
So using these observations, we can construct the array containing exactly K elements divisible by their positions, as follows:
- For position 1, place any element greater than 1, because 1 will divide all integers
- For positions greater than 1, choose K-1 positions, and place them in the array at corresponding indices.
- The remaining N-K positions can be placed at any other position to match the required criteria.
Illustrations:
Consider an example: N = 6, K = 5
The empty array of size 6 will be:
arr[]: _ _ _ _ _ _
positions: 1 2 3 4 5 6
Step 1: Fill position 1 with any integer greater than 1
- For 1st value equal to its position, we have 2 options – to insert 1 at 1, and to insert some integer greater than 1 at 1. If we insert 1 at 1, there will be a case when we cannot have K=5 values same as their positions. So we will insert some other value greater than 1 at position 1 (say 5):
- arr[]: 5 _ _ _ _ _
positions: 1 2 3 4 5 6
Step 2: Fill K-1 (=4) positions at corresponding indices
- For 2nd value equal to its position:
- arr[]: 5 2 _ _ _ _
positions: 1 2 3 4 5 6
- For 3rd value equal to its position:
- arr[]: 5 2 3 _ _ _
positions: 1 2 3 4 5 6
- For 4th value equal to its position:
- arr[]: 5 2 3 4 _ _
positions: 1 2 3 4 5 6
- For 5th value equal to its position, we cannot insert 5 at position 5, as it is already used at position 1. So we will insert 1 at position 5, and 6 at position 6:
- arr[]: 5 2 3 4 1 6
positions: 1 2 3 4 5 6
Therefore the final array will be: 5 2 3 4 1 6
Follow the steps below to implement the above approach:
- Create an array of N consecutive positive integers from 1 to N.
- After the index N-K, there will be K-1 elements left, we will not interfere with these elements. So, we have K-1 elements, which are divisible by their position.
- We will make First element of the array equal to the element at index N-K. This would also be divisible by its position.
- We will make the remaining elements (i.e. from index 1 to N-K) equal to the elements immediate left to them. These all N-K elements will not be divisible by their position then and remaining K elements would be divisible by their position.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > constructArray( int N, int K)
{
vector< int > A(N, 0);
for ( int i = 0; i < N; i++) {
A[i] = i + 1;
}
int target = N - K;
int prev = A[0];
A[0] = A[target];
for ( int i = 1; i <= target; i++) {
int temp = A[i];
A[i] = prev;
prev = temp;
}
return A;
}
int main()
{
int N = 6, K = 2;
vector< int > A = constructArray(N, K);
for ( int i = 0; i < N; i++)
cout << A[i] << " " ;
cout << endl;
}
|
Java
import java.util.*;
class GFG
{
public static int [] constructArray( int N, int K)
{
int A[] = new int [N];
for ( int i = 0 ; i < A.length; ++i) {
A[i] = 0 ;
}
for ( int i = 0 ; i < N; i++) {
A[i] = i + 1 ;
}
int target = N - K;
int prev = A[ 0 ];
A[ 0 ] = A[target];
for ( int i = 1 ; i <= target; i++) {
int temp = A[i];
A[i] = prev;
prev = temp;
}
return A;
}
public static void main(String[] args)
{
int N = 6 , K = 2 ;
int A[] = constructArray(N, K);
for ( int i = 0 ; i < N; i++)
System.out.print(A[i] + " " );
System.out.println();
}
}
|
Python3
def constructArray(N, K):
A = [ 0 ] * N
for i in range (N):
A[i] = i + 1
target = N - K
prev = A[ 0 ]
A[ 0 ] = A[target]
for i in range ( 1 ,target + 1 ):
temp = A[i]
A[i] = prev
prev = temp
return A
N = 6
K = 2
A = constructArray(N, K)
for i in range (N):
print (A[i],end = " " )
|
C#
using System;
class GFG {
static int [] constructArray( int N, int K)
{
int [] A = new int [N];
for ( int i = 0; i < A.Length; ++i) {
A[i] = 0;
}
for ( int i = 0; i < N; i++) {
A[i] = i + 1;
}
int target = N - K;
int prev = A[0];
A[0] = A[target];
for ( int i = 1; i <= target; i++) {
int temp = A[i];
A[i] = prev;
prev = temp;
}
return A;
}
public static void Main()
{
int N = 6, K = 2;
int [] A = constructArray(N, K);
for ( int i = 0; i < N; i++)
Console.Write(A[i] + " " );
Console.WriteLine();
}
}
|
Javascript
<script>
const constructArray = (N, K) => {
let A = new Array(N).fill(0);
for (let i = 0; i < N; i++) {
A[i] = i + 1;
}
let target = N - K;
let prev = A[0];
A[0] = A[target];
for (let i = 1; i <= target; i++) {
let temp = A[i];
A[i] = prev;
prev = temp;
}
return A;
}
let N = 6, K = 2;
let A = constructArray(N, K);
for (let i = 0; i < N; i++)
document.write(`${A[i]} `);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...