Introduction to Arrays
1D Arrays
An array is a collection of elements of the same type placed in contiguous memory locations. It's used to store multiple values in a single variable, making it easier to manage and manipulate data sets.
// Declaration and Initialization
int arr[5] = {1, 2, 3, 4, 5};
// Accessing elements (0-indexed)
cout << arr[0] << endl; // Prints 1
cout << arr[4] << endl; // Prints 5
// Iterating over an array
for(int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}2D Arrays (Matrices)
A 2D array is essentially an array of arrays. It is useful for representing grids, matrices, or any tabular data.
// Declaration of a 3x4 matrix
int grid[3][4];
// Initialization
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
// Accessing elements
cout << matrix[1][2] << endl; // Prints 6 (row 1, column 2)Ad-Hoc Problems
What are Ad-Hoc problems?
Ad-hoc problems do not fall into standard algorithmic categories (like Graph Theory or Dynamic Programming). They typically require careful reading, logical deduction, and translating the problem description directly into code.
Tips for Solving Ad-Hoc
- Read Carefully: Don't miss any details or constraints. Often the solution is literally written in the problem statement.
- Simulate: Try to simulate the process described in the problem using pen and paper on the given test cases.
- Edge Cases: Always think about the smallest and largest possible inputs. What if N=1 or N=100000?
- Data Types: Check if intermediate calculations could exceed the limits of standard integers (e.g., use
long long).