Advertisement
skb50bd

Maximum_Subarray_Problem [Brute_Force (Iterative)]

Sep 28th, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. '''
  4. Author: Shakib Haris
  5. Problem: Maximum Subarray Problem
  6. Input: The Price(s), not the Change(s)
  7. Output: Maximum Subarray
  8. '''
  9.  
  10. def bruteForce (A):
  11.     maxDif = A[1] - A[0]
  12.     lenA = len(A)
  13.     for buy in range (lenA - 1):
  14.         for sell in range (buy + 1, lenA):
  15.             diff = A[sell] - A[buy]
  16.             if diff > maxDif:
  17.                 start, end, maxdif = buy, sell, diff
  18.     return start, end
  19.  
  20. def display (A):
  21.     for item in A:
  22.         print ("%d " %(item), end = "")
  23.     print ("")
  24.  
  25.  
  26. A = [int(x) for x in input().split()]
  27. print("Original Array: ", end = "")
  28. display(A)
  29.  
  30. start, end = bruteForce(A)
  31.  
  32. print("Sub-array: ", end = "")
  33. display(A[start:end + 1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement