Problem Statement Select langua Python 3 Consider a non-empty string array inarr whose elements contain only digits from 0 to 5 such that no element in inarr will contain all the digits from 0 to 5. Identity and print outstr using the logic given below: 2 #inves >print(" • Identify the non-consecutive unique pairs of elements of inar, starting from left most element, such that all the digits from 0 to 5 are present at least once in the pair of elements • Form strings for each pair identified by concatenating the elements of the pair such that the order of their occurrence in inarr is preserved Set putatr with the largest string formed in the above step. If more than one string exists as largest strings, set outstr with the string whose first or second element used for concatenation appear first in inarr when traversing from left to right. Largest string is a string with maximum number of characters .no such pair is identified, print -1 Input format Read inarr with the elements separated by (comma) from the standard input stream Cutput format: Print outstr or-1 accordingly to the standard output stream
Answers
Answer:
# input the array
arr = [int(x) for x in input().split()]
list_of_pairs=[]
for i in range(len(arr)-2):
for j in range(i+2,len(arr)):
l=str(arr[i])+str(arr[j])
list_of_pairs.append(l)
unique_pairs=[]
for number in list_of_pairs:
temp = []
for num in number:
if num not in temp:
temp.append(num)
if len(temp) == 6:
unique_pairs.append(number)
if len(unique_pairs)==0:
print(-1)
else:
max_length = 0
for pair in unique_pairs:
a = len(pair)
if a > max_length:
length = []
max_length = a
length.append(pair)
elif (a == max_length):
length.append(pair)
no = []
for i in range(len(length)):
for j in range(2):
no.append(length[i][j])
no.sort()
a = len(length)
for m in range(len(length)):
if no[0] == length[m][0] or no[0] == length[m][1]:
print(length[0])
break
Explanation:
Answer:
def compareStrings(str1, str2):
i = 0
while i < len(str1) - 1 and str1[i] == str2[i]:
i += 1
if str1[i] > str2[i]:
return -1
return str1[i] < str2[i]
def searchStr(arr, string, first, last):
if first > last:
return -1
mid = (last + first) // 2
if len(arr[mid]) == 0:
left, right = mid - 1, mid + 1
while True:
if left < first and right > last:
return -1
if right <= last and len(arr[right]) != 0:
mid = right
break
if left >= first and len(arr[left]) != 0:
mid = left
break
right += 1
left -= 1
if compareStrings(string, arr[mid]) == 0:
return mid
if compareStrings(string, arr[mid]) < 0:
return searchStr(arr, string, mid+1, last)
return searchStr(arr, string, first, mid-1)
Explanation:
Modified Binary Search is a superior solution.
We compare the provided str with the middle string, just like in a standard binary search. If the middle string is empty, the nearest non-empty string x is found (by linearly searching on both sides). Once we've located x, we perform a conventional binary search, comparing the provided str against x. If str and x are the same, we return the index of x. We recur for the right half if str is bigger; otherwise, we recur for the left half.
#SPJ3