-
Notifications
You must be signed in to change notification settings - Fork 2
/
move_zeros_to_end_of_arr.cpp
111 lines (88 loc) · 2.12 KB
/
move_zeros_to_end_of_arr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/****************************************************************************
File name: move_zeros_to_end_of_arr.cpp
Author: babajr
*****************************************************************************/
/*
Move zeros to the end of the array.
Inpuy: arr = {12, 10, 0, 1, 5, 0, 6}
Output: arr = {12, 10, 1, 5, 6, 0, 0}
*/
#include <bits/stdc++.h>
using namespace std;
/*
Helper API to swap the array elements.
*/
void swap(int *num1, int *num2)
{
int temp = *num1;
*num1 = *num2;
*num2 = temp;
}
/*
Brute Force Approach.
TC: O (n2)
--> traverse the array. if 0 is found, replace it with next non zero element.
--> 2 for loops are required. one for traversing the array and other
for finding out next non zero element.
*/
void move_zeros_bf(int arr[], int size)
{
// traverse the array and check for 0's.
for(int i = 0; i < size; i++)
{
if(arr[i] == 0)
{
// once zero is found, search for next non zero element.
// swap the 0 with next non zero element.
for(int j = i + 1; j < size; j++)
{
if(arr[j] != 0)
swap(&arr[i], &arr[j]);
}
}
}
}
/*
Efficient Approach.
TC = O(n)
--> while traversing the array, keep the count of non zero elements.
--> swap the zeros with next non zero elements.
i
10 12 1 5 0 0
in
*/
void move_zeros_eff(int arr[], int size)
{
int index = 0;
for(int i = 0; i < size; i++)
{
if(arr[i] != 0)
{
swap(&arr[i], &arr[index]);
index++;
}
}
}
/*
Helper API to print the elements of the array.
*/
void print_array(int arr[], int size)
{
for(int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main(void)
{
int arr[] = {12, 10, 0, 1, 5, 0, 6};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Original Array: \n");
print_array(arr, size);
// move_zeros_bf(arr, size);
move_zeros_eff(arr, size);
printf("Modified Array: \n");
print_array(arr, size);
return 0;
}