First N natural can be divided into two sets with given difference and co | PHP code to determine whether numbers 1 to N can be divided into two sets such that absolute difference between sum of these two sets is M and these two sum are co - prime ; function that returns boolean value on the basis of whether it is possible to divide 1 to N numbers into two sets that satisfy given conditions . ; initializing total sum of 1 to n numbers ; since ( 1 ) total_sum = sum_s1 + sum_s2 and ( 2 ) m = sum_s1 - sum_s2 assuming sum_s1 > sum_s2 . solving these 2 equations to get sum_s1 and sum_s2 ; total_sum = sum_s1 + sum_s2 and therefore ; if total sum is less than the absolute difference then there is no way we can split n numbers into two sets so return false ; check if these two sums are integers and they add up to total sum and also if their absolute difference is m . ; Now if two sum are co - prime then return true , else return false . ; if two sums don 't add up to total  sum or if their absolute difference  is not m, then there is no way to  split n numbers, hence return false ; Driver code ; function call to determine answer
< ? php function __gcd ( $ a , $ b ) { return $ b == 0 ? $ a : __gcd ( $ b , $ a % $ b ) ; } function isSplittable ( $ n , $ m ) { $ total_sum = ( int ) ( ( $ n * ( $ n + 1 ) ) / 2 ) ; $ sum_s1 = ( int ) ( ( $ total_sum + $ m ) / 2 ) ; $ sum_s2 = $ total_sum - $ sum_s1 ; if ( $ total_sum < $ m ) return false ; if ( $ sum_s1 + $ sum_s2 == $ total_sum && $ sum_s1 - $ sum_s2 == $ m ) return ( __gcd ( $ sum_s1 , $ sum_s2 ) == 1 ) ; return false ; } $ n = 5 ; $ m = 7 ; if ( isSplittable ( $ n , $ m ) ) echo " Yes " ; else echo " No " ;