Monday 17 July 2017

Richie rich problem



Sandy likes palindromes. A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as it does forward. For example, madam is a palindrome.
On her birthday, Sandy's uncle, Richie Rich, offered her an -digit check which she refused because the number was not a palindrome. Richie then challenged Sandy to make the number palindromic by changing no more than digits. Sandy can only change digit at a time, and cannot add digits to (or remove digits from) the number.
Given and an -digit number, help Sandy determine the largest possible number she can make by changing digits.
Note: Treat the integers as numeric strings. Leading zeros are permitted and can't be ignored (So 0011 is not a palindrome, 0110 is a valid palindrome). A digit can be modified more than once.
Input Format
The first line contains two space-separated integers, (the number of digits in the number) and (the maximum number of digits that can be altered), respectively.
The second line contains an -digit string of numbers that Sandy must attempt to make palindromic.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

char* richieRich(char* s, int n, int k){
    // Complete this function
    int temp1= 0;
    int temp2= n-1;
    int a[n];
    for (int arr_i=0;arr_i<n;arr_i++)
    {
        a[arr_i]=0;
    }
    while(temp1<temp2)
    {
        if (s[temp1] != s[temp2])
        {
               
                k--;
                if (k<0)
                {
                    char *m="-1";
                    return m;
                }
               if (s[temp1]>s[temp2])
               {
                   s[temp2]=s[temp1];
                   a[temp2]=1;
               }
               else
               {
                   s[temp1]=s[temp2];
                   a[temp1]=1;
               }
        }
        temp1++;
        temp2--;
    }
    if (k>0)
    {
        for (int arr_i=0;arr_i<=(n/2);arr_i++)
        {
            if (s[arr_i]=='9')
            { 
                continue;
            }
            else 
            {
                if ((a[arr_i]==1)||a[n-1-arr_i]==1)
                {
                    k=k-1;
                    if (k<0)
                    { k=0;
                        break;}
                }
                else
                {
                    k=k-2;
                    if (k<0)
                    {    k=k+2;
                    break;}
                }
           
                s[arr_i]='9';
                s[n-1-arr_i]='9';
            }
        }
      
           
     }
    if ((k==1)&& (n%2!=0))    /*can also combine this in above loop when arr_i
==n/2 or temp1==temp2 means accessing from starting and previous becomes same and decrement it by k=1 */
        {
            s[n/2]='9';
        }
  
    return s ;
}


int main() {
    int n;
    scanf("%i", &n);
    int k;
    scanf("%i", &k);
    char* s = (char *)malloc(512000 * sizeof(char));
    scanf("%s", s);
    int result_size;
    char* result = richieRich(s, n, k);
    printf("%s\n", result);
    return 0;
}




No comments:

Post a Comment