Minimum Coin Flips Amazon OA 2023

Minimum Coin Flips Amazon OA 2023

Given the initial sequence of a coins, find the minimum number of coins that can be flipped to obtain a beautiful sequence. All head facing coins or tails facing coins sequence is also valid.

Input : THHHTH
Output : 2
Explanation : flip first and last coin to obtain the sequence.

Time Complexity : 0(N)

Space Complexity : 0(N)

Minimum Coin Flips Amazon OA
Minimum Coin Flips Amazon OA


Program : Minimum Coin Flips Amazon OA Solution in Python

Lets see how can we solve this question “Minimum Coin Flips Amazon OA”.

  1. find the counts of “T” to the left of each element, store in a list
  2. find the counts of “H” to the right of each element, store in a list
  3. Ideally, we want all “H” to the left and all “T” to the right i.e “HHHTT”. So identifying all the “H” to the right and all the “T” to the left for any index tells us the number of swaps needed across that index.
    e.g S = “TTTTHHH” , say at index 3, which is “T”, there are 3 “T” to the left and 3 “H” to the right, so the total swaps is 3+ 3= 6. Repeat for each index and return the minimum
  4. Find the min of the sum of both lists for each element ,that is the answer
def func(string):
    Tleft = [0]* len(string)
    Hright = [0]*len(string)
    result = len(string)
    # iterate i from i = 1 since Tleft[0] is always 0
    for i in range(1, len(string)):
      if string[i-1] == "T":
          Tleft[i] = Tleft[i-1] + 1
          Tleft[i] = Tleft[i-1]
    #iterate from 2nd to last element since Hright[lastindex] is always 0
    for i in range(len(string)-2,-1,-1):
      if string[i+1] == "H":
          Hright[i] = Hright[i+1]+ 1
      else :
          Hright[i] = Hright[i+1]
    for i in range(len(string)):
         result = min(result, Tleft[i]+ Hright[i])
    #Using if condition to make sure if the given string is already in correct format like "HHTTT" all Head's on right and Tail's on left.
    if result!=0:
        return result
    return 1
#Taking input

Now as we have achieved the solution for Minimum Coin Flips Amazon OA Solution, there are few more questions similar to this logic which gives output in terms of 0’s and 1’s use the same logic approach to solve those questions.

Amazon OA 2023 Questions with Solution

  1. Shopping Patterns Solution Amazon OA 2023
  2. Reorder Data in Log Files Solution Amazon OA 2023
  3. Top K Frequent Words Solution Amazon OA 2023
  4. Trees Height Solution Amazon OA SDE 2023
  5. Counting Binary Substrings Amazon OA 2023
  6. Grid Connections Amazon OA 2023
  7. Shipment Imbalance Amazon OA 2023
  8. Max Profit Amazon OA 2023
  9. Find Lowest Price Amazon OA 2023
  10. Decode String Frequency Amazon OA 2023
  11. Simple Cipher Amazon OA 2023
  12. Valid Discount Coupons Amazon OA 2023 Solution
  13. Count Maximum Teams Amazon OA 2023
  14. Max Average Stock Price Amazon OA 2023 Solution
  15. Robot Bounded In Circle Amazon OA 2023
  16. Shopping Options Amazon OA 2023 Solution
  17. Fill The Truck Maximum Units on a Truck Amazon OA Solution
  18. Maximize Score After N Operations Number Game Solution Amazon OA 2023
  19. Slowest Key Amazon OA 2023 Solution
  20. Five Star Seller Maximum Average Pass Ratio Amazon OA 2023
  21. Split String Into Unique Primes Amazon OA 2023 Solution
  22. Storage Optimization Amazon OA 2023 Solution
  23. Minimum Difficulty of a Job Schedule Amazon OA 2023 Solution
  24. Autoscale Policy Utilization Check Amazon OA 2023
  25. Optimal Utilization Solution Amazon OA 2023
  26. Merge Two Sorted Lists Solution Amazon OA 2023
  27. Two Sum Unique Pairs Solution Amazon OA 2023
  28. Amazon Music Pairs Amazon OA 2023 Solution
  29. Class Grouping Amazon OA 2023 Solution
  30. Find Max products Amazon OA 2023 Solution
  31. Get encrypted number Amazon OA 2023 Solution
  32. Find Total Imbalance Amazon OA 2023 Solution
  33. Find Total Power Amazon OA 2023 Solution

Leave a Comment

18 + seventeen =