Count ways to split an array into subarrays such that sum of the i | Python3 program for the above approach ; Function to count ways to split an array into subarrays such that sum of the i - th subarray is divisible by i ; Stores the prefix sum of array ; Find the prefix sum ; Initialize dp [ ] [ ] array ; Stores the count of splitting ; Iterate over the range [ 0 , N ] ; Update the dp table ; If the last index is reached , then add it to the variable ans ; Return the possible count of splitting of array into subarrays ; Driver Code
import numpy as np NEW_LINE def countOfWays ( arr , N ) : NEW_LINE INDENT pre = [ 0 ] * ( N + 1 ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT pre [ i + 1 ] = pre [ i ] + arr [ i ] ; NEW_LINE DEDENT dp = np . zeros ( ( N + 2 , N + 2 ) ) ; NEW_LINE dp [ 1 ] [ 0 ] += 1 ; NEW_LINE ans = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N , 0 , - 1 ) : NEW_LINE INDENT dp [ j + 1 ] [ pre [ i + 1 ] % ( j + 1 ) ] += dp [ j ] [ pre [ i + 1 ] % j ] ; NEW_LINE if ( i == N - 1 ) : NEW_LINE INDENT ans += dp [ j ] [ pre [ i + 1 ] % j ] ; NEW_LINE DEDENT DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( countOfWays ( arr , N ) ) ; NEW_LINE DEDENT