Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Saturday, December 30, 2017

Maximum difference of zeros and ones


As per observation we are having 0 more than 1
If this is not the case then we have to consider both case whether 0 is more or not.
So create array(1=-1 & 0=1 )
Apply Kadane algo

Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class maxDifference01dp
 {
public static int maxDifference(String str)
{
int len=str.length();
int arr[]=new int[len];
int max=-1,temp=0;
for(int i=0;i<len;++i)
{
if(str.charAt(i)=='1')
arr[i]=-1;
else
arr[i]=1;
}
for(int i=0;i<len;++i)
{
temp+=arr[i];
if(temp<=0)
{
temp=0;
}
else
max=Math.max(max,temp);
}
return max;
}
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
    System.out.println(maxDifference(ab.next()));
}
}
}

0 Comments:

Post a Comment

Stats