Skip to content

Commit d0cd17c

Browse files
authored
Added problem AVERAGE
First commit and PR
1 parent a41a159 commit d0cd17c

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

Average/Average-Question.txt

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
Problem Name: Average
2+
3+
You are given a sequence of integers a1, a2, ..., aN. An element ak is said to be an average element if there are indices i, j (with i ≠ j) such that ak = (ai + aj) / 2.
4+
5+
In the sequence
6+
7+
3 7 10 22 17 15
8+
9+
for i=1, j=5 and k=3, we get ak = (ai + aj)/2. Thus a3 = 10 is an average element in this sequence. You can check that a3 is the only average element in this sequence.
10+
11+
Consider the sequence
12+
13+
3 7 10 3 18
14+
15+
With i=1, j=4 and k=1 we get ak = (ai +aj)/2. Thus a1=3 is an average element. We could also choose i=1, j=4 and k=4 and get ak=(ai +aj)/2. You can check that a1 and a4 are the only average elements of this sequence.
16+
17+
On the other hand, the sequence
18+
19+
3 8 11 17 30
20+
21+
has no average elements.
22+
23+
Your task is to count the number of average elements in the given sequence.
24+
25+
Input format
26+
27+
The first line contains a single integer N indicating the number of elements in the sequence. This is followed by N lines containing one integer each (Line i+1 contains ai). (You may assume that ai + aj would not exceed MAXINT for any i and j).
28+
29+
Output format
30+
31+
The output must consist of a single line containing a single integer k indicating the number of average elements in the given sequence.
32+
33+
Test Data:
34+
35+
You may assume that N ≤ 10000. Further, you may assume that in 30% of the inputs N ≤ 200 and that in 60% of the inputs N ≤ 5000.
36+
37+
Example:
38+
39+
We illustrate the input and output format using the above examples:
40+
41+
Sample Input 1:
42+
43+
6
44+
3
45+
7
46+
10
47+
17
48+
22
49+
15
50+
51+
Sample Output 1:
52+
53+
1
54+
55+
Sample Input 2:
56+
57+
5
58+
3
59+
7
60+
10
61+
3
62+
18
63+
64+
Sample Output 2:
65+
66+
2
67+
68+
Sample Input 3;
69+
70+
5
71+
3
72+
8
73+
11
74+
17
75+
30
76+
77+
Sample Output 3:
78+
79+
0
80+

Average/Solution

19.9 KB
Binary file not shown.

Average/Solution.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include<iostream>
2+
#include<algorithm>
3+
int arr[100000];
4+
using namespace std;
5+
int main()
6+
{
7+
int n;
8+
cin>>n;
9+
for(int h=0;h<n;h++)
10+
cin>>arr[h];
11+
sort(arr,arr+n);
12+
int count=0;
13+
for(int i=0;i<n;i++)
14+
{
15+
if(arr[i]==arr[i-1] || arr[i]==arr[i+1])
16+
count++;
17+
else
18+
{
19+
int l=i-1,h=i+1;
20+
while(l>=0 && h<n)
21+
{
22+
int sum=arr[l]+arr[h];
23+
if(sum==arr[i]*2)
24+
{count++;break;}
25+
else if(sum<arr[i]*2)
26+
++h;
27+
else
28+
--l;
29+
}
30+
}
31+
}
32+
cout<<count<<endl;
33+
return 0;
34+
}

0 commit comments

Comments
 (0)