给定一个由N个3位整数和两个整数a和b组成的数组arr [] ,任务是根据以下规则修改每个数组元素:
- 找到每个数组元素arr [i]的最大数M和最小数m 。
- 将数组元素arr [i]更新为(A * M + B * m) 。
任务是对对数进行计数,以使所选元素仅处于奇数或偶数索引,并且最高有效位(MSD)必须相等,并且由该MSD形成的对数最多为2 。
例子:
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
方法:可以使用频率阵列解决给定的问题。请按照以下步骤解决给定的问题:
- 通过查找元素的最小和最大数字,用(A * X + B * Y)%100更新arr []的每个数组元素,其中X和Y是arr [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)