ADDPERM - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: raysh07
Tester: sushil2006
Editorialist: iceknight1093

DIFFICULTY:

Simple

PREREQUISITES:

None

PROBLEM:

Given N and K, find any permutation P of \{1, 2, \ldots, N\} such that there are exactly K distinct values of P_i + i across all i.

EXPLANATION:

If you play around with a few permutations, you might notice the following extremes:

  • P = [1, 2, 3, \ldots, N] results in the P_i + i values being 2, 4, 6, \ldots, 2N, all of which are distinct.
    This gets us N distinct values.
  • P = [N, N-1, N-2, \ldots, 2, 1] results in all the P_i+i values being N+1.
    This gets us 1 distinct value.

Using these, we’re able to solve K = 1 and K = N.
Let’s try to combine them to solve for all K in the middle.

Suppose we choose an integer x, then use the second (descending) pattern till x and the first (ascending) pattern afterwards, i.e.

[x, x-1, x-2, \ldots, 3, 2, 1, x+1, x+2, x+3, \ldots, N]

Then, it can be seen that:

  • For 1 \leq i \leq x, the value of P_i + i is always x+1.
  • For i \gt x, the value of P_i + i is 2i.

2i cannot equal x+1 for i \gt x, so the total number of distinct values is 1 + (N-x), because the first x positions give us a single distinct value, and then each index after that gives us a new one.

We want K distinct values overall, meaning we want 1 + (N-x) = K, which occurs with x = N+1-K.

So, using the above construction with this value of K gives us a valid solution, i.e. one possible answer is

[N+1-K, N-K, \ldots, 2, 1, N+2-K, N+3-K, \ldots, N]

TIME COMPLEXITY:

\mathcal{O}(N) per testcase.

CODE:

Editorialist's code (PyPy3)
for _ in range(int(input())):
    n, k = map(int, input().split())
    x = n+1-k
    ans = list(reversed(range(1, x+1))) + list(range(x+1, n+1))
    print(*ans)
2 Likes

Hey iceknight1093,
Today I got a mail that my solution matches with 2-3 people. I mean this is one of the most basic thing that we learn during our secondary education. I wouldn’t cheat in such a problem brother.

I won’t deny that I don’t use AI tools but what’s the point when I’m copying other’s solutions? If I had to copy from other, why would be still in 2* from 4-5 months.

You guys wrote that my solution to this problem matches 2 more people, can’t 2-3 people can think about the same logic? Can’t the AI tools show us the same logic or code?

I really want you guys to look in this matter as this profile is everything to me and I’ve worked hard to get into 2*.

Hey iceknight1093,
Today I received a mail that my solution matches with other 2-3 people. I have just started giving contests and what’s the basic i think i have wrote the code upto my knowledge. Infact i didn’t have used AI tools.
You guys wrote that my solution to this problem matches 2 more people, can’t 2-3 people can think about the same logic? Can’t the AI tools show the same logic or code?

Please look into this matter as this profile is everything to me and i am working hard to build my profile.

LOL.
Why would you copy my reply on a topic that involves “flagged for copying” lmao

Bro I got the same problem :smiling_face_with_tear:
Change your thought noone has copied you :joy:
Using same words doesn’t mean copying :joy: