Hello Programmers, In this post, you will know how to solve the HackerRank Non Divisible Subset Solution. This problem is a part of the HackerRank Algorithms Series.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.
HackerRank Non Divisible Subset Solution
Task
Given a set of distinct integers, print the size of a maximal subset of S where the sum of any 2 numbers in S’ is not evenly divisible by k.
S = [19, 10, 12, 10, 24, 25, 22] k = 4
One of the arrays that can be created is S‘[0] = [10, 12, 25]. Another is S‘[1] = [19, 22, 24]. After testing all permutations, the maximum length solution array has 3 elements.
Function Description
Complete the nonDivisibleSubset function in the editor below.
nonDivisibleSubset has the following parameter(s):
- int S[n]: an array of integers
- int k: the divisor
Returns
- int: the length of the longest subset of S meeting the criteria
Input Format
The first line contains 2 space–separated integers, n and k, the number of values in S and the non factor.
The second line contains n space-separated integers, each an S[i], the unique values of the set.
Constraints
- 1 <= n <= 105
- 1 <= k <= 100
- 1 <= S[i] <= 109
- All of the given numbers are distinct.
Sample Input
STDIN Function ----- -------- 4 3 S[] size n = 4, k = 3 1 7 2 4 S = [1, 7, 2, 4]
Sample Output
3
Explanation
The sums of all permutations of two elements from S = {1, 7, 2, 4} are:
1 + 7 = 8
1 + 2 = 3
1 + 4 = 5
7 + 2 = 9
7 + 4 = 11
2 + 4 = 6
Only S‘ = {1, 7, 4} will not ever sum to a multiple of k = 3.
HackerRank Non Divisible Subset Solutions
Non Divisible Subset Solution in C
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main(){ int n; int k; scanf("%d %d",&n,&k); int count[k]; for (int i = 0; i < k; i++) { count[i] = 0; } for(int i = 0; i < n; i++){ int a; scanf("%d",&a); count[a % k]++; } int max = 0; for (int i = 0; i <= k/2; i++) { if (i == 0 || i == k - i) { if (count[i] >= 1) { max += 1; } } else { max += count[i] > count[k-i] ? count[i] : count[k-i]; } } printf("%d\n",max); return 0; }
Non Divisible Subset Solution in Cpp
#include<bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i = (a); i <= (b); ++i) #define FORD(i,a,b) for(int i = (a); i >= (b); --i) #define RI(i,n) FOR(i,1,(n)) #define REP(i,n) FOR(i,0,(n)-1) #define mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) #define mp make_pair #define pb push_back #define st first #define nd second #define sz(w) (int) w.size() typedef vector<int> vi; typedef long long ll; typedef long double ld; typedef pair<int,int> pii; const int inf = 1e9 + 5; const int nax = 1e6 + 5; int t[nax]; int main() { int n, k; scanf("%d%d", &n, &k); REP(_, n) { int a; scanf("%d", &a); ++t[a%k]; } int s = 0; s += min(t[0], 1); RI(i, k-1) { int j = k-i; if(j < i) break; if(i == j) s+= min(t[i], 1); else s += max(t[i], t[j]); } printf("%d\n", s); return 0; }
Non Divisible Subset Solution in Java
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) throws IOException{ new Solution().run(); } public void run() throws IOException{ Scanner in = new Scanner(System.in); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); int n = in.nextInt(); int k = in.nextInt(); int[]a = new int[n]; int[]c = new int[k]; for(int i=0;i<n;i++){ a[i] = in.nextInt(); a[i]=a[i]%k; c[a[i]]++; } int ans=0; ans+=(c[0]>0)?1:0;//good if 1 exists, cannot be more for(int i=1;i<=k-i;i++){ if(i<k-i) { ans+=Math.max(c[i],c[k-i]); } else {//i==k-i ans+=(c[i]>0)?1:0;//not more possible } } log.write("" +ans+"\n"); log.flush(); } }
Non Divisible Subset Solution in Python
rr = raw_input rrM = lambda: map(int,rr().split()) N,K = rrM() A = rrM() S = [0 for _ in xrange(K)] for i in A: S[i%K] += 1 ans = 0 for p in xrange(K/2 + 1): q = (K-p)%K if p==q: ans += 1 if S[p] else 0 else: ans += max(S[p],S[q]) print ans
Non Divisible Subset Solution using JavaScript
function processData(input) { //Enter your code here var lines = input.split(/[\r\n]+/); var n_k = lines[0].split(' ').map(Number); var n = n_k[0], k = n_k[1]; var ar = lines[1].split(' ').map(Number); var o = {}; for(var i = 0; i < ar.length; i++) { var r = ar[i] % k; o[r] = o[r] ? o[r] + 1 : 1; } var c = 0; for(var key in o) { var f = parseInt(key); if (f == 0) { if (o[f] >= 1) { c++; } } else { var f2 = k - f; if (f > f2 && o[f2]) { continue; } else if (f == f2) { if (o[f] >= 1) { c++; } } else { var a = o[f] || 0; var b = o[f2] || 0; c += Math.max(a, b); } } } console.log(c); } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });
Non Divisible Subset Solution in Scala
object Solution { def main(args: Array[String]) { val sc = new java.util.Scanner(System.in) //val sc = new Scanner(new File("/home/maxim/hackerrank/nondiv.txt")) val n = sc.nextInt() val k = sc.nextInt() var values = new Array[Int](k) for (i <- 0 until n) { values(sc.nextInt()%k) += 1 } var maxCount = if (values(0) > 0) 1 else 0 if ((k % 2 == 0) && (values(k/2) >0)) maxCount += 1 for (i <- 1 until k / 2 + k % 2) { if (values(i) > values(k-i)) { maxCount += values(i) } else { maxCount += values(k - i) } } println (maxCount) } }
Non Divisible Subset Solution in Pascal
var i,n,k,t,x:longint; a : array[0..101] of longint; begin readln(n,k); for i:=1 to n do begin read(x); a[x mod k]:=a[x mod k]+1; end; if a[0]>0 then t:=1 else t:=0; for i:=1 to (k-1) div 2 do if (a[i]>a[k-i]) then t:=t+a[i] else t:=t+a[k-i]; if (k mod 2=0) and (a[k div 2]>0) then t:=t+1; writeln(t); end.
Disclaimer: This problem (Non Divisible Subset) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.