Open In App

Check if it is possible form a permutation by dividing elements by K

Last Updated : 18 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code of the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function for checking if it is
// possible to have a permutation
bool is_possible(vector<int>& arr, int n, int K)
{
 
    // Declare set which stores elements
    // that ranges from 1 to n only.
    set<int> st;
    for (int i = 0; i < n; i++) {
 
        // As long as the value is greater
        // than n or it has been already
        // taken, divide it by K
        while (st.find(arr[i]) != st.end() || arr[i] > n) {
            arr[i] = arr[i] / K;
        }
 
        // If the value is not equal to 0,
        // that means it ranges from
        // 1 to n, insert it into set
        if (arr[i] != 0)
            st.insert(arr[i]);
    }
 
    // If set size is equal to n,
    // retrun true.
    if (st.size() == n)
        return true;
 
    // else false
    else
        return false;
}
 
// Driver code
int main()
{
    vector<int> arr = { 1, 8, 25, 2 };
    int n = arr.size();
    int K = 2;
 
    // Function call
    bool ans = is_possible(arr, n, K);
 
    if (ans == true) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
}


Java




import java.util.*;
 
public class Main {
     
    // Function for checking if it is possible to have a permutation
    public static boolean is_possible(List<Integer> arr, int n, int K) {
         
        // Declare set which stores elements
        // that ranges from 1 to n only.
        Set<Integer> st = new HashSet<>();
         
        for (int i = 0; i < n; i++) {
            // As long as the value is greater than n or it has been already taken, divide it by K
            while (st.contains(arr.get(i)) || arr.get(i) > n) {
                arr.set(i, arr.get(i) / K);
            }
            // If the value is not equal to 0, that means it ranges from 1 to n, insert it into set
            if (arr.get(i) != 0) {
                st.add(arr.get(i));
            }
        }
         
        // If set size is equal to n, return true.
        if (st.size() == n) {
            return true;
        }
        // else false
        else {
            return false;
        }
    }
     
    // Driver code
    public static void main(String[] args) {
        List<Integer> arr = new ArrayList<>(Arrays.asList(1, 8, 25, 2));
        int n = arr.size();
        int K = 2;
 
        // Function call
        boolean ans = is_possible(arr, n, K);
 
        if (ans == true) {
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }
    }
}


Python3




# Python code of the above approach
 
# Function for checking if it is
# possible to have a permutation
 
 
def is_possible(arr, n, K):
 
    # Declare set which stores elements
    # that ranges from 1 to n only.
    st = set()
    for i in range(n):
 
        # As long as the value is greater
        # than n or it has been already
        # taken, divide it by K
        while arr[i] > n or arr[i] in st:
            arr[i] //= K
 
        # If the value is not equal to 0,
        # that means it ranges from
        # 1 to n, insert it into set
        if arr[i] != 0:
            st.add(arr[i])
 
    # If set size is equal to n,
    # return True.
    if len(st) == n:
        return True
 
    # else False
    else:
        return False
 
 
# Driver code
arr = [1, 8, 25, 2]
n = len(arr)
K = 2
 
# Function call
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)
    {
        // Declare set which stores elements
        // that ranges from 1 to n only.
        HashSet<int> st = new HashSet<int>();
 
        for (int i = 0; i < n; i++)
        {
            // As long as the value is greater
            // than n or it has been already
            // taken, divide it by K
            while (st.Contains(arr[i]) || arr[i] > n)
            {
                arr[i] = arr[i] / K;
            }
 
            // If the value is not equal to 0,
            // that means it ranges from
            // 1 to n, insert it into set
            if (arr[i] != 0)
                st.Add(arr[i]);
        }
 
        // If set size is equal to n,
        // return true.
        if (st.Count == n)
            return true;
 
        // else false
        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;
 
        // Function call
        bool ans = is_possible(arr, n, K);
 
        if (ans == true)
        {
            Console.WriteLine("Yes");
        }
        else
        {
            Console.WriteLine("No");
        }
    }
}
// This code is contributed by Prajwal Kandekar


Javascript




// JavaScript code of the above approach
 
// Function for checking if it is
// possible to have a permutation
function is_possible(arr, n, K) {
 
    // Declare set which stores elements
    // that ranges from 1 to n only.
    let st = new Set();
    for (let i = 0; i < n; i++) {
 
        // As long as the value is greater
        // than n or it has been already
        // taken, divide it by K
        while (st.has(arr[i]) || arr[i] > n) {
            arr[i] = Math.floor(arr[i] / K);
        }
 
        // If the value is not equal to 0,
        // that means it ranges from
        // 1 to n, insert it into set
        if (arr[i] !== 0) {
            st.add(arr[i]);
        }
    }
 
    // If set size is equal to n,
    // return true.
    if (st.size === n) {
        return true;
    }
 
    // else false
    else {
        return false;
    }
}
 
// Driver code
let arr = [1, 8, 25, 2];
let n = arr.length;
let K = 2;
 
// Function call
let ans = is_possible(arr, n, K);
 
if (ans) {
    console.log("Yes");
}
else {
    console.log("No");
}
 
// This code is contributed by chetan bargal


Output

Yes

Time Complexity: O(N)
Auxiliary Space: O(N)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads