Find all pairs raised to power K differs by exactly N | Java program for the above approach ; Function to print pairs whose difference raised to the power K is X ; Stores the count of valid pairs ; Iterate over the range [ - 1000 , 1000 ] ; Iterate over the range [ - 1000 , 1000 ] ; If the current pair satisfies the given condition ; Increment the count by 1 ; If no such pair exists ; Driver Code
import java . util . * ; class GFG { static void ValidPairs ( int X , int K ) { int count = 0 ; for ( int A = - 1000 ; A <= 1000 ; A ++ ) { for ( int B = - 1000 ; B <= 1000 ; B ++ ) { if ( Math . pow ( A , K ) - Math . pow ( B , K ) == X ) { count ++ ; System . out . println ( A + " ▁ " + B ) ; } } } if ( count == 0 ) { System . out . println ( " - 1" ) ; } } public static void main ( String args [ ] ) { int X = 33 ; int K = 5 ; ValidPairs ( X , K ) ; } }