Form smallest number Infosys springboard coding question with solution
Problem Statement :
Consider a non-empty array inarr consisting of non-zero unique positive integers separated by”,”(comma) and a positive integer innum which denotes the maximum number of swaps allowed.
Write a program to print a number outnum which represents a number with the lowest value that can be formed by concatenating all the elements of inarr with a maximum of innum swaps.
Assumption(s):
inarr will contain at least 2 elements
The elements of inarr will contain at most 3 digits
Note:
• Swapping the array elements of inarr is allowed but not the digits within the elements of inarr
Question :

Code Solution :
k=int(input())
s=input().split(",")
max_len = max([len(i) for i in s])
list2=[]
list5=[]
for i in s:
list5.append(i)
raw_len=len(i)
if raw_len==max_len:
temp=i
else:
to_add=max_len-raw_len
for j in range(0,to_add):
temp=i+i[0]*to_add
list2.append(temp)
list3=[]
for i in list2:
list3.append(i)
list3.sort()
for i in range(0,len(list2)):
list2[i]=int(list2[i])
for i in range(0,len(list2)):
min = 1000
for j in range(i+1,len(list2)):
if list2[j]min:
k-=1
temp=list2[i]
list2[i]=list2[indx]
list2[indx]=temp
temp = list5[i]
list5[i] = list5[indx]
list5[indx] = temp
out=""
for i in list5:
out+=i
print(int(out))
0 Comments