Untitled

 avatar
unknown
plain_text
a year ago
1.6 kB
6
Indexable
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <algorithm>
#include <functional>

using namespace std;

int moves(vector<int> arr) {
    int n = arr.size();
    int even_index = 0;
    int odd_index = n - 1;
    int move_count = 0;

    while (even_index < odd_index) {
        // Find the first odd number from the left
        while (even_index < n && arr[even_index] % 2 == 0) {
            even_index++;
        }

        // Find the first even number from the right
        while (odd_index >= 0 && arr[odd_index] % 2 != 0) {
            odd_index--;
        }

        // Swap the odd number from the left with the even number from the right
        if (even_index < odd_index) {
            swap(arr[even_index], arr[odd_index]);
            move_count++;
        }
    }

    return move_count;
}

// Helper functions to trim whitespace from strings
string ltrim(const string &str) {
    return str.substr(str.find_first_not_of(' '));
}

string rtrim(const string &str) {
    return str.substr(0, str.find_last_not_of(' ') + 1);
}

int main() {
    ofstream fout(getenv("OUTPUT_PATH"));
    string arr_count_temp;
    getline(cin, arr_count_temp);
    int arr_count = stoi(ltrim(rtrim(arr_count_temp)));
    
    vector<int> arr(arr_count);
    for (int i = 0; i < arr_count; i++) {
        string arr_item_temp;
        getline(cin, arr_item_temp);
        int arr_item = stoi(ltrim(rtrim(arr_item_temp)));
        arr[i] = arr_item;
    }

    int result = moves(arr);
    fout << result << endl;
    fout.close();
    return 0;
}
Editor is loading...
Leave a Comment