Given an array of
positive integers
,we would like to convert it to a palindrome. To convert it to a palindrome we are only allowed to combine adjacent pairs (add them together). For example for the array
[3, 5, 2, 1]
we combine 2 and 1 to get
[3, 5, 3]
which is a palindrome.
For the array in the text file, what is the minimum number of combinations required to convert it to a palindrome?
Explicit examples
:
[1, 2, 3, 2, 1]
: 0 combination.
[1, 2, 5, 7]
: 3 combinations.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Why did you check for numbers at the front and back? Why not start merging from somewhere in the middle?
Log in to reply
Good question. First, noticed that any string of numbers can always be transformed into a palindrome after some merging (worst case just merge till one number left). Secondly, a palindrome S is defined recursively such that after the removal of the first and last character, the substring remains a palindrome.
With this two properties, we can only consider the head and tails. After they match, we deal with the substring.
Log in to reply
On your second statement, if you start merging from somewhere in between the middle you get a palindrome as well? Example : 1,2,3, 1,2, 2,1?
Log in to reply
@Siva Bathula – Technically yes. For your example it will be merging the 4th and 5th number to achieve a palindrome.
1 |
|
However for larger cases, this is not a very good approach. This is because you don't know where is the "middle" / "core" of the palindrome should be. If your example changed to
1 2 3 1 2 5 1 1 4 1
then we should merge the 5th and 6th, 2nd and 3rd, 8th and 9th numbers. Of course, if you know that the middle of the transformed palindrome is the merged result of 5th and 6th number, then you can loop till the end (head and tails) of the string.
Log in to reply
@Christopher Boo – Yep, so the end solution will always be different? The end palindrome as well as the number of steps to reach that palindrome both are different. So the problem is not about the approach, but the least number of combinations needed to convert it to a palindrome?
Log in to reply
@Siva Bathula – Yup, you're right.
Log in to reply
@Christopher Boo – So although 36 is a possible solution, it may not be the least number of combinations? A better solution can be still be found?
Log in to reply
@Siva Bathula – No, 3 6 is the least number of merging already.
nice solution, +1
can you please add an example to your problem "Friends (Hard)"? :)
Log in to reply
Ahh, I am terribly sorry for that problem. I put the answer as 9 9 9 just to preview the problem statement and forgot to change it back to the correct answer.
Will add an additional example for the problem. :)
Python3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Nice iterative solution. Also, we have the recursive approach:
1 2 3 4 5 6 7 8 9 |
|
... which gives the output as 3 6 .
Problem Loading...
Note Loading...
Set Loading...
We only need to concentrate on the head and tails of the list. If both numbers are the same, we move on. Else, the smaller one should combine with its neighbor, until we loop through the entire list.
This is a greedy algorithm. That is, there is always a optimal approach regardless of the situation. In this case, we always combine the smaller one to its neighbor, but not the larger one. If we do the latter, it would make it the gap even worse.