PROBLEM LINK:
Contest Division 1
Contest Division 2
Contest Division 3
Contest Division 4
Setter: Kanhaiya Mohan
Tester: Nishank Suresh, Utkarsh Gupta
Editorialist: Pratiyush Mishra
DIFFICULTY:
To be Calculated
PREREQUISITES:
None
PROBLEM:
Given integers N and X, generate a palindrome of length N consisting of lowercase English alphabets such that the number of distinct characters in the palindrome is exactly X.
If it is impossible to do so, print -1 instead.
EXPLANATION:
Lets tackle this problem case by case:
- Case 1: When N = 1:
Here the answer can be any character, example ‘a’. - Case 2: When N\lt(2 \times X) - 1:
Here it won’t be possible to construct the required string, since for X distinct characters we need to use at least (2 \times X - 1) characters. - Case 3: When N = (2 \times X) -1:
Assuming s to be a string of length X having all distinct characters, we can construct the following string as our answer:s_1s_2...s_{x-1}s_xs_{x-1}s_{x-2}....s_1 - Case 4: When N \gt (2 \times X) - 1:
Assuming s to be a string of length X having all distinct characters, we can have three parts as:left = s \\ mid = A \;string \;of \;length \;(N - 2 \times X) \; having\; all\; characters\; as\; s_1 \\ right = reverse(s)Thus our final answer for this case would then be:ans = left + mid + right
TIME COMPLEXITY:
O(N), for each test case.
SOLUTION:
Editorialist’s Solution
Setter’s Solution
Tester1’s Solution
Tester2’s Solution