Count of triplets whose indices and element at that indices are in increasing order
Last Updated :
22 Mar, 2023
Given an array arr[] consisting of N integers, the task is to find the number of triplets whose indices and elements at that indices are in increasing order.
Examples:
Input: arr[] = {1, 2, 4, 3}
Output: 2
Explanation: There are 2 possible triplets in the given array such that the elements are in increasing order, i.e, {1, 2, 4} and {1, 2, 3}.
Input: arr[] = {1, 2, 3, 4, 5}
Output: 10
Naive Approach: The given problem can be solved by iterating over all the possible triplets (i, j, k) of the given array and keep track of the number of triplets whose indices and elements at that indices are in increasing order. After checking for all the triplets, print the total count of triplets obtained.
C++
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int arr[], int n)
{
int totalCount = 0;
for ( int i=0;i<n;i++)
{
for ( int j=0;j<n;j++)
{
for ( int k=0;k<n;k++)
{
if (arr[i]<arr[j]&&arr[j]<arr[k])
totalCount++;
}
}
}
return totalCount;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << countTriplets(arr, N);
return 0;
}
|
Java
import java.util.*;
public class Main {
static int countTriplets( int arr[], int n)
{
int totalCount = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++) {
for ( int k = 0 ; k < n; k++) {
if (arr[i] < arr[j]
&& arr[j] < arr[k]) {
totalCount++;
}
}
}
}
return totalCount;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int N = arr.length;
System.out.println(countTriplets(arr, N));
}
}
|
C#
using System;
public class GFG{
static int countTriplets( int [] arr, int n)
{
int totalCount = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
for ( int k = 0; k < n; k++) {
if (arr[i] < arr[j]
&& arr[j] < arr[k]) {
totalCount++;
}
}
}
}
return totalCount;
}
static public void Main()
{
int [] arr = { 1, 2, 3, 4, 5 };
int N = arr.Length;
Console.WriteLine(countTriplets(arr, N));
}
}
|
Python3
def countTriplets(arr, n):
totalCount = 0
for i in range (n):
for j in range (n):
for k in range (n):
if (arr[i] < arr[j] and arr[j] < arr[k]):
totalCount + = 1
return totalCount
arr = [ 1 , 2 , 3 , 4 , 5 ]
N = len (arr)
print (countTriplets(arr, N))
|
Javascript
function countTriplets(arr, n) {
let totalCount = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
for (let k = 0; k < n; k++) {
if (arr[i] < arr[j] && arr[j] < arr[k]) {
totalCount++;
}
}
}
}
return totalCount;
}
const arr = [1, 2, 3, 4, 5];
const N = arr.length;
console.log(countTriplets(arr, N));
|
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by the observation that for any given index i, the number of triplets having arr[i] as their middle element will be the count of integers smaller than arr[i] in the range [1, i – 1] * count of integers larger that arr[i] in the range [i + 1, N]. Below are the steps to follow to solve the given problem using the above observation:
- Initialize a variable, totalCount as 0 which stores the count of valid triplets.
- Iterate array arr[] over the range [1, N – 2] using the variable i and perform the following steps:
- Count the number of values less than arr[i] over the range [0, i – 1] and store the value in a variable K1.
- Count the number of values greater than arr[i] over the range [i + 1, N – 1], and store the value in a variable K2.
- Add the value of K1*K2 to the totalCount.
- The value stored in totalCount is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSmaller( int arr[], int N, int value)
{
int count = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] < value)
count++;
}
return count;
}
int countLarger( int arr[], int i, int N,
int value)
{
int count = 0;
while (i < N) {
if (arr[i] > value)
count++;
i++;
}
return count;
}
int countTriplets( int arr[], int N)
{
int totalCount = 0;
for ( int i = 1; i < N - 1; i++) {
int K1 = countSmaller(arr, i,
arr[i]);
int K2 = countLarger(arr, i + 1,
N, arr[i]);
totalCount += K1 * K2;
}
return totalCount;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << countTriplets(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countSmaller( int arr[], int N, int value)
{
int count = 0 ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] < value)
count++;
}
return count;
}
static int countLarger( int arr[], int i, int N,
int value)
{
int count = 0 ;
while (i < N) {
if (arr[i] > value)
count++;
i++;
}
return count;
}
static int countTriplets( int arr[], int N)
{
int totalCount = 0 ;
for ( int i = 1 ; i < N - 1 ; i++) {
int K1 = countSmaller(arr, i, arr[i]);
int K2 = countLarger(arr, i + 1 , N, arr[i]);
totalCount += K1 * K2;
}
return totalCount;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int N = arr.length;
System.out.println(countTriplets(arr, N));
}
}
|
Python3
def countSmaller(arr, N, value):
count = 0
for i in range (N):
if (arr[i] < value):
count + = 1
return count
def countLarger( arr, i, N, value):
count = 0
while (i < N):
if (arr[i] > value):
count + = 1
i + = 1
return count
def countTriplets( arr, N):
totalCount = 0
for i in range ( 0 , N - 1 ):
K1 = countSmaller(arr, i, arr[i])
K2 = countLarger(arr, i + 1 , N, arr[i])
totalCount + = K1 * K2
return totalCount
arr = [ 1 , 2 , 3 , 4 , 5 ];
N = len (arr)
print (countTriplets(arr, N))
|
C#
using System;
class GFG {
static int countSmaller( int [] arr, int N, int value)
{
int count = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] < value)
count++;
}
return count;
}
static int countLarger( int [] arr, int i, int N,
int value)
{
int count = 0;
while (i < N) {
if (arr[i] > value)
count++;
i++;
}
return count;
}
static int countTriplets( int [] arr, int N)
{
int totalCount = 0;
for ( int i = 1; i < N - 1; i++) {
int K1 = countSmaller(arr, i, arr[i]);
int K2 = countLarger(arr, i + 1, N, arr[i]);
totalCount += K1 * K2;
}
return totalCount;
}
public static void Main( string [] args)
{
int [] arr = { 1, 2, 3, 4, 5 };
int N = arr.Length;
Console.WriteLine(countTriplets(arr, N));
}
}
|
Javascript
<script>
function countSmaller(arr, N, value)
{
let count = 0;
for (let i = 0; i < N; i++) {
if (arr[i] < value)
count++;
}
return count;
}
function countLarger(arr, i, N,
value) {
let count = 0;
while (i < N) {
if (arr[i] > value)
count++;
i++;
}
return count;
}
function countTriplets(arr, N) {
let totalCount = 0;
for (let i = 1; i < N - 1; i++) {
let K1 = countSmaller(arr, i,
arr[i]);
let K2 = countLarger(arr, i + 1,
N, arr[i]);
totalCount += K1 * K2;
}
return totalCount;
}
let arr = [1, 2, 3, 4, 5];
let N = arr.length;
document.write(countTriplets(arr, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...