给定唯一元素的数组arr []和三个整数X , A和B。任务是在与相邻元素的最多X次交换中打印元素A和B之间的最大可能距离。
例子:
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.
方法:可以按照以下步骤计算结果:
- 如果给定的X为0,则| index(A)-index(B)|返回为最终答案。
- 如果元素已经在索引0和n-1处,则返回N-1,因为距离已经最大化。
- 否则,较大的索引元素将与其相邻的元素交换X次,直到小于数组的大小为止。
- 如果X> 0,则到达数组末尾;否则,返回0。然后将较小的索引元素与其之前的元素交换,直到X不等于0。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to maximize the distance
// between the two elements of the
// array by at most X swaps
void max_distance(int a[], int n, int x,
int A, int B)
{
// Initialize the variables
int g = 0, h = 0;
// Iterate till the length of the array
for (int i = 0; i < n; i++) {
// Check if current element is A
if (a[i] == A)
// Store index
g = i;
// Check if current element is B
if (a[i] == B)
// Store index
h = i;
}
// If X = 0, swapping can not be done
if (x == 0) {
cout << abs(g - h);
}
// If elements are at starting and ending
// indices, then the distance
// is already maximum
else if ((g == 0) && (h == (n - 1)))
cout << n - 1 << endl;
else if ((g == n - 1) && (h == 0))
cout << n - 1 << endl;
else {
// Greater index is incremented
// till x > 0 and the
// index of element < size of array
if (h > g) {
while ((x > 0) && (h < n - 1)) {
h++;
x--;
}
// Check if reached the size of array
// and x > 0, then the
// smaller index is decremented
if (x > 0) {
while ((x > 0) && (g > 0)) {
g--;
x--;
}
}
cout << h - g << endl;
}
// Greater index is incremented till x>0
// and index of element < size of array
else {
while ((x > 0) && (g < n - 1)) {
g++;
x--;
}
// Check if reached the size of the array
// and x > 0 the smaller index
// is decremented
if (x > 0) {
while ((x > 0) && (h > 0)) {
h--;
x--;
}
}
cout << g - h << endl;
}
}
}
// Driver code
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
// Java program for the above approach
import java.util.*;
class GFG{
// Function to maximize the distance
// between the two elements of the
// array by at most X swaps
static void max_distance(int a[], int n, int x,
int A, int B)
{
// Initialize the variables
int i, g = 0, h = 0;
// Iterate till the length of the array
for(i = 0; i < n; i++)
{
// Check if current element is A
if (a[i] == A)
// Store index
g = i;
// Check if current element is B
if (a[i] == B)
// Store index
h = i;
}
// If X = 0, swapping can not be done
if (x == 0)
{
System.out.print(Math.abs(g - h));
}
// If elements are at starting and
// ending indices, then the distance
// is already maximum
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
{
// Greater index is incremented
// till x > 0 and theindex of
// element < size of array
if (h > g)
{
while ((x > 0) && (h < n - 1))
{
h++;
x--;
}
// Check if reached the size of
// array and x > 0, then the
// smaller index is decremented
if (x > 0)
{
while ((x > 0) && (g > 0))
{
g--;
x--;
}
}
System.out.println(h - g);
}
// Greater index is incremented till x>0
// and index of element < size of array
else
{
while ((x > 0) && (g < n - 1))
{
g++;
x--;
}
// Check if reached the size of the array
// and x > 0 the smaller index
// is decremented
if (x > 0)
{
while ((x > 0) && (h > 0))
{
h--;
x--;
}
}
System.out.println(g - h);
}
}
}
// Driver code
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);
}
}
// This code is contributed by chitranayal
Python3
# Python3 program for the above approach
# Function to maximize the distance
# between the two elements of the
# array by at most X swaps
def max_distance(a, n, x, A, B):
# Initialize the variables
g = 0
h = 0
for i in range(0, n):
# Check if current element is A
if (a[i] == A):
# Store index
g = i
# Check if current element is B
if (a[i] == B):
# Store index
h = i
# If X = 0, swapping can not be done
if (x == 0):
print(abs(g - h))
# If elements are at starting and
# ending indices, then the distance
# is already maximum
elif ((g == 0) and (h == (n - 1))):
print(n - 1, end = '')
elif ((g == n - 1) and (h == 0)):
print(n - 1, end = '')
else:
# Greater index is incremented
# till x > 0 and the
# index of element < size of array
if (h > g):
while ((x > 0) and (h < n - 1)):
h += 1
x -= 1
# Check if reached the size
# of array and x > 0, then the
# smaller index is decremented
if (x > 0):
while ((x > 0) and (g > 0)):
g -= 1
x -= 1
print(h - g, end = '')
# Greater index is incremented till x>0
# and index of element < size of array
else:
while ((x > 0) and (g < n - 1)):
g += 1
x -= 1
# Check if reached the size of
# the array and x > 0 the smaller
# index is decremented
if (x > 0):
while ((x > 0) and (h > 0)):
h -= 1
x -= 1
print(g - h, end = '')
# Driver code
if __name__ == '__main__':
a = [ 100, 33, 10, 1 ]
x = 5
A = 100
B = 1
n = len(a)
max_distance(a, n, x, A, B)
# This code is contributed by virusbuddah_
C#
// C# program for the above approach
using System;
class GFG{
// Function to maximize the distance
// between the two elements of the
// array by at most X swaps
static void max_distance(int []a, int n, int x,
int A, int B)
{
// Initialize the variables
int i, g = 0, h = 0;
// Iterate till the length of the array
for(i = 0; i < n; i++)
{
// Check if current element is A
if (a[i] == A)
// Store index
g = i;
// Check if current element is B
if (a[i] == B)
// Store index
h = i;
}
// If X = 0, swapping can not be done
if (x == 0)
{
Console.Write(Math.Abs(g - h));
}
// If elements are at starting and
// ending indices, then the distance
// is already maximum
else if ((g == 0) && (h == (n - 1)))
Console.WriteLine(n - 1);
else if ((g == n - 1) && (h == 0))
Console.WriteLine(n - 1);
else
{
// Greater index is incremented
// till x > 0 and theindex of
// element < size of array
if (h > g)
{
while ((x > 0) && (h < n - 1))
{
h++;
x--;
}
// Check if reached the size of
// array and x > 0, then the
// smaller index is decremented
if (x > 0)
{
while ((x > 0) && (g > 0))
{
g--;
x--;
}
}
Console.WriteLine(h - g);
}
// Greater index is incremented till x>0
// and index of element < size of array
else
{
while ((x > 0) && (g < n - 1))
{
g++;
x--;
}
// Check if reached the size of the
// array and x > 0 the smaller index
// is decremented
if (x > 0)
{
while ((x > 0) && (h > 0))
{
h--;
x--;
}
}
Console.WriteLine(g - h);
}
}
}
// Driver code
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);
}
}
// This code is contributed by Amit Katiyar
输出:
3