Maximum size of subset of given array such that a triangle can be formed by any three integers as the sides of the triangle
Given an array arr[] consisting of N integers, the task is to find the size of the largest subset of the array such that a triangle can be formed from any of the three integers of the subset as the sides of a triangle.
Examples:
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
Approach: The given problem can be solved with the help of the Greedy Approach using the Sliding Window Technique. It is known that for a triangle having side lengths A, B, and C, A + B > C must hold true where A and B are the sides with smaller lengths. Based on the above observation the given problem can be solved using the following steps:
- Sort the given array arr[] in non-decreasing order.
- Maintain two variables i and j where i keep track of the starting point of the current window and j keep track of the ending point of the current window. Initially i = 0 and j = i + 2.
- Increment the value of j until arr[i] + arr[i+1] > arr[j] and keep track of the maximum value of j – i in a variable maxSize. If arr[i] + arr[i+1] > arr[j], increment the value of i by 1.
- Follow the above step till the whole array has been traversed.
- After completing the above steps, the value stored in maxSize is the required result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximizeSubset( int arr[], int N)
{
sort(arr, arr + N);
int maxSize = 0;
int i = 0;
int j = i + 2;
while (i < N - 2) {
while (arr[i] + arr[i + 1] > arr[j]) {
j++;
}
maxSize = max(maxSize, j - i);
i++;
j = max(j, i + 2);
}
return maxSize;
}
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
import java.util.*;
class GFG{
static int maximizeSubset( int arr[], int N)
{
Arrays.sort(arr);
int maxSize = 0 ;
int i = 0 ;
int j = i + 2 ;
while (i < N - 2 ) {
while (j<N && arr[i] + arr[i + 1 ] > arr[j]) {
j++;
}
maxSize = Math.max(maxSize, j - i);
i++;
j = Math.max(j, i + 2 );
}
return maxSize;
}
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" );
}
}
|
Python3
def maximizeSubset(arr, N):
arr.sort()
maxSize = 0
i = 0
j = i + 2
while (i < N - 2 ):
while (j < N and arr[i] + arr[i + 1 ] > arr[j]):
j = j + 1
maxSize = max (maxSize, j - i)
i + = 1
j = max (j, i + 2 )
return maxSize
if __name__ = = "__main__" :
arr = [ 2 , 7 , 4 , 1 , 6 , 9 , 5 , 3 ]
N = len (arr)
print (maximizeSubset(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int maximizeSubset( int []arr, int N)
{
Array.Sort(arr);
int maxSize = 0;
int i = 0;
int j = i + 2;
while (i < N - 2) {
if (j>=N || i+1 >=N)
break ;
while (j<N && arr[i] + arr[i + 1] > arr[j]) {
j++;
}
maxSize = Math.Max(maxSize, j - i);
i++;
j = Math.Max(j, i + 2);
}
return maxSize;
}
public static void Main()
{
int []arr = { 2, 7, 4, 1, 6, 9, 5, 3 };
int N = arr.Length;
Console.Write(maximizeSubset(arr, N));
}
}
|
Javascript
<script>
const maximizeSubset = (arr, N) => {
arr.sort((a, b) => a - b)
let maxSize = 0;
let i = 0;
let j = i + 2;
while (i < N - 2) {
while (arr[i] + arr[i + 1] > arr[j]) {
j++;
}
maxSize = Math.max(maxSize, j - i);
i++;
j = Math.max(j, i + 2);
}
return maxSize;
}
let arr = [2, 7, 4, 1, 6, 9, 5, 3];
let N = arr.length;
document.write(maximizeSubset(arr, N));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Last Updated :
13 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...