📌  相关文章
📜  计数由不同元素子阵列形成的对

📅  最后修改于: 2022-05-13 01:57:52.560000             🧑  作者: Mango

计数由不同元素子阵列形成的对

给定一个数组,计算可以从包含不同数字的所有可能的连续子数组中形成的对数。该数组包含 0 到 n-1 之间的正数,其中 n 是数组的大小。
例子:

Input:  [1, 4, 2, 4, 3, 2]
Output: 8
The subarrays with distinct elements 
are [1, 4, 2], [2, 4, 3] and [4, 3, 2].
There are 8 pairs that can be formed 
from above array.
(1, 4), (1, 2), (4, 2), (2, 4), (2, 3),
(4, 3), (4, 2), (3, 2)


Input:  [1, 2, 2, 3]
Output: 2
There are 2 pairs that can be formed
from above array.
(1, 2), (2, 3)

这个想法是为给定的数组使用滑动窗口。让我们使用从索引左到右的窗口覆盖和访问的布尔数组来标记当前窗口中的元素。窗口不变量是窗口内的所有元素都是不同的。我们继续向右扩展窗口,如果发现重复,我们从左边缩小窗口,直到所有元素再次不同。我们在此过程中更新当前窗口中的对数。观察表明,在扩展窗口中,对的数量可以增加等于窗口大小 - 1 的值。例如,

Expanding Window   Count
  
[1]              Count = 0

[1, 2]           Count += 1 pair  
                 i.e. (1, 2)

[1, 2, 3]        Count += 2 pairs 
                 i.e. (1, 3) and (2, 3)

[1, 2, 3, 4]     Count += 3 pairs 
                 i.e. (1, 4), (2, 4) 
                 and (3, 4)

因此,包含不同元素的上述大小为 4 的窗口的总计数为 6。当窗口缩小时,无需执行任何操作。
下面是这个想法的实现。

C++
// C++ program to count number of distinct pairs
// that can be formed from all possible contiguous
// sub-arrays containing distinct numbers
#include 
using namespace std;

int countPairs(int arr[], int n)
{
    // initialize number of pairs to zero
    int count = 0;

    //Left and right indexes of current window
    int right = 0, left = 0;

    // Boolean array visited to mark elements in
    // current window. Initialized as false
    vector visited(n, false);

    // While right boundary of current window
    // doesn't cross right end
    while (right < n)
    {
        // If current window contains all distinct
        // elements, widen the window toward right
        while (right < n && !visited[arr[right]])
        {
            count += (right - left);
            visited[arr[right]] = true;
            right++;
        }

        // If duplicate is found in current window,
        // then reduce the window from left
        while (left < right && (right != n &&
               visited[arr[right]]))
        {
            visited[arr[left]] = false;
            left++;
        }
    }

    return count;
}

// Driver code
int main()
{
    int arr[] = {1, 4, 2, 4, 3, 2};
    int n = sizeof arr / sizeof arr[0];

    cout << countPairs(arr, n);

    return 0;
}


Java
// Java program to count number of distinct pairs 
// that can be formed from all possible contiguous 
// sub-arrays containing distinct numbers 
class GFG
{

static int countPairs(int arr[], int n) 
{ 
    // initialize number of pairs to zero 
    int count = 0; 

    //Left and right indexes of current window 
    int right = 0, left = 0; 

    // Boolean array visited to mark elements in 
    // current window. Initialized as false 
    boolean visited[] = new boolean[n];
    
    for(int i = 0; i < n; i++)
        visited[i] = false;

    // While right boundary of current window 
    // doesn't cross right end 
    while (right < n) 
    { 
        // If current window contains all distinct 
        // elements, widen the window toward right 
        while (right < n && !visited[arr[right]]) 
        { 
            count += (right - left); 
            visited[arr[right]] = true; 
            right++; 
        } 

        // If duplicate is found in current window, 
        // then reduce the window from left 
        while (left < right && (right != n && 
            visited[arr[right]])) 
        { 
            visited[arr[left]] = false; 
            left++; 
        } 
    } 

    return count; 
} 

// Driver code 
public static void main(String args[])
{ 
    int arr[] = {1, 4, 2, 4, 3, 2}; 
    int n = arr.length; 

    System.out.println( countPairs(arr, n)); 
}
} 

// This code is contributed by Arnab Kundu


Python3
# Python 3 program to count number of distinct 
# pairs that can be formed from all possible 
# contiguous sub-arrays containing distinct numbers

def countPairs(arr, n):
    
    # initialize number of pairs to zero
    count = 0

    # Left and right indexes of 
    # current window
    right = 0
    left = 0

    # Boolean array visited to mark elements 
    # in current window. Initialized as false
    visited = [False for i in range(n)]

    # While right boundary of current 
    # window doesn't cross right end
    while (right < n):
        
        # If current window contains all distinct
        # elements, widen the window toward right
        while (right < n and 
               visited[arr[right]] == False):
            count += (right - left)
            visited[arr[right]] = True
            right += 1

        # If duplicate is found in current window,
        # then reduce the window from left
        while (left < right and (right != n and 
               visited[arr[right]] == True)):
            visited[arr[left]] = False
            left += 1

    return count

# Driver code
if __name__ == '__main__':
    arr = [1, 4, 2, 4, 3, 2]
    n = len(arr)

    print(countPairs(arr, n))

# This code is contributed by
# Sanjit_Prasad


C#
// C# program to count number of distinct pairs 
// that can be formed from all possible contiguous 
// sub-arrays containing distinct numbers 
using System;

class GFG
{

static int countPairs(int []arr, int n) 
{ 
    // initialize number of pairs to zero 
    int count = 0; 

    //Left and right indexes of current window 
    int right = 0, left = 0; 

    // Boolean array visited to mark elements in 
    // current window. Initialized as false 
    bool [] visited = new bool[n];
    
    for(int i = 0; i < n; i++)
        visited[i] = false;

    // While right boundary of current window 
    // doesn't cross right end 
    while (right < n) 
    { 
        // If current window contains all distinct 
        // elements, widen the window toward right 
        while (right < n && !visited[arr[right]]) 
        { 
            count += (right - left); 
            visited[arr[right]] = true; 
            right++; 
        } 

        // If duplicate is found in current window, 
        // then reduce the window from left 
        while (left < right && (right != n && 
            visited[arr[right]])) 
        { 
            visited[arr[left]] = false; 
            left++; 
        } 
    } 

    return count; 
} 

// Driver code 
public static void Main()
{ 
    int [] arr = {1, 4, 2, 4, 3, 2}; 
    int n = arr.Length; 

    Console.Write( countPairs(arr, n)); 
}
} 

// This code is contributed by mohit kumar 29


Javascript


输出:

8