📌  相关文章
📜  通过最多X个交换使Array的两个元素之间的距离最大

📅  最后修改于: 2021-05-14 00:48:51             🧑  作者: Mango

给定唯一元素的数组arr []和三个整数XAB。任务是在与相邻元素的最多X次交换中打印元素AB之间的最大可能距离。
例子:

方法:可以按照以下步骤计算结果:

  • 如果给定的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