📌  相关文章
📜  最大化数组中连续元素子集的大小

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

最大化数组中连续元素子集的大小

给定一个整数数组和一个整数 k。数组元素表示一维数轴上的点的位置,找到可以通过在数轴上放置另一个 k 点来形成的点的连续值的点子集的最大大小。请注意,所有坐标都应该是不同的,并且数组的元素是按递增顺序排列的。

例子:

Input : arr[] = {1, 2, 3, 4, 10, 11, 14, 15},
            k = 4 
Output : 8
For maximum size subset, it is optimal to
choose the points on number line at 
coordinates 12, 13, 16 and 17, so that the
size of the consecutive valued subset will
become 8 which will be maximum .

Input : arr[] = {7, 8, 12, 13, 15, 18}
        k = 5
Output : 10
For maximum size subset, it is optimal to choose
the points on number line at coordinates 9, 10, 
11, 14 and 16, so that the size of the consecutive 
valued subset will become 10 which will be maximum .

蛮力包括检查条件 ((arr[r]-arr[l])-(rl)) ≤ k 的所有可能的 (l, r) 对。为了确定一对 (l, r) 是否有效,我们应该检查需要放置在这两个初始点之间的点的数量是否不大于 K。因为 arr[i] 是输入数组 (arr) 中的第 i 个点,那么我们需要检查 (arr[r] – arr[l]) – (r – l ) ≤ k。

该解决方案的复杂度为 O(N 2 )。

C++
/* C++ program to find the maximum size of subset of
   points that can have consecutive values using
   brute force */
#include 
using namespace std;
 
int maximiseSubset(int arr[], int n, int k)
{
    // Since we can always enforce the solution
    // to contain all the K added points
    int ans = k;
 
    for (int l = 0; l < n - 1; l++)
        for (int r = l; r < n; r++)
 
            // check if the number of points that
            // need to be placed between these two
            // initial ones is not greater than k
            if ((arr[r] - arr[l]) - (r - l) <= k)
                ans = max(ans, r - l + k + 1);
 
    return (ans);
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 10, 11, 14, 15 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 4;
    printf("%dn", maximiseSubset(arr, n, k));
    return 0;
}


Java
/* Java program to find the maximum size of subset of
points that can have consecutive values using
brute force */
import java.util.*;
 
class GFG
{
 
    static int maximiseSubset(int[] arr, int n, int k)
    {
        // Since we can always enforce the solution
        // to contain all the K added points
        int ans = k;
 
        for (int l = 0; l < n - 1; l++)
            for (int r = l; r < n; r++)
 
                // check if the number of points that
                // need to be placed between these two
                // initial ones is not greater than k
                if ((arr[r] - arr[l]) - (r - l) <= k)
                    ans = Math.max(ans, r - l + k + 1);
 
        return (ans);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = { 1, 2, 3, 4, 10, 11, 14, 15 };
        int n = arr.length;
        int k = 4;
        System.out.println(maximiseSubset(arr, n, k)); 
    }
}
/* This code is contributed by Mr. Somesh Awasthi */


Python3
# Python3 program to find the maximum size
# of subset of points that can have consecutive
# values using brute force
 
def maximiseSubset(arr , n, k):
 
    # Since we can always enforce the solution
    # to contain all the K added points
    ans = k
 
    for l in range(n - 1):
        for r in range(l, n):
 
            # check if the number of points that
            # need to be placed between these two
            # initial ones is not greater than k
            if ((arr[r] - arr[l]) - (r - l) <= k) :
                ans = max(ans, r - l + k + 1)
 
    return (ans)
 
# Driver code
if __name__ == "__main__":
     
    arr = [ 1, 2, 3, 4, 10, 11, 14, 15 ]
    n = len(arr)
    k = 4
    print(maximiseSubset(arr, n, k))
 
# This code is contributed by ita_c


C#
/* C# program to find the
maximum size of subset of
points that can have
consecutive values using
brute force */
using System;
 
class GFG
{
 
    static int maximiseSubset(int[] arr,
                              int n, int k)
    {
        // Since we can always enforce
        // the solution to contain all
        // the K added points
        int ans = k;
 
        for (int l = 0; l < n - 1; l++)
            for (int r = l; r < n; r++)
 
                // check if the number of
                // points that need to be
                // placed between these
                // two initial ones is not
                // greater than k
                if ((arr[r] - arr[l]) -
                              (r - l) <= k)
                    ans = Math.Max(ans, r - l +
                                        k + 1);
 
        return (ans);
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr = {1, 2, 3, 4,
                    10, 11, 14, 15};
        int n = arr.Length;
        int k = 4;
        Console.WriteLine(maximiseSubset(arr, n, k));
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


C++
/* C++ program to find the maximum size of subset
   of points that can have consecutive values
   using efficient approach */
#include 
using namespace std;
 
int maximiseSubset(int arr[], int n, int k)
{
    // Since we can always enforce the
    // solution to contain all the K added
    // points
    int ans = k;
 
    int l = 0, r = 0;
    while (r < n) {
 
        // increment l until the number of points
        // that need to be placed between index l
        // and index r is not greater than k
        while ((arr[r] - arr[l]) - (r - l) > k)
            l++;
         
        // update the solution as below
        ans = max(ans, r - l + k + 1);
         
        r++;
    }
 
    return (ans);
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 10, 11, 14, 15 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 4;
    printf("%d", maximiseSubset(arr, n, k));
    return 0;
}


Java
/* Java program to find the maximum size of subset
of points that can have consecutive values
using efficient approach */
import java.util.*;
 
class GFG
{
    static int maximiseSubset(int[] arr, int n, int k)
    {
        // Since we can always enforce the
        // solution to contain all the K added
        // points
        int ans = k;
 
        int l = 0, r = 0;
        while (r < n) {
 
            // increment l until the number of points
            // that need to be placed between index l
            // and index r is not greater than k
            while ((arr[r] - arr[l]) - (r - l) > k)
                l++;
         
            // update the solution as below
            ans = Math.max(ans, r - l + k + 1);
         
            r++;
        }
 
        return (ans);
    }
 
    // Driver code 
    public static void main(String[] args)
    {
        int[] arr = { 1, 2, 3, 4, 10, 11, 14, 15 };
        int n = arr.length;
        int k = 4;
        System.out.println(maximiseSubset(arr, n, k));
    }
}
/* This code is contributed by Mr. Somesh Awasthi */


Python3
# Python 3 program to find the maximum size
# of subset of points that can have consecutive
# values using efficient approach
def maximiseSubset(arr, n, k):
 
    # Since we can always enforce the solution
    # to contain all the K added points
    ans = k;
 
    l = 0; r = 0;
    while (r < n):
 
        # increment l until the number of points
        # that need to be placed between index l
        # and index r is not greater than k
        while ((arr[r] - arr[l]) - (r - l) > k):
            l = l + 1;
         
        # update the solution as below
        ans = max(ans, r - l + k + 1);
         
        r = r + 1;
         
    return (ans);
 
# Driver code
arr = [ 1, 2, 3, 4, 10, 11, 14, 15 ];
n = len(arr);
k = 4;
print(maximiseSubset(arr, n, k));
 
# This code is contributed
# by Akanksha Rai


C#
/* C# program to find the
maximum size of subset
of points that can have
consecutive values using
efficient approach */
using System;
 
class GFG
{
    static int maximiseSubset(int[] arr,
                              int n, int k)
    {
        // Since we can always enforce
        // the solution to contain all
        // the K added points
        int ans = k;
 
        int l = 0, r = 0;
        while (r < n)
        {
 
            // increment l until the
            // number of points that
            // need to be placed
            // between index l and
            // index r is not greater
            // than k
            while ((arr[r] - arr[l]) -
                        (r - l) > k)
                l++;
         
            // update the
            // solution as below
            ans = Math.Max(ans, r - l +
                                k + 1);
         
            r++;
        }
 
        return (ans);
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr = {1, 2, 3, 4,
                    10, 11, 14, 15};
        int n = arr.Length;
        int k = 4;
        Console.WriteLine(maximiseSubset(arr, n, k));
    }
}
 
// This code is contributed
// by anuj_67.


PHP
 $k)
            $l++;
     
        // update the solution as below
        $ans = max($ans, $r - $l + $k + 1);
     
        $r++;
    }
 
    return ($ans);
}
 
// Driver code
$arr = array(1, 2, 3, 4, 10, 11, 14, 15 );
$n = sizeof($arr);
$k = 4;
echo(maximiseSubset($arr, $n, $k));
 
// This code is contributed by Mukul Singh
?>


Javascript


输出:

8

高效方法(时间复杂度 - O(N))

为了优化蛮力,请注意如果 r 增加,则 l 也会增加(或至少保持不变)。我们可以维护两个索引。将 l 和 r 都初始化为 0。然后我们开始增加 r。当我们这样做时,在每一步我们都会增加 l,直到在蛮力方法中使用的条件变为真。当 r 到达最后一个索引时,我们停止。

C++

/* C++ program to find the maximum size of subset
   of points that can have consecutive values
   using efficient approach */
#include 
using namespace std;
 
int maximiseSubset(int arr[], int n, int k)
{
    // Since we can always enforce the
    // solution to contain all the K added
    // points
    int ans = k;
 
    int l = 0, r = 0;
    while (r < n) {
 
        // increment l until the number of points
        // that need to be placed between index l
        // and index r is not greater than k
        while ((arr[r] - arr[l]) - (r - l) > k)
            l++;
         
        // update the solution as below
        ans = max(ans, r - l + k + 1);
         
        r++;
    }
 
    return (ans);
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 10, 11, 14, 15 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 4;
    printf("%d", maximiseSubset(arr, n, k));
    return 0;
}

Java

/* Java program to find the maximum size of subset
of points that can have consecutive values
using efficient approach */
import java.util.*;
 
class GFG
{
    static int maximiseSubset(int[] arr, int n, int k)
    {
        // Since we can always enforce the
        // solution to contain all the K added
        // points
        int ans = k;
 
        int l = 0, r = 0;
        while (r < n) {
 
            // increment l until the number of points
            // that need to be placed between index l
            // and index r is not greater than k
            while ((arr[r] - arr[l]) - (r - l) > k)
                l++;
         
            // update the solution as below
            ans = Math.max(ans, r - l + k + 1);
         
            r++;
        }
 
        return (ans);
    }
 
    // Driver code 
    public static void main(String[] args)
    {
        int[] arr = { 1, 2, 3, 4, 10, 11, 14, 15 };
        int n = arr.length;
        int k = 4;
        System.out.println(maximiseSubset(arr, n, k));
    }
}
/* This code is contributed by Mr. Somesh Awasthi */

Python3

# Python 3 program to find the maximum size
# of subset of points that can have consecutive
# values using efficient approach
def maximiseSubset(arr, n, k):
 
    # Since we can always enforce the solution
    # to contain all the K added points
    ans = k;
 
    l = 0; r = 0;
    while (r < n):
 
        # increment l until the number of points
        # that need to be placed between index l
        # and index r is not greater than k
        while ((arr[r] - arr[l]) - (r - l) > k):
            l = l + 1;
         
        # update the solution as below
        ans = max(ans, r - l + k + 1);
         
        r = r + 1;
         
    return (ans);
 
# Driver code
arr = [ 1, 2, 3, 4, 10, 11, 14, 15 ];
n = len(arr);
k = 4;
print(maximiseSubset(arr, n, k));
 
# This code is contributed
# by Akanksha Rai

C#

/* C# program to find the
maximum size of subset
of points that can have
consecutive values using
efficient approach */
using System;
 
class GFG
{
    static int maximiseSubset(int[] arr,
                              int n, int k)
    {
        // Since we can always enforce
        // the solution to contain all
        // the K added points
        int ans = k;
 
        int l = 0, r = 0;
        while (r < n)
        {
 
            // increment l until the
            // number of points that
            // need to be placed
            // between index l and
            // index r is not greater
            // than k
            while ((arr[r] - arr[l]) -
                        (r - l) > k)
                l++;
         
            // update the
            // solution as below
            ans = Math.Max(ans, r - l +
                                k + 1);
         
            r++;
        }
 
        return (ans);
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr = {1, 2, 3, 4,
                    10, 11, 14, 15};
        int n = arr.Length;
        int k = 4;
        Console.WriteLine(maximiseSubset(arr, n, k));
    }
}
 
// This code is contributed
// by anuj_67.

PHP

 $k)
            $l++;
     
        // update the solution as below
        $ans = max($ans, $r - $l + $k + 1);
     
        $r++;
    }
 
    return ($ans);
}
 
// Driver code
$arr = array(1, 2, 3, 4, 10, 11, 14, 15 );
$n = sizeof($arr);
$k = 4;
echo(maximiseSubset($arr, $n, $k));
 
// This code is contributed by Mukul Singh
?>

Javascript


输出:

8

时间复杂度: O(N)