|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 10, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "name": "stdout", |
| 10 | + "output_type": "stream", |
| 11 | + "text": [ |
| 12 | + "2\n", |
| 13 | + "5\n", |
| 14 | + "4 2 1 5 3\n", |
| 15 | + "2 1 -1 3 -1\n", |
| 16 | + "6\n", |
| 17 | + "5 6 2 3 1 7\n", |
| 18 | + "-1 2 -1 1 -1 -1\n" |
| 19 | + ] |
| 20 | + } |
| 21 | + ], |
| 22 | + "source": [ |
| 23 | + "#immediate smaller element -----> AMAZON\n", |
| 24 | + "#marks = 1, difficulty = Basic\n", |
| 25 | + "'''\n", |
| 26 | + "Given an integer array of size N. For each element in the array, check whether the right adjacent element (on the next immediate position) of the array is smaller. If next element is smaller, print that element. If not, then print -1.\n", |
| 27 | + "\n", |
| 28 | + "Input:\n", |
| 29 | + "The first line of input contains an integer T denoting the number of test cases. T testcases follow. Each testcase contains 2 lines of input:\n", |
| 30 | + "The first line contains an integer N, where N is the size of array.\n", |
| 31 | + "The second line contains N integers(elements of the array) sperated with spaces.\n", |
| 32 | + "\n", |
| 33 | + "Output:\n", |
| 34 | + "For each test case, print the next immediate smaller elements for each element in the array.\n", |
| 35 | + "\n", |
| 36 | + "Constraints:\n", |
| 37 | + "1 ≤ T ≤ 200\n", |
| 38 | + "1 ≤ N ≤ 107\n", |
| 39 | + "1 ≤ arr[i] ≤ 1000\n", |
| 40 | + "\n", |
| 41 | + "Example:\n", |
| 42 | + "Input\n", |
| 43 | + "2\n", |
| 44 | + "5\n", |
| 45 | + "4 2 1 5 3\n", |
| 46 | + "6\n", |
| 47 | + "5 6 2 3 1 7\n", |
| 48 | + "\n", |
| 49 | + "Output\n", |
| 50 | + "2 1 -1 3 -1\n", |
| 51 | + "-1 2 -1 1 -1 -1\n", |
| 52 | + "\n", |
| 53 | + "Explanation:\n", |
| 54 | + "Testcase 1: Array elements are 4, 2, 1, 5, 3. Next to 4 is 2 which is smaller, so we print 2. Next of 2 is 1 which is smaller, so we print 1. Next of 1 is 5 which is greater, so we print -1. Next of 5 is 3 which is smaller so we print 3. Note that for last element, output is always going to be -1 because there is no element on right.\n", |
| 55 | + "'''\n", |
| 56 | + "n=int(input())\n", |
| 57 | + "for i in range(n):\n", |
| 58 | + " m=int(input())\n", |
| 59 | + " a=list(map(int,input().split()))\n", |
| 60 | + " for j in range(m-1):\n", |
| 61 | + " if j<m-1:\n", |
| 62 | + " if a[j]>a[j+1]:\n", |
| 63 | + " print(a[j+1],end=' ')\n", |
| 64 | + " else:\n", |
| 65 | + " print(-1,end=' ')\n", |
| 66 | + " else:\n", |
| 67 | + " print(-1)" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "code", |
| 72 | + "execution_count": 2, |
| 73 | + "metadata": {}, |
| 74 | + "outputs": [ |
| 75 | + { |
| 76 | + "name": "stdout", |
| 77 | + "output_type": "stream", |
| 78 | + "text": [ |
| 79 | + "2\n", |
| 80 | + "5\n", |
| 81 | + "1 2 3 4 5\n", |
| 82 | + "2\n", |
| 83 | + "3 4 5 1 2 \n", |
| 84 | + "1\n", |
| 85 | + "5\n", |
| 86 | + "1\n", |
| 87 | + "5 \n" |
| 88 | + ] |
| 89 | + } |
| 90 | + ], |
| 91 | + "source": [ |
| 92 | + "#rotating an array ---> Codenation\n", |
| 93 | + "#marks = 1, difficulty = Basic\n", |
| 94 | + "'''\n", |
| 95 | + "Given an array of N size. The task is to rotate array by d elements where d is less than or equal to N.\n", |
| 96 | + "\n", |
| 97 | + "Input:\n", |
| 98 | + "The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of three lines. The first line of each test case consists of an integer N, where N is the size of array.\n", |
| 99 | + "The second line of each test case contains N space separated integers denoting array elements. The third line of each test case contains \"d\" .\n", |
| 100 | + "\n", |
| 101 | + "Output:\n", |
| 102 | + "Corresponding to each test case, in a new line, print the modified array.\n", |
| 103 | + "\n", |
| 104 | + "Constraints:\n", |
| 105 | + "1 ≤ T ≤ 200\n", |
| 106 | + "1 ≤ N ≤ 200\n", |
| 107 | + "1 ≤ A[i] ≤ 1000\n", |
| 108 | + "\n", |
| 109 | + "Example:\n", |
| 110 | + "Input\n", |
| 111 | + "1\n", |
| 112 | + "5\n", |
| 113 | + "1 2 3 4 5\n", |
| 114 | + "2\n", |
| 115 | + "\n", |
| 116 | + "Output\n", |
| 117 | + "3 4 5 1 2\n", |
| 118 | + "'''\n", |
| 119 | + "for _ in range(int(input())):\n", |
| 120 | + " m=int(input())\n", |
| 121 | + " a=list(map(int,input().split()))\n", |
| 122 | + " d=int(input())\n", |
| 123 | + " for j in range(m-d):\n", |
| 124 | + " print(a[j+d],end=' ')\n", |
| 125 | + " for j in range(d):\n", |
| 126 | + " print(a[j],end=' ')\n", |
| 127 | + " print()" |
| 128 | + ] |
| 129 | + }, |
| 130 | + { |
| 131 | + "cell_type": "code", |
| 132 | + "execution_count": 1, |
| 133 | + "metadata": {}, |
| 134 | + "outputs": [ |
| 135 | + { |
| 136 | + "name": "stdout", |
| 137 | + "output_type": "stream", |
| 138 | + "text": [ |
| 139 | + "2\n", |
| 140 | + "5 2\n", |
| 141 | + "1 2 3 4 5\n", |
| 142 | + "3 4 5 1 2 \n", |
| 143 | + "10 3\n", |
| 144 | + "2 4 6 8 10 12 14 16 18 20\n", |
| 145 | + "8 10 12 14 16 18 20 2 4 6 \n" |
| 146 | + ] |
| 147 | + } |
| 148 | + ], |
| 149 | + "source": [ |
| 150 | + "# Rotate Array ---> Amazon,MAQ Software\n", |
| 151 | + "# Difficulty: Basic Marks: 1\n", |
| 152 | + "'''\n", |
| 153 | + "Given an unsorted array arr[] of size N, rotate it by D elements (clockwise). \n", |
| 154 | + "\n", |
| 155 | + "Input:\n", |
| 156 | + "The first line of the input contains T denoting the number of testcases. First line of eacg test case contains two space separated elements, N denoting the size of the array and an integer D denoting the number size of the rotation. Subsequent line will be the N space separated array elements.\n", |
| 157 | + "\n", |
| 158 | + "Output:\n", |
| 159 | + "For each testcase, in a new line, output the rotated array.\n", |
| 160 | + "\n", |
| 161 | + "Constraints:\n", |
| 162 | + "1 <= T <= 200\n", |
| 163 | + "1 <= N <= 107\n", |
| 164 | + "1 <= D <= N\n", |
| 165 | + "0 <= arr[i] <= 105\n", |
| 166 | + "\n", |
| 167 | + "Example:\n", |
| 168 | + "Input:\n", |
| 169 | + "2\n", |
| 170 | + "5 2\n", |
| 171 | + "1 2 3 4 5 \n", |
| 172 | + "10 3\n", |
| 173 | + "2 4 6 8 10 12 14 16 18 20\n", |
| 174 | + "\n", |
| 175 | + "Output:\n", |
| 176 | + "3 4 5 1 2\n", |
| 177 | + "8 10 12 14 16 18 20 2 4 6\n", |
| 178 | + "\n", |
| 179 | + "Explanation :\n", |
| 180 | + "Testcase 1: 1 2 3 4 5 when rotated by 2 elements, it becomes 3 4 5 1 2.\n", |
| 181 | + "'''\n", |
| 182 | + "for _ in range(int(input())):\n", |
| 183 | + " m,d=tuple(map(int,(input().split())))\n", |
| 184 | + " a=list(map(int,input().split()))\n", |
| 185 | + " for j in range(m-d):\n", |
| 186 | + " print(a[j+d],end=' ')\n", |
| 187 | + " for j in range(d):\n", |
| 188 | + " print(a[j],end=' ')\n", |
| 189 | + " print()" |
| 190 | + ] |
| 191 | + }, |
| 192 | + { |
| 193 | + "cell_type": "code", |
| 194 | + "execution_count": 39, |
| 195 | + "metadata": {}, |
| 196 | + "outputs": [ |
| 197 | + { |
| 198 | + "name": "stdout", |
| 199 | + "output_type": "stream", |
| 200 | + "text": [ |
| 201 | + "3\n", |
| 202 | + "6\n", |
| 203 | + "16 17 4 3 5 2\n", |
| 204 | + "17 5 2 \n", |
| 205 | + "5\n", |
| 206 | + "1 2 3 4 0\n", |
| 207 | + "4 0 \n", |
| 208 | + "5\n", |
| 209 | + "7 4 5 7 3\n", |
| 210 | + "7 7 3 \n" |
| 211 | + ] |
| 212 | + } |
| 213 | + ], |
| 214 | + "source": [ |
| 215 | + "# Leaders in an array ---> Amazon, Payu, Samsung\n", |
| 216 | + "# Difficulty: Easy Marks: 2\n", |
| 217 | + "'''\n", |
| 218 | + "Given an array of positive integers. Your task is to find the leaders in the array.\n", |
| 219 | + "Note: An element of array is leader if it is greater than or equal to all the elements to its right side. Also, the rightmost element is always a leader. \n", |
| 220 | + "\n", |
| 221 | + "Input:\n", |
| 222 | + "The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.\n", |
| 223 | + "The first line of each test case contains a single integer N denoting the size of array.\n", |
| 224 | + "The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.\n", |
| 225 | + "\n", |
| 226 | + "Output:\n", |
| 227 | + "Print all the leaders.\n", |
| 228 | + "\n", |
| 229 | + "Constraints:\n", |
| 230 | + "1 <= T <= 100\n", |
| 231 | + "1 <= N <= 107\n", |
| 232 | + "0 <= Ai <= 107\n", |
| 233 | + "\n", |
| 234 | + "Example:\n", |
| 235 | + "Input:\n", |
| 236 | + "3\n", |
| 237 | + "6\n", |
| 238 | + "16 17 4 3 5 2\n", |
| 239 | + "5\n", |
| 240 | + "1 2 3 4 0\n", |
| 241 | + "5\n", |
| 242 | + "7 4 5 7 3\n", |
| 243 | + "Output:\n", |
| 244 | + "17 5 2\n", |
| 245 | + "4 0\n", |
| 246 | + "7 7 3\n", |
| 247 | + "\n", |
| 248 | + "Explanation:\n", |
| 249 | + "Testcase 3: All elements on the right of 7 (at index 0) are smaller than or equal to 7. Also, all the elements of right side of 7 (at index 3) are smaller than 7. And, the last element 3 is itself a leader since no elements are on its right.\n", |
| 250 | + "'''\n", |
| 251 | + "for _ in range(int(input())):\n", |
| 252 | + " n=int(input())\n", |
| 253 | + " a=list(map(int,input().split()))\n", |
| 254 | + " c=a.copy()\n", |
| 255 | + " for i in range(len(a)-1):\n", |
| 256 | + " for j in range(i+1,len(a)):\n", |
| 257 | + " if a[i]<a[j]:\n", |
| 258 | + " c.remove(a[i])\n", |
| 259 | + " break\n", |
| 260 | + " for i in c:\n", |
| 261 | + " print(i,end=' ')\n", |
| 262 | + " print()" |
| 263 | + ] |
| 264 | + } |
| 265 | + ], |
| 266 | + "metadata": { |
| 267 | + "kernelspec": { |
| 268 | + "display_name": "Python 3", |
| 269 | + "language": "python", |
| 270 | + "name": "python3" |
| 271 | + }, |
| 272 | + "language_info": { |
| 273 | + "codemirror_mode": { |
| 274 | + "name": "ipython", |
| 275 | + "version": 3 |
| 276 | + }, |
| 277 | + "file_extension": ".py", |
| 278 | + "mimetype": "text/x-python", |
| 279 | + "name": "python", |
| 280 | + "nbconvert_exporter": "python", |
| 281 | + "pygments_lexer": "ipython3", |
| 282 | + "version": "3.7.5" |
| 283 | + } |
| 284 | + }, |
| 285 | + "nbformat": 4, |
| 286 | + "nbformat_minor": 2 |
| 287 | +} |
0 commit comments