Knowee
Questions
Features
Study Tools

Anytime MedianMax Score: 100Given an integer array, print the median for the sub-array 0 to i, for every i, 0 <= i <= N-1.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines - the first line contains N - the size of the array. The second line contains N integers - the elements of the array.Output FormatFor each test case, print the median for the sub-array 0 to i, for every i, separated by space. Print a new line between the output of different test cases. Note that in the case of even length sub-array, print the smaller element as the median.Constraints30 points1 <= T <= 1001 <= N <= 10370 points1 <= T <= 1001 <= N <= 104General Constraints-104 <= A[i] <= 104ExampleInput25-10 14 11 -5 7 32 -5 14 Output-10 -10 11 -5 7 2 -5 2 ExplanationSelf Explanatory

Question

Anytime MedianMax Score: 100Given an integer array, print the median for the sub-array 0 to i, for every i, 0 <= i <= N-1.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines - the first line contains N - the size of the array. The second line contains N integers - the elements of the array.Output FormatFor each test case, print the median for the sub-array 0 to i, for every i, separated by space. Print a new line between the output of different test cases. Note that in the case of even length sub-array, print the smaller element as the median.Constraints30 points1 <= T <= 1001 <= N <= 10370 points1 <= T <= 1001 <= N <= 104General Constraints-104 <= A[i] <= 104ExampleInput25-10 14 11 -5 7 32 -5 14 Output-10 -10 11 -5 7 2 -5 2 ExplanationSelf Explanatory

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

This problem is asking for a program that calculates the median of a sub-array for each index in an array. The program should print the median for the sub-array from index 0 to i for every i in the array.

Here are the steps to solve this problem:

  1. Read the number of test cases, T, from the input.
  2. For each test case, do the following: a. Read the size of the array, N, from the input. b. Read the elements of the array from the input. c. Initialize an empty list to store the medians. d. For each index i from 0 to N-1, do the following: i. Extract the sub-array from index 0 to i. ii. Sort the sub-array. iii. If the length of the sub-array is even, append the smaller of the two middle elements to the list of medians. Otherwise, append the middle element to the list of medians.
  3. Print the list of medians for each test case, separated by spaces. Print a new line between the output of different test cases.

Note: The constraints specify that the array can contain negative numbers and that the length of the array can be up to 10^4. Therefore, the sorting step could potentially be a performance bottleneck. A more efficient approach might be to use a data structure that maintains a sorted list, such as a balanced binary search tree or a heap.

This problem has been solved

Similar Questions

Swap the contents and indexes in an array.Max. score: 100Given an array with size n and its contents are from 0 to n-1. Swap the index and contents at that index.INPUT:Accept the size and its corresponding values.OUTPUT:Print the processed array.Explaination:input:a[0] = 3 a[1] = 2 a[2] = 4 a[3] = 1 a[4] = 0output:a[0] = 4 a[1] = 3 a[2] = 1 a[3] = 0 a[4] = 2SAMPLE INPUT 54 2 0 1 3SAMPLE OUTPUT 2 3 1 4 0

You work in the examination department of a school, and you've been given a task to sort the test scores of students in ascending order. The scores are stored in a list where each element represents the score of a student.Input FormatThe first line contains the integer N, the size of the array. The next line contains N space-separated integers.Constraints• 1<=N<=1000 • -1000<=a[i]<=1000Output FormatPrint the array as a row of space-separated integers in each iteration.Sample Input 01010 9 8 7 6 5 4 3 2 1Sample Output 05 9 8 7 6 10 4 3 2 15 4 8 7 6 10 9 3 2 15 4 3 7 6 10 9 8 2 15 4 3 2 6 10 9 8 7 15 4 3 2 1 10 9 8 7 63 4 5 2 1 10 9 8 7 63 2 5 4 1 10 9 8 7 61 2 3 4 5 10 9 8 7 61 2 3 4 5 10 9 8 7 61 2 3 4 5 10 9 8 7 61 2 3 4 5 8 9 10 7 61 2 3 4 5 8 7 10 9 61 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 10

Question8Max. score: 40.00Reverse first half and second half of the array.Input format :Size of the arrayArray elementsOutput format :Reversed arrayConstraints :1<=N<=100000Sample input1 2 3 4 7 8 6 5 4Sample output4 3 2 1 7 4 5 6 8ExplanationInput :71 2 3 4 5 6 7Output :3 2 1 7 6 5 4Note:Your code must be able to print the sample output from the provided sample input. However, your code is run against multiple hidden test cases. Therefore, your code must pass these hidden test cases to solve the problem statement.LimitsTime Limit: 5.0 sec(s) for each input fileMemory Limit: 256 MBSource Limit: 1024 KBScoringScore is assigned if any testcase passesAllowed LanguagesC

Q4 - Average Of all Array ElementsWrite a Program to Find the average of all elements in a Given Array?Constraints:Input          :- First Line of Input Consists of One Integer Value (Array Size).                     Second Line of Input Consists of few Integer Values Separated by Space (Array Elements).Output        :- Print the Average of All the Array Elements.Constraints  :- Array Size must be Greater then 0 or else Print "Invalid ArRay Size.".                      Print the Average value upto 5 Decimal points.Example:Input 1  :    6                  10 02 20 21 11 54Output 1:    Average of Given Array Elements is 19.66666. Input 2  :    4                  1 2 3 0Output 2:    Average of Given Array Elements is 1.50000.Explanation:Input 1  :    6                  10 02 20 21 11 54Output 1:    19.66666Explanation:                  Sum = 10 + 02 + 20 + 21 + 11 + 54                          = 118                  Average = 118 / 6 = 19.66666. Input 2  :    4                  1 2 3 0Output 2:    1.50000.Explanation:                  Sum = 1 + 2 + 3 + 0                          = 6                  Average = 6 / 4 = 1.55555.

Even SplitMax Score: 50Given a number N, check if you can split the number into 2 non-zero even parts.Input FormatFirst line of input contains T - number of test cases. Its followed by T lines, each line contains a single integer N.Output FormatFor each test case, print "Yes" if you can split the number into 2 non-zero even parts, "No" otherwise, separated by new line.Constraints1 <= T <= 1050 <= N <= 1018ExampleInput285OutputYesNoExplanationExample 1:You can split 8 as 4,4 or 6,2.Example 2:You cannot split 5 into 2 even parts.

1/3

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.