|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Sort 0 1 2\n", |
| 8 | + "\n", |
| 9 | + "You are given an integer array/list(ARR) of size N. It contains only 0s, 1s and 2s. Write a solution to sort this array/list in a 'single scan'.<br>\n", |
| 10 | + "<br>\n", |
| 11 | + "'Single Scan' refers to iterating over the array/list just once or to put it in other words, you will be visiting each element in the array/list just once.<br>\n", |
| 12 | + "<br>\n", |
| 13 | + "Note:You need to change in the given array/list itself. Hence, no need to return or print anything. <br>\n", |
| 14 | + "<br>\n", |
| 15 | + "Input format :<br>\n", |
| 16 | + "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.<br>\n", |
| 17 | + "<br>\n", |
| 18 | + "First line of each test case or query contains an integer 'N' representing the size of the array/list.<br>\n", |
| 19 | + "<br>\n", |
| 20 | + "Second line contains 'N' single space separated integers(all 0s, 1s and 2s) representing the elements in the array/list.<br>\n", |
| 21 | + "Output Format :<br>\n", |
| 22 | + "For each test case, print the sorted array/list elements in a row separated by a single space.<br>\n", |
| 23 | + "<br>\n", |
| 24 | + "Output for every test case will be printed in a separate line.<br>\n", |
| 25 | + "<br>\n", |
| 26 | + "Constraints :<br>\n", |
| 27 | + "1 <= t <= 10^2<br>\n", |
| 28 | + "0 <= N <= 10^5<br>\n", |
| 29 | + "Time Limit: 1 sec<br>\n", |
| 30 | + "<br>\n", |
| 31 | + "Sample Input 1:<br>\n", |
| 32 | + "1<br>\n", |
| 33 | + "7<br>\n", |
| 34 | + "0 1 2 0 2 0 1<br>\n", |
| 35 | + "Sample Output 1:<br>\n", |
| 36 | + "0 0 0 1 1 2 2 <br>\n", |
| 37 | + "Sample Input 2:<br>\n", |
| 38 | + "2<br>\n", |
| 39 | + "5<br>\n", |
| 40 | + "2 2 0 1 1<br>\n", |
| 41 | + "7<br>\n", |
| 42 | + "0 1 2 0 1 2 0<br>\n", |
| 43 | + "Sample Output 2:<br>\n", |
| 44 | + "0 1 1 2 2 <br>\n", |
| 45 | + "0 0 0 1 1 2 2" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "execution_count": 1, |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [ |
| 53 | + { |
| 54 | + "name": "stdout", |
| 55 | + "output_type": "stream", |
| 56 | + "text": [ |
| 57 | + "1\n", |
| 58 | + "11\n", |
| 59 | + "2 2 1 1 2 1 0 0 1 2 2 \n", |
| 60 | + "0 0 1 1 1 1 2 2 2 2 2\n" |
| 61 | + ] |
| 62 | + } |
| 63 | + ], |
| 64 | + "source": [ |
| 65 | + "#Remove All Coding Ninjas Code, Then Paste my Code\n", |
| 66 | + "loop=int(input())\n", |
| 67 | + "list=[]\n", |
| 68 | + "for i in range(loop):\n", |
| 69 | + " no_of_ele=int(input())\n", |
| 70 | + " if no_of_ele!=0:\n", |
| 71 | + " list=[int(x) for x in input().split()]\n", |
| 72 | + " list.sort()\n", |
| 73 | + " print(*list)" |
| 74 | + ] |
| 75 | + } |
| 76 | + ], |
| 77 | + "metadata": { |
| 78 | + "kernelspec": { |
| 79 | + "display_name": "Python 3", |
| 80 | + "language": "python", |
| 81 | + "name": "python3" |
| 82 | + }, |
| 83 | + "language_info": { |
| 84 | + "codemirror_mode": { |
| 85 | + "name": "ipython", |
| 86 | + "version": 3 |
| 87 | + }, |
| 88 | + "file_extension": ".py", |
| 89 | + "mimetype": "text/x-python", |
| 90 | + "name": "python", |
| 91 | + "nbconvert_exporter": "python", |
| 92 | + "pygments_lexer": "ipython3", |
| 93 | + "version": "3.8.6" |
| 94 | + } |
| 95 | + }, |
| 96 | + "nbformat": 4, |
| 97 | + "nbformat_minor": 4 |
| 98 | +} |
0 commit comments