Count subarrays having a single distinct element that can be obtained from a given array | python 3 program for the above approach ; Function to count subarrays of single distinct element into which given array can be split ; Stores the count ; Stores frequency of array elements ; Traverse the array ; Traverse the map ; Increase count of subarrays by ( frequency - 1 ) ; Driver Code
from collections import defaultdict NEW_LINE def divisionalArrays ( arr , N ) : NEW_LINE INDENT sum = N NEW_LINE mp = defaultdict ( int ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT for x in mp : NEW_LINE INDENT if ( mp [ x ] > 1 ) : NEW_LINE INDENT sum += mp [ x ] - 1 NEW_LINE DEDENT DEDENT print ( sum ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE divisionalArrays ( arr , N ) NEW_LINE DEDENT