text: Minimum steps for increasing and decreasing Array to reach either 0 or N | C ++ program for the above approach ; Function that finds the minimum steps to reach either 0 or N for given increasing and decreasing arrays ; Initialize variable to find the minimum element ; Find minimum element in increasing [ ] array ; Initialize variable to find the maximum element ; Find maximum element in decreasing [ ] array ; Find the minimum steps ; Print the minimum steps ; Driver code ; Given N ; Given increasing and decreasing array ; Find length of arrays increasing and decreasing ; Function call
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; void minSteps ( int N , int increasing [ ] , int decreasing [ ] , int m1 , int m2 ) { int mini = INT_MAX ; for ( int i = 0 ; i < m1 ; i ++ ) { if ( mini > increasing [ i ] ) mini = increasing [ i ] ; } int maxi = INT_MIN ; for ( int i = 0 ; i < m2 ; i ++ ) { if ( maxi < decreasing [ i ] ) maxi = decreasing [ i ] ; } int minSteps = max ( maxi , N - mini ) ; cout << minSteps << endl ; } int main ( ) { int N = 7 ; int increasing [ ] = { 3 , 5 } ; int decreasing [ ] = { 6 } ; int m1 = sizeof ( increasing ) / sizeof ( increasing [ 0 ] ) ; int m2 = sizeof ( decreasing ) / sizeof ( decreasing [ 0 ] ) ; minSteps ( N , increasing , decreasing , m1 , m2 ) ; }