Click here to Skip to main content
15,886,770 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to solve this problem http://codeforces.com/gym/100155/problem/A[^]

But it gives me wrong answer at testCase 35.
Help me by telling me a testCase that my code will not solve it correctly or tell me if I did a mistake while writing my code.

Java
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


public class Cand {
public static void main(String[] args) {
	Scanner sc = new Scanner (System.in);
	int c=sc.nextInt();
	int v=sc.nextInt();
	if(c==1 && v==0)
	{
		System.out.println(1+" "+1 );
		return;
	}
	HashMap<integer,integer> hm=new HashMap<integer,integer>();
	int [][]arr=new int [v][c];
	
	for (int i = 0; i <v;>		for (int j = 0; j <c;>			arr[i][j]=sc.nextInt();
		}
	}
	
	for (int i = 0; i < v; i++) {
		if(hm.containsKey(arr[i][0]))hm.replace(arr[i][0],hm.get(arr[i][0])+1);
		else hm.put(arr[i][0],1);
	}
	
	int firstMax=0;
	int secondMax=0;
	int firstKey=0;
	int secondKey=0;
	for (Map.Entry<integer,integer>map:hm.entrySet()) {
		if(map.getValue()>v/2)
		{
			System.out.println(map.getKey()+" "+1);
			return;
		}
		else if(map.getValue()>secondMax &&map.getValue()>firstMax)
			{
				firstMax=map.getValue();
				firstKey=map.getKey();
			}
		else if(map.getValue()>secondMax)
			{
				secondMax=map.getValue();
				secondKey=map.getKey();
			}
	}
	
	for (int i = 0; i < v; i++) {
		for (int j = 1; j <c mode="hold" />			if(arr[i][0]==firstKey || arr[i][0]==secondKey)break;
			else if(arr[i][j]==firstKey)
			{
				firstMax++;
				break;
			}
			else if(arr[i][j]==secondKey)
			{
				secondMax++;
				break;
			}
			
		}
	}
	if(firstMax>secondMax){
		
		System.out.println(firstKey+" "+2);
		
	}
	else System.out.println(secondKey+" "+2);
	
}
}
Posted
Updated 29-Nov-15 0:09am
v2

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- You see your code behaviour, you match it against your expectations.

secondary effects
- Your will be proud of finding bugs yourself.
- Your skills will improve.

You should find pretty quickly what is wrong.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Nov-15 12:51pm    
Sure, a 5.
—SA
Patrice T 29-Nov-15 12:59pm    
Thank you Sergey
Well, it's look like an homework, which can't be done here. Rather you should identify the error by yourself.
Develop a skill called - Debugging & check what's happening with your code, where it is failing the case.

-KR
 
Share this answer
 
Comments
omaryasser3 30-Nov-15 1:04am    
first of all , it is not a homework :) and the second important part is that I don't know the testCase that made my code be a wrong answer so all what I could have done was to try different testcases from my mind . BTW I have modified my code and it worked .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900