Check if final remainder is present in original Array by reducing it based on given conditions | C ++ program to implement above approach ; copying original array ; loop till length of array become 2. ; find middle element ; pop element from front and rear ; find remainder ; append remainder to a ; now since length of array is 2 take product and divide it by n ; if remainder is present is original array return 1 , else return 0 ; calling function Reduced ; if x = 1 print YES else NO
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Reduced ( vector < int > a , int n ) { vector < int > original_array ; original_array = a ; while ( a . size ( ) != 2 ) { int mid = a . size ( ) / 2 ; int mid_ele = a [ mid ] ; int start = a [ 0 ] ; a . erase ( a . begin ( ) ) ; int end = a [ a . size ( ) - 1 ] ; a . pop_back ( ) ; int rmd = ( start * end ) % mid_ele ; a . push_back ( rmd ) ; int remainder = ( a [ 0 ] * a [ 1 ] ) % n ; for ( int i = 0 ; i < original_array . size ( ) ; i ++ ) { if ( original_array [ i ] == remainder ) { return 1 ; } } } return 0 ; } int main ( ) { vector < int > Arr = { 2 , 3 , 4 , 8 , 5 , 7 } ; int N = Arr . size ( ) ; int x = Reduced ( Arr , N ) ; if ( x ) cout << ( " YES " ) ; else cout << ( " NO " ) ; return 0 ; }