human
Largest subarray having sum greater than k | Comparison function used to sort preSum vector ..
Function to find index in preSum vector upto which all prefix sum values are less than or equal to val ..
Starting and ending index of search space ..
To store required index value ..
If middle value is less than or equal to val then index can lie in mid + 1. . n else it lies in 0. . mid - 1..
Function to find largest subarray having sum greater than or equal to k ..
Length of largest subarray ..
Vector to store pair of prefix sum and corresponding ending index value ..
To store the current value of prefix sum ..
To store minimum index value in range 0. . i of preSum vector ..
Insert values in preSum vector ..
Update minInd array ..
If sum is greater than k , then answer is i + 1..
If sum is less than or equal to k , then find if there is a prefix array having sum that needs to be added to current sum to make its value greater than k . If yes , then compare length of updated subarray with maximum length found so far ..
Driver code ."
How can the above be solved in Python?
 ... What is a code to largest subarray having sum greater than k and the other steps required?
assistant
#Here is code to largest subarray having sum greater than k and the other steps required.
 def compare ( a , b ) :
	if a [ 0 ] == b [ 0 ] :
		return a [ 1 ] < b [ 1 ]
	return a [ 0 ] < b [ 0 ]
def findInd ( preSum , n , val ) :
	l , h = 0 , n - 1
	ans = - 1
	while l <= h :
		mid = ( l + h ) // 2
		if preSum [ mid ] [ 0 ] <= val :
			ans = mid
			l = mid + 1
		else :
			h = mid - 1
	return ans
def largestSub ( arr , n , k ) :
	maxlen = 0
	preSum = [ ]
	Sum = 0
	minInd = [ None ] * ( n )
	for i in range ( 0 , n ) :
		Sum = Sum + arr [ i ]
		preSum . append ( [ Sum , i ] )
	preSum . sort ( )
	minInd [ 0 ] = preSum [ 0 ] [ 1 ]
	for i in range ( 1 , n ) :
		minInd [ i ] = min ( minInd [ i - 1 ] , preSum [ i ] [ 1 ] )
	Sum = 0
	for i in range ( 0 , n ) :
		Sum = Sum + arr [ i ]
		if Sum > k :
			maxlen = i + 1
		else :
			ind = findInd ( preSum , n , Sum - k - 1 )
			if ind != - 1 and minInd [ ind ] < i :
				maxlen = max ( maxlen , i - minInd [ ind ] )
	return maxlen
if __name__ == " _ _ main _ _ " :
	arr = [ - 2 , 1 , 6 , - 3 ]
	n = len ( arr )
	k = 5
	print ( largestSub ( arr , n , k ) )