human
Can you provide a code to count of longest possible subarrays with sum not divisible by K and the other steps required given the following:
Count of longest possible subarrays with sum not divisible by K | Function to find the count of longest subarrays with sum not divisible by K.
Sum of all elements in an array.
If overall sum is not divisible then return 1 , as only one subarray of size n is possible.
Index of the first number not divisible by K.
Index of the last number not divisible by K.
Subarray doesn 't exist.
Sum of the window.
Calculate the sum of rest of the windows of size len.
Driver Code"
Solution in Python:
assistant
#Here is code to count of longest possible subarrays with sum not divisible by K and the other steps required.
 def CountLongestSubarrays ( arr , n , k ) :
	s = 0
	for i in range ( n ) :
		s += arr [ i ]
	if ( s % k ) :
		return 1
	else :
		ini = 0
		while ( ini < n and arr [ ini ] % k == 0 ) :
			ini += 1
		final = n - 1
		while ( final >= 0 and arr [ final ] % k == 0 ) :
			final -= 1
		sum , count = 0 , 0
		if ( ini == n ) :
			return - 1
		else :
			length = max ( n - 1 - ini , final )
		for i in range ( length ) :
			sum += arr [ i ]
		if ( sum % k != 0 ) :
			count += 1
		for i in range ( length , n ) :
			sum = sum + arr [ i ]
			sum = sum + arr [ i - length ]
			if ( sum % k != 0 ) :
				count += 1
		return count
if __name__ == ' _ _ main _ _ ' :
	arr = [ 3 , 2 , 2 , 2 , 3 ]
	n = len ( arr )
	k = 3
	print ( CountLongestSubarrays ( arr , n , k ) )