Maximum size of subset of given array such that a triangle can be formed by any three integers as the sides of the triangle | 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 ; Sort arr [ ] in increasing order ; Stores the maximum size of a valid subset of the given array ; Stores the starting index of the current window ; Stores the last index of the current window ; Iterate over the array arr [ ] ; Increment j till the value of arr [ i ] + arr [ i + 1 ] > arr [ j ] holds true ; Update the value of maxSize ; Return Answer ; Driver Code
def maximizeSubset ( arr , N ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE maxSize = 0 NEW_LINE i = 0 NEW_LINE j = i + 2 NEW_LINE while ( i < N - 2 ) : NEW_LINE INDENT while ( j < N and arr [ i ] + arr [ i + 1 ] > arr [ j ] ) : NEW_LINE INDENT j = j + 1 NEW_LINE DEDENT maxSize = max ( maxSize , j - i ) NEW_LINE i += 1 NEW_LINE j = max ( j , i + 2 ) NEW_LINE DEDENT return maxSize NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 7 , 4 , 1 , 6 , 9 , 5 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE print ( maximizeSubset ( arr , N ) ) NEW_LINE DEDENT