Pairwise Sum and Divide

  • + 0 comments

    My Python code that passed all test cases:

    def solve(a):
        n1 = a.count(1) # no. of 1's in a
        n2 = a.count(2) # no. of 2's in a
        n = len(a)
        
        cnt_1_1 = n1*(n1 - 1)//2  # no. of (1,1) pairs
        cnt_2_2 = n2*(n2 - 1)//2  # no. of (2,2) pairs
        cnt_1_n = n1*(n - n1)    #  no. of (1,n) pairs
        
        return 2*cnt_1_1 + cnt_2_2 + cnt_1_n