📌  相关文章
📜  最大子集,其总和小于等于各个索引的总和

📅  最后修改于: 2021-04-21 23:51:57             🧑  作者: Mango

给定数组arr [] ,任务是查找元素总和小于或等于其索引之和(基于1的索引)的最大子集的长度。

例子:

天真的方法:
解决该问题的最简单方法是生成所有可能的子集,并计算子元素的总和小于或等于其相应索引之和的子集的长度。

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

高效方法:
请按照以下步骤解决问题:

  • 遍历所有索引,并仅考虑其值大于或等于存储在其中的各个值的值的那些索引。
  • 继续更新在上述步骤中获得的差异的总和
  • 对于其余元素,将它们的差异与各自的索引一起存储。排序差异。
  • 由一个包括元素到子集的一个和减去总和的差。继续包含,直到遇到与其索引的差超过剩余总和的元素,或者已经包含所有数组元素。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the length
// of the longest subset
int findSubset(int* a, int n)
{
    // Stores the sum of differences
    // between elements and
    // their respective index
    int sum = 0;
 
    // Stores the size of
    // the subset
    int cnt = 0;
 
    vector v;
 
    // Iterate over the array
    for (int i = 1; i <= n; i++) {
 
        // If an element which is
        // smaller than or equal
        // to its index is encountered
        if (a[i - 1] - i <= 0) {
 
            // Increase count and sum
            sum += a[i - 1] - i;
            cnt += 1;
        }
 
        // Store the difference with
        // index of the remaining
        // elements
        else {
            v.push_back(a[i - 1] - i);
        }
    }
 
    // Sort the differences
    // in increasing order
    sort(v.begin(), v.end());
 
    int ptr = 0;
 
    // Include the differences while
    // sum remains positive or
    while (ptr < v.size()
           && sum + v[ptr] <= 0) {
        cnt += 1;
        ptr += 1;
        sum += v[ptr];
    }
 
    // Return the size
    return cnt;
}
 
// Driver Code
int main()
{
    int arr[] = { 4, 1, 6, 7,
                  8, 2 };
 
    int n = sizeof(arr)
            / sizeof(arr[0]);
 
    // Function Calling
    cout << findSubset(arr, n)
         << endl;
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
     
// Function to find the length
// of the longest subset
public static int findSubset(int[] a, int n)
{
     
    // Stores the sum of differences
    // between elements and
    // their respective index
    int sum = 0;
 
    // Stores the size of
    // the subset
    int cnt = 0;
 
    Vector v = new Vector<>();
 
    // Iterate over the array
    for(int i = 1; i <= n; i++)
    {
         
        // If an element which is
        // smaller than or equal
        // to its index is encountered
        if (a[i - 1] - i <= 0)
        {
             
            // Increase count and sum
            sum += a[i - 1] - i;
            cnt += 1;
        }
 
        // Store the difference with
        // index of the remaining
        // elements
        else
        {
            v.add(a[i - 1] - i);
        }
    }
 
    // Sort the differences
    // in increasing order
    Collections.sort(v);
 
    int ptr = 0;
 
    // Include the differences while
    // sum remains positive or
    while (ptr < v.size() &&
           sum + v.get(ptr) <= 0)
    {
        cnt += 1;
        ptr += 1;
        sum += v.get(ptr);
    }
 
    // Return the size
    return cnt;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 4, 1, 6, 7, 8, 2 };
    int n = arr.length;
 
    // Function Calling
    System.out.println(findSubset(arr, n));
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to implement 
# the above approach
 
# Function to find the length 
# of the longest subset
def findSubset(a, n):
     
    # Stores the sum of differences 
    # between elements and 
    # their respective index 
    sum = 0
   
    # Stores the size of 
    # the subset 
    cnt = 0
   
    v = [] 
   
    # Iterate over the array 
    for i in range(1, n + 1):
         
        # If an element which is 
        # smaller than or equal 
        # to its index is encountered 
        if (a[i - 1] - i <= 0):
             
            # Increase count and sum 
            sum += a[i - 1] - i 
            cnt += 1
     
        # Store the difference with 
        # index of the remaining 
        # elements 
        else:
            v.append(a[i - 1] - i)
             
    # Sort the differences 
    # in increasing order 
    v.sort()
   
    ptr = 0
   
    # Include the differences while 
    # sum remains positive or 
    while (ptr < len(v) and
           sum + v[ptr] <= 0):
        cnt += 1
        ptr += 1 
        sum += v[ptr] 
     
    # Return the size 
    return cnt
 
# Driver code
if __name__=="__main__":
     
    arr = [ 4, 1, 6, 7, 8, 2 ] 
    n = len(arr)
   
    # Function calling 
    print(findSubset(arr, n))
 
# This code is contributed by rutvik_56


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to find the length
// of the longest subset
public static int findSubset(int[] a, int n)
{
     
    // Stores the sum of differences
    // between elements and
    // their respective index
    int sum = 0;
 
    // Stores the size of
    // the subset
    int cnt = 0;
 
    List v = new List();
 
    // Iterate over the array
    for(int i = 1; i <= n; i++)
    {
         
        // If an element which is
        // smaller than or equal
        // to its index is encountered
        if (a[i - 1] - i <= 0)
        {
             
            // Increase count and sum
            sum += a[i - 1] - i;
            cnt += 1;
        }
 
        // Store the difference with
        // index of the remaining
        // elements
        else
        {
            v.Add(a[i - 1] - i);
        }
    }
 
    // Sort the differences
    // in increasing order
    v.Sort();
 
    int ptr = 0;
 
    // Include the differences while
    // sum remains positive or
    while (ptr < v.Count &&
           sum + v[ptr] <= 0)
    {
        cnt += 1;
        ptr += 1;
        sum += v[ptr];
    }
 
    // Return the size
    return cnt;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 4, 1, 6, 7, 8, 2 };
    int n = arr.Length;
 
    // Function calling
    Console.WriteLine(findSubset(arr, n));
}
}
 
// This code is contributed by amal kumar choubey


输出:
3

时间复杂度: O(N)
空间复杂度: O(N)