1. Linear Scan (Brute Force)
Intuition
Traverse the entire array and keep track of the minimum value.
Algorithm
- 1. Set minVal = nums[0].
- 2. For each element x in nums: - minVal = min(minVal, x).
- 3. Return minVal.
Brute Force Linear Scan Minimum Search
2 of 2 questions left todayPhase 1 — minVal = 2 Begin linear scan: Step 1: Initialize minVal with the first element: minVal = 2
Phase 1 — minVal = 2 Begin linear scan: Step 1: Initialize minVal with the first element: minVal = 2.
#include <iostream>#include <vector>#include <algorithm>using namespace std; // Brute Force Approach: Linear Scanint findMin(const vector<int>& nums) { int minVal = nums[0]; for (int x : nums) { minVal = min(minVal, x); } return minVal;} int main() { vector<int> nums = {2, 2, 2, 0, 1}; int result = findMin(nums); cout << "Output: " << result << endl; return 0;}Time & Space Complexity
- Time complexity:
O(N)— Single linear pass visiting all N elements. - Space complexity:
O(1)— Only constant extra variables are used.
Why Brute Force Doesn't Scale:
For N = 105, quadratic O(N2) approaches require ~1010 operations, exceeding the standard 1-second CPU budget.
The optimal approach processes N elements in a single or logarithmic pass, completing in under 10ms with minimal memory footprint.