A Company is planning a big sale at which they will give their customers a special promotional discount. Each customer that purchases product from a company has a unique customerID number from 0 to N-1. Andy, the marketing head of all the company has selected bill amounts of N customers for promotional scheme .The discount will be given to customers whose fill amounts are perfect squares .The customers may use this discount on a future purchase.
A Company is planning a big sale at which they will give their customers a special promotional discount. Each customer that purchases product from a company has a unique customerID number from 0 to N-1. Andy, the marketing head of all the company has selected bill amounts of N customers for promotional scheme .The discount will be given to customers whose fill 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 contains of N space separated
integers bill0,bill1....bill(n-1) representing the bill amounts of the N
customers selected for promotional discount.
Output:
Print an integer representing the
number of customers that will be given discounts.
Example:
6
25 77 54 81 48 34
Output
2
Here, only 25 and 81 are perfect
squares so only 2 customers get discount
Code:
import java.util.Scanner;
public class PerfectRoot {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int numofCust=sc.nextInt();
sc.nextLine();
String discount=sc.nextLine();
String[] discounts=discount.split(" ");
int count=0;
for(int i=0;i<numofCust;i++)
{
if(isPerfectSquare(Integer.parseInt(discounts[i])))
count++;
}
System.out.println(count);
}
static boolean isPerfectSquare(int x) {
if (x >= 0) {
int sr = (int)Math.sqrt(x);
return ((sr * sr) == x);
}
return false;
}
}
Comments
Post a Comment