给定大小为N的数组arr [] ,任务是重新排列数组元素,以使满足条件arr [i]> arr [j]
例子:
Input: arr[] = {1, 4, 3, 3, 2, 2, 5}
Output:
Count of triplets: 3
Array: {3, 1, 3, 2, 4, 2, 5}
Explanation:
Rearrange the array to {3, 1, 3, 2, 4, 2, 5} which gives the maximum count of triplets {(3, 1, 3), (3, 2, 4), (4, 2, 5)} that satisfies the given condition.
Therefore, the required count of triplets is 3.
Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 7}
Output:
Count of triplets = 3
Array = {5, 1, 6, 2, 7, 3, 7, 4}
天真的方法:解决问题的最简单方法是生成数组的所有可能排列,并计算满足给定条件的三元组的数量。最后,打印三元组的数目和给定数组的排列,其中包含给定条件下三元组的最大数目。
时间复杂度: O(N!)
辅助空间: O(1)
高效方法:为了优化上述方法,其思想是先对给定数组进行排序,然后将给定数组的前半部分放置在临时数组中的奇数索引处,并将数组的后半部分放置在偶数索引处。最后,在给定条件下打印临时数组和三元组的计数。请按照以下步骤解决问题:
- 初始化一个临时数组,例如temp [] ,以存储给定数组的排列。
- 对给定数组arr []进行排序。
- 将给定数组的前半部分放置在temp数组的奇数索引处。
- 将给定数组的最后一半放在temp数组的偶数索引处。
- 最后,在temp []和temp数组中打印具有给定条件的三元组的计数。
以下是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to rearrange the array
// to maximize the count of triplets
void ctTriplets(int arr[], int N)
{
// Sort the given array
sort(arr, arr + N);
// Stores the permutations
// of the given array
vector temp(N);
// Stores the index
// of the given array
int index = 0;
// Place the first half
// of the given array
for (int i = 1; i < N;
i += 2) {
temp[i]
= arr[index++];
}
// Place the last half
// of the given array
for (int i = 0; i < N;
i += 2) {
temp[i]
= arr[index++];
}
// Stores the count
// of triplets
int ct = 0;
for (int i = 0; i < N; i++) {
// Check the given conditions
if (i > 0 && i + 1 < N) {
if (temp[i] < temp[i + 1]
&& temp[i] < temp[i - 1]) {
ct++;
}
}
}
cout << "Count of triplets:"
<< ct << endl;
cout << "Array:";
for (int i = 0; i < N;
i++) {
cout << temp[i] << " ";
}
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
ctTriplets(arr, N);
}
Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.Arrays;
class GFG{
// Function to rearrange the array
// to maximize the count of triplets
static void ctTriplets(int arr[], int N)
{
// Sort the given array
Arrays.sort(arr);
// Stores the permutations
// of the given array
int[] temp = new int[N];
// Stores the index
// of the given array
int index = 0;
// Place the first half
// of the given array
for(int i = 1; i < N; i += 2)
{
temp[i] = arr[index++];
}
// Place the last half
// of the given array
for(int i = 0; i < N; i += 2)
{
temp[i] = arr[index++];
}
// Stores the count
// of triplets
int ct = 0;
for(int i = 0; i < N; i++)
{
// Check the given conditions
if (i > 0 && i + 1 < N)
{
if (temp[i] < temp[i + 1] &&
temp[i] < temp[i - 1])
{
ct++;
}
}
}
System.out.println("Count of triplets:" + ct );
System.out.print("Array:");
for(int i = 0; i < N; i++)
{
System.out.print(temp[i] + " ");
}
}
// Driver Code
public static void main (String[] args)
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int N = arr.length;
ctTriplets(arr, N);
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program to implement
# the above approach
# Function to rearrange the array
# to maximize the count of triplets
def ctTriplets(arr, N):
# Sort the given array
arr.sort()
# Stores the permutations
# of the given array
temp = [0] * N
# Stores the index
# of the given array
index = 0
# Place the first half
# of the given array
for i in range(1, N, 2):
temp[i] = arr[index]
index += 1
# Place the last half
# of the given array
for i in range(0, N, 2):
temp[i] = arr[index]
index += 1
# Stores the count
# of triplets
ct = 0
for i in range(N):
# Check the given conditions
if (i > 0 and i + 1 < N):
if (temp[i] < temp[i + 1] and
temp[i] < temp[i - 1]):
ct += 1
print("Count of triplets:", ct)
print("Array:", end = "")
for i in range(N):
print(temp[i], end = " ")
# Driver Code
arr = [ 1, 2, 3, 4, 5, 6 ]
N = len(arr)
ctTriplets(arr, N)
# This code is contributed by sanjoy_62
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to rearrange the array
// to maximize the count of triplets
static void ctTriplets(int[] arr, int N)
{
// Sort the given array
Array.Sort(arr);
// Stores the permutations
// of the given array
int[] temp = new int[N];
// Stores the index
// of the given array
int index = 0;
// Place the first half
// of the given array
for(int i = 1; i < N; i += 2)
{
temp[i] = arr[index++];
}
// Place the last half
// of the given array
for(int i = 0; i < N; i += 2)
{
temp[i] = arr[index++];
}
// Stores the count
// of triplets
int ct = 0;
for(int i = 0; i < N; i++)
{
// Check the given conditions
if (i > 0 && i + 1 < N)
{
if (temp[i] < temp[i + 1] &&
temp[i] < temp[i - 1])
{
ct++;
}
}
}
Console.WriteLine("Count of triplets:" + ct );
Console.Write("Array:");
for(int i = 0; i < N; i++)
{
Console.Write(temp[i] + " ");
}
}
// Driver Code
public static void Main ()
{
int[] arr = { 1, 2, 3, 4, 5, 6 };
int N = arr.Length;
ctTriplets(arr, N);
}
}
// This code is contributed by sanjoy_62
Javascript
Count of triplets:2
Array:4 1 5 2 6 3
时间复杂度:O(N)
辅助空间:O(N)