Non-Divisible Subset

Non-Divisible Subset

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.

For example, the array S = [19,10,12,10,24,25,22] and 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. It should return an integer representing the length of the longest subset of S meeting the criteria.

nonDivisibleSubset has the following parameter(s):

  • S: an array of integers
  • k: an integer

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 describing 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.

Output Format

Print the size of the largest possible subset (S).

Sample Input

1
2
4 3
1 7 2 4

Sample Output

1
3

Explanation

The sums of all permutations of two elements from S` = {1,7,2,4} are:

1
2
3
4
5
6
1 + 7 = 8
1 + 2 = 3
1 + 4 = 5
7 + 2 = 9
7 + 4 = 11
2 + 4 = 6

We see that only S` = {1,7,4} will not ever sum to a multiple of k = 3.


Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
* Complete the 'nonDivisibleSubset' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER k
* 2. INTEGER_ARRAY s
*/

function nonDivisibleSubset(k, s) {
// Write your code here
let values = new Array(k).fill(0);
let result = 0;

s.reduce((target, item, index) => {
values[item % k] += 1;

return target;
}, []);

for (let i of Array.from(
{ length: (k + 1) / 2 - 1 },
(value, index) => index + 1
)) {
result += Math.max(values[i], values[k - i]);
}

!(k % 2) && !!values[k / 2] && (result += 1);

values[0] && (result += 1);

return result;
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×