pythontantra 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,37 @@
1
+ from .algorithms import (
2
+ dsa_search,
3
+ dsa_cpp,
4
+ dsa_python,
5
+ dsa_mcq,
6
+ dsa_help,
7
+ pythontantra_search,
8
+ pythontantra_get,
9
+ pythontantra_mcq,
10
+ pythontantra_help,
11
+ codetantra_search,
12
+ codetantra_get,
13
+ codetantra_mcq,
14
+ codetantra_help,
15
+ get_binary_search_cpp,
16
+ get_quick_sort_cpp,
17
+ PROBLEMS
18
+ )
19
+
20
+ __all__ = [
21
+ 'dsa_search',
22
+ 'dsa_cpp',
23
+ 'dsa_python',
24
+ 'dsa_mcq',
25
+ 'dsa_help',
26
+ 'pythontantra_search',
27
+ 'pythontantra_get',
28
+ 'pythontantra_mcq',
29
+ 'pythontantra_help',
30
+ 'codetantra_search',
31
+ 'codetantra_get',
32
+ 'codetantra_mcq',
33
+ 'codetantra_help',
34
+ 'get_binary_search_cpp',
35
+ 'get_quick_sort_cpp',
36
+ 'PROBLEMS'
37
+ ]
@@ -0,0 +1,438 @@
1
+ import os
2
+ import json
3
+ import difflib
4
+
5
+ PROBLEMS = {
6
+ 1: {
7
+ "title": "Binary Search (Basic Iterative with Sorting)",
8
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n\tint a[20], i, j, n, key, flag = 0, low, high, mid, temp;\n\tcout << \"Enter value of n : \";\n\tcin >> n;\n\tcout << \"Enter array elements : \";\n\tfor (i=0;i<n;i++) {\n\t\tcin >> a[i];\n\t}\n\tcout << \"Enter key element : \";\n\tcin >> key;\n\tfor (i =0;i<n-1;i++) {\n\t\tfor (j=i+1;j<n;j++) {\n\t\t\tif (a[i]>a[j]) {\n\t\t\t\tswap(a[i],a[j]);\n\t\t\t}\n\t\t}\n\t}\n\tcout << \"After sorting the elements in the array : \";\n\tfor (i =0;i<n;i++) {\n\t\tcout << a[i] << \" \";\n\t}\n\tcout << \"\\n\";\n\tlow = 0;\n\thigh = n-1;\n\twhile (low<=high) {\n\t\tmid = low + (high-low)/2;\n\t\tif (a[mid]==key) {\n\t\t\tflag = 1;\n\t\t\tbreak;\n\t\t} else if (a[mid]<key) {\n\t\t\tlow = mid+1;\n\t\t} else if (a[mid]>key) {\n\t\t\thigh = mid-1;\n\t\t}\n\t}\n\tif (flag) {\n\t\tcout << \"The key element \" << key << \" is found at the position : \" << mid << endl;\n\t} else {\n\t\tcout << \"The key element \" << key << \" is not found in the array\\n\";\n\t}\n}",
9
+ "python_code": "def main():\n n = int(input(\"Enter value of n : \"))\n print(\"Enter array elements : \", end=\"\")\n a = [int(x) for x in input().split()]\n key = int(input(\"Enter key element : \"))\n\n for i in range(n - 1):\n for j in range(i + 1, n):\n if a[i] > a[j]:\n a[i], a[j] = a[j], a[i]\n\n print(\"After sorting the elements in the array : \", end=\"\")\n for i in range(n):\n print(a[i], end=\" \")\n print()\n\n low, high = 0, n - 1\n flag = 0\n mid = 0\n while low <= high:\n mid = low + (high - low) // 2\n if a[mid] == key:\n flag = 1\n break\n elif a[mid] < key:\n low = mid + 1\n else:\n high = mid - 1\n\n if flag:\n print(f\"The key element {key} is found at the position : {mid}\")\n else:\n print(f\"The key element {key} is not found in the array\")\n\nif __name__ == \"__main__\":\n main()"
10
+ },
11
+ 2: {
12
+ "title": "Merge Sort (Basic)",
13
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nvoid display(int arr[15], int n) {\n\tfor(int i=0;i<n;i++){\n\t\tcout << arr[i] << \" \";\n\t}\n\tcout << endl;\n}\nvoid merge(int arr[15], int low, int mid, int high) {\n\tint i=low;\n\tint j=mid+1;\n\tint temp[high-low+1];\n\tint k=0;\n\twhile(i<=mid && j<=high){\n\t\tif(arr[i]<=arr[j]){\n\t\t\ttemp[k++]=arr[i++];\n\t\t}else{\n\t\t\ttemp[k++]=arr[j++];\n\t\t}\n\t}\n\twhile(i<=mid){\n\t\ttemp[k++]=arr[i++];\n\t}\n\twhile(j<=high){\n\t\ttemp[k++] = arr[j++];\n\t}\n\tfor(int i=low,k=0;i<=high;i++,k++){\n\t\tarr[i]=temp[k];\n\t}\n}\nvoid splitAndMerge(int arr[15], int low, int high) {\n\tif(low<high){\n\t\tint mid = low+(high-low)/2;\n\t\tsplitAndMerge(arr,low,mid);\n\t\tsplitAndMerge(arr,mid+1,high);\n\t\tmerge(arr,low,mid,high);\n\t}\n}\n\nint main() {\n\tint n;\n\tcin >> n;\n\tint arr[15];\n\tfor (int i = 0; i < n; i++) cin >> arr[i];\n\tsplitAndMerge(arr, 0, n - 1);\n\tdisplay(arr, n);\n\treturn 0;\n}",
14
+ "python_code": "def display(arr, n):\n for i in range(n):\n print(arr[i], end=\" \")\n print()\n\ndef merge(arr, low, mid, high):\n i = low\n j = mid + 1\n temp = []\n while i <= mid and j <= high:\n if arr[i] <= arr[j]:\n temp.append(arr[i])\n i += 1\n else:\n temp.append(arr[j])\n j += 1\n while i <= mid:\n temp.append(arr[i])\n i += 1\n while j <= high:\n temp.append(arr[j])\n j += 1\n for k in range(len(temp)):\n arr[low + k] = temp[k]\n\ndef split_and_merge(arr, low, high):\n if low < high:\n mid = low + (high - low) // 2\n split_and_merge(arr, low, mid)\n split_and_merge(arr, mid + 1, high)\n merge(arr, low, mid, high)\n\ndef main():\n n = int(input())\n arr = [int(x) for x in input().split()]\n split_and_merge(arr, 0, n - 1)\n display(arr, n)\n\nif __name__ == \"__main__\":\n main()"
15
+ },
16
+ 3: {
17
+ "title": "Quick Sort (Basic)",
18
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nvoid display(int arr[15], int n) {\n\tfor(int i=0;i<n;i++){\n\t\tcout << arr[i] << \" \";\n\t}\n\tcout << endl;\n}\nint partition(int arr[15], int low, int high) {\n\tint part = arr[high];\n\tint i = low-1;\n\tfor(int j=low;j<high;j++){\n\t\tif(arr[j]<part){\n\t\t\ti++;\n\t\t\tswap(arr[i],arr[j]);\n\t\t}\n\t}\n\tswap(arr[i+1],arr[high]);\n\treturn i+1;\n}\nvoid quickSort(int arr[15], int low, int high) {\n\tif(low<high){\n\t\tint part = partition(arr,low,high);\n\t\tquickSort(arr,low,part-1);\n\t\tquickSort(arr,part+1,high);\n\t}\n}\n\nint main() {\n\tint n;\n\tcin >> n;\n\tint arr[15];\n\tfor (int i = 0; i < n; i++) cin >> arr[i];\n\tquickSort(arr, 0, n - 1);\n\tdisplay(arr, n);\n\treturn 0;\n}",
19
+ "python_code": "def display(arr, n):\n for i in range(n):\n print(arr[i], end=\" \")\n print()\n\ndef partition(arr, low, high):\n part = arr[high]\n i = low - 1\n for j in range(low, high):\n if arr[j] < part:\n i += 1\n arr[i], arr[j] = arr[j], arr[i]\n arr[i + 1], arr[high] = arr[high], arr[i + 1]\n return i + 1\n\ndef quick_sort(arr, low, high):\n if low < high:\n part = partition(arr, low, high)\n quick_sort(arr, low, part - 1)\n quick_sort(arr, part + 1, high)\n\ndef main():\n n = int(input())\n arr = [int(x) for x in input().split()]\n quick_sort(arr, 0, n - 1)\n display(arr, n)\n\nif __name__ == \"__main__\":\n main()"
20
+ },
21
+ 4: {
22
+ "title": "Binary Search using Divide and Conquer",
23
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint binaryComp(int arr[], int low, int high, int key, int &count){\n if (low > high) {\n return -1;\n }\n int mid = low + (high - low) / 2;\n count++;\n if (key == arr[mid]) {\n return mid;\n }\n if (key < arr[mid]) {\n return binaryComp(arr, low, mid - 1, key, count);\n }\n return binaryComp(arr, mid + 1, high, key, count);\n}\n\nint main() {\n int n, key;\n cin >> n;\n int arr[n];\n for (int i = 0; i < n; i++) {\n cin >> arr[i];\n }\n cin >> key;\n int count = 0;\n int index = binaryComp(arr, 0, n - 1, key, count);\n cout << \"Index: \" << index << endl << \"Comparisons: \" << count << endl;\n return 0;\n}",
24
+ "python_code": "def main():\n n = int(input())\n a = list(map(int, input().split()))\n key = int(input())\n\n low = 0\n high = n - 1\n comparisons = 0\n index = -1\n\n while low <= high:\n mid = low + (high - low) // 2\n comparisons += 1\n\n if a[mid] == key:\n index = mid\n break\n elif a[mid] < key:\n low = mid + 1\n else:\n high = mid - 1\n\n print(index)\n print(comparisons)\n\nif __name__ == \"__main__\":\n main()"
25
+ },
26
+ 5: {
27
+ "title": "First and Last Occurrence of Element in a Sorted Array",
28
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint firstOccurrence(int arr[],int n,int key){\n\tint low = 0,high=n-1;\n\tint ans = -1;\n\twhile(low<=high){\n\t\tint mid = low + (high-low)/2;\n\t\tif(arr[mid] == key){\n\t\t\tans = mid;\n\t\t\thigh = mid-1;\n\t\t}else if (arr[mid] < key){\n\t\t\tlow = mid+1;\n\t\t}else{\n\t\t\thigh = mid-1;\n\t\t}\n\t}\n\treturn ans;\n}\n\nint lastOccurrence(int arr[],int n,int key){\n\tint low = 0,high = n-1;\n\tint ans = -1;\n\twhile(low<=high){\n\t\tint mid = low + (high-low)/2;\n\t\tif(arr[mid]==key){\n\t\t\tans = mid;\n\t\t\tlow = mid+1;\n\t\t}else if(arr[mid]<key){\n\t\t\tlow = mid+1;\n\t\t}else{\n\t\t\thigh = mid-1;\n\t\t}\n\t}\n\treturn ans;\n}\n\nint main() {\n\tint n, key;\n\tcin >> n;\n\tint arr[n];\n\tfor (int i = 0; i < n; i++) cin >> arr[i];\n\tcin >> key;\n\tcout << firstOccurrence(arr, n, key) << \" \" << lastOccurrence(arr, n, key) << endl;\n\treturn 0;\n}",
29
+ "python_code": "def firstOccurrence(arr, n, key):\n low = 0\n high = n - 1\n ans = -1\n\n while low <= high:\n mid = low + (high - low) // 2\n\n if arr[mid] == key:\n ans = mid\n high = mid - 1\n elif arr[mid] < key:\n low = mid + 1\n else:\n high = mid - 1\n\n return ans\n\n\ndef lastOccurrence(arr, n, key):\n low = 0\n high = n - 1\n ans = -1\n\n while low <= high:\n mid = low + (high - low) // 2\n\n if arr[mid] == key:\n ans = mid\n low = mid + 1\n elif arr[mid] < key:\n low = mid + 1\n else:\n high = mid - 1\n\n return ans\n\ndef main():\n n = int(input())\n arr = list(map(int, input().split()))\n key = int(input())\n\n print(firstOccurrence(arr, n, key), end=\" \")\n print(lastOccurrence(arr, n, key))\n\nif __name__ == \"__main__\":\n main()"
30
+ },
31
+ 6: {
32
+ "title": "Merge Sort with Recursion & Comparison Tracking",
33
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\nint rec = 0;\nint comp = 0;\n\nvoid merge(int arr[],int low,int mid,int high){\n\tint i = low;\n\tint j = mid+1;\n\tint temp[high-low+1];\n\tint k = 0;\n\twhile(i<=mid && j<=high){\n\t\tcomp++;\n\t\tif(arr[i]<arr[j]){\n\t\t\ttemp[k] = arr[i];\n\t\t\tk++;\n\t\t\ti++;\n\t\t}else{\n\t\t\ttemp[k] = arr[j];\n\t\t\tk++;\n\t\t\tj++;\n\t\t}\n\t}\n\twhile(i<=mid){\n\t\ttemp[k] = arr[i];\n\t\tk++;\n\t\ti++;\n\t}\n\twhile(j<=high){\n\t\ttemp[k] = arr[j];\n\t\tk++;\n\t\tj++;\n\t}\n\tfor(int i=low,k=0;i<=high;i++,k++){\n\t\tarr[i] = temp[k];\n\t}\n}\n\nvoid mergeSort(int arr[], int low, int high) {\n\tif (low < high) {\n\t\trec++;\n\t\tint mid = low + (high - low) / 2;\n\t\tmergeSort(arr, low, mid);\n\t\tmergeSort(arr, mid + 1, high);\n\t\tmerge(arr, low, mid, high);\n\t}\n}\n\nint main() {\n\tint n;\n\tcin >> n;\n\tint arr[n];\n\tfor (int i = 0; i < n; i++) cin >> arr[i];\n\tmergeSort(arr, 0, n - 1);\n\tfor (int i = 0; i < n; i++) cout << arr[i] << \" \";\n\tcout << \"\\n\" << rec << \"\\n\" << comp << \"\\n\";\n\treturn 0;\n}",
34
+ "python_code": "rec = 0\ncomp = 0\n\ndef merge(arr, low, mid, high):\n global comp\n\n i = low\n j = mid + 1\n temp = []\n\n while i <= mid and j <= high:\n comp += 1\n\n if arr[i] < arr[j]:\n temp.append(arr[i])\n i += 1\n else:\n temp.append(arr[j])\n j += 1\n\n while i <= mid:\n temp.append(arr[i])\n i += 1\n\n while j <= high:\n temp.append(arr[j])\n j += 1\n\n for k in range(len(temp)):\n arr[low + k] = temp[k]\n\n\ndef mergeSort(arr, low, high):\n global rec\n\n rec += 1\n\n if low < high:\n mid = low + (high - low) // 2\n\n mergeSort(arr, low, mid)\n mergeSort(arr, mid + 1, high)\n\n merge(arr, low, mid, high)\n\ndef main():\n n = int(input())\n arr = list(map(int, input().split()))\n\n mergeSort(arr, 0, n - 1)\n\n print(*arr, end=\" \\n\")\n print(rec)\n print(comp)\n\nif __name__ == \"__main__\":\n main()"
35
+ },
36
+ 7: {
37
+ "title": "Inversion Pairs Count using Merge Sort",
38
+ "code": "#include<bits/stdc++.h>\nusing namespace std;\n\nlong long merge(int arr[],int low,int mid,int high){\n\tint i = low;\n\tint j = mid+1;\n\tint temp[high-low+1];\n\tint k=0;\n\tlong long count=0;\n\twhile(i<=mid && j<=high){\n\t\tif(arr[i]<=arr[j])\n\t\t\ttemp[k++] = arr[i++];\n\t\telse{\n\t\t\ttemp[k++] = arr[j++];\n\t\t\tcount +=mid - i +1;\n\t\t}\n\t}\n\twhile(i<=mid)\n\t\ttemp[k++] = arr[i++];\n\twhile(j<=high)\n\t\ttemp[k++] = arr[j++];\n\tfor(i=low,k=0;i<=high;i++,k++){\n\t\tarr[i]=temp[k];\n\t}\n\treturn count;\n}\n\nlong long mergeSort(int arr[],int low,int high){\n\tlong long count = 0;\n\tif(low<high){\n\t\tint mid = low + (high-low)/2;\n\t\tcount += mergeSort(arr,low,mid);\n\t\tcount += mergeSort(arr,mid+1,high);\n\t\tcount += merge(arr,low,mid,high);\n\t}\n\treturn count;\n}\n\nint main(){\n\tint n;\n\tcin >> n;\n\tint arr[n];\n\tfor(int i=0;i<n;i++) cin >> arr[i];\n\tcout << mergeSort(arr,0,n-1) << endl;\n\treturn 0;\n}",
39
+ "python_code": "def merge(arr, low, mid, high):\n i = low\n j = mid + 1\n temp = []\n count = 0\n\n while i <= mid and j <= high:\n if arr[i] <= arr[j]:\n temp.append(arr[i])\n i += 1\n else:\n temp.append(arr[j])\n j += 1\n count += mid - i + 1\n\n while i <= mid:\n temp.append(arr[i])\n i += 1\n\n while j <= high:\n temp.append(arr[j])\n j += 1\n\n for k in range(len(temp)):\n arr[low + k] = temp[k]\n\n return count\n\n\ndef mergeSort(arr, low, high):\n count = 0\n\n if low < high:\n mid = low + (high - low) // 2\n\n count += mergeSort(arr, low, mid)\n count += mergeSort(arr, mid + 1, high)\n count += merge(arr, low, mid, high)\n\n return count\n\ndef main():\n n = int(input())\n arr = list(map(int, input().split()))\n\n ans = mergeSort(arr, 0, n - 1)\n\n print(ans)\n\nif __name__ == \"__main__\":\n main()"
40
+ },
41
+ 8: {
42
+ "title": "Quick Sort with Calls & Comparison Tracking",
43
+ "code": "#include<bits/stdc++.h>\nusing namespace std;\nint calls=0,comp=0;\n\nint partition(int arr[],int low,int high){\n\tint pivot = arr[high];\n\tint i = low-1;\n\tfor(int j=low;j<=high-1;j++){\n\t\tcomp++;\n\t\tif(arr[j]<pivot){\n\t\t\ti++;\n\t\t\tswap(arr[i],arr[j]);\n\t\t}\n\t}\n\tswap(arr[i+1],arr[high]);\n\treturn i+1;\n}\n\nvoid quickSort(int arr[],int low,int high){\n\tif(low<high){\n\t\tcalls++;\n\t\tint part = partition(arr,low,high);\n\t\tquickSort(arr,low,part-1);\n\t\tquickSort(arr,part+1,high);\n\t}\n}\n\nint main(){\n\tint n;\n\tcin >> n;\n\tint arr[n];\n\tfor(int i=0;i<n;i++){\n\t\tcin >> arr[i];\n\t}\n\tquickSort(arr,0,n-1);\n\tfor(int i=0;i<n;i++){\n\t\tcout << arr[i] << \" \";\n\t}\n\tcout << \"\\n\" << comp << \"\\n\" << calls << endl;\n\treturn 0;\n}",
44
+ "python_code": "comp = 0\nrec = 0\n\n\ndef partition(arr, low, high):\n global comp\n\n pivot = arr[high]\n i = low - 1\n\n for j in range(low, high):\n comp += 1\n\n if arr[j] < pivot:\n i += 1\n arr[i], arr[j] = arr[j], arr[i]\n\n arr[i + 1], arr[high] = arr[high], arr[i + 1]\n\n return i + 1\n\n\ndef quick_sort(arr, low, high):\n global rec\n\n if low < high:\n rec += 1\n\n p = partition(arr, low, high)\n\n quick_sort(arr, low, p - 1)\n quick_sort(arr, p + 1, high)\n\ndef main():\n n = int(input())\n arr = list(map(int, input().split()))\n\n quick_sort(arr, 0, n - 1)\n\n print(*arr, end=\" \\n\")\n print(comp)\n print(rec)\n\nif __name__ == \"__main__\":\n main()"
45
+ },
46
+ 9: {
47
+ "title": "Contiguous Subarray with Largest Sum (Kadane's Algorithm)",
48
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n\tint n;\n\tcin >> n;\n\tint arr[n];\n\tfor(int i=0;i<n;i++){\n\t\tcin >> arr[i];\n\t}\n\n\tint curSum = arr[0];\n\tint sumMax = arr[0];\n\tfor(int i=1;i<n;i++){\n\t\tcurSum = max(arr[i],curSum+arr[i]);\n\t\tsumMax = max(curSum,sumMax);\n\t}\n\t\n\tcout << sumMax << endl;\n\treturn 0;\n}",
49
+ "python_code": "def max_crossing_sum(arr, low, mid, high):\n left_sum = -10**18\n total = 0\n\n for i in range(mid, low - 1, -1):\n total += arr[i]\n left_sum = max(left_sum, total)\n\n right_sum = -10**18\n total = 0\n\n for i in range(mid + 1, high + 1):\n total += arr[i]\n right_sum = max(right_sum, total)\n\n return left_sum + right_sum\n\n\ndef max_subarray(arr, low, high):\n if low == high:\n return arr[low]\n\n mid = low + (high - low) // 2\n\n left = max_subarray(arr, low, mid)\n right = max_subarray(arr, mid + 1, high)\n cross = max_crossing_sum(arr, low, mid, high)\n\n return max(left, right, cross)\n\ndef main():\n n = int(input())\n arr = list(map(int, input().split()))\n\n ans = max_subarray(arr, 0, n - 1)\n\n print(ans)\n\nif __name__ == \"__main__\":\n main()"
50
+ },
51
+ 10: {
52
+ "title": "Kth Largest Element in an Array (Min Heap)",
53
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nvoid heapify(vector<int>& heap,int n,int i){\n\tint smallest = i;\n\tint left = 2*i+1;\n\tint right = 2*i+2;\n\tif(left<n && heap[left]<heap[smallest])\n\t\tsmallest = left;\n\tif(right < n && heap[right] < heap[smallest])\n\t\tsmallest = right;\n\tif(smallest!=i){\n\t\tswap(heap[i],heap[smallest]);\n\t\theapify(heap,n,smallest);\n\t}\n}\n\nint kthLargest(vector<int>& nums,int k){\n\tvector<int> heap;\n\tfor (int num : nums){\n\t\theap.push_back(num);\n\t\tint n = heap.size();\n\t\tfor(int i=n/2-1;i>=0;i--){\n\t\t\theapify(heap,n,i);\n\t\t}\n\t\tif(heap.size()>k){\n\t\t\theap[0] = heap.back();\n\t\t\theap.pop_back();\n\t\t\theapify(heap,heap.size(),0);\n\t\t}\n\t}\n\treturn heap[0];\n}\n\nint main(){\n\tint n;\n\tcin >> n;\n\tvector<int> nums;\n\tfor(int i=0;i<n;i++){\n\t\tint a;\n\t\tcin >> a;\n\t\tnums.push_back(a);\n\t}\n\tint kth;\n\tcin >> kth;\n\tcout << kthLargest(nums,kth) << endl;\n\treturn 0;\n}",
54
+ "python_code": "def heapify(heap, n, i):\n smallest = i\n left = 2 * i + 1\n right = 2 * i + 2\n\n if left < n and heap[left] < heap[smallest]:\n smallest = left\n\n if right < n and heap[right] < heap[smallest]:\n smallest = right\n\n if smallest != i:\n heap[i], heap[smallest] = heap[smallest], heap[i]\n heapify(heap, n, smallest)\n\n\ndef kthLargest(nums, k):\n heap = []\n\n for num in nums:\n heap.append(num)\n n = len(heap)\n\n for i in range(n // 2 - 1, -1, -1):\n heapify(heap, n, i)\n\n if len(heap) > k:\n heap[0] = heap[-1]\n heap.pop()\n heapify(heap, len(heap), 0)\n\n return heap[0]\n\ndef main():\n n = int(input())\n nums = list(map(int, input().split()))\n k = int(input())\n\n print(kthLargest(nums, k))\n\nif __name__ == \"__main__\":\n main()"
55
+ },
56
+ 11: {
57
+ "title": "Missing Number of the Sequence",
58
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n string input;\n cin >> input;\n\n vector<int> arr;\n stringstream ss(input);\n string x;\n\n while (getline(ss, x, ',')) {\n arr.push_back(stoi(x));\n }\n\n int n = arr.size();\n int actualSum = 0;\n\n for (int i : arr) {\n actualSum += i;\n }\n\n int totalSum = n * (n + 1) / 2;\n\n cout << totalSum - actualSum << endl;\n\n return 0;\n}",
59
+ "python_code": "def main():\n n = input()\n lst = n.split(',')\n actualSum = 0\n sum_val = 0\n max_val = int(lst[0])\n for i in lst:\n actualSum += int(i)\n if int(i) > max_val:\n max_val = int(i)\n for i in range(0, max_val + 1):\n sum_val += i\n print(sum_val - actualSum)\n\nif __name__ == \"__main__\":\n main()"
60
+ },
61
+ 12: {
62
+ "title": "Merge Sorted Array",
63
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nvoid merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {\n int i = m - 1;\n int j = n - 1;\n int k = m + n - 1;\n\n while (j >= 0) {\n if (i >= 0 && nums1[i] > nums2[j]) {\n nums1[k] = nums1[i];\n i--;\n } else {\n nums1[k] = nums2[j];\n j--;\n }\n k--;\n }\n}\n\nint main() {\n vector<int> nums1;\n int x;\n\n string line;\n getline(cin, line);\n stringstream ss(line);\n\n while (ss >> x) {\n nums1.push_back(x);\n }\n\n int m;\n cin >> m;\n\n vector<int> nums2;\n getline(cin >> ws, line);\n stringstream ss2(line);\n\n while (ss2 >> x) {\n nums2.push_back(x);\n }\n\n int n;\n cin >> n;\n\n merge(nums1, m, nums2, n);\n\n cout << \"[\";\n for (int i = 0; i < m + n; i++) {\n cout << nums1[i];\n if (i < m + n - 1)\n cout << \", \";\n }\n cout << \"]\" << endl;\n\n return 0;\n}",
64
+ "python_code": "def merge(nums1, m, nums2, n):\n i = m - 1\n j = n - 1\n k = m + n - 1\n while j >= 0:\n if i >= 0 and nums1[i] > nums2[j]:\n nums1[k] = nums1[i]\n i -= 1\n else:\n nums1[k] = nums2[j]\n j -= 1\n k -= 1\n\ndef main():\n nums1 = list(map(int, input().split()))\n m = int(input())\n nums2 = list(map(int, input().split()))\n n = int(input())\n merge(nums1, m, nums2, n)\n print(nums1)\n\nif __name__ == \"__main__\":\n main()"
65
+ },
66
+ 13: {
67
+ "title": "Longest Binary Subarray after K Flips",
68
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint binarySubarray(int arr[], int low, int high, int zeros) {\n int i = low;\n int j = low;\n int count = 0;\n int maxLen = 0;\n while (j <= high) {\n if (arr[j] == 0) {\n count++;\n }\n if (count > zeros) {\n if (arr[i] == 0) {\n count--;\n }\n i++;\n }\n maxLen = max(maxLen, j - i + 1);\n j++;\n }\n return maxLen;\n}\n\nint main() {\n int n;\n cin >> n;\n int arr[n];\n for (int i = 0; i < n; i++) {\n cin >> arr[i];\n }\n int zeros;\n cin >> zeros;\n cout << binarySubarray(arr, 0, n - 1, zeros);\n return 0;\n}",
69
+ "python_code": "def binarySubarray(arr, low, high, zeros):\n i = low\n j = low\n count = 0\n maxLen = 0\n\n while j <= high:\n if arr[j] == 0:\n count += 1\n\n if count > zeros:\n if arr[i] == 0:\n count -= 1\n i += 1\n\n maxLen = max(maxLen, j - i + 1)\n j += 1\n\n return maxLen\n\ndef main():\n n = int(input())\n arr = list(map(int, input().split()))\n zeros = int(input())\n\n print(binarySubarray(arr, 0, n - 1, zeros))\n\nif __name__ == \"__main__\":\n main()"
70
+ },
71
+ 14: {
72
+ "title": "Sorting by Set Bit Count using Divide and Conquer",
73
+ "code": "#include <iostream>\nusing namespace std;\n\nvoid stringSorting(int len, char *arr) {\n int i, max, location, temp, j, k;\n if (len % 2 == 0) {\n for (i = 0; i < len; i++) {\n max = arr[i];\n location = i;\n for (j = i + 1; j < len; j++)\n if (arr[j] < arr[location]) {\n max = arr[j];\n location = j;\n }\n temp = arr[i];\n arr[i] = arr[location];\n arr[location] = temp;\n }\n }\n else {\n for (i = 0; i < len; i++) {\n max = arr[i];\n location = i;\n for (j = i; j < len; j++)\n if (arr[j] > arr[location]) {\n max = arr[j];\n location = j;\n }\n temp = arr[i];\n arr[i] = arr[location];\n arr[location] = temp;\n }\n }\n}\n\nint main() {\n int n;\n cin >> n;\n char *str = new char[n + 1];\n cin >> str;\n stringSorting(n, str);\n cout << str;\n return 0;\n}",
74
+ "python_code": "def string_sorting(length, arr):\n arr = list(arr)\n if length % 2 == 0:\n for i in range(length):\n location = i\n for j in range(i + 1, length):\n if arr[j] < arr[location]:\n location = j\n arr[i], arr[location] = arr[location], arr[i]\n else:\n for i in range(length):\n location = i\n for j in range(i, length):\n if arr[j] > arr[location]:\n location = j\n arr[i], arr[location] = arr[location], arr[i]\n return \"\".join(arr)\n\ndef main():\n n = int(input())\n str_val = input()\n res = string_sorting(n, str_val)\n print(res, end=\"\")\n\nif __name__ == \"__main__\":\n main()"
75
+ },
76
+ 15: {
77
+ "title": "Book Allocation with Minimum Maximum Load",
78
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int n, students;\n cin >> n >> students;\n vector<int> pages(n);\n int low=0,high=0;\n for (int i=0; i<n;i++) {\n cin >> pages[i];\n low = max(low,pages[i]);\n high += pages[i];\n }\n if (students>n) {\n cout <<-1<<\"\\n\";\n return 0;\n }\n for (int limit=low;limit<=high;limit++) {\n int count=1;\n int sum=0;\n for (int i=0;i<n;i++) {\n if (sum + pages[i] <= limit) {\n sum += pages[i];\n } else {\n count++;\n sum = pages[i];\n }\n }\n if (count <= students) {\n cout << limit << \"\\n\";\n break;\n }\n }\n return 0;\n}",
79
+ "python_code": "def main():\n n, students = map(int, input().split())\n pages = list(map(int, input().split()))\n\n low = 0\n high = 0\n\n for page in pages:\n low = max(low, page)\n high += page\n\n if students > n:\n print(-1)\n else:\n for limit in range(low, high + 1):\n count = 1\n total = 0\n\n for i in range(n):\n if total + pages[i] <= limit:\n total += pages[i]\n else:\n count += 1\n total = pages[i]\n\n if count <= students:\n print(limit)\n break\n\nif __name__ == \"__main__\":\n main()"
80
+ },
81
+ 16: {
82
+ "title": "Minimum Element in Rotated Sorted Array",
83
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int n;\n cin >> n;\n vector<int> a(n);\n for (int i=0; i<n; i++)\n cin >> a[i];\n int k;\n cin >> k;\n k %= n;\n for (int i = 0; i<n; i++) {\n int idx = (n-k+i) % n;\n if (i > 0) cout << \" \";\n cout << a[idx];\n }\n cout << '\\n' << a[0] << '\\n';\n return 0;\n}",
84
+ "python_code": "def main():\n n = int(input())\n a = list(map(int, input().split()))\n k = int(input())\n\n k %= n\n\n for i in range(n):\n idx = (n - k + i) % n\n\n if i > 0:\n print(\" \", end=\"\")\n\n print(a[idx], end=\"\")\n\n print()\n print(a[0])\n\nif __name__ == \"__main__\":\n main()"
85
+ },
86
+ 17: {
87
+ "title": "Search in Rotated Sorted Array",
88
+ "code": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nint main() {\n int n;\n cin >> n;\n vector<int> a(n);\n for (int i = 0; i < n; i++) {\n cin >> a[i];\n }\n int pivot;\n cin >> pivot;\n int target;\n cin >> target;\n for (int i = pivot; i < n; i++) {\n if (i != pivot)\n cout << \" \";\n cout << a[i];\n }\n for (int i = 0; i < pivot; i++) {\n cout << \" \" << a[i];\n }\n cout << \"\\n\";\n int index = -1;\n for (int i = 0; i < n; i++) {\n int value;\n if (i < n - pivot)\n value = a[pivot + i];\n else\n value = a[i - (n - pivot)];\n if (value == target) {\n index = i;\n break;\n }\n }\n cout << index << \"\\n\";\n return 0;\n}",
89
+ "python_code": "def main():\n n = int(input())\n a = list(map(int, input().split()))\n\n pivot = int(input())\n target = int(input())\n\n rotated = a[pivot:] + a[:pivot]\n\n print(*rotated)\n\n index = -1\n\n for i in range(n):\n if rotated[i] == target:\n index = i\n break\n\n print(index)\n\nif __name__ == \"__main__\":\n main()"
90
+ },
91
+ 18: {
92
+ "title": "Koko Eating Bananas",
93
+ "code": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n int n;\n cin >> n;\n vector<long long> piles(n);\n for (int i=0;i<n;i++)\n cin >> piles[i];\n long long h;\n cin >> h;\n long long maxPile = *max_element(piles.begin(),piles.end());\n for (long long k=1;k<=maxPile;k++) {\n long long hours = 0;\n for (long long bananas : piles) {\n hours += (bananas+k-1)/k;\n }\n if (hours <= h) {\n cout << k << endl;\n break;\n }\n }\n return 0;\n}",
94
+ "python_code": "def main():\n n = int(input())\n piles = list(map(int, input().split()))\n h = int(input())\n\n maxPile = max(piles)\n\n for k in range(1, maxPile + 1):\n hours = 0\n\n for bananas in piles:\n hours += (bananas + k - 1) // k\n\n if hours <= h:\n print(k)\n break\n\nif __name__ == \"__main__\":\n main()"
95
+ },
96
+ 19: {
97
+ "title": "Single Number",
98
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n\tmap<int,int> mp;\n\tint n;\n\tcin >> n;\n\tint arr[n];\n\tfor(int i=0;i<n;i++){\n\t\tcin >> arr[i];\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tmp[arr[i]]++;\n\t}\n\tfor(auto& n : mp){\n\t\tif(n.second==1){\n\t\t\tcout << n.first;\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn 0;\n}",
99
+ "python_code": "def main():\n n = int(input())\n arr = list(map(int, input().split()))\n\n mp = {}\n\n for x in arr:\n if x in mp:\n mp[x] += 1\n else:\n mp[x] = 1\n\n for key in sorted(mp):\n if mp[key] == 1:\n print(key, end=\"\")\n break\n\nif __name__ == \"__main__\":\n main()"
100
+ },
101
+ 20: {
102
+ "title": "Kth Missing Positive Number",
103
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int n;\n cin >> n;\n vector<int> a(n);\n for (int i = 0; i < n; i++)\n cin >> a[i];\n int k;\n cin >> k;\n int num = 1, count = 0;\n while (count < k) {\n bool found = false;\n for (int i = 0; i < n; i++) {\n if (a[i] == num) {\n found = true;\n break;\n }\n }\n if (!found) {\n count++;\n if (count == k) {\n cout << num << '\\n';\n break;\n }\n }\n num++;\n }\n return 0;\n}",
104
+ "python_code": "def main():\n n = int(input())\n a = list(map(int, input().split()))\n k = int(input())\n\n num = 1\n count = 0\n\n while count < k:\n found = False\n\n for i in range(n):\n if a[i] == num:\n found = True\n break\n\n if not found:\n count += 1\n\n if count == k:\n print(num)\n break\n\n num += 1\n\nif __name__ == \"__main__\":\n main()"
105
+ },
106
+ 21: {
107
+ "title": "Merge Two Sorted Linked Lists",
108
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n\tint x,y;\n\tcin >> x >> y;\n\tvector<int> ans;\n\tfor(int i=0;i<x;i++){\n\t\tint ele;\n\t\tcin >> ele;\n\t\tans.push_back(ele);\n\t}\n\tfor(int i=0;i<y;i++){\n\t\tint ele;\n\t\tcin >> ele;\n\t\tans.push_back(ele);\n\t}\n\tsort(ans.begin(),ans.end());\n\tfor(int i : ans){\n\t\tcout << i << \" \";\n\t}\n\treturn 0;\n}",
109
+ "python_code": "def main():\n x, y = map(int, input().split())\n\n a = list(map(int, input().split()))\n b = list(map(int, input().split()))\n\n ans = a + b\n ans.sort()\n\n for i in ans:\n print(i, end=\" \")\n\nif __name__ == \"__main__\":\n main()"
110
+ },
111
+ 22: {
112
+ "title": "Basic Hash Table Implementation",
113
+ "code": "#include <iostream>\nusing namespace std;\n\n#define SIZE 10\nint HashTable[SIZE];\n\nint hash_function(int x) {\n\treturn x % SIZE;\n}\n\nvoid insertEle(int x) {\n\tint index = hash_function(x);\n\n\tif (HashTable[index] == -1) {\n\t\tHashTable[index] = x;\n\t\tcout << \"Successfully inserted.\" << endl;\n\t}\n\telse {\n\t\tcout << \"Collision Occured.\" << endl;\n\t}\n}\n\nvoid deleteEle(int x) {\n\tint index = hash_function(x);\n\n\tif (HashTable[index] == x) {\n\t\tHashTable[index] = -1;\n\t\tcout << \"Successfully deleted.\" << endl;\n\t}\n\telse {\n\t\tcout << \"Element not found. So cannot delete the element.\" << endl;\n\t}\n}\n\nvoid searchEle(int x) {\n\tint index = hash_function(x);\n\n\tif (HashTable[index] == x) {\n\t\tcout << \"Element found.\" << endl;\n\t}\n\telse {\n\t\tcout << \"Element not found.\" << endl;\n\t}\n}\n\nvoid print() {\n\tfor (int i = 0; i < SIZE; i++) {\n\t\tcout << \"[\" << i << \"]=>\" << HashTable[i] << endl;\n\t}\n}",
114
+ "python_code": "SIZE = 10\nHashTable = [-1] * SIZE\n\ndef hash_function(x):\n return x % SIZE\n\ndef insertEle(x):\n index = hash_function(x)\n if HashTable[index] == -1:\n HashTable[index] = x\n print(\"Successfully inserted.\")\n else:\n print(\"Collision Occured.\")\n\ndef deleteEle(x):\n index = hash_function(x)\n if HashTable[index] == x:\n HashTable[index] = -1\n print(\"Successfully deleted.\")\n else:\n print(\"Element not found. So cannot delete the element.\")\n\ndef searchEle(x):\n index = hash_function(x)\n if HashTable[index] == x:\n print(\"Element found.\")\n else:\n print(\"Element not found.\")\n\ndef print_table():\n for i in range(SIZE):\n print(f\"[{i}]=>{HashTable[i]}\")"
115
+ },
116
+ 23: {
117
+ "title": "Hash Table with Double Hashing",
118
+ "code": "#include <iostream>\nusing namespace std;\n\n#define SIZE 10\nint HashTable[SIZE];\n\nint hash1(int x) {\n return x % SIZE;\n}\n\nint hash2(int x) {\n return 7 - (x % 7);\n}\n\nvoid insertEle(int x) {\n int index = hash1(x);\n int start = index;\n int i = 1;\n\n while (HashTable[index] != -1) {\n if (HashTable[index] == x) {\n cout << x << \" already exists in the hash table.\" << endl;\n return;\n }\n\n index = (start + i * hash2(x)) % SIZE;\n i++;\n\n if (index == start) {\n cout << \"Hash table is full.\" << endl;\n return;\n }\n }\n\n HashTable[index] = x;\n cout << \"Successfully inserted.\" << endl;\n}\n\nvoid deleteEle(int x) {\n int index = hash1(x);\n int start = index;\n int i = 1;\n\n while (HashTable[index] != x) {\n index = (start + i * hash2(x)) % SIZE;\n i++;\n\n if (index == start) {\n cout << \"Element not found. So cannot delete the element.\" << endl;\n return;\n }\n }\n\n HashTable[index] = -1;\n cout << \"Successfully deleted.\" << endl;\n}\n\nvoid searchEle(int x) {\n int index = hash1(x);\n int start = index;\n int i = 1;\n\n while (HashTable[index] != x) {\n index = (start + i * hash2(x)) % SIZE;\n i++;\n\n if (index == start) {\n cout << \"Element not found.\" << endl;\n return;\n }\n }\n\n cout << \"Element found.\" << endl;\n}\n\nvoid print() {\n for (int i = 0; i < SIZE; i++) {\n cout << \"[\" << i << \"]=>\" << HashTable[i] << endl;\n }\n}",
119
+ "python_code": "SIZE = 10\nHashTable = [-1] * SIZE\n\ndef hash1(x):\n return x % SIZE\n\ndef hash2(x):\n return 7 - (x % 7)\n\ndef insertEle(x):\n index = hash1(x)\n start = index\n i = 1\n\n while HashTable[index] != -1:\n if HashTable[index] == x:\n print(f\"{x} already exists in the hash table.\")\n return\n\n index = (start + i * hash2(x)) % SIZE\n i += 1\n\n if index == start:\n print(\"Hash table is full.\")\n return\n\n HashTable[index] = x\n print(\"Successfully inserted.\")\n\ndef deleteEle(x):\n index = hash1(x)\n start = index\n i = 1\n\n while HashTable[index] != x:\n index = (start + i * hash2(x)) % SIZE\n i += 1\n\n if index == start:\n print(\"Element not found. So cannot delete the element.\")\n return\n\n HashTable[index] = -1\n print(\"Successfully deleted.\")\n\ndef searchEle(x):\n index = hash1(x)\n start = index\n i = 1\n\n while HashTable[index] != x:\n index = (start + i * hash2(x)) % SIZE\n i += 1\n\n if index == start:\n print(\"Element not found.\")\n return\n\n print(\"Element found.\")\n\ndef print_table():\n for i in range(SIZE):\n print(f\"[{i}]=>{HashTable[i]}\")"
120
+ },
121
+ 24: {
122
+ "title": "Hash Table with Linear Probing",
123
+ "code": "#include <iostream>\nusing namespace std;\n\n#define SIZE 10\nint HashTable[SIZE];\n\nint hash_function(int x) {\n\treturn x % SIZE;\n}\n\nvoid insertEle(int x) {\n\tint index = hash_function(x);\n\tint start = index;\n\twhile (HashTable[index] != -1) {\n\t\tindex = (index + 1) % SIZE;\n\t\tif (index == start) {\n\t\t\tcout << \"Hash table is full.\\n\";\n\t\t\treturn;\n\t\t}\n\t}\n\tHashTable[index] = x;\n\tcout << \"Successfully inserted.\\n\";\n}\n\nvoid deleteEle(int x) {\n\tint index = hash_function(x);\n\tint start = index;\n\twhile (HashTable[index] != x) {\n\t\tindex = (index + 1) % SIZE;\n\t\tif (index == start) {\n\t\t\tcout << \"Element not found. So cannot delete the element.\\n\";\n\t\t\treturn;\n\t\t}\n\t}\n\tHashTable[index] = -1;\n\tcout << \"Successfully deleted.\\n\";\n}\n\nvoid searchEle(int x) {\n\tint index = hash_function(x);\n\tint start = index;\n\twhile (HashTable[index] != x) {\n\t\tindex = (index + 1) % SIZE;\n\t\tif (index == start) {\n\t\t\tcout << \"Element not found.\\n\";\n\t\t\treturn;\n\t\t}\n\t}\n\tcout << \"Element found.\\n\";\n}\n\nvoid print() {\n\tfor (int i = 0; i < SIZE; i++) {\n\t\tcout << \"[\" << i << \"]=>\" << HashTable[i] << endl;\n\t}\n}",
124
+ "python_code": "SIZE = 10\nHashTable = [-1] * SIZE\n\ndef hash_function(x):\n return x % SIZE\n\ndef insertEle(x):\n index = hash_function(x)\n start = index\n while HashTable[index] != -1:\n index = (index + 1) % SIZE\n if index == start:\n print(\"Hash table is full.\")\n return\n HashTable[index] = x\n print(\"Successfully inserted.\")\n\ndef deleteEle(x):\n index = hash_function(x)\n start = index\n while HashTable[index] != x:\n index = (index + 1) % SIZE\n if index == start:\n print(\"Element not found. So cannot delete the element.\")\n return\n HashTable[index] = -1\n print(\"Successfully deleted.\")\n\ndef searchEle(x):\n index = hash_function(x)\n start = index\n while HashTable[index] != x:\n index = (index + 1) % SIZE\n if index == start:\n print(\"Element not found.\")\n return\n print(\"Element found.\")\n\ndef print_table():\n for i in range(SIZE):\n print(f\"[{i}]=>{HashTable[i]}\")"
125
+ },
126
+ 25: {
127
+ "title": "Hash Table with Quadratic Probing",
128
+ "code": "#include <iostream>\nusing namespace std;\n\n#define SIZE 10\nint HashTable[SIZE];\n\nint hash_function(int x) {\n\treturn x % SIZE;\n}\n\nvoid insertEle(int x) {\n\tint i = 1, index, start;\n\tindex = hash_function(x);\n\tstart = index;\n\twhile (HashTable[index] != -1) {\n\t\tindex = (start + i * i) % SIZE;\n\t\ti++;\n\t\tif (index == start) {\n\t\t\tcout << \"Hash table is full.\" << endl;\n\t\t\treturn;\n\t\t}\n\t}\n\tHashTable[index] = x;\n\tcout << \"Successfully inserted.\" << endl;\n}\n\nvoid deleteEle(int x) {\n\tint i = 1, index, start;\n\tindex = hash_function(x);\n\tstart = index;\n\twhile (HashTable[index] != x) {\n\t\tindex = (start + i * i) % SIZE;\n\t\ti++;\n\t\tif (index == start) {\n\t\t\tcout << \"Element not found. So cannot delete the element.\" << endl;\n\t\t\treturn;\n\t\t}\n\t}\n\tHashTable[index] = -1;\n\tcout << \"Successfully deleted.\" << endl;\n}\n\nvoid searchEle(int x) {\n\tint i = 1, index, start;\n\tindex = hash_function(x);\n\tstart = index;\n\twhile (HashTable[index] != x) {\n\t\tindex = (start + i * i) % SIZE;\n\t\ti++;\n\t\tif (index == start) {\n\t\t\tcout << \"Element not found.\" << endl;\n\t\t\treturn;\n\t\t}\n\t}\n\tcout << \"Element found.\" << endl;\n}\n\nvoid print() {\n\tfor (int i = 0; i < SIZE; i++) {\n\t\tcout << \"[\" << i << \"]=>\" << HashTable[i] << endl;\n\t}\n}",
129
+ "python_code": "SIZE = 10\nHashTable = [-1] * SIZE\n\ndef hash_function(x):\n return x % SIZE\n\ndef insertEle(x):\n i = 1\n index = hash_function(x)\n start = index\n while HashTable[index] != -1:\n index = (start + i * i) % SIZE\n i += 1\n if index == start:\n print(\"Hash table is full.\")\n return\n HashTable[index] = x\n print(\"Successfully inserted.\")\n\ndef deleteEle(x):\n i = 1\n index = hash_function(x)\n start = index\n while HashTable[index] != x:\n index = (start + i * i) % SIZE\n i += 1\n if index == start:\n print(\"Element not found. So cannot delete the element.\")\n return\n HashTable[index] = -1\n print(\"Successfully deleted.\")\n\ndef searchEle(x):\n i = 1\n index = hash_function(x)\n start = index\n while HashTable[index] != x:\n index = (start + i * i) % SIZE\n i += 1\n if index == start:\n print(\"Element not found.\")\n return\n print(\"Element found.\")\n\ndef print_table():\n for i in range(SIZE):\n print(f\"[{i}]=>{HashTable[i]}\")"
130
+ },
131
+ 26: {
132
+ "title": "Count Frequencies of Elements using Hashing",
133
+ "code": "#include <iostream>\n#include <vector>\n#include <map>\nusing namespace std;\n\nint main(){\n\tint n;\n\tcin >> n;\n\tmap<int,int> mp;\n\tvector<int> occ;\n\tfor(int i=0;i<n;i++){\n\t\tint ele;\n\t\tcin >> ele;\n\t\tif(mp.find(ele)==mp.end()){\n\t\t\tocc.push_back(ele);\n\t\t}\n\t\tmp[ele]++;\n\t}\n\tfor(int i : occ){\n\t\tcout << i << \":\" << mp[i] << endl;\n\t}\n\treturn 0;\n}",
134
+ "python_code": "def main():\n n = int(input())\n mp = {}\n occ = []\n\n arr = list(map(int, input().split()))\n\n for ele in arr:\n if ele not in mp:\n occ.append(ele)\n mp[ele] = 0\n mp[ele] += 1\n\n for i in occ:\n print(i, \":\", mp[i], sep=\"\")\n\nif __name__ == \"__main__\":\n main()"
135
+ },
136
+ 27: {
137
+ "title": "Check Nearby Duplicates using Hashing",
138
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n\tint n;\n\tcin >> n;\n\tvector<int> nums;\n\tfor(int i=0;i<n;i++){\n\t\tint num;\n\t\tcin >> num;\n\t\tnums.push_back(num);\n\t}\n\tint k;\n\tcin>>k;\n\tint i=0,j=k;\n\tbool isfound=false;\n\tfor (int i=0; i<nums.size();i++) {\n\t\tfor (int j=i+1; j<=i+k && j<nums.size();j++) {\n\t\t\tif (nums[i] == nums[j]) {\n\t\t\t\tisfound = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (isfound) break;\n\t}\n\n\tif (isfound) {\n\t\tcout << \"True\\n\";\n\t} else {\n\t\tcout << \"False\\n\";\n\t}\n\treturn 0;\n}",
139
+ "python_code": "def main():\n n = int(input())\n nums = list(map(int, input().split()))\n k = int(input())\n\n isfound = False\n\n for i in range(len(nums)):\n for j in range(i + 1, min(i + k + 1, len(nums))):\n if nums[i] == nums[j]:\n isfound = True\n break\n if isfound:\n break\n\n if isfound:\n print(\"True\")\n else:\n print(\"False\")\n\nif __name__ == \"__main__\":\n main()"
140
+ },
141
+ 28: {
142
+ "title": "Check for Pair with Given Sum using Hashing",
143
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int n;\n cin >> n;\n\n vector<int> arr(n);\n\n for (int i = 0; i < n; i++) {\n cin >> arr[i];\n }\n\n int target;\n cin >> target;\n\n set<int> seen;\n\n for (int x : arr) {\n if (seen.find(target - x) != seen.end()) {\n cout << \"Yes\" << endl;\n return 0;\n }\n\n seen.insert(x);\n }\n\n cout << \"No\" << endl;\n\n return 0;\n}",
144
+ "python_code": "def main():\n n = int(input())\n arr = list(map(int, input().split()))\n target = int(input())\n\n seen = set()\n\n for x in arr:\n if target - x in seen:\n print(\"Yes\")\n break\n seen.add(x)\n else:\n print(\"No\")\n\nif __name__ == \"__main__\":\n main()"
145
+ },
146
+ 29: {
147
+ "title": "Count Distinct Elements in Every Window of Size K",
148
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n\tint n;\n\tcin >> n;\n\tvector<int> arr;\n\tfor(int i=0;i<n;i++){\n\t\tint num;\n\t\tcin >> num;\n\t\tarr.push_back(num);\n\t}\n\tint key;\n\tcin >> key;\n\tint i=0,j=key-1;\n\tvector<int> ans;\n\twhile(j<=arr.size()-1){\n\t\tset<int> st;\n\t\tint temp = i;\n\t\twhile(temp<=j){\n\t\t\tst.insert(arr[temp]);\n\t\t\ttemp++;\n\t\t}\n\t\tans.push_back(st.size());\n\t\ti++;\n\t\tj++;\n\t}\n\tfor(int val : ans){\n\t\tcout << val << endl;\n\t}\n\treturn 0;\n}",
149
+ "python_code": "def main():\n n = int(input())\n arr = list(map(int, input().split()))\n key = int(input())\n\n i = 0\n j = key - 1\n\n ans = []\n\n while j <= len(arr) - 1:\n st = set()\n temp = i\n\n while temp <= j:\n st.add(arr[temp])\n temp += 1\n\n ans.append(len(st))\n\n i += 1\n j += 1\n\n for val in ans:\n print(val)\n\nif __name__ == \"__main__\":\n main()"
150
+ },
151
+ 30: {
152
+ "title": "Top K Frequent Elements",
153
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n\tint n;\n\tcin >> n;\n\tmap<int,int> mp;\n\tfor(int i=0;i<n;i++){\n\t\tint num;\n\t\tcin >> num;\n\t\tmp[num]++;\n\t}\n\tint k;\n\tcin >> k;\n\tint i=0;\n\tfor(auto& n : mp){\n\t\tif(i>=k) break;\n\t\ti++;\n\t\tcout << n.first << \" \";\n\t}\n\treturn 0;\n}",
154
+ "python_code": "def main():\n n = int(input())\n arr = list(map(int, input().split()))\n k = int(input())\n\n freq = {}\n first = {}\n\n for i in range(n):\n num = arr[i]\n\n if num not in freq:\n freq[num] = 0\n first[num] = i\n\n freq[num] += 1\n\n elements = list(freq.keys())\n elements.sort(key=lambda x: (-freq[x], first[x]))\n\n for i in range(k):\n print(elements[i], end=\" \")\n\nif __name__ == \"__main__\":\n main()"
155
+ },
156
+ 31: {
157
+ "title": "Group Anagrams",
158
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int n;\n cin >> n;\n\n map<string, vector<string>> groups;\n\n for (int i = 0; i < n; i++) {\n string s;\n cin >> s;\n\n string key = s;\n sort(key.begin(), key.end());\n\n groups[key].push_back(s);\n }\n\n vector<vector<string>> ans;\n\n for (auto &p : groups) {\n ans.push_back(p.second);\n }\n\n sort(ans.begin(), ans.end(), [](vector<string> &a, vector<string> &b) {\n return a[0] < b[0];\n });\n\n for (auto &group : ans) {\n for (string s : group) {\n cout << s << \" \";\n }\n cout << endl;\n }\n\n return 0;\n}",
159
+ "python_code": "def main():\n n = int(input())\n\n groups = {}\n\n for s in input().split():\n key = ''.join(sorted(s))\n\n if key not in groups:\n groups[key] = []\n\n groups[key].append(s)\n\n groups = list(groups.values())\n groups.sort(key=lambda group: group[0])\n\n for group in groups:\n print(\" \".join(group), end=\" \\n\")\n\nif __name__ == \"__main__\":\n main()"
160
+ },
161
+ 32: {
162
+ "title": "Find Pair of Numbers with Target Sum",
163
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n\tint n;\n\tcin >> n;\n\tvector<int> v;\n\tfor(int i=0;i<n;i++){\n\t\tint num;\n\t\tcin >> num;\n\t\tv.push_back(num);\n\t}\n\tint target;\n\tcin >> target;\n\tfor(int i=0;i<n;i++){\n\t\tfor(int j=i+1;j<n;j++){\n\t\t\tif(v[i]+v[j]==target){\n\t\t\t\tcout << i << \" \"<< j << endl;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\treturn 0;\n}",
164
+ "python_code": "def main():\n n = int(input())\n\n v = list(map(int, input().split()))\n\n target = int(input())\n\n for i in range(n):\n for j in range(i + 1, n):\n if v[i] + v[j] == target:\n print(i, j)\n break\n\nif __name__ == \"__main__\":\n main()"
165
+ },
166
+ 33: {
167
+ "title": "Hash Table with Separate Chaining",
168
+ "code": "#include <iostream>\nusing namespace std;\n\n#define SIZE 10\n\nstruct Node {\n int data;\n Node* next;\n};\n\nNode* table[SIZE];\n\nint hash_func(int x) {\n return x % SIZE;\n}\n\nNode* newNode(int x) {\n Node* temp = new Node;\n temp->data = x;\n temp->next = NULL;\n return temp;\n}\n\nvoid insertEle(int x) {\n int index = hash_func(x);\n Node* temp = newNode(x);\n temp->next = table[index];\n table[index] = temp;\n}\n\nvoid deleteEle(int x) {\n int index = hash_func(x);\n Node* temp = table[index];\n Node* prev = NULL;\n\n if (temp == NULL) {\n cout << \"Element not found. So cannot delete.\" << endl;\n return;\n }\n\n if (temp->data == x) {\n table[index] = temp->next;\n delete temp;\n cout << \"Successfully deleted.\" << endl;\n return;\n }\n\n while (temp != NULL && temp->data != x) {\n prev = temp;\n temp = temp->next;\n }\n\n if (temp == NULL) {\n cout << \"Element not found. So cannot delete.\" << endl;\n return;\n }\n\n prev->next = temp->next;\n delete temp;\n cout << \"Successfully deleted.\" << endl;\n}\n\nvoid searchEle(int x) {\n int index = hash_func(x);\n Node* temp = table[index];\n while (temp != NULL && temp->data != x) {\n temp = temp->next;\n }\n if (temp == NULL) {\n cout << \"Element not found.\" << endl;\n return;\n }\n cout << \"Element found.\" << endl;\n}\n\nvoid display() {\n for (int i = 0; i < SIZE; i++) {\n Node* temp = table[i];\n cout << \"[\" << i << \"]=> \";\n while (temp != NULL) {\n cout << temp->data << \" \";\n temp = temp->next;\n }\n cout << endl;\n }\n}",
169
+ "python_code": "class Node:\n def __init__(self, data):\n self.data = data\n self.next = None\n\nSIZE = 10\ntable = [None] * SIZE\n\ndef hash_func(x):\n return x % SIZE\n\ndef insertEle(x):\n index = hash_func(x)\n temp = Node(x)\n temp.next = table[index]\n table[index] = temp\n\ndef deleteEle(x):\n index = hash_func(x)\n temp = table[index]\n prev = None\n\n if temp is None:\n print(\"Element not found. So cannot delete.\")\n return\n\n if temp.data == x:\n table[index] = temp.next\n print(\"Successfully deleted.\")\n return\n\n while temp is not None and temp.data != x:\n prev = temp\n temp = temp.next\n\n if temp is None:\n print(\"Element not found. So cannot delete.\")\n return\n\n prev.next = temp.next\n print(\"Successfully deleted.\")\n\ndef searchEle(x):\n index = hash_func(x)\n temp = table[index]\n while temp is not None and temp.data != x:\n temp = temp.next\n\n if temp is None:\n print(\"Element not found.\")\n return\n\n print(\"Element found.\")\n\ndef display():\n for i in range(SIZE):\n temp = table[i]\n print(f\"[{i}]=> \", end=\"\")\n while temp is not None:\n print(f\"{temp.data} \", end=\"\")\n temp = temp.next\n print()"
170
+ },
171
+ 34: {
172
+ "title": "Maximum XOR Subarray with Hashing",
173
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n int n; cin>>n;\n vector<int>a(n); for(int&x:a)cin>>x;\n int mx=-1,l=0,r=0;\n for(int i=0;i<n;i++){\n int x=0;\n for(int j=i;j<n;j++){\n x^=a[j];\n if(x>mx) mx=x,l=i,r=j;\n }\n }\n cout<<mx<<\" \"<<l<<\" \"<<r;\n}",
174
+ "python_code": "def main():\n n = int(input())\n a = [int(x) for x in input().split()]\n mx, l, r = -1, 0, 0\n for i in range(n):\n x = 0\n for j in range(i, n):\n x ^= a[j]\n if x > mx:\n mx, l, r = x, i, j\n print(f\"{mx} {l} {r}\")\n\nif __name__ == \"__main__\":\n main()"
175
+ },
176
+ 35: {
177
+ "title": "Least K Elements",
178
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n\tset<int> st;\n\tint n;\n\tcin >> n;\n\tfor(int i=0;i<n;i++){\n\t\tint ele;\n\t\tcin >> ele;\n\t\tst.insert(ele);\n\t}\n\tint key;\n\tcin >> key;\n\tvector<int> v;\n\tfor(auto& t: st ){\n\t\tv.emplace_back(t);\n\t}\n\tcout << \"[\";\n\tfor(int i=key-1;i>=0;i--){\n\t\tif(i==0){\n\t\t\tcout << v[i];\n\t\t}else{\n\t\t\tcout << v[i] << \", \"; \n\t\t}\n\t}\n\tcout << \"]\";\n}",
179
+ "python_code": "def main():\n n = int(input())\n\n arr = list(map(int, input().split()))\n\n st = set()\n\n for ele in arr:\n st.add(ele)\n\n key = int(input())\n\n v = sorted(st)\n\n print(\"[\", end=\"\")\n\n for i in range(key - 1, -1, -1):\n if i == 0:\n print(v[i], end=\"\")\n else:\n print(v[i], end=\", \")\n\n print(\"]\")\n\nif __name__ == \"__main__\":\n main()"
180
+ },
181
+ 36: {
182
+ "title": "Count Distinct Elements in Every Window of Size K",
183
+ "code": "#include<bits/stdc++.h>\nusing namespace std;\n\nint main(){\n int n,k; cin>>n;\n vector<int>a(n);\n for(int&x:a)cin>>x;\n cin>>k;\n\n for(int i=0;i<=n-k;i++){\n set<int>s(a.begin()+i,a.begin()+i+k);\n cout<<s.size()<<'\\n';\n }\n}",
184
+ "python_code": "def main():\n n = int(input())\n a = [int(x) for x in input().split()]\n k = int(input())\n for i in range(n - k + 1):\n st = set(a[i : i + k])\n print(len(st))\n\nif __name__ == \"__main__\":\n main()"
185
+ },
186
+ 37: {
187
+ "title": "Debugging Minimum Element using Divide and Conquer",
188
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint findMin(int arr[], int l, int r) {\n if (l == r)\n return arr[l];\n\n int m = (l+r) / 2;\n int leftMin = findMin(arr, l, m);\n int rightMin = findMin(arr, m+1, r);\n\n return min(leftMin, rightMin);\n}\n\nint main() {\n int n;\n cin >> n;\n int arr[n];\n for (int i = 0; i < n; i++) cin >> arr[i];\n cout << findMin(arr, 0, n - 1) << endl;\n return 0;\n}",
189
+ "python_code": "def find_min(arr, l, r):\n if l == r:\n return arr[l]\n m = (l + r) // 2\n left_min = find_min(arr, l, m)\n right_min = find_min(arr, m + 1, r)\n return min(left_min, right_min)\n\ndef main():\n n = int(input())\n arr = [int(x) for x in input().split()]\n print(find_min(arr, 0, n - 1))\n\nif __name__ == \"__main__\":\n main()"
190
+ },
191
+ 38: {
192
+ "title": "Debugging Merge Sorted Array",
193
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nvoid merge(std::vector<int>& nums1, int m, const std::vector<int>& nums2, int n) {\n\n int i = m-1;\n int j = n - 1;\n int k = m + n - 1;\n\n while (i >= 0 && j >= 0) {\n if (nums1[i] >= nums2[j]) {\n nums1[k--] = nums1[i--];\n } else {\n nums1[k--] = nums2[j--];\n }\n }\n\n while (j >= 0) {\n nums1[k--] = nums2[j--];\n }\n}\n\nint main() {\n int m, n;\n cin >> m >> n;\n vector<int> nums1(m + n);\n vector<int> nums2(n);\n for (int i = 0; i < m; i++) cin >> nums1[i];\n for (int i = 0; i < n; i++) cin >> nums2[i];\n merge(nums1, m, nums2, n);\n for (int x : nums1) cout << x << \" \";\n cout << endl;\n return 0;\n}",
194
+ "python_code": "def merge(nums1, m, nums2, n):\n i = m - 1\n j = n - 1\n k = m + n - 1\n while i >= 0 and j >= 0:\n if nums1[i] >= nums2[j]:\n nums1[k] = nums1[i]\n i -= 1\n else:\n nums1[k] = nums2[j]\n j -= 1\n k -= 1\n while j >= 0:\n nums1[k] = nums2[j]\n j -= 1\n k -= 1\n\ndef main():\n m, n = map(int, input().split())\n nums1_in = [int(x) for x in input().split()]\n nums2 = [int(x) for x in input().split()]\n nums1 = nums1_in + [0] * n\n merge(nums1, m, nums2, n)\n print(\" \".join(map(str, nums1)))\n\nif __name__ == \"__main__\":\n main()"
195
+ },
196
+ 39: {
197
+ "title": "Debugging Kth Smallest Element",
198
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint compare(const void* a, const void* b) {\n return (*(int*)a - *(int*)b);\n}\n\nint kthSmallestElement(vector<int>& arr, int k) {\n int n = arr.size();\n\n int* temp = new int[n];\n\n for (int i = 0; i < n; i++) {\n temp[i] = arr[i];\n }\n\n qsort(temp, n, sizeof(int), compare);\n\n int result = temp[k - 1];\n\n delete[] temp;\n return result;\n}\n\nint main() {\n int n, k;\n cin >> n >> k;\n vector<int> arr(n);\n for (int i = 0; i < n; i++) cin >> arr[i];\n cout << kthSmallestElement(arr, k) << endl;\n return 0;\n}",
199
+ "python_code": "def kth_smallest_element(arr, k):\n temp = sorted(arr)\n return temp[k - 1]\n\ndef main():\n n, k = map(int, input().split())\n arr = [int(x) for x in input().split()]\n print(kth_smallest_element(arr, k))\n\nif __name__ == \"__main__\":\n main()"
200
+ },
201
+ 40: {
202
+ "title": "Debugging Hash Table - Linear Probing",
203
+ "code": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nint main() {\n int n, m;\n cin >> n >> m;\n\n vector<int> keys(n);\n\n for (int i = 0; i < n; i++) {\n cin >> keys[i];\n }\n\n vector<int> T(m, -1);\n\n if (n == 0) {\n cout << \"Hash Table is empty\" << endl;\n return 0;\n }\n\n bool full = false;\n\n for (int i = 0; i < n; i++) {\n int key = keys[i];\n int index = key % m;\n int originalIndex = index;\n bool inserted = false;\n int attempts = 1;\n\n while (attempts <= m) {\n if (T[index] == -1) {\n T[index] = key;\n inserted = true;\n break;\n } else {\n index = (index + 1) % m;\n attempts++;\n }\n }\n\n if (inserted == false) {\n cout << \"Hash table is full. Cannot insert records from key \"\n << i + 1 << endl;\n full = true;\n break;\n }\n }\n\n for (int i = 0; i < m; i++) {\n cout << \"T[\" << i << \"]-> \" << T[i] << endl;\n }\n\n return 0;\n}",
204
+ "python_code": "def main():\n n, m = map(int, input().split())\n keys = [int(x) for x in input().split()]\n T = [-1] * m\n if n == 0:\n print(\"Hash Table is empty\")\n return\n full = False\n for i in range(n):\n key = keys[i]\n index = key % m\n inserted = False\n attempts = 1\n while attempts <= m:\n if T[index] == -1:\n T[index] = key\n inserted = True\n break\n else:\n index = (index + 1) % m\n attempts += 1\n if not inserted:\n print(f\"Hash table is full. Cannot insert records from key {i + 1}\")\n full = True\n break\n for i in range(m):\n print(f\"T[{i}]-> {T[i]}\")\n\nif __name__ == \"__main__\":\n main()"
205
+ },
206
+ 41: {
207
+ "title": "Quadratic Probing Technique",
208
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int n, m;\n cin >> n >> m;\n\n if (n == 0) {\n cout << \"Hash Table is empty\";\n } else {\n vector<int> table(m, -1);\n\n for (int i = 0; i < n; i++) {\n int key;\n cin >> key;\n\n int h = key % m;\n bool inserted = false;\n\n for (int j = 0; j < m; j++) {\n int index = (h + j * j) % m;\n\n if (table[index] == -1) {\n table[index] = key;\n inserted = true;\n break;\n }\n }\n\n if (!inserted) {\n cout << \"Hash table is full. Cannot insert records from key \" << key << endl;\n break;\n }\n }\n\n for (int i = 0; i < m; i++) {\n cout << \"T[\" << i << \"] -> \" << table[i] << endl;\n }\n }\n\n return 0;\n}",
209
+ "python_code": "def main():\n n, m = map(int, input().split())\n\n if n == 0:\n print(\"Hash Table is empty\")\n else:\n table = [-1] * m\n keys = list(map(int, input().split()))\n\n for key in keys:\n h = key % m\n inserted = False\n\n for i in range(m):\n index = (h + i * i) % m\n\n if table[index] == -1:\n table[index] = key\n inserted = True\n break\n\n if not inserted:\n print(\"Hash table is full. Cannot insert records from key\", key)\n break\n\n for i in range(m):\n print(f\"T[{i}] -> {table[i]}\")\n\nif __name__ == \"__main__\":\n main()"
210
+ },
211
+ 42: {
212
+ "title": "Linear Probing Technique",
213
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int n, m;\n cin >> n >> m;\n\n if (n == 0) {\n cout << \"Hash Table is empty\";\n } else {\n vector<int> table(m, -1);\n\n for (int i = 0; i < n; i++) {\n int key;\n cin >> key;\n\n int index = key % m;\n bool inserted = false;\n\n for (int j = 0; j < m; j++) {\n int pos = (index + j) % m;\n\n if (table[pos] == -1) {\n table[pos] = key;\n inserted = true;\n break;\n }\n }\n\n if (!inserted) {\n cout << \"Hash table is full. Cannot insert records from key \" << key << endl;\n break;\n }\n }\n\n for (int i = 0; i < m; i++) {\n cout << \"T[\" << i << \"] -> \" << table[i] << endl;\n }\n }\n\n return 0;\n}",
214
+ "python_code": "def main():\n n, m = map(int, input().split())\n\n if n == 0:\n print(\"Hash Table is empty\")\n else:\n table = [-1] * m\n keys = list(map(int, input().split()))\n\n for key in keys:\n index = key % m\n inserted = False\n\n for i in range(m):\n pos = (index + i) % m\n\n if table[pos] == -1:\n table[pos] = key\n inserted = True\n break\n\n if not inserted:\n print(f\"Hash table is full. Cannot insert records from key {key}\")\n break\n\n for i in range(m):\n print(f\"T[{i}] -> {table[i]}\")\n\nif __name__ == \"__main__\":\n main()"
215
+ },
216
+ 43: {
217
+ "title": "Strategic Battle",
218
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int t;\n cin >> t;\n\n double phi = (1 + sqrt(5.0)) / 2;\n vector<string> answers;\n\n for (int i = 0; i < t; i++) {\n int r, c;\n cin >> r >> c;\n\n int a = min(r, c);\n int b = max(r, c);\n\n int k = b - a;\n\n int losing_a = (int)(k * phi);\n\n if (a == losing_a) {\n answers.push_back(\"sami\");\n } else {\n answers.push_back(\"canthi\");\n }\n }\n\n for (string ans : answers) {\n cout << ans << endl;\n }\n\n return 0;\n}",
219
+ "python_code": "import math\n\ndef main():\n t = int(input())\n phi = (1 + math.sqrt(5)) / 2\n answers = []\n for _ in range(t):\n r, c = map(int, input().split())\n a = min(r, c)\n b = max(r, c)\n k = b - a\n losing_a = int(k * phi)\n if a == losing_a:\n answers.append(\"sami\")\n else:\n answers.append(\"canthi\")\n print(\"\\n\".join(answers))\n\nif __name__ == \"__main__\":\n main()"
220
+ },
221
+ 44: {
222
+ "title": "Maximum number of valid triplets",
223
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int n, m;\n cin >> n >> m;\n\n vector<int> freq(m + 1, 0);\n\n for (int i = 0; i < n; i++) {\n int x;\n cin >> x;\n\n if (x <= m) {\n freq[x]++;\n }\n }\n\n int ans = 0;\n\n for (int i = 1; i <= m; i++) {\n if (i + 2 <= m) {\n int count = min({freq[i], freq[i + 1], freq[i + 2]});\n\n ans += count;\n\n freq[i] -= count;\n freq[i + 1] -= count;\n freq[i + 2] -= count;\n }\n\n ans += freq[i] / 3;\n }\n\n cout << ans << endl;\n\n return 0;\n}",
224
+ "python_code": "def main():\n n, m = map(int, input().split())\n\n freq = [0] * (m + 1)\n\n for _ in range(n):\n x = int(input())\n if x <= m:\n freq[x] += 1\n\n ans = 0\n\n for i in range(1, m + 1):\n if i + 2 <= m:\n count = min(freq[i], freq[i + 1], freq[i + 2])\n ans += count\n\n freq[i] -= count\n freq[i + 1] -= count\n freq[i + 2] -= count\n ans += freq[i] // 3\n\n print(ans)\n\nif __name__ == \"__main__\":\n main()"
225
+ },
226
+ 45: {
227
+ "title": "Hash Table using Open Hashing Separate Chaining",
228
+ "code": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n int n;\n cin >> n;\n\n vector<int> table[10];\n\n // Insert initial keys\n for (int i = 0; i < n; i++) {\n int key;\n cin >> key;\n\n int index = key % 10;\n\n // Insert at front\n table[index].insert(table[index].begin(), key);\n }\n\n int q;\n cin >> q;\n\n vector<string> answers;\n\n for (int i = 0; i < q; i++) {\n int op, key;\n cin >> op >> key;\n\n int index = key % 10;\n\n if (op == 1) {\n // Insert\n table[index].insert(table[index].begin(), key);\n }\n else if (op == 2) {\n // Delete\n auto it = find(table[index].begin(), table[index].end(), key);\n\n if (it != table[index].end()) {\n table[index].erase(it);\n }\n }\n else if (op == 3) {\n // Search\n auto it = find(table[index].begin(), table[index].end(), key);\n\n if (it != table[index].end())\n answers.push_back(\"Found\");\n else\n answers.push_back(\"Not Found\");\n }\n }\n\n // Print search results\n for (string ans : answers) {\n cout << ans << endl;\n }\n\n // Print final hash table\n for (int i = 0; i < 10; i++) {\n cout << \"Bucket[\" << i << \"]:\";\n \n for (int key : table[i]) {\n cout << \" \" << key;\n }\n\n cout << endl;\n }\n\n return 0;\n}",
229
+ "python_code": "def main():\n n = int(input())\n\n table = [[] for _ in range(10)]\n\n # Insert initial keys\n keys = list(map(int, input().split()))\n\n for key in keys:\n index = key % 10\n table[index].insert(0, key)\n\n q = int(input())\n\n answers = []\n\n for _ in range(q):\n op, key = map(int, input().split())\n index = key % 10\n\n if op == 1:\n # Insert\n table[index].insert(0, key)\n\n elif op == 2:\n # Delete\n if key in table[index]:\n table[index].remove(key)\n\n elif op == 3:\n # Search\n if key in table[index]:\n answers.append(\"Found\")\n else:\n answers.append(\"Not Found\")\n\n # Print search results\n for ans in answers:\n print(ans)\n\n # Print hash table\n for i in range(10):\n print(f\"Bucket[{i}]:\", end=\"\")\n\n for key in table[i]:\n print(f\" {key}\", end=\"\")\n\n print()\n\nif __name__ == \"__main__\":\n main()"
230
+ },
231
+ 46: {
232
+ "title": "Longest Consecutive Number",
233
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n\tint n;\n\tcin >> n;\n\tvector<int> v;\n\tfor(int i=0;i<n;i++){\n\t\tint num;\n\t\tcin >> num;\n\t\tv.push_back(num);\n\t}\n\tsort(v.begin(),v.end());\n\tint len=(n>0) ? 1 : 0;\n\tint sum=1;\n\tfor(int i=1;i<v.size();i++){\n\t\tif(v[i]-v[i-1]==1){\n\t\t\tsum++;\n\t\t}else{\n\t\t\tsum=1;\n\t\t}\n\t\tlen=max(sum,len);\n\t}\n\tcout << len;\n\treturn 0;\n}",
234
+ "python_code": "def main():\n n = int(input())\n\n arr = list(map(int, input().split()))\n\n st = set(arr)\n\n length = 0\n\n for num in st:\n if num - 1 not in st:\n current = num\n count = 1\n\n while current + 1 in st:\n current += 1\n count += 1\n\n length = max(length, count)\n\n print(length)\n\nif __name__ == \"__main__\":\n main()"
235
+ },
236
+ 47: {
237
+ "title": "First Non Repeating character",
238
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n string s;\n cin >> s;\n unordered_map<char,int> mp;\n for (auto c : s) {\n mp[c]++;\n }\n for (char c : s) {\n if (mp[c] == 1) {\n cout << c;\n return 0;\n }\n }\n cout << -1;\n}",
239
+ "python_code": "def main():\n s = input()\n\n mp = {}\n\n for c in s:\n if c in mp:\n mp[c] += 1\n else:\n mp[c] = 1\n\n for c in s:\n if mp[c] == 1:\n print(c, end=\"\")\n break\n else:\n print(-1, end=\"\")\n\nif __name__ == \"__main__\":\n main()"
240
+ },
241
+ 48: {
242
+ "title": "Intersection of two sorted Array",
243
+ "code": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n\tvector<int> v;\n\tmap<int,int> mp;\n\tint n;\n\tcin >> n;\n\tfor(int i=0;i<n;i++){\n\t\tint num;\n\t\tcin >> num;\n\t\tmp[num]++;\n\t}\n\tint m;\n\tcin >> m;\n\tfor(int i=0;i<m;i++){\n\t\tint num;\n\t\tcin >> num;\n\t\tif(mp[num]){\n\t\t\tv.push_back(num);\n\t\t\tmp[num]--;\n\t\t}\n\t}\n\tif(v.size()>0){\n\t\tfor(int i : v){\n\t\t\tcout << i << \" \";\n\t\t}\n\t}\n\telse{\n\t\tcout << -1;\n\t}\n\t\n}",
244
+ "python_code": "def main():\n n = int(input())\n\n mp = {}\n\n arr = list(map(int, input().split()))\n\n for num in arr:\n if num in mp:\n mp[num] += 1\n else:\n mp[num] = 1\n\n m = int(input())\n\n b = list(map(int, input().split()))\n\n v = []\n\n for num in b:\n if mp.get(num, 0) > 0:\n v.append(num)\n mp[num] -= 1\n\n if len(v) > 0:\n for i in v:\n print(i, end=\" \")\n else:\n print(-1, end=\"\")\n\nif __name__ == \"__main__\":\n main()"
245
+ },
246
+ }
247
+
248
+ def dsa_search():
249
+ """Display the list of all available DSA problems in numerical order."""
250
+ print("=" * 70)
251
+ print(" pythontantra Available Problems")
252
+ print("=" * 70)
253
+ for num, data in PROBLEMS.items():
254
+ print(f"{num:2d}. {data['title']}")
255
+ print("=" * 70)
256
+ print("Use dsa_cpp(number) to retrieve C++ code or dsa_python(number) for Python code.")
257
+ print("=" * 70)
258
+
259
+ def dsa_cpp(problem_num):
260
+ """Retrieve and display the C++ implementation for a specific problem number."""
261
+ if not isinstance(problem_num, int) or problem_num not in PROBLEMS:
262
+ print(f"Error: Invalid problem number '{problem_num}'. Please provide a number between 1 and {len(PROBLEMS)}.")
263
+ return
264
+
265
+ data = PROBLEMS[problem_num]
266
+ print(f"// ======================================================================")
267
+ print(f"// Problem {problem_num}: {data['title']} (C++)")
268
+ print(f"// ======================================================================\n")
269
+ print(data['code'])
270
+
271
+ def dsa_python(problem_num):
272
+ """Retrieve and display the Python implementation for a specific problem number."""
273
+ if not isinstance(problem_num, int) or problem_num not in PROBLEMS:
274
+ print(f"Error: Invalid problem number '{problem_num}'. Please provide a number between 1 and {len(PROBLEMS)}.")
275
+ return
276
+
277
+ data = PROBLEMS[problem_num]
278
+ py_code = data.get('python_code', '')
279
+ print(f"# ======================================================================")
280
+ print(f"# Problem {problem_num}: {data['title']} (Python)")
281
+ print(f"# ======================================================================\n")
282
+ print(py_code)
283
+
284
+ # Aliases for backwards compatibility
285
+ pythontantra_search = dsa_search
286
+ pythontantra_get = dsa_cpp
287
+ codetantra_search = dsa_search
288
+ codetantra_get = dsa_cpp
289
+ get_binary_search_cpp = lambda: dsa_cpp(1)
290
+ get_quick_sort_cpp = lambda: dsa_cpp(3)
291
+
292
+ def dsa_mcq(search_query=None, limit=5):
293
+ """
294
+ Search for MCQs matching the search_query using fuzzy searching.
295
+ Displays matching questions along with their answers.
296
+ """
297
+ pkg_dir = os.path.dirname(os.path.abspath(__file__))
298
+ mcq_path = os.path.join(pkg_dir, "mcq_data.json")
299
+
300
+ if not os.path.exists(mcq_path):
301
+ mcq_path = os.path.join(os.path.dirname(pkg_dir), "pythontantra", "mcq_data.json")
302
+
303
+ if not os.path.exists(mcq_path):
304
+ mcq_path = os.path.join(os.getcwd(), "mcq_data.json")
305
+
306
+ if not os.path.exists(mcq_path):
307
+ print("Error: MCQ dataset 'mcq_data.json' could not be found.")
308
+ return
309
+
310
+ try:
311
+ with open(mcq_path, "r", encoding="utf-8") as f:
312
+ mcqs = json.load(f)
313
+ except Exception as e:
314
+ print(f"Error loading MCQ dataset: {e}")
315
+ return
316
+
317
+ if not search_query or not str(search_query).strip():
318
+ print("=" * 70)
319
+ print(f" pythontantra MCQs ({len(mcqs)} Available)")
320
+ print("=" * 70)
321
+ print("Usage: dsa_mcq('search query') to search for specific MCQs.\n")
322
+ print("Sample MCQs:")
323
+ for item in mcqs[:limit]:
324
+ print(f"Q{item['id']}. {item['question']}")
325
+ print(f" Answer: {item['correct_answer']}\n")
326
+ print("=" * 70)
327
+ return
328
+
329
+ q_clean = str(search_query).strip().lower()
330
+ q_words = [w for w in q_clean.split() if len(w) > 1]
331
+
332
+ results = []
333
+ for item in mcqs:
334
+ q_text = item.get("question", "")
335
+ ans_text = item.get("correct_answer", "")
336
+ full_text = f"{q_text} {ans_text}".lower()
337
+
338
+ # 1. SequenceMatcher ratio on question
339
+ seq_ratio = difflib.SequenceMatcher(None, q_clean, q_text.lower()).ratio()
340
+
341
+ # 2. Word overlap ratio
342
+ word_matches = sum(1 for w in q_words if w in full_text)
343
+ token_ratio = (word_matches / len(q_words)) if q_words else 0.0
344
+
345
+ # 3. Substring match
346
+ sub_ratio = 1.0 if q_clean in full_text else 0.0
347
+
348
+ score = max(sub_ratio, token_ratio * 0.75 + seq_ratio * 0.25)
349
+
350
+ if score > 0.15 or word_matches > 0:
351
+ results.append((score, item))
352
+
353
+ results.sort(key=lambda x: x[0], reverse=True)
354
+
355
+ print("=" * 70)
356
+ print(f" pythontantra MCQ Search Results for '{search_query}'")
357
+ print("=" * 70)
358
+
359
+ if not results:
360
+ print("No matching MCQs found. Try different keywords (e.g., 'binary search', 'time complexity', 'merge sort').")
361
+ else:
362
+ for idx, (score, item) in enumerate(results[:limit], 1):
363
+ print(f"{idx}. [ID: {item['id']}] {item['question']}")
364
+ print(f" Answer: {item['correct_answer']}\n")
365
+
366
+ print("=" * 70)
367
+
368
+ def dsa_help():
369
+ """Display comprehensive help and usage documentation for all pythontantra functions."""
370
+ doc = f"""
371
+ ======================================================================
372
+ pythontantra Help Documentation
373
+ ======================================================================
374
+
375
+ Welcome to pythontantra! A Python library for Data Structures & Algorithms
376
+ code implementations (C++ & Python) and interactive MCQ problem solving.
377
+
378
+ AVAILABLE COMMANDS:
379
+
380
+ 1. dsa_search()
381
+ Description:
382
+ Lists all available algorithm problems (1 to {len(PROBLEMS)}) with their
383
+ IDs and titles.
384
+ Usage:
385
+ >>> from pythontantra import dsa_search
386
+ >>> dsa_search()
387
+
388
+ 2. dsa_cpp(problem_num)
389
+ Description:
390
+ Prints the complete C++ implementation for the specified problem ID.
391
+ Parameters:
392
+ problem_num (int): Problem number (1 to {len(PROBLEMS)}).
393
+ Usage:
394
+ >>> from pythontantra import dsa_cpp
395
+ >>> dsa_cpp(1) # C++ code for Binary Search
396
+ >>> dsa_cpp(41) # C++ code for Quadratic Probing
397
+
398
+ 3. dsa_python(problem_num)
399
+ Description:
400
+ Prints the complete Python implementation for the specified problem ID.
401
+ Parameters:
402
+ problem_num (int): Problem number (1 to {len(PROBLEMS)}).
403
+ Usage:
404
+ >>> from pythontantra import dsa_python
405
+ >>> dsa_python(1) # Python code for Binary Search
406
+ >>> dsa_python(43) # Python code for Strategic Battle
407
+
408
+ 4. dsa_mcq(search_query=None, limit=5)
409
+ Description:
410
+ Perform fuzzy searching across the MCQ database (60+ questions)
411
+ matching topics, keywords, or question text.
412
+ Parameters:
413
+ search_query (str, optional): Keyword or topic (e.g. 'binary search', 'time complexity').
414
+ limit (int, optional): Max number of matching MCQs to display (default: 5).
415
+ Usage:
416
+ >>> from pythontantra import dsa_mcq
417
+ >>> dsa_mcq("binary search")
418
+ >>> dsa_mcq("worst case merge sort", limit=3)
419
+ >>> dsa_mcq() # Shows sample MCQs and usage guide
420
+
421
+ 5. dsa_help()
422
+ Description:
423
+ Displays this help and usage documentation guide.
424
+ Usage:
425
+ >>> from pythontantra import dsa_help
426
+ >>> dsa_help()
427
+
428
+ ======================================================================
429
+ """
430
+ print(doc.strip())
431
+
432
+ # Additional Aliases
433
+ pythontantra_mcq = dsa_mcq
434
+ pythontantra_help = dsa_help
435
+ codetantra_mcq = dsa_mcq
436
+ codetantra_help = dsa_help
437
+
438
+
@@ -0,0 +1,476 @@
1
+ [
2
+ {
3
+ "id": 1,
4
+ "question": "What does 'asymptotic notation' primarily describe about an algorithm?",
5
+ "correct_letter": "B",
6
+ "correct_answer": "(B) Its growth rate as input size becomes very large"
7
+ },
8
+ {
9
+ "id": 2,
10
+ "question": "Which notation gives an upper bound on the growth rate of a function?",
11
+ "correct_letter": "A",
12
+ "correct_answer": "(A) Big-O (O)"
13
+ },
14
+ {
15
+ "id": 3,
16
+ "question": "Which notation gives a tight bound (both upper and lower) on a function's growth?",
17
+ "correct_letter": "C",
18
+ "correct_answer": "(C) \u0398"
19
+ },
20
+ {
21
+ "id": 4,
22
+ "question": "Which notation gives a lower bound on the growth rate of a function?",
23
+ "correct_letter": "B",
24
+ "correct_answer": "(B) \u03a9"
25
+ },
26
+ {
27
+ "id": 5,
28
+ "question": "Arrange the following growth rates from slowest to fastest: O(n), O(log n), O(1), O(n^2).",
29
+ "correct_letter": "A",
30
+ "correct_answer": "(A) O(1) < O(log n) < O(n) < O(n^2)"
31
+ },
32
+ {
33
+ "id": 6,
34
+ "question": "Which of the following represents exponential growth?",
35
+ "correct_letter": "C",
36
+ "correct_answer": "(C) O(2^n)"
37
+ },
38
+ {
39
+ "id": 7,
40
+ "question": "An algorithm whose running time does not depend on the input size at all has which complexity?",
41
+ "correct_letter": "B",
42
+ "correct_answer": "(B) O(1)"
43
+ },
44
+ {
45
+ "id": 8,
46
+ "question": "Binary Search follows which algorithm design technique?",
47
+ "correct_letter": "C",
48
+ "correct_answer": "(C) Divide and Conquer"
49
+ },
50
+ {
51
+ "id": 9,
52
+ "question": "What is a strict prerequisite for applying standard Binary Search on an array?",
53
+ "correct_letter": "A",
54
+ "correct_answer": "(A) The array must be sorted"
55
+ },
56
+ {
57
+ "id": 10,
58
+ "question": "What is the time complexity of Binary Search in the worst case?",
59
+ "correct_letter": "B",
60
+ "correct_answer": "(B) O(log n)"
61
+ },
62
+ {
63
+ "id": 11,
64
+ "question": "What is the time complexity of Binary Search in the best case (target at the middle)?",
65
+ "correct_letter": "A",
66
+ "correct_answer": "(A) O(1)"
67
+ },
68
+ {
69
+ "id": 12,
70
+ "question": "In each step of Binary Search, the size of the search space is reduced by roughly:",
71
+ "correct_letter": "A",
72
+ "correct_answer": "(A) A factor of 2"
73
+ },
74
+ {
75
+ "id": 13,
76
+ "question": "Which of these best describes the 'Divide' step in Divide and Conquer?",
77
+ "correct_letter": "B",
78
+ "correct_answer": "(B) Breaking the problem into smaller sub-problems"
79
+ },
80
+ {
81
+ "id": 14,
82
+ "question": "Which of these best describes the 'Combine' step in Divide and Conquer?",
83
+ "correct_letter": "C",
84
+ "correct_answer": "(C) Merging sub-problem solutions into the overall solution"
85
+ },
86
+ {
87
+ "id": 15,
88
+ "question": "Merge Sort follows which algorithmic paradigm?",
89
+ "correct_letter": "B",
90
+ "correct_answer": "(B) Divide and Conquer"
91
+ },
92
+ {
93
+ "id": 16,
94
+ "question": "What is the worst-case time complexity of Merge Sort?",
95
+ "correct_letter": "B",
96
+ "correct_answer": "(B) O(n log n)"
97
+ },
98
+ {
99
+ "id": 17,
100
+ "question": "What is the best-case time complexity of standard Merge Sort?",
101
+ "correct_letter": "C",
102
+ "correct_answer": "(C) O(n log n)"
103
+ },
104
+ {
105
+ "id": 18,
106
+ "question": "What additional space does standard Merge Sort require (auxiliary space)?",
107
+ "correct_letter": "C",
108
+ "correct_answer": "(C) O(n)"
109
+ },
110
+ {
111
+ "id": 19,
112
+ "question": "Is Merge Sort a stable sorting algorithm?",
113
+ "correct_letter": "A",
114
+ "correct_answer": "(A) Yes"
115
+ },
116
+ {
117
+ "id": 20,
118
+ "question": "In Merge Sort, which step actually performs the comparisons between elements?",
119
+ "correct_letter": "B",
120
+ "correct_answer": "(B) The merge step"
121
+ },
122
+ {
123
+ "id": 21,
124
+ "question": "Quick Sort follows which algorithmic paradigm?",
125
+ "correct_letter": "A",
126
+ "correct_answer": "(A) Divide and Conquer"
127
+ },
128
+ {
129
+ "id": 22,
130
+ "question": "What is the key operation used in Quick Sort to divide the array?",
131
+ "correct_letter": "B",
132
+ "correct_answer": "(B) Partitioning around a pivot"
133
+ },
134
+ {
135
+ "id": 23,
136
+ "question": "What is the worst-case time complexity of Quick Sort?",
137
+ "correct_letter": "C",
138
+ "correct_answer": "(C) O(n^2)"
139
+ },
140
+ {
141
+ "id": 24,
142
+ "question": "What is the average-case time complexity of Quick Sort?",
143
+ "correct_letter": "A",
144
+ "correct_answer": "(A) O(n log n)"
145
+ },
146
+ {
147
+ "id": 25,
148
+ "question": "Is Quick Sort generally considered an in-place sorting algorithm?",
149
+ "correct_letter": "A",
150
+ "correct_answer": "(A) Yes, it uses O(log n) extra stack space typically"
151
+ },
152
+ {
153
+ "id": 26,
154
+ "question": "Why is Randomized Quick Sort introduced as an improvement over the basic version?",
155
+ "correct_letter": "B",
156
+ "correct_answer": "(B) It avoids worst-case behavior on already-sorted input by randomizing pivot choice"
157
+ },
158
+ {
159
+ "id": 27,
160
+ "question": "In Randomized Quick Sort, how is the pivot typically chosen?",
161
+ "correct_letter": "C",
162
+ "correct_answer": "(C) Randomly from the subarray"
163
+ },
164
+ {
165
+ "id": 28,
166
+ "question": "Which of the following sorting algorithms is NOT based on Divide and Conquer?",
167
+ "correct_letter": "D",
168
+ "correct_answer": "(D) Bubble Sort"
169
+ },
170
+ {
171
+ "id": 29,
172
+ "question": "What is the time complexity of the brute-force approach to finding the maximum subarray sum by checking all subarrays?",
173
+ "correct_letter": "C",
174
+ "correct_answer": "(C) O(n^2) or worse"
175
+ },
176
+ {
177
+ "id": 30,
178
+ "question": "The 'kth largest element' problem asks you to find:",
179
+ "correct_letter": "A",
180
+ "correct_answer": "(A) The kth element in sorted order counting from the largest"
181
+ },
182
+ {
183
+ "id": 31,
184
+ "question": "For an algorithm with recurrence T(n) = 2T(n/2) + O(n), what is T(n) by the Master Theorem?",
185
+ "correct_letter": "B",
186
+ "correct_answer": "(B) O(n log n)"
187
+ },
188
+ {
189
+ "id": 32,
190
+ "question": "For Binary Search, the recurrence relation is T(n) = T(n/2) + O(1). What is the resulting complexity?",
191
+ "correct_letter": "B",
192
+ "correct_answer": "(B) O(log n)"
193
+ },
194
+ {
195
+ "id": 33,
196
+ "question": "You are given a sorted array with duplicate values and need to find the first (starting) and last (ending) position of a target value in O(log n) time. Which approach works?",
197
+ "correct_letter": "A",
198
+ "correct_answer": "(A) Run two separate modified binary searches, one biased to find the leftmost occurrence and one for the rightmost"
199
+ },
200
+ {
201
+ "id": 34,
202
+ "question": "In the 'find first and last position' problem, if the target is not present in the array, what should be returned?",
203
+ "correct_letter": "B",
204
+ "correct_answer": "(B) [-1, -1]"
205
+ },
206
+ {
207
+ "id": 35,
208
+ "question": "What is the overall time complexity of finding both the first and last position of a target using two binary searches?",
209
+ "correct_letter": "A",
210
+ "correct_answer": "(A) O(log n)"
211
+ },
212
+ {
213
+ "id": 36,
214
+ "question": "An 'inversion' in an array arr is defined as a pair (i, j) such that:",
215
+ "correct_letter": "B",
216
+ "correct_answer": "(B) i < j and arr[i] > arr[j]"
217
+ },
218
+ {
219
+ "id": 37,
220
+ "question": "Counting inversions using Merge Sort achieves what time complexity, compared to the O(n^2) brute-force approach?",
221
+ "correct_letter": "A",
222
+ "correct_answer": "(A) O(n log n)"
223
+ },
224
+ {
225
+ "id": 38,
226
+ "question": "During the merge step of the inversion-counting algorithm, when an element from the right half is placed before remaining elements of the left half, how many inversions are added?",
227
+ "correct_letter": "B",
228
+ "correct_answer": "(B) The number of remaining elements in the left half"
229
+ },
230
+ {
231
+ "id": 39,
232
+ "question": "For Quick Sort using the last element as pivot, what input arrangement typically causes worst-case O(n^2) behavior?",
233
+ "correct_letter": "B",
234
+ "correct_answer": "(B) An already sorted (or reverse sorted) array"
235
+ },
236
+ {
237
+ "id": 40,
238
+ "question": "In Quick Sort's partition step (Lomuto scheme, last element as pivot), what does the partition function return?",
239
+ "correct_letter": "C",
240
+ "correct_answer": "(C) The index at which the pivot lands in its correct sorted position"
241
+ },
242
+ {
243
+ "id": 41,
244
+ "question": "Why does Randomized Quick Sort have an expected time complexity of O(n log n) regardless of the input's original order?",
245
+ "correct_letter": "B",
246
+ "correct_answer": "(B) Because a random pivot is unlikely to repeatedly produce a highly unbalanced partition"
247
+ },
248
+ {
249
+ "id": 42,
250
+ "question": "Which statement about comparing Quick Sort and Randomized Quick Sort execution time is generally true in practice?",
251
+ "correct_letter": "B",
252
+ "correct_answer": "(B) On already sorted/adversarial input, Randomized Quick Sort typically performs much better than the deterministic last-pivot version"
253
+ },
254
+ {
255
+ "id": 43,
256
+ "question": "Which sorting algorithm's worst-case complexity matches its average-case complexity: Merge Sort or Quick Sort?",
257
+ "correct_letter": "A",
258
+ "correct_answer": "(A) Merge Sort"
259
+ },
260
+ {
261
+ "id": 44,
262
+ "question": "The Divide and Conquer approach to the Maximum Subarray Sum problem divides the array into two halves and considers three cases. Which of these is NOT one of the three cases?",
263
+ "correct_letter": "D",
264
+ "correct_answer": "(D) Maximum subarray formed by sorting the array first"
265
+ },
266
+ {
267
+ "id": 45,
268
+ "question": "What is the time complexity of the Divide and Conquer solution to the Maximum Subarray Sum problem?",
269
+ "correct_letter": "B",
270
+ "correct_answer": "(B) O(n log n)"
271
+ },
272
+ {
273
+ "id": 46,
274
+ "question": "When finding the maximum crossing subarray (for Maximum Subarray Sum) in the Divide and Conquer approach, how is it computed?",
275
+ "correct_letter": "A",
276
+ "correct_answer": "(A) By scanning from the midpoint outward in both directions and summing, in O(n) time for that step"
277
+ },
278
+ {
279
+ "id": 47,
280
+ "question": "The Quickselect algorithm for finding the kth largest element is based on which sorting algorithm's partitioning idea?",
281
+ "correct_letter": "B",
282
+ "correct_answer": "(B) Quick Sort"
283
+ },
284
+ {
285
+ "id": 48,
286
+ "question": "What is the average time complexity of Quickselect for finding the kth largest element?",
287
+ "correct_letter": "A",
288
+ "correct_answer": "(A) O(n)"
289
+ },
290
+ {
291
+ "id": 49,
292
+ "question": "What is the worst-case time complexity of Quickselect (without randomization or median-of-medians)?",
293
+ "correct_letter": "C",
294
+ "correct_answer": "(C) O(n^2)"
295
+ },
296
+ {
297
+ "id": 50,
298
+ "question": "A heap-based approach to find the kth largest element using a min-heap of size k has what overall time complexity?",
299
+ "correct_letter": "A",
300
+ "correct_answer": "(A) O(n log k)"
301
+ },
302
+ {
303
+ "id": 51,
304
+ "question": "Given the recurrence T(n) = 2T(n/2) + O(n log n) (relevant to certain merge-based algorithms), what is the resulting time complexity by the Master Theorem (extended/Akra-Bazzi case)?",
305
+ "correct_letter": "B",
306
+ "correct_answer": "(B) O(n log^2 n)"
307
+ },
308
+ {
309
+ "id": 52,
310
+ "question": "Why can't standard Binary Search be directly applied to find the maximum subarray sum, even though Divide and Conquer is used for both problems?",
311
+ "correct_letter": "A",
312
+ "correct_answer": "(A) Binary Search requires sorted, single-value comparisons, while max subarray sum requires combining sums across a boundary region, which needs a different combine step"
313
+ },
314
+ {
315
+ "id": 53,
316
+ "question": "For Randomized Quick Sort, the expected number of comparisons over all possible pivot choices is O(n log n). Which technique is typically used to formally prove this bound?",
317
+ "correct_letter": "A",
318
+ "correct_answer": "(A) Indicator random variables and linearity of expectation over all pairs of elements"
319
+ },
320
+ {
321
+ "id": 54,
322
+ "question": "In the inversion-counting Merge Sort algorithm, if the left half has L elements remaining and an element from the right half is placed, the total inversions contributed by that placement is added to a running count. What is the overall recurrence for the total work done (including inversion counting) across the whole algorithm?",
323
+ "correct_letter": "A",
324
+ "correct_answer": "(A) T(n) = 2T(n/2) + O(n), giving O(n log n) total"
325
+ },
326
+ {
327
+ "id": 55,
328
+ "question": "Consider Quick Sort with the last element always chosen as pivot on an array that is already sorted in increasing order. What is the depth of the recursion tree, and why?",
329
+ "correct_letter": "B",
330
+ "correct_answer": "(B) O(n), because each partition removes only one element (the pivot lands at the end), producing maximally unbalanced splits"
331
+ },
332
+ {
333
+ "id": 56,
334
+ "question": "For the Divide and Conquer Maximum Subarray Sum algorithm, what is the tightest recurrence describing its running time, and what closed-form complexity does it yield?",
335
+ "correct_letter": "A",
336
+ "correct_answer": "(A) T(n) = 2T(n/2) + O(n) -> O(n log n)"
337
+ },
338
+ {
339
+ "id": 57,
340
+ "question": "Kadane's Algorithm solves the Maximum Subarray Sum problem in O(n) using dynamic programming instead of Divide and Conquer. Why might the Divide and Conquer O(n log n) version still be discussed academically despite being asymptotically slower?",
341
+ "correct_letter": "A",
342
+ "correct_answer": "(A) It illustrates how Divide and Conquer can be applied even when a linear-time alternative exists, reinforcing the general D&C; combine-step technique"
343
+ },
344
+ {
345
+ "id": 58,
346
+ "question": "When implementing Quickselect to find the kth largest element, using the 'median-of-medians' pivot selection strategy guarantees which worst-case time complexity, at the cost of higher constant factors?",
347
+ "correct_letter": "B",
348
+ "correct_answer": "(B) O(n)"
349
+ },
350
+ {
351
+ "id": 59,
352
+ "question": "For Randomized Quick Sort, what is the probability that a specific pair of elements (i-th and j-th smallest in the final sorted order) get compared during the entire algorithm, in terms of their rank distance?",
353
+ "correct_letter": "B",
354
+ "correct_answer": "(B) 2 / (j - i + 1), where the pair is compared only if one of them is chosen as a pivot before any element strictly between them"
355
+ },
356
+ {
357
+ "id": 60,
358
+ "question": "Given two algorithms for the same problem \u2014 one with worst-case O(n log n) (like Merge Sort) and one with worst-case O(n^2) but average-case O(n log n) (like standard Quick Sort) \u2014 under what circumstance would you prefer the O(n^2) worst-case algorithm in practice?",
359
+ "correct_letter": "B",
360
+ "correct_answer": "(B) When average-case performance, low memory overhead (in-place sorting), and good cache locality matter more than guaranteeing worst-case bounds, and worst-case triggers are unlikely or mitigated (e.g., via randomization)"
361
+ },
362
+ {
363
+ "id": 61,
364
+ "question": "What is the space complexity of the following C++ code snippet?\n\nint* arr = new int[n];",
365
+ "correct_letter": "C",
366
+ "correct_answer": "(C) O(n)"
367
+ },
368
+ {
369
+ "id": 62,
370
+ "question": "What is the time complexity of the recurrence relation T(n) = T(n-1) + 1?",
371
+ "correct_letter": "A",
372
+ "correct_answer": "(A) O(n)"
373
+ },
374
+ {
375
+ "id": 63,
376
+ "question": "Which of the following methods is used to solve recurrence relations?",
377
+ "correct_letter": "D",
378
+ "correct_answer": "(D) All of the options"
379
+ },
380
+ {
381
+ "id": 64,
382
+ "question": "Which of the following algorithms has a time complexity of O(1)?",
383
+ "correct_letter": "C",
384
+ "correct_answer": "(C) Accessing an element in an array by its index"
385
+ },
386
+ {
387
+ "id": 65,
388
+ "question": "What is the time complexity of the following C++ code snippet?\n\nint max = arr[0];\nfor (int i = 1; i < n; i++) {\n if (arr[i] > max) {\n max = arr[i];\n }\n}",
389
+ "correct_letter": "A",
390
+ "correct_answer": "(A) O(n)"
391
+ },
392
+ {
393
+ "id": 66,
394
+ "question": "What is the space complexity of the following C++ code snippet?\n\nint** matrix = new int*[n];\nfor (int i = 0; i < n; i++) {\n matrix[i] = new int[n];\n}",
395
+ "correct_letter": "D",
396
+ "correct_answer": "(D) O(n^2)"
397
+ },
398
+ {
399
+ "id": 67,
400
+ "question": "What is the primary goal of algorithm design?",
401
+ "correct_letter": "C",
402
+ "correct_answer": "(C) To solve a problem efficiently"
403
+ },
404
+ {
405
+ "id": 68,
406
+ "question": "What is the time complexity of the following C++ code snippet?\n\nfor (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n cout << i << \" \" << j << endl;\n }\n}",
407
+ "correct_letter": "C",
408
+ "correct_answer": "(C) O(n^2)"
409
+ },
410
+ {
411
+ "id": 69,
412
+ "question": "Which of the following is NOT a characteristic of an algorithm?",
413
+ "correct_letter": "D",
414
+ "correct_answer": "(D) Ambiguity"
415
+ },
416
+ {
417
+ "id": 70,
418
+ "question": "What does the Big O notation represent?",
419
+ "correct_letter": "B",
420
+ "correct_answer": "(B) The upper bound of an algorithm's time complexity"
421
+ },
422
+ {
423
+ "question": "Which hashing technique uses multiple hash functions to minimize collisions?",
424
+ "correct_letter": "A",
425
+ "correct_answer": "(A) Double hashing",
426
+ "id": 71
427
+ },
428
+ {
429
+ "question": "In open addressing, what is the worst-case time complexity of searching an element in a hash table?",
430
+ "correct_letter": "C",
431
+ "correct_answer": "(C) O(n)",
432
+ "id": 72
433
+ },
434
+ {
435
+ "question": "What is the technique used to resolve hash collisions by linking all keys with the same hash into a list?",
436
+ "correct_letter": "B",
437
+ "correct_answer": "(B) Separate chaining",
438
+ "id": 73
439
+ },
440
+ {
441
+ "question": "As the load factor increases in a hash table, what happens to the likelihood of collisions?",
442
+ "correct_letter": "A",
443
+ "correct_answer": "(A) It increases",
444
+ "id": 74
445
+ },
446
+ {
447
+ "question": "In open addressing, when a collision occurs, how does linear probing attempt to find an empty slot?",
448
+ "correct_letter": "B",
449
+ "correct_answer": "(B) Linearly probing to the next slot",
450
+ "id": 75
451
+ },
452
+ {
453
+ "question": "What is the worst-case time complexity of inserting an element in Separate Chaining when duplicate checks or unsorted linked lists are used?",
454
+ "correct_letter": "C",
455
+ "correct_answer": "(C) O(n)",
456
+ "id": 76
457
+ },
458
+ {
459
+ "question": "Double hashing is one of the best methods available for open addressing because it reduces both primary and secondary clustering.",
460
+ "correct_letter": "A",
461
+ "correct_answer": "(A) TRUE",
462
+ "id": 77
463
+ },
464
+ {
465
+ "question": "Which of the following is NOT a drawback of open addressing?",
466
+ "correct_letter": "D",
467
+ "correct_answer": "(D) More memory efficient than separate chaining",
468
+ "id": 78
469
+ },
470
+ {
471
+ "question": "In which scenario is quadratic probing preferred over linear probing in hash tables?",
472
+ "correct_letter": "B",
473
+ "correct_answer": "(B) When primary clustering needs to be reduced",
474
+ "id": 79
475
+ }
476
+ ]
@@ -0,0 +1,111 @@
1
+ Metadata-Version: 2.4
2
+ Name: pythontantra
3
+ Version: 1.0.0
4
+ Summary: A package containing C++ and Python implementations for common DSA problems
5
+ Home-page: https://github.com/GitUtk/dsa-pkg-pypi
6
+ Author: antigravity
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Operating System :: OS Independent
10
+ Requires-Python: >=3.6
11
+ Description-Content-Type: text/markdown
12
+ Dynamic: author
13
+ Dynamic: classifier
14
+ Dynamic: description
15
+ Dynamic: description-content-type
16
+ Dynamic: home-page
17
+ Dynamic: requires-python
18
+ Dynamic: summary
19
+
20
+ # pythontantra
21
+
22
+ A Python package providing clean C++ and Python implementations for **48** classic Data Structures and Algorithms (DSA) problems, along with a fuzzy-searchable **79+ MCQ bank** for exams, interviews, and revision.
23
+
24
+ ## Installation
25
+
26
+ ```bash
27
+ pip install pythontantra
28
+ ```
29
+
30
+ ## Quick Start
31
+
32
+ ```python
33
+ from pythontantra import dsa_search, dsa_cpp, dsa_python, dsa_mcq, dsa_help
34
+
35
+ # 1. Search & list all 48 available DSA problems
36
+ dsa_search()
37
+
38
+ # 2. Get C++ code for a specific problem (e.g. Problem 48)
39
+ dsa_cpp(48)
40
+
41
+ # 3. Get Python code for a specific problem (e.g. Problem 48)
42
+ dsa_python(48)
43
+
44
+ # 4. Fuzzy search MCQ bank (79+ questions with answers)
45
+ dsa_mcq("binary search")
46
+ dsa_mcq("double hashing")
47
+
48
+ # 5. Display interactive help manual
49
+ dsa_help()
50
+ ```
51
+
52
+ ## Available Functions
53
+
54
+ - `dsa_search()`: List all 48 available problems with their numerical IDs.
55
+ - `dsa_cpp(problem_num)`: Retrieve the C++ solution for problem number `problem_num` (1-48).
56
+ - `dsa_python(problem_num)`: Retrieve the Python solution for problem number `problem_num` (1-48).
57
+ - `dsa_mcq(search_query=None, limit=5)`: Fuzzy search across 79+ DSA Multiple Choice Questions by keyword/topic.
58
+ - `dsa_help()`: Displays comprehensive package help documentation.
59
+
60
+ *(Note: `pythontantra_search()`, `pythontantra_get()`, `pythontantra_mcq()`, and `pythontantra_help()` as well as legacy `codetantra_*` are also supported for backward compatibility).*
61
+
62
+ ## Available Problems (1 - 48)
63
+
64
+ 1. **Binary Search (Basic Iterative with Sorting)**
65
+ 2. **Merge Sort (Basic)**
66
+ 3. **Quick Sort (Basic)**
67
+ 4. **Binary Search using Divide and Conquer**
68
+ 5. **First and Last Occurrence of Element in a Sorted Array**
69
+ 6. **Merge Sort with Recursion & Comparison Tracking**
70
+ 7. **Inversion Pairs Count using Merge Sort**
71
+ 8. **Quick Sort with Calls & Comparison Tracking**
72
+ 9. **Contiguous Subarray with Largest Sum (Kadane's Algorithm)**
73
+ 10. **Kth Largest Element in an Array (Min Heap)**
74
+ 11. **Missing Number of the Sequence**
75
+ 12. **Merge Sorted Array**
76
+ 13. **Longest Binary Subarray after K Flips**
77
+ 14. **Sorting by Set Bit Count using Divide and Conquer**
78
+ 15. **Book Allocation with Minimum Maximum Load**
79
+ 16. **Minimum Element in Rotated Sorted Array**
80
+ 17. **Search in Rotated Sorted Array**
81
+ 18. **Koko Eating Bananas**
82
+ 19. **Single Number**
83
+ 20. **Kth Missing Positive Number**
84
+ 21. **Merge Two Sorted Linked Lists**
85
+ 22. **Basic Hash Table Implementation**
86
+ 23. **Hash Table with Double Hashing**
87
+ 24. **Hash Table with Linear Probing**
88
+ 25. **Hash Table with Quadratic Probing**
89
+ 26. **Count Frequencies of Elements using Hashing**
90
+ 27. **Check Nearby Duplicates using Hashing**
91
+ 28. **Check for Pair with Given Sum using Hashing**
92
+ 29. **Count Distinct Elements in Every Window of Size K**
93
+ 30. **Top K Frequent Elements**
94
+ 31. **Group Anagrams**
95
+ 32. **Find Pair of Numbers with Target Sum**
96
+ 33. **Hash Table with Separate Chaining**
97
+ 34. **Maximum XOR Subarray with Hashing**
98
+ 35. **Least K Elements**
99
+ 36. **Count Distinct Elements in Every Window of Size K**
100
+ 37. **Debugging Minimum Element using Divide and Conquer**
101
+ 38. **Debugging Merge Sorted Array**
102
+ 39. **Debugging Kth Smallest Element**
103
+ 40. **Debugging Hash Table - Linear Probing**
104
+ 41. **Quadratic Probing Technique**
105
+ 42. **Linear Probing Technique**
106
+ 43. **Strategic Battle**
107
+ 44. **Maximum number of valid triplets**
108
+ 45. **Hash Table using Open Hashing Separate Chaining**
109
+ 46. **Longest Consecutive Number**
110
+ 47. **First Non Repeating character**
111
+ 48. **Intersection of two sorted Array**
@@ -0,0 +1,7 @@
1
+ pythontantra/__init__.py,sha256=QiJj5JL0DxocxwFxyFJTGS--ipCXJnCLVugW3OxrREA,699
2
+ pythontantra/algorithms.py,sha256=CwLEja4EEKOrYdP-6rtE_9X0Bme6tKGu7QdJlrtB_NI,79321
3
+ pythontantra/mcq_data.json,sha256=D-o0J7ayt9ahuAHENJkroiA3ck2yNllT1S-OGgxrcYY,19288
4
+ pythontantra-1.0.0.dist-info/METADATA,sha256=UwAn-zf8nMPA4NA8d_kHt6zTDMlgW46EG7Rf6fExVFM,4169
5
+ pythontantra-1.0.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
6
+ pythontantra-1.0.0.dist-info/top_level.txt,sha256=7sb-8SCGgLemhyN4i9seqyjufxHIjQ8m0OTUQMa7fPQ,13
7
+ pythontantra-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ pythontantra