Banana-Leopard/TASTY huggingface dataset:
# A sorting based solution to find the
# minimum number of subsets of a set
# such that every subset contains distinct
# elements.
 
# function to count subsets such that all
# subsets have distinct elements.
def subset(ar, n):
 
    # take input and initialize res = 0
    res = 0
 
    # sort the array
    ar.sort()
 
    # traverse the input array and
    # find maximum frequency
    for i in range(0, n) :
        count = 1
 
        # for each number find its repetition / frequency
        for i in range(n - 1):
            if ar[i] == ar[i + 1]:
                count+=1
            else:
                break
             
        # update res
        res = max(res, count)
     
    return res
 
 
# Driver code
ar = [ 5, 6, 9, 3, 4, 3, 4 ]
n = len(ar)
print(subset(ar, n))
 
# This code is contributed by
# Smitha Dinesh Semwal

Time complexity: quadratic
Space complexity: constant
# A hashing based solution to find the
# minimum number of subsets of a set such 
# that every subset contains distinct
# elements.
 
# Function to count subsets such that
# all subsets have distinct elements.
def subset(arr, n):
     
    # Traverse the input array and
    # store frequencies of elements
    mp = {i:0 for i in range(10)}
    for i in range(n):
        mp[arr[i]] += 1
     
    # Find the maximum value in map.
    res = 0
    for key, value in mp.items():
        res = max(res, value)
 
    return res
 
# Driver code
if __name__ == '__main__':
    arr = [5, 6, 9, 3, 4, 3, 4]
    n = len(arr)
    print(subset(arr, n))
 
# This code is contributed by
# Surendra_Gangwar

Time complexity: linear
Space complexity: linear
# Python3 program to find minimum
# element to remove so no common
# element exist in both array
 
# To find no elements to remove
# so no common element exist
def minRemove(a, b, n, m):
 
    # To store count of array element
    countA = dict()
    countB = dict()
 
    # Count elements of a
    for i in range(n):
        countA[a[i]] = countA.get(a[i], 0) + 1
 
    # Count elements of b
    for i in range(n):
        countB[b[i]] = countB.get(b[i], 0) + 1
 
    # Traverse through all common
    # element, and pick minimum
    # occurrence from two arrays
    res = 0
    for x in countA:
        if x in countB.keys():
            res += min(countA[x],countB[x])
 
    # To return count of
    # minimum elements
    return res
 
# Driver Code
a = [ 1, 2, 3, 4 ]
b = [2, 3, 4, 5, 8 ]
n = len(a)
m = len(b)
print(minRemove(a, b, n, m))
 
# This code is contributed
# by mohit kumar

Time complexity: linear
Space complexity: linear
# Python implementation to
# count items common to both
# the lists but with different
# prices
 
# function to count items
# common to both
# the lists but with different prices
def countItems(list1, list2):
    count = 0
     
    # for each item of 'list1'
    # check if it is in 'list2'
    # but with a different price
    for i in list1:
        for j in list2:
 
            if i[0] == j[0] and i[1] != j[1]:
                count += 1
     
    # required count of items
    return count
 
# Driver program to test above
list1 = [("apple", 60), ("bread", 20),
            ("wheat", 50), ("oil", 30)]
list2 = [("milk", 20), ("bread", 15),
            ("wheat", 40), ("apple", 60)]
     
print("Count = ", countItems(list1, list2))
     
# This code is contributed by Ansu Kumari.

Time complexity: quadratic
Space complexity: constant
# Python3 implementation to count
# items common to both the lists
# but with different prices
 
# Details of an item
from ast import Str
from functools import cmp_to_key
 
class item:
    def __init__(self, name, price):
        self.name = name
        self.price = price
 
# Function to search 'str' in 'list2[]'.
# If it exists then price associated
# with 'str' in 'list2[]' is being
# returned else -1 is returned. Here
# binary search technique is being
# applied for searching
def binary_search(list2, low, high, str):
    while (low <= high):
        mid = ((low + high) // 2)
  
       # if true the item 'str' is in 'list2'
        # print(list2[mid].name,str)
        if (list2[mid].name == str):
            return list2[mid].price
        elif (list2[mid].name < str):
            low = mid + 1
        else:
            high = mid - 1
  
    # item 'str' is not
    # in 'list2'
    return -1
 
# Function to count items common to both
# the lists but with different prices
def custom_logic(a, b):
    return a.name == b.name
 
def countItems(list1, m, list2, n):
 
    # sort 'list2' in alphabetical
    # order of items name
    sorted(list2,key=cmp_to_key(custom_logic))
  
    # initial count
    count = 0
  
    for i in range(m):
   
        # get the price of item 'list1[i]'
        # from 'list2' if item in not
        # present in second list then -1
        # is being obtained
        r = binary_search(list2, 0,
                          n - 1,
                          list1[i].name)
  
        # if item is present in list2
        # with a different price
        if ((r != -1) and (r != list1[i].price)):
            count += 1
  
  # Required count of items
    return count
 
# Driver code   
list1=[item("apple", 60),
                  item("bread", 20),
                  item("wheat", 50),
                  item("oil", 30)]
list2=[item("milk", 20),
                  item("bread", 15),
                  item("wheat", 40),
                  item("apple", 60)]
m = len(list1)
n = len(list2)
print(f"Count = {countItems(list1, m,list2, n)}")
 
# This code is contributed by shinjanpatra

Time complexity: nlogn
Space complexity: constant
# Python3 implementation to count items common to both
# the lists but with different prices
 
# details of an item
class item:
    def __init__(self, name, price):
        self.name = name
        self.price = price
 
# function to count items common to both
# the lists but with different prices
def countItems(list1, m,list2,  n):
 
    # 'um' implemented as hash table that contains
    # item name as the key and price as the value
    # associated with the key
    um = {}
    count = 0
     
    # insert elements of 'list1' in 'um'
    for i in range(m):
        um[list1[i].name] = list1[i].price;
     
    # for each element of 'list2' check if it is
    # present in 'um' with a different price
    # value
    for i in range(n):  
        if ((um.get(list2[i].name) != None) and (um[list2[i].name] != list2[i].price)):
            count+=1
     
    # required count of items       
    return count       
 
 
# Driver program to test above   
list1=[item("apple", 60),
                  item("bread", 20),
                  item("wheat", 50),
                  item("oil", 30)]
list2=[item("milk", 20),
                  item("bread", 15),
                  item("wheat", 40),
                  item("apple", 60)]
m = len(list1)
n = len(list2)
   
print("Count = " ,countItems(list1, m, list2, n))
 
 
# This code is contributed by Abhijeet Kumar(abhijeet19403)

Time complexity: linear
Space complexity: linear
# Function to print common strings
# with minimum index sum
def find(list1, list2):
    res = [] # resultant list
    max_possible_sum = len(list1) + len(list2) - 2
 
    # iterating over sum in ascending order
    for sum in range(max_possible_sum + 1):
         
        # iterating over one list and check index
        # (Corresponding to given sum) in other list
        for i in range(sum + 1):
 
            # put common strings in resultant list
            if (i < len(list1) and
               (sum - i) < len(list2) and
                list1[i] == list2[sum - i]):
                res.append(list1[i])
 
        # if common string found then break as we are
        # considering index sums in increasing order.
        if (len(res) > 0):
            break
 
    # print the resultant list
    for i in range(len(res)):
        print(res[i], end = " ")
 
# Driver code
 
# Creating list1
list1 = []
list1.append("GeeksforGeeks")
list1.append("Udemy")
list1.append("Coursera")
list1.append("edX")
 
# Creating list2
list2 = []
list2.append("Codecademy")
list2.append("Khan Academy")
list2.append("GeeksforGeeks")
 
find(list1, list2)
 
# This code is contributed by Mohit Kumar

Time complexity: linear
Space complexity: linear
# Hashing based Python3 program to find
# common elements with minimum index sum
import sys
 
# Function to print common strings
# with minimum index sum
def find(list1, list2):
     
    # Mapping strings to their indices
    Map = {}
    for i in range(len(list1)):
        Map[list1[i]] = i
         
    # Resultant list
    res = []
     
    minsum = sys.maxsize
     
    for j in range(len(list2)):
        if list2[j] in Map:
             
            # If current sum is smaller
            # than minsum
            Sum = j + Map[list2[j]]
             
            if (Sum < minsum):
                minsum = Sum
                res.clear()
                res.append(list2[j])
  
            # If index sum is same then put this 
            # string in resultant list as well  
            else if (Sum == minsum):
                res.append(list2[j])
  
    # Print result
    print(*res, sep = " ")
  
# Driver code
  
# Creating list1
list1 = []
list1.append("GeeksforGeeks")
list1.append("Udemy")
list1.append("Coursera")
list1.append("edX")
  
# Creating list2
list2 = []
list2.append("Codecademy")
list2.append("Khan Academy")
list2.append("GeeksforGeeks")
find(list1, list2)
  
# This code is contributed by avanitrachhadiya2155

Time complexity: linear
Space complexity: linear
# Python 3 program to find a pair with
# given sum such that every element of
# pair is in different rows.
MAX = 100
 
# Function to find pair for given
# sum in matrix mat[][] --> given matrix
# n --> order of matrix
# sum --> given sum for which we
# need to find pair
def pairSum(mat, n, sum):
 
    # First sort all the rows
    # in ascending order
    for i in range(n):
        mat[i].sort()
 
    # Select i'th row and find pair for
    # element in i'th row in j'th row
    # whose summation is equal to given sum
    for i in range(n - 1):
        for j in range(i + 1, n):
            left = 0
            right = n - 1
            while (left < n and right >= 0):
                if ((mat[i][left] + mat[j][right]) == sum):
                    print( "(", mat[i][left],
                           ", ", mat[j][right], "), ",
                                            end = " ")
                    left += 1
                    right -= 1
                 
                else:
                    if ((mat[i][left] +
                         mat[j][right]) < sum):
                        left += 1
                    else:
                        right -= 1
 
# Driver Code
if __name__ == "__main__":
    n = 4
    sum = 11
    mat = [[1, 3, 2, 4],
           [5, 8, 7, 6],
           [9, 10, 13, 11],
           [12, 0, 14, 15]]
    pairSum(mat, n, sum)
 
# This code is contributed
# by ChitraNayal

Time complexity: cubic
Space complexity: constant
# A Program to prints common element
# in all rows of matrix
 
# Specify number of rows and columns
M = 4
N = 5
 
# prints common element in all
# rows of matrix
def printCommonElements(mat):
 
    mp = dict()
 
    # initialize 1st row elements
    # with value 1
    for j in range(N):
        mp[mat[0][j]] = 1
 
    # traverse the matrix
    for i in range(1, M):
        for j in range(N):
             
            # If element is present in the
            # map and is not duplicated in
            # current row.
            if (mat[i][j] in mp.keys() and
                             mp[mat[i][j]] == i):
                                  
            # we increment count of the
            # element in map by 1
                mp[mat[i][j]] = i + 1
 
                # If this is last row
                if i == M - 1:
                    print(mat[i][j], end = " ")
             
# Driver Code
mat = [[1, 2, 1, 4, 8],
       [3, 7, 8, 5, 1],
       [8, 7, 7, 3, 1],
       [8, 1, 2, 7, 9]]
 
printCommonElements(mat)
 
# This code is contributed
# by mohit kumar 29

Time complexity: quadratic
Space complexity: linear
# Python program to find all
# permutations of a given row
 
# Function to find all
# permuted rows of a given row r
def permutatedRows(mat, m, n, r):
 
 
    # Creating an empty set
    s=set()
 
    # Count frequencies of
    # elements in given row r
    for j in range(n):
        s.add(mat[r][j])   
 
    # Traverse through all remaining rows
    for i in range(m):
 
        # we do not need to check
        # for given row r
        if i == r:
            continue
 
        # initialize hash i.e
        # count frequencies
        # of elements in row i
        for j in range(n):
            if mat[i][j] not in s:
 
                # to avoid the case when last
                # element does not match
                j = j - 2
                break;
        if j + 1 != n:
            continue
        print(i)
             
     
 
# Driver program to run the case
m = 4
n = 4
r = 3
mat = [[3, 1, 4, 2],
       [1, 6, 9, 3],
       [1, 2, 3, 4],
       [4, 3, 2, 1]]
 
permutatedRows(mat, m, n, r)
 
# This code is contributed
# by Upendra Singh Bartwal.

Time complexity: quadratic
Space complexity: linear
# Python3 code to make a permutation
# of numbers from 1 to n using
# minimum changes.
 
def makePermutation (a, n):
 
    # Store counts of all elements.
    count = dict()
    for i in range(n):
        if count.get(a[i]):
            count[a[i]] += 1
        else:
            count[a[i]] = 1;
         
    next_missing = 1
    for i in range(n):
        if count[a[i]] != 1 or a[i] > n or a[i] < 1:
            count[a[i]] -= 1
             
            # Find next missing element to put
            # in place of current element.
            while count.get(next_missing):
                next_missing+=1
             
            # Replace with next missing and
            # insert the missing element in hash.
            a[i] = next_missing
            count[next_missing] = 1
 
# Driver Code
A = [ 2, 2, 3, 3 ]
n = len(A)
makePermutation(A, n)
 
for i in range(n):
    print(A[i], end = " ")
     
# This code is contributed by "Sharad_Bhardwaj".

Time complexity: nlogn
Space complexity: linear
# Python3 implementation of simple method
# to find count of pairs with given sum.
 
# Returns number of pairs in arr[0..n-1]
# with sum equal to 'sum'
 
 
def getPairsCount(arr, n, sum):
 
    count = 0  # Initialize result
 
    # Consider all possible pairs
    # and check their sums
    for i in range(0, n):
        for j in range(i + 1, n):
            if arr[i] + arr[j] == sum:
                count += 1
 
    return count
 
 
# Driver function
arr = [1, 5, 7, -1, 5]
n = len(arr)
sum = 6
print("Count of pairs is",
      getPairsCount(arr, n, sum))
 
# This code is contributed by Smitha Dinesh Semwal

Time complexity: quadratic
Space complexity: constant
# Python code to implement the approach
import bisect
 
# Function to find the count of pairs
 
 
def getPairsCount(arr, n, k):
    arr.sort()
    x, c = 0, 0
    for i in range(n-1):
        x = k-arr[i]
 
        # Lower bound from i+1
        y = bisect.bisect_left(arr, x, i+1, n)
 
        # Upper bound from i+1
        z = bisect.bisect(arr, x, i+1, n)
        c = c+z-y
    return c
 
 
# Driver function
arr = [1, 5, 7, -1, 5]
n = len(arr)
k = 6
 
# Function call
print("Count of pairs is", getPairsCount(arr, n, k))
 
# This code is contributed by Pushpesh Raj

Time complexity: nlogn
Space complexity: constant
# Python 3 implementation of simple method
# to find count of pairs with given sum.
import sys
 
# Returns number of pairs in arr[0..n-1]
# with sum equal to 'sum'
 
 
def getPairsCount(arr, n, sum):
 
    m = [0] * 1000
 
    # Store counts of all elements in map m
    for i in range(0, n):
        m[arr[i]] += 1
 
    twice_count = 0
 
    # Iterate through each element and increment
    # the count (Notice that every pair is
    # counted twice)
    for i in range(0, n):
 
        twice_count += m[sum - arr[i]]
 
        # if (arr[i], arr[i]) pair satisfies the
        # condition, then we need to ensure that
        # the count is  decreased by one such
        # that the (arr[i], arr[i]) pair is not
        # considered
        if (sum - arr[i] == arr[i]):
            twice_count -= 1
 
    # return the half of twice_count
    return int(twice_count / 2)
 
 
# Driver function
arr = [1, 5, 7, -1, 5]
n = len(arr)
sum = 6
 
print("Count of pairs is", getPairsCount(arr,
                                         n, sum))
 
# This code is contributed by
# Smitha Dinesh Semwal

Time complexity: linear
Space complexity: linear
# Python implementation of simple method to find count of
# pairs with given sum.
 
# Returns number of pairs in arr[0..n-1] with sum equal to 'sum'
 
 
def getPairsCount(arr, n, sum):
    unordered_map = {}
    count = 0
    for i in range(n):
        if sum - arr[i] in unordered_map:
            count += unordered_map[sum - arr[i]]
        if arr[i] in unordered_map:
            unordered_map[arr[i]] += 1
        else:
            unordered_map[arr[i]] = 1
    return count
 
 
# Driver code
arr = [1, 5, 7, -1, 5]
n = len(arr)
sum = 6
print('Count of pairs is', getPairsCount(arr, n, sum))
 
# This code is contributed by Manish Thapa

Time complexity: linear
Space complexity: linear
# A Python3 implementation to count
# quadruples from four sorted arrays
# whose sum is equal to a given value x
 
# function to count all quadruples
# from four sorted arrays whose sum
# is equal to a given value x
def countquadruples(arr1, arr2,
                     arr3, arr4, n, x):
    count = 0
 
    # generate all possible
    # quadruples from the four
    # sorted arrays
    for i in range(n):
        for j in range(n):
            for k in range(n):
                for l in range(n):
 
                    # check whether elements of
                    # quadruple sum up to x or not
                    if (arr1[i] + arr2[j] +
                        arr3[k] + arr4[l] == x):
                        count += 1
                         
    # required count of quadruples
    return count
 
# Driver Code
arr1 = [1, 4, 5, 6]
arr2 = [2, 3, 7, 8]
arr3 = [1, 4, 6, 10]
arr4 = [2, 4, 7, 8 ]
n = len(arr1)
x = 30
print("Count = ", countquadruples(arr1, arr2,
                                   arr3, arr4, n, x))
 
# This code is contributed
# by Shrikant13

Time complexity: np
Space complexity: constant
# Python implementation to count quadruples from
# four sorted arrays whose sum is equal to a
# given value x
 
# find the 'value' in the given array 'arr[]'
# binary search technique is applied
def isPresent(arr,low,high,value):
    while(low<=high):
        mid=(low+high)//2
        # 'value' found
        if(arr[mid]==value):
            return True
        elif(arr[mid]>value):
            high=mid-1
        else:
            low=mid+1
    # 'value' not found
    return False
 
# function to count all quadruples from four
# sorted arrays whose sum is equal to a given value x
 
def countQuadruples(arr1,arr2,arr3,arr4,n,x):
    count=0
     
    #generate all triplets from the 1st three arrays
    for i in range(n):
        for j in range(n):
            for k in range(n):
                # calculate the sum of elements in
                # the triplet so generated
                T=arr1[i]+arr2[j]+arr3[k]
                 
                # check if 'x-T' is present in 4th
                # array or not
                if(isPresent(arr4,0,n-1,x-T)):
                    # increment count
                    count=count+1
    # required count of quadruples
    return count
     
# Driver program to test above
 
# four sorted arrays each of size 'n'
arr1=[1, 4, 5, 6]
arr2=[2, 3, 7, 8]
arr3=[1, 4, 6, 10]
arr4=[2, 4, 7, 8]
 
n=len(arr1)
x=30
print("Count = {}".format(countQuadruples(arr1,arr2,arr3,arr4,n,x)))
 
# This code is contributed by Pushpesh Raj.

Time complexity: cubic
Space complexity: constant
# Python3 implementation to
# count quadruples from four
# sorted arrays whose sum is
# equal to a given value x
# count pairs from the two
# sorted array whose sum
# is equal to the given 'value'
def countPairs(arr1, arr2,
               n, value):
   
     count = 0
     l = 0
     r = n - 1
     
     # traverse 'arr1[]' from
     # left to right
     # traverse 'arr2[]' from
     # right to left
     while (l < n and r >= 0):
          sum = arr1[l] + arr2[r]
           
          # if the 'sum' is equal
          # to 'value', then
          # increment 'l', decrement
          # 'r' and increment 'count'
          if (sum == value):
               l += 1
               r -= 1
               count += 1
               
               # if the 'sum' is greater
               # than 'value', then decrement r
          elif (sum > value):
               r -= 1
               
          # else increment l
          else:
               l += 1
               
     # required count of pairs
     # print(count)
     return count
 
# function to count all quadruples
# from four sorted arrays whose sum
# is equal to a given value x
def countQuadruples(arr1, arr2,
                    arr3, arr4,
                    n, x):
     count = 0
     
     # generate all pairs from
     # arr1[] and arr2[]
     for i in range(0, n):
          for j in range(0, n):
             
               # calculate the sum of
               # elements in the pair
               # so generated
               p_sum = arr1[i] + arr2[j]
                 
               # count pairs in the 3rd
               # and 4th array having
               # value 'x-p_sum' and then
               # accumulate it to 'count
               count += int(countPairs(arr3, arr4,
                                       n, x - p_sum))
     # required count of quadruples
     return count
 
# Driver code
arr1 = [1, 4, 5, 6]
arr2 = [2, 3, 7, 8]
arr3 = [1, 4, 6, 10]
arr4 = [2, 4, 7, 8]
n = len(arr1)
x = 30
print("Count = ", countQuadruples(arr1, arr2,
                                  arr3, arr4,
                                  n, x))
 
# This code is contributed by Stream_Cipher

Time complexity: cubic
Space complexity: constant
# Python implementation to count quadruples from
# four sorted arrays whose sum is equal to a
# given value x
 
# function to count all quadruples from four sorted
# arrays whose sum is equal to a given value x
def countQuadruples(arr1, arr2, arr3, arr4, n, x):
    count = 0
     
    # unordered_map 'um' implemented as hash table
    # for <sum, frequency> tuples  
    m = {}
     
    # count frequency of each sum obtained from the
    # pairs of arr1[] and arr2[] and store them in 'um'
    for i in range(n):
        for j in range(n):
            if (arr1[i] + arr2[j]) in m:
                m[arr1[i] + arr2[j]] += 1
            else:
                m[arr1[i] + arr2[j]] = 1
     
    # generate pair from arr3[] and arr4[]
    for k in range(n):
        for l in range(n):
             
            # calculate the sum of elements in
            # the pair so generated
            p_sum = arr3[k] + arr4[l]
             
            # if 'x-p_sum' is present in 'um' then
            # add frequency of 'x-p_sum' to 'count'
            if (x - p_sum) in m:
                count += m[x - p_sum]
     
    # required count of quadruples
    return count
 
# Driver program to test above
 
# four sorted arrays each of size 'n'
arr1 = [1, 4, 5, 6]
arr2 = [2, 3, 7, 8 ]
arr3 = [1, 4, 6, 10]
arr4 = [2, 4, 7, 8 ]
 
n = len(arr1)
x = 30
print("Count =", countQuadruples(arr1, arr2, arr3, arr4, n, x))
 
# This code is contributed by avanitrachhadiya2155

Time complexity: quadratic
Space complexity: quadratic
# Python3 program for
# the above approach
arr = [ 10, 2, -2, -20, 10 ]
n = len(arr)
k = -10
res = 0
 
# Calculate all subarrays
for i in range(n):
    summ = 0           
    for j in range(i, n):
         
        # Calculate required sum
        summ += arr[j]
 
        # Check if sum is equal to
        # required sum
        if summ == k:
            res += 1
 
print(res)
 
# This code is contributed by kavan155gondalia

Time complexity: quadratic
Space complexity: constant