LeetCode 1421: NPV Queries Solution

Master LeetCode problem 1421 (NPV Queries), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1421. NPV Queries

Problem Explanation

Explanation:

Given a list of integers nums, we need to support two types of queries:

  1. Query(Type=1, P): Add P to all elements in the list
  2. Query(Type=2, P): Return the sum of all elements in the list modulo P

To efficiently handle these queries, we can maintain a running sum of all elements in the list. When a query of type 1 is received, we simply add P to the running sum. When a query of type 2 is received, we return the running sum modulo P.

: :

Solution Code

class NPVQueries {
    long runningSum = 0;

    public void query(int type, int p) {
        if (type == 1) {
            runningSum += p;
        } else if (type == 2) {
            System.out.println(runningSum % p);
        }
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1421 (NPV Queries)?

This page provides optimized solutions for LeetCode problem 1421 (NPV Queries) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1421 (NPV Queries)?

The time complexity for LeetCode 1421 (NPV Queries) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1421 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1421 in Java, C++, or Python.

Back to LeetCode Solutions