Find all good indices in the given Array | C # 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
using System ; using System . Collections . Generic ; class GFG { static void niceIndices ( int [ ] A , int n ) { int sum = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( A [ i ] ) ) { var val = mp [ A [ i ] ] ; mp . Remove ( A [ i ] ) ; mp . Add ( A [ i ] , val + 1 ) ; sum += A [ i ] ; } else { mp . Add ( A [ i ] , 0 ) ; sum += A [ i ] ; } } for ( int i = 0 ; i < n ; ++ i ) { int k = sum - A [ i ] ; if ( k % 2 == 0 ) { k = k >> 1 ; if ( mp . ContainsKey ( k ) ) { if ( ( A [ i ] == k && mp [ k ] > 1 ) || ( A [ i ] != k ) ) Console . Write ( ( i + 1 ) + " ▁ " ) ; } } } } public static void Main ( String [ ] args ) { int [ ] A = { 8 , 3 , 5 , 2 } ; int n = A . Length ; niceIndices ( A , n ) ; } }