मान लीजिए हमारे पास एक संख्या n है, हमें उस अधिकतम संख्या को ढूंढना है जो हम संख्या में कहीं भी 5 डालकर बना सकते हैं।
उदाहरण के लिए, यदि इनपुट है n = 826, तो आउटपुट होगा 8526।
हल करने के चरण
temp := nको स्ट्रिंग के रूप में।ans := -infiको 0 सेtempके आकार तक:cand := temp[0:i] + '5' + temp[i:]- यदि
i == 0औरtemp[0] == '-', तो अगले पुनरावृत्ति के लिए जारी रखें। ans := max(ans, int(cand))
ansलौटाएं।
उदाहरण
class Solution:
def solve(self, n):
temp = str(n)
ans = float('-inf')
for i in range(len(temp) + 1):
cand = temp[:i] + '5' + temp[i:]
if i == 0 and temp[0] == '-':
continue
ans = max(ans, int(cand))
return ans
ob = Solution()
print(ob.solve(826))
इनपुट
826
आउटपुट
8526