📌  相关文章
📜  具有频率大于其他元素频率的元素的最小子阵列

📅  最后修改于: 2021-10-25 11:28:13             🧑  作者: Mango

给定一个正整数数组arr ,任务是找到长度大于 1 的最小长度子数组,其中一个元素出现的次数比任何其他元素都多。

例子:

朴素方法:解决问题的朴素方法可以是找到具有满足给定条件的元素的所有子数组,然后找到所有这些子数组中的最小值。

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

有效的方法:这个问题可以简化为发现如果有任何元素在一个子数组中出现两次,那么它可能是一个潜在的答案。因为这种子数组的最小长度可以是两个相同元素之间的最小距离

这个想法是使用一个额外的数组来维护给定数组中元素的最后一次出现。然后找到元素的最后一次出现与当前位置之间的距离,并找到所有这些长度中的最小值。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find subarray
void FindSubarray(int arr[], int n)
{
    // If the array has only one element,
    // then there is no answer.
    if (n == 1) {
        cout << "No such subarray!"
             << endl;
    }
 
    // Array to store the last occurrences
    // of the elements of the array.
    int vis[n + 1];
    memset(vis, -1, sizeof(vis));
    vis[arr[0]] = 0;
 
    // To maintain the length
    int len = INT_MAX, flag = 0;
 
    // Variables to store
    // start and end indices
    int start, end;
 
    for (int i = 1; i < n; i++) {
        int t = arr[i];
 
        // Check if element is occurring
        // for the second time in the array
        if (vis[t] != -1) {
            // Find distance between last
            // and current index
            // of the element.
            int distance = i - vis[t] + 1;
 
            // If the current distance
            // is less then len
            // update len and
            // set 'start' and 'end'
            if (distance < len) {
                len = distance;
                start = vis[t];
                end = i;
            }
            flag = 1;
        }
 
        // Set the last occurrence
        // of current element to be 'i'.
        vis[t] = i;
    }
 
    // If flag is equal to 0,
    // it means there is no answer.
    if (flag == 0)
        cout << "No such subarray!"
             << endl;
    else {
        for (int i = start; i <= end; i++)
            cout << arr[i] << " ";
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 3, 2, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    FindSubarray(arr, n);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to find subarray
public static void FindSubarray(int[] arr,
                                int n)
{
     
    // If the array has only one element,
    // then there is no answer.
    if (n == 1)
    {
        System.out.println("No such subarray!");
    }
 
    // Array to store the last occurrences
    // of the elements of the array.
    int[] vis = new int[n + 1];
    Arrays.fill(vis, -1);
    vis[arr[0]] = 0;
 
    // To maintain the length
    int len = Integer.MAX_VALUE, flag = 0;
 
    // Variables to store
    // start and end indices
    int start = 0, end = 0;
 
    for(int i = 1; i < n; i++)
    {
        int t = arr[i];
 
        // Check if element is occurring
        // for the second time in the array
        if (vis[t] != -1)
        {
             
            // Find distance between last
            // and current index
            // of the element.
            int distance = i - vis[t] + 1;
 
            // If the current distance
            // is less then len
            // update len and
            // set 'start' and 'end'
            if (distance < len)
            {
                len = distance;
                start = vis[t];
                end = i;
            }
            flag = 1;
        }
 
        // Set the last occurrence
        // of current element to be 'i'.
        vis[t] = i;
    }
     
    // If flag is equal to 0,
    // it means there is no answer.
    if (flag == 0)
        System.out.println("No such subarray!");
         
    else
    {
        for(int i = start; i <= end; i++)
            System.out.print(arr[i] + " ");
    }
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 2, 3, 2, 4, 5 };
    int n = arr.length;
 
    FindSubarray(arr, n);
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program for the above approach
import sys
 
# Function to find subarray
def FindSubarray(arr, n):
 
    # If the array has only one element,
    # then there is no answer.
    if (n == 1):
        print("No such subarray!")
 
    # Array to store the last occurrences
    # of the elements of the array.
    vis = [-1] * (n + 1)
    vis[arr[0]] = 0
 
    # To maintain the length
    length = sys.maxsize
    flag = 0
     
    for i in range(1, n):
        t = arr[i]
 
        # Check if element is occurring
        # for the second time in the array
        if (vis[t] != -1):
             
            # Find distance between last
            # and current index
            # of the element.
            distance = i - vis[t] + 1
 
            # If the current distance
            # is less then len
            # update len and
            # set 'start' and 'end'
            if (distance < length):
                length = distance
                start = vis[t]
                end = i
             
            flag = 1
 
        # Set the last occurrence
        # of current element to be 'i'.
        vis[t] = i
 
    # If flag is equal to 0,
    # it means there is no answer.
    if (flag == 0):
        print("No such subarray!")
    else:
        for i in range(start, end + 1):
            print(arr[i], end = " ")
 
# Driver Code
if __name__ == "__main__":
 
    arr = [ 2, 3, 2, 4, 5 ]
    n = len(arr)
 
    FindSubarray(arr, n)
 
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
class GFG{
     
// Function to find subarray
public static void FindSubarray(int[] arr,
                                int n)
{
     
    // If the array has only one element,
    // then there is no answer.
    if (n == 1)
    {
        Console.WriteLine("No such subarray!");
    }
 
    // Array to store the last occurrences
    // of the elements of the array.
    int[] vis = new int[n + 1];
    for(int i = 0; i < n + 1; i++)
        vis[i] = -1;
    vis[arr[0]] = 0;
 
    // To maintain the length
    int len = int.MaxValue, flag = 0;
 
    // Variables to store
    // start and end indices
    int start = 0, end = 0;
 
    for(int i = 1; i < n; i++)
    {
        int t = arr[i];
 
        // Check if element is occurring
        // for the second time in the array
        if (vis[t] != -1)
        {
             
            // Find distance between last
            // and current index
            // of the element.
            int distance = i - vis[t] + 1;
 
            // If the current distance
            // is less then len
            // update len and
            // set 'start' and 'end'
            if (distance < len)
            {
                len = distance;
                start = vis[t];
                end = i;
            }
            flag = 1;
        }
 
        // Set the last occurrence
        // of current element to be 'i'.
        vis[t] = i;
    }
     
    // If flag is equal to 0,
    // it means there is no answer.
    if (flag == 0)
        Console.WriteLine("No such subarray!");
         
    else
    {
        for(int i = start; i <= end; i++)
            Console.Write(arr[i] + " ");
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 2, 3, 2, 4, 5 };
    int n = arr.Length;
 
    FindSubarray(arr, n);
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
2 3 2

时间复杂度: O(N) ,其中 n 是数组的长度。

辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程