I am trying to solve this GeeksForGeeks problem and for a specific input file, my solutions seems to be insufficient. I am not sure why the code is not working. Here is the question.
QUESTION
Given an array a of size N which contains elements from 0 to N-1, you need to find all the elements occurring more than once in the given array. Return the answer in ascending order. If no such element is found, return list containing [-1].
Note: The extra space is only for the array to be returned. Try and perform all operations within the provided array.
Example 1:
Input:
N = 4
a[] = {0,3,1,2}
Output:
Explanation:
There is no repeating element in the array. Therefore output is -1.
Example 2:
Input:
N = 5
a[] = {2,3,1,2,3}
Output:
2 3
Explanation:
2 and 3 occur more than once in the given array.
Your Task:
Complete the function duplicates() which takes array a[] and n as input as parameters and returns a list of elements that occur more than once in the given array in a sorted manner.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(n).
Constraints:
1 <= N <= 105
0 <= A[i] <= N-1, for each valid i
MY CODE
def duplicates(self, arr, n):
# code here
result = []
for i in range(0,n):
idx=abs(arr[i])
if idx==0 and idx not in result and arr.count(idx)>1:
result.append(idx)
elif arr[idx]>=0:
arr[idx] = -arr[idx]
elif idx not in result:
result.append(idx)
if len(result)==0:
return [-1]
return sorted(result)
The Input File
835
722 421 253 502 377 206 380 671 341 358 728 338 818 387 265 48 305 789 809 159 543 710 568 655 655 764 454 149 475 709 352 670 415 51 113 90 572 118 775 618 537 659 752 156 128 455 296 142 108 152 517 408 223 452 775 163 586 103 669 574 712 665 267 144 610 103 22 26 804 413 200 293 704 415 287 531 353 134 494 545 438 186 575 222 446 670 783 327 243 411 62 807 357 456 140 68 104 704 474 514 526 715 438 420 182 433 700 737 757 190 524 679 387 570 338 318 575 384 669 556 184 264 417 113 612 130 98 535 561 26 114 478 73 273 470 356 340 626 653 464 57 585 570 473 441 69 15 134 671 109 823 356 714 484 404 51 624 804 174 677 683 563 288 503 677 444 651 164 827 623 696 201 154 251 493 336 24 96 650 131 635 800 707 348 421 341 156 62 32 83 513 536 593 346 62 320 499 537 384 820 208 600 545 219 503 476 88 575 363 516 369 352 431 328 727 419 350 717 434 613 188 207 106 455 770 259 600 461 271 730 651 690 766 46 135 774 606 51 700 737 785 523 232 169 90 342 566 99 258 110 555 691 230 84 149 652 405 269 351 201 133 194 670 291 768 237 236 417 253 529 446 38 141 248 257 206 517 443 16 486 258 299 461 103 580 549 81 178 108 713 699 43 314 204 401 647 304 318 598 414 744 357 692 705 372 415 685 643 813 267 446 250 11 70 523 85 482 393 688 464 645 582 313 681 454 423 320 171 272 386 542 732 714 113 202 664 29 176 286 584 141 313 401 438 798 429 70 620 81 714 790 105 656 681 796 60 53 722 178 468 668 257 240 511 125 177 765 421 753 523 786 182 416 479 228 525 107 631 730 817 603 568 560 276 811 290 0 311 550 620 61 735 625 317 699 320 246 152 327 377 798 61 341 183 705 291 566 470 162 298 179 405 223 712 506 379 285 477 584 798 719 279 573 44 268 239 11 489 313 74 61 582 670 248 264 57 344 138 313 329 26 417 490 338 499 262 185 502 82 584 416 690 232 724 757 476 304 675 463 95 663 14 639 349 828 181 398 93 179 441 553 33 585 43 801 723 753 35 122 628 118 503 704 359 294 101 601 644 761 322 570 272 661 297 544 736 645 342 403 568 81 614 810 16 399 245 269 388 799 476 93 612 259 470 373 796 685 333 429 706 581 526 536 679 550 218 441 599 688 725 341 682 596 508 86 158 260 547 147 651 697 794 415 387 327 34 15 272 213 57 651 285 758 678 361 324 636 573 296 541 30 204 296 255 462 231 20 234 504 207 15 718 370 658 273 187 726 532 242 559 457 220 142 549 137 719 470 76 659 742 590 834 203 302 145 130 130 125 467 297 382 481 510 821 7 809 203 321 772 105 195 307 558 762 165 235 248 734 247 159 425 114 41 584 815 587 191 277 543 563 556 500 136 284 395 135 269 143 219 363 775 178 707 629 734 616 17 54 674 320 337 781 208 434 388 239 369 139 531 45 815 708 790 441 589 97 325 738 212 93 670 167 791 827 102 753 664 620 143 386 143 460 772 446 757 115 365 322 681 259 17 211 325 364 517 222 445 771 523 288 199 250 550 91 424 197 267 300 492 196 59 16 240 811 320 370 582 535 691 190 178 516 637 276 394 221 259 247 430 366 623 403 24 237 633 591 672 729 378 749 206 447 26 334 431 117 496 700 356 192 524 338 783 604 223 154 533 574 215 159 189 406 29 386 586 743 129 102 207 630 442 224 488 657 817 708 537 828 287 96 366 729 599 503 828 154 350 356 293 711 697 743 580 278 673 563 544 415 34 699 158 327 82 583 190 747 407 604 558 509 165 753 355 707 497 294 268 389 740 746 129 783 50 350 316 323 734 613 702 435 412 383 569 594 564
My Output File
11 15 16 17 24 26 29 34 43 51 57 61 62 70 81 82 90 93 96 102 103 105 108 113 114 118 125 129 130 134 135 141 142 143 149 152 154 156 158 159 165 178 179 182 190 201 203 204 206 207 208 219 222 223 232 237 239 240 247 248 250 253 257 258 259 264 267 268 269 272 273 276 285 287 288 291 293 294 296 297 304 313 318 320 322 325 327 338 341 342 350 352 356 357 363 366 369 370 377 384 387 388 401 403 405 415 416 417 421 429 431 434 438 441 446 454 455 461 464 470 476 499 502 503 516 517 523 524 526 531 535 536 537 543 544 545 549 550 556 558 563 566 568 570 573 574 575 580 582 584 585 586 599 600 604 612 613 620 623 645 651 655 659 664 669 670 671 677 679 681 685 688 690 691 697 699 700 704 705 707 708 712 714 719 722 729 730 734 737 743 753 757 772 775 783 790 796 798 804 809 811 815 817 827 828
Correct Output File
11 15 16 17 24 26 29 34 43 51 57 61 62 70 81 82 90 93 96 102 103 105 108 113 114 118 125 129 130 134 135 141 142 143 149 152 154 156 158 159 165 178 179 182 190 201 203 204 206 207 208 219 222 223 232 237 239 240 247 248 250 253 257 258 259 264 267 268 269 272 273 276 285 287 288 291 293 294 296 297 304 313 318 320 322 325 327 338 341 342 350 352 356 357 363 366 369 370 377 384 386 387 388 401 403 405 415 416 417 421 429 431 434 438 441 446 454 455 461 464 470 476 499 502 503 516 517 523 524 526 531 535 536 537 543 544 545 549 550 556 558 563 566 568 570 573 574 575 580 582 584 585 586 599 600 604 612 613 620 623 645 651 655 659 664 669 670 671 677 679 681 685 688 690 691 697 699 700 704 705 707 708 712 714 719 722 729 730 734 737 743 753 757 772 775 783 790 796 798 804 809 811 815 817 827 828
The only thing I have understood is that I have a missing value of 386 in my output but I cannot figure out why.