Find all good indices in the given Array | Python3 program to find all good indices in the given array ; Function to find all good indices in the given array ; hash to store frequency of each element ; Storing frequency of each element and calculating sum simultaneously ; check if array is good after removing i - th index element ; print good indices ; Driver Code
from collections import defaultdict NEW_LINE def niceIndices ( A , n ) : NEW_LINE INDENT Sum = 0 NEW_LINE m = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ A [ i ] ] += 1 NEW_LINE Sum += A [ i ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT k = Sum - A [ i ] NEW_LINE if k % 2 == 0 : NEW_LINE INDENT k = k >> 1 NEW_LINE if k in m : NEW_LINE INDENT if ( ( A [ i ] == k and m [ k ] > 1 ) or ( A [ i ] != k ) ) : NEW_LINE INDENT print ( ( i + 1 ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 8 , 3 , 5 , 2 ] NEW_LINE n = len ( A ) NEW_LINE niceIndices ( A , n ) NEW_LINE DEDENT