question link =
i have also tried to apply quick sort in that question instead of Arrays.sort(a);
but still nzec error is there…(just giving it a try -_-)…
the code is working fine on ideone and eclipse.
my code :-
import java.io.;
import java.util.;
import java.math.*;
public class Main
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
int t = kb.nextInt();
while(t-->0)
{
int n,m;
n = kb.nextInt();
m = kb.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++)
a[i] = kb.nextInt();
Arrays.sort(a);
long count = 0;
int ii = n-1;
for(int i=n-1;i>=0;i--)
{
count+=a[i];
ii = i;
if(count>=m)
break;
}
System.out.println(n-ii);
}
}
}
1 Like
Solutions are not public currently (weird its an old problem, maybe this is currently featuring in some live contest ?!!).
@mediocoder i have posted the solution here…please have a look…
hey himanshu,
Even i code on java and used to have same problems.
Actually NZEC error on java occurs mostly due to some exceptions in I/O process.
You may use ‘try’ and ‘catch’ to remove those exceptions!
After that it will run perfectly.
try it using Try and Catch!
2 Likes
hey himanshu,
Even i code on java and used to have same problems.
Actually NZEC error on java occurs mostly due to some exceptions in I/O process.
You may use ‘try’ and ‘catch’ to remove those exceptions!
After that it will run perfectly.
try it using Try and Catch!
2 Likes
There are two main problems in your code for the said problem.
- Constraint is 1 ≤ M, P_{i} ≤ 10^{18}. So you need to use long datatype instead of int for m and also the array. This is the reason for NZEC.
- You are missing the output format for -1, read the output statements again. It says If it’s impossible to take at least M meatballs, print -1..You are not printing -1.
The corrected code is given below.
import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
boolean flag; // for the case where If it's impossible to take at least M meatballs, print -1.
int t = kb.nextInt();
while(t-->0)
{
int n;
long m; // constraint for M is 10^18
flag=false;
n = kb.nextInt();
m = kb.nextLong();
long[] a = new long[n]; //constraint for Pi is 10^18
for(int i=0;i<n;i++)
a[i] = kb.nextLong();
Arrays.sort(a);
long count = 0;
int ii = n-1;
for(int i=n-1;i>=0;i--)
{
count+=a[i];
ii = i;
if(count>=m){
flag=true;
break;
}
}
if(flag)
System.out.println(n-ii);
else
System.out.println("-1");
}
}
}
Check the ACcepted code with the said modifications here.
1 Like
ok…i got it…
i have one more doubt…as i am new to java…
code_hard123 has said that i should use try and catch block…for this…
can you please tell me that why and when we are required to use this…?
1 Like
By using try-catch block you can overcome/ignore the NZEC but for this specific situation, try-catch block will not give you the correct answer. Instead of NZEC, the judge will give you some other NZEC or WA.
Here ( in codechef ), you rarely need ( almost never ) try-catch to solve the problems. Try-catch is used to handle exceptions.
1 Like
This exception handling in java is an astonishing, very very powerful feature and very very very much used in real world projects/programs. It is seldom used in competitive programming but you can use this if you want to.
If you have no idea ( as you said you are a beginner ) about try-catch then search in the internet or go through a book but it is usually not needed here.
1 Like