Sort Integers by Factor Value

Moderate
0/80
Average time to solve is 30m
profile
Contributed by
1 upvote
Asked in companies
MicrosoftZoho Corporation

Problem statement

You have been given an array/list ‘ARR’ of integers consisting of ‘N’ integers. The factor value is the number of following operations it takes for the number to become 1.

The two operations are as follows:-

If ‘x’ is even then ‘x’ will become ‘x / 2’.

If ‘x’ is odd then ‘x’ will become ‘x * 3 + 1’.

Your need to sort them in increasing order of factor value i.e. if two integers have the same factor value then sort in increasing order of their value. Your task is to return the ‘K-th’ value in the list after sorting.

Example:
Let’s say you have an array/list [1, 3, 4, 5] and ‘K’=2. The factor values are [0, 7, 2, 5] respectively. Finally, our array will look like [1, 4, 5, 3]. Since ‘K’ is 2 return 4.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains a single integer ‘T’ representing the number of test cases.

The first line of each test case contains two space-separated integers ‘N’ and ‘K’ representing the size of the array/list ‘ARR’ and position whose value in the sorted list you need to return.

The second line and the last line of input contain ‘N’ single space-separated integers representing the array/list elements.
Output Format:
For each test case, print a single line containing a single integer denoting the ‘K-th’ element in sorted list. 

The output of each test case will be printed in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 10
1 <= N <= 1000
1 <= K <= N
1 <= ‘ARR[i]’ <= 10 ^ 4

Where ‘ARR[i]’ is an element of array/list ARR.  

Time Limit: 1sec
Sample Input 1:
2
4 4
1 2 3 4 
2 1
13 12
Sample Output 1:
3
12

Sample Output 1 Explanation:

Test case 1:

The factor values of [1, 2, 3, 4] are [0, 1, 7, 2]. The array/list will become [1, 2, 4, 3] on sorting according to factor value.

Therefore the answer is 3.

Test case 2:

Both 12 and 13 have a factor value of 9. Since they have the same factor value we will sort them according to their value. Therefore on sorting ‘arr’ will be [12,13].

Therefore the answer is 12.
Sample Input 2:
2
4 3
1 2 6 4
4 2
7 8 9 10
Sample Output 2:
4
10
Hint

Use Priority Queue to store the result after each operation.

Approaches (1)
Using Priority Queue.

We will find the factor value of each element in the array. Finally, we will use a priority queue to maintain the sorted list.

 

We will apply the algorithm as follows:-

  • Declare a priority queue ‘pq’ which has two parameters. The first parameter represents the factor value of a number and the second is the original value of the number.
  • Iterate through the array ‘arr’:-
    • Declare a pair of integers ‘p’ whose first parameter is factor value and initialize it with 0. Initialize the second parameter with ‘arr[i]’.
    • Declare ‘j’ and initialize it with ‘arr[i]’.Do the following till ‘j’ becomes 1:-
      • Increment first parameter i.e. factor value of ‘p’ by 1.
      • If ‘j’ is even, update ‘j’ as ‘j’/2. Else update ‘j’ as  3*‘j’+1.
    • Insert the pair ‘p’ in priority queue ‘pq’.
    • If the size of ‘pq’ becomes greater than ‘k’ we can be sure that top element ‘k’ cannot be our answer as it is already at position ‘k+1’ in the sorted list. Therefore ‘pop’ the ‘top’ element.
  • Pop-out the top of the priority queue as it is ‘k-th’ element in the sorted list and store it in a pair of integers ‘ans’.
  • Return the second parameter of the ‘ans’.
Time Complexity

O(N * log K), where ‘N’ denotes the size of the array/list and ‘K’ denotes the position whose value in the sorted list you need to return.

 

We are inserting ‘N’ elements in the priority queue which takes ‘log K’ time for each insertion.

Space Complexity

O(K), where ‘K’ denotes a position whose value in the sorted list you need to return.

 

We build a priority queue whose maximum size is ‘K’.

Code Solution
(100% EXP penalty)
Sort Integers by Factor Value
Full screen
Console