用于以最大最小形式重新排列数组的Java程序 - 集 2(O(1) 额外空间)
给定一个正整数的排序数组,交替重新排列数组,即第一个元素应该是最大值,第二个最小值,第三个最大值,第四个最小值等等。
例子:
Input: arr[] = {1, 2, 3, 4, 5, 6, 7}
Output: arr[] = {7, 1, 6, 2, 5, 3, 4}
Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: arr[] = {6, 1, 5, 2, 4, 3}
我们在下面的帖子中讨论了一个解决方案:
以最大最小形式重新排列数组 | Set 1 : 这里讨论的解决方案需要额外的空间,如何用 O(1) 额外的空间来解决这个问题。
在这篇文章中,我们讨论了一个需要 O(n) 时间和 O(1) 额外空间的解决方案。这个想法是使用乘法和模块化技巧将两个元素存储在索引处。
even index : remaining maximum element.
odd index : remaining minimum element.
max_index : Index of remaining maximum element
(Moves from right to left)
min_index : Index of remaining minimum element
(Moves from left to right)
Initialize: max_index = 'n-1'
min_index = 0
// can be any element which is more
// than the maximum value in array
max_element = arr[max_index] + 1
For i = 0 to n-1
If 'i' is even
arr[i] += arr[max_index] % max_element * max_element
max_index--
// if 'i' is odd
ELSE
arr[i] += arr[min_index] % max_element * max_element
min_index++
表达式“arr[i] += arr[max_index] % max_element * max_element”是如何工作的?
此表达式的目的是在索引 arr[i] 处存储两个元素。 arr[max_index] 存储为乘数,“arr[i]”存储为余数。例如在 {1 2 3 4 5 6 7 8 9} 中,max_element 为 10,我们将 91 存储在索引 0 处。有了 91,我们可以得到原始元素为 91%10,新元素为 91/10。
下面实现上述想法:
Java
// Java program to rearrange an
// array in minimum maximum form
public class Main {
// Prints max at first position, min
// at second position second max at
// third position, second min at
// fourth position and so on.
public static void rearrange(int arr[],
int n)
{
// Initialize index of first minimum
// and first maximum element
int max_idx = n - 1, min_idx = 0;
// Store maximum element of array
int max_elem = arr[n - 1] + 1;
// Traverse array elements
for (int i = 0; i < n; i++)
{
// At even index : we have to put
// maximum element
if (i % 2 == 0)
{
arr[i] += ((arr[max_idx] % max_elem) *
max_elem);
max_idx--;
}
// At odd index : we have to put
// minimum element
else
{
arr[i] += ((arr[min_idx] % max_elem) *
max_elem);
min_idx++;
}
}
// Array elements back to it's
// original form
for (int i = 0; i < n; i++)
arr[i] = arr[i] / max_elem;
}
// Driver code
public static void main(String args[])
{
int arr[] = {1, 2, 3, 4, 5,
6, 7, 8, 9};
int n = arr.length;
System.out.println(
"Original Array");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
rearrange(arr, n);
System.out.print(
"Modified Array");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}
// This code is contributed by Swetank Modi
Java
// Java program to rearrange an
// array in minimum maximum form
public class Main
{
// Prints max at first position, min
// at second position second max at
// third position, second min at
// fourth position and so on.
public static void rearrange(int arr[],
int n)
{
// Initialize index of first minimum
// and first maximum element
int max_ele = arr[n - 1];
int min_ele = arr[0];
// Traverse array elements
for (int i = 0; i < n; i++)
{
// At even index : we have to put
// maximum element
if (i % 2 == 0)
{
arr[i] = max_ele;
max_ele -= 1;
}
// At odd index : we have to put
// minimum element
else
{
arr[i] = min_ele;
min_ele += 1;
}
}
}
// Driver code
public static void main(String args[])
{
int arr[] = {1, 2, 3, 4, 5,
6, 7, 8, 9};
int n = arr.length;
System.out.println("Original Array");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
rearrange(arr, n);
System.out.print(
"Modified Array");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}
输出 :
Original Array
1 2 3 4 5 6 7 8 9
Modified Array
9 1 8 2 7 3 6 4 5
感谢 Saurabh Srivastava 和 Gaurav Ahirwar 提出这种方法。
另一种方法:一种更简单的方法是观察最大元素和最小元素的索引位置。偶数索引存储最大元素,奇数索引存储最小元素。每增加一个索引,最大元素减少一,最小元素增加一。可以做一个简单的遍历,再次填写arr[]。
注意:这种方法仅在给定排序数组的元素是连续的(即相差一个单位)时才有效。
下面是上述方法的实现:
Java
// Java program to rearrange an
// array in minimum maximum form
public class Main
{
// Prints max at first position, min
// at second position second max at
// third position, second min at
// fourth position and so on.
public static void rearrange(int arr[],
int n)
{
// Initialize index of first minimum
// and first maximum element
int max_ele = arr[n - 1];
int min_ele = arr[0];
// Traverse array elements
for (int i = 0; i < n; i++)
{
// At even index : we have to put
// maximum element
if (i % 2 == 0)
{
arr[i] = max_ele;
max_ele -= 1;
}
// At odd index : we have to put
// minimum element
else
{
arr[i] = min_ele;
min_ele += 1;
}
}
}
// Driver code
public static void main(String args[])
{
int arr[] = {1, 2, 3, 4, 5,
6, 7, 8, 9};
int n = arr.length;
System.out.println("Original Array");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
rearrange(arr, n);
System.out.print(
"Modified Array");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}
输出 :
Original Array
1 2 3 4 5 6 7 8 9
Modified Array
9 1 8 2 7 3 6 4 5
请参阅有关以最大最小形式重新排列数组的完整文章 |设置 2(O(1) 额外空间)以获取更多详细信息!