Maximize total count from the given Array


Given an array nums of length N which contains two types of numbers, one which has the value zero, the second which is a positive integer, the task is to collect numbers from the below operations and return the maximum value you can collect.

  • If the given number is a positive integer, then it’s your choice, whether you can put it on the top of the queue or not.
  • Else, if the number is zero, then pick the topmost number from the queue and remove it.

Examples:

Input: N = 7, nums = [1, 2, 3, 0, 4, 5, 0]
Output: 8
Explanation: To maximize the total value do the following operation while iterating the nums[ ]:
nums[0] = 1, put on the top of the queue. Queue becomes: [1]
nums[1] = 2, put on the top of the queue. Queue becomes: [2, 1]
nums[2] = 3, put on the top of the queue. Queue becomes: [3, 2, 1]
nums[3] = 0, pick the top value from the queue and remove it. Total val = 3, and queue becomes: [2, 1]
nums[4] = 4, put on the top of the queue. Queue becomes: [4, 2, 1]
nums[5] = 5, put on the top of the queue. Queue becomes: [5, 4, 2, 1]
nums[6] = 0, pick the top value from the queue and remove it. Total val = 3 + 5 = 8, and queue becomes: [4, 2, 1]
Return val = 8.

Input: N = 8, nums = [5, 1, 2, 0, 0, 4, 3, 0]
Output: 11
Explanation: To maximize the total value do the following operation while iterating the nums[ ]:
nums[0] = 5,  put on the top of the queue. Queue becomes: [5]
nums[1] = 1, ignore this number. Queue remains: [5]
nums[2] = 2,  put on the top of the queue. Queue becomes: [2, 5]
nums[3] = 0, pick the top value from the queue and remove it. Total val = 0 + 2 = 2, and queue becomes: [5]
nums[4] = 0, pick the top value from the queue and remove it. Total val = 2 + 5 = 7, and queue becomes: [ ]
nums[5] = 4, put on the top of the queue. Queue becomes: [4]
nums[6] = 3, ignore this number. Queue remains: [4]
nums[7] = 0, pick the top value from the queue and remove it. Total val = 7 + 4 = 11, and queue becomes: [ ]
Return val = 11.

Approach: To solve the problem follow the below idea:

We will use a decreasing priority queue and store the positive integers in it, when we encounter zero we will take the peek() element (if it is not empty) from the priority queue and add it to the variable val.

Below are the steps for the above approach:

  • Initialize a decreasing priority queue.
  • Iterate the given array,
    • If you encounter any positive integer, add it to the priority queue.
    • Else, if you encounter a zero, check whether the priority queue is empty or not. If it is not empty, remove the top element from it and add it to the variable val which contains the current sum of the maximum value.
  • Return the final answer val.

Below is the code for the above approach:

Java

  

import java.util.*;

  

class GFG {

  

    

    public static void main(String[] args)

    {

        int N = 8;

        int[] nums = { 5, 1, 2, 0, 0, 4, 3, 0 };

        System.out.println("Maximum value is : "

                           + calculateMaxVal(nums, N));

    }

  

    

    public static int calculateMaxVal(int[] nums, int N)

    {

        PriorityQueue<Integer> decreasing

            = new PriorityQueue<Integer>(

                Collections.reverseOrder());

        int val = 0;

        for (int i = 0; i < N; i++) {

            if (nums[i] == 0) {

                if (!decreasing.isEmpty())

                    val += decreasing.remove();

            }

            else {

                decreasing.add(nums[i]);

            }

        }

  

        return val;

    }

}

Output
Maximum value is : 11

Time Complexity: O(N * log(N))
Auxiliary Space: O(N)


Given an array nums of length N which contains two types of numbers, one which has the value zero, the second which is a positive integer, the task is to collect numbers from the below operations and return the maximum value you can collect.

  • If the given number is a positive integer, then it’s your choice, whether you can put it on the top of the queue or not.
  • Else, if the number is zero, then pick the topmost number from the queue and remove it.

Examples:

Input: N = 7, nums = [1, 2, 3, 0, 4, 5, 0]
Output: 8
Explanation: To maximize the total value do the following operation while iterating the nums[ ]:
nums[0] = 1, put on the top of the queue. Queue becomes: [1]
nums[1] = 2, put on the top of the queue. Queue becomes: [2, 1]
nums[2] = 3, put on the top of the queue. Queue becomes: [3, 2, 1]
nums[3] = 0, pick the top value from the queue and remove it. Total val = 3, and queue becomes: [2, 1]
nums[4] = 4, put on the top of the queue. Queue becomes: [4, 2, 1]
nums[5] = 5, put on the top of the queue. Queue becomes: [5, 4, 2, 1]
nums[6] = 0, pick the top value from the queue and remove it. Total val = 3 + 5 = 8, and queue becomes: [4, 2, 1]
Return val = 8.

Input: N = 8, nums = [5, 1, 2, 0, 0, 4, 3, 0]
Output: 11
Explanation: To maximize the total value do the following operation while iterating the nums[ ]:
nums[0] = 5,  put on the top of the queue. Queue becomes: [5]
nums[1] = 1, ignore this number. Queue remains: [5]
nums[2] = 2,  put on the top of the queue. Queue becomes: [2, 5]
nums[3] = 0, pick the top value from the queue and remove it. Total val = 0 + 2 = 2, and queue becomes: [5]
nums[4] = 0, pick the top value from the queue and remove it. Total val = 2 + 5 = 7, and queue becomes: [ ]
nums[5] = 4, put on the top of the queue. Queue becomes: [4]
nums[6] = 3, ignore this number. Queue remains: [4]
nums[7] = 0, pick the top value from the queue and remove it. Total val = 7 + 4 = 11, and queue becomes: [ ]
Return val = 11.

Approach: To solve the problem follow the below idea:

We will use a decreasing priority queue and store the positive integers in it, when we encounter zero we will take the peek() element (if it is not empty) from the priority queue and add it to the variable val.

Below are the steps for the above approach:

  • Initialize a decreasing priority queue.
  • Iterate the given array,
    • If you encounter any positive integer, add it to the priority queue.
    • Else, if you encounter a zero, check whether the priority queue is empty or not. If it is not empty, remove the top element from it and add it to the variable val which contains the current sum of the maximum value.
  • Return the final answer val.

Below is the code for the above approach:

Java

  

import java.util.*;

  

class GFG {

  

    

    public static void main(String[] args)

    {

        int N = 8;

        int[] nums = { 5, 1, 2, 0, 0, 4, 3, 0 };

        System.out.println("Maximum value is : "

                           + calculateMaxVal(nums, N));

    }

  

    

    public static int calculateMaxVal(int[] nums, int N)

    {

        PriorityQueue<Integer> decreasing

            = new PriorityQueue<Integer>(

                Collections.reverseOrder());

        int val = 0;

        for (int i = 0; i < N; i++) {

            if (nums[i] == 0) {

                if (!decreasing.isEmpty())

                    val += decreasing.remove();

            }

            else {

                decreasing.add(nums[i]);

            }

        }

  

        return val;

    }

}

Output
Maximum value is : 11

Time Complexity: O(N * log(N))
Auxiliary Space: O(N)

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – admin@technoblender.com. The content will be deleted within 24 hours.
arrayCountMaximizeTechTechnoblenderTechnologytotal
Comments (0)
Add Comment