Bit Majority Number Generation
You are given an array arr
consisting of N
integers. Construct a number from these integers such that, for each bit position, if the quantity of set bits (1s) is greater than the quantity of unset bits (0s), that bit position in the resulting number should be set (1). Otherwise, it should be unset (0).
Example:
Input:
Three numbers: 8, 5, and 10.
Representation:
8 is 1 0 0 0
5 is 0 1 0 1
10 is 1 0 1 0
Bit positions are labeled i j k l.
Output:
1 0 0 0 -> 8
Explanation:
For position i: Majority are set bits, thus 1.
For positions j, k, and l: Majority are unset bits, thus 0.
Input Format:
The first line contains a single integer ‘T’ representing the number of test cases.
For each test case:
- The first line contains an integer ‘N’, the size of the array ‘arr’.
- The second line contains ‘N’ space-separated integers denoting the elements in the ‘arr’.
Output Format:
For each test case, output a single integer as per the described condition.
Constraints:
1 <= T <= 5
1 <= N <= 10^3
1 <= arr[i] <= 5 * 10^3
- Time Limit: 1 sec
AnswerBot
3d
Generate a number based on majority set bits in given integers array.
Iterate through each bit position and count set and unset bits
Construct the resulting number based on majority set bits at each pos...read more
Help your peers!
Add answer anonymously...
Top LinkedIn Software Developer interview questions & answers
Popular interview questions of Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app