×

# [closed] August Challenge 2014: PRGIFT Can anyone tell me why my code is giving wrong answer?

 -15 1 I just counted the number of even numbers in the array. And if they are greater than or equal to k then it is possible for him to choose a segment otherwise not. #include using namespace std; int main() { int t; cin >> t; while(t--) { int a[51]; int n,k; int ans = 0; cin >> n >> k; for(int i = 0;i < n;i++) { cin >> a[i]; if(a[i]%2 == 0) ans++; } if(ans >= k) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }  asked 02 Aug '14, 09:02 6★ketanhwr 1.9k●3●18●44 accept rate: 15% same problem...is it consecutive even numbers is what they want ? (02 Aug '14, 11:31) But it still won't matter. I proved my algo. Its correct. (02 Aug '14, 11:33) ketanhwr6★ your method is wrong think it over. (02 Aug '14, 16:04)

### The question has been closed for the following reason "Problem is not reproducible or outdated" by ketanhwr 19 Sep '14, 13:39

 4 I think the case you are missing when k=0 and all number are even .....your code would output YES but the correct ans is NO answered 11 Aug '14, 15:03 597●4●11 accept rate: 16%
 1 suppose k=0 and number of even integers in the array are n...then ur code won't work answered 11 Aug '14, 15:04 71●1●1●4 accept rate: 0%
 1 Added one more if condition. This will work fine. #include using namespace std; int main() { int t; cin >> t; while(t--) { int a[51]; int n,k; int ans = 0; cin >> n >> k; for(int i = 0;i < n;i++) { cin >> a[i]; if(a[i]%2 == 0) ans++; } if(ans==n && k==0) cout << "NO" << endl; else if(ans >= k) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }  answered 11 Aug '14, 15:14 1.6k●11●31●41 accept rate: 7%
 1 If your ans value is n and k is 0 then it should return no instead of yes answered 11 Aug '14, 15:19 2★ravjsing 11 accept rate: 0%
 0 Hello, This was my logic too, especially due to this sentence: " So for the gift, chef will choose a consecutive non-empty segment of the array. The segment should contain exactly k even integers. Though it can have any number of odd integers. " As for the highlighted part in bold, I thought something along the lines of: "Well, they if I choose the entire array, I only need to check if the even numbers on it is >= K" However, this gave A LOT of Wrong Answers... I also wonder why... In the end, the only way I managed to get AC was to code a plain brute force solution by generating all the 50*49 consecutive subarrays... Maybe I misunderstood the problem or maybe it was badly phrased... Didn't you try the brute force method? Bruno answered 11 Aug '14, 15:00 3★kuruma 17.7k●72●143●209 accept rate: 8%
 0 One special condition check is enough. It is when whole array consists of even integers and k = 0. answered 11 Aug '14, 15:05 3★shadek 109●1●3 accept rate: 0%
 -1 Consider the constraint, k=0 i.e The chef wants to gift a segment which contains no even numbers. Eg 5 0 4 2 4 10 12 Your answer YES whereas the answer should have been NO. answered 11 Aug '14, 15:06 2★azix6 10●2 accept rate: 0%

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×7

question asked: 02 Aug '14, 09:02

question was seen: 3,106 times

last updated: 19 Sep '14, 13:39