Find the winner of the game based on greater number of divisors

Given two arrays arr1[] and arr2[], player A picks an element from arr1[] and player B picks an element from arr2[], the player which has the element with more number of divisors wins the round. If both have elements with same number of divisors then player A wins that round. The task is to find whether player A has more probability of winning the game or the player B.

Examples:

Input: arr1[] = {4, 12, 24}, arr2[] = {25, 28, 13, 45}
Output: A
Pairs where A wins are (3, 2), (3, 3), (6, 2), (6, 3), (6, 6), (6, 6), (8, 2), (8, 3), (8, 6) and (8, 6).
Total = 10.
B wins in 2 cases.
Hence, A has more probability of winning the game.

Input: arr1[] = {7, 3, 4}, arr2[] = {5, 4, 12, 10}
Output: B

Approach:



Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
#define ll long long int
  
// Function to return the count
// of divisors of elem
int divisorcount(int elem)
{
    int ans = 0;
    for (int i = 1; i <= sqrt(elem); i++) {
        if (elem % i == 0) {
            if (i * i == elem)
                ans++;
            else
                ans += 2;
        }
    }
  
    return ans;
}
  
// Function to return the winner of the game
string findwinner(int A[], int B[], int N, int M)
{
    // Convert every element of A[]
    // to their divisor count
    for (int i = 0; i < N; i++) {
        A[i] = divisorcount(A[i]);
    }
  
    // Convert every element of B[]
    // to their divisor count
    for (int i = 0; i < M; i++) {
        B[i] = divisorcount(B[i]);
    }
  
    // Sort both the arrays
    sort(A, A + N);
    sort(B, B + M);
  
    int winA = 0;
    for (int i = 0; i < N; i++) {
        int val = A[i];
        int start = 0;
        int end = M - 1;
        int index = -1;
  
        // For every element of A apply Binary Search
        // to find number of pairs where A wins
        while (start <= end) {
            int mid = (start + end) / 2;
            if (B[mid] <= val) {
                index = mid;
                start = mid + 1;
            }
            else {
                end = mid - 1;
            }
        }
  
        winA += (index + 1);
    }
  
    // B wins if A doesnot win
    int winB = N * M - winA;
  
    if (winA > winB) {
        return "A";
    }
    else if (winB > winA) {
        return "B";
    }
  
    return "Draw";
}
  
// Driver code
int main()
{
    int A[] = { 4, 12, 24 };
    int N = sizeof(A) / sizeof(A[0]);
  
    int B[] = { 25, 28, 13, 45 };
    int M = sizeof(B) / sizeof(B[0]);
  
    cout << findwinner(A, B, N, M);
  
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
from math import *
  
# Function to return the count 
# of divisors of elem
def divisorcount(elem):
  
    ans = 0
    for i in range(1, int(sqrt(elem)) + 1):
        if (elem % i == 0):
            if (i * i == elem):
                ans += 1
            else:
                ans += 2
  
    return ans
  
# Function to return the winner of the game 
def findwinner(A, B, N, M):
  
    # Convert every element of A[] 
    # to their divisor count 
    for i in range(N):
        A[i] = divisorcount(A[i])
  
    # Convert every element of B[] 
    # to their divisor count 
    for i in range(M):
        B[i] = divisorcount(B[i])
  
    # Sort both the arrays
    A.sort()
    B.sort()
      
    winA = 0
    for i in range(N):
        val = A[i]
        start = 0
        end = M - 1
        index = -1
          
        # For every element of A[] apply
        # binary search to find number 
        # of pairs where A wins
        while (start <= end):
            mid = (start + end) // 2
              
            if (B[mid] <= val):
                index = mid
                start = mid + 1
                  
            else:
                end = mid - 1
          
        winA += (index + 1)
      
    # B wins if A doesnot win
    winB = N * M - winA
  
    if (winA > winB):
        return "A"
    elif (winB > winA):
        return "B"
  
    return "Draw"
  
# Driver code
if __name__ == '__main__':
          
    A = [ 4, 12, 24 ]
    N = len(A)
  
    B = [ 25, 28, 13, 45 ]
    M = len(B)
  
    print(findwinner(A, B, N, M))
  
# This code is contributed by himanshu77
chevron_right

Output:
A

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : himanshu77

Article Tags :