text: Check if any subarray can be made palindromic by replacing less than half of its elements | C ++ program for the above approach ; A Utility Function to check if a subarray can be palindromic by replacing less than half of the elements present in it ; Stores frequency of array elements ; Traverse the array ; Update frequency of each array element ; Iterator over the Map ; If frequency of any element exceeds 1 ; If no repetition is found ; Function to check and print if any subarray can be made palindromic by replacing less than half of its elements ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isConsistingSubarrayUtil ( int arr [ ] , int n ) { map < int , int > mp ; for ( int i = 0 ; i < n ; ++ i ) { mp [ arr [ i ] ] ++ ; } map < int , int > :: iterator it ; for ( it = mp . begin ( ) ; it != mp . end ( ) ; ++ it ) { if ( it -> second > 1 ) { return true ; } } return false ; } void isConsistingSubarray ( int arr [ ] , int N ) { if ( isConsistingSubarrayUtil ( arr , N ) ) { cout << " Yes " << endl ; } else { cout << " No " << endl ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; isConsistingSubarray ( arr , N ) ; return 0 ; }