XOR Subsequences Discussions | Algorithms | HackerRank
  • + 0 comments

    Due to time error, I don't know how to solve this...

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    from collections import Counter
    from operator import itemgetter
    #
    # Complete the 'xorSubsequence' function below.
    #
    # The function is expected to return an INTEGER_ARRAY.
    # The function accepts LONG_INTEGER_ARRAY a as parameter.
    #
    
    def xor_sub(arr, n):
        
        result = []
        for i in range(n):
            result.append(arr[i])
        for j in range(2, n+1):
            for i in range(len(arr) - j + 1):
                a = arr[i]
                for k in range(1,j):
                    a ^= arr[i+k]
                result.append(a)
        return result
                
        
    
    def maxOccur(A):
        count = Counter(A)
        sorted_count = sorted(count.items(), key=lambda x: (-x[1],x[0]))
        return sorted_count[0]
    
    
    def xorSubsequence(a):
        # Write your code here
        l = []
        l = xor_sub(a,len(a))
        print(l)
        # check the frequency
        # a = Counter(l)
        # a_sorted = sorted(a.items(),key=itemgetter(1), reverse=True)
        # a_sorted = sorted(a_sorted)
        ti = maxOccur(l)
        retL = []
        retL.append(ti[0])
        retL.append(ti[1])
        return retL
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        a_count = int(input().strip())
    
        a = []
    
        for _ in range(a_count):
            a_item = int(input().strip())
            a.append(a_item)
    
        result = xorSubsequence(a)
    
        fptr.write(' '.join(map(str, result)))
        fptr.write('\n')
    
        fptr.close()