I was solving an ICPC 2012 question.The question says -

"You have a wall of the cave consisting of 'lighted diamonds' arranged in a N by M grid (basically, you have a light behind each diamond which can be turned on or off). Further, you have a switch corresponding to each row of this diamond-grid. When you operate a switch, it will toggle (flip) the lights corresponding to that row.

You are given the current configuration of the lighted diamonds. Gimli challenges Legolas to turn on as many diamonds as possible using EXACTLY K on/off operations of the switches. Since Legolas is an Elf of the Wood and doesn't care much for things that glitter, he instead asks for your help. Note that the same switch (i.e. row) can be chosen multiple times.

Input (STDIN):

The first line contains the number of test cases T. Each test case contains N, M and K on the first line followed by N lines containing M characters each. The ith line denotes the state of the diamonds in the ith row, where '*' denotes a diamond which is on and '.' denotes a diamond which is off.

Output (STDOUT):

Output T lines containing the answer for the corresponding case.

Between successive test cases, there should not be any blank lines in the output.

Constraints:

1 <= T <= 100

1 <= N,M <= 50

1 <= K <= 100

Sample Input:

2

2 2 1

..

**

2 2 2

..

**

Sample Output:

4

2

Notes/Explanation of Sample Input:

In the first test case, row 1 can be toggled hence leaving all 4 lights to be in the ON state.

In the second test case, row 1 (or row 2) can be toggled twice, hence maintaining the state of the initial configuration. "

I first toggle the rows which result in positive or zero(toggling a row 2 times gives 0 change) change in *'s in decreasing order. At the end, if there is 1 toggle left, toggle the row with the least absolute value amongst the rows having negative changes.

I tested my code on various test cases but still the code is not accepted by the online judges.I need some help to figure out what is possibly going wrong with my code.

To have more test cases please go through -

http://ideone.com/etDkSF

My code -

Code Java:

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(t=t;t>0;t--){ int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); char [][] array= new char[n][m] ; int []tracker=new int[n]; int []trackerl=new int[n]; int i,j; for(i = 0; i < n; i++) { String temp = sc.next(); for(j = 0; j < m; j++) { array[i][j] = (temp.charAt(j)); } } int light=0,diamond=0; for(i=0;i<n;i++){ for(j=0;j<m;j++){ if(array[i][j]=='*') {diamond++;} } tracker[i]=diamond; diamond=0; } int a, b; int temp; int sortTheNumbers = n; for (a = 1; a < sortTheNumbers; a++) { for (b = 0; b < sortTheNumbers-a; b++) { if (tracker[b] > tracker[b + 1]) { temp = tracker[b]; tracker[b] = tracker[b + 1]; tracker[b + 1] = temp; } } } for(i=0;i<n;i++) trackerl[i]=m-tracker[i]; int br=0; try{ if(m%2==0) { for(i=0;i<=n-1;i++) if(tracker[i]>(m)/2) br++; } if(m%2!=0) { for(i=0;i<=n-1;i++) if(tracker[i]>=(m+1)/2) br++; }}catch(Exception e){} int ans=0; try{ if(br>=k) { for(i=n-1;i>(n-1-k);i--) ans+=tracker[i]; for(i=(n-1-k);i>=0;i--) ans+=trackerl[i]; } if(br<k) { if(br!=0 && br!=n) {for(i=n-1;i>(n-br);i--) {ans+=tracker[i];k--;} int pass1=0,pass2=0; if(k%2==0) pass1=Math.max(tracker[(n-br)]+tracker[(n-br-1)],trackerl[(n-br)]+trackerl[(n-br-1)]); if(k%2!=0) pass2=Math.max(tracker[(n-br)]+trackerl[(n-br-1)],trackerl[(n-br)]+tracker[(n-br-1)]);//System.out.print("Hp"+tracker[(n-br)]);} ans+=Math.max(pass1, pass2); for(i=(n-2-br);i>=0;i--) ans+=trackerl[i];} if(br!=0 && br==n) { for(i=n-1;i>(n-br);i--) {ans+=tracker[i];k--;} if(k%2!=0){ans+=tracker[(n-br)];} if(k%2==0){ans+=trackerl[(n-br)];} for(i=(n-1-br);i>=0;i--) { ans+=trackerl[i];} } if(br==0) { if(k%2!=0){ans+=tracker[(n-1)];} if(k%2==0){ans+=trackerl[(n-1)];} for(i=(n-2);i>=0;i--) { ans+=trackerl[i];} } }}catch(Exception e){} System.out.println(""+ans); } } }