Friday, January 10, 2025
Google search engine
HomeData Modelling & AIMaximize distance between smallest and largest Array elements by a single swap

Maximize distance between smallest and largest Array elements by a single swap

Given an arr[] consisting of N elements in the range [1, N], the task is to maximize the distance between smallest and largest array element by a single swap.

Examples: 

Input: arr[] = {1, 4, 3, 2} 
Output:
Explanation: 
Swapping of arr[1] and arr[3] maximizes the distance. 

Input: arr[] = {1, 6, 5, 3, 4, 7, 2} 
Output:
Explanation: 
Swapping of arr[5] and arr[6] maximizes the distance. 

Approach 

  1. Find the indices of 1 and N in the array.
  2. Let minIdx and maxIdx be the minimum and maximum of the two indices, respectively.
  3. Now, maxIdx – minIdx is the current distance between the two elements. It can be maximized by the maximum possible from the following two swaps: 
    • Swapping a[minIdx] with a[0] increasing the distance by minIdx.
    • Swapping a[maxIdx] with a[N – 1] increasing the distance by N – 1 – maxIdx.

Below is the implementation of the above approach:

C++




// C++ program maximize the
// distance between smallest
// and largest array element
// by a single swap
#include <bits/stdc++.h>
using namespace std;
 
// Function to maximize the distance
// between the smallest and largest
// array element by a single swap
int find_max_dist(int arr[], int N)
{
 
    int minIdx = -1, maxIdx = -1;
 
    for (int i = 0; i < N; i++) {
        if (arr[i] == 1 || arr[i] == N) {
            if (minIdx == -1)
                minIdx = i;
            else {
                maxIdx = i;
                break;
            }
        }
    }
 
    return maxIdx - minIdx
           + max(minIdx, N - 1 - maxIdx);
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 4, 3, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << find_max_dist(arr, N) << endl;
    return 0;
}


Java




// Java program maximize the distance
// between smallest and largest array
// element by a single swap
import java.util.*;
 
class GFG{
 
// Function to maximize the distance
// between the smallest and largest
// array element by a single swap
static int find_max_dist(int arr[], int N)
{
    int minIdx = -1, maxIdx = -1;
 
    for(int i = 0; i < N; i++)
    {
       if (arr[i] == 1 || arr[i] == N)
       {
           if (minIdx == -1)
               minIdx = i;
           else
           {
               maxIdx = i;
               break;
           }
       }
    }
    return maxIdx - minIdx +
           Math.max(minIdx, N - 1 - maxIdx);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 4, 3, 2 };
    int N = arr.length;
     
    System.out.print(find_max_dist(arr, N) + "\n");
}
}
 
// This code is contributed by Amit Katiyar


Python3




# Python3 program maximize the
# distance between smallest
# and largest array element
# by a single swap
 
# Function to maximize the distance
# between the smallest and largest
# array element by a single swap
def find_max_dist(arr, N):
 
    minIdx, maxIdx = -1, -1
 
    for i in range(N):
        if (arr[i] == 1 or arr[i] == N):
            if (minIdx == -1) :
                minIdx = i
                 
            else :
                maxIdx = i
                break
 
    return (maxIdx - minIdx +
        max(minIdx, N - 1 - maxIdx))
 
# Driver code
arr = [ 1, 4, 3, 2 ]
N = len(arr)
 
print(find_max_dist(arr, N))
 
# This code is contributed by divyeshrabadiya07


C#




// C# program maximize the distance
// between smallest and largest array
// element by a single swap
using System;
 
class GFG{
 
// Function to maximize the distance
// between the smallest and largest
// array element by a single swap
static int find_max_dist(int []arr, int N)
{
    int minIdx = -1, maxIdx = -1;
 
    for(int i = 0; i < N; i++)
    {
       if (arr[i] == 1 || arr[i] == N)
       {
           if (minIdx == -1)
               minIdx = i;
           else
           {
               maxIdx = i;
               break;
           }
       }
    }
    return maxIdx - minIdx +
           Math.Max(minIdx, N - 1 - maxIdx);
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 4, 3, 2 };
    int N = arr.Length;
     
    Console.Write(find_max_dist(arr, N) + "\n");
}
}
 
// This code is contributed by Amit Katiyar


Javascript




<script>
// javascript program maximize the distance
// between smallest and largest array
// element by a single swap
 
// Function to maximize the distance
// between the smallest and largest
// array element by a single swap
function find_max_dist(arr , N)
{
    var minIdx = -1, maxIdx = -1;
 
    for(i = 0; i < N; i++)
    {
       if (arr[i] == 1 || arr[i] == N)
       {
           if (minIdx == -1)
               minIdx = i;
           else
           {
               maxIdx = i;
               break;
           }
       }
    }
    return maxIdx - minIdx +
           Math.max(minIdx, N - 1 - maxIdx);
}
 
// Driver Code
var arr = [ 1, 4, 3, 2 ];
var N = arr.length;
 
document.write(find_max_dist(arr, N) + "\n");
 
// This code is contributed by Amit Katiyar
</script>


Output: 

3

 

Time Complexity: O(N)
Auxiliary space: O(1) as it is using constant space for variable

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Last Updated :
15 Dec, 2022
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments