|
550 | 550 | "For the second query, we have 2 pairs in total that sum up to 10. They are, (2, 8) and (5, 5).\n",
|
551 | 551 | "'''\n",
|
552 | 552 | "\n",
|
| 553 | + "# Solution-1\n", |
| 554 | + "\n", |
| 555 | + "from sys import stdin\n", |
| 556 | + "\n", |
| 557 | + "def pairSum(arr, n, num) :\n", |
| 558 | + " arr.sort()\n", |
| 559 | + " \n", |
| 560 | + " si = 0\n", |
| 561 | + " ei = n-1\n", |
| 562 | + " numpair = 0\n", |
| 563 | + " \n", |
| 564 | + " while si<ei:\n", |
| 565 | + " if arr[si]+arr[ei]<num:\n", |
| 566 | + " si+=1\n", |
| 567 | + " \n", |
| 568 | + " elif arr[si]+arr[ei]>num:\n", |
| 569 | + " ei-=1\n", |
| 570 | + " \n", |
| 571 | + " else:\n", |
| 572 | + " startele = arr[si]\n", |
| 573 | + " endele = arr[ei]\n", |
| 574 | + " \n", |
| 575 | + " if startele == endele: \n", |
| 576 | + " totalele = (ei-si)+1\n", |
| 577 | + " numpair += (totalele*(totalele-1)//2)\n", |
| 578 | + " \n", |
| 579 | + " return numpair\n", |
| 580 | + " \n", |
| 581 | + " tempsi = si+1\n", |
| 582 | + " tempei = ei-1\n", |
| 583 | + " \n", |
| 584 | + " while (tempsi<=tempei) and (arr[tempsi]==startele):\n", |
| 585 | + " tempsi += 1\n", |
| 586 | + " \n", |
| 587 | + " while (tempei>=tempsi) and (arr[tempei]==endele):\n", |
| 588 | + " tempei -= 1\n", |
| 589 | + " \n", |
| 590 | + " totalelefromstart = (tempsi-si)\n", |
| 591 | + " totalelefromend = (ei-tempei)\n", |
| 592 | + " \n", |
| 593 | + " numpair += (totalelefromstart * totalelefromend)\n", |
| 594 | + " \n", |
| 595 | + " si = tempsi\n", |
| 596 | + " ei = tempei\n", |
| 597 | + " \n", |
| 598 | + " return numpair\n", |
| 599 | + "\n", |
| 600 | + "#taking input using fast I/O method\n", |
| 601 | + "def takeInput() :\n", |
| 602 | + " n = int(stdin.readline().strip())\n", |
| 603 | + " if n == 0 :\n", |
| 604 | + " return list(), 0\n", |
| 605 | + "\n", |
| 606 | + " arr = list(map(int, stdin.readline().strip().split(\" \")))\n", |
| 607 | + " return arr, n\n", |
| 608 | + "\n", |
| 609 | + "def printList(arr, n) : \n", |
| 610 | + " for i in range(n) :\n", |
| 611 | + " print(arr[i], end = \" \")\n", |
| 612 | + " print()\n", |
| 613 | + "\n", |
| 614 | + "#main\n", |
| 615 | + "t = int(stdin.readline().strip())\n", |
| 616 | + "while t > 0 : \n", |
| 617 | + " arr, n = takeInput()\n", |
| 618 | + " num = int(stdin.readline().strip())\n", |
| 619 | + " print(pairSum(arr, n, num))\n", |
| 620 | + " t -= 1\n", |
| 621 | + "\n", |
| 622 | + " \n", |
| 623 | + "\n", |
| 624 | + "# Solution-2\n", |
| 625 | + "\n", |
553 | 626 | "from sys import stdin\n",
|
554 | 627 | "from collections import OrderedDict\n",
|
555 | 628 | "\n",
|
|
609 | 682 | "execution_count": null,
|
610 | 683 | "outputs": []
|
611 | 684 | },
|
| 685 | + { |
| 686 | + "cell_type": "code", |
| 687 | + "source": [ |
| 688 | + "'''\n", |
| 689 | + "Triplet sum\n", |
| 690 | + "\n", |
| 691 | + "You have been given a random integer array/list(ARR) and a number X. Find and return the triplet(s) in the array/list which sum \n", |
| 692 | + "to X.\n", |
| 693 | + "\n", |
| 694 | + "Note :\n", |
| 695 | + "Given array/list can contain duplicate elements.\n", |
| 696 | + "\n", |
| 697 | + "Input format :\n", |
| 698 | + "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.\n", |
| 699 | + "\n", |
| 700 | + "First line of each test case or query contains an integer 'N' representing the size of the first array/list.\n", |
| 701 | + "\n", |
| 702 | + "Second line contains 'N' single space separated integers representing the elements in the array/list.\n", |
| 703 | + "\n", |
| 704 | + "Third line contains an integer 'X'.\n", |
| 705 | + "\n", |
| 706 | + "Output format :\n", |
| 707 | + "For each test case, print the total number of triplets present in the array/list.\n", |
| 708 | + "\n", |
| 709 | + "Output for every test case will be printed in a separate line.\n", |
| 710 | + "\n", |
| 711 | + "Constraints :\n", |
| 712 | + "1 <= t <= 10^2\n", |
| 713 | + "0 <= N <= 10^3\n", |
| 714 | + "0 <= X <= 10^9\n", |
| 715 | + "Time Limit: 1 sec\n", |
| 716 | + "\n", |
| 717 | + "Sample Input 1:\n", |
| 718 | + "1\n", |
| 719 | + "7\n", |
| 720 | + "1 2 3 4 5 6 7 \n", |
| 721 | + "12\n", |
| 722 | + "Sample Output 1:\n", |
| 723 | + "5\n", |
| 724 | + "\n", |
| 725 | + "Sample Input 2:\n", |
| 726 | + "2\n", |
| 727 | + "7\n", |
| 728 | + "1 2 3 4 5 6 7 \n", |
| 729 | + "19\n", |
| 730 | + "9\n", |
| 731 | + "2 -5 8 -6 0 5 10 11 -3\n", |
| 732 | + "10\n", |
| 733 | + "Sample Output 2:\n", |
| 734 | + "0\n", |
| 735 | + "5\n", |
| 736 | + "\n", |
| 737 | + "Explanation for Input 2:\n", |
| 738 | + "Since there doesn't exist any triplet with sum equal to 19 for the first query, we print 0.\n", |
| 739 | + "\n", |
| 740 | + "For the second query, we have 5 triplets in total that sum up to 10. They are, (2, 8, 0), (2, 11, -3), (-5, 5, 10), \n", |
| 741 | + "(8, 5, -3) and (-6, 5, 11)\n", |
| 742 | + "'''\n", |
| 743 | + "\n", |
| 744 | + "from sys import stdin\n", |
| 745 | + "\n", |
| 746 | + "def tripletSum(arr, n, num) :\n", |
| 747 | + " arr.sort()\n", |
| 748 | + " numtriplets = 0\n", |
| 749 | + " \n", |
| 750 | + " for i in range(n):\n", |
| 751 | + " pairsumfor = num-arr[i]\n", |
| 752 | + " numpairs = pairSum(arr,i+1,n-1,pairsumfor)\n", |
| 753 | + " numtriplets +=numpairs\n", |
| 754 | + " return numtriplets\n", |
| 755 | + " \n", |
| 756 | + " \n", |
| 757 | + "def pairSum(arr, si, ei, num) :\n", |
| 758 | + " \n", |
| 759 | + " numpair=0\n", |
| 760 | + " while si <ei:\n", |
| 761 | + " if arr[si]+arr[ei]<num:\n", |
| 762 | + " si+=1\n", |
| 763 | + " elif arr[si]+arr[ei]>num:\n", |
| 764 | + " ei-=1\n", |
| 765 | + " else:\n", |
| 766 | + " startele=arr[si]\n", |
| 767 | + " endele=arr[ei]\n", |
| 768 | + " \n", |
| 769 | + " if startele == endele: \n", |
| 770 | + " totalele=(ei-si)+1\n", |
| 771 | + " numpair+=(totalele*(totalele-1)//2)\n", |
| 772 | + " \n", |
| 773 | + " return numpair\n", |
| 774 | + " \n", |
| 775 | + " tempsi=si+1\n", |
| 776 | + " tempei=ei-1\n", |
| 777 | + " \n", |
| 778 | + " while (tempsi<=tempei) and (arr[tempsi]==startele):\n", |
| 779 | + " tempsi+=1\n", |
| 780 | + " \n", |
| 781 | + " while (tempei>=tempsi) and (arr[tempei]==endele):\n", |
| 782 | + " tempei-=1\n", |
| 783 | + " \n", |
| 784 | + " totalelefromstart=(tempsi-si)\n", |
| 785 | + " totalelefromend=(ei-tempei)\n", |
| 786 | + " \n", |
| 787 | + " numpair+=(totalelefromstart * totalelefromend)\n", |
| 788 | + " \n", |
| 789 | + " si=tempsi\n", |
| 790 | + " ei=tempei\n", |
| 791 | + " \n", |
| 792 | + " return numpair\n", |
| 793 | + "\n", |
| 794 | + "\n", |
| 795 | + "def takeInput() :\n", |
| 796 | + " n = int(stdin.readline().strip())\n", |
| 797 | + " if n == 0 :\n", |
| 798 | + " return list(), 0\n", |
| 799 | + "\n", |
| 800 | + " arr = list(map(int, stdin.readline().strip().split(\" \")))\n", |
| 801 | + " return arr, n\n", |
| 802 | + "\n", |
| 803 | + "def printList(arr, n) : \n", |
| 804 | + " for i in range(n) :\n", |
| 805 | + " print(arr[i], end = \" \")\n", |
| 806 | + " print()\n", |
| 807 | + "\n", |
| 808 | + "#main\n", |
| 809 | + "t = int(stdin.readline().strip())\n", |
| 810 | + "while t > 0 : \n", |
| 811 | + " arr, n = takeInput()\n", |
| 812 | + " num = int(stdin.readline().strip())\n", |
| 813 | + " print(tripletSum(arr, n, num))\n", |
| 814 | + " t -= 1" |
| 815 | + ], |
| 816 | + "metadata": { |
| 817 | + "id": "-oX-8TjPeBU8" |
| 818 | + }, |
| 819 | + "execution_count": null, |
| 820 | + "outputs": [] |
| 821 | + }, |
| 822 | + { |
| 823 | + "cell_type": "code", |
| 824 | + "source": [ |
| 825 | + "'''\n", |
| 826 | + "Rotate array\n", |
| 827 | + "\n", |
| 828 | + "You have been given a random integer array/list(ARR) of size N. Write a function that rotates the given array/list by D elements(towards the left).\n", |
| 829 | + "\n", |
| 830 | + "Note:\n", |
| 831 | + "Change in the input array/list itself. You don't need to return or print the elements.\n", |
| 832 | + "\n", |
| 833 | + "Input format :\n", |
| 834 | + "The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.\n", |
| 835 | + "\n", |
| 836 | + "First line of each test case or query contains an integer 'N' representing the size of the array/list.\n", |
| 837 | + "\n", |
| 838 | + "Second line contains 'N' single space separated integers representing the elements in the array/list.\n", |
| 839 | + "\n", |
| 840 | + "Third line contains the value of 'D' by which the array/list needs to be rotated.\n", |
| 841 | + "\n", |
| 842 | + "Output Format :\n", |
| 843 | + "For each test case, print the rotated array/list in a row separated by a single space.\n", |
| 844 | + "\n", |
| 845 | + "Output for every test case will be printed in a separate line.\n", |
| 846 | + "\n", |
| 847 | + "Constraints :\n", |
| 848 | + "1 <= t <= 10^4\n", |
| 849 | + "0 <= N <= 10^6\n", |
| 850 | + "0 <= D <= N\n", |
| 851 | + "Time Limit: 1 sec\n", |
| 852 | + "\n", |
| 853 | + "Sample Input 1:\n", |
| 854 | + "1\n", |
| 855 | + "7\n", |
| 856 | + "1 2 3 4 5 6 7\n", |
| 857 | + "2\n", |
| 858 | + "Sample Output 1:\n", |
| 859 | + "3 4 5 6 7 1 2\n", |
| 860 | + "\n", |
| 861 | + "Sample Input 2:\n", |
| 862 | + "2\n", |
| 863 | + "7\n", |
| 864 | + "1 2 3 4 5 6 7\n", |
| 865 | + "0\n", |
| 866 | + "4\n", |
| 867 | + "1 2 3 4\n", |
| 868 | + "2\n", |
| 869 | + "Sample Output 2:\n", |
| 870 | + "1 2 3 4 5 6 7\n", |
| 871 | + "3 4 1 2\n", |
| 872 | + "'''\n", |
| 873 | + "\n", |
| 874 | + "from sys import stdin\n", |
| 875 | + "\n", |
| 876 | + "def rotate(arr, n, d):\n", |
| 877 | + " \n", |
| 878 | + " if d>n:\n", |
| 879 | + " d = d%n\n", |
| 880 | + " \n", |
| 881 | + " arr[:] = arr[d:] + arr[:n]\n", |
| 882 | + " \n", |
| 883 | + " return arr\n", |
| 884 | + "\n", |
| 885 | + "#Taking Input Using Fats I/O\n", |
| 886 | + "def takeInput() :\n", |
| 887 | + " n = int(stdin.readline().rstrip())\n", |
| 888 | + " if n == 0:\n", |
| 889 | + " return list(), 0\n", |
| 890 | + " arr = list(map(int, stdin.readline().rstrip().split(\" \")))\n", |
| 891 | + " return arr, n\n", |
| 892 | + "\n", |
| 893 | + "#to print the array/list \n", |
| 894 | + "def printList(arr, n) : \n", |
| 895 | + " for i in range(n) :\n", |
| 896 | + " print(arr[i], end = \" \")\n", |
| 897 | + " print()\n", |
| 898 | + "\n", |
| 899 | + "#main\n", |
| 900 | + "t = int(stdin.readline().rstrip())\n", |
| 901 | + "while t > 0 :\n", |
| 902 | + " arr, n = takeInput()\n", |
| 903 | + " d = int(stdin.readline().rstrip())\n", |
| 904 | + " rotate(arr, n, d)\n", |
| 905 | + " printList(arr, n)\n", |
| 906 | + " t -= 1" |
| 907 | + ], |
| 908 | + "metadata": { |
| 909 | + "id": "U9v5HMwvoabf" |
| 910 | + }, |
| 911 | + "execution_count": null, |
| 912 | + "outputs": [] |
| 913 | + }, |
612 | 914 | {
|
613 | 915 | "cell_type": "markdown",
|
614 | 916 | "source": [
|
|
700 | 1002 | "id": "RmnDHeXiRand",
|
701 | 1003 | "outputId": "416db181-8893-4cd8-bdd6-86f4a5b11269"
|
702 | 1004 | },
|
703 |
| - "execution_count": 1, |
| 1005 | + "execution_count": null, |
704 | 1006 | "outputs": [
|
705 | 1007 | {
|
706 | 1008 | "output_type": "stream",
|
|
0 commit comments