Minimize replacements to make every element in an array exceed every element in another given array
Last Updated :
04 Mar, 2023
Given two arrays A[] and B[] of size N and M respectively, where each element is in the range [0, 9], the task is to make each element of the array A[] strictly greater than or smaller than every element in the array B[] by changing any element from either array to any number in the range [0, 9], minimum number of times.
Examples:
Input: A[] = [0, 1, 0], B[] = [2, 0, 0]
Output: 2
Explanation:
Modifying the array B[] to [2, 2, 2] makes every element in the array A[] strictly less than every element in the array B[].
Hence, the minimum number of changes required = 2.
Input: A[] = [0, 0, 1, 3, 3], B[] = [0, 2, 3]
Output: 3
Explanation:
Modifying the array B[] to [4, 4, 4] makes every element in the array A[] strictly less than every element in the array B[].
Hence, the minimum number of changes required = 3.
Approach: The idea to solve the given problem is to use two auxiliary arrays prefix_a[] and prefix_b[] of size 10, where prefix_a[i] and prefix_b[i] stores the number of elements in the array A[] ≤ i and the number of elements in the array B[] ≤ i respectively. Follow the steps below to solve the problem:
- Initialize two arrays, prefix_a[] and prefix_b[] of size 10 with {0}.
- Store the frequencies of every element in the arrays A[] and B[] in arrays prefix_a, and prefix_b respectively.
- Perform the prefix sum on the array prefix_a by iterating in the range [1, 9] using the variable i and update prefix_a[i] as (prefix[i] + prefix_a[i – 1]).
- Repeat the above step for the array prefix_b[].
- Iterate in the range [0, 9] using the variable i
- Store the number of operations to make every element in the array A[] strictly greater than the digit i and make every element in the array B[] less than digit i in a variable, say X.
- Initialize it with prefix_a[i] + M – prefix_b[i].
- Similarly, store the number of operations to make every element in the array B[] strictly greater than digit i and make every element in the array A[] less than digit i in a variable, say Y.
- Initialize it with prefix_b[i] + N – prefix_a[i].
- Update the overall minimum number of operations as the minimum of X and Y. Store the minimum obtained in a variable, say ans.
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void MinTime( int * a, int * b, int n, int m)
{
int ans = INT_MAX;
int prefix_a[10] = { 0 };
int prefix_b[10] = { 0 };
for ( int i = 0; i < n; i++) {
prefix_a[a[i]]++;
}
for ( int i = 0; i < m; i++) {
prefix_b[b[i]]++;
}
for ( int i = 1; i <= 9; i++) {
prefix_a[i] += prefix_a[i - 1];
}
for ( int i = 1; i <= 9; i++) {
prefix_b[i] += prefix_b[i - 1];
}
for ( int i = 0; i <= 9; i++) {
ans = min(ans, prefix_a[i] + m
- prefix_b[i]);
ans = min(ans, n - prefix_a[i]
+ prefix_b[i]);
}
cout << ans;
}
int main()
{
int A[] = { 0, 0, 1, 3, 3 };
int B[] = { 2, 0, 3 };
int N = sizeof (A) / sizeof (A[0]);
int M = sizeof (B) / sizeof (B[0]);
MinTime(A, B, N, M);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void MinTime( int []a, int []b, int n, int m)
{
int ans = 2147483647 ;
int []prefix_a = new int [ 10 ];
int []prefix_b = new int [ 10 ];
for ( int i = 0 ; i < n; i++) {
prefix_a[a[i]]++;
}
for ( int i = 0 ; i < m; i++) {
prefix_b[b[i]]++;
}
for ( int i = 1 ; i <= 9 ; i++) {
prefix_a[i] += prefix_a[i - 1 ];
}
for ( int i = 1 ; i <= 9 ; i++) {
prefix_b[i] += prefix_b[i - 1 ];
}
for ( int i = 0 ; i <= 9 ; i++) {
ans = Math.min(ans, prefix_a[i] + m
- prefix_b[i]);
ans = Math.min(ans, n - prefix_a[i]
+ prefix_b[i]);
}
System.out.println(ans);
}
public static void main(String [] args)
{
int []A = { 0 , 0 , 1 , 3 , 3 };
int []B = { 2 , 0 , 3 };
int N = A.length;
int M = B.length;
MinTime(A, B, N, M);
}
}
|
Python3
def MinTime(a, b, n, m):
ans = float ( 'inf' )
prefix_a = [ 0 ] * 10
prefix_b = [ 0 ] * 10
for i in range (n):
prefix_a[a[i]] + = 1
for i in range (m):
prefix_b[b[i]] + = 1
for i in range ( 1 , 10 ):
prefix_a[i] + = prefix_a[i - 1 ]
for i in range ( 1 , 10 ):
prefix_b[i] + = prefix_b[i - 1 ]
for i in range ( 1 , 10 ):
ans = min (ans, prefix_a[i] + m - prefix_b[i])
ans = min (ans, n - prefix_a[i] + prefix_b[i])
print (ans)
A = [ 0 , 0 , 1 , 3 , 3 ]
B = [ 2 , 0 , 3 ]
N = len (A)
M = len (B)
MinTime(A, B, N, M)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void MinTime( int []a, int []b, int n, int m)
{
int ans = 2147483647;
int []prefix_a = new int [10];
int []prefix_b = new int [10];
Array.Clear(prefix_a,0,prefix_a.Length);
Array.Clear(prefix_b,0,prefix_b.Length);
for ( int i = 0; i < n; i++) {
prefix_a[a[i]]++;
}
for ( int i = 0; i < m; i++) {
prefix_b[b[i]]++;
}
for ( int i = 1; i <= 9; i++) {
prefix_a[i] += prefix_a[i - 1];
}
for ( int i = 1; i <= 9; i++) {
prefix_b[i] += prefix_b[i - 1];
}
for ( int i = 0; i <= 9; i++) {
ans = Math.Min(ans, prefix_a[i] + m
- prefix_b[i]);
ans = Math.Min(ans, n - prefix_a[i]
+ prefix_b[i]);
}
Console.WriteLine(ans);
}
public static void Main()
{
int []A = { 0, 0, 1, 3, 3 };
int []B = { 2, 0, 3 };
int N = A.Length;
int M = B.Length;
MinTime(A, B, N, M);
}
}
|
Javascript
<script>
function Mletime(a, b, n, m)
{
let ans = 2147483647;
let prefix_a = [];
for (let i = 0; i < 10; i++) {
prefix_a[i] = 0;
}
let prefix_b = [];
for (let i = 0; i < 10; i++) {
prefix_b[i] = 0;
}
for (let i = 0; i < n; i++) {
prefix_a[a[i]]++;
}
for (let i = 0; i < m; i++) {
prefix_b[b[i]]++;
}
for (let i = 1; i <= 9; i++) {
prefix_a[i] += prefix_a[i - 1];
}
for (let i = 1; i <= 9; i++) {
prefix_b[i] += prefix_b[i - 1];
}
for (let i = 0; i <= 9; i++) {
ans = Math.min(ans, prefix_a[i] + m
- prefix_b[i]);
ans = Math.min(ans, n - prefix_a[i]
+ prefix_b[i]);
}
document.write(ans);
}
let A = [ 0, 0, 1, 3, 3 ];
let B = [ 2, 0, 3 ];
let N = A.length;
let M = B.length;
Mletime(A, B, N, M);
</script>
|
Time Complexity: O(N + M)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...