facing problem when submittin Solution of CHAIRS problem COOK79 , giving WA to mySolution

below is Soluton Submitted by me…

 import java.util.ArrayList;
 import java.util.Scanner;
 class CHAIRS {
static int finalsum = 0;
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int noOfCases = s.nextInt();
    for (int i = 0; i < noOfCases; i++) {
        int noOfChairs = s.nextInt();
        String BinaryString = s.next();
        calculteMinimumSteps(BinaryString);
    }
}

private static void calculteMinimumSteps(String BinaryString) {
    finalsum = 0;
    ArrayList<Integer> al = new ArrayList(1000000);
    for (int i = 0; i < BinaryString.length(); i++) {
        if (BinaryString.charAt(i) == '1') {
            al.add(i + 1);
        }
    }
    for (int i = 1; i <= al.size(); i++) {
        int value1 = al.get(i - 1);
        int sum = 0;
        for (int j = 1; j <= al.size(); j++) {
            if (j == i) {
                continue;
            }
            int value2 = al.get(j - 1);
            int count;

            if (j > i) {
                int countFromFrontSide = value2 - value1 - 1 - (j - i - 1);
                int countFromBackSide = BinaryString.length() - value2 - (al.size() - j) + value1 - 1 - (i - 1);
                count = countFromFrontSide < countFromBackSide ? countFromFrontSide : countFromBackSide;
            } else {
                int countFromFrontSide = value1 - value2 - 1 - (i - j - 1);
                int countFromBackSide = BinaryString.length() - value1 - (al.size() - i) + value2 - 1 - (j - 1);
                count = countFromFrontSide < countFromBackSide ? countFromFrontSide : countFromBackSide;

            }
            sum = sum + count;
        }

        if (i == 1) {
            finalsum = sum;
        } else if (sum < finalsum) {
            finalsum = sum;
        }
    }
    System.out.println(finalsum);
}

}

i checked all cases on my netbeans-ide which i can think… its giving right answer… can anyone tell me any edge cases whose correct answer is not given by my code

Here is your code failing here.

input:

1

11

11011111010

Yours output: 3

Actual output: 2

3 Likes

are u sure, by 2 steps we will be able to sit all childs together… i think it takes minimum 3 step to sit all together… please exaplain little bit about ur comment how is it possible…?

In first iteration, 2nd child from left moves anticlockwise and result will be:- 10011111011

Now the first child move anticlockwise and arrangement will be:- 00011111111.
Tada! Problem solved.

okey bro… i got it… i misunderstood problem… i thought i can move child only 1 place… now question clear on my mind