给定由N个唯一元素组成的数组arr [] ,任务是生成长度为N的数组B [] ,使得B [i]是子序列数 其中arr [i]是最大元素。
例子:
Input: arr[] = {2, 3, 1}
Output: {2, 4, 1}
Explanation: Subsequences in which arr[0] ( = 2) is maximum are {2}, {2, 1}.
Subsequences in which arr[1] ( = 3) is maximum are {3}, {1, 3, 2}, {2, 3}, {1, 3}.
Subsequence in which arr[2] ( = 1) is maximum is {1}.
Input: arr[] = {23, 34, 12, 7, 15, 31}
Output: {8, 32, 2, 1, 4, 16}
方法:可以通过观察元素arr [i]出现的所有子序列来解决该问题,因为最大值将包含小于arr [i]的所有元素。因此,不同子序列的总数将为2 (元素数小于arr [i]) 。请按照以下步骤解决问题:
- 相对于给定数组中存在的对应值对数组arr []索引进行排序,并将该索引存储在数组index []中,其中arr [indices [i]]
。 - 用1初始化一个整数subseq以存储可能的子序列数。
- 使用变量i在指针[0,N-1]范围内迭代N次。
- B [indices [i]]是其中arr [indices [i]]最大的子序列数,即2 i ,因为将有i个元素小于arr [indices [i]]。
- 将B [indices [i]]的答案存储为B [indices [i]] = subseq 。
- 通过将subseq乘以2来更新subseq 。
- 打印数组B []的元素作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to merge the subarrays
// arr[l .. m] and arr[m + 1, .. r]
// based on indices[]
void merge(int* indices, int* a, int l,
int mid, int r)
{
int temp_ind[r - l + 1], j = mid + 1;
int i = 0, temp_l = l, k;
while (l <= mid && j <= r) {
// If a[indices[l]] is less than
// a[indices[j]], add indice[l] to temp
if (a[indices[l]] < a[indices[j]])
temp_ind[i++] = indices[l++];
// Else add indices[j]
else
temp_ind[i++] = indices[j++];
}
// Add remaining elements
while (l <= mid)
temp_ind[i++] = indices[l++];
// Add remainging elements
while (j <= r)
temp_ind[i++] = indices[j++];
for (k = 0; k < i; k++)
indices[temp_l++] = temp_ind[k];
}
// Recursive function to divide
// the array into to parts
void divide(int* indices, int* a, int l, int r)
{
if (l >= r)
return;
int mid = l / 2 + r / 2;
// Recursive call for elements before mid
divide(indices, a, l, mid);
// Recursive call for elements after mid
divide(indices, a, mid + 1, r);
// Merge the two sorted arrays
merge(indices, a, l, mid, r);
}
// Function to find the number of
// subsequences for each element
void noOfSubsequences(int arr[], int N)
{
int indices[N], i;
for (i = 0; i < N; i++)
indices[i] = i;
// Sorting the indices according
// to array arr[]
divide(indices, arr, 0, N - 1);
// Array to store output nmbers
int B[N];
// Initialize subseq
int subseq = 1;
for (i = 0; i < N; i++) {
// B[i] is 2^i
B[indices[i]] = subseq;
// Doubling the subsequences
subseq *= 2;
}
// Print the final output, array B[]
for (i = 0; i < N; i++)
cout << B[i] << " ";
}
// Driver Code
int main()
{
// Given array
int arr[] = { 2, 3, 1 };
// Given length
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
noOfSubsequences(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to merge the subarrays
// arr[l .. m] and arr[m + 1, .. r]
// based on indices[]
static void merge(int[] indices, int[] a, int l,
int mid, int r)
{
int []temp_ind = new int[r - l + 1];
int j = mid + 1;
int i = 0, temp_l = l, k;
while (l <= mid && j <= r)
{
// If a[indices[l]] is less than
// a[indices[j]], add indice[l] to temp
if (a[indices[l]] < a[indices[j]])
temp_ind[i++] = indices[l++];
// Else add indices[j]
else
temp_ind[i++] = indices[j++];
}
// Add remaining elements
while (l <= mid)
temp_ind[i++] = indices[l++];
// Add remainging elements
while (j <= r)
temp_ind[i++] = indices[j++];
for(k = 0; k < i; k++)
indices[temp_l++] = temp_ind[k];
}
// Recursive function to divide
// the array into to parts
static void divide(int[] indices, int[] a,
int l, int r)
{
if (l >= r)
return;
int mid = l / 2 + r / 2;
// Recursive call for elements before mid
divide(indices, a, l, mid);
// Recursive call for elements after mid
divide(indices, a, mid + 1, r);
// Merge the two sorted arrays
merge(indices, a, l, mid, r);
}
// Function to find the number of
// subsequences for each element
static void noOfSubsequences(int arr[], int N)
{
int []indices = new int[N];
int i;
for(i = 0; i < N; i++)
indices[i] = i;
// Sorting the indices according
// to array arr[]
divide(indices, arr, 0, N - 1);
// Array to store output nmbers
int[] B = new int[N];
// Initialize subseq
int subseq = 1;
for(i = 0; i < N; i++)
{
// B[i] is 2^i
B[indices[i]] = subseq;
// Doubling the subsequences
subseq *= 2;
}
// Print the final output, array B[]
for(i = 0; i < N; i++)
System.out.print(B[i] + " ");
}
// Driver Code
public static void main(String[] args)
{
// Given array
int arr[] = { 2, 3, 1 };
// Given length
int N = arr.length;
// Function call
noOfSubsequences(arr, N);
}
}
// This code is contributed by Princi Singh
Python3
# Python3 program for the above approach
# Function to merge the subarrays
# arr[l .. m] and arr[m + 1, .. r]
# based on indices[]
def merge(indices, a, l, mid, r):
temp_ind = [0] * (r - l + 1)
j = mid + 1
i = 0
temp_l = l
while (l <= mid and j <= r):
# If a[indices[l]] is less than
# a[indices[j]], add indice[l] to temp
if (a[indices[l]] < a[indices[j]]):
temp_ind[i] = indices[l]
i += 1
l += 1
# Else add indices[j]
else:
temp_ind[i] = indices[j]
i += 1
j += 1
# Add remaining elements
while (l <= mid):
temp_ind[i] = indices[l]
i += 1
l += 1
# Add remainging elements
while (j <= r):
temp_ind[i] = indices[j]
i += 1
j += 1
for k in range(i):
indices[temp_l] = temp_ind[k]
temp_l += 1
# Recursive function to divide
# the array into to parts
def divide(indices, a, l, r):
if (l >= r):
return
mid = l // 2 + r // 2
# Recursive call for elements
# before mid
divide(indices, a, l, mid)
# Recursive call for elements
# after mid
divide(indices, a, mid + 1, r)
# Merge the two sorted arrays
merge(indices, a, l, mid, r)
# Function to find the number of
# subsequences for each element
def noOfSubsequences(arr, N):
indices = N * [0]
for i in range(N):
indices[i] = i
# Sorting the indices according
# to array arr[]
divide(indices, arr, 0, N - 1)
# Array to store output nmbers
B = [0] * N
# Initialize subseq
subseq = 1
for i in range(N):
# B[i] is 2^i
B[indices[i]] = subseq
# Doubling the subsequences
subseq *= 2
# Print the final output, array B[]
for i in range(N):
print(B[i], end = " ")
# Driver Code
if __name__ == "__main__":
# Given array
arr = [ 2, 3, 1 ]
# Given length
N = len(arr)
# Function call
noOfSubsequences(arr, N)
# This code is contributed by chitranayal
C#
// C# program for the above approach
using System;
class GFG{
// Function to merge the subarrays
// arr[l .. m] and arr[m + 1, .. r]
// based on indices[]
static void merge(int[] indices, int[] a, int l,
int mid, int r)
{
int []temp_ind = new int[r - l + 1];
int j = mid + 1;
int i = 0, temp_l = l, k;
while (l <= mid && j <= r)
{
// If a[indices[l]] is less than
// a[indices[j]], add indice[l] to temp
if (a[indices[l]] < a[indices[j]])
temp_ind[i++] = indices[l++];
// Else add indices[j]
else
temp_ind[i++] = indices[j++];
}
// Add remaining elements
while (l <= mid)
temp_ind[i++] = indices[l++];
// Add remainging elements
while (j <= r)
temp_ind[i++] = indices[j++];
for(k = 0; k < i; k++)
indices[temp_l++] = temp_ind[k];
}
// Recursive function to divide
// the array into to parts
static void divide(int[] indices, int[] a,
int l, int r)
{
if (l >= r)
return;
int mid = l / 2 + r / 2;
// Recursive call for elements before mid
divide(indices, a, l, mid);
// Recursive call for elements after mid
divide(indices, a, mid + 1, r);
// Merge the two sorted arrays
merge(indices, a, l, mid, r);
}
// Function to find the number of
// subsequences for each element
static void noOfSubsequences(int []arr, int N)
{
int []indices = new int[N];
int i;
for(i = 0; i < N; i++)
indices[i] = i;
// Sorting the indices according
// to array []arr
divide(indices, arr, 0, N - 1);
// Array to store output nmbers
int[] B = new int[N];
// Initialize subseq
int subseq = 1;
for(i = 0; i < N; i++)
{
// B[i] is 2^i
B[indices[i]] = subseq;
// Doubling the subsequences
subseq *= 2;
}
// Print the readonly output, array []B
for(i = 0; i < N; i++)
Console.Write(B[i] + " ");
}
// Driver Code
public static void Main(String[] args)
{
// Given array
int []arr = { 2, 3, 1 };
// Given length
int N = arr.Length;
// Function call
noOfSubsequences(arr, N);
}
}
// This code is contributed by Amit Katiyar
输出:
2 4 1
时间复杂度: O(NlogN)其中N是给定数组的长度。
辅助空间: O(N)