<jupyter_start><jupyter_text>Power Of A Number<jupyter_code>def power(x, n):
    if n == 0:
        return 1
    return x * power(x, n-1)

# Main
from sys import setrecursionlimit
setrecursionlimit(11000)
x, n=list(int(i) for i in input().strip().split(' '))
print(power(x, n))
def power(x, n):
    if n == 0:
        return 1
    smallPower = power(x, n/2)
    if n % 2 == 0:
        return smallPower * smallPower
    else:
        return x * smallPower * smallPower

# Main
#from sys import setrecursionlimit
#setrecursionlimit(11000)
#x, n=list(int(i) for i in input().strip().split(' '))
x = int(input())
n = int(input())
print(power(x, n))<jupyter_output><empty_output><jupyter_text>Array IntersectionYou have been given two integer arrays/list(ARR1 and ARR2) of size N and M, respectively. You need to print their intersection; An intersection for this problem can be defined when both the arrays/lists contain a particular value or to put it in other words, when there is a common value that exists in both the arrays/lists.Note :Input arrays/lists can contain duplicate elements.The intersection elements printed would be in the order they appear in the first sorted array/list(ARR1).<jupyter_code>def insertion_sort(arr, l):
    for i in range(1, l):
        key = arr[i]
        j = i - 1
        while(j >= 0 and arr[j] > key):
            arr[j+1] = arr[j]
            j = j - 1
        arr[j+1] = key

def intersection(arr1, arr2, n, m):
    insertion_sort(arr1, n)
    print(arr1)
    insertion_sort(arr2, m)
    print(arr2)
    i, j = 0, 0
    while i < n and j < m:
        if arr1[i] == arr2[j]:
            print(arr1[i], end=' ')
            i += 1
            j += 1
        elif arr1[i] < arr2[j]:
            i += 1
        else:
            j += 1

arr1 = [2, 6, 8, 5, 4, 3]
arr2 = [2, 3, 4, 7]
intersection(arr1, arr2, len(arr1), len(arr2))
def intersection(arr1, arr2, n, m):
    mergeSort(arr1)
    print(arr1)
    mergeSort(arr2)
    print(arr2)
    i, j = 0, 0
    while i < n and j < m:
        if arr1[i] == arr2[j]:
            print(arr1[i], end=' ')
            i += 1
            j += 1
        elif arr1[i] < arr2[j]:
            i += 1
        else:
            j += 1

arr1 = [2, 6, 8, 5, 4, 3]
arr2 = [2, 3, 4, 7]
intersection(arr1, arr2, len(arr1), len(arr2))<jupyter_output>[2, 3, 4, 5, 6, 8]
[2, 3, 4, 7]
2 3 4<jupyter_text>Array Equilibrium IndexFor a given array/list(ARR) of size 'N,' find and return the 'Equilibrium Index' of the array/list.Equilibrium Index of an array/list is an index 'i' such that the sum of elements at indices [0 to (i - 1)] is equal to the sum of elements at indices [(i + 1) to (N-1)]. One thing to note here is, the item at the index 'i' is not included in either part.If more than one equilibrium indices are present, then the index appearing first in left to right fashion should be returned. Negative one(-1) if no such index is present.Example:Let's consider an array/list Arr = [2, 3, 10, -10, 4, 2, 9]  of size, N = 7.There exist two equilibrium indices, one at 2 and another at 3.At index 2, the sum of all the elements to the left, [2 + 3] is 5, and the elements to its right, [-10 + 4 + 2 + 9] is also 5. Hence index 2 is an equilibrium index according to the condition we want to achieve. Mind it that we haven't included the item at index 2, which is 10, to either of the parts.Similarly, we can see at index 3, the elements to its left sum up to 15 and to the right, sum up to 15 either. Since index 2 comes early in the order, left to right, the answer would be 2.<jupyter_code>def equi(arr):
    rs, ls, ts = 0, 0, 0
    for i in range(0, len(arr), 1):
        ts += arr[i]
    index = 0
    while index < len(arr):
        rs = ts - ls - arr[index]
        if rs == ls:
            return index
        else:
            ls = ls + arr[index]
        index += 1
    return -1
    
arr = [1,3,4,2,2,5,4,1]
print(equi(arr))<jupyter_output>4<jupyter_text>Find the Unique ElementYou have been given an integer array/list(ARR) of size N. Where N is equal to [2M + 1].Now, in the given array/list, 'M' numbers are present twice and one number is present only once.You need to find and return that number which is unique in the array/list. Note:Unique element is always present in the array/list according to the given condition.<jupyter_code>def unique(arr):
    xor = 0
    for i in arr:
        xor = xor ^ i
    return xor

arr = [2, 3, 1, 6, 3, 6, 2]
unique(arr)<jupyter_output><empty_output><jupyter_text>Duplicate in arrayYou have been given an integer array/list(ARR) of size N which contains numbers from 0 to (N - 2). Each number is present at least once. That is, if N = 5, the array/list constitutes values ranging from 0 to 3, and among these, there is a single integer value that is present twice. You need to find and return that duplicate number present in the array.Note :Duplicate number is always present in the given array/list.<jupyter_code>def duplicate(arr, n):
    ts = 0
    for i in arr:
        ts += i
    ns = ((n-2)*(n-1))//2
    return ts - ns

arr = [0, 7, 2, 5, 4, 7, 1, 3, 6]
duplicate(arr, len(arr))<jupyter_output><empty_output><jupyter_text>Pair sum in arrayYou have been given an integer array/list(ARR) and a number 'num'. Find and return the total number of pairs in the array/list which sum to 'num'.Note: Given array/list can contain duplicate elements.<jupyter_code>def pair_sum(arr, n, sum):
    for i in range(0, n ):
        for j in range(i + 1, n ):
            if (arr[i] + arr[j] == sum):
                print("(", arr[i], ", ", arr[j], ")", sep = "")

arr = [2,2,2,2,2]
n = len(arr)
sum = 4
pair_sum(arr, n, sum)
def pair_sum(arr, n, sum):
    low = 0;
    high = len(arr) - 1;
    for i in range(1, n):
        if arr[0] != arr[i]:
            break
    else:
        return ((n)*(n-1))//2
    while (low < high):
        if (arr[low] + arr[high] == sum):
            print("The pair is : (", arr[low], ", ", arr[high], ")");
        if (arr[low] + arr[high] > sum):
            high -= 1;
        else:
            low += 1;

arr = [2,2,2,2,2]
#arr = [3,3,3,3,3,3]
#arr = [2, 3, 4, -2, 6, 8, 9, 11]
arr.sort()
n = len(arr)
sum = 7
pair_sum(arr, n, sum)
def printPairs(arr, n, sum):
    mydict = dict()
    for i in range(n):
        temp = sum - arr[i]
        if temp in mydict:
            count = mydict[temp]
            for j in range(count):
                print("(", temp, ", ", arr[i],
                      ")", sep = "", end = '\n')
                       
        if arr[i] in mydict:
            mydict[arr[i]] += 1
        else:
            mydict[arr[i]] = 1
            
arr = [2,2,2,2,2]
n = len(arr)
sum = 4
pair_sum(arr, n, sum)
def getPairsCount(arr, n, sum):
    m = [0] * 10000
    for i in range(0, n):
        m[arr[i]] += 1
        
    twice_count = 0
    for i in range(0, n):
        twice_count += m[sum - arr[i]]
        if (sum - arr[i] == arr[i]):
            twice_count -= 1

    return int(twice_count / 2)

arr = [2,2,2,2,2]
#arr = [1, 5, 7, -1, 5]
n = len(arr)
sum = 6
print(getPairsCount(arr, n, sum))<jupyter_output>0<jupyter_text>Triplet sumYou have been given a random integer array/list(ARR) and a number X. Find and return the triplet(s) in the array/list which sum to X.Note :Given array/list can contain duplicate elements.<jupyter_code>def findTriplets(arr, n, Sum):
    c = 0
    for i in range(n - 1):
        s = dict() 
        for j in range(i + 1, n): 
            x = Sum - (arr[i] + arr[j]) 
            if x in s.keys(): 
                c += 1 
            else: 
                s[arr[j]] = 1
    return c

#arr = [1, 2, 3, 4, 5, 6, 7] 
arr = [6, 1, 6, 5, 3, 2, 5, 0, 5, 6, 0]
Sum = 5
n = len(arr)
findTriplets(arr, n, Sum)
def findTriplets(arr, n, sum):
    arr.sort()
  
    for i in range(0, n - 1):
        l = i + 1
        r = n - 1
        x = arr[i]
        while (l < r) : 
            if (x + arr[l] + arr[r] == sum) : 
                print(x, arr[l], arr[r])
                l = l + 1
                r = r - 1
            elif (x + arr[l] + arr[r] < sum): 
                l = l + 1
            else: 
                r = r - 1

arr = [6, 1, 6, 5, 3, 2, 5, 0, 5, 6, 0]
sum = 5
n = len(arr)
findTriplets(arr, n, sum)
def tripletSum(arr, n, num):
    arr.sort()
    numTriplets = 0
    for i in range(n):
        pairSumFor = num - arr[i]
        numPairs = pairSum(arr, (i + 1), (n-1), pairSumFor)
        numTriplets += numPairs
    return numTriplets

def pairSum(arr, si, ei, num):
    numPair = 0
    while si < ei:
        if arr[si] + arr[ei] < num:
            si += 1
        elif arr[si] + arr[ei] > num:
            ei -= 1
        else:
            elementAtStart = arr[si]
            elementAtEnd = arr[ei]
            if elementAtStart == elementAtEnd:
                totalElementsFromStartToEnd = (ei - si) + 1
                nmPair += (totalElementsFromStartToEnd * (totalElementsFromStartToEnd - 1) // 2)
                return numPair
            tempsi = si + 1
            tempei = ei - 1

            while tempsi <= tempei and arr[tempsi] == elementAtStart:
                tempsi += 1

            while tempei >= tempsi and arr[tempei] == elementAtEnd:
                tempei -= 1

            totalElementsFromStart = tempsi - si
            totalElementsFromEnd = ei - tempei

            numPair += totalElementsFromStart * totalElementsFromEnd

            si = tempsi
            ei = tempei
    return numPair

arr = [1, 2, 3, 4, 5, 6, 7]
print(tripletSum(arr, len(arr), 12))<jupyter_output>5<jupyter_text>Rotate arrayYou have been given a random integer array/list(ARR) of size N. Write a function that rotates the given array/list by D elements(towards the left). Note:Change in the input array/list itself. You don't need to return or print the elements.<jupyter_code>def rotate(arr, n, x):
    a = arr[:x]
    p = 0
    for i in range(x, n):
        arr[p] = arr[i]
        p += 1
    p = 0
    for i in range(n-x, n):
        arr[i] = a[p]
        p += 1
    
arr = [2,3,4,5,1]
rotate(arr, len(arr), 2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1282764098490866, 'fraction_numerical': 0.033260524225575856, 'mean_word_length': 2.1866497943688707, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '537362', 'license': 'Apache-2.0', 'n_tokens_mistral': 3775, 'n_tokens_neox': 3479, 'n_words': 1301, 'path': 'Time and Space Complexity/timeNspace.ipynb', 'repo_name': 'jarvis-1805/DSAwithPYTHON', 'size': '10072'}