Maximize equal elements in two Arrays after at most K increments
Last Updated :
30 Jan, 2023
Given two arrays arr1[] and arr2[] of length N each and an integer K, The task is to maximize the number of equal elements at the same index in arr1[] and arr2[] by incrementing any element of arr2[] but the total increment must be at most K.
Examples:
Input: arr1[] = {4, 5, 6, 7}, arr2[] = {3, 4, 5, 7}, K = 3
Output: 4
Explanation: In arr2[] increment the element at index 0 once to make it 4.
Then increment the element at index 1 once to make it 5.
Then increment the element at index 2 once to make it 6.
Total increments performed = 1 + 1 + 1 = 3.
Now both the array contains elements {4, 5, 6, 7}.
Input2: arr1[]={5, 2, 5}, arr2[]={1, 1, 4}, K = 2
Output: 2
Approach: The problem can be solved based on the following idea:
We should perform minimum increment operation in each element to maximize the number of the same elements after at most K increments. To find the minimum increment for each element, we should sort difference between same indexed elementsof the two arrays and perform the increment operations accordingly.
Follow the steps mentioned below to implement the idea:
- Iterate from i = 0 to N-1:
- Calculate the difference between the element of arr2[i] and arr1[i].
- Sort the difference array.
- Iterate from i = 0 to N-1:
- Check if this much difference is possible.
- If possible, decrease the value of K by that much and also make the difference 0.
- The total number of elements having a difference of 0 is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxStrength(vector< int >& arr1,
vector< int >& arr2, int k, int n)
{
for ( int i = 0; i < n; i++) {
arr1[i] -= arr2[i];
}
sort(arr1.begin(), arr1.end());
int count = 0;
for ( int i = 0; i < n; i++) {
if (arr1[i] == 0) {
count++;
}
else if (arr1[i] <= k and arr1[i]>0) {
count++;
k -= arr1[i];
}
else {
break ;
}
}
return count;
}
int main()
{
int N = 4;
vector< int > arr1{ 4, 5, 6, 7 };
vector< int > arr2{ 3, 4, 5, 7 };
int K = 3;
cout << maxStrength(arr1, arr2, K, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int maxStrength( int [] arr1, int [] arr2, int k, int n)
{
for ( int i = 0 ; i < n; i++) {
arr1[i] -= arr2[i];
}
Arrays.sort(arr1);
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr1[i] == 0 ) {
count++;
}
else if (arr1[i] <= k && arr1[i]> 0 ) {
count++;
k -= arr1[i];
} else {
break ;
}
}
return count;
}
public static void main(String[] args) {
int N = 4 ;
int [] arr1 = { 4 , 5 , 6 , 7 };
int arr2[] = { 3 , 4 , 5 , 7 };
int K = 3 ;
System.out.print(maxStrength(arr1, arr2, K, N));
}
}
|
Python3
def maxStrength(arr1, arr2, k, n):
for i in range (n):
arr1[i] - = arr2[i]
arr1.sort()
count = 0
for i in range (n):
if (arr1[i] = = 0 ):
count + = 1
elif (arr1[i] < = k and arr1[i]> 0 ):
count + = 1
k - = arr1[i]
else :
break
return count
N = 4
arr1 = [ 4 , 5 , 6 , 7 ]
arr2 = [ 3 , 4 , 5 , 7 ]
K = 3
print (maxStrength(arr1, arr2, K, N))
|
C#
using System;
public class GFG {
static int maxStrength( int [] arr1, int [] arr2, int k, int n)
{
for ( int i = 0; i < n; i++) {
arr1[i] -= arr2[i];
}
Array.Sort(arr1);
int count = 0;
for ( int i = 0; i < n; i++) {
if (arr1[i] == 0) {
count++;
}
else if (arr1[i] <= k && arr1[i]>0) {
count++;
k -= arr1[i];
} else {
break ;
}
}
return count;
}
public static void Main(String[] args) {
int N = 4;
int [] arr1 = { 4, 5, 6, 7 };
int []arr2 = { 3, 4, 5, 7 };
int K = 3;
Console.Write(maxStrength(arr1, arr2, K, N));
}
}
|
Javascript
<script>
function maxStrength(arr1,arr2, k, n)
{
for (let i = 0; i < n; i++) {
arr1[i] -= arr2[i];
}
arr1.sort( function (a, b){ return a - b});
let count = 0;
for (let i = 0; i < n; i++) {
if (arr1[i] == 0) {
count++;
}
else if (arr1[i] <= k && arr1[i]>0) {
count++;
k -= arr1[i];
}
else {
break ;
}
}
return count;
}
let N = 4;
let arr1=[ 4, 5, 6, 7 ];
let arr2=[ 3, 4, 5, 7 ];
let K = 3;
document.write(maxStrength(arr1, arr2, K, N));
</script>
|
Time Complexity: O(N * logN)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...