Check whether a number can be represented by sum of two squares | An efficient approach based implementation to find if a number can be written as sum of two squares . ; function to check if there exist two numbers sum of whose squares is n . ; store square value in hashmap ; Driver Code
import java . util . * ; class GFG { static boolean sumSquare ( int n ) { HashMap < Integer , Integer > s = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i * i <= n ; ++ i ) { s . put ( i * i , 1 ) ; if ( s . containsKey ( n - i * i ) ) { System . out . println ( ( int ) Math . sqrt ( n - i * i ) + " ^ 2 ▁ + ▁ " + i + " ^ 2" ) ; return true ; } } return false ; } public static void main ( String [ ] args ) { int n = 169 ; System . out . print ( sumSquare ( n ) ? "YES " ▁ : ▁ " NO "); } }