📌  相关文章
📜  使得i <j <k且a [k] <a [i] <a [j]的数组(i,j,k)中的三元组的计数

📅  最后修改于: 2021-05-13 23:49:18             🧑  作者: Mango

给定N个整数的数组arr [] ,任务是对数组中的三元组(i,j,k)进行计数以使a [k] i
例子:

天真的方法:想法是迭代3个循环,并检查每个三元组(i,j,k)是否满足给定条件。如果是,则增加该三元组并在检查所有三胞胎后打印最终计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count triplets with the
// given conditions
int CountTriplets(int arr[], int n)
{
    int cnt = 0;
 
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
            for (int k = j + 1; k < n; k++)
 
                // If it satisfy the
                // given conditions
                if (arr[k] < arr[i]
                    && arr[i] < arr[j]) {
 
                    cnt += 1;
                }
 
    // Return the final count
    return cnt;
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 2, 5, 1, 3, 0 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << CountTriplets(arr, n)
         << endl;
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to count triplets
// with the given conditions
static int CountTriplets(int arr[], int n)
{
    int cnt = 0;
 
    for(int i = 0; i < n; i++)
       for(int j = i + 1; j < n; j++)
          for(int k = j + 1; k < n; k++)
              
             // If it satisfy the
             // given conditions
             if (arr[k] < arr[i] &&
                 arr[i] < arr[j])
             {
                 cnt += 1;
             }
              
    // Return the final count
    return cnt;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array arr[]
    int arr[] = new int[]{ 2, 5, 1, 3, 0 };
 
    int n = arr.length;
     
    System.out.print(CountTriplets(arr, n));
}
}
 
// This code is contributed by Pratima Pandey


Python3
# Python3 program for the above approach
 
# Function to count triplets with the
# given conditions
def CountTriplets(arr, n):
 
    cnt = 0;
 
    for i in range(0, n):
        for j in range(i + 1, n):
            for k in range(j + 1, n):
 
                # If it satisfy the
                # given conditions
                if (arr[k] < arr[i] and arr[i] < arr[j]):
                    cnt += 1;
                 
    # Return the final count
    return cnt;
 
# Driver Code
 
# Given array arr[]
arr = [ 2, 5, 1, 3, 0 ];
 
n = len(arr);
 
# Function Call
print(CountTriplets(arr, n))
 
# This code is contributed by Code_Mech


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to count triplets
// with the given conditions
static int CountTriplets(int []arr, int n)
{
    int cnt = 0;
 
    for(int i = 0; i < n; i++)
       for(int j = i + 1; j < n; j++)
          for(int k = j + 1; k < n; k++)
              
             // If it satisfy the
             // given conditions
             if (arr[k] < arr[i] &&
                 arr[i] < arr[j])
             {
                 cnt += 1;
             }
             
    // Return the final count
    return cnt;
}
 
// Driver Code
public static void Main(string[] args)
{
     
    // Given array arr[]
    int []arr = new int[]{ 2, 5, 1, 3, 0 };
 
    int n = arr.Length;
     
    Console.Write(CountTriplets(arr, n));
}
}
 
// This code is contributed by Ritik Bansal


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count  triplets
int CountTriplets(int a[], int n)
{
 
    // To store count of total triplets
    int ans = 0;
 
    for (int i = 0; i < n; i++) {
 
        // Initialize count to zero
        int cnt = 0;
 
        for (int j = i + 1; j < n; j++) {
 
            // If a[j] > a[i] then,
            // increment cnt
            if (a[j] > a[i])
                cnt++;
 
            // If a[j] < a[i], then
            // it mean we have found a[k]
            // such that a[k] < a[i] < a[j]
            else
                ans += cnt;
        }
    }
 
    // Return the final count
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 5, 1, 3, 0 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << CountTriplets(arr, n) << endl;
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to count triplets
static int CountTriplets(int a[], int n)
{
 
    // To store count of total triplets
    int ans = 0;
 
    for (int i = 0; i < n; i++)
    {
 
        // Initialize count to zero
        int cnt = 0;
 
        for (int j = i + 1; j < n; j++)
        {
 
            // If a[j] > a[i] then,
            // increment cnt
            if (a[j] > a[i])
                cnt++;
 
            // If a[j] < a[i], then
            // it mean we have found a[k]
            // such that a[k] < a[i] < a[j]
            else
                ans += cnt;
        }
    }
 
    // Return the final count
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 2, 5, 1, 3, 0 };
 
    int n = arr.length;
 
    System.out.print(CountTriplets(arr, n));
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python3 program for
# the above approach
 
# Function to count  triplets
def CountTriplets(a, n):
 
    # To store count
    # of total triplets
    ans = 0
 
    for i in range (n):
 
        # Initialize count to zero
        cnt = 0
 
        for j in range (i + 1 , n):
 
            # If a[j] > a[i] then,
            # increment cnt
            if (a[j] > a[i]):
                cnt += 1
 
            # If a[j] < a[i], then
            # it mean we have found a[k]
            # such that a[k] < a[i] < a[j]
            else:
                ans += cnt
      
    # Return the final count
    return ans
 
# Driver code
if __name__ == "__main__": 
    arr = [2, 5, 1, 3, 0]
    n = len(arr)
    print (CountTriplets(arr, n))
 
# This code is contributed by Chitranayal


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to count triplets
static int CountTriplets(int []a, int n)
{
 
    // To store count of total triplets
    int ans = 0;
 
    for (int i = 0; i < n; i++)
    {
 
        // Initialize count to zero
        int cnt = 0;
 
        for (int j = i + 1; j < n; j++)
        {
 
            // If a[j] > a[i] then,
            // increment cnt
            if (a[j] > a[i])
                cnt++;
 
            // If a[j] < a[i], then
            // it mean we have found a[k]
            // such that a[k] < a[i] < a[j]
            else
                ans += cnt;
        }
    }
 
    // Return the final count
    return ans;
}
 
// Driver code
public static void Main()
{
    int []arr = { 2, 5, 1, 3, 0 };
 
    int n = arr.Length;
 
    Console.Write(CountTriplets(arr, n));
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
4

时间复杂度: O(N 3 )
辅助空间: O(1)

高效的方法:我们可以使用以下步骤将复杂度从N ^ 3降低到N ^ 2:

  1. 运行两个循环以查找对(i,j) ,使i arr [j]> arr [i]并使这些对的计数保持为cnt
  2. 在上面的循环中,如果存在任何元素,例如arr [j] 则将三元组的计数增加cnt,因为当前元素是第K个元素,从而a [k] 表示三元组i

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to count  triplets
int CountTriplets(int a[], int n)
{
 
    // To store count of total triplets
    int ans = 0;
 
    for (int i = 0; i < n; i++) {
 
        // Initialize count to zero
        int cnt = 0;
 
        for (int j = i + 1; j < n; j++) {
 
            // If a[j] > a[i] then,
            // increment cnt
            if (a[j] > a[i])
                cnt++;
 
            // If a[j] < a[i], then
            // it mean we have found a[k]
            // such that a[k] < a[i] < a[j]
            else
                ans += cnt;
        }
    }
 
    // Return the final count
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 5, 1, 3, 0 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << CountTriplets(arr, n) << endl;
 
    return 0;
}

Java

// Java program for the above approach
class GFG{
 
// Function to count triplets
static int CountTriplets(int a[], int n)
{
 
    // To store count of total triplets
    int ans = 0;
 
    for (int i = 0; i < n; i++)
    {
 
        // Initialize count to zero
        int cnt = 0;
 
        for (int j = i + 1; j < n; j++)
        {
 
            // If a[j] > a[i] then,
            // increment cnt
            if (a[j] > a[i])
                cnt++;
 
            // If a[j] < a[i], then
            // it mean we have found a[k]
            // such that a[k] < a[i] < a[j]
            else
                ans += cnt;
        }
    }
 
    // Return the final count
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 2, 5, 1, 3, 0 };
 
    int n = arr.length;
 
    System.out.print(CountTriplets(arr, n));
}
}
 
// This code is contributed by shivanisinghss2110

Python3

# Python3 program for
# the above approach
 
# Function to count  triplets
def CountTriplets(a, n):
 
    # To store count
    # of total triplets
    ans = 0
 
    for i in range (n):
 
        # Initialize count to zero
        cnt = 0
 
        for j in range (i + 1 , n):
 
            # If a[j] > a[i] then,
            # increment cnt
            if (a[j] > a[i]):
                cnt += 1
 
            # If a[j] < a[i], then
            # it mean we have found a[k]
            # such that a[k] < a[i] < a[j]
            else:
                ans += cnt
      
    # Return the final count
    return ans
 
# Driver code
if __name__ == "__main__": 
    arr = [2, 5, 1, 3, 0]
    n = len(arr)
    print (CountTriplets(arr, n))
 
# This code is contributed by Chitranayal

C#

// C# program for the above approach
using System;
class GFG{
 
// Function to count triplets
static int CountTriplets(int []a, int n)
{
 
    // To store count of total triplets
    int ans = 0;
 
    for (int i = 0; i < n; i++)
    {
 
        // Initialize count to zero
        int cnt = 0;
 
        for (int j = i + 1; j < n; j++)
        {
 
            // If a[j] > a[i] then,
            // increment cnt
            if (a[j] > a[i])
                cnt++;
 
            // If a[j] < a[i], then
            // it mean we have found a[k]
            // such that a[k] < a[i] < a[j]
            else
                ans += cnt;
        }
    }
 
    // Return the final count
    return ans;
}
 
// Driver code
public static void Main()
{
    int []arr = { 2, 5, 1, 3, 0 };
 
    int n = arr.Length;
 
    Console.Write(CountTriplets(arr, n));
}
}
 
// This code is contributed by Code_Mech

Java脚本


输出:
4

时间复杂度: O(N 2 )
辅助空间: O(1)