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++ program for the above approach #include <bits/stdc++.h> 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 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<N && arr[i] + arr[i + 1 ] > 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 |
# 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# 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<N && arr[i] + arr[i + 1] > 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. |
<script> // JavaScript 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
const maximizeSubset = (arr, N) => {
// Sort arr[] in increasing order
arr.sort((a, b) => a - b)
// Stores the maximum size of a valid
// subset of the given array
let maxSize = 0;
// Stores the starting index of the
// current window
let i = 0;
// Stores the last index of the
// current window
let 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 = Math.max(maxSize, j - i);
i++;
j = Math.max(j, i + 2);
}
// Return Answer
return maxSize;
}
// Driver Code
let arr = [2, 7, 4, 1, 6, 9, 5, 3];
let N = arr.length;
document.write(maximizeSubset(arr, N));
// This code is contributed by rakeshsahni
</script> |
4
Time Complexity: O(N*log N)
Auxiliary Space: O(1)