Count possible removals to make absolute difference between the sum of odd and even indexed elements equal to K | Function to check if difference between the sum of odd and even indexed elements after removing the first element is K or not ; Stores the sum of elements at odd and even indices ; Return 1 if difference is K ; Function to check if difference between the sum of odd and even indexed elements after removing the second element is K or not ; Stores the sum of elements at odd and even indices ; Return 1 if difference is K ; Function to count number of elements to be removed to make sum of differences between odd and even indexed elements equal to K ; Size of given array ; Base Conditions ; Stores prefix and suffix sums ; Base assignments ; Store prefix sums of even indexed elements ; Store prefix sums of odd indexed elements ; Similarly , store suffix sums of elements at even and odd indices ; Stores the count of possible removals ; Traverse and remove the ith element ; If the current element is excluded , then previous index ( i - 1 ) points to ( i + 2 ) and ( i - 2 ) points to ( i + 1 ) ; Find count when 0 th element is removed ; Find count when 1 st element is removed ; Count gives the required answer ; Driver Code ; Function call
def findCount0th ( arr , N , K ) : NEW_LINE INDENT oddsum = 0 NEW_LINE evensum = 0 NEW_LINE for i in range ( 1 , N , 2 ) : NEW_LINE INDENT oddsum += arr [ i ] NEW_LINE DEDENT for i in range ( 2 , N , 2 ) : NEW_LINE INDENT evensum += arr [ i ] NEW_LINE DEDENT if ( abs ( oddsum - evensum ) == K ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def findCount1st ( arr , N , K ) : NEW_LINE INDENT evensum = arr [ 0 ] NEW_LINE oddsum = 0 NEW_LINE for i in range ( 3 , N , 2 ) : NEW_LINE INDENT evensum += arr [ i ] NEW_LINE DEDENT for i in range ( 2 , N , 2 ) : NEW_LINE INDENT oddsum += arr [ i ] NEW_LINE DEDENT if ( abs ( oddsum - evensum ) == K ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def countTimes ( arr , K ) : NEW_LINE INDENT N = len ( arr ) NEW_LINE if ( N == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( N < 3 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( N == 3 ) : NEW_LINE INDENT cnt = 0 NEW_LINE if abs ( arr [ 0 ] - arr [ 1 ] ) == K : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT if abs ( arr [ 2 ] - arr [ 1 ] ) == K : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT if abs ( arr [ 0 ] - arr [ 2 ] ) == K : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT return cnt NEW_LINE DEDENT prefix = [ 0 ] * ( N + 2 ) NEW_LINE suffix = [ 0 ] * ( N + 2 ) NEW_LINE prefix [ 0 ] = arr [ 0 ] NEW_LINE prefix [ 1 ] = arr [ 1 ] NEW_LINE suffix [ N - 1 ] = arr [ N - 1 ] NEW_LINE suffix [ N - 2 ] = arr [ N - 2 ] NEW_LINE for i in range ( 2 , N , 2 ) : NEW_LINE INDENT prefix [ i ] = arr [ i ] + prefix [ i - 2 ] NEW_LINE DEDENT for i in range ( 3 , N , 2 ) : NEW_LINE INDENT prefix [ i ] = arr [ i ] + prefix [ i - 2 ] NEW_LINE DEDENT for i in range ( N - 3 , - 1 , - 2 ) : NEW_LINE INDENT suffix [ i ] = arr [ i ] + suffix [ i + 2 ] NEW_LINE DEDENT for i in range ( N - 4 , - 1 , - 2 ) : NEW_LINE INDENT suffix [ i ] = arr [ i ] + suffix [ i + 2 ] NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT if ( abs ( prefix [ i - 1 ] + suffix [ i + 2 ] - prefix [ i - 2 ] - suffix [ i + 1 ] ) == K ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT count += findCount0th ( arr , N , K ) NEW_LINE count += findCount1st ( arr , N , K ) NEW_LINE return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 5 , 6 ] NEW_LINE K = 2 NEW_LINE print ( countTimes ( arr , K ) ) NEW_LINE DEDENT