Maximize distance between two elements of Array by at most X swaps
Last Updated :
15 Dec, 2022
Given an array arr[] of unique elements and three integers X, A, and B. The task is to print the maximum possible distance between elements A and B in atmost X swaps with the adjacent elements.
Examples:
Input: arr[] = {5, 1, 3, 2}, X = 1, A = 2, B = 3
Output: 2
Explanation:
3 is swapped with it’s adjacent element 1. So, the distance between element 3 and 2 is 2 and no more interchanges are possible since X = 1.
Input: arr = {100, 33, 10, 1}, X = 5, A = 100, B = 1
Output: 3
Explanation:
As the elements are already the beginning and ending element, the distance between them is already maximized.
Approach: The following steps can be followed to compute the result:
- In case the given X is 0, then |index(A)-index(B)| is returned as final answer.
- If the elements are already at index 0 and n-1, return N-1 as the distance is already maximized.
- Else, the greater index element is swapped X times with their adjacent element till it is less than the size of the array.
- After reaching the end of the array if X > 0; then the smaller index element is swapped with its previous elements till X is not equal to 0.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void max_distance( int a[], int n, int x,
int A, int B)
{
int g = 0, h = 0;
for ( int i = 0; i < n; i++) {
if (a[i] == A)
g = i;
if (a[i] == B)
h = i;
}
if (x == 0) {
cout << abs (g - h);
}
else if ((g == 0) && (h == (n - 1)))
cout << n - 1 << endl;
else if ((g == n - 1) && (h == 0))
cout << n - 1 << endl;
else {
if (h > g) {
while ((x > 0) && (h < n - 1)) {
h++;
x--;
}
if (x > 0) {
while ((x > 0) && (g > 0)) {
g--;
x--;
}
}
cout << h - g << endl;
}
else {
while ((x > 0) && (g < n - 1)) {
g++;
x--;
}
if (x > 0) {
while ((x > 0) && (h > 0)) {
h--;
x--;
}
}
cout << g - h << endl;
}
}
}
int main()
{
int a[] = { 100, 33, 10, 1 };
int x = 5, A = 100, B = 1;
int n = sizeof (a) / sizeof (a[0]);
max_distance(a, n, x, A, B);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void max_distance( int a[], int n, int x,
int A, int B)
{
int i, g = 0 , h = 0 ;
for (i = 0 ; i < n; i++)
{
if (a[i] == A)
g = i;
if (a[i] == B)
h = i;
}
if (x == 0 )
{
System.out.print(Math.abs(g - h));
}
else if ((g == 0 ) && (h == (n - 1 )))
System.out.println(n - 1 );
else if ((g == n - 1 ) && (h == 0 ))
System.out.println(n - 1 );
else
{
if (h > g)
{
while ((x > 0 ) && (h < n - 1 ))
{
h++;
x--;
}
if (x > 0 )
{
while ((x > 0 ) && (g > 0 ))
{
g--;
x--;
}
}
System.out.println(h - g);
}
else
{
while ((x > 0 ) && (g < n - 1 ))
{
g++;
x--;
}
if (x > 0 )
{
while ((x > 0 ) && (h > 0 ))
{
h--;
x--;
}
}
System.out.println(g - h);
}
}
}
public static void main (String []args)
{
int a[] = { 100 , 33 , 10 , 1 };
int x = 5 , A = 100 , B = 1 ;
int n = a.length;
max_distance(a, n, x, A, B);
}
}
|
Python3
def max_distance(a, n, x, A, B):
g = 0
h = 0
for i in range ( 0 , n):
if (a[i] = = A):
g = i
if (a[i] = = B):
h = i
if (x = = 0 ):
print ( abs (g - h))
elif ((g = = 0 ) and (h = = (n - 1 ))):
print (n - 1 , end = '')
elif ((g = = n - 1 ) and (h = = 0 )):
print (n - 1 , end = '')
else :
if (h > g):
while ((x > 0 ) and (h < n - 1 )):
h + = 1
x - = 1
if (x > 0 ):
while ((x > 0 ) and (g > 0 )):
g - = 1
x - = 1
print (h - g, end = '')
else :
while ((x > 0 ) and (g < n - 1 )):
g + = 1
x - = 1
if (x > 0 ):
while ((x > 0 ) and (h > 0 )):
h - = 1
x - = 1
print (g - h, end = '')
if __name__ = = '__main__' :
a = [ 100 , 33 , 10 , 1 ]
x = 5
A = 100
B = 1
n = len (a)
max_distance(a, n, x, A, B)
|
C#
using System;
class GFG{
static void max_distance( int []a, int n, int x,
int A, int B)
{
int i, g = 0, h = 0;
for (i = 0; i < n; i++)
{
if (a[i] == A)
g = i;
if (a[i] == B)
h = i;
}
if (x == 0)
{
Console.Write(Math.Abs(g - h));
}
else if ((g == 0) && (h == (n - 1)))
Console.WriteLine(n - 1);
else if ((g == n - 1) && (h == 0))
Console.WriteLine(n - 1);
else
{
if (h > g)
{
while ((x > 0) && (h < n - 1))
{
h++;
x--;
}
if (x > 0)
{
while ((x > 0) && (g > 0))
{
g--;
x--;
}
}
Console.WriteLine(h - g);
}
else
{
while ((x > 0) && (g < n - 1))
{
g++;
x--;
}
if (x > 0)
{
while ((x > 0) && (h > 0))
{
h--;
x--;
}
}
Console.WriteLine(g - h);
}
}
}
public static void Main(String []args)
{
int []a = { 100, 33, 10, 1 };
int x = 5, A = 100, B = 1;
int n = a.Length;
max_distance(a, n, x, A, B);
}
}
|
Javascript
<script>
function max_distance(a,n,x,A,B)
{
let i, g = 0, h = 0;
for (i = 0; i < n; i++)
{
if (a[i] == A)
g = i;
if (a[i] == B)
h = i;
}
if (x == 0)
{
document.write(Math.abs(g - h));
}
else if ((g == 0) && (h == (n - 1)))
document.write(n - 1);
else if ((g == n - 1) && (h == 0))
document.write(n - 1);
else
{
if (h > g)
{
while ((x > 0) && (h < n - 1))
{
h++;
x--;
}
if (x > 0)
{
while ((x > 0) && (g > 0))
{
g--;
x--;
}
}
document.write(h - g);
}
else
{
while ((x > 0) && (g < n - 1))
{
g++;
x--;
}
if (x > 0)
{
while ((x > 0) && (h > 0))
{
h--;
x--;
}
}
document.write(g - h);
}
}
}
let a=[ 100, 33, 10, 1];
let x = 5, A = 100, B = 1;
let n = a.length;
max_distance(a, n, x, A, B);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...