human
What is a code to subset with sum divisible by m and the other steps required given the following:
Subset with sum divisible by m | Returns true if there is a subset of arr [ ] with sum divisible by m.
This array will keep track of all the possible sum ( after modulo m ) which can be made using subsets of arr [ ] initialising boolean array with all false.
we 'll loop through all the elements of arr[].
anytime we encounter a sum divisible by m , we are done.
To store all the new encountered sum ( after modulo ) . It is used to make sure that arr [ i ] is added only to those entries for which DP [ j ] was true before current iteration ..
For each element of arr [ ] , we loop through all elements of DP table from 1 to m and we add current element i . e . , arr [ i ] to all those elements which are true in DP table.
if an element is true in DP table.
We update it in temp and update to DP once loop of j is over.
Updating all the elements of temp to DP table since iteration over j is over.
Also since arr [ i ] is a single element subset , arr [ i ] % m is one of the possible sum.
Driver code"
Solution in Python:
assistant
#Here is code to subset with sum divisible by m and the other steps required.
 def modularSum ( arr , n , m ) :
	if ( n > m ) :
		return True
	DP = [ False for i in range ( m ) ]
	for i in range ( n ) :
		if ( DP [ 0 ] ) :
			return True
		temp = [ False for i in range ( m ) ]
		for j in range ( m ) :
			if ( DP [ j ] == True ) :
				if ( DP [ ( j + arr [ i ] ) % m ] == False ) :
					temp [ ( j + arr [ i ] ) % m ] = True
		for j in range ( m ) :
			if ( temp [ j ] ) :
				DP [ j ] = True
		DP [ arr [ i ] % m ] = True
	return DP [ 0 ]
arr = [ 1 , 7 ]
n = len ( arr )
m = 5
print ( " YES " ) if ( modularSum ( arr , n , m ) ) else print ( " NO " )