# Highest Value Palindrome

### Problem Statement :

```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 0's left of all higher digits in your tests. For example 0110 is valid,  0011 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.

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, n and k, the number of digits in the number and the maximum number of changes allowed.
The second line contains an n-digit string of numbers.

Constraints

0  <=  n  <=  10^5
0  <=  k  <=  10^5
Each character i in the number is an integer where 0  <=  i  <=  9.```

### Solution :

```                            ```Solution in C :

In  C++ :

#include <bits/stdc++.h>

using namespace std;

int N, K;
int A[100000];
int B[100000];

int main()
{
scanf("%d%d", &N, &K);
char c;
for(int i=0; i<N; i++)
{
scanf(" %c", &c);
A[i]=c-'0';
}
for(int i=0; i<N-i-1; i++) if(A[i]!=A[N-i-1])
{
if(A[i]>A[N-i-1])
A[N-i-1]=A[i], B[N-i-1]=1;
else
A[i]=A[N-i-1], B[i]=1;
K--;
}
if(K<0)
printf("-1\n");
else
{
for(int i=0; i<=N-i-1; i++) if(A[i]!=9)
{
int cost=2;
if(i==N-i-1)
cost=1;
if(B[i])
cost--;
if(i!=N-i-1 && B[N-i-1])
cost--;
if(K>=cost)
{
K-=cost;
A[i]=A[N-i-1]=9;
}
}
for(int i=0; i<N; i++)
printf("%d", A[i]);
printf("\n");
}
return 0;
}

In  Java  :

import java.util.Scanner;

public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();
int k = scanner.nextInt();
char[] c = scanner.next().toCharArray();
boolean[] ch = new boolean[n];
for (int i = 0; i < n/2; ++i) {
if (c[i] != c[n - i - 1]) {
c[i] = c[n - i - 1] = (char)Math.max(c[i], c[n - i - 1]);
ch[i] = true;
--k;
}
}
if (k < 0) {
System.out.println(-1);
return;
}
for (int i = 0; i < n/2; ++i) {
if (c[i] != '9') {
if (ch[i] && k > 0) {
c[i] = c[n - i - 1] = '9';
--k;
}
if (!ch[i] && k > 1) {
c[i] = c[n - i - 1] = '9';
k -= 2;
}
}
}
if (n % 2 == 1 && k > 0) {
c[n/2] = '9';
}
System.out.println(new String(c));
}
}

In   C  :

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
int n,k,i,l,r;
scanf("%d %d",&n,&k);
char str[n+1];
char hash[n];
scanf("%s",str);
for(i=0;i<n;i++){
hash[i] = '0';
}
l=0,r=n-1;
while(l<r){
if(str[l]!=str[r]){
if(k==0){
printf("-1");
return 0;
}
if(str[l]<str[r]){
str[l] = str[r];
hash[l] = '1';
}
else{
str[r] = str[l];
hash[r] = '1';
}
k--;
}
l++;
r--;
}
l=0,r=n-1;
while(l<r && k>0){
if(str[l]!='9'){
if(hash[l]=='1'||hash[r]=='1'){
str[l] = str[r] = '9';
k--;
}
else if(k>1){
str[l] = str[r] = '9';
k-=2;
}
}
l++;
r--;
}
if(l==r && k>0)
str[l] = '9';
printf("%s",str);
return 0;
}

In   Python3 :

l, k = input().split(' ')
l = int(l)
k = int(k)
n = list(input())
midl = int(len(n)/2)

diff = 0
for i in range(0,midl):
if (n[i] != n[l-1-i]):
diff += 1

if (diff > k):
print(-1)
exit(0)

more = k - diff

count = 0
for i in range(0,midl):
if (n[i] == n[l-1-i]):
if (n[i] != '9' and more >= 2):
n[i], n[l-1-i] = '9', '9'
more -= 2
continue
maxn = n[i] if (n[i] > n[l-1-i]) else n[l-1-i]
if (maxn != '9' and more >= 1):
more -= 1
maxn = '9'
n[i], n[l-1-i] = maxn, maxn
#count += 1

if (more > 0):
n[midl] = '9'

print("".join(n))```
```

## Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

## Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

## Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

## Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

## Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

## Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan