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
Count smaller elements present in the array for each array element
Given an array arr[] consisting of N integers, the task is for each array element, say arr[i], is to find the number of array elements that are smaller than arr[i]. Examples: Input: arr[] = {3, 4, 1, 1, 2}Output: 3 4 0 0 2Explanation:The elements which are smaller than arr[0](= 3) are {1, 1, 2}. Hen
9 min read
Count pairs in an array that hold i+j= arr[i]+arr[j]
Given an array of integers arr[], the task is to count all the pairs (arr[i], arr[j]) such that i + j = arr[i] + arr[j] for all 0 ? i < j < n. Note: Pairs (x, y) and (y, x) are considered a single pair. Examples: Input: arr[] = {8, 4, 2, 1, 5, 4, 2, 1, 2, 3} Output: 1 The only possible pair is
12 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 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
Find an array B with at least arr[i] elements in B not equal to the B[i]
Given an array arr[] of size N, the task is to find an array of size N, such that for every ith element of the array arr[], the output array should contain at least arr[i] elements that are not equal to the ith element of the output array. If there exists no such array, then print "Impossible". Exam
9 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 index pairs with equal elements in an array | Set 2
Given an array arr[] of N elements. The task is to count the total number of indices (i, j) such that arr[i] = arr[j] and i != j Examples: Input: arr[]={1, 2, 1, 1}Output: 3 Explanation:In the array arr[0]=arr[2]=arr[3]Valid Pairs are (0, 2), (0, 3) and (2, 3) Input: arr[]={2, 2, 3, 2, 3}Output: 4Ex
8 min read
Count pairs from an array having product of their sum and difference equal to 0
Given an array arr[] of size N, the task is to count possible pairs of array elements (arr[i], arr[j]) such that (arr[i] + arr[j]) * (arr[i] - arr[j]) is 0. Examples: Input: arr[] = {2, -2, 1, 1}Output : 2Explanation:(arr[0] + arr[1]) * (arr[0] - arr[1]) = 0(arr[3] + arr[4]) * (arr[3] - arr[4]) = 0
8 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