From e7ddb349ad05b0e675823420809ef2c1b8ec5dae Mon Sep 17 00:00:00 2001 From: mitulvaniya14 Date: Sun, 19 Oct 2025 17:43:23 +0530 Subject: [PATCH 1/2] Add solution for LeetCode 283: Move Zeroes Implement the solution for LeetCode problem 283, which moves all zeroes in the array to the end while maintaining the order of non-zero elements. --- leetcode_283MoveZeroes.cpp | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 leetcode_283MoveZeroes.cpp diff --git a/leetcode_283MoveZeroes.cpp b/leetcode_283MoveZeroes.cpp new file mode 100644 index 0000000..0e70881 --- /dev/null +++ b/leetcode_283MoveZeroes.cpp @@ -0,0 +1,54 @@ +// LeetCode 283: Move Zeroes +// Solution using Two Pointers (in-place) +// Time Complexity: O(n) + +#include +#include + +using namespace std; + +class Solution { +public: + void moveZeroes(vector& nums) { + // 'insertPos' is the index where the next non-zero element should be placed + int insertPos = 0; + + // First pass: move all non-zero elements to the front + for (int i = 0; i < nums.size(); ++i) { + if (nums[i] != 0) { + nums[insertPos] = nums[i]; + insertPos++; + } + } + + // Second pass: fill the rest of the array with zeroes + for (int i = insertPos; i < nums.size(); ++i) { + nums[i] = 0; + } + } +}; + +// Helper function to print a vector +void printVector(const vector& nums) { + cout << "[ "; + for (int num : nums) { + cout << num << " "; + } + cout << "]" << endl; +} + +// Main function to test the code +int main() { + Solution sol; + vector nums = {0, 1, 0, 3, 12}; + + cout << "Original array: "; + printVector(nums); + + sol.moveZeroes(nums); + + cout << "Array after moving zeroes: "; + printVector(nums); // Should be [ 1 3 12 0 0 ] + + return 0; +} From 39f06f4a711833ab5558e91edbfe365feaa7fdbc Mon Sep 17 00:00:00 2001 From: mitulvaniya14 Date: Sun, 19 Oct 2025 17:53:09 +0530 Subject: [PATCH 2/2] Refactor moveZeroes to use size_t and add tests Updated indexing to use size_t for better compatibility with vector sizes and added edge case tests for the moveZeroes function. --- leetcode_283MoveZeroes.cpp | 45 ++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/leetcode_283MoveZeroes.cpp b/leetcode_283MoveZeroes.cpp index 0e70881..7b2a411 100644 --- a/leetcode_283MoveZeroes.cpp +++ b/leetcode_283MoveZeroes.cpp @@ -11,10 +11,12 @@ class Solution { public: void moveZeroes(vector& nums) { // 'insertPos' is the index where the next non-zero element should be placed - int insertPos = 0; + // Use size_t for indexing + size_t insertPos = 0; // First pass: move all non-zero elements to the front - for (int i = 0; i < nums.size(); ++i) { + // Use size_t for index 'i' to match nums.size() + for (size_t i = 0; i < nums.size(); ++i) { if (nums[i] != 0) { nums[insertPos] = nums[i]; insertPos++; @@ -22,7 +24,8 @@ class Solution { } // Second pass: fill the rest of the array with zeroes - for (int i = insertPos; i < nums.size(); ++i) { + // Use size_t for index 'i' + for (size_t i = insertPos; i < nums.size(); ++i) { nums[i] = 0; } } @@ -31,24 +34,38 @@ class Solution { // Helper function to print a vector void printVector(const vector& nums) { cout << "[ "; + // Range-based for loop is fine here for (int num : nums) { cout << num << " "; } cout << "]" << endl; } -// Main function to test the code +// Main function to test the code with edge cases int main() { Solution sol; - vector nums = {0, 1, 0, 3, 12}; - cout << "Original array: "; - printVector(nums); - - sol.moveZeroes(nums); - - cout << "Array after moving zeroes: "; - printVector(nums); // Should be [ 1 3 12 0 0 ] + // Test 1: Standard case + vector nums1 = {0, 1, 0, 3, 12}; + cout << "Test 1 (Standard):" << endl; + cout << " Original: "; printVector(nums1); + sol.moveZeroes(nums1); + cout << " Moved: "; printVector(nums1); // Expected: [ 1 3 12 0 0 ] - return 0; -} + // Test 2: Empty array + vector nums2 = {}; + cout << "\nTest 2 (Empty Array):" << endl; + cout << " Original: "; printVector(nums2); + sol.moveZeroes(nums2); + cout << " Moved: "; printVector(nums2); // Expected: [ ] + + // Test 3: All zeros + vector nums3 = {0, 0, 0, 0}; + cout << "\nTest 3 (All Zeros):" << endl; + cout << " Original: "; printVector(nums3); + sol.moveZeroes(nums3); + cout << " Moved: "; printVector(nums3); // Expected: [ 0 0 0 0 ] + + // Test 4: All non-zeros + vector nums4 = {1, 2, 3, 4, 5}; + cout << "\nTest 4 (All