Maximum distance between i and j such that i ≤ j and A[i] ≤ B[j]
Last Updated :
28 Feb, 2022
Given two non-increasing arrays A[] and B[] of size N, the task is to find the maximum distance between i and j such that i ≤ j and A[i] ≤ B[j].
Examples:
Input: A[] = {2, 2, 2}, B[] = {10, 10, 1}
Output: 1
Explanation:
The valid pairs satisfying the conditions are (0, 0), (0, 1), and (1, 1).
Therefore, the maximum distance is 1 between the indices 0 of A[] and 1 of B[].
Input: A[] = {55, 30, 5, 4, 2}, B[] = {100, 20, 10, 10, 5}
Output: 2
Explanation:
The valid pairs satisfying the conditions are (0, 0), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), and (4, 4).
Therefore, the maximum distance is 2 between the indices 2 of A[] and 4 of B[].
Naive Approach: The simplest approach to solve the problem is to traverse the array A[] and for each element of A[] traverse the array B[] and find the maximum distance between points such that j>=i and B[j] >= A[i].
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized further by using the binary search algorithm. Follow the steps below to solve the problem:
- Initialize a variable, say ans as 0 to store the maximum distance between two elements in two non-increasing arrays.
- Iterate in the range [0, N-1] using the variable i, and perform the following steps:
- Initialize two variables, say low as i, high as N-1.
- Iterate until low is less than or equal to high and do the following:
- Initialize a variable, say mid as low+(high-low)/2.
- If A[i] is less than equal to B[mid], then update ans to max of ans and mid-i and low to mid+1.
- Otherwise, update high to mid-1.
- Finally, after completing the above steps, print the value of ans as the answer
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxDistance( int A[], int B[], int N)
{
int ans = 0;
for ( int i = 0; i < N; i++) {
int low = i, high = N - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (A[i] <= B[mid]) {
ans = max(ans, mid - i);
low = mid + 1;
}
else {
high = mid - 1;
}
}
}
return ans;
}
int main()
{
int A[] = { 2, 2, 2 };
int B[] = { 10, 10, 1 };
int N = 3;
cout << maxDistance(A, B, N);
}
|
Java
public class GFG {
static int maxDistance( int A[], int B[], int N)
{
int ans = 0 ;
for ( int i = 0 ; i < N; i++) {
int low = i, high = N - 1 ;
while (low <= high) {
int mid = low + (high - low) / 2 ;
if (A[i] <= B[mid]) {
ans = Math.max(ans, mid - i);
low = mid + 1 ;
}
else {
high = mid - 1 ;
}
}
}
return ans;
}
public static void main(String[] args)
{
int A[] = { 2 , 2 , 2 };
int B[] = { 10 , 10 , 1 };
int N = 3 ;
System.out.println(maxDistance(A, B, N));
}
}
|
Python3
def maxDistance(A, B, N):
ans = 0
for i in range (N):
low = i
high = N - 1
while (low < = high):
mid = low + (high - low) / / 2
if (A[i] < = B[mid]):
ans = max (ans, mid - i)
low = mid + 1
else :
high = mid - 1
return ans
if __name__ = = '__main__' :
A = [ 2 , 2 , 2 ]
B = [ 10 , 10 , 1 ]
N = 3
print (maxDistance(A, B, N))
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static int maxDistance( int [] A, int [] B, int N)
{
int ans = 0;
for ( int i = 0; i < N; i++) {
int low = i, high = N - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (A[i] <= B[mid]) {
ans = Math.Max(ans, mid - i);
low = mid + 1;
}
else {
high = mid - 1;
}
}
}
return ans;
}
public static void Main()
{
int [] A = { 2, 2, 2 };
int [] B = { 10, 10, 1 };
int N = 3;
Console.Write(maxDistance(A, B, N));
}
}
|
Javascript
<script>
function maxDistance(A, B, N) {
let ans = 0;
for (let i = 0; i < N; i++) {
let low = i, high = N - 1;
while (low <= high) {
let mid = low + (high - low) / 2;
if (A[i] <= B[mid]) {
ans = Math.max(ans, mid - i);
low = mid + 1;
}
else {
high = mid - 1;
}
}
}
return ans;
}
let A = [2, 2, 2];
let B = [10, 10, 1];
let N = 3;
document.write(maxDistance(A, B, N));
</script>
|
Time Complexity: O(N×log(N))
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized further by using two pointers technique. Follow the steps below to solve the problem:
- Initialize two variables, say, i, and j to perform a two-pointer.
- Iterate until i and j are less than N and perform the following steps:
- If A[i] is greater than B[j], then increment i by 1, because the arrays are sorted in non-increasing.
- Otherwise, update ans to max of ans and j-i, and increment j by 1.
- Finally, after completing the above steps, print the value of ans as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxDistance( int A[], int B[], int N)
{
int i = 0, j = 0;
int ans = 0;
while (i < N && j < N) {
if (A[i] > B[j]) {
++i;
}
else {
ans = max(ans, j - i);
j++;
}
}
return ans;
}
int main()
{
int A[] = { 2, 2, 2 };
int B[] = { 10, 10, 1 };
int N = sizeof (A) / sizeof (A[0]);
cout << maxDistance(A, B, N);
}
|
Java
import java.io.*;
class GFG {
static int maxDistance( int A[], int B[], int N)
{
int i = 0 , j = 0 ;
int ans = 0 ;
while (i < N && j < N) {
if (A[i] > B[j]) {
++i;
}
else {
ans = Math.max(ans, j - i);
j++;
}
}
return ans;
}
public static void main (String[] args)
{
int A[] = { 2 , 2 , 2 };
int B[] = { 10 , 10 , 1 };
int N = A.length;
System.out.println(maxDistance(A, B, N));
}
}
|
Python3
def maxDistance(A, B, N):
i = 0
j = 0
ans = 0
while (i < N and j < N):
if (A[i] > B[j]):
i + = 1
else :
ans = max (ans, j - i)
j + = 1
return ans
if __name__ = = '__main__' :
A = [ 2 , 2 , 2 ]
B = [ 10 , 10 , 1 ]
N = len (A)
print (maxDistance(A, B, N))
|
C#
using System;
class GFG {
static int maxDistance( int []A, int []B, int N)
{
int i = 0, j = 0;
int ans = 0;
while (i < N && j < N) {
if (A[i] > B[j]) {
++i;
}
else {
ans = Math.Max(ans, j - i);
j++;
}
}
return ans;
}
public static void Main (String[] args)
{
int []A = { 2, 2, 2 };
int []B = { 10, 10, 1 };
int N = A.Length;
Console.Write(maxDistance(A, B, N));
}
}
|
Javascript
<script>
function maxDistance( A, B, N)
{
var i = 0, j = 0;
var ans = 0;
while (i < N && j < N) {
if (A[i] > B[j]) {
++i;
}
else {
ans = Math.max(ans, j - i);
j++;
}
}
return ans;
}
var A = [ 2, 2, 2 ];
var B = [ 10, 10, 1 ];
var N = A.length;
document.write(maxDistance(A, B, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...