Problem Statement:

An e-commerce company is planning to give a special discount on all its products to its customers for the Christmas holiday. The company possesses data on its stock of N product types. The data for each product type represents the count of customers who have ordered that product.

If the data K is positive, it means the product has been ordered by K customers and is in stock. If the data K is negative, it means it has been ordered by K customers but is not in stock.

The company will fulfill the order directly if the ordered product is in stock. If it is not in stock, the company will fulfill the order after they replenish the stock from the warehouse. They are planning to offer a discount amount A for each product. The discount value will be distributed among the customers who have purchased that selected product.

The discount will be distributed only if the decided amount A can be divided evenly by the number of orders for a particular product.


Objective

Write an algorithm for the sales team to find the number of products out of N for which the discount will be distributed.


Input Format

  • The first line of the input consists of an integer numOfProducts, representing the number of different types of products (N).
  • The second line consists of N space-separated integers: order0, order1, ..., orderN-1, representing the current order status for each product.
  • The last line consists of an integer disAmount, representing the discount amount to be distributed among customers.

Output Format

Print an integer representing the number of products out of N for which the discount will be distributed.


Constraints


0 <= numOfProducts, discountAmount <= 10^5
-10^6 <= order[i] <= 10^6
0 <= i < numOfProducts

Example


Input:
7
9 -13 8 -7 -8 18 10
18

Output:
2

Explanation:

The products for which the number of customers will collect the discount are product type 0 and 5, i.e., 9 and 18 respectively.

Because:

  • For 9 customers → 18 % 9 == 0 ✅
  • For 18 customers → 18 % 18 == 0 ✅
So, the output is 2.


Algorithm

  1. Read the number of products N.
  2. Read an array of order counts for each product.
  3. Read the discount amount A.
  4. Initialize a counter count = 0.
  5. For each product:
    • If the order count is positive (product in stock) and A % order[i] == 0, increment the counter.
  6. Return the final count.

Java Solution


import java.util.*;

public class Solution {

    public static int noOfProducts(int[] order, int disAmount) {
        int answer = 0;

        for (int i : order) {
            if (i > 0 && disAmount % i == 0) {
                answer++;
            }
        }

        return answer;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int order_size = in.nextInt();
        int[] order = new int[order_size];

        for (int idx = 0; idx < order_size; idx++) {
            order[idx] = in.nextInt();
        }

        int disAmount = in.nextInt();

        int result = noOfProducts(order, disAmount);
        System.out.println(result);
    }
}

Step-by-Step Example Execution

Input:


7
9 -13 8 -7 -8 18 10
18

Execution:

  1. Product 0 → 9 → 18 % 9 == 0 ✅
  2. Product 1 → -13 → not in stock ❌
  3. Product 2 → 8 → 18 % 8 != 0 ❌
  4. Product 3 → -7 → not in stock ❌
  5. Product 4 → -8 → not in stock ❌
  6. Product 5 → 18 → 18 % 18 == 0 ✅
  7. Product 6 → 10 → 18 % 10 != 0 ❌

Result: 2 products are eligible.


Final Output


2

Key Takeaways

  • Only products that are in stock (positive order count) are eligible for discount distribution.
  • Discount amount must be divisible by the number of orders for that product.
  • Use modulus operation (%) to check divisibility.
  • Efficient solution — runs inO(N) time complexity.