Max Distance that can be travelled coding Question

clock icon

Asked 8 months ago

message

0 Answers

eye

1 Views

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int maxDistance(vector<int>& arr) {
    int n = arr.size();
    int maxDist = 0;
    
    for (int i = 0; i < n; i++) {
        int sum = 0;
        for (int j = 0; j < n; j++) {
            sum += arr[(i + j) % n];
            maxDist = max(maxDist, abs(sum));
        }
    }
    
    return maxDist;
}

int main() {
    int n;
    cin >> n;
    
    vector<int> arr(n);
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    
    cout << maxDistance(arr) << endl;
    
    return 0;
}

Write your answer here

0 Responses