Array Manipulation Techniques in C++

  1. Binary Search Implementation

  2. Element Removal Optimization

  3. Sorted Squares Generation

  4. Spiral Matrix Construction

  5. Binary Search Implementation


Binary seearch implementation requires careful consideration of boundary conditions:

  • Loop condition: left < right vs left <= right
  • Right boundary update: right = middle vs right = middle - 1

The choice depends on whether we maintain a closed interval [left, right] or half-open interval [left, right). Closed intervals require inclusive comparisons while half-open intervals use exclusive comparisons.

Closed Interval Implementation


class Solution {
public:
    int findTarget(vector<int>& arr, int target) {
        int left = 0, right = arr.size() - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (arr[mid] == target) return mid;
            else if (arr[mid] < target) left = mid + 1;
            else right = mid - 1;
        }
        return -1;
    }
};
</int>

Half-Open Interval Implementation


class Solution {
public:
    int findTarget(vector<int>& arr, int target) {
        int left = 0, right = arr.size();
        while (left < right) {
            int mid = (left + right) / 2;
            if (arr[mid] == target) return mid;
            else if (arr[mid] < target) left = mid + 1;
            else right = mid;
        }
        return -1;
    }
};
</int>
  1. Element Removal Optimization

While standard library functions can solve this problem, implementing a two-pointer approach achieves O(N) time complexity:


class Solution {
public:
    int removeValue(vector<int>& arr, int val) {
        int slow = 0, fast = 0;
        while (fast < arr.size()) {
            if (arr[fast] != val) {
                arr[slow++] = arr[fast++];
            } else {
                fast++;
            }
        }
        return slow;
    }
};
</int>
  1. Sorted Squares Generation

Leverage the property that largest values exist at array ends:


class Solution {
public:
    vector<int> getSortedSquares(vector<int>& arr) {
        int n = arr.size();
        for (int i = 0; i < n; i++) arr[i] *= arr[i];
        
        vector<int> result(n);
        int left = 0, right = n - 1, index = n - 1;
        
        while (left <= right) {
            if (arr[left] <= arr[right]) {
                result[index--] = arr[right--];
            } else {
                result[index--] = arr[left++];
            }
        }
        return result;
    }
};
</int></int></int>
  1. Spiral Matrix Construction

Implement spiral filling with loop invariants:


class Solution {
public:
    vector<vector>> createSpiral(int size) {
        vector<vector>> matrix(size, vector<int>(size, 0));
        int loops = size / 2;
        int row_start = 0, col_start = 0;
        int offset = 1;
        int value = 1;
        
        while (loops--) {
            for (int col = col_start; col < size - offset; col++) {
                matrix[row_start][col] = value++;
            }
            for (int row = row_start; row < size - offset; row++) {
                matrix[row][size - offset] = value++;
            }
            for (int col = size - offset; col > col_start; col--) {
                matrix[size - offset][col] = value++;
            }
            for (int row = size - offset; row > row_start; row--) {
                matrix[row][col_start] = value++;
            }
            row_start++;
            col_start++;
            offset++;
        }
        
        if (size % 2 == 1) {
            matrix[size/2][size/2] = value;
        }
        return matrix;
    }
};
</int></vector></vector>

Tags: C++ binary-search two-pointers array-algorithms matrix-operations

Posted on Sat, 16 May 2026 15:05:13 +0000 by hiprakhar