Posted on

Longest Arithmatic

Description

Submission

#include <iostream>
#include <vector>
using namespace std;

int solve(vector<int>& nums)
{
    if(nums.size() <= 2) return nums.size();
    int step = 0, count = 1, maxCount = 1;
    for(int i = 1; i < nums.size(); ++i) {
        if(nums[i] - nums[i - 1] == step) {
            count++;
        }
        else {
            step = nums[i] - nums[i-1];
            count = 2;
        }
        maxCount = max(count, maxCount);
    }
    return maxCount;
}
int main() {
    int T; cin >> T;
    for(int Ti = 0; Ti < T; ++Ti) {
        int N; cin >> N;
        vector<int> nums(N);
        for(int i = 0; i < N; ++i) {
            cin >> nums[i];
        }
        cout << "Case #" << Ti + 1 << ": " << solve(nums) << "\n";
    }
    return 0;
}

High Buildings

Submission

#include <iostream>
#include <vector>

using namespace std;

vector<int> solve(int n, int a, int b, int c) {
    if(n == 1) {
        if(a == 1 && b == 1 && c == 1) return {1};
        return {};
    }

    int unique_a = a - c;
    int unique_b = b - c;
    int total_min = unique_a + unique_b + c;
    if(total_min > n) return {};
    vector<int> res;
    for(int i = 0; i < unique_a; ++i) {
        res.push_back(n - 1);
    }
    int n_padding = n - total_min;
    bool padded = false;
    if(!padded && unique_a > 0) {
        for(int i = 0; i < n_padding; ++i) {
            res.push_back(1);
        }
        padded = true;
    }

    for(int i = 0; i < c; ++i) {
        if(!padded && i == 1) {
            for(int i = 0; i < n_padding; ++i) {
                res.push_back(1);
            }
            padded = true;
        }
        res.push_back(n);
    }
    if(!padded && unique_b > 0) {
        for(int i = 0; i < n_padding; ++i) {
            res.push_back(1);
        }
        padded = true;
    }
    for(int i = 0; i < unique_b; ++i) {
        res.push_back((n - 1));
    }
    if(!padded) return {};
    return res;
}

int main() {
    int T; cin >> T;
    for(int Ti = 0; Ti < T; ++Ti) {
        int N, A, B, C;
        cin >> N >> A >> B >> C;
        cout << "Case #" << Ti + 1 << ":";
        auto res = solve(N, A, B, C);
        if(res.empty()) {
            cout << " IMPOSSIBLE\n";
            continue;
        }
        for(int i = 0; i < res.size(); ++i) {
            cout << " " << res[i];
        }
        cout << "\n";
    }
    return 0;
}

I forgot the time and was one hour and half late. Although some people can make it within that time, I failed.😭

Anyways, I’m making progress!

Leave a Reply

Your email address will not be published. Required fields are marked *