📌  相关文章
📜  计算具有递增索引的不同数组元素的非等距三元组

📅  最后修改于: 2021-05-17 20:03:32             🧑  作者: Mango

给定大小N的数组arr []仅由0 s, 1 s和2 s组成,任务是查找包含不同数组元素的索引(i,j,k)的三元组的计数,使得i 并且数组元素不是等距的,即(j – i)!=(k – j)

例子:

方法:想法是将数组元素0 s, 1 s和2 s的索引存储在三个单独的数组中,然后找到满足给定条件的三元组计数。请按照以下步骤解决问题:

  • 初始化两个数组,例如zero_i []one_i [] ,以分别存储给定数组中0 s和1 s的索引。
  • 初始化一个映射,例如mp ,以存储给定数组中2 s的索引。
  • 通过将zero_i []one_i []mp的大小相乘,找到所有可能的三元组的总数。
  • 现在,减去所有违反给定条件的三元组。
  • 为了查找这样的三元组,请遍历数组zero_i []one_i []并尝试在Map中查找违反条件的第三个索引。
  • 为了找到违反条件的第三个索引,出现以下三种情况:
    1. 第三索引与两个索引等距,并且存在于它们之间。
    2. 第三索引与两个索引等距,并位于第一索引的左侧。
    3. 第三个索引与两个索引等距,并位于第二个索引的右侧。
  • 从三胞胎总数中删除所有此类三胞胎。
  • 最后,打印获得的三胞胎总数。

下面是上述方法的实现:

C++14
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the total count of
// triplets (i, j, k) such that i < j < k
// and (j - i) != (k - j)
int countTriplets(int* arr, int N)
{
 
    // Stores indices of 0s
    vector zero_i;
 
    // Stores indices of 1s
    vector one_i;
 
    // Stores indices of 2s
    unordered_map mp;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // If current array element
        // is 0
        if (arr[i] == 0)
            zero_i.push_back(i + 1);
 
        // If current array element is 1
        else if (arr[i] == 1)
            one_i.push_back(i + 1);
 
        // If current array element
        // is 2
        else
            mp[i + 1] = 1;
    }
 
    // Total count of triplets
    int total = zero_i.size()
                * one_i.size() * mp.size();
 
    // Traverse  the array zero_i[]
    for (int i = 0; i < zero_i.size();
         i++) {
 
        // Traverse the array one_i[]
        for (int j = 0; j < one_i.size();
             j++) {
 
            // Stores index of 0s
            int p = zero_i[i];
 
            // Stores index of 1s
            int q = one_i[j];
 
            // Stores third element of
            // triplets that does not
            // satisfy the condition
            int r = 2 * p - q;
 
            // If r present
            // in the map
            if (mp[r] > 0)
                total--;
 
            // Update r
            r = 2 * q - p;
 
            // If r present
            // in the map
            if (mp[r] > 0)
                total--;
 
            // Update r
            r = (p + q) / 2;
 
            // If r present in the map
            // and equidistant
            if (mp[r] > 0 && abs(r - p) == abs(r - q))
                total--;
        }
    }
 
    // Print the obtained count
    cout << total;
}
 
// Driver Code
int main()
{
    int arr[] = { 0, 1, 2, 1 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    countTriplets(arr, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to find the total count of
// triplets (i, j, k) such that i < j < k
// and (j - i) != (k - j)
static void countTriplets(int []arr, int N)
{
     
    // Stores indices of 0s
    Vector zero_i = new Vector();
 
    // Stores indices of 1s
    Vector one_i = new Vector();
 
    // Stores indices of 2s
    HashMap mp = new HashMap();
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // If current array element
        // is 0
        if (arr[i] == 0)
            zero_i.add(i + 1);
 
        // If current array element is 1
        else if (arr[i] == 1)
            one_i.add(i + 1);
 
        // If current array element
        // is 2
        else
            mp.put(i + 1, 1);
    }
 
    // Total count of triplets
    int total = zero_i.size() *
                 one_i.size() * mp.size();
 
    // Traverse  the array zero_i[]
    for(int i = 0; i < zero_i.size(); i++)
    {
         
        // Traverse the array one_i[]
        for(int j = 0; j < one_i.size(); j++)
        {
             
            // Stores index of 0s
            int p = zero_i.get(i);
 
            // Stores index of 1s
            int q = one_i.get(j);
 
            // Stores third element of
            // triplets that does not
            // satisfy the condition
            int r = 2 * p - q;
 
            // If r present
            // in the map
            if (mp.containsKey(r) && mp.get(r) > 0)
                total--;
 
            // Update r
            r = 2 * q - p;
 
            // If r present
            // in the map
            if (mp.containsKey(r) && mp.get(r) > 0)
                total--;
 
            // Update r
            r = (p + q) / 2;
 
            // If r present in the map
            // and equidistant
            if (mp.containsKey(r) &&
                    mp.get(r) > 0 &&
                  Math.abs(r - p) == Math.abs(r - q))
                total--;
        }
    }
 
    // Print the obtained count
    System.out.print(total);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 0, 1, 2, 1 };
    int N = arr.length;
 
    countTriplets(arr, N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to implement
# the above approach
 
# Function to find the total count of
# triplets (i, j, k) such that i < j < k
# and (j - i) != (k - j)
def countTriplets(arr, N):
 
    # Stores indices of 0s
    zero_i = []
 
    # Stores indices of 1s
    one_i = []
 
    # Stores indices of 2s
    mp = {}
 
    # Traverse the array
    for i in range(N):
 
        # If current array element
        # is 0
        if (arr[i] == 0):
            zero_i.append(i + 1)
 
        # If current array element is 1
        elif (arr[i] == 1):
            one_i.append(i + 1)
 
        # If current array element
        # is 2
        else:
            mp[i + 1] = 1
 
    # Total count of triplets
    total = len(zero_i) * len(one_i) * len(mp)
 
    # Traverse  the array zero_i[]
    for i in range(len(zero_i)):
 
        # Traverse the array one_i[]
        for j in range(len(one_i)):
 
            # Stores index of 0s
            p = zero_i[i]
 
            # Stores index of 1s
            q = one_i[j]
 
            # Stores third element of
            # triplets that does not
            # satisfy the condition
            r = 2 * p - q
 
            # If r present
            # in the map
            if (r in mp):
                total -= 1
 
            # Update r
            r = 2 * q - p
 
            # If r present
            # in the map
            if (r in mp):
                total -= 1
 
            # Update r
            r = (p + q) // 2
 
            # If r present in the map
            # and equidistant
            if ((r in mp) and abs(r - p) == abs(r - q)):
                total -= 1
 
    # Prthe obtained count
    print (total)
 
# Driver Code
if __name__ == '__main__':
    arr = [0, 1, 2, 1]
    N = len(arr)
    countTriplets(arr, N)
 
    # This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the total count of
// triplets (i, j, k) such that i < j < k
// and (j - i) != (k - j)
static void countTriplets(int []arr, int N)
{
     
    // Stores indices of 0s
    List zero_i = new List();
 
    // Stores indices of 1s
    List one_i = new List();
 
    // Stores indices of 2s
    Dictionary mp = new Dictionary();
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // If current array element
        // is 0
        if (arr[i] == 0)
            zero_i.Add(i + 1);
 
        // If current array element is 1
        else if (arr[i] == 1)
            one_i.Add(i + 1);
 
        // If current array element
        // is 2
        else
            mp.Add(i + 1, 1);
    }
 
    // Total count of triplets
    int total = zero_i.Count *
                 one_i.Count * mp.Count;
 
    // Traverse  the array zero_i[]
    for(int i = 0; i < zero_i.Count; i++)
    {
         
        // Traverse the array one_i[]
        for(int j = 0; j < one_i.Count; j++)
        {
             
            // Stores index of 0s
            int p = zero_i[i];
 
            // Stores index of 1s
            int q = one_i[j];
 
            // Stores third element of
            // triplets that does not
            // satisfy the condition
            int r = 2 * p - q;
 
            // If r present
            // in the map
            if (mp.ContainsKey(r) && mp[r] > 0)
                total--;
 
            // Update r
            r = 2 * q - p;
 
            // If r present
            // in the map
            if (mp.ContainsKey(r) && mp[r] > 0)
                total--;
 
            // Update r
            r = (p + q) / 2;
 
            // If r present in the map
            // and equidistant
            if (mp.ContainsKey(r) &&
                    mp[r] > 0 &&
                  Math.Abs(r - p) == Math.Abs(r - q))
                total--;
        }
    }
 
    // Print the obtained count
    Console.Write(total);
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 0, 1, 2, 1 };
    int N = arr.Length;
    countTriplets(arr, N);
}
}
 
// This code contributed by shikhasingrajput


输出:
1

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