Open In App

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given an array arr[] of N 3-digit integers and two integers a and b, the task is to modify each array element according to the following rules:

The task is to count the number of pairs such that the chosen elements are at only odd or even indices and the most significant digits(MSD) must be equal and the number of pairs formed from that MSD is at most 2.

Examples:

Input: arr[] = {234, 567, 321, 345, 123, 110, 767, 111}, N = 8, A = 11, B  = 7
Output: 3
Explanation:
Modify the array elements by the following operations:

  • The maximum and minimum digit of arr[0] (= 234) are 4 and 2 respectively. Therefore, replace arr[0] by 11 * 4 + 7 * 2 = 58.
  • The maximum and minimum digit of arr[1] (= 567) are 7 and 5 respectively. Therefore, replace arr[1] by 11 * 7 + 7 * 5 = 77 + 35 = 112.
  • The maximum and minimum digit of arr[2] (= 321) and arr[4] (= 123) are 3 and 1 respectively. Therefore, replace them by 11 * 3 + 7 * 1 = 40.
  • The maximum and minimum digit of arr[3] (= 345) are 5 and 3 respectively. Therefore, replace arr[3] by 11 * 5 + 7 * 3 = 76.
  • The maximum and minimum digit of arr[5] (= 110 ) are 1 and 0 respectively. Therefore, replace arr[5] by 11 * 1 + 7 * 0 = 11.
  • The maximum and minimum digit of arr[6] (= 767) are 7 and 6 respectively. Therefore, replace arr[6] by 11 * 7 + 7 * 6 = 77 + 42 = 119.
  • The maximum and minimum digit of arr[7] (= 111) are 1 and 2 respectively. Therefore, replace arr[7] by 11 * 1 + 7 * 1 = 18.

Therefore, the array arr[] modifies to {58, 12, 40, 76, 40, 11, 19, 18].
One possible way of forming pairs is {{40, 40}, {12, 11}, {11, 18}}.

Input: arr[] = {123, 452, 345, 124, 453}, N = 5, A = 11, B = 7
Output:1

Approach: The given problem can be solved using a frequency array. Follow the steps below to solve the given problem:

  • Update each array element of arr[] with (A * X + B * Y)%100 by finding the minimum and maximum digit of the element, where X and Y are maximum and minimum digits of the arr[i].
  • Initialize a 2D array, say mp[10][2], to store the count of numbers with MSD at even and odd positions separately.
  • Traverse the array arr[] and increment the count of mp[arr[i]/10][i%2] by 1.
  • Initialize a variable, say res, to store the resultant count of pairs.
  • Iterate over the range [0, 9] and perform the following steps:
    • Increment the count of res by 2 if there exist at least 3 numbers with i as MSD at even indices or odd indices i.e., mp[i][0] >= 3 || mp[i][1] >= 3.
    • Otherwise, if there exists one pair at odd indices and one pair at even indices i.e., mp[i][0] == 2 && mp[i][1] == 2, then increment the count of res by 2.
    • Otherwise, if there exists a pair either at odd indices or even indices with MSD as i i.e., mp[i][0] == 2 || mp[i][1] == 2, then increment the count of res by 1.
  • After completing the above steps, print the value of res as the count of pairs.

Below is the implementation of the above approach:

C++14




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to modify N into
// A * max digit + B * min digit
// and calculate score
int bit_score(int N)
{
    // Stores maximum and minimum digit
    int maxi = 0, mini = 11;
    
    // Stores the val of N
    int score;
    
      // Find the maximum and minimum digits
    for (int i = 0; i < 3; i++) {
        
          // Update maximum digit
        maxi = max(maxi, N % 10);
            
          // Update minimum digit
        mini = min(mini, N % 10);
        N /= 10;
        if (N == 0)
            break;
    }
    
    // Calculate the modified number
    score = maxi * 11 + mini * 7;
    
      // Extract last two digits
    score = score % 100;
    
      // Return the final score
    return score;
}
  
// Function to count the number of
// pairs possible from either odd
// or even indices having same MSD
int findPairs(int arr[], int N, int a, int b)
{
    // Update the array
    for (int i = 0; i < N; i++) {
        arr[i] = bit_score(arr[i]);
    }
    
    // Stores the total number of pairs
    int pairs = 0;
  
    // Stores the count of numbers having
    // MSD at even or odd position separately
    int mp[10][2];
    
    // Initialize all elements as 0
    memset(mp, 0, sizeof(mp));
  
    // Calculate the count of a MSD
    // at even and odd positions
    for (int i = 0; i < N; i++)
        mp[arr[i] / 10][i % 2]++;
  
    // Iterate over range [0, 9]
    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 the resultant count of pairs
    return pairs;
}
  
// Driver Code
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;
}


Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
  
class GFG {
  
  // Function to modify N into
  // A * max digit + B * min digit
  // and calculate score
  static int bit_score(int N)
  {
    // Stores maximum and minimum digit
    int maxi = 0, mini = 11;
  
    // Stores the val of N
    int score;
  
    // Find the maximum and minimum digits
    for (int i = 0; i < 3; i++) {
  
      // Update maximum digit
      maxi = Math.max(maxi, N % 10);
  
      // Update minimum digit
      mini = Math.min(mini, N % 10);
      N /= 10;
      if (N == 0)
        break;
    }
  
    // Calculate the modified number
    score = maxi * 11 + mini * 7;
  
    // Extract last two digits
    score = score % 100;
  
    // Return the final score
    return score;
  }
  
  // Function to count the number of
  // pairs possible from either odd
  // or even indices having same MSD
  static int findPairs(int arr[], int N, int a, int b)
  {
    // Update the array
    for (int i = 0; i < N; i++) {
      arr[i] = bit_score(arr[i]);
    }
  
    // Stores the total number of pairs
    int pairs = 0;
  
    // Stores the count of numbers having
    // MSD at even or odd position separately
    int mp[][] = new int[10][2];
  
    // Calculate the count of a MSD
    // at even and odd positions
    for (int i = 0; i < N; i++)
      mp[arr[i] / 10][i % 2]++;
  
    // Iterate over range [0, 9]
    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 the resultant count of pairs
    return pairs;
  }
  
  // Driver Code
  public static void main(String[] args)
  {
  
    int arr[] = { 234, 567, 321, 345, 123, 110, 767, 111 };
    int N = arr.length;
    int a = 11, b = 7;
  
    System.out.println(findPairs(arr, N, a, b));
  }
}
  
// This code is contributed by Kingash.


Python3




# Python3 program for the above approach
  
# Function to modify N into
# A * max digit + B * min digit
# and calculate score
def bit_score(N):
    
    # Stores maximum and minimum digit
    maxi , mini = 0, 11
  
    # Stores the val of N
    score = 0
  
      # Find the maximum and minimum digits
    for i in range(3):
  
          # Update maximum digit
        maxi = max(maxi, N % 10)
  
          # Update minimum digit
        mini = min(mini, N % 10)
        N //= 10
        if (N == 0):
            break
  
    # Calculate the modified number
    score = maxi * 11 + mini * 7
  
      # Extract last two digits
    score = score % 100
  
      # Return the final score
    return score
  
# Function to count the number of
# pairs possible from either odd
# or even indices having same MSD
def findPairs(arr, N, a, b):
    #Update the array
    for i in range(N):
        arr[i] = bit_score(arr[i])
  
    # Stores the total number of pairs
    pairs = 0
  
    # Stores the count of numbers having
    # MSD at even or odd position separately
    mp = [[0 for i in range(2)] for i in range(10)]
  
    # Initialize all elements as 0
    # memset(mp, 0, sizeof(mp))
  
    # Calculate the count of a MSD
    # at even and odd positions
    for i in range(N):
        mp[arr[i] // 10][i % 2] += 1
  
    # Iterate over range [0, 9]
    for i in range(10):
        if (mp[i][1] >= 3 or mp[i][0] >= 3):
            pairs += 2
        elif (mp[i][1] == 2 and mp[i][0] == 2):
            pairs += 2
        elif (mp[i][1] == 2 or mp[i][0] == 2):
            pairs += 1
  
    # Return the resultant count of pairs
    return pairs
  
# Driver Code
if __name__ == '__main__':
    arr = [234, 567, 321, 345, 123, 110, 767, 111]
    N = len(arr)
    a, b = 11, 7
  
    print (findPairs(arr, N, a, b))
  
# This code is contributed by mohit kumar 29.


C#




// C# program for the above approach
using System;
  
class GFG{
  
// Function to modify N into
// A * max digit + B * min digit
// and calculate score
static int bit_score(int N)
{
      
    // Stores maximum and minimum digit
    int maxi = 0, mini = 11;
      
    // Stores the val of N
    int score;
      
    // Find the maximum and minimum digits
    for(int i = 0; i < 3; i++) 
    {
      
        // Update maximum digit
        maxi = Math.Max(maxi, N % 10);
          
        // Update minimum digit
        mini = Math.Min(mini, N % 10);
        N /= 10;
          
        if (N == 0)
            break;
    }
      
    // Calculate the modified number
    score = maxi * 11 + mini * 7;
      
    // Extract last two digits
    score = score % 100;
      
    // Return the final score
    return score;
}
      
// Function to count the number of
// pairs possible from either odd
// or even indices having same MSD
static int findPairs(int[] arr, int N, 
                     int a, int b)
{
      
    // Update the array
    for(int i = 0; i < N; i++)
    {
        arr[i] = bit_score(arr[i]);
    }
      
    // Stores the total number of pairs
    int pairs = 0;
      
    // Stores the count of numbers having
    // MSD at even or odd position separately
    int[,] mp = new int[10, 2];
      
    // Calculate the count of a MSD
    // at even and odd positions
    for(int i = 0; i < N; i++)
        mp[arr[i] / 10, i % 2]++;
      
    // Iterate over range [0, 9]
    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 the resultant count of pairs
    return pairs;
}
  
// Driver Code
public static void Main()
{
    int[] arr = { 234, 567, 321, 345,
                  123, 110, 767, 111 };
    int N = arr.Length;
    int a = 11, b = 7;
  
    Console.Write(findPairs(arr, N, a, b));
}
}
  
// This code is contributed by sanjoy_62


Javascript




<script>
  
// Javascript program for the above approach
  
// Function to modify N into
// A * max digit + B * min digit
// and calculate score
function bit_score(N)
{
      
    // Stores maximum and minimum digit
    let maxi = 0, mini = 11;
      
    // Stores the val of N
    let score;
      
    // Find the maximum and minimum digits
    for(let i = 0; i < 3; i++) 
    {
          
        // Update maximum digit
        maxi = Math.max(maxi, N % 10);
          
        // Update minimum digit
        mini = Math.min(mini, N % 10);
        N /= 10;
          
        if (N == 0)
            break;
    }
      
    // Calculate the modified number
    score = maxi * 11 + mini * 7;
      
    // Extract last two digits
    score = score % 100;
      
    // Return the final score
    return score;
}
      
// Function to count the number of
// pairs possible from either odd
// or even indices having same MSD
function findPairs(arr, N, a, b)
{
      
    // Update the array
    for(let i = 0; i < N; i++)
    {
        arr[i] = bit_score(arr[i]);
    }
      
    // Stores the total number of pairs
    let pairs = 0;
      
    // Stores the count of numbers having
    // MSD at even or odd position separately
    let mp = new Array(10);
      
    // Loop to create 2D array using 1D array
    for (let i = 0; i < mp.length; i++) 
    {
        mp[i] = new Array(2);
    }
      
    for(let i = 0; i < mp.length; i++) 
    {
        for(let j = 0; j < mp.length; j++)
        {
            mp[i][j] = 0;
        }
    }
      
    // Calculate the count of a MSD
    // at even and odd positions
    for(let i = 0; i < N; i++)
        mp[(Math.floor(arr[i] / 10))][i % 2]++;
      
    // Iterate over range [0, 9]
    for(let 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 the resultant count of pairs
    return pairs;
}
  
// Driver code
  
// Given Input
let arr = [ 234, 567, 321, 345, 
            123, 110, 767, 111 ];
let N = arr.length;
let a = 11, b = 7;
  
document.write(findPairs(arr, N, a, b));
  
// This code is contributed by target_2  
  
</script>


Output: 

3

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 



Last Updated : 24 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads