|
251 | 251 | "for _ in range(int(input())):\n",
|
252 | 252 | " n=int(input())\n",
|
253 | 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", |
| 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 | + " c=[a[-1]]\n", |
| 261 | + " for i in range(len(a)-2,-1,-1):\n", |
| 262 | + " if a[i]>= c[-1]:\n", |
| 263 | + " c.append(a[i])\n", |
| 264 | + "# for i in c:\n", |
| 265 | + "# print(i,end=' ')\n", |
| 266 | + " print(*(c[::-1]),end='')\n", |
262 | 267 | " print()"
|
263 | 268 | ]
|
| 269 | + }, |
| 270 | + { |
| 271 | + "cell_type": "code", |
| 272 | + "execution_count": 1, |
| 273 | + "metadata": {}, |
| 274 | + "outputs": [ |
| 275 | + { |
| 276 | + "name": "stdout", |
| 277 | + "output_type": "stream", |
| 278 | + "text": [ |
| 279 | + "1\n", |
| 280 | + "4\n", |
| 281 | + "1 2 3 4\n", |
| 282 | + "4 3 2 1\n" |
| 283 | + ] |
| 284 | + } |
| 285 | + ], |
| 286 | + "source": [ |
| 287 | + "# Reverse an array ---> Infosys, Moonfrog Labs\n", |
| 288 | + "# Difficulty: School Marks: 0\n", |
| 289 | + "'''\n", |
| 290 | + "Given an array A of size N, print the reverse of it.\n", |
| 291 | + "\n", |
| 292 | + "Input:\n", |
| 293 | + "First line contains an integer denoting the test cases 'T'. T testcases follow. Each testcase contains two lines of input. First line contains N the size of the array A. The second line contains the elements of the array.\n", |
| 294 | + "\n", |
| 295 | + "Output:\n", |
| 296 | + "For each testcase, in a new line, print the array in reverse order.\n", |
| 297 | + "\n", |
| 298 | + "Constraints:\n", |
| 299 | + "1 <= T <= 100\n", |
| 300 | + "1 <= N <=100\n", |
| 301 | + "0 <= Ai <= 100\n", |
| 302 | + "\n", |
| 303 | + "Example:\n", |
| 304 | + "Input:\n", |
| 305 | + "1\n", |
| 306 | + "4\n", |
| 307 | + "1 2 3 4\n", |
| 308 | + "Output:\n", |
| 309 | + "4 3 2 1\n", |
| 310 | + "'''\n", |
| 311 | + "for _ in range(int(input())):\n", |
| 312 | + " n=int(input())\n", |
| 313 | + " a=list(map(int,input().split()))\n", |
| 314 | + " a.reverse()\n", |
| 315 | + " print(*a)" |
| 316 | + ] |
| 317 | + }, |
| 318 | + { |
| 319 | + "cell_type": "code", |
| 320 | + "execution_count": 26, |
| 321 | + "metadata": {}, |
| 322 | + "outputs": [ |
| 323 | + { |
| 324 | + "name": "stdout", |
| 325 | + "output_type": "stream", |
| 326 | + "text": [ |
| 327 | + "1\n", |
| 328 | + "6 2\n", |
| 329 | + "10 20 30 40 50 60\n", |
| 330 | + "20 10 40 30 60 50 " |
| 331 | + ] |
| 332 | + } |
| 333 | + ], |
| 334 | + "source": [ |
| 335 | + "# Reverse array in groups ---> Amazon\n", |
| 336 | + "# Difficulty: Basic Marks: 1\n", |
| 337 | + "'''\n", |
| 338 | + "Given an array arr[] of positive integers of size N. Reverse every sub-array of K group elements.\n", |
| 339 | + "\n", |
| 340 | + "Input:\n", |
| 341 | + "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 two lines of input. The first line of each test case consists of an integer N(size of array) and an integer K separated by a space. The second line of each test case contains N space separated integers denoting the array elements.\n", |
| 342 | + "\n", |
| 343 | + "Output:\n", |
| 344 | + "For each test case, print the modified array.\n", |
| 345 | + "\n", |
| 346 | + "Constraints:\n", |
| 347 | + "1 ≤ T ≤ 200\n", |
| 348 | + "1 ≤ N, K ≤ 107\n", |
| 349 | + "1 ≤ A[i] ≤ 1018\n", |
| 350 | + "\n", |
| 351 | + "Example:\n", |
| 352 | + "Input\n", |
| 353 | + "2\n", |
| 354 | + "5 3\n", |
| 355 | + "1 2 3 4 5\n", |
| 356 | + "6 2\n", |
| 357 | + "10 20 30 40 50 60\n", |
| 358 | + "\n", |
| 359 | + "Output\n", |
| 360 | + "3 2 1 5 4\n", |
| 361 | + "20 10 40 30 60 50\n", |
| 362 | + "\n", |
| 363 | + "Explanation:\n", |
| 364 | + "Testcase 1: Reversing groups in size 3, first group consists of elements 1, 2, 3. Reversing this group, we have elements in order as 3, 2, 1.\n", |
| 365 | + "'''\n", |
| 366 | + "from math import ceil\n", |
| 367 | + "for i in range(int(input())):\n", |
| 368 | + " n,k=map(int,input().split())\n", |
| 369 | + " arr=list(map(int,input().split()))\n", |
| 370 | + " arr[:k]=arr[k-1::-1] #i was having a bit difficulty in converting the first group inside the loop\n", |
| 371 | + " for i in range(1,ceil(float(n)/k)):\n", |
| 372 | + " arr[k*i:k*i+k]=arr[k*i+k-1:k*i-1:-1]\n", |
| 373 | + " print(*arr)\n", |
| 374 | + "'''for _ in range(int(input())):\n", |
| 375 | + " n1,n2=input().split()\n", |
| 376 | + " a=list(map(int,input().split()))\n", |
| 377 | + "# if int(n1)%int(n2)!=0:\n", |
| 378 | + "# n2=int(n1)//int(n2)+1\n", |
| 379 | + " j=int(n1)//int(n2)\n", |
| 380 | + " for i in range(j):\n", |
| 381 | + " c=[]\n", |
| 382 | + " for k in range(int(n2)*i,int(n2)*(i+1)):\n", |
| 383 | + " c.append(a[k])\n", |
| 384 | + " print(*(c[::-1]),end=' ')\n", |
| 385 | + " print('',*(a[:int(n2)*j-1:-1]),end='',sep=' ')\n", |
| 386 | + " print()\n", |
| 387 | + "'''\n" |
| 388 | + ] |
| 389 | + }, |
| 390 | + { |
| 391 | + "cell_type": "code", |
| 392 | + "execution_count": 7, |
| 393 | + "metadata": {}, |
| 394 | + "outputs": [ |
| 395 | + { |
| 396 | + "name": "stdout", |
| 397 | + "output_type": "stream", |
| 398 | + "text": [ |
| 399 | + "2\n", |
| 400 | + "6\n", |
| 401 | + "7 10 4 3 20 15\n", |
| 402 | + "3\n", |
| 403 | + "7\n", |
| 404 | + "5\n", |
| 405 | + "7 10 4 20 15\n", |
| 406 | + "4\n", |
| 407 | + "15\n" |
| 408 | + ] |
| 409 | + } |
| 410 | + ], |
| 411 | + "source": [ |
| 412 | + "# Kth smallest element ---> ABCO, Accolite, Amazon, Cisco, Hike, Microsoft, Snapdeal, VMWare\n", |
| 413 | + "# Difficulty: Medium Marks: 4\n", |
| 414 | + "'''\n", |
| 415 | + "Given an array arr[] and a number K where K is smaller than size of array, the task is to find the Kth smallest element in the given array. It is given that all array elements are distinct.\n", |
| 416 | + "\n", |
| 417 | + "Expected Time Complexity: O(n)\n", |
| 418 | + "\n", |
| 419 | + "Input:\n", |
| 420 | + "The first line of input contains an integer T, denoting the number of testcases. Then T test cases follow. Each test case consists of three lines. First line of each testcase contains an integer N denoting size of the array. Second line contains N space separated integer denoting elements of the array. Third line of the test case contains an integer K.\n", |
| 421 | + "\n", |
| 422 | + "Output:\n", |
| 423 | + "Corresponding to each test case, print the kth smallest element in a new line.\n", |
| 424 | + "\n", |
| 425 | + "Constraints:\n", |
| 426 | + "1 <= T <= 100\n", |
| 427 | + "1 <= N <= 105\n", |
| 428 | + "1 <= arr[i] <= 105\n", |
| 429 | + "1 <= K <= N\n", |
| 430 | + "\n", |
| 431 | + "Example:\n", |
| 432 | + "Input:\n", |
| 433 | + "2\n", |
| 434 | + "6\n", |
| 435 | + "7 10 4 3 20 15\n", |
| 436 | + "3\n", |
| 437 | + "5\n", |
| 438 | + "7 10 4 20 15\n", |
| 439 | + "4\n", |
| 440 | + "\n", |
| 441 | + "Output:\n", |
| 442 | + "7\n", |
| 443 | + "15\n", |
| 444 | + "\n", |
| 445 | + "Explanation:\n", |
| 446 | + "Testcase 1: 3rd smallest element in the given array is 7.\n", |
| 447 | + "'''\n", |
| 448 | + "for _ in range(int(input())):\n", |
| 449 | + " n=int(input())\n", |
| 450 | + " a=list(map(int,input().split()))\n", |
| 451 | + " i=int(input())\n", |
| 452 | + " a.sort()\n", |
| 453 | + " print(a[i-1])" |
| 454 | + ] |
| 455 | + }, |
| 456 | + { |
| 457 | + "cell_type": "code", |
| 458 | + "execution_count": 2, |
| 459 | + "metadata": {}, |
| 460 | + "outputs": [ |
| 461 | + { |
| 462 | + "data": { |
| 463 | + "text/plain": [ |
| 464 | + "[55, 9, 7, 6, 6, 5, 5, 4, 3, 3, 1, 1]" |
| 465 | + ] |
| 466 | + }, |
| 467 | + "execution_count": 2, |
| 468 | + "metadata": {}, |
| 469 | + "output_type": "execute_result" |
| 470 | + } |
| 471 | + ], |
| 472 | + "source": [ |
| 473 | + "a=[1,3,4,5,6,7,3,1,9,6,55,5]\n", |
| 474 | + "a.sort(reverse=True)\n", |
| 475 | + "a" |
| 476 | + ] |
| 477 | + }, |
| 478 | + { |
| 479 | + "cell_type": "code", |
| 480 | + "execution_count": 4, |
| 481 | + "metadata": {}, |
| 482 | + "outputs": [ |
| 483 | + { |
| 484 | + "name": "stdout", |
| 485 | + "output_type": "stream", |
| 486 | + "text": [ |
| 487 | + "1\n", |
| 488 | + "5\n", |
| 489 | + "3 1 2 5 4\n", |
| 490 | + "5 3 1 4 2\n" |
| 491 | + ] |
| 492 | + } |
| 493 | + ], |
| 494 | + "source": [ |
| 495 | + "for _ in range(int(input())):\n", |
| 496 | + " n=int(input())\n", |
| 497 | + " a=list(map(int,input().split()))\n", |
| 498 | + " a.sort(reverse=True)\n", |
| 499 | + " print(*(a[::2]),end=' ')\n", |
| 500 | + " print(*(a[1::2]))" |
| 501 | + ] |
| 502 | + }, |
| 503 | + { |
| 504 | + "cell_type": "code", |
| 505 | + "execution_count": null, |
| 506 | + "metadata": {}, |
| 507 | + "outputs": [], |
| 508 | + "source": [] |
264 | 509 | }
|
265 | 510 | ],
|
266 | 511 | "metadata": {
|
|
0 commit comments