📌  相关文章
📜  在用最大数字 * A 和最小数字 * B 的总和替换每个元素后,计算具有相同 MSD 的相同奇偶索引元素的对

📅  最后修改于: 2021-09-06 06:53:28             🧑  作者: 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 的数字,则将res的计数增加2 ,即mp[i][0] >= 3 || 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.


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 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()
{
    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


输出:
3

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