Maximum possible sum of squares of stack elements satisfying the given properties | Function to find the maximum sum of squares of stack elements ; Stores the sum ofsquares of stack elements ; Check if sum is valid ; Initialize all stack elements with 1 ; Stores the count the number of stack elements not equal to 1 ; Add the sum of squares of stack elements not equal to 1 ; Add 1 * 1 to res as the remaining stack elements are 1 ; Prvar the result ; Driver Code ; Function call
function maxSumOfSquares ( N , S ) { var res = 0 ; if ( S < N S > 9 * N ) { document . write ( - 1 ) ; return ; } S = S - N ; var c = 0 ; while ( S > 0 ) { c ++ ; if ( parseInt ( S / 8 ) > 0 ) { res += 9 * 9 ; S -= 8 ; } else { res += ( S + 1 ) * ( S + 1 ) ; break ; } } res = res + ( N - c ) ; document . write ( res ) ; } var N = 3 ; var S = 12 ; maxSumOfSquares ( N , S ) ;