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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void niceIndices ( int A [ ] , int n ) { int sum = 0 ; map < int , int > m ; for ( int i = 0 ; i < n ; ++ i ) { m [ A [ i ] ] ++ ; sum += A [ i ] ; } for ( int i = 0 ; i < n ; ++ i ) { int k = sum - A [ i ] ; if ( k % 2 == 0 ) { k = k >> 1 ; if ( m . find ( k ) != m . end ( ) ) { if ( ( A [ i ] == k && m [ k ] > 1 ) || ( A [ i ] != k ) ) cout << ( i + 1 ) << " ▁ " ; } } } } int main ( ) { int A [ ] = { 8 , 3 , 5 , 2 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; niceIndices ( A , n ) ; return 0 ; }