📌  相关文章
📜  用最大位数* A和最小位数* B替换每个元素后,对具有相同MSD的相同奇偶校验索引元素对进行计数

📅  最后修改于: 2021-04-17 19:25:07             🧑  作者: Mango

给定一个由N个3位整数和两个整数ab组成的数组arr [] ,任务是根据以下规则修改每个数组元素:

  • 找到每个数组元素arr [i]的最大数M和最小数m
  • 将数组元素arr [i]更新为(A * M + B * m)

任务是对对数进行计数,以使所选元素仅处于奇数或偶数索引,并且最高有效位(MSD)必须相等,并且由该MSD形成的对数最多为2

例子:

方法:可以使用频率阵列解决给定的问题。请按照以下步骤解决给定的问题:

  • 通过查找元素的最小和最大数字,用(A * X + B * Y)%100更新arr []的每个数组元素,其中XYarr [i]的最大和最小数字
  • 初始化一个二维数组,例如mp [10] [2],以用MSD分别在偶数和奇数位置存储数字计数。
  • 遍历数组arr []并将mp [arr [i] / 10] [i%2]的计数增加1
  • 初始化一个变量,例如res ,以存储对的结果计数。
  • 遍历范围[ 0,9 ]并执行以下步骤:
    • 如果存在至少3个i为MSD的偶数索引或奇数索引,即mp [i] [0]> = 3 ||,则将res的计数增加2。 mp [i] [1]> = 3
    • 否则,如果在奇数索引处存在一对,而在偶数索引处存在一对,即mp [i] [0] == 2 && mp [i] [1] == 2 ,则将res的计数增加2
    • 否则,如果在奇数索引或偶数索引中存在一对,且MSD为i,mp [i] [0] == 2 ||。 mp [i] [1] == 2 ,然后将res的计数增加1
  • 完成上述步骤后,将res的值打印为对数。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
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 seperately
    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 seperately
    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 seperately
    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.


输出:
3

时间复杂度: O(N)
辅助空间: O(1)