1 of 9

COUNT ELEMENTS WITH SMALLER AND LARGER NEIGHBOURS

A PRESENTATION BY:

KORRAPATI SHANMUKHA SAI KRISHNA

251FA18418

SECTION-28

2 of 9

QUESTION

Given an integer array num, return the number of elements that have both a strictly smaller and a strictly greater element appear in num.

  • Example 1:

Input: num [11,7,2,15]

Output: 2

  • Example 2:

Input: num [-3,3,3,90]

Output: 2

3 of 9

ALGORITHM

1. Start

2. Check if the array is empty; if yes, return 0.

3. Initialize two variables: min_val to a large number (e.g., maximum integer) and max_val to a small number (e.g., minimum integer).

4. Traverse the entire array once: update min_val if current element is smaller, and max_val if current element is larger.

5. Initialize a counter to 0.

6. Traverse the array again: for each element, check if it is strictly greater than min_val AND strictly less than max_val.

7. If the condition in step 5 holds, increment the counter.

8. Return the counter value.

4 of 9

EXPLANATION

1. Goal: Count array elements that have BOTH smaller AND larger elements in array.

2. Key Insight: These are elements STRICTLY BETWEEN overall min and max values.

3. Step 1: Find minimum value in entire array.

4. Step 2: Find maximum value in entire array.

5. Step 3: Count elements where min < element < max.

6. Result: That's your answer - elements equal to min or max are excluded.

5 of 9

PROGRAM

6 of 9

PROGRAM

7 of 9

EXAMPLE PROGRAM

Problem={11,7,2,15}

Sample output:

8 of 9

GENERAL APPLICATIONS

This identifies "middle-range" values excluding extremes, useful in:

  1. Data analysis: Filter typical values in datasets (e.g., average test scores excluding outliers for fair grading).​
  2. Finance: Count transactions between min/max amounts to detect normal spending patterns.​
  3. Resource management: Identify usable server loads between lowest/highest capacities for balanced allocation.​
  4. Health monitoring: Track heart rates strictly between session min/max to flag normal trends.

9 of 9

THANKYOU