Monday, March 8, 2021

Published March 08, 2021 by Anonymous with 0 comment

Count pairs of same parity indexed elements with same MSD after replacing each element by the sum of maximum digit * A and minimum digits * B

#include <bits/stdc++.h>

using namespace std;

  

int bit_score(int N)

{

    

    int maxi = 0, mini = 11;

    

    

    int score;

    

      

    for (int i = 0; i < 3; i++) {

        

          

        maxi = max(maxi, N % 10);

            

          

        mini = min(mini, N % 10);

        N /= 10;

        if (N == 0)

            break;

    }

    

    

    score = maxi * 11 + mini * 7;

    

      

    score = score % 100;

    

      

    return score;

}

  

int findPairs(int arr[], int N, int a, int b)

{

    

    for (int i = 0; i < N; i++) {

        arr[i] = bit_score(arr[i]);

    }

    

    

    int pairs = 0;

  

    

    

    int mp[10][2];

    

    

    memset(mp, 0, sizeof(mp));

  

    

    

    for (int i = 0; i < N; i++)

        mp[arr[i] / 10][i % 2]++;

  

    

    for (int i = 0; i < 10; i++) {

  

        if (mp[i][1] >= 3 || mp[i][0] >= 3)

            pairs += 2;

        else if (mp[i][1] == 2 && mp[i][0] == 2)

            pairs += 2;

        else if (mp[i][1] == 2 || mp[i][0] == 2)

            pairs += 1;

    }

    

    

    return pairs;

}

  

int main()

{

    int arr[] = { 234, 567, 321, 345,

                  123, 110, 767, 111 };

    int N = sizeof(arr) / sizeof(arr[0]);

      int a = 11, b = 7;

    

    cout << findPairs(arr, N, a, b);

  

    return 0;

}

Let's block ads! (Why?)

      edit

0 comments:

Post a Comment