给定一个大小为N的数组arr[] ,任务是重新排列数组元素以最大化满足条件arr[i] > arr[j] < arr[k]的三元组 ( i, j, k ) 的计数并且i < j < k 。
例子:
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数组中的给定条件打印三元组的计数。
下面是上述方法的实现:
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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live