Chargebee | SWE | Minimise UniqSum
Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2022-09-28Added By Anonymous
Source: Link
Company: Chargebee
Role: SWE (Likely Software Engineer)
Interview Stage: unknown
Location: unknown
Year: unknown
Original Experience:
You are given an array nums consisting og N integers, the integers in the array are an integer power of 2. UniqSum is defined as the sum of all distinct elements in nums. You have to perform the followinf operatin at most K times: 1. Select an integer i such that 1 <= i <= size, nums[i] != 1 2. Delete nums[i] 3. Add two numbers with value nums[i]/2 at the end of the array. If the selected number has a value of 4, then append two integers with value 2 at the end, and delete the original integer. Note: 1- based indexing is followed. Task Print the minimum possible UniqSum of the given list after applying at most K operations. Example 1 Assumptions * N = 2 * K = 1 * nums = [2, 4] * For the given case, after one operation possible values of nums are : * Select i = 1, [4, 1, 1] UniqSum = 4 + 1 = 5 * Select i = 2, [2, 2, 2] UniqSum = 2 Hence, the answer is 2. Example 2 Assumptions * N = 2 * K = 3 * nums = [2, 16] * Approach: * After atmost three operations possible values of nums are [2, 8, 8], [2, 4, 4, 8], [2, 16], ..., [2, 4, 4, 4, 4] * [2, 4, 4, 4, 4] jas minimum UniqSum of all possible values. * [2, 4, 4, 4, 4] can be obtained by selecting 16, then 8, and finally 8. Hence, the answer is 6. Function Description Complete the minUniqSum function provided in the editor. The function takes the following 2 parameters and return the minimum possible UniqSum based on the parameters: * N: Represents an integer denoting the number of integers * K: Represents an integer denoting the maximum number of operations * nums: Represents an array containing N integers Input format Note : This is the input format that you must use to provide custom input. * The first line contains*T* denoting the number of test cases. T also specifies the number of times you have to run the minUniqSum function on a different set of inputs * For each test case: * The first line contains *N* denoting the number of integers. * The secnd ine contains *K* denoting the number of operations. * The third line contains *N* space separated integers denoting the elements on array nums. Output format For each test case print the answer representing the minimum possible sum of all distinct elements in nums.
Role: SWE (Likely Software Engineer)
Interview Stage: unknown
Location: unknown
Year: unknown
Original Experience:
You are given an array nums consisting og N integers, the integers in the array are an integer power of 2. UniqSum is defined as the sum of all distinct elements in nums. You have to perform the followinf operatin at most K times: 1. Select an integer i such that 1 <= i <= size, nums[i] != 1 2. Delete nums[i] 3. Add two numbers with value nums[i]/2 at the end of the array. If the selected number has a value of 4, then append two integers with value 2 at the end, and delete the original integer. Note: 1- based indexing is followed. Task Print the minimum possible UniqSum of the given list after applying at most K operations. Example 1 Assumptions * N = 2 * K = 1 * nums = [2, 4] * For the given case, after one operation possible values of nums are : * Select i = 1, [4, 1, 1] UniqSum = 4 + 1 = 5 * Select i = 2, [2, 2, 2] UniqSum = 2 Hence, the answer is 2. Example 2 Assumptions * N = 2 * K = 3 * nums = [2, 16] * Approach: * After atmost three operations possible values of nums are [2, 8, 8], [2, 4, 4, 8], [2, 16], ..., [2, 4, 4, 4, 4] * [2, 4, 4, 4, 4] jas minimum UniqSum of all possible values. * [2, 4, 4, 4, 4] can be obtained by selecting 16, then 8, and finally 8. Hence, the answer is 6. Function Description Complete the minUniqSum function provided in the editor. The function takes the following 2 parameters and return the minimum possible UniqSum based on the parameters: * N: Represents an integer denoting the number of integers * K: Represents an integer denoting the maximum number of operations * nums: Represents an array containing N integers Input format Note : This is the input format that you must use to provide custom input. * The first line contains*T* denoting the number of test cases. T also specifies the number of times you have to run the minUniqSum function on a different set of inputs * For each test case: * The first line contains *N* denoting the number of integers. * The secnd ine contains *K* denoting the number of operations. * The third line contains *N* space separated integers denoting the elements on array nums. Output format For each test case print the answer representing the minimum possible sum of all distinct elements in nums.