You are not logged in. Please login at www.codechef.com to post your questions!

×

[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<iostream>
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

ketanhwr's gravatar image

6★ketanhwr
1.9k31844
accept rate: 15%

closed 19 Sep '14, 13:39

same problem...is it consecutive even numbers is what they want ?

(02 Aug '14, 11:31) codebreaker1234★

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) anshkhanna74★

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


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

link

answered 11 Aug '14, 15:03

thechamp103's gravatar image

3★thechamp103
597411
accept rate: 16%

suppose k=0 and number of even integers in the array are n...then ur code won't work

link

answered 11 Aug '14, 15:04

cs1120him's gravatar image

4★cs1120him
71114
accept rate: 0%

Added one more if condition. This will work fine.

#include<iostream>
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;
}
link

answered 11 Aug '14, 15:14

filmwalams's gravatar image

5★filmwalams
1.6k113141
accept rate: 7%

edited 11 Aug '14, 15:17

If your ans value is n and k is 0 then it should return no instead of yes

link

answered 11 Aug '14, 15:19

ravjsing's gravatar image

2★ravjsing
11
accept rate: 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

link

answered 11 Aug '14, 15:00

kuruma's gravatar image

3★kuruma
17.7k72143209
accept rate: 8%

edited 11 Aug '14, 15:02

One special condition check is enough. It is when whole array consists of even integers and k = 0.

link

answered 11 Aug '14, 15:05

shadek's gravatar image

3★shadek
10913
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.

link

answered 11 Aug '14, 15:06

azix6's gravatar image

2★azix6
102
accept rate: 0%

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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