human
Minimize insertions to make sum of every pair of consecutive array elements at most K | Function to count minimum number of insertions required to make sum of every pair of adjacent elements at most K.
Stores if it is possible to make sum of each pair of adjacent elements at most K.
Stores the count of insertions.
Stores the previous value to index i.
Traverse the array.
If arr [ i ] is greater than or equal to K.
Mark possible 0.
If last + arr [ i ] is greater than K.
Increment res by 1.
Assign arr [ i ] to last.
If possible to make the sum of pairs of adjacent elements at most K.
Otherwise print " - 1".
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to minimize insertions to make sum of every pair of consecutive array elements at most K and the other steps required.
 def minimumInsertions ( arr , N , K ) :
	possible = 1
	res = 0
	last = 0
	for i in range ( N ) :
		if ( arr [ i ] >= K ) :
			possible = 0
			break
		if ( last + arr [ i ] > K ) :
			res += 1
		last = arr [ i ]
	if ( possible ) :
		print ( res )
	else :
		print ( " - 1" )
if __name__ == " _ _ main _ _ " :
	arr = [ 1 , 2 , 3 , 4 , 5 ]
	K = 6
	N = len ( arr )
	minimumInsertions ( arr , N , K )