# Decrease operations to make String palindrome by incrementing prefix by 1

0
6

Given a string S of numbers of size N, the duty is to seek out the minimal variety of operations required to alter a string into palindrome and we are able to carry out the next process on the string :

• Select an index i (0 ≤ i < N) and for all 0 ≤ j ≤ i, set Sj = Sj + 1 (i.e. add 1 to each factor within the prefix of size i).

Word: Whether it is unattainable to transform S to a palindrome, print −1.

Examples:

Enter: S = “1234”
Output: 3
Clarification: We will carry out the next operations:
Choose i=1, “1234”→”2234″
Choose i=2, “2234”→”3334″
Choose i=1, “3334”→”4334″
Therefore 3 variety of operations required to alter a string into palindrome.

Enter:  S = “2442”
Output: 0
Clarification: The string is already a palindrome.

Strategy: The issue will be solved based mostly on the next remark:

Initially test if the string is already a palindrome or not. If it isn’t a palindrome, then it may be transformed right into a palindrome provided that all the weather in the precise half of the string is larger than those within the left half and the distinction between the characters nearer to finish is larger or equal to the distinction between those nearer to the centre.

Comply with the steps talked about under to implement the thought:

• First set i = 0, j = N -1 and max = IntegerMaximumValue and ans = 0.
• After that iterate a loop till j > i
• Test if S[j] < S[i], whether it is true then we are able to’t change the string into palindrome and return -1.
• In any other case, take absolutely the distinction of S[j] and S[i] and evaluate it with ans to seek out the utmost between them:
• If the utmost worth is lower than absolutely the distinction of S[j] and S[i], return -1.
• In any other case, max is absolutely the distinction between S[j] and S[i]
• Return ans which is the minimal variety of operations required to alter a string right into a palindrome.

Under is the implementation of the above strategy.

## Java

 ` `  `import` `java.io.*;` `import` `java.util.*;` ` `  `class` `GFG {` ` `  `    ` `    ` `    ``public` `static` `int` `minOperation(String str, ``int` `n)` `    ``{` `        ``int` `j = n - ``1``;` `        ``int` `i = ``0``;` `        ``int` `max = Integer.MAX_VALUE;` `        ``int` `ans = ``0``;` `        ``whereas` `(j > i) {` `            ``if` `(str.charAt(j) < str.charAt(i)) {` `                ``return` `-``1``;` `            ``}` `            ``int` `ok = Math.abs(str.charAt(j) - str.charAt(i));` `            ``ans = Math.max(ans, ok);` `            ``if` `(max < ok) {` `                ``return` `-``1``;` `            ``}` `            ``max = ok;` `            ``i++;` `            ``j--;` `        ``}` `        ``return` `ans;` `    ``}` ` `  `    ` `    ``public` `static` `void` `major(String[] args)` `    ``{` `        ``String S = ``"1234"``;` `        ``int` `N = S.size();` ` `  `        ` `        ``System.out.println(minOperation(S, N));` `    ``}` `}`

Time Complexity: O(N)
Auxiliary Area: O(1)