给定数组子集的最大大小,使得三角形可以由任意三个整数作为三角形的边组成
给定一个由N个整数组成的数组arr[] ,任务是找到数组的最大子集的大小,使得可以从子集的三个整数中的任何一个作为三角形的边形成三角形。
例子:
Input: arr[] = {1, 4, 7, 4}
Output: 3
Explanation: A possible subsets that follow the given conditions are {1, 4, 4} and {4, 4, 7}. The size of both of these subsets is 3 which is the maximum possible.
Input: arr[] = {2, 7, 4, 1, 6, 9, 5, 3}
Output: 4
方法:给定的问题可以通过使用滑动窗口技术的贪婪方法来解决。众所周知,对于具有边长A 、 B和C的三角形, A + B > C必须成立,其中A和B是具有较小长度的边。基于上述观察,可以使用以下步骤解决给定问题:
- 以非递减顺序对给定数组 arr[] 进行排序。
- 维护两个变量i和j ,其中i跟踪当前窗口的起点, j跟踪当前窗口的终点。最初i = 0和j = i + 2 。
- 增加j的值直到arr[i] + arr[i+1] > arr[j]并在变量maxSize中跟踪j – i的最大值。如果arr[i] + arr[i+1] > arr[j] ,将i的值增加1 。
- 按照上述步骤,直到遍历整个数组。
- 完成以上步骤后, maxSize中存储的值就是需要的结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum size of
// the subset of the given array such
// that a triangle can be formed from any
// three integers of the subset as sides
int maximizeSubset(int arr[], int N)
{
// Sort arr[] in increasing order
sort(arr, arr + N);
// Stores the maximum size of a valid
// subset of the given array
int maxSize = 0;
// Stores the starting index of the
// current window
int i = 0;
// Stores the last index of the
// current window
int j = i + 2;
// Iterate over the array arr[]
while (i < N - 2) {
// Increment j till the value
// of arr[i] + arr[i + 1] >
// arr[j] holds true
while (arr[i] + arr[i + 1] > arr[j]) {
j++;
}
// Update the value of maxSize
maxSize = max(maxSize, j - i);
i++;
j = max(j, i + 2);
}
// Return Answer
return maxSize;
}
// Driver Code
int main()
{
int arr[] = { 2, 7, 4, 1, 6, 9, 5, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << maximizeSubset(arr, N) << endl;
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the maximum size of
// the subset of the given array such
// that a triangle can be formed from any
// three integers of the subset as sides
static int maximizeSubset(int arr[], int N)
{
// Sort arr[] in increasing order
Arrays.sort(arr);
// Stores the maximum size of a valid
// subset of the given array
int maxSize = 0;
// Stores the starting index of the
// current window
int i = 0;
// Stores the last index of the
// current window
int j = i + 2;
// Iterate over the array arr[]
while (i < N - 2) {
// Increment j till the value
// of arr[i] + arr[i + 1] >
// arr[j] holds true
while (j arr[j]) {
j++;
}
// Update the value of maxSize
maxSize = Math.max(maxSize, j - i);
i++;
j = Math.max(j, i + 2);
}
// Return Answer
return maxSize;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 2, 7, 4, 1, 6, 9, 5, 3 };
int N = arr.length;
System.out.print(maximizeSubset(arr, N) +"\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# python program for the above approach
# Function to find the maximum size of
# the subset of the given array such
# that a triangle can be formed from any
# three integers of the subset as sides
def maximizeSubset(arr, N):
# Sort arr[] in increasing order
arr.sort()
# Stores the maximum size of a valid
# subset of the given array
maxSize = 0
# Stores the starting index of the
# current window
i = 0
# Stores the last index of the
# current window
j = i + 2
# Iterate over the array arr[]
while (i < N - 2):
# Increment j till the value
# of arr[i] + arr[i + 1] >
# arr[j] holds true
while (j < N and arr[i] + arr[i + 1] > arr[j]):
j = j + 1
# Update the value of maxSize
maxSize = max(maxSize, j - i)
i += 1
j = max(j, i + 2)
# Return Answer
return maxSize
# Driver Code
if __name__ == "__main__":
arr = [2, 7, 4, 1, 6, 9, 5, 3]
N = len(arr)
print(maximizeSubset(arr, N))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the maximum size of
// the subset of the given array such
// that a triangle can be formed from any
// three integers of the subset as sides
static int maximizeSubset(int []arr, int N)
{
// Sort arr[] in increasing order
Array.Sort(arr);
// Stores the maximum size of a valid
// subset of the given array
int maxSize = 0;
// Stores the starting index of the
// current window
int i = 0;
// Stores the last index of the
// current window
int j = i + 2;
// Iterate over the array arr[]
while (i < N - 2) {
// Increment j till the value
// of arr[i] + arr[i + 1] >
// arr[j] holds true
if(j>=N || i+1 >=N)
break;
while (j arr[j]) {
j++;
}
// Update the value of maxSize
maxSize = Math.Max(maxSize, j - i);
i++;
j = Math.Max(j, i + 2);
}
// Return Answer
return maxSize;
}
// Driver Code
public static void Main()
{
int []arr = { 2, 7, 4, 1, 6, 9, 5, 3 };
int N = arr.Length;
Console.Write(maximizeSubset(arr, N));
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出:
4
时间复杂度: O(N*log N)
辅助空间: O(1)