Maximum length of same indexed subarrays from two given arrays satisfying the given condition | C # program for the above approach ; Stores the segment tree node values ; Function to find maximum length of subarray such that sum of maximum element in subarray in brr [ ] and sum of subarray in arr [ ] * K is at most C ; Base Case ; Let maximum length be 0 ; Perform Binary search ; Find mid value ; Check if the current mid satisfy the given condition ; If yes , then store length ; Otherwise ; Return maximum length stored ; Function to check if it is possible to have such a subarray of length K ; Check for first window of size K ; Calculate the total cost and check if less than equal to c ; If it satisfy the condition ; Find the sum of current subarray and calculate total cost ; Include the new element of current subarray ; Discard the element of last subarray ; Calculate total cost and check <= c ; If possible , then return true ; If it is not possible ; Function that builds segment Tree ; If there is only one element ; Find the value of mid ; Build left and right parts of segment tree recursively ; Update the value at current index ; Function to find maximum element in the given range ; If the query is out of bounds ; If the segment is completely inside the query range ; Calculate the mid ; Return maximum in left & right of the segment tree recursively ; Function that initializes the segment Tree ; Driver Code ; Initialize and Build the Segment Tree ; Function Call
using System ; class GFG { static int [ ] seg ; static int maxLength ( int [ ] a , int [ ] b , int n , int c ) { if ( n == 0 ) return 0 ; int max_length = 0 ; int low = 0 , high = n ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( possible ( a , b , n , c , mid ) ) { max_length = mid ; low = mid + 1 ; } else high = mid - 1 ; } return max_length ; } static bool possible ( int [ ] a , int [ ] b , int n , int c , int k ) { int sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) { sum += a [ i ] ; } int total_cost = sum * k + getMax ( b , 0 , n - 1 , 0 , k - 1 , 0 ) ; if ( total_cost <= c ) return true ; for ( int i = k ; i < n ; i ++ ) { sum += a [ i ] ; sum -= a [ i - k ] ; total_cost = sum * k + getMax ( b , 0 , n - 1 , i - k + 1 , i , 0 ) ; if ( total_cost <= c ) return true ; } return false ; } static void build ( int [ ] b , int index , int s , int e ) { if ( s == e ) { seg [ index ] = b [ s ] ; return ; } int mid = s + ( e - s ) / 2 ; build ( b , 2 * index + 1 , s , mid ) ; build ( b , 2 * index + 2 , mid + 1 , e ) ; seg [ index ] = Math . Max ( seg [ 2 * index + 1 ] , seg [ 2 * index + 2 ] ) ; } public static int getMax ( int [ ] b , int ss , int se , int qs , int qe , int index ) { if ( se < qs ss > qe ) return Int32 . MinValue / 2 ; if ( ss >= qs && se <= qe ) return seg [ index ] ; int mid = ss + ( se - ss ) / 2 ; return Math . Max ( getMax ( b , ss , mid , qs , qe , 2 * index + 1 ) , getMax ( b , mid + 1 , se , qs , qe , 2 * index + 2 ) ) ; } static void initialiseSegmentTree ( int N ) { seg = new int [ 4 * N ] ; } static void Main ( ) { int [ ] A = { 1 , 2 , 1 , 6 , 5 , 5 , 6 , 1 } ; int [ ] B = { 14 , 8 , 15 , 15 , 9 , 10 , 7 , 12 } ; int C = 40 ; int N = A . Length ; initialiseSegmentTree ( N ) ; build ( B , 0 , 0 , N - 1 ) ; Console . WriteLine ( maxLength ( A , B , N , C ) ) ; } }