
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Move all zeros to start and ones to end in an Array of random integers in C++
In this tutorial, we are going to write a program that moves all zeroes to front and ones to end of the array.
Given an array with zeroes and ones along with random integers. We have to move all the zeroes to start and ones to the end of the array. Let's see an example.
Input
arr = [4, 5, 1, 1, 0, 0, 2, 0, 3, 1, 0, 1]
Output
0 0 0 0 4 5 2 3 1 1 1 1
Algorithm
Initialise the array.
Initialise an index to 1.
Iterate over the given array.
If the current element is not zero, then update the value at the index with the current element.
Increment the index.
Write a loop that iterates from the above index to n
Update all the elements to 1.
Similarly, do for 0. Instead of increasing the index, decrease it to move all zeroes to front of the array.
Implementation
Following is the implementation of the above algorithm in C++
#include <bits/stdc++.h> using namespace std; void update1And0Positions(int arr[], int n) { int index = 0; for (int i = 0; i < n; i++) { if (arr[i] != 1) { arr[index++] = arr[i]; } } while (index < n) { arr[index++] = 1; } index = 0; for (int i = n - 1; i >= 0; i--) { if (arr[i] == 1) { continue; } if (!index) { index = i; } if (arr[i] != 0) { arr[index--] = arr[i]; } } while (index >= 0) { arr[index--] = 0; } } int main() { int arr[] = { 4, 5, 1, 1, 0, 0, 2, 0, 3, 1, 0, 1 }; int n = 12; update1And0Positions(arr, n); for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; return 0; }
Output
If you run the above code, then you will get the following result.
0 0 0 0 4 5 2 3 1 1 1 1
- Related Articles
- Move All the Zeros to the End of Array in Java
- Move all zeroes to end of array in C++
- In-place Move Zeros to End of List in Python
- In-place Algorithm to Move Zeros to End of List in JavaScript
- How to move all the zeros to the end of the array from the given array of integer numbers using C#?
- Write an algorithm that takes an array and moves all of the zeros to the end JavaScript
- Move all zeroes to end of the array using List Comprehension in Python
- Create an array with ones above the main diagonal and zeros elsewhere in Numpy
- Create an array with ones below the main diagonal and zeros elsewhere in Numpy
- Move all zeros to the front of the linked list in C++
- C++ program to find minimum number of steps needed to move from start to end
- Create an array with ones at and below the given diagonal and zeros elsewhere in Numpy
- Minimum move to end operations to make all strings equal in C++
- Take an array of integers and create an array of all the possible permutations in JavaScript
- Generate a random array of integers in Java
