Techno Blender
Digitally Yours.

Minimum moves to make String Palindrome incrementing all characters of Substrings

0 39


  

import java.io.*;

import java.util.*;

  

public class GFG {

  

    

    

    public static void minMoves(int a[], int n)

    {

        int b[] = new int[n];

        for (int i = 0; i < n; i++) {

            if (a[i] > a[n - i - 1]) {

                b[i] = a[i] - a[n - i - 1];

            }

        }

        long ans = b[0];

        for (int i = 1; i < n; i++) {

            ans += Math.max(0, b[i] - b[i - 1]);

        }

        System.out.println(ans);

    }

  

    

    public static void main(String[] args)

    {

        

        String S1 = "2643";

        int N = S1.length();

        int[] A = new int[N];

        for (int i = 0; i < N; i++) {

            A[i] = S1.charAt(i) - '0';

        }

  

        

        minMoves(A, N);

  

        

        String S2 = "113678";

        N = S2.length();

        int[] B = new int[N];

        for (int i = 0; i < N; i++) {

            B[i] = S2.charAt(i) - '0';

        }

  

        

        minMoves(B, N);

    }

}


  

import java.io.*;

import java.util.*;

  

public class GFG {

  

    

    

    public static void minMoves(int a[], int n)

    {

        int b[] = new int[n];

        for (int i = 0; i < n; i++) {

            if (a[i] > a[n - i - 1]) {

                b[i] = a[i] - a[n - i - 1];

            }

        }

        long ans = b[0];

        for (int i = 1; i < n; i++) {

            ans += Math.max(0, b[i] - b[i - 1]);

        }

        System.out.println(ans);

    }

  

    

    public static void main(String[] args)

    {

        

        String S1 = "2643";

        int N = S1.length();

        int[] A = new int[N];

        for (int i = 0; i < N; i++) {

            A[i] = S1.charAt(i) - '0';

        }

  

        

        minMoves(A, N);

  

        

        String S2 = "113678";

        N = S2.length();

        int[] B = new int[N];

        for (int i = 0; i < N; i++) {

            B[i] = S2.charAt(i) - '0';

        }

  

        

        minMoves(B, N);

    }

}

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment