Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 46 additions & 14 deletions Arrays/Flip.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* You are given a binary string(i.e. with characters 0 and 1) S consisting of characters S1, S2, , SN. In a single operation, you can choose two indices L and R such that 1 ? L ? R ? N and flip the characters SL, SL+1, , SR. By flipping, we mean change character 0 to 1 and vice-versa.
* You are given a binary string(i.e. with characters 0 and 1) S consisting of characters S1, S2, , SN. In a single operation, you can choose two indices L and R such that 1 ? L ? R ? N and flip the characters SL, SL+1, , SR. By flipping, we mean change character 0 to 1 and vice-versa.

You aim is to perform ATMOST one operation such that in final string number of 1s is maximised. If you dont want to perform the operation, return an empty array. Else, return an array consisting of two elements denoting L and R. If there are multiple solutions, return the lexicographically smallest pair of L and R.
You aim is to perform ATMOST one operation such that in final string number of 1s is maximised. If you dont want to perform the operation, return an empty array. Else, return an array consisting of two elements denoting L and R. If there are multiple solutions, return the lexicographically smallest pair of L and R.

Notes:
- Pair (a, b) is lexicographically smaller than pair (c, d) if a < c or, if a == b, then b < d.
Expand All @@ -25,17 +25,49 @@


*/
import java.util.*;
public class Flip {
public static ArrayList<Integer> flip(String A) {
ArrayList<Integer> result = new ArrayList<Integer>();
char[] ch = A.toCharArray();
int L = 0;
int R = A.length();


}
public static void main(String[] args) {
public class Solution {
public ArrayList<Integer> flip(String A) {
int i = 0, maxSum = Integer.MIN_VALUE, sum = 0, left = 0, finalLeft = 0, finalRight = 0;
/*
sum : represesnts the potential effective no. of maximum 1s after flip, hence it is incremented and decremented by 1 when it encounters encountered 0 or 1 respectively, If sum becomes -ve it is restored to 0, and left is chenged.
left : represesnts the temporary initial left position for the pair.
finalLeft : represesnts the final answer of left position
finalRight : likewise, but for right position
maxSum : Is kept to compare previous max and current sum for max no. of 1s after flip.
*/
ArrayList<Integer> n = new ArrayList<Integer>();
while( i < A.length() )
{
if(A.charAt(i) == '1')
{
sum--; // since after flip it becomes 0
}
else if (A.charAt(i) == '0')
{
sum++; // since after flip it becomes 1
}

if( sum < 0 )
{
left = i+1;
sum = 0;
}

else if( sum > maxSum )
{
finalLeft = left;
finalRight = i;
maxSum = sum;
}
i++;
}

if(maxSum<=0)
{
return n; // if no such way of flip found that increases no. of 1s
}
n.add(finalLeft+1);// +1 since the answer is expected that way (index: 0 -> 1, basically)
n.add(finalRight+1);
return n;
}
}
}