Блог пользователя PanPey

Автор PanPey, история, 3 года назад, По-английски

Given a positive integer N and an array A of size N such that 1 <= Ai <= N. Our aim is to find whether it can be made permutable. i.e each number from 1 to N appears in A exactly once. We perform an operation to do so. In an operation, we can choose any integer and increase its value. Find the minimum number of changes to make the array permutable and print it. If the array is already permutable print 0, -1 if it's not possible.

Consider 3 test cases. First-line contains N. Next line contains an array of size N.

Sample Input
 3 
 4
 1 3 3 4
 5
 1 2 3 5 4
 5
 2 3 2 1 3

Sample Output
 -1
 0
 2 
  • Проголосовать: нравится
  • +9
  • Проголосовать: не нравится

»
3 года назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

not sure though....1st sort the array...And then make last element n,previous element n-1,....maybe this will need minimum operation....but if you find that sorted array[i]>i (1 based indexing) you will print -1..

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

These noobies can't even solve basic questions