Larger Smaller
Source: codechef
Difficulty: 1035

Larger Smaller
You are given an array$A$of$N$positive integers.
An integer$X$is said to be
good
if there exists indices$i$and$j$($1 \le i, j \le N$) such that$A_i < X$and$X < A_j$(i.e. there exists a smaller element and a larger element).
Find the number of
good
integers. It can be proven that the answer is always finite.
Input Format
The first line of input will contain a single integer$T$, denoting the number of test cases.
Each test case consists of multiple lines of input.
The first line contains$N$- the size of the array.
The second line contains$N$integers -$A_1, A_2, ..., A_N$.
Output Format
For each test case, output the number of
good
integers.
Constraints$1 \le T \le 100$$1 \le N \le 100$$1 \le A_i \le 100$Sample 1:
Explanation:
Test Case 1
: It can be shown that only the integers$2$,$5$,$3$and$4$are
good
.
For example,$2$is good because we can choose$i = 3$and$j = 2$,$A_i = 1 < 2 = X$and$A_j = 3 > 2 = X$.

Sample Input:
2
4
2 3 1 6
2
3 3

Sample Output:
4
0

C++ solution:
//Larger Smaller(cc)
//dfs code // Kadane Algorithum
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
void solve()
{
   ll n; cin >> n;
   set<ll> s;
   for(ll i=0;i<n;i++)
   {
        ll x; cin >> x;
        s.insert(x);
   }
   if(s.size() == 1)
   {
        cout  << "0" << endl;
   }
   else
   {
        cout << *s.rbegin()-*s.begin()-1 << endl;
   }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll t = 1;
    cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}

Go solution:
package main
import(
    "fmt"
    "bufio"
    "os"
    )

func main(){
    // your code goes here
    sc := bufio.NewReader(os.Stdin)
    var t int
    fmt.Fscan(sc,&t)
    for ;t>0;t--{
        var n int
        max := 0
        min := 2000
        fmt.Fscan(sc,&n)
        arr := make([]int, n)
        for i:=0;i<n;i++{
            fmt.Fscan(sc,&arr[i])
            if arr[i]< min{
                min = arr[i]
            }
            if arr[i]>max {
                max = arr[i]
            }
        }
        if max-min <= 1 {
            fmt.Println(0)
        }else{
            fmt.Println(max-min-1)
        }
    }
}

Java solution:
import java.util.*;
import java.io.*;

class Codechef {
    public static void main(String[] args) {
        FastReader sc = new FastReader();
    int t = sc.nextInt();
    while(t-->0){
        int a  =sc.nextInt();
        int[]ar = new int[a];
        for(int i=0;i<a;i++){
            ar[i]=sc.nextInt();
            
        }
        Arrays.sort(ar);
        int minn = ar[0];
        int mx = ar[ar.length-1];
        if(minn==mx){
            System.out.println(0);
            
        }
        else{
            int dif = mx-minn-1;
            System.out.println(dif);
        }
    }
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }
    }
}

JavaScript solution:
const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let inputLines = [];
let currentLine = 0;

rl.on('line', function (line) {
    inputLines.push(line.trim());
});

rl.on('close', function () {
    let T = parseInt(inputLines[currentLine++]);

    while (T--) {
        let N = parseInt(inputLines[currentLine++]);
        let nums = inputLines[currentLine++].split(' ').map(Number);

        let mn = 101;
        let mx = 0;

        for (let i = 0; i < N; i++) {
            let x = nums[i];
            if (x < mn) mn = x;
            if (x > mx) mx = x;
        }

        let ans = mx - mn - 1;
        if (ans < 0) ans = 0;

        console.log(ans);
    }
});

Python solution:
t = int(input())
for i in range(t):
    n = int(input())
    arr = list(map(int,input().split()))
    min_val = min(arr)
    max_val = max(arr)
    uniques = set(arr)
    res=[]
    good_count= max(0, max_val- min_val-1)
    print(good_count)

Ruby solution:
def count_good_integers
  test_cases = gets.to_i
  test_cases.times do
    n = gets.to_i
    arr = gets.split.map(&:to_i)
    min_val = arr.min
    max_val = arr.max
    
    if min_val == max_val
      puts 0
    else
      puts max_val - min_val - 1
    end
  end
end

count_good_integers