1. Ship Locks
A ship has n doors locked and the captain calls out an integer k. For the crew to get on the ship, all doors must be unlocked. The array locks represents n locks, where the ith lock has an initial value locks[i]. All the doors will unlock only when the values on all locks are the same.
In one operation, consider the current value of the ith lock.
1. If the current value = 1, its next value can be k or 2
2. If the current value = k, its next value can be k - 1 or 1
3. Otherwise, its next value can be current value - 1 or current value + 1.
Find the minimum number of operations such that the values on all locks are the same.
For example, k = 100, n = 3, locks = [1, 2, 99].
In the optimal assignment, the final common value should be 1
1. For 1 - operations required = 0
2. For 2 - operations required = 1 i.e 2 -> 1
3. For 99 - operations required = 2 i.e 99 -> 100 -> 1
It takes 0 + 1 + 2 = 3 operations to make all the values equal. Therefore, the minimum number of operations required is equal to 3.  Complete the function minOperations in the editor below.
minOperations has the following parameter(s):
int k: an integer
int locks[n]: the initial lock values
Returns
long int: the minimum number of operations required to have a common value on all locks
Constraints
• 2≤ k ≤109
1≤ n ≤105
• 1 ≤ locks[i] ≤ k
Input Format For Custom Testing
▼ Sample Case 0
Sample Input For Custom Testing
100
3
1
2
98
Sample Output
4  for this question i have code but it is getting falied for big inputs so optmise this below code for above question #include <bits/stdc++.h>
using namespace std;

// Function to compute the sum of minimum distances for a given t
long long compute_sum(int t, const vector<int>& locks, int k) {
    long long sum = 0;
    for(auto s : locks){
        long long d = abs((long long)s - t);
        if(d > k - d){
            d = k - d;
        }
        sum += d;
    }
    return sum;
}

long minOperations(int k, vector<int> locks) {
    int n = locks.size();
    
    // Initialize low and high for ternary search
    int low = 1;
    int high = k;
    long long best = LLONG_MAX;

    // Perform ternary search
    while(high - low > 3){
        int m1 = low + (high - low) / 3;
        int m2 = high - (high - low) / 3;
        long long sum1 = compute_sum(m1, locks, k);
        long long sum2 = compute_sum(m2, locks, k);
        if(sum1 < sum2){
            high = m2;
        }
        else{
            low = m1;
        }
    }

    // After ternary search, find the minimum in the remaining range
    for(int t = low; t <= high; t++){
        long long current_sum = compute_sum(t, locks, k);
        if(current_sum < best){
            best = current_sum;
        }
    }

    // Explicitly check the boundary values t=1 and t=k
    // This ensures that if the optimal t is at the boundary, it is considered
    long long sum_t1 = compute_sum(1, locks, k);
    if(sum_t1 < best){
        best = sum_t1;
    }

    long long sum_tk = compute_sum(k, locks, k);
    if(sum_tk < best){
        best = sum_tk;
    }

    return best;
}

// Main function to test the minOperations function with the sample input.
int main(){
    // Sample Input:
    // 100
    // 3
    // 1
    // 2
    // 98
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    
    int k, n;
    cin >> k;
    cin >> n;
    vector<int> locks(n);
    for(int i=0;i<n;i++) cin >> locks[i];
    long res = minOperations(k, locks);
    cout << res;
}