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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find greater elements
void findMaxElements(
int arr1[], int arr2[], int n)
{
// Index counter for arr1
int cnt1 = 0;
// Index counter for arr2
int cnt2 = 0;
// To store the maximum elements
int maxelements = 0;
while (cnt1 < n && cnt2 < n) {
// If element is greater,
// update maxelements and counters
// for both the arrays
if (arr1[cnt1] > arr2[cnt2]) {
maxelements++;
cnt1++;
cnt2++;
}
else {
cnt1++;
}
}
// Print the maximum elements
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
// Java program for the above approach
class Main{
// Function to find greater elements
static void findmaxelements(int arr1[], int arr2[], int n)
{
// Index counter for arr1
int cnt1 = 0;
// Index counter for arr1
int cnt2 = 0;
// To store the maximum elements
int maxelements = 0;
while(cnt1 < n && cnt2 < n)
{
// If element is greater,
// update maxelements and counters
// for both the arrays
if(arr1[cnt1] > arr2[cnt2])
{
maxelements++;
cnt1++;
cnt2++;
}
else
{
cnt1++;
}
}
// Print the maximum elements
System.out.println(maxelements);
}
// Driver Code
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);
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python3 program for the above approach
# Function to find greater elements
def findmaxelements(arr1, arr2, n):
# Index counter for arr1
cnt1 = 0
# Index counter for arr2
cnt2 = 0
# To store the maximum elements
maxelements = 0
# If element is greater,
# update maxelements and counters
# for both the arrays
while cnt1 < n and cnt2 < n :
if arr1[cnt1] > arr2[cnt2] :
maxelements += 1
cnt1 += 1
cnt2 += 1
else :
cnt1 += 1
# Print the maximum elements
print(maxelements)
# Driver Code
arr1 = [ 10, 15, 20, 25, 30, 35 ]
arr2 = [ 12, 14, 26, 32, 34, 40 ]
findmaxelements(arr1, arr2, len(arr1))
# This code is contributed by divyeshrabadiya07
C#
// C# program for the above approach
using System;
class GFG{
// Function to find greater elements
static void findmaxelements(int[] arr1,
int[] arr2, int n)
{
// Index counter for arr1
int cnt1 = 0;
// Index counter for arr1
int cnt2 = 0;
// To store the maximum elements
int maxelements = 0;
while(cnt1 < n && cnt2 < n)
{
// If element is greater, update
// maxelements and counters for
// both the arrays
if(arr1[cnt1] > arr2[cnt2])
{
maxelements++;
cnt1++;
cnt2++;
}
else
{
cnt1++;
}
}
// Print the maximum elements
Console.Write(maxelements);
}
// Driver Code
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);
}
}
// This code is contributed by rutvik_56
JavaScript
<script>
// Javascript program for the above approach
// Function to find greater elements
function findMaxElements( arr1, arr2, n)
{
// Index counter for arr1
var cnt1 = 0;
// Index counter for arr2
var cnt2 = 0;
// To store the maximum elements
var maxelements = 0;
while (cnt1 < n && cnt2 < n) {
// If element is greater,
// update maxelements and counters
// for both the arrays
if (arr1[cnt1] > arr2[cnt2]) {
maxelements++;
cnt1++;
cnt2++;
}
else {
cnt1++;
}
}
// Print the maximum elements
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);
// This code is contributed by rrrtnx.
</script>
Time Complexity: O(N), where N is the length of the array.
Auxiliary Space: O(1)
Similar Reads
Count elements in first Array with absolute difference greater than K with an element in second Array Given two arrays arr1[] and arr2[] and an integer K, our task is to find the number elements in the first array, for an element x, in arr1[], there exists at least one element y, in arr2[] such that absolute difference of x and y is greater than the integer K. Examples: Input: arr1 = {3, 1, 4}, arr2
7 min read
Count distinct elements after adding each element of First Array with Second Array Given two arrays arr1[] and arr2[]. We can generate another array arr3[] by adding each element of the array arr1[] to each element arr2[]. The task is to find the count of distinct element in the array arr3[]. Examples: Input: Arr1[] = {1, 2}, Arr2[] = {3, 4}, MAX = 4 Output: 4 -> 1 5 -> 2 6
15+ min read
Count of array elements which are greater than all elements on its left Given an array arr[] of size n, the task is to count the number of array elements such that all the elements to its left are strictly smaller than it.Note: The first element of the array will be considered to be always satisfying the condition.Examples :Input: arr[] = [2, 4, 5, 6]Output: 4Explanatio
8 min read
Find just strictly greater element from first array for each element in second array Given two arrays A[] and B[] containing N elements, the task is to find, for every element in the array B[], the element which is just strictly greater than that element which is present in the array A[]. If no value is present, then print 'null'. Note: The value from the array A[] can only be used
10 min read
For each element in 1st array count elements less than or equal to it in 2nd array | Set 2 Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Examples: Input : arr1[] = [1, 2, 3, 4, 7, 9] arr2[] = [0, 1, 2, 1, 1, 4] Output : [4, 5, 5, 6, 6, 6] Explanation: There are 4 elements less t
13 min read