其索引和该索引处的元素按递增顺序排列的三元组计数
给定一个由N个整数组成的数组arr[] ,任务是找到其索引和该索引处的元素按递增顺序排列的三元组的数量。
例子:
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
朴素方法:给定问题可以通过迭代给定数组的所有可能的三元组(i,j,k)并跟踪其索引和索引处的元素按递增顺序排列的三元组的数量来解决。检查所有三元组后,打印获得的三元组总数。
时间复杂度: O(N 3 )
辅助空间: O(1)
有效方法:上述方法也可以通过观察来优化,即对于任何给定的索引 i,以 arr[i] 作为中间元素的三元组的数量将是[1] 范围内小于 arr[i] 的整数的计数, i – 1] * 在 [i + 1, N] 范围内大于 arr[i] 的整数计数。以下是使用上述观察解决给定问题的步骤:
- 将变量totalCount初始化为 0,该变量存储有效三元组的计数。
- 使用变量i在[1, N – 2]范围内迭代数组arr[]并执行以下步骤:
- 在[0, i – 1]范围内计算小于arr[i]的值的数量,并将该值存储在变量K1中。
- 在[i + 1, N – 1]范围内计算大于arr[i]的值的数量,并将该值存储在变量K2中。
- 将K1*K2的值添加到totalCount 。
- 存储在totalCount中的值是所需的答案。
下面是上述方法的实现:
C++
// C++ program of the above approach
#include
using namespace std;
// Function to find number of integers
// smaller than value in range [0, N]
int countSmaller(int arr[], int N, int value)
{
// Stores the count of integers
int count = 0;
// Iterate the given array
for (int i = 0; i < N; i++) {
if (arr[i] < value)
count++;
}
// Return total count
return count;
}
// Function to find number of integers
// greater than value in range [i, N]
int countLarger(int arr[], int i, int N,
int value)
{
// Stores the count of integers
int count = 0;
// Iterate the given array
while (i < N) {
if (arr[i] > value)
count++;
i++;
}
// Return total count
return count;
}
// Function to find the count of triplets
// whose indices and elements at that indices
// are also in increasing order
int countTriplets(int arr[], int N)
{
// Stores the count of valid triplets
int totalCount = 0;
// Loop to iterate over the array
for (int i = 1; i < N - 1; i++) {
// Count of smaller elements than
// arr[i] in range [0, i-1]
int K1 = countSmaller(arr, i,
arr[i]);
// Count of greater elements than
// arr[i] in range [i+1, N]
int K2 = countLarger(arr, i + 1,
N, arr[i]);
// Add to total count
totalCount += K1 * K2;
}
// Return Answer
return totalCount;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << countTriplets(arr, N);
return 0;
}
Java
// Java program of the above approach
import java.io.*;
class GFG {
// Function to find number of integers
// smaller than value in range [0, N]
static int countSmaller(int arr[], int N, int value)
{
// Stores the count of integers
int count = 0;
// Iterate the given array
for (int i = 0; i < N; i++) {
if (arr[i] < value)
count++;
}
// Return total count
return count;
}
// Function to find number of integers
// greater than value in range [i, N]
static int countLarger(int arr[], int i, int N,
int value)
{
// Stores the count of integers
int count = 0;
// Iterate the given array
while (i < N) {
if (arr[i] > value)
count++;
i++;
}
// Return total count
return count;
}
// Function to find the count of triplets
// whose indices and elements at that indices
// are also in increasing order
static int countTriplets(int arr[], int N)
{
// Stores the count of valid triplets
int totalCount = 0;
// Loop to iterate over the array
for (int i = 1; i < N - 1; i++) {
// Count of smaller elements than
// arr[i] in range [0, i-1]
int K1 = countSmaller(arr, i, arr[i]);
// Count of greater elements than
// arr[i] in range [i+1, N]
int K2 = countLarger(arr, i + 1, N, arr[i]);
// Add to total count
totalCount += K1 * K2;
}
// Return Answer
return totalCount;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = arr.length;
System.out.println(countTriplets(arr, N));
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python program of the above approach
# Function to find number of integers
# smaller than value in range [0, N]
def countSmaller(arr, N, value):
# Stores the count of integers
count = 0
# Iterate the given array
for i in range (N):
if (arr[i] < value):
count += 1
# Return total count
return count
# Function to find number of integers
# greater than value in range [i, N]
def countLarger( arr, i, N, value):
# Stores the count of integers
count = 0
# Iterate the given array
while (i < N):
if (arr[i] > value):
count += 1
i += 1
# Return total count
return count
# Function to find the count of triplets
# whose indices and elements at that indices
# are also in increasing order
def countTriplets( arr, N):
# Stores the count of valid triplets
totalCount = 0
# Loop to iterate over the array
for i in range(0, N - 1):
# Count of smaller elements than
# arr[i] in range [0, i-1]
K1 = countSmaller(arr, i, arr[i])
# Count of greater elements than
# arr[i] in range [i+1, N]
K2 = countLarger(arr, i + 1, N, arr[i])
# Add to total count
totalCount += K1 * K2
# Return Answer
return totalCount
# Driver Code
arr = [ 1, 2, 3, 4, 5 ];
N = len(arr)
print(countTriplets(arr, N))
# This code is contributed by shivanisinghss2110
C#
// C# program of the above approach
using System;
class GFG {
// Function to find number of integers
// smaller than value in range [0, N]
static int countSmaller(int[] arr, int N, int value)
{
// Stores the count of integers
int count = 0;
// Iterate the given array
for (int i = 0; i < N; i++) {
if (arr[i] < value)
count++;
}
// Return total count
return count;
}
// Function to find number of integers
// greater than value in range [i, N]
static int countLarger(int[] arr, int i, int N,
int value)
{
// Stores the count of integers
int count = 0;
// Iterate the given array
while (i < N) {
if (arr[i] > value)
count++;
i++;
}
// Return total count
return count;
}
// Function to find the count of triplets
// whose indices and elements at that indices
// are also in increasing order
static int countTriplets(int[] arr, int N)
{
// Stores the count of valid triplets
int totalCount = 0;
// Loop to iterate over the array
for (int i = 1; i < N - 1; i++) {
// Count of smaller elements than
// arr[i] in range [0, i-1]
int K1 = countSmaller(arr, i, arr[i]);
// Count of greater elements than
// arr[i] in range [i+1, N]
int K2 = countLarger(arr, i + 1, N, arr[i]);
// Add to total count
totalCount += K1 * K2;
}
// Return Answer
return totalCount;
}
// Driver Code
public static void Main(string[] args)
{
int[] arr = { 1, 2, 3, 4, 5 };
int N = arr.Length;
Console.WriteLine(countTriplets(arr, N));
}
}
// This code is contributed by ukasp.
Javascript
输出:
10
时间复杂度: O(N 2 )
辅助空间: O(1)