# Rearrange array to maximize count of triplets (i, j, k) such that arr[i] > arr[j] < arr[k] and i < j < k

• Last Updated : 05 May, 2021

Given an array, arr[] of size N, the task is to rearrange the array elements to maximize the count of triplets (i, j, k) that satisfy the condition arr[i] > arr[j] < arr[k] and i < j < k.

Examples:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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}

Naive Approach: The simplest approach to solve the problem is to generate all possible permutations of the array and count the number of triplets that satisfy the given condition. Finally, print the count of triplets and the permutations of the given array which contain the maximum count of triplets with the given condition.

Time Complexity: O(N!)
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, the idea is to first sort the given array and place the first half of the given array at odd indexes and the last half of the array at even indexes in a temporary array. Finally, print the temporary array and count of triplets with the given condition. Follow the steps below to solve the problem:

1. Initialize a temporary array, say temp[], to store the permutations of the given array.
2. Sort the given array, arr[].
3. Place the first half of the given array at odd indexes of the temp array.
4. Place the last half of the given array at even indexes of the temp array.
5. Finally, print the count of triplets with the given conditions in temp[] and temp array.

Below are the implementations of the above approach:

## 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<``int``> 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

 ``
Output:
```Count of triplets:2
Array:4 1 5 2 6 3 ```

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up