Question: Airtel Payments Bank, Recent Online Assessment Questions (16th August 2023 - SET 1) | Find the Total | Count Product Sequence
0
Entering edit mode

ADD COMMENTlink 15 months ago PoGo 2.4k
0
Entering edit mode

.

 

ADD COMMENTlink 15 months ago Prakhar Raghuvanshi • 0
0
Entering edit mode

QUESTION1) 

#include <bits/stdc++.h>
using namespace std;

void remover(vector<int> &arr, int idx) {
    int left = max(0, idx - 1);
    int right = min((int)arr.size() - 1, idx + 1);

    arr.erase(arr.begin() + left, arr.begin() + right + 1);
}

int main() {
    int n;
    cin >> n;

    vector<int> arr(n);
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }

    int totalSum = 0;

    while (arr.size() > 1) {
        int minElement = *min_element(arr.begin(), arr.end());
        int minIndex = min_element(arr.begin(), arr.end()) - arr.begin();

        totalSum += minElement;

        remover(arr, minIndex);
    }

    cout << totalSum << endl;

    return 0;
}

ADD COMMENTlink 3 months ago hehehaha • 10

Login before adding your answer.

Similar Posts
Loading Similar Posts