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: 3
Explanation:
Swapping of arr[1] and arr[3] maximizes the distance.Input: arr[] = {1, 6, 5, 3, 4, 7, 2}
Output: 6
Explanation:
Swappinf of arr[5] and arr[6] maximizes the distance.
Approach
- Find the indices of 1 and N in the array.
- Let minIdx and maxIdx be the minimum and maximum of the two indices, respectively.
-
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.
Hence, maxIdx – minIdx + max(minIdx, N – 1 – maxIdx) gives us the required answer.
Illustration:
arr[] = {2, 4, 6, 1, 3, 5}
Smallest element = 1
Largest element = 6
minIdx = 2
maxIdx = 3
Required Answer = maxIdx – minIdx + max(minIdx, N – 1 – maxIdx)
= 3 – 2 + max(2, 5 – 3)
= 1 + 2 = 3
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 |
3
Time Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- Maximize distance between two elements of Array by at most X swaps
- Maximize sum of an Array by flipping sign of all elements of a single subarray
- Average of remaining elements after removing K largest and K smallest elements from array
- Maximize minimum distance between repetitions from any permutation of the given Array
- Maximize distance between any two consecutive 1's after flipping M 0's
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array
- Find the smallest and second smallest elements in an array
- Lexicographically smallest array formed by at most one swap for every pair of adjacent indices
- Difference between the largest and the smallest primes in an array
- Maximize sum of array by reducing array elements to contain no triplets (i, j, k) where a[i] < a[j] and a[i] < a[k] and j <i < k
- k largest(or smallest) elements in an array | added Min Heap method
- Maximize occurrences of values between L and R on sequential addition of Array elements with modulo H
- Sort Array such that smallest is at 0th index and next smallest it at last index and so on
- Replace elements with absolute difference of smallest element on left and largest element on right
- Sum and product of k smallest and k largest prime numbers in the array
- Sum and product of k smallest and k largest composite numbers in the array
- Sum and product of K smallest and largest Fibonacci numbers in the array
- Lexicographically largest string possible in one swap
- Maximize sum of K elements in Array by taking only corner elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

