giulio98/xlcost-single-prompt huggingface dataset:
# find the missing number | getmissingno takes list as argument ; 
def getMissingNo(a, n):
    n_elements_sum = n * (n + 1) // 2
    return n_elements_sum - sum(a)


if __name__ == '__main__':
    a = [1, 2, 4, 5, 6]
    n = len(a) + 1
    miss = getMissingNo(a, n)
    print(miss)

# Expected output:
3
# count number of occurrences ( or frequency ) in a sorted array | returns number of times x occurs in arr [ 0. . n - 1 ] ; 
def countOccurrences(arr, n, x):
    res = 0
    for i in range(n):
        if x == arr[i]:
            res += 1
    return res


arr = [1, 2, 2, 2, 2, 3, 4, 7, 8, 8]
n = len(arr)
x = 2
print(countOccurrences(arr, n, x))

# Expected output:
4
# count number of occurrences ( or frequency ) in a sorted array | a recursive binary search function . it returns location of x in given array arr [ l . . r ] is present , otherwise - 1 ; if the element is present at the middle itself ; if element is smaller than mid , then it can only be present in left subarray ; else the element can only be present in right subarray ; returns number of times x occurs in arr [ 0. . n - 1 ] ; if element is not present ; count elements on left side . ; count elements on right side . ; 
def binarySearch(arr, l, r, x):
    if (r < l):
        return -1
    mid = int(l + (r - l) / 2)
    if arr[mid] == x:
        return mid
    if arr[mid] > x:
        return binarySearch(arr, l, mid - 1, x)
    return binarySearch(arr, mid + 1, r, x)


def countOccurrences(arr, n, x):
    ind = binarySearch(arr, 0, n - 1, x)
    if ind == -1:
        return 0
    count = 1
    left = ind - 1
    while (left >= 0 and arr[left] == x):
        count += 1
        left -= 1
    right = ind + 1
    while (right < n and arr[right] == x):
        count += 1
        right += 1
    return count


arr = [1, 2, 2, 2, 2, 3, 4, 7, 8, 8]
n = len(arr)
x = 2
print(countOccurrences(arr, n, x))

# Expected output:
4
# given a sorted array and a number x , find the pair in array whose sum is closest to x | a sufficiently large value greater than any element in the input array ; prints the pair with sum closest to x ; to store indexes of result pair ; initialize left and right indexes and difference between pair sum and x ; while there are elements between l and r ; check if this pair is closer than the closest pair so far ; if this pair has more sum , move to smaller values . ; move to larger values ; 

MAX_VAL = 1000000000


def printClosest(arr, n, x):
    res_l, res_r = 0, 0
    l, r, diff = 0, n - 1, MAX_VAL
    while r > l:
        if abs(arr[l] + arr[r] - x) < diff:
            res_l = l
            res_r = r
            diff = abs(arr[l] + arr[r] - x)
        if arr[l] + arr[r] > x:
            r -= 1
        else:
            l += 1
    print('The closest pair is {} and {}'.format(arr[res_l], arr[res_r]))


if __name__ == "__main__":
    arr = [10, 22, 28, 29, 30, 40]
    n = len(arr)
    x = 54
    printClosest(arr, n, x)

# Expected output:
The closest pair is 22 and 30
# find lost element from a duplicated array | function to find missing element based on binary search approach . arr1 [ ] is of larger size and n is size of it . arr1 [ ] and arr2 [ ] are assumed to be in same order . ; special case , for only element which is missing in second array ; special case , for first element missing ; initialize current corner points ; loop until lo < hi ; if element at mid indices are equal then go to right subarray ; if lo , hi becomes contiguous , break ; missing element will be at hi index of bigger array ; this function mainly does basic error checking and calls findmissingutil ; 
def findMissingUtil(arr1, arr2, N):
    if N == 1:
        return arr1[0]
    if arr1[0] != arr2[0]:
        return arr1[0]
    lo = 0
    hi = N - 1
    while (lo < hi):
        mid = (lo + hi) / 2
        if arr1[mid] == arr2[mid]:
            lo = mid
        else:
            hi = mid
        if lo == hi - 1:
            break
    return arr1[hi]


def findMissing(arr1, arr2, M, N):
    if N == M - 1:
        print("Missing Element is", findMissingUtil(arr1, arr2, M))
    elif M == N - 1:
        print("Missing Element is", findMissingUtil(arr2, arr1, N))
    else:
        print("Invalid Input")


arr1 = [1, 4, 5, 7, 9]
arr2 = [4, 5, 7, 9]
M = len(arr1)
N = len(arr2)
findMissing(arr1, arr2, M, N)

# Expected output:
Missing Element is 1
# find lost element from a duplicated array | this function mainly does xor of all elements of arr1 [ ] and arr2 [ ] ; do xor of all element ; 
def findMissing(arr1, arr2, M, N):
    if (M != N - 1 and N != M - 1):
        print("Invalid Input")
        return
    res = 0
    for i in range(0, M):
        res = res ^ arr1[i]
    for i in range(0, N):
        res = res ^ arr2[i]
    print("Missing element is", res)


arr1 = [4, 1, 5, 9, 7]
arr2 = [7, 5, 9, 4]
M = len(arr1)
N = len(arr2)
findMissing(arr1, arr2, M, N)

# Expected output:
Missing element is 1
# find the repeating and the missing | added 3 new methods | python3 code to find the repeating and the missing elements ; 
def printTwoElements(arr, size):
    for i in range(size):
        if arr[abs(arr[i]) - 1] > 0:
            arr[abs(arr[i]) - 1] = -arr[abs(arr[i]) - 1]
        else:
            print("The repeating element is", abs(arr[i]))
    for i in range(size):
        if arr[i] > 0:
            print("and the missing element is", i + 1)


arr = [7, 3, 4, 5, 5, 6, 2]
n = len(arr)
printTwoElements(arr, n)

# Expected output:
The repeating element is 5
and the missing element is 1
# find the repeating and the missing | added 3 new methods | the output of this function is stored at x and y ; will hold xor of all elements and numbers from 1 to n ; get the xor of all array elements ; xor the previous result with numbers from 1 to n ; will have only single set bit of xor1 ; now divide elements into two sets by comparing a rightmost set bit of xor1 with the bit at the same position in each element . also , get xors of two sets . the two xors are the output elements . the following two for loops serve the purpose ; arr [ i ] belongs to first set ; arr [ i ] belongs to second set ; i belongs to first set ; i belongs to second set ; 
def getTwoElements(arr, n):
    global x, y
    x = 0
    y = 0
    xor1 = arr[0]
    for i in range(1, n):
        xor1 = xor1 ^ arr[i]
    for i in range(1, n + 1):
        xor1 = xor1 ^ i
    set_bit_no = xor1 & ~(xor1 - 1)
    for i in range(n):
        if (arr[i] & set_bit_no) != 0:
            x = x ^ arr[i]
        else:
            y = y ^ arr[i]
    for i in range(1, n + 1):
        if (i & set_bit_no) != 0:
            x = x ^ i
        else:
            y = y ^ i


arr = [1, 3, 4, 5, 5, 6, 2]
n = len(arr)
getTwoElements(arr, n)
print("The missing element is", x, "and the repeating number is", y)

# Expected output:
The missing element is 7 and the repeating number is 5
# Find the repeating and the missing | Added 3 new methods | Python3 program to find the repeating and missing elements using Maps
def main():
    arr = [4, 3, 6, 2, 1, 1]
    numberMap = {}
    max = len(arr)
    for i in arr:
        if i not in numberMap:
            numberMap[i] = True
        else:
            print("Repeating =", i)
    for i in range(1, max + 1):
        if i not in numberMap:
            print("Missing =", i)


main()

# Expected output:
Repeating = 1
Missing = 5
# find four elements that sum to a given value | set 1 ( n ^ 3 solution ) | a naive solution to print all combination of 4 elements in a [ ] with sum equal to x ; fix the first element and find other three ; fix the second element and find other two ; fix the third element and find the fourth ; find the fourth ; 
def findFourElements(A, n, X):
    for i in range(0, n - 3):
        for j in range(i + 1, n - 2):
            for k in range(j + 1, n - 1):
                for l in range(k + 1, n):
                    if A[i] + A[j] + A[k] + A[l] == X:
                        print("%d, %d, %d, %d" % (A[i], A[j], A[k], A[l]))


A = [10, 2, 3, 4, 5, 9, 7, 8]
n = len(A)
X = 23
findFourElements(A, n, X)

# Expected output:
10, 2, 3, 8
10, 2, 4, 7
2, 4, 9, 8
2, 5, 9, 7
3, 4, 9, 7
3, 5, 7, 8
# find four elements that sum to a given value | set 2 | the function finds four elements with given summ x ; store summs of all pairs in a hash table ; traverse through all pairs and search for x - ( current pair summ ) . ; if x - summ is present in hash table , ; making sure that all elements are distinct array elements and an element is not considered more than once . ; 
def findFourElements(arr, n, X):
    mp = {}
    for i in range(n - 1):
        for j in range(i + 1, n):
            mp[arr[i] + arr[j]] = [i, j]
    for i in range(n - 1):
        for j in range(i + 1, n):
            summ = arr[i] + arr[j]
            if (X - summ) in mp:
                p = mp[X - summ]
                if (p[0] != i and p[0] != j and p[1] != i and p[1] != j):
                    print(arr[i], ", ", arr[j], ", ",
                          arr[p[0]], ", ", arr[p[1]], sep="")
                    return


arr = [10, 20, 30, 40, 1, 2]
n = len(arr)
X = 91
findFourElements(arr, n, X)

# Expected output:
20, 30, 40, 1
# search an element in an array where difference between adjacent elements is 1 | x is the element to be searched in arr [ 0. . n - 1 ] ; traverse the given array starting from leftmost element ; if x is found at index i ; jump the difference between current array element and x ; 
def search(arr, n, x):
    i = 0
    while (i < n):
        if (arr[i] == x):
            return i
        i = i + abs(arr[i] - x)
    print("number is not present!")
    return -1


arr = [8, 7, 6, 7, 6, 5, 4, 3, 2, 3, 4, 3]
n = len(arr)
x = 3
print("Element", x, " is present at index ", search(arr, n, 3))

# Expected output:
Element 3  is present at index  7
# third largest element in an array of distinct elements | python 3 program to find third largest element in an array of distinct elements ; there should be atleast three elements ; find first largest element ; find second largest element ; find third largest element ; 

import sys


def thirdLargest(arr, arr_size):
    if (arr_size < 3):
        print(" Invalid Input ")
        return
    first = arr[0]
    for i in range(1, arr_size):
        if (arr[i] > first):
            first = arr[i]
    second = -sys .maxsize
    for i in range(0, arr_size):
        if (arr[i] > second and arr[i] < first):
            second = arr[i]
    third = -sys .maxsize
    for i in range(0, arr_size):
        if (arr[i] > third and arr[i] < second):
            third = arr[i]
    print("The Third Largest", "element is", third)


arr = [12, 13, 1, 10, 34, 16]
n = len(arr)
thirdLargest(arr, n)

# Expected output:
The Third Largest element is 13
# third largest element in an array of distinct elements | python3 program to find third largest element in an array ; there should be atleast three elements ; initialize first , second and third largest element ; traverse array elements to find the third largest ; if current element is greater than first , then update first , second and third ; if arr [ i ] is in between first and second ; if arr [ i ] is in between second and third ; 

import sys


def thirdLargest(arr, arr_size):
    if (arr_size < 3):
        print(" Invalid Input ")
        return
    first = arr[0]
    second = -sys .maxsize
    third = -sys .maxsize
    for i in range(1, arr_size):
        if (arr[i] > first):
            third = second
            second = first
            first = arr[i]
        elif (arr[i] > second):
            third = second
            second = arr[i]
        elif (arr[i] > third):
            third = arr[i]
    print("The third Largest", "element is", third)


arr = [12, 13, 1, 10, 34, 16]
n = len(arr)
thirdLargest(arr, n)

# Expected output:
The third Largest element is 13
# check if there exist two elements in an array whose sum is equal to the sum of rest of the array | function to check whether two elements exist whose sum is equal to sum of rest of the elements . ; find sum of whole array ; / if sum of array is not even than we can not divide it into two part ; for each element arr [ i ] , see if there is another element with value sum - arr [ i ] ; if element exist than return the pair ; 
def checkPair(arr, n):
    s = set()
    sum = 0
    for i in range(n):
        sum += arr[i]
    if sum % 2 != 0:
        return False
    sum = sum / 2
    for i in range(n):
        val = sum - arr[i]
        if arr[i]not in s:
            s .add(arr[i])
        if val in s:
            print("Pair elements are", arr[i], "and", int(val))


arr = [2, 11, 5, 1, 4, 7]
n = len(arr)
if checkPair(arr, n) == False:
    print("No pair found")

# Expected output:
Pair elements are 4 and 11
# search an element in an unsorted array using minimum number of comparisons | function to search an element in minimum number of comparisons ; 1 st comparison ; no termination condition and thus no comparison ; this would be executed at - most n times and therefore at - most n comparisons ; replace arr [ n - 1 ] with its actual element as in original 'arr[]   ; if ' x ' is found before the ' ( n - 1 ) th ' index , then it is present in the array final comparison ; else not present in the array ; 
def search(arr, n, x):
    if (arr[n - 1] == x):
        return "Found"
    backup = arr[n - 1]
    arr[n - 1] = x
    i = 0
    while (i < n):
        if (arr[i] == x):
            arr[n - 1] = backup
            if (i < n - 1):
                return "Found"
            return "Not Found"
        i = i + 1


arr = [4, 6, 1, 5, 8]
n = len(arr)
x = 1
print(search(arr, n, x))

# Expected output:
Found
# count of only repeated element in a sorted array of consecutive elements | assumptions : vector a is sorted , max - difference of two adjacent elements is 1 ; if a [ m ] = m + a [ 0 ] , there is no repeating character in [ s . . m ] ; if a [ m ] < m + a [ 0 ] , there is a repeating character in [ s . . m ] ; 
def sequence(a):
    if (len(a) == 0):
        return [0, 0]
    s = 0
    e = len(a) - 1
    while (s < e):
        m = (s + e) // 2
        if (a[m] >= m + a[0]):
            s = m + 1
        else:
            e = m
    return [a[s], len(a) - (a[len(a) - 1] - a[0])]


p = sequence([1, 2, 3, 4, 4, 4, 5, 6])
print("Repeated element is", p[0], ", it appears", p[1], "times")

# Expected output:
Repeated element is 4 , it appears 3 times
# find element in a sorted array whose frequency is greater than or equal to n / 2. | python 3 code to find majority element in a sorted array ; 
def findMajority(arr, n):
    return arr[int(n / 2)]


arr = [1, 2, 2, 3]
n = len(arr)
print(findMajority(arr, n))

# Expected output:
2
# minimum absolute difference of adjacent elements in a circular array | python3 program to find maximum difference between adjacent elements in a circular array . ; checking normal adjacent elements ; checking circular link ; 
def minAdjDifference(arr, n):
    if (n < 2):
        return
    res = abs(arr[1] - arr[0])
    for i in range(2, n):
        res = min(res, abs(arr[i] - arr[i - 1]))
    res = min(res, abs(arr[n - 1] - arr[0]))
    print("Min Difference = ", res)


a = [10, 12, 13, 15, 10]
n = len(a)
minAdjDifference(a, n)

# Expected output:
Min Difference =  0
# find the first , second and third minimum elements in an array | a python program to find the first , second and third minimum element in an array ; check if current element is less than firstmin , then update first , second and third ; check if current element is less than secmin then update second and third ; check if current element is less than , then upadte third ; 

MAX = 100000


def Print3Smallest(arr, n):
    firstmin = MAX
    secmin = MAX
    thirdmin = MAX
    for i in range(0, n):
        if arr[i] < firstmin:
            thirdmin = secmin
            secmin = firstmin
            firstmin = arr[i]
        elif arr[i] < secmin:
            thirdmin = secmin
            secmin = arr[i]
        elif arr[i] < thirdmin:
            thirdmin = arr[i]
    print("First min = ", firstmin)
    print("Second min = ", secmin)
    print("Third min = ", thirdmin)


arr = [4, 9, 1, 32, 12]
n = len(arr)
Print3Smallest(arr, n)

# Expected output:
First min =  1
Second min =  4
Third min =  9
# closest greater element for every array element from another array | python implementation to find result from target array for closest element ; function for printing resultant array ; sort list for ease ; list for result ; calculate resultant array ; check location of upper bound element ; if no element found push - 1 ; else puch the element ; add to resultant ; 

import bisect


def closestResult(a, b, n):
    a .sort()
    c = []
    for i in range(n):
        up = bisect .bisect_right(a, b[i])
        if up == n:
            c .append(-1)
        else:
            c .append(a[up])
    print("Result = ", end="")
    for i in c:
        print(i, end=" ")


if __name__ == "__main__":
    a = [2, 5, 6, 1, 8, 9]
    b = [2, 1, 0, 5, 4, 9]
    n = len(a)
    closestResult(a, b, n)

# Expected output:
Result = 5 2 1 6 5 -1
# count frequencies of all elements in array in o ( 1 ) extra space and o ( n ) time | function to find counts of all elements present in arr [ 0. . n - 1 ] . the array elements must be range from 1 to n ; hashmap ; traverse all array elements ; update the frequency of array [ i ] ; increase the index ; 
def findCounts(arr, n):
    hash = [0 for i in range(n)]
    i = 0
    while (i < n):
        hash[arr[i] - 1] += 1
        i += 1
    print("Below are counts of all elements")
    for i in range(n):
        print(i + 1, "->", hash[i], end=" ")
        print()


arr = [2, 3, 3, 2, 5]
findCounts(arr, len(arr))
arr1 = [1]
findCounts(arr1, len(arr1))
arr3 = [4, 4, 4, 4]
findCounts(arr3, len(arr3))
arr2 = [1, 3, 5, 7, 9, 1, 3, 5, 7, 9, 1]
findCounts(arr2, len(arr2))
arr4 = [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
findCounts(arr4, len(arr4))
arr5 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
findCounts(arr5, len(arr5))
arr6 = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
findCounts(arr6, len(arr6))

# Expected output:
Below are counts of all elements
1 -> 0 
2 -> 2 
3 -> 2 
4 -> 0 
5 -> 1 
Below are counts of all elements
1 -> 1 
Below are counts of all elements
1 -> 0 
2 -> 0 
3 -> 0 
4 -> 4 
Below are counts of all elements
1 -> 3 
2 -> 0 
3 -> 2 
4 -> 0 
5 -> 2 
6 -> 0 
7 -> 2 
8 -> 0 
9 -> 2 
10 -> 0 
11 -> 0 
Below are counts of all elements
1 -> 0 
2 -> 0 
3 -> 11 
4 -> 0 
5 -> 0 
6 -> 0 
7 -> 0 
8 -> 0 
9 -> 0 
10 -> 0 
11 -> 0 
Below are counts of all elements
1 -> 1 
2 -> 1 
3 -> 1 
4 -> 1 
5 -> 1 
6 -> 1 
7 -> 1 
8 -> 1 
9 -> 1 
10 -> 1 
11 -> 1 
Below are counts of all elements
1 -> 1 
2 -> 1 
3 -> 1 
4 -> 1 
5 -> 1 
6 -> 1 
7 -> 1 
8 -> 1 
9 -> 1 
10 -> 1 
11 -> 1
# count frequencies of all elements in array in o ( 1 ) extra space and o ( n ) time | function to find counts of all elements present in arr [ 0. . n - 1 ] . the array elements must be range from 1 to n ; traverse all array elements ; if this element is already processed , then nothing to do ; find index corresponding to this element for example , index for 5 is 4 ; if the elementindex has an element that is not processed yet , then first store that element to arr [ i ] so that we don 't lose anything. ; after storing arr [ elementindex ] , change it to store initial count of 'arr[i]   ; if this is not first occurrence of arr [ i ] , then decrement its count . ; and initialize arr [ i ] as 0 means the element ' i + 1' is not seen so far ; 
def findCounts(arr, n):
    i = 0
    while i < n:
        if arr[i] <= 0:
            i += 1
            continue
        elementIndex = arr[i] - 1
        if arr[elementIndex] > 0:
            arr[i] = arr[elementIndex]
            arr[elementIndex] = -1
        else:
            arr[elementIndex] -= 1
            arr[i] = 0
            i += 1
    print("Below are counts of all elements")
    for i in range(0, n):
        print("%d -> %d" % (i + 1, abs(arr[i])))
    print("")


arr = [2, 3, 3, 2, 5]
findCounts(arr, len(arr))
arr1 = [1]
findCounts(arr1, len(arr1))
arr3 = [4, 4, 4, 4]
findCounts(arr3, len(arr3))
arr2 = [1, 3, 5, 7, 9, 1, 3, 5, 7, 9, 1]
findCounts(arr2, len(arr2))
arr4 = [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
findCounts(arr4, len(arr4))
arr5 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
findCounts(arr5, len(arr5))
arr6 = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
findCounts(arr6, len(arr6))

# Expected output:
Below are counts of all elements
1 -> 0
2 -> 2
3 -> 2
4 -> 0
5 -> 1

Below are counts of all elements
1 -> 1

Below are counts of all elements
1 -> 0
2 -> 0
3 -> 0
4 -> 4

Below are counts of all elements
1 -> 3
2 -> 0
3 -> 2
4 -> 0
5 -> 2
6 -> 0
7 -> 2
8 -> 0
9 -> 2
10 -> 0
11 -> 0

Below are counts of all elements
1 -> 0
2 -> 0
3 -> 11
4 -> 0
5 -> 0
6 -> 0
7 -> 0
8 -> 0
9 -> 0
10 -> 0
11 -> 0

Below are counts of all elements
1 -> 1
2 -> 1
3 -> 1
4 -> 1
5 -> 1
6 -> 1
7 -> 1
8 -> 1
9 -> 1
10 -> 1
11 -> 1

Below are counts of all elements
1 -> 1
2 -> 1
3 -> 1
4 -> 1
5 -> 1
6 -> 1
7 -> 1
8 -> 1
9 -> 1
10 -> 1
11 -> 1
# count frequencies of all elements in array in o ( 1 ) extra space and o ( n ) time | function to find counts of all elements present in arr [ 0. . n - 1 ] . the array elements must be range from 1 to n ; subtract 1 from every element so that the elements become in range from 0 to n - 1 ; use every element arr [ i ] as index and add ' n ' to element present at arr [ i ] % n to keep track of count of occurrences of arr [ i ] ; to print counts , simply print the number of times n was added at index corresponding to every element ; 
def printfrequency(arr, n):
    for j in range(n):
        arr[j] = arr[j] - 1
    for i in range(n):
        arr[arr[i] % n] = arr[arr[i] % n] + n
    for i in range(n):
        print(i + 1, "->", arr[i] // n)


arr = [2, 3, 3, 2, 5]
n = len(arr)
printfrequency(arr, n)

# Expected output:
1 -> 0
2 -> 2
3 -> 2
4 -> 0
5 -> 1
# delete an element from array ( using two traversals and one traversal ) | this function removes an element x from arr [ ] and returns new size after removal ( size is reduced only when x is present in arr [ ] ; search x in array ; if x found in array ; reduce size of array and move all elements on space ahead ; 
def deleteElement(arr, n, x):
    for i in range(n):
        if (arr[i] == x):
            break
    if (i < n):
        n = n - 1
        for j in range(i, n, 1):
            arr[j] = arr[j + 1]
    return n


if __name__ == '__main__':
    arr = [11, 15, 6, 8, 9, 10]
    n = len(arr)
    x = 6
    n = deleteElement(arr, n, x)
    print("Modified array is")
    for i in range(n):
        print(arr[i], end=" ")

# Expected output:
Modified array is
11 15 8 9 10
# delete an element from array ( using two traversals and one traversal ) | this function removes an element x from arr [ ] and returns new size after removal . returned size is n - 1 when element is present . otherwise 0 is returned to indicate failure . ; if x is last element , nothing to do ; start from rightmost element and keep moving elements one position ahead . ; if element was not found ; else move the next element in place of x ; 
def deleteElement(arr, n, x):
    if arr[n - 1] == x:
        return n - 1
    prev = arr[n - 1]
    for i in range(n - 2, 1, -1):
        if arr[i] != x:
            curr = arr[i]
            arr[i] = prev
            prev = curr
    if i < 0:
        return 0
    arr[i] = prev
    return n - 1


arr = [11, 15, 6, 8, 9, 10]
n = len(arr)
x = 6
n = deleteElement(arr, n, x)
print("Modified array is")
for i in range(n):
    print(arr[i], end=" ")

# Expected output:
Modified array is
11 15 8 9 10
# count inversions of size three in a given array | returns count of inversions of size 3 ; initialize result ; count all smaller elements on right of arr [ i ] ; count all greater elements on left of arr [ i ] ; update inversion count by adding all inversions that have arr [ i ] as middle of three elements ; 
def getInvCount(arr, n):
    invcount = 0
    for i in range(1, n - 1):
        small = 0
        for j in range(i + 1, n):
            if (arr[i] > arr[j]):
                small += 1
        great = 0
        for j in range(i - 1, -1, -1):
            if (arr[i] < arr[j]):
                great += 1
        invcount += great * small
    return invcount


arr = [8, 4, 2, 1]
n = len(arr)
print("Inversion Count :", getInvCount(arr, n))

# Expected output:
Inversion Count : 4