For each in 1st array count less than or equal to it in 2nd array
Last Updated :
23 Apr, 2025
You are given two unsorted arrays a[]
and b[]
. Both arrays may contain duplicate elements. For each element in a[]
, your task is to count how many elements in b[]
are less than or equal to that element.
Examples:
Input: a[] = [1, 2, 3, 4, 7, 9], b = [0, 1, 2, 1, 1, 4]
Output: [4, 5, 5, 6, 6, 6]
Explanation:
For a[0] = 1, there are 4 elements in b (0, 1, 1, 1) that are ≤ 1.
For a[1] = 2, there are 5 elements in b (0, 1, 1, 1, 2) that are ≤ 2.
For a[2] = 3, there are 5 elements in b that are ≤ 3.
Similarly, for a[3] = 4, there are 6 elements in b that are ≤ 4, and for a[4] = 7 and a[5] = 9, there are also 6 elements in b that are ≤ 7 and ≤ 9, respectively.
Input: a[] = [4, 8, 7, 5, 1], b = [4, 48, 3, 0, 1, 1, 5]
Output: [5, 6, 6, 6, 3]
Explanation:
For a[0] = 4, there are 5 elements in b (4, 3, 0, 1, 1) that are ≤ 4.
For a[1] = 8 and a[2] = 7, there are 6 elements in b that are ≤ 8 and ≤ 7.
For a[3] = 5, there are 6 elements in b that are ≤ 5.
For a[4] = 1, there are 3 elements in b (0, 1, 1) that are ≤ 1.
[Naive Approach] - Using Nested Loops - O(n * m) Time and O(n) Space
The idea is to use two loops, the outer loop for elements of array a[] and an inner loop for elements of array b[]. Then for each element of a[], count elements less than or equal to it in b[].
Follow the below given step by step approach:
- Traverse through the elements of the first array from start to end.
- For every element in the first array.
- Traverse through the elements in the second array and find the count of elements that are less than or equal to the element of the first array.
- Print the count for every index.
Below is given the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
vector<int> countEleLessThanOrEqual
(vector<int>& a, vector<int>& b) {
int n = a.size(), m = b.size();
// to store the result
vector<int> res(n);
// outer loop for each element of a[]
for (int i = 0; i < n; i++) {
int count = 0;
// inner loop for each element of b[]
for (int j = 0; j < m; j++)
if (b[j] <= a[i])
count++;
res[i] = count;
}
return res;
}
int main() {
vector<int> a = {1, 2, 3, 4, 7, 9};
vector<int> b = {0, 1, 2, 1, 1, 4};
vector<int> result = countEleLessThanOrEqual(a, b);
for (int i : result) {
cout << i << " ";
}
return 0;
}
Java
import java.util.*;
class GfG {
// to store the result
public static ArrayList<Integer>
countEleLessThanOrEqual(int[] a, int[] b) {
int n = a.length, m = b.length;
// to store the result
ArrayList<Integer> res = new ArrayList<>();
// outer loop for each element of a[]
for (int i = 0; i < n; i++) {
int count = 0;
// inner loop for each element of b[]
for (int j = 0; j < m; j++)
if (b[j] <= a[i])
count++;
res.add(count);
}
return res;
}
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 7, 9};
int[] b = {0, 1, 2, 1, 1, 4};
ArrayList<Integer> result = countEleLessThanOrEqual(a, b);
for (int i : result) {
System.out.print(i + " ");
}
}
}
Python
# to store the result
def countEleLessThanOrEqual(a, b):
n = len(a)
m = len(b)
# to store the result
res = [0] * n
# outer loop for each element of a[]
for i in range(n):
count = 0
# inner loop for each element of b[]
for j in range(m):
if b[j] <= a[i]:
count += 1
res[i] = count
return res
a = [1, 2, 3, 4, 7, 9]
b = [0, 1, 2, 1, 1, 4]
result = countEleLessThanOrEqual(a, b)
for i in result:
print(i, end=" ")
C#
using System;
using System.Collections.Generic;
class GfG {
// to store the result
public static List<int>
countEleLessThanOrEqual(int[] a, int[] b) {
int n = a.Length, m = b.Length;
// to store the result
List<int> res = new List<int>();
// outer loop for each element of a[]
for (int i = 0; i < n; i++) {
int count = 0;
// inner loop for each element of b[]
for (int j = 0; j < m; j++)
if (b[j] <= a[i])
count++;
res.Add(count);
}
return res;
}
public static void Main(string[] args) {
int[] a = {1, 2, 3, 4, 7, 9};
int[] b = {0, 1, 2, 1, 1, 4};
List<int> result = countEleLessThanOrEqual(a, b);
foreach (int i in result) {
Console.Write(i + " ");
}
}
}
JavaScript
// to store the result
function countEleLessThanOrEqual(a, b) {
let n = a.length, m = b.length;
// to store the result
let res = new Array(n);
// outer loop for each element of a[]
for (let i = 0; i < n; i++) {
let count = 0;
// inner loop for each element of b[]
for (let j = 0; j < m; j++)
if (b[j] <= a[i])
count++;
res[i] = count;
}
return res;
}
let a = [1, 2, 3, 4, 7, 9];
let b = [0, 1, 2, 1, 1, 4];
let result = countEleLessThanOrEqual(a, b);
console.log(result.join(" "));
[Better Approach - 1] - Using Sorting - O((n + m) * log m) Time and O(n) Space
The idea is to sort the elements of array b[], then perform a modified binary search on array b[]. For each element x of array a[], find the last index of the largest element smaller than or equal to x in sorted array b[]. The index of the largest element will give the count of elements.
Below is given the step by step process:
- Sort the second array.
- Traverse through the elements of the first array from start to end.
- For every element in the first array.
- Do a binary search on the second array and find the index of the largest element smaller than or equal to the element of the first array.
- The index of the largest element will give the count of elements. Print the count for every index.
Below is given the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
// to perform the binary search
int binarySearch(vector<int> &arr, int x) {
int low = 0, high = arr.size() - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] <= x)
low = mid + 1;
else
high = mid - 1;
}
return low;
}
vector<int> countEleLessThanOrEqual
(vector<int>& a, vector<int>& b) {
int n = a.size(), m = b.size();
// to store the result
vector<int> res(n);
// sort the array b[]
sort(b.begin(), b.end());
// outer loop for each element of a[]
for (int i = 0; i < n; i++) {
res[i] = binarySearch(b, a[i]);
}
return res;
}
int main() {
vector<int> a = {1, 2, 3, 4, 7, 9};
vector<int> b = {0, 1, 2, 1, 1, 4};
vector<int> result = countEleLessThanOrEqual(a, b);
for (int i : result) {
cout << i << " ";
}
return 0;
}
Java
import java.util.*;
class GfG {
// to perform the binary search
static int binarySearch(int[] arr, int x) {
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] <= x)
low = mid + 1;
else
high = mid - 1;
}
return low;
}
// to store the result
static ArrayList<Integer>
countEleLessThanOrEqual(int[] a, int[] b) {
int n = a.length, m = b.length;
// to store the result
ArrayList<Integer> res = new ArrayList<>();
// sort the array b[]
Arrays.sort(b);
// outer loop for each element of a[]
for (int i = 0; i < n; i++) {
res.add(binarySearch(b, a[i]));
}
return res;
}
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 7, 9};
int[] b = {0, 1, 2, 1, 1, 4};
ArrayList<Integer> result = countEleLessThanOrEqual(a, b);
for (int i : result) {
System.out.print(i + " ");
}
}
}
Python
# to perform the binary search
def binarySearch(arr, x):
low = 0
high = len(arr) - 1
while low <= high:
mid = low + (high - low) // 2
if arr[mid] <= x:
low = mid + 1
else:
high = mid - 1
return low
# to store the result
def countEleLessThanOrEqual(a, b):
n = len(a)
m = len(b)
# to store the result
res = [0] * n
# sort the array b[]
b.sort()
# outer loop for each element of a[]
for i in range(n):
res[i] = binarySearch(b, a[i])
return res
a = [1, 2, 3, 4, 7, 9]
b = [0, 1, 2, 1, 1, 4]
result = countEleLessThanOrEqual(a, b)
for i in result:
print(i, end=" ")
C#
using System;
using System.Collections.Generic;
class GfG {
// to perform the binary search
static int binarySearch(int[] arr, int x) {
int low = 0, high = arr.Length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] <= x)
low = mid + 1;
else
high = mid - 1;
}
return low;
}
// to store the result
static List<int>
countEleLessThanOrEqual(int[] a, int[] b) {
int n = a.Length, m = b.Length;
// to store the result
List<int> res = new List<int>();
// sort the array b[]
Array.Sort(b);
// outer loop for each element of a[]
for (int i = 0; i < n; i++) {
res.Add(binarySearch(b, a[i]));
}
return res;
}
public static void Main(string[] args) {
int[] a = {1, 2, 3, 4, 7, 9};
int[] b = {0, 1, 2, 1, 1, 4};
List<int> result = countEleLessThanOrEqual(a, b);
foreach (int i in result) {
Console.Write(i + " ");
}
}
}
JavaScript
// to perform the binary search
function binarySearch(arr, x) {
let low = 0, high = arr.length - 1;
while (low <= high) {
let mid = low + Math.floor((high - low) / 2);
if (arr[mid] <= x)
low = mid + 1;
else
high = mid - 1;
}
return low;
}
// to store the result
function countEleLessThanOrEqual(a, b) {
let n = a.length, m = b.length;
// to store the result
let res = new Array(n);
// sort the array b[]
b.sort((x, y) => x - y);
// outer loop for each element of a[]
for (let i = 0; i < n; i++) {
res[i] = binarySearch(b, a[i]);
}
return res;
}
let a = [1, 2, 3, 4, 7, 9];
let b = [0, 1, 2, 1, 1, 4];
let result = countEleLessThanOrEqual(a, b);
console.log(result.join(" "));
[Better Approach - 2] - Using Inbuilt Function - O((n + m) * log m) Time and O(n) Space
Instead of implementing the binary search in the above approach, we can use inbuilt function upper bound in C++ (and bisect_right in Python) that returns the first index ind in the array where the value arr[ind] > target.
C++
#include <bits/stdc++.h>
using namespace std;
vector<int> countEleLessThanOrEqual
(vector<int>& a, vector<int>& b) {
int n = a.size(), m = b.size();
// to store the result
vector<int> res(n);
// sort the array b[]
sort(b.begin(), b.end());
// outer loop for each element of a[]
for (int i = 0; i < n; i++) {
res[i] = upper_bound(b.begin(),
b.end(), a[i]) - b.begin();
}
return res;
}
int main() {
vector<int> a = {1, 2, 3, 4, 7, 9};
vector<int> b = {0, 1, 2, 1, 1, 4};
vector<int> result = countEleLessThanOrEqual(a, b);
for (int i : result) {
cout << i << " ";
}
return 0;
}
Java
import java.util.*;
class GfG {
// to perform upper_bound functionality
static int upperBound(int[] arr, int x) {
int low = 0, high = arr.length;
while (low < high) {
int mid = low + (high - low) / 2;
if (arr[mid] <= x)
low = mid + 1;
else
high = mid;
}
return low;
}
// to store the result
public static ArrayList<Integer>
countEleLessThanOrEqual(int[] a, int[] b) {
int n = a.length, m = b.length;
// sort the array b[]
Arrays.sort(b);
// outer loop for each element of a[]
ArrayList<Integer> res = new ArrayList<>();
for (int i = 0; i < n; i++) {
res.add(upperBound(b, a[i]));
}
return res;
}
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 7, 9};
int[] b = {0, 1, 2, 1, 1, 4};
ArrayList<Integer> result =
countEleLessThanOrEqual(a, b);
for (int i : result) {
System.out.print(i + " ");
}
}
}
Python
# to store the result
def countEleLessThanOrEqual(a, b):
n = len(a)
m = len(b)
# sort the array b[]
b.sort()
# to store the result
res = [0] * n
# outer loop for each element of a[]
for i in range(n):
# bisect_right is equivalent to upper_bound
import bisect
res[i] = bisect.bisect_right(b, a[i])
return res
a = [1, 2, 3, 4, 7, 9]
b = [0, 1, 2, 1, 1, 4]
result = countEleLessThanOrEqual(a, b)
for i in result:
print(i, end=" ")
C#
using System;
using System.Collections.Generic;
class GfG {
// to perform upper_bound functionality
static int UpperBound(int[] arr, int x) {
int low = 0, high = arr.Length;
while (low < high) {
int mid = low + (high - low) / 2;
if (arr[mid] <= x)
low = mid + 1;
else
high = mid;
}
return low;
}
// to store the result
public static List<int>
countEleLessThanOrEqual(int[] a, int[] b) {
int n = a.Length, m = b.Length;
// sort the array b[]
Array.Sort(b);
// outer loop for each element of a[]
List<int> res = new List<int>();
for (int i = 0; i < n; i++) {
res.Add(UpperBound(b, a[i]));
}
return res;
}
public static void Main(string[] args) {
int[] a = {1, 2, 3, 4, 7, 9};
int[] b = {0, 1, 2, 1, 1, 4};
List<int> result = countEleLessThanOrEqual(a, b);
foreach (int i in result) {
Console.Write(i + " ");
}
}
}
JavaScript
// to perform upper_bound functionality
function upperBound(arr, x) {
let low = 0, high = arr.length;
while (low < high) {
let mid = low + Math.floor((high - low) / 2);
if (arr[mid] <= x)
low = mid + 1;
else
high = mid;
}
return low;
}
// to store the result
function countEleLessThanOrEqual(a, b) {
let n = a.length, m = b.length;
// sort the array b[]
b.sort((x, y) => x - y);
// outer loop for each element of a[]
let res = new Array(n);
for (let i = 0; i < n; i++) {
res[i] = upperBound(b, a[i]);
}
return res;
}
let a = [1, 2, 3, 4, 7, 9];
let b = [0, 1, 2, 1, 1, 4];
let result = countEleLessThanOrEqual(a, b);
console.log(result.join(" "));
[Better Approach - 3] - Using Sorting and Two Pointers - O(n*logn + m*logm) Time and O(n) Space
The idea is to sort both the arrays and put pointers i and j for array a[] and b[] at the beginning. Then traverse over the elements of a by using i pointer & inside this loop, we will go over each element of b by j pointer. Wherever arr2[j] <= arr1[i] we will increase j . if the condition fails store j.
Note:- This method will return the value in sorted array, thus can be used for sorted arrays.
C++
#include <bits/stdc++.h>
using namespace std;
vector<int> countEleLessThanOrEqual(vector<int>& a, vector<int>& b) {
int n = a.size(), m = b.size();
// to store the result
vector<int> res(n);
// sort both the arrays
sort(a.begin(), a.end());
sort(b.begin(), b.end());
int ind = 0;
// outer loop for each element of a[]
for (int i = 0; i < n; i++) {
// inner loop for each element of b[]
while (ind < m && b[ind] <= a[i]) {
ind++;
}
res[i] = ind;
}
return res;
}
int main() {
vector<int> a = {1, 2, 3, 4, 7, 9};
vector<int> b = {0, 1, 2, 1, 1, 4};
vector<int> result = countEleLessThanOrEqual(a, b);
for (int i : result) {
cout << i << " ";
}
return 0;
}
Java
import java.util.*;
class GfG {
// to store the result
public static ArrayList<Integer>
countEleLessThanOrEqual(int[] a, int[] b) {
int n = a.length, m = b.length;
// to store the result
ArrayList<Integer> res = new ArrayList<>();
for (int i = 0; i < n; i++) {
res.add(0);
}
// sort both the arrays
Arrays.sort(a);
Arrays.sort(b);
int ind = 0;
// outer loop for each element of a[]
for (int i = 0; i < n; i++) {
// inner loop for each element of b[]
while (ind < m && b[ind] <= a[i]) {
ind++;
}
res.set(i, ind);
}
return res;
}
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 7, 9};
int[] b = {0, 1, 2, 1, 1, 4};
ArrayList<Integer> result = countEleLessThanOrEqual(a, b);
for (int i : result) {
System.out.print(i + " ");
}
}
}
Python
# to store the result
def countEleLessThanOrEqual(a, b):
n = len(a)
m = len(b)
# to store the result
res = [0] * n
# sort both the arrays
a.sort()
b.sort()
ind = 0
# outer loop for each element of a[]
for i in range(n):
# inner loop for each element of b[]
while ind < m and b[ind] <= a[i]:
ind += 1
res[i] = ind
return res
a = [1, 2, 3, 4, 7, 9]
b = [0, 1, 2, 1, 1, 4]
result = countEleLessThanOrEqual(a, b)
for i in result:
print(i, end=" ")
C#
using System;
using System.Collections.Generic;
class GfG {
// to store the result
public static List<int>
countEleLessThanOrEqual(int[] a, int[] b) {
int n = a.Length, m = b.Length;
// to store the result
List<int> res = new List<int>();
for (int i = 0; i < n; i++) {
res.Add(0);
}
// sort both the arrays
Array.Sort(a);
Array.Sort(b);
int ind = 0;
// outer loop for each element of a[]
for (int i = 0; i < n; i++) {
// inner loop for each element of b[]
while (ind < m && b[ind] <= a[i]) {
ind++;
}
res[i] = ind;
}
return res;
}
public static void Main(string[] args) {
int[] a = {1, 2, 3, 4, 7, 9};
int[] b = {0, 1, 2, 1, 1, 4};
List<int> result = countEleLessThanOrEqual(a, b);
foreach (int i in result) {
Console.Write(i + " ");
}
}
}
JavaScript
// to store the result
function countEleLessThanOrEqual(a, b) {
let n = a.length, m = b.length;
// to store the result
let res = new Array(n).fill(0);
// sort both the arrays
a.sort((x, y) => x - y);
b.sort((x, y) => x - y);
let ind = 0;
// outer loop for each element of a[]
for (let i = 0; i < n; i++) {
// inner loop for each element of b[]
while (ind < m && b[ind] <= a[i]) {
ind++;
}
res[i] = ind;
}
return res;
}
let a = [1, 2, 3, 4, 7, 9];
let b = [0, 1, 2, 1, 1, 4];
let result = countEleLessThanOrEqual(a, b);
console.log(result.join(" "));
[Expected Approach for Small Range] - Using Count Array and Prefix Sum - O(n + m) Time and O(n + m) Space
The idea is to create hash table cnt[] of size 10^5 to store the frequency of elements in array b[] (considering the max value in both the arrays <= 10^5). Now, create the prefix array of the hash table, then every element cnt[ind] will store the number of elements in range [0, ind]. Now for every element a[i], the value cnt[a[i]] stores the required answer.
Follow the steps to implement the approach:
- Create a hash table to store the frequency of elements in b[]. Initialize the hash table with zeros.
- Iterate through b[] and update the frequency of each element in the hash table.
- Create a prefix sum array cnt[] of size 100010 (maximum element value + 10). Initialize cnt[0] with the frequency of the first element in b[].
- Calculate the prefix sum array by adding the current frequency with the previous prefix sum.
- Iterate through a. For each element a[i], access cnt[a[i]] to get the count of elements less than or equal to a[i] in b.
- Store the counts in a result array or directly print them.
Below is given the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
vector<int> countEleLessThanOrEqual
(vector<int>& a, vector<int>& b) {
int n = a.size(), m = b.size();
// to store the result
vector<int> res(n);
// to store frequency of elements in b[]
vector<int> cnt(100001, 0);
for (int i = 0; i < m; i++) {
cnt[b[i]]++;
}
// transform cnt[] to prefix sum array
for (int i = 1; i < 100001; i++) {
cnt[i] += cnt[i - 1];
}
// loop for each element of a[]
for (int i = 0; i < n; i++) {
res[i] = cnt[a[i]];
}
return res;
}
int main() {
vector<int> a = {1, 2, 3, 4, 7, 9};
vector<int> b = {0, 1, 2, 1, 1, 4};
vector<int> result = countEleLessThanOrEqual(a, b);
for (int i : result) {
cout << i << " ";
}
return 0;
}
Java
import java.util.*;
class GfG {
// to store the result
public static ArrayList<Integer>
countEleLessThanOrEqual(int[] a, int[] b) {
int n = a.length, m = b.length;
// to store the result
ArrayList<Integer> res = new ArrayList<>();
for (int i = 0; i < n; i++) {
res.add(0);
}
// to store frequency of elements in b[]
int[] cnt = new int[100001];
for (int i = 0; i < 100001; i++) {
cnt[i] = 0;
}
for (int i = 0; i < m; i++) {
cnt[b[i]]++;
}
// transform cnt[] to prefix sum array
for (int i = 1; i < 100001; i++) {
cnt[i] += cnt[i - 1];
}
// loop for each element of a[]
for (int i = 0; i < n; i++) {
res.set(i, cnt[a[i]]);
}
return res;
}
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 7, 9};
int[] b = {0, 1, 2, 1, 1, 4};
ArrayList<Integer> result = countEleLessThanOrEqual(a, b);
for (int i : result) {
System.out.print(i + " ");
}
}
}
Python
# to store the result
def countEleLessThanOrEqual(a, b):
n = len(a)
m = len(b)
# to store the result
res = [0] * n
# to store frequency of elements in b[]
cnt = [0] * 100001
for i in range(m):
cnt[b[i]] += 1
# transform cnt[] to prefix sum array
for i in range(1, 100001):
cnt[i] += cnt[i - 1]
# loop for each element of a[]
for i in range(n):
res[i] = cnt[a[i]]
return res
a = [1, 2, 3, 4, 7, 9]
b = [0, 1, 2, 1, 1, 4]
result = countEleLessThanOrEqual(a, b)
for i in result:
print(i, end=" ")
C#
using System;
using System.Collections.Generic;
class GfG {
// to store the result
public static List<int>
countEleLessThanOrEqual(int[] a, int[] b) {
int n = a.Length, m = b.Length;
// to store the result
List<int> res = new List<int>();
for (int i = 0; i < n; i++) {
res.Add(0);
}
// to store frequency of elements in b[]
int[] cnt = new int[100001];
for (int i = 0; i < 100001; i++) {
cnt[i] = 0;
}
for (int i = 0; i < m; i++) {
cnt[b[i]]++;
}
// transform cnt[] to prefix sum array
for (int i = 1; i < 100001; i++) {
cnt[i] += cnt[i - 1];
}
// loop for each element of a[]
for (int i = 0; i < n; i++) {
res[i] = cnt[a[i]];
}
return res;
}
public static void Main(string[] args) {
int[] a = {1, 2, 3, 4, 7, 9};
int[] b = {0, 1, 2, 1, 1, 4};
List<int> result = countEleLessThanOrEqual(a, b);
foreach (int i in result) {
Console.Write(i + " ");
}
}
}
JavaScript
// to store the result
function countEleLessThanOrEqual(a, b) {
let n = a.length, m = b.length;
// to store the result
let res = new Array(n).fill(0);
// to store frequency of elements in b[]
let cnt = new Array(100001).fill(0);
for (let i = 0; i < m; i++) {
cnt[b[i]]++;
}
// transform cnt[] to prefix sum array
for (let i = 1; i < 100001; i++) {
cnt[i] += cnt[i - 1];
}
// loop for each element of a[]
for (let i = 0; i < n; i++) {
res[i] = cnt[a[i]];
}
return res;
}
let a = [1, 2, 3, 4, 7, 9];
let b = [0, 1, 2, 1, 1, 4];
let result = countEleLessThanOrEqual(a, b);
console.log(result.join(" "));
Similar Reads
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
Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
Given two unsorted arrays arr1[] and arr2[], the task is to find the sum of elements of arr1[] such that the number of elements less than or equal to them in arr2[] is maximum. Examples: Input: arr1[] = {1, 2, 3, 4, 7, 9}, arr2[] = {0, 1, 2, 1, 1, 4} Output: 20 Below table shows the count of element
15 min read
Count pairs in an array that hold i*arr[i] > j*arr[j]
Given an array of integers arr[0..n-1], count all pairs (arr[i], arr[j]) in the such that i*arr[i] > j*arr[j], 0 =< i < j < n. Examples : Input : arr[] = {5 , 0, 10, 2, 4, 1, 6} Output: 5 Pairs which hold condition i*arr[i] > j*arr[j] are (10, 2) (10, 4) (10, 1) (2, 1) (4, 1) Input :
15+ min read
Count of pairs having each element equal to index of the other from an Array
Given an integer N and an array arr[] that contains elements in the range [1, N], the task is to find the count of all pairs (arr[i], arr[j]) such that i < j and i == arr[j] and j == arr[i]. Examples: Input: N = 4, arr[] = {2, 1, 4, 3} Output: 2 Explanation: All possible pairs are {1, 2} and {3,
6 min read
Count of pairs (arr[i], arr[j]) such that arr[i] + j and arr[j] + i are equal
Given an array arr[], the task is to count pairs i, j such that, i < j and arr[i] + j = arr[j] + i. Examples: Input: arr[] = {4, 1, 2, 3}Output: 3Explanation: In total three pairs are satisfying the given condition those are {1, 2}, {2, 3} and {1, 3}.So, the final answer is 3. Input: arr[] = {1,
5 min read
Count of equal value pairs from given two Arrays such that a[i] equals b[j]
Given two arrays a[] and b[] of length N and M respectively, sorted in non-decreasing order. The task is to find the number of pairs (i, j) such that, a[i] equals b[j]. Examples: Input: a[] = {1, 1, 3, 3, 3, 5, 8, 8}, b[] = {1, 3, 3, 4, 5, 5, 5}Output: 11Explanation: Following are the 11 pairs with
14 min read
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 pairs from two arrays having sum equal to K
Given an integer K and two arrays A1 and A2, the task is to return the total number of pairs (one element from A1 and one element from A2) with a sum equal to K. Note: Arrays can have duplicate elements. We consider every pair as different, the only constraint is, an element (of any array) can parti
6 min read
Count sub-arrays which have elements less than or equal to X
Given an array of n elements and an integer X. Count the number of sub-arrays of this array which have all elements less than or equal to X. Examples: Input : arr[] = {1, 5, 7, 8, 2, 3, 9} X = 6 Output : 6 Explanation : Sub-arrays are {1}, {5}, {2}, {3}, {1, 5}, {2, 3} Input : arr[] = {1, 10, 12, 4,
15 min read
Count pairs in given Array having sum of index and value at that index equal
Given an array arr[] containing positive integers, count the total number of pairs for which arr[i]+i = arr[j]+j such that 0â¤i<jâ¤n-1. Examples: Input: arr[] = { 6, 1, 4, 3 }Output: 3Explanation: The elements at index 0, 2, 3 has same value of a[i]+i as all sum to 6 {(6+0), (4+2), (3+3)}. Input: a
8 min read