Count of elements in first Array greater than second Array with each element considered only once
Last Updated :
09 Jan, 2023
Given two sorted array of size N. The task is to find the maximum number of elements in the first array which are strictly greater than the elements of the second array such that an element can be considered only once.
Examples:
Input: arr1[] = { 20, 30, 50 }, arr2[] = { 25, 40, 60 }
Output: 2
Explanation:
Maximum 2 elements 30 (30 > 25) and 50 (50 > 40) of array arr1 is greater than arr2.
Input: arr1[] = { 10, 15, 20, 25, 30, 35 }, arr2[] = { 12, 14, 26, 32, 34, 40 }
Output: 4
Explanation:
Maximum 4 elements 15 (15 > 12), 20 (20 > 14), 30 (30 > 26) and 35 (35 > 34) of arr1 is greater than arr2.
Approach:
- Compare the elements of both the arrays from index 0 one by one.
- If the element at the index of arr1 is greater than the element at the index of arr2 then increase the answer and the index of both arrays by 1.
- If the element at the index of arr1 is lesser or equal to the element at the index of arr2 then
increase the index of arr1.
- Repeat the above steps until any array’s index reaches to the last element.
- Print the answer
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMaxElements(
int arr1[], int arr2[], int n)
{
int cnt1 = 0;
int cnt2 = 0;
int maxelements = 0;
while (cnt1 < n && cnt2 < n) {
if (arr1[cnt1] > arr2[cnt2]) {
maxelements++;
cnt1++;
cnt2++;
}
else {
cnt1++;
}
}
cout << maxelements << endl;
}
int main()
{
int arr1[] = { 10, 15, 20, 25, 30, 35 };
int arr2[] = { 12, 14, 26, 32, 34, 40 };
int n = sizeof (arr1) / sizeof (arr1[0]);
findMaxElements(arr1, arr2, n);
return 0;
}
|
Java
class Main{
static void findmaxelements( int arr1[], int arr2[], int n)
{
int cnt1 = 0 ;
int cnt2 = 0 ;
int maxelements = 0 ;
while (cnt1 < n && cnt2 < n)
{
if (arr1[cnt1] > arr2[cnt2])
{
maxelements++;
cnt1++;
cnt2++;
}
else
{
cnt1++;
}
}
System.out.println(maxelements);
}
public static void main(String[] args)
{
int arr1[] = { 10 , 15 , 20 , 25 , 30 , 35 };
int arr2[] = { 12 , 14 , 26 , 32 , 34 , 40 };
findmaxelements(arr1, arr2, arr1.length);
}
}
|
Python3
def findmaxelements(arr1, arr2, n):
cnt1 = 0
cnt2 = 0
maxelements = 0
while cnt1 < n and cnt2 < n :
if arr1[cnt1] > arr2[cnt2] :
maxelements + = 1
cnt1 + = 1
cnt2 + = 1
else :
cnt1 + = 1
print (maxelements)
arr1 = [ 10 , 15 , 20 , 25 , 30 , 35 ]
arr2 = [ 12 , 14 , 26 , 32 , 34 , 40 ]
findmaxelements(arr1, arr2, len (arr1))
|
C#
using System;
class GFG{
static void findmaxelements( int [] arr1,
int [] arr2, int n)
{
int cnt1 = 0;
int cnt2 = 0;
int maxelements = 0;
while (cnt1 < n && cnt2 < n)
{
if (arr1[cnt1] > arr2[cnt2])
{
maxelements++;
cnt1++;
cnt2++;
}
else
{
cnt1++;
}
}
Console.Write(maxelements);
}
static public void Main( string [] args)
{
int [] arr1 = { 10, 15, 20, 25, 30, 35 };
int [] arr2 = { 12, 14, 26, 32, 34, 40 };
findmaxelements(arr1, arr2, arr1.Length);
}
}
|
Javascript
<script>
function findMaxElements( arr1, arr2, n)
{
var cnt1 = 0;
var cnt2 = 0;
var maxelements = 0;
while (cnt1 < n && cnt2 < n) {
if (arr1[cnt1] > arr2[cnt2]) {
maxelements++;
cnt1++;
cnt2++;
}
else {
cnt1++;
}
}
document.write( maxelements );
}
var arr1 = [10, 15, 20, 25, 30, 35];
var arr2 = [12, 14, 26, 32, 34, 40];
var n = arr1.length;
findMaxElements(arr1, arr2, n);
</script>
|
Time Complexity: O(N), where N is the length of the array.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...