Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Sort Integers by Factor Value
MEDIUM
30 mins
1 upvotes
Arrays
Heap
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Arrays
-
-
Heap
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
SQL
-
-
Binary Search Trees
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Sort Integers by Factor Value

Contributed by
Sounak Majumder
Medium
yellow-spark
0/80
Avg time to solve 30 mins
Success Rate 70 %
Share
1 upvotes

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, Constraints, Images )
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
Reset Code
Full screen
Auto
copy-code
Console