Largest possible Subset from an Array such that no element is K times any other element in the Subset | Function to find the maximum size of the required subset ; Size of the array ; Sort the array ; Stores which index is included or excluded ; Stores the indices of array elements ; Count of pairs ; Iterate through all the element ; If element is included ; Check if a [ i ] * k is present in the array or not ; Increase count of pair ; Exclude the pair ; Driver code
function findMaxLen ( a , k ) { let n = a . length ; a . sort ( ) ; let vis = Array . from ( { length : n } , ( _ , i ) => 0 ) ; let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { mp . set ( a [ i ] , i ) ; } let c = 0 ; for ( let i = 0 ; i < n ; ++ i ) { if ( vis [ i ] == false ) { let check = a [ i ] * k ; if ( mp . has ( check ) ) { c ++ ; vis [ mp . get ( check ) ] = true ; } } } return n - c ; } let K = 3 ; let arr = [ 1 , 4 , 3 , 2 ] ; document . write ( findMaxLen ( arr , K ) ) ;