Test time left: 01:28:08smaller elements to the right of that respective elementGiven an List of integers, you need to return a new list where each element in the new list is the number of smaller elements to the right of that respective element in the original input list.For example:You are given the List of integer values [3, 4, 9, 6, 1] and the resultant list you return is [1, 1, 2, 1, 0].Explanation:There is 1 smaller element to the right of `3`There is 1 smaller element to the right of `4`There are 2 smaller elements to the right of `9`There is 1 smaller element to the right of `6`There are no i.e. 0 smaller elements to the right of `1`Constraint: Input list can contain positive as well as negative integer values.You Just have to complete the function get_smaller_right(arr) where arr is the list of integer elements passed, and function will return the resultant array as required.Instruction: To execute your custom test cases, please provide input as mentioned in the visible sample test cases.Sample Test CasesTest Case 1:Expected Output:Enter·the·elements·of·the·list·separated·by·spaces:·3 4 9 6 1The·resultant·sub·array·is:·[1,·1,·2,·1,·0]Test Case 2:Expected Output:Enter·the·elements·of·the·list·separated·by·spaces:·1 2 3 4 5The·resultant·sub·array·is:·[0,·0,·0,·0,·0]Test Case 3:Expected Output:Enter·the·elements·of·the·list·separated·by·spaces:·5 4 3 2 1The·resultant·sub·array·is:·[4,·3,·2,·1,·0]Submit12345678910111213141516def·get_smaller_right(arr):¬————n=len(arr)¬————result·=·[0]*n¬————for·i·in·range(n):¬————————count·=·0¬————————for·j·in·range(i+1,n):¬————————————if·arr[j]<arr[i]:¬————————————————count·+=1¬————————————result[i]·=·count¬————————return·result¬————————————¬————————————¬list1·=·[int(i)·for·i·in·input("Enter·the·elements·of·the·list·separated·by·spaces:·").split("·")]¬resultArray·=·get_smaller_right(list1)¬print("The·resultant·sub·array·is:",resultArray)¬·¶Execution Results 1 out of 3 shown cases successful0 out of 4 hidden cases successfulShow only failed cases Test Case - 1 (Execution Time: 3 ms) Expected Output User OutputEnter·the·elements·of·the·list·separated·by·spaces:·3 4 9 6 1Enter·the·elements·of·the·list·separated·by·spaces:·3 4 9 6 1The·resultant·sub·array·is:·[1,·1,·2,·1,·0] The·resultant·sub·array·is:·[1,·0,·0,·0,·0] : indicates the mismatch in the expected output. Test Case - 2 (Execution Time: 4 ms) Expected Output User OutputEnter·the·elements·of·the·list·separated·by·spaces:·1 2 3 4 5Enter·the·elements·of·the·list·separated·by·spaces:·1 2 3 4 5The·resultant·sub·array·is:·[0,·0,·0,·0,·0]The·resultant·sub·array·is:·[0,·0,·0,·0,·0] Test Case - 3 (Execution Time: 5 ms) Expected Output User OutputEnter·the·elements·of·the·list·separated·by·spaces:·5 4 3 2 1Enter·the·elements·of·the·list·separated·by·spaces:·5 4 3 2 1The·resultant·sub·array·is:·[4,·3,·2,·1,·0] The·resultant·sub·array·is:·[4,·0,·0,·0,·0] : indicates the mismatch in the expected output.
Question
Test time left: 01:28:08smaller elements to the right of that respective elementGiven an List of integers, you need to return a new list where each element in the new list is the number of smaller elements to the right of that respective element in the original input list.For example:You are given the List of integer values [3, 4, 9, 6, 1] and the resultant list you return is [1, 1, 2, 1, 0].Explanation:There is 1 smaller element to the right of 3There is 1 smaller element to the right of 4There are 2 smaller elements to the right of 9There is 1 smaller element to the right of 6There are no i.e. 0 smaller elements to the right of 1Constraint: Input list can contain positive as well as negative integer values.You Just have to complete the function get_smaller_right(arr) where arr is the list of integer elements passed, and function will return the resultant array as required.Instruction: To execute your custom test cases, please provide input as mentioned in the visible sample test cases.Sample Test CasesTest Case 1:Expected Output:Enter·the·elements·of·the·list·separated·by·spaces:·3 4 9 6 1The·resultant·sub·array·is:·[1,·1,·2,·1,·0]Test Case 2:Expected Output:Enter·the·elements·of·the·list·separated·by·spaces:·1 2 3 4 5The·resultant·sub·array·is:·[0,·0,·0,·0,·0]Test Case 3:Expected Output:Enter·the·elements·of·the·list·separated·by·spaces:·5 4 3 2 1The·resultant·sub·array·is:·[4,·3,·2,·1,·0]Submit12345678910111213141516def·get_smaller_right(arr):¬————n=len(arr)¬————result·=·[0]*n¬————for·i·in·range(n):¬————————count·=·0¬————————for·j·in·range(i+1,n):¬————————————if·arr[j]<arr[i]:¬————————————————count·+=1¬————————————result[i]·=·count¬————————return·result¬————————————¬————————————¬list1·=·[int(i)·for·i·in·input("Enter·the·elements·of·the·list·separated·by·spaces:·").split("·")]¬resultArray·=·get_smaller_right(list1)¬print("The·resultant·sub·array·is:",resultArray)¬·¶Execution Results 1 out of 3 shown cases successful0 out of 4 hidden cases successfulShow only failed cases Test Case - 1 (Execution Time: 3 ms) Expected Output User OutputEnter·the·elements·of·the·list·separated·by·spaces:·3 4 9 6 1Enter·the·elements·of·the·list·separated·by·spaces:·3 4 9 6 1The·resultant·sub·array·is:·[1,·1,·2,·1,·0] The·resultant·sub·array·is:·[1,·0,·0,·0,·0] : indicates the mismatch in the expected output. Test Case - 2 (Execution Time: 4 ms) Expected Output User OutputEnter·the·elements·of·the·list·separated·by·spaces:·1 2 3 4 5Enter·the·elements·of·the·list·separated·by·spaces:·1 2 3 4 5The·resultant·sub·array·is:·[0,·0,·0,·0,·0]The·resultant·sub·array·is:·[0,·0,·0,·0,·0] Test Case - 3 (Execution Time: 5 ms) Expected Output User OutputEnter·the·elements·of·the·list·separated·by·spaces:·5 4 3 2 1Enter·the·elements·of·the·list·separated·by·spaces:·5 4 3 2 1The·resultant·sub·array·is:·[4,·3,·2,·1,·0] The·resultant·sub·array·is:·[4,·0,·0,·0,·0] : indicates the mismatch in the expected output.
Solution
The problem is asking to return a new list where each element in the new list is the number of smaller elements to the right of that respective element in the original input list.
Here is the step by step solution:
-
First, we need to define a function
get_smaller_right(arr)wherearris the list of integer elements passed. -
Inside the function, we initialize a variable
nto the length of the array andresultto a new list of sizenfilled with zeros. -
Then, we iterate over the array from the start to the end. For each element at index
i, we initialize a countercountto 0. -
We then iterate over the array from index
i+1to the end. For each element at indexj, if the element at indexjis smaller than the element at indexi, we increment the countercount. -
After the inner loop, we set the element at index
iin theresultlist to the countercount. -
After the outer loop, we return the
resultlist. -
Outside the function, we take the input from the user, convert it to a list of integers, and store it in
list1. -
We then call the function
get_smaller_right(list1)and store the result inresultArray. -
Finally, we print the
resultArray.
The provided code seems to have a bug as it is not providing the expected output for all test cases. The issue is with the placement of the line result[i] = count. This line should be placed outside the inner loop, not inside it. Here is the corrected code:
def get_smaller_right(arr):
n = len(arr)
result = [0]*n
for i in range(n):
count = 0
for j in range(i+1, n):
if arr[j] < arr[i]:
count += 1
result[i] = count
return result
list1 = [int(i) for i in input("Enter the elements of the list separated by spaces: ").split()]
resultArray = get_smaller_right(list1)
print("The resultant sub array is:", resultArray)
This code should now work as expected for all test cases.
Similar Questions
smaller elements to the right of that respective elementGiven an List of integers, you need to return a new list where each element in the new list is the number of smaller elements to the right of that respective element in the original input list.For example:You are given the List of integer values [3, 4, 9, 6, 1] and the resultant list you return is [1, 1, 2, 1, 0].Explanation:There is 1 smaller element to the right of `3`There is 1 smaller element to the right of `4`There are 2 smaller elements to the right of `9`There is 1 smaller element to the right of `6`There are no i.e. 0 smaller elements to the right of `1`Constraint: Input list can contain positive as well as negative integer values.You Just have to complete the function get_smaller_right(arr) where arr is the list of integer elements passed, and function will return the resultant array as required.Instruction: To execute your custom test cases, please provide input as mentioned in the visible sample test cases.Sample Test CasesTest Case 1:Expected Output:Enter·the·elements·of·the·list·separated·by·spaces:·3 4 9 6 1The·resultant·sub·array·is:·[1,·1,·2,·1,·0]Test Case 2:Expected Output:Enter·the·elements·of·the·list·separated·by·spaces:·1 2 3 4 5The·resultant·sub·array·is:·[0,·0,·0,·0,·0]Test Case 3:Expected Output:Enter·the·elements·of·the·list·separated·by·spaces:·5 4 3 2 1The·resultant·sub·array·is:·[4,·3,·2,·1,·0]
Test time left: 11:37Select the correct answerSuppose list1 is [28, 383, 26, 89, 25], What is list1[:-1]?
Test time left: 22:45Write the codeGiven a set of distinct positive integers, find the largest subset such that every pair of elements in the subset (i, j) satisfies either i % j = 0 or j % i = 0.For example, given the set [3, 5, 10, 20, 21], you should return [5, 10, 20]. Given [1, 3, 6, 24], return [1, 3, 6, 24]
Create a list num=[23,12,5,9,65,44].➢ Print the length of the list.➢ Print the elements from second to fourth position using positive indexing.➢ Print the elements from position third to fifth using negative indexing
949. Largest Time for Given DigitsMedium6961048Add to ListShareGiven an array arr of 4 digits, find the latest 24-hour time that can be made using each digit exactly once.24-hour times are formatted as "HH:MM", where HH is between 00 and 23, and MM is between 00 and 59. The earliest 24-hour time is 00:00, and the latest is 23:59.Return the latest 24-hour time in "HH:MM" format. If no valid time can be made, return an empty string. Example 1:Input: arr = [1,2,3,4]Output: "23:41"Explanation: The valid 24-hour times are "12:34", "12:43", "13:24", "13:42", "14:23", "14:32", "21:34", "21:43", "23:14", and "23:41". Of these times, "23:41" is the latest.Example 2:Input: arr = [5,5,5,5]Output: ""Explanation: There are no valid 24-hour times as "55:55" is not valid. Constraints:arr.length == 40 <= arr[i] <= 9Accepted85,586Submissions243,556Seen this question in a real interview before?YesNoCompaniesRelated TopicsSimilar Questions
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.