Minimize the maximum of 0s left or 1s deleted from Binary String – GeeksforGeeks

  

import java.io.*;

  

class GFG {

  

    

    public static int minCost(String s)

    {

        int n = s.length();

        int count0 = 0;

        int ans = Integer.MAX_VALUE;

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

            if (s.charAt(i) == '0')

                count0++;

        }

        int prefixCountZero[] = new int[n];

        int suffixCountZero[] = new int[n];

  

        

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

            if (i != 0)

                prefixCountZero[i] = prefixCountZero[i - 1];

            if (s.charAt(i) == '0')

                prefixCountZero[i]++;

        }

  

        

        for (int i = n - 1; i >= 0; i--) {

            if (i != n - 1)

                suffixCountZero[i] = suffixCountZero[i + 1];

            if (s.charAt(i) == '0')

                suffixCountZero[i]++;

        }

  

        

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

            int x;

            if (i == 0) {

                x = count0 - suffixCountZero[n - count0];

            }

            else if (i == count0) {

                x = count0 - prefixCountZero[count0 - 1];

            }

            else {

                x = count0 - prefixCountZero[i - 1]

                    - suffixCountZero[n - (count0 - i)];

            }

            ans = Math.min(ans, x);

        }

        return ans;

    }

  

    

    public static void main(String[] args)

    {

        String S = "101110110";

  

        

        System.out.println(minCost(S));

    }

}

 

Stay connected with us on social media platform for instant update click here to join our  Twitter, & Facebook We are now on Telegram. Click here to join our channel (@TechiUpdate) and stay updated with the latest Technology headlines. For all the latest Technology News Click Here 

Read original article here

Denial of responsibility! FineRadar is an automatic aggregator around the global media. All the content are available free on Internet. We have just arranged it in one platform for educational purpose only. 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 on our website, please contact us by email – abuse@fineradar.com. The content will be deleted within 24 hours.
binaryDeletedfineradar updateFree Fire Redeem Codesgadget updateGeeksforGeeksLatest tech newsleftmaximumMinimizestringTech Headlinestech newsTech News UpdatesTechnologyTechnology News
Comments (0)
Add Comment