Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.

book
Number Permutation

Bob has an array of n elements, where each element is an integer between 1 and n. He has recently learned about permutations and their lexicographical order. Now, he wants to modify (replace) the fewest number of elements in his array to transform it into a valid permutation, where every integer from 1 to n appears exactly once. If there are multiple ways to achieve this, Bob prefers the lexicographically smallest permutation.

To summarize, his goal is to:

  • Minimize the number of changes needed to turn the array into a valid permutation.
  • Among the possible valid permutations, choose the lexicographically smallest one.

When comparing two permutations to determine which is lexicographically smaller, we first compare their first elements. If those are equal, we compare the second elements, and so on. A permutation x is considered lexicographically smaller than permutation y if, at the first position where they differ, the element in x is smaller than in y.

Return an array of the lexicographically minimal permutation which can be obtained from array with minimum changes

Example 1

Input:

[3, 2, 2, 3]

Output:

[1, 2, 4, 3]
main.cpp

main.cpp

C++

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt