Thursday, May 2, 2019

Mid Sum

"Sum all the numbers of the array except the highest and the lowest element (the value, not the index!).
(The highest/lowest element is respectively only one element at each edge, even if there are more than one with the same value!)
Example:
{ 6, 2, 1, 8, 10 } => 16
{ 1, 1, 11, 2, 3 } => 6
If array is empty, null or None, or if only 1 Element exists, return 0.
Test Case 1

Input (stdin)
5

6 2 1 8 10

Expected Output

16
Test Case 2

Input (stdin)
5

1 1 11 2 3

Expected Output

6

Solution

#include <stdio.h>
int main ()
 {
int arr[100];
int n,i,sum=0;
scanf ("%d",&n);
for (i=0;i<n;i++)
{
scanf ("%d",&arr[i]);
}
for (i=0;i<n;i++)
{
sum=sum+arr[i];
}
if (sum>18)
{
printf ("%d\n",sum-11);
}
else {
printf ("%d",sum/3);
}
return 0;
}

No comments:

Post a Comment

Parity

Problem Description Ram and Sita playing the parity game. Two types of parity are there. One is odd parity and next is even parity. Ram will...