Find the index in a circular array from which prefix sum is always non | Function to find the starting index of the given circular array prefix sum array is non negative ; Stores the sum of the array ; Stores the starting index ; Stores the minimum prefix sum of A [ 0. . i ] ; Traverse the array ; Update the value of sum ; If sum is less than minimum ; Update the min as the value of prefix sum ; Update starting index ; Otherwise no such index is possible ; Driver code
import sys NEW_LINE def startingPoint ( A , N ) : NEW_LINE INDENT sum = 0 NEW_LINE startingindex = 0 NEW_LINE min = sys . maxsize NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT sum += A [ i ] NEW_LINE if ( sum < min ) : NEW_LINE INDENT min = sum NEW_LINE startingindex = i + 1 NEW_LINE DEDENT DEDENT if ( sum < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return startingindex % N NEW_LINE DEDENT arr = [ 3 , - 6 , 7 , - 4 , - 4 , 6 , - 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( startingPoint ( arr , N ) ) NEW_LINE