LeetCode-in-Java

3080. Mark Elements on Array by Performing Queries

Medium

You are given a 0-indexed array nums of size n consisting of positive integers.

You are also given a 2D array queries of size m where queries[i] = [indexi, ki].

Initially all elements of the array are unmarked.

You need to apply m queries on the array in order, where on the ith query you do the following:

Return an array answer of size m where answer[i] is the sum of unmarked elements in the array after the ith query.

Example 1:

Input: nums = [1,2,2,1,2,3,1], queries = [[1,2],[3,3],[4,2]]

Output: [8,3,0]

Explanation:

We do the following queries on the array:

Example 2:

Input: nums = [1,4,2,3], queries = [[0,1]]

Output: [7]

Explanation: We do one query which is mark the element at index 0 and mark the smallest element among unmarked elements. The marked elements will be nums = [**1**,4,**2**,3], and the sum of unmarked elements is 4 + 3 = 7.

Constraints:

Solution

@SuppressWarnings({"java:S1871", "java:S6541"})
public class Solution {
    public long[] unmarkedSumArray(int[] nums, int[][] queries) {
        int l = nums.length;
        int[] orig = new int[l];
        for (int i = 0; i < l; i++) {
            orig[i] = i;
        }
        int x = 1;
        while (x < l) {
            int[] temp = new int[l];
            int[] teor = new int[l];
            int y = 0;
            while (y < l) {
                int s1 = 0;
                int s2 = 0;
                while (s1 + s2 < 2 * x && y + s1 + s2 < l) {
                    if (s2 >= x || y + x + s2 >= l) {
                        temp[y + s1 + s2] = nums[y + s1];
                        teor[y + s1 + s2] = orig[y + s1];
                        s1++;
                    } else if (s1 >= x) {
                        temp[y + s1 + s2] = nums[y + x + s2];
                        teor[y + s1 + s2] = orig[y + x + s2];
                        s2++;
                    } else if (nums[y + s1] <= nums[y + x + s2]) {
                        temp[y + s1 + s2] = nums[y + s1];
                        teor[y + s1 + s2] = orig[y + s1];
                        s1++;
                    } else {
                        temp[y + s1 + s2] = nums[y + x + s2];
                        teor[y + s1 + s2] = orig[y + x + s2];
                        s2++;
                    }
                }
                y += 2 * x;
            }
            for (int i = 0; i < l; i++) {
                nums[i] = temp[i];
                orig[i] = teor[i];
            }
            x *= 2;
        }
        int[] change = new int[l];
        for (int i = 0; i < l; i++) {
            change[orig[i]] = i;
        }
        boolean[] mark = new boolean[l];
        int m = queries.length;
        int st = 0;
        long sum = 0;
        for (int num : nums) {
            sum += num;
        }
        long[] out = new long[m];
        for (int i = 0; i < m; i++) {
            int a = queries[i][0];
            if (!mark[change[a]]) {
                mark[change[a]] = true;
                sum -= nums[change[a]];
            }
            int b = queries[i][1];
            int many = 0;
            while (many < b) {
                if (st == l) {
                    out[i] = sum;
                    break;
                }
                if (!mark[st]) {
                    mark[st] = true;
                    sum -= nums[st];
                    many++;
                }
                st++;
            }
            out[i] = sum;
        }
        return out;
    }
}