Help me in solving LARGESTK problem

My issue

I was stuck at a testcase in contest.After contest I solve it in practice.
I see the testcase where i stuck then i correct it after running again it again wrong answer so i run into my vscode with the same testcase and it showing correct answer and showing wrong answer in codechef.

My code

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 N=sc.nextInt();
	    for(int x=0;x<N;x++){
	        int n=sc.nextInt();
	        HashMap<Integer,Integer> H=new HashMap<>();
	        for(int i=0;i<n;i++){
	            int v=sc.nextInt();
	            H.put(v,H.getOrDefault(v,0)+1);
	        }
	        int ans=H.size();
	        for(int i=n;i>H.size();i--){
	              if((i%H.size())==0){
	                   ans=i;
	                   break;
	                }
	        }
	        System.out.println(ans);
	    }
	}
}

Problem Link: Largest K Practice Coding Problem

@tg62500
here plzz refer my c++ code for better understanding of the logic

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int n;
	    cin>>n;
	    int a[n];
	    unordered_map<int,int> mp;
	    vector<int> v;
	    
	    for(int i=0;i<n;i++)
	    {
	        cin>>a[i];
	        mp[a[i]]++;
	    }
	    
	    for(auto x:mp)
	    {
	        v.push_back(x.second);
	    }
	    
	    sort(v.begin(),v.end(),greater<int>());
	    int j=0,sm=0;
	    int ans=0;
	    for(int i=1;i<=mp.size();i++)
	    {
	        sm+=v[j];
	        int ch=sm%i;
	        ans=max(ans,sm-ch);
	        j++;
	    }
	    cout<<ans<<endl;
	}
	return 0;
}