COUNT ELEMENTS WITH SMALLER AND LARGER NEIGHBOURS
A PRESENTATION BY:
KORRAPATI SHANMUKHA SAI KRISHNA
251FA18418
SECTION-28
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.
Input: num [11,7,2,15]
Output: 2
Input: num [-3,3,3,90]
Output: 2
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.
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.
PROGRAM
PROGRAM
EXAMPLE PROGRAM
Problem={11,7,2,15}
Sample output:
GENERAL APPLICATIONS
This identifies "middle-range" values excluding extremes, useful in:
THANKYOU