So, based on this problem, let's find out what is the longest palindrome for the case when K.length == 1. This case can be safely ignored, since there is no value greater than K, which is the palindrome K. The same applies to K.length == 2. Therefore, the pseudo-code for the evaluation is as follows:
if K.length <= 2 pass
When K.length == 3 values ββthat we need are K [0] and K [2], this gives us boundaries. For example, K == 100. the values ββwe care about are 1 and 0. If K [0] is greater than K [2], we know that we must do K [2] = K [0], and we are done. Another example is K == 200, the first value is greater than 202, which is the first simple, equal. If K [0] == K [2] and K <999, we increase K [1] by 1, and we are done. Pseudocode as follows:
if K[0] > K[2] K[2] = K[0] if K[0] == K[2] and K < 999 K[1]++
If all values ββin K are 9 (999,9999, etc.), increment K by 2 and end the process. I will leave you the general form of the algorithm if you are not ultimately stuck.
source share