The Wayback Machine - https://web.archive.org/web/20220313043802/https://www.geeksforgeeks.org/find-k-to-convert-given-array-into-permutation-by-performing-modulo-by-k/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Find K to convert given Array into permutation by performing modulo by K

  • Difficulty Level : Hard
  • Last Updated : 11 Mar, 2022

Given an array arr[] of N integers, the task is to find a value K such that the operation arr[ i ] = arr[ i ] % K (0 ≤ i ≤ N-1), makes the array a permutation. If no such K exists print -1.

A sequence of N integers is called permutation if it contains all integers from 1 to N exactly once.

Examples:  

Input: N = 3, arr[ ] = {2, 7, 1}
Output: 4
Explanation: [2%4, 7%4, 1%4] = [2, 3, 1] which is a permutation of N = 3.

Input: N = 5, arr[ ] = {18, 81, 29, 36, 11}
Output: 8
Explanation: [18%8, 81%8, 29%8, 36%8, 11%8] = [2, 1, 5, 4, 3] which is a permutation of N = 5.

Input: N = 2, arr[ ] = {2, 2}
Output: -1

 

Approach: The given problem can be solved with the help of the following observation:

If the given arr[] has at least one repeated element, it can never become a permutation. So, all arr[i] must be unique in arr[]. If not, then print -1.

If there exists such K. So, we express each arr[i] in terms of K as given:

  • A1 = Q1*K + R1
    A2 = Q2*K + R2
    A3 = Q3*K + R3
    . . . . . . 
    AN = QN*K + RN
  • To be a valid K, (R1, R2, R3 …. RN) must be a permutation from 1 to N. 
  • Hence, Σ arr[i] = (Σ Qi) *K + (Σ Ri). 
    • We know (Σ Ri) = (N*(N+1))/2 = Sr (Say).
    • let S = (Q’) *K +Sr, where Q’ = (Σ Qi).
    • let Srem = (Q’) *K = S – Sr = (sum of all array elements) – (N*(N+1))/2.
  • It is evident that K must a divisor of Srem.

Follow the steps mentioned below to solve the problem:

  • Check If arr[] is already a permutation, then a possible answer will simply be (N+1). 
  • Otherwise, find the sum of all the array elements.
  • Find all divisors of Srem and check which of the divisors satisfies the condition that it makes the array permutation upon applying the given modulo operation. 

Below is the implementation of the above approach.

C++




// C++ code to implement above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to check is given set a
// permutation
bool is_given_set_permutation(set<int>&
                                  hash,
                              int N)
{
    int last_element = *hash.rbegin();
    if (hash.size() == N
        && last_element == N) {
        return true;
    }
    return false;
}
  
// Function to find the divisors
vector<int> findDivisors(int n)
{
    // Note that this loop runs till
    // square root
    vector<int> divisors;
  
    for (int i = 1; i <= sqrt(n); i++) {
        if (n % i == 0) {
            // If divisors are equal,
            // push only one
            if (n / i == i)
                divisors.push_back(i);
  
            // Otherwise push both
            else {
                divisors.push_back(i);
                divisors.push_back(n / i);
            }
        }
    }
    return divisors;
}
  
// Function returns if the current divisor
// is the required answer
bool is_current_divisor_answer(int arr[],
                               int N, int div)
{
    set<int> hash;
    for (int i{ 0 }; i < N; i++) {
        hash.insert(arr[i] % div);
    }
    return is_given_set_permutation(hash, N);
}
  
// Function to the number d which on applying
// arr[i]%d to each element makes the array
// a permuatation
int number_to_make_permuation(int arr[], int N)
{
    // Set to store elements of arr[]
    set<int> hash;
  
    for (int i{ 0 }; i < N; i++) {
        hash.insert(arr[i]);
    }
  
    if (hash.size() != N) {
  
        // When hash size !=N, there are
        // repeating elements in arr[].
        // So it can never be a permutation
        return -1;
    }
  
    if (is_given_set_permutation(hash, N)) {
  
        // Given arr[] is already a
        // permutation
        return (N + 1);
    }
    int total_sum{ 0 };
  
    // Loop to find the sum of the array
    for (int i{ 0 }; i < N; i++) {
        total_sum += arr[i];
    }
  
    int sum_of_permutation
        = (N * (N + 1)) / 2;
    int remaining_sum = total_sum - sum_of_permutation;
    vector<int> divisors = findDivisors(
        remaining_sum);
  
    // Loop to check if any divisor
    // satisfies the condition
    for (int i{ 0 }; i < divisors.size();
         i++) {
        // A divisor <= N can
        // never be an answer
        if (divisors[i] <= N)
            continue;
        if (is_current_divisor_answer(arr, N,
                                      divisors[i])) {
            return divisors[i];
        }
    }
    return -1;
}
  
// Driver code
int main()
{
    int arr[] = { 18, 81, 29, 36, 11 };
    int N = sizeof(arr) / sizeof(int);
  
    // Function call
    cout << number_to_make_permuation(arr, N);
    return 0;
}
Output
8

Time Complexity: O(N * F), where F is the maximum number of divisors.
Auxiliary Space: O(N)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!