Sort by

recency

|

805 Discussions

|

  • + 1 comment

    I think test case 6 is incorrect. Given: n=5 k=1 s = [1 2 3 4 5] The "correct" output is 1 but all whole numbers are divisible by 1. Shouldn't the actual answer be 0 since a subset of size zero has no elements divisible by k?

    • + 0 comments

      Maximum size of subarray that satisfy the conditions is 1 because : it is the minimum possible subset of any array & (1+2) / 1, (1+3) / 1, (1+4) / 1, (1+5) / 1 ..... are all factors of 1.

  • + 0 comments

    I implemented algorithm in Java , and after testing , I found that in Test Case 5 , the expected result is 50 , but my actual calculation is 45 , Has anyone encountered this issue?

    I know the issue is on my side , Please , could someone help me check where the problem is in my Code ,

    The following is my code ...

        public static int nonDivisibleSubset(int k, List<Integer> s) {
            s=s.stream().map(item->item%k).collect(Collectors.toList());
            // Write your code here
            s=s.stream().sorted().collect(Collectors.toList());
            int max = 0;
            for (int i = 0; i < s.size(); i++) {
                List<Integer> ans = new ArrayList<>();
                ans.add(s.get(i));
                for (int j = i+1; j < s.size(); j++) {
                    Integer needCheck = s.get(j);
                    boolean match = true;
                    for (Integer an : ans) {
                        if ((an + needCheck) % k == 0) {
                            match = false;
                            break;
                        }
                    }
                    if (match) {
                        ans.add(needCheck);
                    }
                }
                if (max < ans.size()) {
                    max = ans.size();
                }
            }
            return max;
        }
    
  • + 0 comments

    I implemented algorithm in Java , and after testing , I found that in Test Case 5 , the expected result is 50 , but my actual calculation is 45 , Has anyone encountered this issue?

    I know the issue is on my side , Please , could someone help me check where the problem is in my Code ,

    The following is my code ...

        public static int nonDivisibleSubset(int k, List<Integer> s) {
            s=s.stream().map(item->item%k).collect(Collectors.toList());
            // Write your code here
            s=s.stream().sorted().collect(Collectors.toList());
            int max = 0;
            for (int i = 0; i < s.size(); i++) {
                List<Integer> ans = new ArrayList<>();
                ans.add(s.get(i));
                for (int j = i+1; j < s.size(); j++) {
                    Integer needCheck = s.get(j);
                    boolean match = true;
                    for (Integer an : ans) {
                        if ((an + needCheck) % k == 0) {
                            match = false;
                            break;
                        }
                    }
                    if (match) {
                        ans.add(needCheck);
                    }
                }
                if (max < ans.size()) {
                    max = ans.size();
                }
            }
            return max;
        }
    
  • + 0 comments

    this is very dificult if you dont do a little search for the algorithm

  • + 0 comments

    Here is a simple and effective code

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    def nonDivisibleSubset(k, s):
        rem=[0]*k
        for i in range(len(s)):
            rem[s[i]%k]+=1
        count=min(rem[0],1)
        for j in range(1,(k//2)+1):
            if j!=k-j:
                count+=max(rem[j],rem[k-j])
            else:
                count+=min(rem[j],1)
        return count
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        first_multiple_input = input().rstrip().split()
    
        n = int(first_multiple_input[0])
    
        k = int(first_multiple_input[1])
    
        s = list(map(int, input().rstrip().split()))
    
        result = nonDivisibleSubset(k, s)
    
        fptr.write(str(result) + '\n')
    
        fptr.close()