Longest Span in two Binary Arrays Python
PROGRAM TO FIND THE LONGEST SPAN WITH SAME SUM IN TWO BINARY ARRAYS
OUTPUT:
Length of the longest common span with same sum is 6
def longestCommonSum(arr1, arr2, n): # Initialize result maxLen = 0 # One by one pick all possible starting points # of subarrays for i in range(0,n): # Initialize sums of current subarrays sum1 = 0 sum2 = 0 # Conider all points for starting with arr[i] for j in range(i,n): # Update sums sum1 += arr1[j] sum2 += arr2[j] # If sums are same and current length is # more than maxLen, update maxLen if (sum1 == sum2): len = j-i+1 if (len > maxLen): maxLen = len return maxLen# Driver program to test above functionarr1 = [0, 1, 0, 1, 1, 1, 1]arr2 = [1, 1, 1, 1, 1, 0, 1]n = len(arr1)print("Length of the longest common span with same " "sum is",longestCommonSum(arr1, arr2, n))
Length of the longest common span with same sum is 6
Comments
Post a Comment