We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
my code is a little bit of trash, but it fully matches logic of
wkusnierczyk's python code above and goes throw all tests.
U're welcome, but all thanks are still for wkusnierczyk, Im just a translator.
//PS I tried to post my code in a clear format using tags 'pre' or 'code', but I failed. So, it's just a bunch of code below, sorry for this. :)
<------------------------------------------->
Scanner scan = new Scanner(System.in);
int n = scan.nextInt(); int k = scan.nextInt(); int[] arr = new int[n];
for (int i = 0; i < n; ++i){ arr[i] = scan.nextInt(); } int[] cnts = new int[k];
for (int i = 0; i < k; ++i){cnts[i] = 0; }
for (int i = 0; i < n; ++i){ cnts[arr[i] % k] += 1; }
int cnt = cnts[0] < 1 ? cnts[0] : 1;
for (int i = 1; i < k/2+1; ++i){ if (i != k - i){cnt += cnts[i] > cnts[k-i] ? cnts[i] : cnts[k-i];}}
if (k % 2 == 0){ cnt += 1; }
System.out.println(cnt);
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Non-Divisible Subset
You are viewing a single comment's thread. Return to all comments →
my code is a little bit of trash, but it fully matches logic of wkusnierczyk's python code above and goes throw all tests. U're welcome, but all thanks are still for wkusnierczyk, Im just a translator. //PS I tried to post my code in a clear format using tags 'pre' or 'code', but I failed. So, it's just a bunch of code below, sorry for this. :)
<------------------------------------------->
Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int k = scan.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; ++i){ arr[i] = scan.nextInt(); } int[] cnts = new int[k]; for (int i = 0; i < k; ++i){cnts[i] = 0; } for (int i = 0; i < n; ++i){ cnts[arr[i] % k] += 1; } int cnt = cnts[0] < 1 ? cnts[0] : 1; for (int i = 1; i < k/2+1; ++i){ if (i != k - i){cnt += cnts[i] > cnts[k-i] ? cnts[i] : cnts[k-i];}} if (k % 2 == 0){ cnt += 1; } System.out.println(cnt);