C/C++ Competitive Programming

Tech Stack: Visual Studio, C, C++, C#, CodeForces

Profile Link

Why Am I Doing This?

I found Competitive Programming a couple years ago with the video on the right of this paragraph. I immediately took and interest in it. I have since been doing problems every once in a while. But there have been periods in the past that I have just been solving problems all week. I like Competitive Programming because I learn a bunch of new stuff about algorithms and the best way to implement them.

What I‘ve learned so far

I‘ve learned a lot about the best way to implement algorithms using C/C++. I‘ve learned to use the language C++ a lot better. I‘ve learned the best way to think about problems and how to best understand and solve them.

Code Highlights

#include <iostream> #include <vector> int main() { int amount{ 0 }; std::cin >> amount; std::vector<int> even; std::vector<int> odd; int index{1}; for (size_t i = 0; i < amount; i++) { int num; std::cin >> num; if (num % 2 == 0) { even.push_back(index);//adds the index of the even number } else { odd.push_back(index); } index++; } int result = even.size() == 1 ? even[0] : odd[0];//checks if one of the vectors is only 1 long, if so it logs that vector(evne) else it logs the other one (odd) std::cout << result; }

Explanation:
In this problem you had to help Bob pass the IQ test. Bob was given an array of numbers and you had to return the index of which number is different from the others (either even or uneven). In this instance I make use of vectors so I can dynamically add numbers, I chose to use a vector because as stated in the problem you can‘t get more than 100 numbers per test case. So it won‘t have a noticeable difference in performance. In the code the for-loop goes through each number to check if it‘s even or uneven and adding each number depending on what the number is. And at the end we check the length of the even vector to check if it‘s length is equal to one. Depending on what number differs, we choose the smallest vector. And we return the index of the number.
Link to problem


#include <iostream> using namespace std; int t; int n[3]; int main() { cin >> t; while (t--) { int x, y, z; cin >> x >> y >> z; n[0] += x; n[1] += y; n[2] += z; } if (n[0] == 0 && n[1] == 0 && n[2] == 0) { cout << "YES" << endl; return 0; } else { cout << "NO" << endl; } }

Explanation:
In this problem you were given a 2D array of vectors(forces) where you had to check if the body on which these vectors were exercised is in equilibrium or not. In the code above, you can see I made an array with the length 3 and I counted up the x, y and z forces until all the vectors were counted. After that the code checks if all the forces equal 0(equilibrium) and prints "YES" or "NO" in the console.
Link to problem

Reflection On My Progress

I have really enjoyed my time with Competitive Programming and I don‘t think I‘ll stop doing this as a hobby. Some problems are really challenging and complex to implement and I love the challenge. I started in C++ not knowing how to setup a class correctly, now I am very comfortable with working in C/C++.