Pairwise Sum and Divide

Sort by

recency

|

10 Discussions

|

  • + 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
    
  • + 0 comments

    Note that a,b ≥ 1, so a+b/ab quickly approaches 0. Because we take the floor of the function only a few value pairs will give non-0 results. Experimenting around, you'll see that 1,1 gives 1+1/1*1 = 2/1 = 2 and 1,n | n≥2, yields 1+n/n, which will yield 1 for all values n. Next, 2,2 yields 4/4 = 1, and 2,n yields 2+n/2n = 0 for n ≥ 3.

    So, we need 2*nP2 | n = # of 1s, which is 2*n!/(n-2)!2! = 2n(n-1)/2 = n(n-1)

    Also, for each 1, each non-1 will yield 1, so with m 1s and n other numbers, we'll need n*m (note than n = len(a)-m).

    Lastly, we'll need nP2 | n = # of 2s, which, similar to above = n(n-1)/2

    As for coding, I reccommend creating a counter (you could just do an array [m,n], then calculate the necessary values from that and total them.

  • + 2 comments

    Test case 11 is not correct rest everything works fine. Any suggestions

  • + 0 comments

    Very Simple . We have to care about the only 2 and 1. Because other numbers , like 3,4 will give (3+4)/(3*4)= 0 .So don't care about them .

    All one(1) among them , make value 2 because (1+1)/(1*1) = 2.

    All one(1) with other numbers, make value 1 because like for 3 (1+3)/(1*3)= 1

    All two among them will make 1 . Because for 2 (2+2)/(2*2)=1 .

    So only count them and give the result

    My solution with comments line

    https://github.com/joy-mollick/Problem-Solving-Solutions-Math-Greedy-/blob/master/HackerRank-Pairwise%20Sum%20and%20Divide.cpp

  • + 0 comments

    Simple question

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    int main() {int t;
                cin>>t;
                while(t--)
                {
                    long int n;
                    cin>>n;
                    long long int a[n];
                    long long int s=0;
                    long int c1=0,c2=0;
                    for(int i=0;i<n;i++)
                    {
                        cin>>a[i];
                        if(a[i]==1)c1++;
                        if(a[i]==2)c2++;
                    }
                    s+=c1*(n-1);     
                    s+=(c2*(c2-1))/2;
                    cout<<s<<endl;
                }
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
        return 0;
    }