rajeshinternshala

Untitled

Oct 14th, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.85 KB | None | 0 0
  1. # Python3 Program to implement
  2. # the above approach
  3.  
  4. # Function to find maximum
  5. # distance between every
  6. # two element
  7. def max_distance(a, temp, n):
  8.  
  9.     # Stores index of last
  10.     # occurrence of each
  11.     # array element
  12.     mp = {}
  13.  
  14.     # Initialize temp[]
  15.     # with -1
  16.     for i in range(1, n + 1):
  17.         temp[i] = -1
  18.  
  19.     # Traverse the array
  20.     for i in range(n):
  21.  
  22.         # If array element has
  23.         # not occurred previously
  24.         if (a[i] not in mp):
  25.  
  26.             # Update index in temp
  27.             temp[a[i]] = i + 1
  28.  
  29.         # Otherwise
  30.         else:
  31.  
  32.             # Compare temp[a[i]] with
  33.             # distance from its previous
  34.             # occurrence and store the maximum
  35.             temp[a[i]] = max(temp[a[i]],
  36.                             i - mp[a[i]])
  37.  
  38.         mp[a[i]] = i
  39.  
  40.     for i in range(1, n + 1):
  41.  
  42.         # Compare temp[i] with
  43.         # distance of its last
  44.         # occurrence from the end
  45.         # of the array and store
  46.         # the maximum
  47.         if (temp[i] != -1):
  48.             temp[i] = max(temp[i],
  49.                         n - mp[i])
  50.  
  51. # Function to find the minimum
  52. # common element in subarrays
  53. # of all possible lengths
  54. def min_comm_ele(a, ans,
  55.                 temp, n):
  56.  
  57.     # Function call to find
  58.     # a the maximum distance
  59.     # between every pair of
  60.     # repetition
  61.     max_distance(a, temp, n)
  62.  
  63.     # Initialize ans[] to -1
  64.     for i in range(1, n + 1):
  65.         ans[i] = -1
  66.  
  67.     for i in range(1, n + 1):
  68.  
  69.         # Check if subarray of length
  70.         # temp[i] contains i as one
  71.         # of the common elements
  72.         if (ans[temp[i]] == -1):
  73.             ans[temp[i]] = i
  74.  
  75.     for i in range(1, n + 1):
  76.  
  77.         # Find the minimum of all
  78.         # common elements
  79.         if (i > 1 and
  80.             ans[i - 1] != -1):
  81.  
  82.             if (ans[i] == -1):
  83.                 ans[i] = ans[i - 1]
  84.             else:
  85.                 ans[i] = min(ans[i],
  86.                             ans[i - 1])
  87.  
  88.         print(ans[i], end = " ")
  89.  
  90. # Driver Code
  91. if __name__ == "__main__":
  92.  
  93.     N = 6
  94.     a = [1, 3, 4, 5, 6, 7]
  95.     temp = [0] * 100
  96.     ans = [0] * 100
  97.     min_comm_ele(a, ans,
  98.                 temp, N)
  99.  
  100. # This code is contributed by Chitranayal
  101.  
Add Comment
Please, Sign In to add comment