We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
use (short)(Math.pow(2,15)-1) instead of (short) Math.pow(2,15) -1 because:
The value of Math.pow(2,15) = 32768.0 ,when you type cast it to short its value becomes -32768 and when you subtract it from 1 the value is -32769 .So,first subtract 1 from the Math.pow(2,15) then type cast to short.The same logic for the int and long.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Datatypes
You are viewing a single comment's thread. Return to all comments →
use (short)(Math.pow(2,15)-1) instead of (short) Math.pow(2,15) -1 because:
The value of Math.pow(2,15) = 32768.0 ,when you type cast it to short its value becomes -32768 and when you subtract it from 1 the value is -32769 .So,first subtract 1 from the Math.pow(2,15) then type cast to short.The same logic for the int and long.