
Asked in Deloitte
Sort 0 and 1 Problem Statement
Given an integer array ARR
of size N
containing only integers 0 and 1, implement a function to sort this array. The solution should scan the array only once without using any additional arrays.
Input:
The first line contains an integer 't', denoting the number of test cases or queries to be run.
The next t
lines contain:
- An integer 'N' representing the size of the array/list in each test case.
- 'N' single space-separated integers (all 0s and 1s) representing the elements in the array.
Output:
For each test case, print the sorted array elements in one row, separated by a single space.
Output for every test case will be printed on a separate line.
Example:
Input:
2
5
0 1 1 0 1
3
1 0 0
Output:
0 0 1 1 1
0 0 1
Constraints:
1 <= t <= 10^2
0 <= N <= 10^5
- Time Limit: 1 sec
Note:
You need to modify the given array/list itself. No need to return or print anything within the function.

AnswerBot
1y
The function sorts an integer array containing only 0s and 1s in linear time complexity.
Use two pointers, one starting from the beginning and the other from the end of the array.
Swap the elements at t...read more
Himanshu Gupta
18d
public static int[] sortArray(int[] array) { for (int i = 0, j = array.length - 1 ; i < j ;) { if (array[i] == 1 && array[j] == 0) { int temp = array[i]; array[i] = array[j]; array[j] = temp; i++; j--...read more
Anonymous
1mo
public static int[] sort(int[] arr){ int j=0; for(int i=0; i<arr.length; i++){ if(arr[i]==0){ swap(arr, i, j); j++; } System.out.println(Arrays.toString(arr)); } return arr; } private static void swap...read more
Sashi Kumar
1mo
def sort_zeros_and_ones(arr): """ Sorts an array containing only 0s and 1s in-place. Args: arr: The input array of 0s and 1s. Returns: None. The array is modified in-place. """ count_zeros = arr.count...read more
Ajay kumar
3mo
package Arrays; public class Sort_0_and_1 { public static void sort(int[] nums){ int i =0,j=0; while(j<nums.length){ if(nums[j]==0){ int temp = nums[j]; nums[j]=nums[i]; nums[i]=temp; i++; } j++; }...read more
Add answer anonymously...
Top Java Developer Interview Questions Asked at Deloitte
Q. What is a Spring profile?
Q. What is the difference between an abstract class and an interface?
Q. What is a functional interface in programming?
Interview Questions Asked to Java Developer at Other Companies
Top Skill-Based Questions for Deloitte Java Developer
Java Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
Web Development Interview Questions and Answers
250 Questions
Algorithms Interview Questions and Answers
250 Questions
Software Development Interview Questions and Answers
250 Questions
Spring Boot Interview Questions and Answers
50 Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

