📌  相关文章
📜  每个子序列中出现最小的元素

📅  最后修改于: 2021-04-24 21:04:07             🧑  作者: Mango

给定一个由N个不同整数组成的数组。对于每种元素,任务是从最小元素为当前元素的所有可能子序列中找到子序列的计数。
例子:

天真的方法:想法是生成给定数组的所有可能子序列,并对每个子序列中的最小元素进行计数,并为数组中的每个元素打印其计数。
时间复杂度: O(2 N )
辅助空间: O(N)

高效方法:这个想法是观察一个模式,即观察到最小元素出现2 n – 1次,第二个最小元素出现2 n – 2次,依此类推…… 。例如:

步骤如下:

  1. 将每个元素的索引存储在Map中,以便我们可以按照原始数组的顺序打印该元素。
  2. 对给定的数组进行排序。
  3. 现在元素按递增顺序排列,从上述观察中可以遍历给定的数组,并保留子序列数,以使每个元素都是最小的元素由pow(2,N – 1 – i)给出
  4. 现在根据原始数组中的元素遍历子序列的映射和打印计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function that count the subsequence
// such that each element as the
// minimum element in the subsequence
void solve(int arr[], int N)
{
    map M;
 
    // Store index in a map
    for (int i = 0; i < N; i++) {
        M[i] = arr[i];
    }
 
    // Sort the array
    sort(arr, arr + N);
 
    // To store count of subsequence
    unordered_map Count;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Store count of subsequence
        Count[arr[i]] = pow(2, N - i - 1);
    }
 
    // Print the count of subsequence
    for (auto& it : M) {
        cout << Count[M[it.second]] << ' ';
    }
}
 
// Driver code
int main()
{
    int arr[] = { 5, 2, 1 };
    int N = sizeof arr / sizeof arr[0];
 
    // Function call
    solve(arr, N);
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function that count the subsequence
// such that each element as the
// minimum element in the subsequence
static void solve(int arr[], int N)
{
    HashMap M = new HashMap<>();
 
    // Store index in a map
    for (int i = 0; i < N; i++)
    {
        M.put(i, arr[i]);
    }
 
    // Sort the array
    Arrays.sort(arr);
 
    // To store count of subsequence
    HashMap Count = new HashMap<>();
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
        // Store count of subsequence
        Count.put(arr[i],
                 (int)Math.pow(2, N - i - 1));
    }
 
    // Print the count of subsequence
    for (Map.Entry m : M.entrySet())
    {
        System.out.print(Count.get(m.getValue()) + " ");
    }
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 5, 2, 1 };
    int N = arr.length;
 
    // Function call
    solve(arr, N);
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function that count the subsequence
# such that each element as the
# minimum element in the subsequence
def solve(arr, N):
 
    M = {}
 
    # Store index in a map
    for i in range(N):
        M[i] = arr[i]
     
    # Sort the array
    arr.sort()
 
    # To store count of subsequence
    Count = {}
 
    # Traverse the array
    for i in range(N):
 
        # Store count of subsequence
        Count[arr[i]] = pow(2, N - i - 1)
 
    # Print the count of subsequence
    for it in Count.values():
        print(it, end = " ")
 
# Driver code
if __name__ == "__main__":
 
    arr = [ 5, 2, 1 ]
    N = len(arr)
 
    # Function call
    solve(arr, N)
 
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function that count the subsequence
// such that each element as the
// minimum element in the subsequence
static void solve(int []arr, int N)
{
    Dictionary M = new Dictionary();
 
    // Store index in a map
    for (int i = 0; i < N; i++)
    {
        M.Add(i, arr[i]);
    }
 
    // Sort the array
    Array.Sort(arr);
 
    // To store count of subsequence
    Dictionary Count = new Dictionary();
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
        // Store count of subsequence
        Count.Add(arr[i],
                 (int)Math.Pow(2, N - i - 1));
    }
  // Print the count of subsequence
   foreach(KeyValuePair m in M)
{
    Console.Write(Count[m.Value]);  
} 
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 5, 2, 1 };
    int N = arr.Length;
 
    // Function call
    solve(arr, N);
}
}
 
// This code is contributed by PrinciRaj1992


输出:
4 2 1





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