Help me in solving MISSP problem

My issue

The code is running well for the simple test cases and also for the special cases but fails to give the correct answer when checked for the large test cases during submission. Plz help me out with it I got stuck with this problem.

My code

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t-->0){
		int n=sc.nextInt();
		int i,c=0;
		ArrayList a=new ArrayList<Integer>();
		for(i=0;i<n;i++){
		    a.add(sc.nextInt());
		}
		Collections.sort(a);
		for(i=0;i<n;i=i+2){
		    if(i<n-1){
		    if(a.get(i)!=a.get(i+1)){
		        c=i;
		        break;
		    }
		    }
		    else{
		        c=n-1;
		    }
		    
		}
		System.out.println(a.get(c));
		a.clear();
		}
		
	}
}

Learning course: Arrays, Strings & Sorting
Problem Link: CodeChef: Practical coding for everyone

1 Like

@indranil2004
Your logic is not right bro.
Your is failing for the test case
1
4
1
1
1
3
the correct answer would be 3
but your code is giving 1.

1 Like

That case it should give 4 not 3

but my code is correct for the. both test case when i submit it is wrong ans
please resolve my following code

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner in = new Scanner(System.in);
		int t = in.nextInt();
		while(t-->0){
		    int n = in.nextInt();
		    int[] a = new int[n];
		    for(int i = 0; i < a.length; i++){
		        a[i] = in.nextInt();
		    }
		    int count = 0;
		    for(int i = 0; i < a.length; i++){
		        if(a[i] % 2 != 0){
		            count++;
		        }
		    }
		    System.out.println(count);
		}
	}
}

@prasaddanidev
Your logic is not right .
U have to print the number that has frequency 1.
like for test case
1
4
1 1 1 2
the correct answer would be 2 becozz it has frequency 1 but your code will print 3 which is count of odd numbers in the array.

1 Like

@dpcoder_007
How can you take three 1’s as input even after its specified that chef collects dolls in pairs and a pair doesn’t mean three doll!!
Please give a legit reason addressing the fault in my logic and help me get off this problem…

Ok taking your logic I rectified the code.
Here it is…

import java.util.;
import java.lang.
;
import java.io.*;
class Codechef
{
public static void main (String args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t–>0){
int n=sc.nextInt();
int i,c=0;
ArrayList a=new ArrayList<>();
for(i=0;i<n;i++){
a.add(sc.nextInt());
}
Collections.sort(a);
for(i=0;i<n;i=i+2){
if(i<n-1){
if(a.get(i)!=a.get(i+1)){
if(i>0){
if(a.get(i)!=a.get(i-1)){
c=i;
break;
}
else
i–;
}
else{
c=i;
break;
}
}
}
else{
c=n-1;
}
}
System.out.println(a.get(c));
a.clear();
}
}
}

But the problem is not resolved its still says wrong answer…

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner in = new Scanner(System.in);
		int t = in.nextInt();
		while(t-- > 0){
		    int n = in.nextInt();
		    int[] a = new int[n];
            HashMap<Integer, Integer> map = new HashMap<Integer,Integer>();
            for(int i = 0; i < a.length; i++){
                a[i] =  in.nextInt();
            }
            for(int i = 0 ; i < a.length; i++){
                if(map.containsKey(a[i])){
                    int c = map.get(a[i]);
                    map.put(a[i], c + 1);
                }else{
                    map.put(a[i], 1);
                }
            }
            Set<Integer> keys = map.keySet();
            int missing = 0;
            for(int key : keys){
                int value = map.get(key);
                if(value % 2 != 0){
                    System.out.println(key);
                }
            }
		}
	}
}

This code is correct