Length of longest strictly increasing subset with each pair of adjacent elements satisfying the condition 2 * A [ i ] â ‰¥ A [ i + 1 ] | Function to find the length of the longest subset satisfying given conditions ; Sort the array in ascending order ; Stores the starting index and maximum length of the required subset ; Pointer to traverse the array ; Iterate while i < n ; Stores end point of current subset ; Store the length of the current subset ; Continue adding elements to the current subset till the condition satisfies ; Increment length of the current subset ; Increment the pointer j ; If length of the current subset exceeds overall maximum length ; Update maxlen ; Update index ; Increment j ; Update i ; Store the starting index of the required subset in i ; Print the required subset ; Print the array element ; Decrement maxlen ; Increment i ; Given array ; Store the size of the array
function maxLenSubset ( a , n ) { a . sort ( function ( a , b ) { return a - b ; } ) let index = 0 , maxlen = - 1 ; let i = 0 ; while ( i < n ) { let j = i ; let len = 1 ; while ( j < n - 1 ) { if ( 2 * a [ j ] >= a [ j + 1 ] ) { len ++ ; } else break ; j ++ ; } if ( maxlen < len ) { maxlen = len ; index = i ; } j ++ ; i = j ; } i = index ; while ( maxlen > 0 ) { document . write ( a [ i ] + " " ) maxlen -- ; i ++ ; } } let a = [ 3 , 1 , 5 , 11 ] ; let n = a . length maxLenSubset ( a , n )