From f4e522e38fcb85decd61052ab0edb4a04f0450d0 Mon Sep 17 00:00:00 2001 From: Aniket Artani Date: Fri, 1 Oct 2021 12:47:31 +0530 Subject: [PATCH 1/7] Create practise-question.md --- lessons/practise-question.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 lessons/practise-question.md diff --git a/lessons/practise-question.md b/lessons/practise-question.md new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/lessons/practise-question.md @@ -0,0 +1 @@ + From 270a22363f1983522b3d8d8f37f0fc44adcc23ff Mon Sep 17 00:00:00 2001 From: Aniket Artani Date: Fri, 1 Oct 2021 13:04:48 +0530 Subject: [PATCH 2/7] Updated Practise-questions.md --- lessons/practise-question.md | 52 ++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/lessons/practise-question.md b/lessons/practise-question.md index 8b137891..0a2da88e 100644 --- a/lessons/practise-question.md +++ b/lessons/practise-question.md @@ -1 +1,53 @@ +### Q1) Move All Zeros to End of an Array +You have been given a random integer array of size N. You have been required to push all the zeros that are present in the array to the end of it such that the relative order of the non-zero elements should be maintained. +```java +Sample Input/Output +​Input: arr[]={3,0,1,5,0,5} +Output: arr[]={3,1,5,5,0,0} +``` + ## Approach + Before we discuss the approach for this question let’s see what exactly the question requires us to do. It seems that we have to push all the 0s in the array towards the end of the array. It can also be looked at as pushing all the non-zero elements in the array towards the beginning of the array.
+First, traverse the whole array and initialize a variable count whose value should be equal to zero. Now, check each element of the array whether it is equal to zero or not. If the element is not equal to zero,put that element at the count position of the array(arr[count]) and increment the value of count by one.
+Now, after traversing the whole array fill the remaining positions of the array with zeroes. +## Program +```java +package main; +import java.util.*; +public class main { + public static void pushZerosToEnd(int[] arr, int n) { + int count=0; + for(int i=0;i +Make a function named pushZeroToEnd and input an array.
+Traverse through the array and check each element whether it is equal to zero or not.
+If the element is not equal to zero, put the element at the countth position of the array.
+Increase the value of count by 1.
+At last, fill the remaining positions of the array with 0.
+ +## Time Complexity +Since we will be traversing the whole array twice therefore the time complexity of algorithm would be Time complexity of first loop O(N)+ Time complexity of second loop O(N)=O(N),where n is number of elements in input array. From baa9b5b713b69e492f0f5ac7029d54250a7dcc79 Mon Sep 17 00:00:00 2001 From: Aniket Artani Date: Fri, 1 Oct 2021 14:01:31 +0530 Subject: [PATCH 3/7] Update lessons/practise-question.md Co-authored-by: Utkarsh Mishra <76392681+Utkarsh1504@users.noreply.github.com> --- lessons/practise-question.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lessons/practise-question.md b/lessons/practise-question.md index 0a2da88e..b3036875 100644 --- a/lessons/practise-question.md +++ b/lessons/practise-question.md @@ -13,7 +13,7 @@ Now, after traversing the whole array fill the remaining positions of the array ```java package main; import java.util.*; -public class main { +public class Main { public static void pushZerosToEnd(int[] arr, int n) { int count=0; for(int i=0;i Date: Fri, 1 Oct 2021 14:02:13 +0530 Subject: [PATCH 4/7] Update lessons/practise-question.md Co-authored-by: Utkarsh Mishra <76392681+Utkarsh1504@users.noreply.github.com> --- lessons/practise-question.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lessons/practise-question.md b/lessons/practise-question.md index b3036875..3d242566 100644 --- a/lessons/practise-question.md +++ b/lessons/practise-question.md @@ -1,4 +1,10 @@ -### Q1) Move All Zeros to End of an Array +--- +path: "/practise-questions" +title: "Practise Questions" +order: "5F" +section: "Searching & Sorting" +description: "learn Searching & sorting algorithms" +--- You have been given a random integer array of size N. You have been required to push all the zeros that are present in the array to the end of it such that the relative order of the non-zero elements should be maintained. ```java Sample Input/Output From 1c197fdf1192f91f9fe0e29d4527b4a4bbe551fd Mon Sep 17 00:00:00 2001 From: Aniket Artani Date: Fri, 1 Oct 2021 14:11:02 +0530 Subject: [PATCH 5/7] Update lessons/practise-question.md Co-authored-by: Utkarsh Mishra <76392681+Utkarsh1504@users.noreply.github.com> --- lessons/practise-question.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lessons/practise-question.md b/lessons/practise-question.md index 3d242566..32600a4a 100644 --- a/lessons/practise-question.md +++ b/lessons/practise-question.md @@ -55,5 +55,7 @@ Increase the value of count by 1.
At last, fill the remaining positions of the array with 0.
## Time Complexity -Since we will be traversing the whole array twice therefore the time complexity of algorithm would be Time complexity of first loop O(N)+ Time complexity of second loop O(N)=O(N),where n is number of elements in input array. +Since we will be traversing the whole array twice therefore the time complexity of algorithm would be: +- **Time complexity of first loop O(N) + Time complexity of second loop O(N) = O(N)**, +where n is number of elements in input array. From bbfda31305db2fe30b4c34396b1fad32494c966a Mon Sep 17 00:00:00 2001 From: Aniket Artani Date: Fri, 1 Oct 2021 14:11:08 +0530 Subject: [PATCH 6/7] Update lessons/practise-question.md Co-authored-by: Utkarsh Mishra <76392681+Utkarsh1504@users.noreply.github.com> --- lessons/practise-question.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lessons/practise-question.md b/lessons/practise-question.md index 32600a4a..6344c1ff 100644 --- a/lessons/practise-question.md +++ b/lessons/practise-question.md @@ -47,12 +47,12 @@ Output 3542200 ``` ## Explanation -Initialize a variable count with a value equal to zero.
-Make a function named pushZeroToEnd and input an array.
-Traverse through the array and check each element whether it is equal to zero or not.
-If the element is not equal to zero, put the element at the countth position of the array.
-Increase the value of count by 1.
-At last, fill the remaining positions of the array with 0.
+- Initialize a variable count with a value equal to zero.
+- Make a function named pushZeroToEnd and input an array.
+- Traverse through the array and check each element whether it is equal to zero or not.
+- If the element is not equal to zero, put the element at the countth position of the array.
+- Increase the value of count by 1.
+- At last, fill the remaining positions of the array with 0.
## Time Complexity Since we will be traversing the whole array twice therefore the time complexity of algorithm would be: From dbdb489bc90f82c1f63a0263f9eaa1df78c9e567 Mon Sep 17 00:00:00 2001 From: Aniket Artani Date: Fri, 1 Oct 2021 14:35:44 +0530 Subject: [PATCH 7/7] Update practise-question.md --- lessons/practise-question.md | 1 + 1 file changed, 1 insertion(+) diff --git a/lessons/practise-question.md b/lessons/practise-question.md index 6344c1ff..3b0c80ac 100644 --- a/lessons/practise-question.md +++ b/lessons/practise-question.md @@ -5,6 +5,7 @@ order: "5F" section: "Searching & Sorting" description: "learn Searching & sorting algorithms" --- +**Q1) Move All Zeros to End of an Array**
You have been given a random integer array of size N. You have been required to push all the zeros that are present in the array to the end of it such that the relative order of the non-zero elements should be maintained. ```java Sample Input/Output