找到奇数索引处的元素总和大于偶数索引处的元素总和的数组排列
给定一个由N个整数组成的数组arr[] ,任务是找到数组元素的排列,使得奇数索引元素的总和大于或等于偶数索引元素的总和。
例子:
Input: arr[] = {1, 2, 3, 4}
Output: 1 4 2 3
Explanation:
Consider the permutation of the given array as {1, 4, 2, 3}.
Now, the sum of elements at odd indices = (4 + 3) = 7 and the sum of elements at even indices = (1 + 2) = 3.
As the sum at odd indices elements is greater than the sum of even indices element. Therefore, print the current permutation.
Input: arr[] = {123, 45, 67, 89, 60, 33}
Output: 33 123 45 89 60 67
朴素方法:解决给定问题的最简单方法是生成给定数组的所有可能排列,并打印奇数索引元素之和大于或等于偶数索引元素之和的数组的排列。
时间复杂度: O(N*N!)
辅助空间: O(N)
高效方法:上述方法也可以通过对给定数组进行排序和使用双指针方法进行优化。请按照以下步骤解决问题:
- 按升序对给定数组arr[]进行排序。
- 初始化两个变量,比如i为0和j为(N – 1) 。
- 使用变量K迭代范围[0, N – 1]并执行以下步骤:
- 如果K的值是偶数,则打印arr[i]的值并将i的值增加1 。
- 否则,打印arr[j]的值并将j的值减1 。
以下是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the permutation of
// array elements such that the sum of
// elements at odd āindices is greater
// than sum of elements at even indices
void rearrangeArray(int arr[], int n)
{
// Sort the given array
sort(arr, arr + n);
// Initialize the two pointers
int j = n - 1;
int i = 0;
// Traverse the array arr[]
for (int k = 0; k < n; k++) {
// Check if k is even
if (k % 2 == 0) {
cout << arr[i] << " ";
// Increment the value
// of i
i++;
}
else {
cout << arr[j] << " ";
// Decrement the value
// of j
j--;
}
}
}
// Driver Code
int main()
{
int arr[] = { 123, 45, 67, 89, 60, 33 };
int N = sizeof(arr) / sizeof(arr[0]);
rearrangeArray(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the permutation of
// array elements such that the sum of
// elements at odd āindices is greater
// than sum of elements at even indices
static void rearrangeArray(int arr[], int n)
{
// Sort the given array
Arrays.sort(arr);
// Initialize the two pointers
int j = n - 1;
int i = 0;
// Traverse the array arr[]
for(int k = 0; k < n; k++)
{
// Check if k is even
if (k % 2 == 0)
{
System.out.print(arr[i] + " ");
// Increment the value
// of i
i++;
}
else
{
System.out.print(arr[j] + " ");
// Decrement the value
// of j
j--;
}
}
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 123, 45, 67, 89, 60, 33 };
int N = arr.length;
rearrangeArray(arr, N);
}
}
// This code is contributed by avijitmondal1998
Python3
# Python3 program for the above approach
# Function to find the permutation of
# array elements such that the sum of
# elements at odd āindices is greater
# than sum of elements at even indices
def rearrangeArray(arr, n):
# Sort the given array
arr = sorted(arr)
# Initialize the two pointers
j = n - 1
i = 0
# Traverse the array arr[]
for k in range(n):
# Check if k is even
if (k % 2 == 0):
print(arr[i], end = " ")
# Increment the value
# of i
i += 1
else:
print(arr[j], end = " ")
# Decrement the value
# of j
j -= 1
# Driver Code
if __name__ == '__main__':
arr = [ 123, 45, 67, 89, 60, 33 ]
N = len(arr)
rearrangeArray(arr, N)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the permutation of
// array elements such that the sum of
// elements at odd āindices is greater
// than sum of elements at even indices
static void rearrangeArray(int[] arr, int n)
{
// Sort the given array
Array.Sort(arr);
// Initialize the two pointers
int j = n - 1;
int i = 0;
// Traverse the array arr[]
for(int k = 0; k < n; k++)
{
// Check if k is even
if (k % 2 == 0)
{
Console.Write(arr[i] + " ");
// Increment the value
// of i
i++;
}
else
{
Console.Write(arr[j] + " ");
// Decrement the value
// of j
j--;
}
}
}
// Driver Code
public static void Main()
{
int[] arr = { 123, 45, 67, 89, 60, 33 };
int N = arr.Length;
rearrangeArray(arr, N);
}
}
// This code is contributed by subham348
Javascript
33 123 45 89 60 67
时间复杂度: O(N*log N)
辅助空间: O(N)