Minimize the value of Z/K
Last Updated :
22 Mar, 2023
Given two positive integers N (N>1) and K, the task is to output such permutation and the minimum value of Z/K, considering the below conditions:
- A[ i ] ≠i for (1 <= i <= N)
- Value of Z/K is minimized, Where Z = ∑A[ i ] % i for (1 <= i <= N)
Examples:
Input: N = 2, K = 1
Output: Permutation: 2 1
Minimum Z/K = 1
Explanation: The permutation of size 2 is [2, 1] where no element is equal to its index, Which gives Z as ( 2 % 1) + (1 % 2) = 0 + 1 = 1. The value of Z/K = will be 1, Which is the minimum possible.
Input: N = 3, K = 5
Output: Permutation: 2 3 1
Minimum Z/K = 0
Explanation: It can be verified that the value of Z/K is the minimum possible, and the permutation follows all the given conditions.
Approach: Follow the idea given below to solve the problem
The problem is observation based and can be solved by using those observations. For more clarification see the Concept of approach section.
Concept of approach:
We have to minimize the value of Z/K, Where Z = ∑A[ i ] % i for (1 <= i <= N). Value Z/K will minimized only and only if Z is the minimum possible. For making Z a possible minimum, The permutation can be created by following below steps:
- Create an array of N elements from 1 to N.
- Left rotate the whole array once in a circular manner by assuming that the array is circularly connected.
- After this operation, there will no index in permutation such that A[ i ] == i, and the value of Z will also be minimized.
Example:
N = 5, Array = {1, 2, 3, 4, 5}, Left rotated Array = {2, 3, 4, 5, 1}
Value of Z for array = (A[ 1 ] % 1 ) + (A[ 2 ] % 2 ) + (A[ 3 ] % 3 ) + (A[ 4 ] % 4 ) + (A[ 5 ] % 5 ) = (2 % 1) + (3 % 2) + (4 % 3) + (5%4) + (1 % 5) = 0+1+1+1+1 = 4. Which is the minimum possible value of Z. Then just output the value of Z/K along with the permutation. Formally, for any value of N (N>1) the minimum value of Z/K will be ( N – 1 )/K. Because the steps described for creating permutation will always give the value of Z = N-1, Which is the minimum possible.
Steps were taken to solve the problem:
- For creating permutation:
- Run a loop from i = 2 to i<=N and print the value of i for each iteration.
- Print 1 after executing the loop successfully.
- For the minimum value of Z/K:
- Print the value of (N-1)/K.
Below is the code to implement the approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minZ( int N, int K)
{
for ( int i = 2; i <= N; i++) {
cout << i << " " ;
}
cout << 1 << endl;
cout<< (N - 1) / K << endl;
}
int main() {
int N = 3;
int K = 5;
minZ(N, K);
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String[] args)
throws java.lang.Exception
{
int N = 3 ;
int K = 5 ;
minZ(N, K);
}
static void minZ( int N, int K)
{
for ( int i = 2 ; i <= N; i++) {
System.out.print(i + " " );
}
System.out.println( 1 );
System.out.println((N - 1 ) / K);
}
}
|
Python3
def minZ(N, K):
for i in range ( 2 , N + 1 ):
print (i, end = " " )
print ( 1 )
print ((N - 1 ) / / K)
if __name__ = = "__main__" :
N = 3
K = 5
minZ(N, K)
|
C#
using System;
public class GFG{
static void minZ( int N, int K)
{
for ( int i = 2; i <= N; i++) {
Console.Write(i + " " );
}
Console.WriteLine(1);
Console.WriteLine((N - 1) / K);
}
static public void Main (){
int N = 3;
int K = 5;
minZ(N, K);
}
}
|
Javascript
function minZ(N, K){
let permutation = "" ;
for (let i = 2; i <= N; i++) {
permutation += i + " " ;
}
permutation += 1;
console.log(permutation);
console.log(Math.floor((N - 1) / K));
}
let N = 3;
let K = 5;
minZ(N, K);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...