[Solved] A company is planning a big sale at which they will give their customers a special promotional discount

A company is planning a big sale at which they will give their customers a special promotional discount. Each customer that purchases a product from the company has a unique customerID numbered from 0 to N-1. Andy, the marketing head of the company, has selected bill amounts of the N customers for the promotional scheme. The discount will be given to the customers whose bill amounts are perfect squares. The customers may use this discount on a future purchase.

Write an algorithm to help Andy find the number of customers that will be given discounts. Input The first line of the input consists of an integer numOfCust, representing the number of customers whose bills are selected for the promotional discount (N). The second line consists of N space-separated integers-billo, bill…..bill representing the bill amounts of the N customers selected for the promotional discount.

Output

Print an integer representing the number of customers that will be given discounts.

Constraints

0 ≤ numOfCusts 106

Os bills 105

Osi<numOfCust

Example

Input
6
25 77 54 81 48 34

Output:
2
Explanation:
The bill amounts that are perfect squares are 25 and 81.
So, the output is 2.

Solution

public static int countCustomers(int[] bill){
        int count = 0;
        for(int i = 0; i < bill.length; i++){
            if(Math.sqrt(bill[i]) % 1 == 0){
                count++;
            }
        }
        return count;
    }
import java.util.Scanner;

public class a {
    public static int countPerfectSquares(int[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            int sqrt = (int) Math.sqrt(arr[i]);
            if (sqrt * sqrt == arr[i]) {
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        System.out.println(countPerfectSquares(arr));
    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *