**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)

**SOLUTION**

**Program :** **Minimum Coin Flips Amazon OA Solution** in Python

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

- find the counts of “T” to the left of each element, store in a list
- find the counts of “H” to the right of each element, store in a list
- 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 - 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
else:
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
s=input()
print(func(s))
```

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**

- Shopping Patterns Solution Amazon OA 2023
- Reorder Data in Log Files Solution Amazon OA 2023
- Top K Frequent Words Solution Amazon OA 2023
- Trees Height Solution Amazon OA SDE 2023
- Counting Binary Substrings Amazon OA 2023
- Grid Connections Amazon OA 2023
- Shipment Imbalance Amazon OA 2023
- Max Profit Amazon OA 2023
- Find Lowest Price Amazon OA 2023
- Decode String Frequency Amazon OA 2023
- Simple Cipher Amazon OA 2023
- Valid Discount Coupons Amazon OA 2023 Solution
- Count Maximum Teams Amazon OA 2023
- Max Average Stock Price Amazon OA 2023 Solution
- Robot Bounded In Circle Amazon OA 2023
- Shopping Options Amazon OA 2023 Solution
- Fill The Truck Maximum Units on a Truck Amazon OA Solution
- Maximize Score After N Operations Number Game Solution Amazon OA 2023
- Slowest Key Amazon OA 2023 Solution
- Five Star Seller Maximum Average Pass Ratio Amazon OA 2023
- Split String Into Unique Primes Amazon OA 2023 Solution
- Storage Optimization Amazon OA 2023 Solution
- Minimum Difficulty of a Job Schedule Amazon OA 2023 Solution
- Autoscale Policy Utilization Check Amazon OA 2023
- Optimal Utilization Solution Amazon OA 2023
- Merge Two Sorted Lists Solution Amazon OA 2023
- Two Sum Unique Pairs Solution Amazon OA 2023
- Amazon Music Pairs Amazon OA 2023 Solution
- Class Grouping Amazon OA 2023 Solution
- Find Max products Amazon OA 2023 Solution
- Get encrypted number Amazon OA 2023 Solution
- Find Total Imbalance Amazon OA 2023 Solution
- Find Total Power Amazon OA 2023 Solution