Hello World
#include <iostream>
int main()
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
Compile and run:
g++ main.cpp -o main
./main
This is the smallest C++ program shape: main starts the program, and std::cout prints to the terminal.
Variables
#include <string>
std::string name = "Dan";
int count = 1;
bool active = true;
auto language = "C++";
This shows a few common C++ value types and auto for type inference when the initializer already makes the type obvious.
Functions
#include <string>
std::string greet(const std::string& name)
{
return "Hello, " + name + "!";
}
This defines a function that takes a name and returns a greeting string. It is the usual pattern for reusable logic in C++.
C++ Add
int add(int param1, int param2)
{
return param1 + param2;
}
This just adds the two input numbers with the language’s normal arithmetic and returns the sum.
C++ Add Border
#include <string>
#include <vector>
std::vector<std::string> addBorder(std::vector<std::string> picture)
{
picture.insert(picture.begin(), std::string(picture[0].size(), '*'));
for (auto& row : picture) {
row = "*" + row + "*";
}
picture.push_back(std::string(picture[0].size(), '*'));
return picture;
}
This builds a new grid with a * border around every side. It adds a full top and bottom row, then wraps each existing row from left and right.
C++ Adjacent Elements Product
#include <algorithm>
#include <limits>
#include <vector>
long long adjacentElementsProduct(const std::vector<int>& inputArray)
{
long long max = std::numeric_limits<long long>::min();
for (std::size_t i = 0; i + 1 < inputArray.size(); ++i) {
long long product = static_cast<long long>(inputArray[i]) * inputArray[i + 1];
max = std::max(max, product);
}
return max;
}
This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.
C++ Almost Magic Square
#include <algorithm>
#include <array>
#include <vector>
std::vector<int> almostMagicSquare(const std::vector<int>& a)
{
std::array<std::array<int, 3>, 3> grid{};
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
grid[i][j] = a[i * 3 + j];
}
}
std::array<int, 3> rowSum{};
std::array<int, 3> colSum{};
int maxSum = 0;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
rowSum[i] += grid[i][j];
colSum[i] += grid[j][i];
}
}
for (int k = 0; k < 3; ++k) {
maxSum = std::max(maxSum, rowSum[k]);
maxSum = std::max(maxSum, colSum[k]);
}
for (int i = 0, j = 0; i < 3 && j < 3;) {
int diff = std::min(maxSum - rowSum[i], maxSum - colSum[j]);
grid[i][j] += diff;
rowSum[i] += diff;
colSum[j] += diff;
if (rowSum[i] == maxSum) {
++i;
}
if (colSum[j] == maxSum) {
++j;
}
}
std::vector<int> result;
result.reserve(9);
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
result.push_back(grid[i][j]);
}
}
return result;
}
This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.
C++ Are Equally Strong
#include <algorithm>
bool areEquallyStrong(int yourLeft, int yourRight, int friendsLeft, int friendsRight)
{
return std::max(yourRight, yourLeft) == std::max(friendsLeft, friendsRight)
&& std::min(yourLeft, yourRight) == std::min(friendsRight, friendsLeft);
}
This compares each person’s strongest and weakest arm. If both pairs match, the result is true.
C++ Array Change
#include <vector>
long long arrayChange(std::vector<long long> a)
{
long long min = 0;
for (std::size_t k = 0; k + 1 < a.size(); ++k) {
if (a[k] >= a[k + 1]) {
long long dif = a[k] - a[k + 1] + 1;
a[k + 1] += dif;
min += dif;
}
}
return min;
}
This moves left to right and bumps values only when needed so the array becomes strictly increasing.
C++ Array Maximal Adjacement Difference
#include <algorithm>
#include <cstdlib>
#include <vector>
long long arrayMaximalAdjacentDifference(const std::vector<int>& a)
{
long long dif = 0;
for (std::size_t i = 1; i + 1 < a.size(); ++i) {
long long left = std::llabs(static_cast<long long>(a[i]) - a[i - 1]);
long long right = std::llabs(static_cast<long long>(a[i]) - a[i + 1]);
dif = std::max({dif, left, right});
}
return dif;
}
This checks the gap between each pair of neighbors and returns the largest difference.
C++ Binary Gap
#include <algorithm>
int binaryGap(int n)
{
int gap = 0;
int run = -1; // -1 means no leading '1' has been seen yet
while (n > 0) {
if (n & 1) {
if (run >= 0) {
gap = std::max(gap, run);
}
run = 0;
} else if (run >= 0) {
++run;
}
n >>= 1;
}
return gap;
}
This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.
C++ Bracket
#include <string>
#include <vector>
int bracket(const std::string& s)
{
std::vector<char> stack;
for (char c : s) {
switch (c) {
case ')':
if (stack.empty() || stack.back() != '(') {
return 0;
}
stack.pop_back();
break;
case ']':
if (stack.empty() || stack.back() != '[') {
return 0;
}
stack.pop_back();
break;
case '}':
if (stack.empty() || stack.back() != '{') {
return 0;
}
stack.pop_back();
break;
default:
stack.push_back(c);
break;
}
}
return stack.empty() ? 1 : 0;
}
This uses a simple stack approach: open brackets go in, matching closing brackets pop them out.