Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Move all zeroes to end of array #5133

Open
harshraj8843 opened this issue Jan 10, 2024 · 2 comments
Open

Move all zeroes to end of array #5133

harshraj8843 opened this issue Jan 10, 2024 · 2 comments
Labels
auto-track Good First Issue Tracker program

Comments

@harshraj8843
Copy link
Contributor

harshraj8843 commented Jan 10, 2024

Description

Write a program to move all zeroes to end of array

Input  : arr[] = {1, 2, 0, 4, 3, 0, 5, 0};
Output : arr[] = {1, 2, 4, 3, 5, 0, 0, 0};

Input  : arr[] = {1, 2, 0, 0, 0, 3, 6};
Output : arr[] = {1, 2, 3, 6, 0, 0, 0};
### Tracking Issues
- [ ] #5135
- [ ] #5136
- [ ] #5137
- [ ] #5138
- [ ] #5139
- [ ] #5140
- [ ] #5141
- [ ] #5142
- [ ] #5143
- [ ] #5144
- [ ] #5145
- [ ] #5146
- [ ] #5147
- [ ] #5148
- [ ] #5149
- [ ] #5150
- [ ] #5151
- [ ] #5152
- [ ] #5153
- [ ] #5154
@harshraj8843 harshraj8843 added the auto-track Good First Issue Tracker label Jan 10, 2024
@codinasion-bot
Copy link

👋🏻 Hey @harshraj8843

💖 Thanks for opening this issue 💖

A team member should be by to give feedback soon.

@Ruttviii
Copy link

#include
#include

void moveZeroesToEnd(std::vector& nums) {
int nonZeroIndex = 0;

// Iterate through the vector and move non-zero elements to the beginning
for (int num : nums) {
    if (num != 0) {
        nums[nonZeroIndex++] = num;
    }
}

// Fill the remaining elements with zeroes
while (nonZeroIndex < nums.size()) {
    nums[nonZeroIndex++] = 0;
}

}

int main() {
std::vector nums = {0, 2, 0, 4, 0, 6, 8, 0, 10};

moveZeroesToEnd(nums);

std::cout << "Array after moving zeroes to the end:" << std::endl;
for (int num : nums) {
    std::cout << num << " ";
}
std::cout << std::endl;

    return 0;
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
auto-track Good First Issue Tracker program
Projects
None yet
Development

No branches or pull requests

2 participants