Problem: AVERAGE

SOLUTION LINK : SOLUTION

The code is failing, it’s based on the sliding window approach however, giving a WA, can somebody please help me figure out why is it failing or give a test case for the same ?

Thank you

Problem: AVERAGE

SOLUTION LINK : SOLUTION

The code is failing, it’s based on the sliding window approach however, giving a WA, can somebody please help me figure out why is it failing or give a test case for the same ?

Thank you

Consider the testcase:

```
15
28
28
23
15
16
53
53
19
4
30
31
28
22
52
20
```

The answer should be `9`

:

```
The number: 28 is the average of 4 and 52
The number: 28 is the average of 4 and 52
The number: 23 is the average of 15 and 31
The number: 15 is the not average of any pair of numbers
The number: 16 is the average of 4 and 28
The number: 53 is the average of 53 and 53
The number: 53 is the average of 53 and 53
The number: 19 is the average of 15 and 23
The number: 4 is the not average of any pair of numbers
The number: 30 is the not average of any pair of numbers
The number: 31 is the not average of any pair of numbers
The number: 28 is the average of 4 and 52
The number: 22 is the average of 16 and 28
The number: 52 is the not average of any pair of numbers
The number: 20 is the not average of any pair of numbers
9
```

2 Likes

The issue with your code is you are using floating point number for calculating mid. A better way would be to do 2*a[avg] == a[l]+a[r]

Then another issue is that if a number occurs more than once, then for it’s last occurence, you might not detect it as avg. This can be solved by doing another search but this time with outermost loop for r.

Sorry I have switched to c++ from python so I am not that used to. Well I probably have fixed my code but dont understand the issue with this

```
#include <iostream>
using namespace std;
int main() {
if (double(61/2) == 30){
cout << "yo" << endl;
}
return 0;
}
```

which for some reason is giving me the output. What am I doing wrong here ?

I checked it myself aswell the problem being that 30.5 is being recognised as 30. I dont know why

UPD: I have updated the code a bit so that it will probably work, if this issue gets right.

`61`

and `2`

are both `int`

s, so *integer* division is performed, giving `30`

.

The result is *then* cast to a `double`

, but by then it is too late.

1 Like

Because unlike python, when dividing two integers, it will be by default integer division. So the answer you’ll get will be same as you would get by 61//2 in python

2 Likes

Here is an updated version of your code which gives AC.

It has both the changes i mentioned in my previous comment.

https://www.codechef.com/viewsolution/28559911

This was new to me. Thanks lol, bad habits caught on due to python.

1 Like

Changed array type and some other changes to manipulate my answer to AC lol.

AC SOLUTION

But yeah, 2*a[i] approach is a lot better honestly. Thanks @lokesh58 @ssjgz for your time

1 Like