Find range of values for S in given Array with values satisfying [ arr [ i ] = floor ( ( i * S ) / K ) ] | C ++ program for the above approach ; Function to find the range of values for S in a given array that satisfies the given condition ; Stores the left range value ; Stores the right range value ; Find the current left range value for S ; Find the current right range value for S ; Updating L value ; Updating R value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findRange ( int arr [ ] , int N , int K ) { int L = INT_MIN ; int R = INT_MAX ; for ( int i = 0 ; i < N ; i ++ ) { int l = ( int ) ceil ( 1.0 * arr [ i ] * K / ( i + 1 ) ) ; int r = ( int ) ceil ( ( 1.0 + arr [ i ] ) * K / ( i + 1 ) ) - 1 ; L = max ( L , l ) ; R = min ( R , r ) ; } cout << L << " ▁ " << R ; } int main ( ) { int arr [ ] = { 2 , 4 , 6 , 9 , 11 } ; int K = 10 ; int N = sizeof ( arr ) / sizeof ( int ) ; findRange ( arr , N , K ) ; return 0 ; }