i m unable to pass test#1 of this problem
i have written the code and pass Test#0
i think my code is good but still fails in Task#1
can someone provide the test case of task#1
or any other way to help me with
You havn’t shared your code, so i cant really help you out by pointing logical errors in your code, but what i can do is share and explain my code.
void solve()
{
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
sort(all(a));
int t = n;
for (int i = 0; i < n; i++)
t = min(t, a[i] + n - i - 1);
cout << t << endl;
}
This was my code/logic. Pretty straightforward/standard if you go though it…
i saw yours code
and it is great
still can you help me with my logic
this is my code
i know it’s a bit lengthy and complex but i guess it should work correctly
can u tell me if this is correct or not
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner console = new Scanner(System.in);
int t = console.nextInt();
while(t --> 0){
int n = console.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; i++){
arr[i] = console.nextInt();
}
Arrays.sort(arr);
int ans = n;
if(arr[0] >= n){
System.out.println(ans);
}
else{
int i=0;
ans=0;
while(i<n){
int dup = 0;
for(int j=i; j<n; j++){
if(arr[i] == arr[j]){
dup++;
}
else{
break;
}
}
if(arr[i]-ans >= (n-i)){
ans += n-i;
break;
} else {
ans += arr[i]-ans;
}
i += dup;
}
System.out.println(ans);
}
}
}
}
Hi! Sry for getting back a bit late(I am not very active on cc nowadays).
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner console = new Scanner(System.in);
int t = console.nextInt();
while(t --> 0){
int n = console.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; i++){
arr[i] = console.nextInt();
}
Arrays.sort(arr);
int ans = n;
if(arr[0] >= n){
System.out.println(ans);
}
else{
int i=0;
while(i < n){
int j = arr[i] + n - 1 - i;
ans = ans < j ? ans : j;
i++;
}
System.out.println(ans);
}
}
}
}
I edited your code a bit. Honestly the if statement you put out is still useless(you could directly start with the while loop by keeping ans = n(which is the maximum ans possible). I went through your code and honestly I didnt find any explicit flaw with your logic(even thought it is supposed to have a flaw given that it wasnt accepted), aside from the fact that it is immensely complex and having a very high time complexity(O(n^2) - which would have given TLE even if you got the right ans).
Thanks a lot!