We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Strings
  4. Highest Value Palindrome

Highest Value Palindrome

Problem
Submissions
Leaderboard
Discussions
Editorial

Palindromes are strings that read the same from the left or right, for example madam or 0110.

You will be given a string representation of a number and a maximum number of changes you can make. Alter the string, one digit at a time, to create the string representation of the largest number possible given the limit to the number of changes. The length of the string may not be altered, so you must consider 's left of all higher digits in your tests. For example is valid, is not.

Given a string representing the starting number, and a maximum number of changes allowed, create the largest palindromic string of digits possible or the string '-1' if it is not possible to create a palindrome under the contstraints.

Example

Make replacements to get .


Make replacement to get .

Function Description

Complete the highestValuePalindrome function in the editor below.

highestValuePalindrome has the following parameter(s):

  • string s: a string representation of an integer
  • int n: the length of the integer string
  • int k: the maximum number of changes allowed

Returns

  • string: a string representation of the highest value achievable or -1

Input Format

The first line contains two space-separated integers, and , the number of digits in the number and the maximum number of changes allowed.
The second line contains an -digit string of numbers.

Constraints

  • Each character in the number is an integer where .

Output Format

Sample Input 0

STDIN   Function
-----   --------
4 1     n = 4, k = 1
3943    s = '3943'

Sample Output 0

3993

Sample Input 1

6 3
092282

Sample Output 1

992299

Sample Input 2

4 1
0011

Sample Output 2

-1

Explanation

Sample 0

There are two ways to make a palindrome by changing no more than digits:

Author

nabila_ahmed

Difficulty

Medium

Max Score

30

Submitted By

43037

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy