Problem
Chef is stuck in the wavey world of polynomials. You are given all roots of a polynomial . The roots are pairwise distinct integers, but they are not given in any particular order.
To help Chef escape, you should answer queries (numbered through ). For each valid , in the -th query, you are given an integer and you have to determine whether is positive, negative or .
Input
- The first line of the input contains two space-separated integers and .
- The second line contains space-separated integers .
- lines follow. For each valid , the -th of these lines contains a single integer describing the -th query.
Output
For each query, print a single line containing the string "POSITIVE"
, "NEGATIVE"
or "0"
(without quotes) describing the value of the polynomial for the -th query.
Constraints
- for each valid
- are pairwise distinct
- for each valid
Sample 1:
4 6 1 3 5 100 -2 2 4 80 107 5
POSITIVE NEGATIVE POSITIVE NEGATIVE POSITIVE 0
Explanation:
The given polynomial is .
Query : . This means that . Thus, the answer is POSITIVE
.
Query : . This means that . Thus, the answer is NEGATIVE
.
Query : . This means that . Thus, the answer is POSITIVE
.
Query : . This means that . Thus, the answer is 0
.
Program :
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int q = sc.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++)
a[i] = sc.nextInt();
Arrays.sort(a);
while(q-->0)
{
int x = sc.nextInt();
int s =0;
int e = n;
while(s<e)
{
int m = (s+e)/2;
if(x>a[m])
s = m+1;
else
e = m;
}
int l = n-s;
if(l==0)
System.out.println("POSITIVE");
else if(a[s]==x)
System.out.println("0");
else if(l%2!=0)
System.out.println("NEGATIVE");
else
System.out.println("POSITIVE");
}
}
}
0 Comments