Check if it is possible form a permutation by dividing elements by K
Last Updated :
18 Apr, 2023
Given an array, arr[] of size N, and an integer K, the task is to check if it is possible to form a permutation from 1 to N, where you can replace arr[i] by arr[i] / K any number of times.
Examples:
Input: arr[] = [1, 8, 25, 2], K = 2
Output: Yes
Explanation:
- Replace 8 with ⌊8 / 2⌋ = 4, then arr = [1, 4, 25, 2].
- Replace 25 with ⌊25 / 2⌋ = 12, then arr =[1, 4, 12, 2].
- Replace 12 with ⌊12 / 2⌋ = 6, then arr = [1, 4, 6, 2].
- Replace 6 with ⌊6 / 2⌋ = 3, then arr = [1, 4, 3, 2].
Code blockInput: arr[] = [24, 7, 16, 7], K =2
Output: No
Explanation: It is not possible to form the permutation.
Approach: The problem can be solved based on the following idea:
This problem can be solved using a greedy approach. Declare a set that stores elements that range from 1 to n only. Start iterating from the array and as long as the current element is greater than n or has been already taken (present in the set), keep dividing it by K. After completing the operation on the current element push it into the set if and only if it is not equal to 0. After performing the operation on all indexes, check if the set size is equal to n or not, if yes return “Yes” or else “No“.
Follow the steps mentioned below to implement the idea:
- Declare a set that stores the elements that range from 1 to n
- Start iterating from the index 0 and keep dividing the current element by K as long as it is greater than K or it is already been taken in the set.
- Push the current element into the set if it is not 0
- Return true if set size == n
- Else false.
- Print Yes if the returned value is true else No
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool is_possible(vector< int >& arr, int n, int K)
{
set< int > st;
for ( int i = 0; i < n; i++) {
while (st.find(arr[i]) != st.end() || arr[i] > n) {
arr[i] = arr[i] / K;
}
if (arr[i] != 0)
st.insert(arr[i]);
}
if (st.size() == n)
return true ;
else
return false ;
}
int main()
{
vector< int > arr = { 1, 8, 25, 2 };
int n = arr.size();
int K = 2;
bool ans = is_possible(arr, n, K);
if (ans == true ) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
|
Java
import java.util.*;
public class Main {
public static boolean is_possible(List<Integer> arr, int n, int K) {
Set<Integer> st = new HashSet<>();
for ( int i = 0 ; i < n; i++) {
while (st.contains(arr.get(i)) || arr.get(i) > n) {
arr.set(i, arr.get(i) / K);
}
if (arr.get(i) != 0 ) {
st.add(arr.get(i));
}
}
if (st.size() == n) {
return true ;
}
else {
return false ;
}
}
public static void main(String[] args) {
List<Integer> arr = new ArrayList<>(Arrays.asList( 1 , 8 , 25 , 2 ));
int n = arr.size();
int K = 2 ;
boolean ans = is_possible(arr, n, K);
if (ans == true ) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
Python3
def is_possible(arr, n, K):
st = set ()
for i in range (n):
while arr[i] > n or arr[i] in st:
arr[i] / / = K
if arr[i] ! = 0 :
st.add(arr[i])
if len (st) = = n:
return True
else :
return False
arr = [ 1 , 8 , 25 , 2 ]
n = len (arr)
K = 2
ans = is_possible(arr, n, K)
if ans:
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class Program
{
static bool is_possible(List< int > arr, int n, int K)
{
HashSet< int > st = new HashSet< int >();
for ( int i = 0; i < n; i++)
{
while (st.Contains(arr[i]) || arr[i] > n)
{
arr[i] = arr[i] / K;
}
if (arr[i] != 0)
st.Add(arr[i]);
}
if (st.Count == n)
return true ;
else
return false ;
}
static void Main( string [] args)
{
List< int > arr = new List< int >() { 1, 8, 25, 2 };
int n = arr.Count;
int K = 2;
bool ans = is_possible(arr, n, K);
if (ans == true )
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
function is_possible(arr, n, K) {
let st = new Set();
for (let i = 0; i < n; i++) {
while (st.has(arr[i]) || arr[i] > n) {
arr[i] = Math.floor(arr[i] / K);
}
if (arr[i] !== 0) {
st.add(arr[i]);
}
}
if (st.size === n) {
return true ;
}
else {
return false ;
}
}
let arr = [1, 8, 25, 2];
let n = arr.length;
let K = 2;
let ans = is_possible(arr, n, K);
if (ans) {
console.log( "Yes" );
}
else {
console.log( "No" );
}
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...