codeparrot/xlcost-text-to-code huggingface dataset:
Sum of i * countDigits ( i ) ^ 2 for all i in range [ L , R ] | Python3 implementation of the approach ; Function to return the required sum ; If range is valid ; Sum of AP ; Driver code
MOD = 1000000007 ; NEW_LINE def rangeSum ( l , r ) : NEW_LINE INDENT a = 1 ; b = 9 ; res = 0 ; NEW_LINE for i in range ( 1 , 11 ) : NEW_LINE INDENT L = max ( l , a ) ; NEW_LINE R = min ( r , b ) ; NEW_LINE if ( L <= R ) : NEW_LINE INDENT sum = ( L + R ) * ( R - L + 1 ) // 2 ; NEW_LINE res += ( i * i ) * ( sum % MOD ) ; NEW_LINE res %= MOD ; NEW_LINE DEDENT a *= 10 ; NEW_LINE b = b * 10 + 9 ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 98 ; r = 102 ; NEW_LINE print ( rangeSum ( l , r ) ) ; NEW_LINE DEDENT
Generate a random permutation of elements from range [ L , R ] ( Divide and Conquer ) | Python3 implementation of the approach ; To store the random permutation ; Utility function to print the generated permutation ; Function to return a random number between x and y ; Recursive function to generate the random permutation ; Base condition ; Random number returned from the function ; Inserting random number in vector ; Recursion call for [ l , n - 1 ] ; Recursion call for [ n + 1 , r ] ; Driver code ; Generate permutation ; Print the generated permutation
import random NEW_LINE permutation = [ ] NEW_LINE def printPermutation ( ) : NEW_LINE INDENT for i in permutation : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT def give_random_number ( l , r ) : NEW_LINE INDENT x = random . randint ( l , r ) NEW_LINE return x NEW_LINE DEDENT def generate_random_permutation ( l , r ) : NEW_LINE INDENT if ( l > r ) : NEW_LINE INDENT return NEW_LINE DEDENT n = give_random_number ( l , r ) NEW_LINE permutation . append ( n ) NEW_LINE generate_random_permutation ( l , n - 1 ) NEW_LINE generate_random_permutation ( n + 1 , r ) NEW_LINE DEDENT l = 5 NEW_LINE r = 15 NEW_LINE generate_random_permutation ( l , r ) NEW_LINE printPermutation ( ) NEW_LINE
Minimum number N such that total set bits of all numbers from 1 to N is at | Python3 implementation of the above approach ; Function to count sum of set bits of all numbers till N ; Function to find the minimum number ; Binary search for the lowest number ; Find mid number ; Check if it is atleast x ; Driver Code
INF = 99999 NEW_LINE size = 10 NEW_LINE def getSetBitsFromOneToN ( N ) : NEW_LINE INDENT two , ans = 2 , 0 NEW_LINE n = N NEW_LINE while ( n > 0 ) : NEW_LINE INDENT ans += ( N // two ) * ( two >> 1 ) NEW_LINE if ( ( N & ( two - 1 ) ) > ( two >> 1 ) - 1 ) : NEW_LINE INDENT ans += ( N & ( two - 1 ) ) - ( two >> 1 ) + 1 NEW_LINE DEDENT two <<= 1 NEW_LINE n >>= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT def findMinimum ( x ) : NEW_LINE INDENT low = 0 NEW_LINE high = 100000 NEW_LINE ans = high NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) >> 1 NEW_LINE if ( getSetBitsFromOneToN ( mid ) >= x ) : NEW_LINE INDENT ans = min ( ans , mid ) NEW_LINE high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT x = 20 NEW_LINE print ( findMinimum ( x ) ) NEW_LINE
Numbers whose factorials end with n zeros | Function to calculate trailing zeros ; binary search for first number with n trailing zeros ; Print all numbers after low with n trailing zeros . ; Print result ; Driver code
def trailingZeroes ( n ) : NEW_LINE INDENT cnt = 0 NEW_LINE while n > 0 : NEW_LINE INDENT n = int ( n / 5 ) NEW_LINE cnt += n NEW_LINE DEDENT return cnt NEW_LINE DEDENT def binarySearch ( n ) : NEW_LINE INDENT low = 0 NEW_LINE while low < high : NEW_LINE INDENT mid = int ( ( low + high ) / 2 ) NEW_LINE count = trailingZeroes ( mid ) NEW_LINE if count < n : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid NEW_LINE DEDENT DEDENT result = list ( ) NEW_LINE while trailingZeroes ( low ) == n : NEW_LINE INDENT result . append ( low ) NEW_LINE low += 1 NEW_LINE DEDENT for i in range ( len ( result ) ) : NEW_LINE INDENT print ( result [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 2 NEW_LINE binarySearch ( n ) NEW_LINE
Number of days after which tank will become empty | Utility method to get sum of first n numbers ; Method returns minimum number of days after which tank will become empty ; if water filling is more than capacity then after C days only tank will become empty ; initialize binary search variable ; loop until low is less than high ; if cumulate sum is greater than ( C - l ) then search on left side ; if ( C - l ) is more then search on right side ; Final answer will be obtained by adding l to binary search result ; Driver code
def getCumulateSum ( n ) : NEW_LINE INDENT return int ( ( n * ( n + 1 ) ) / 2 ) NEW_LINE DEDENT def minDaysToEmpty ( C , l ) : NEW_LINE INDENT if ( C <= l ) : return C NEW_LINE lo , hi = 0 , 1e4 NEW_LINE while ( lo < hi ) : NEW_LINE INDENT mid = int ( ( lo + hi ) / 2 ) NEW_LINE if ( getCumulateSum ( mid ) >= ( C - l ) ) : NEW_LINE INDENT hi = mid NEW_LINE DEDENT else : NEW_LINE INDENT lo = mid + 1 NEW_LINE DEDENT DEDENT return ( l + lo ) NEW_LINE DEDENT C , l = 5 , 2 NEW_LINE print ( minDaysToEmpty ( C , l ) ) NEW_LINE
Number of days after which tank will become empty | Python3 code to find number of days after which tank will become empty ; Method returns minimum number of days after which tank will become empty ; Driver code
import math NEW_LINE def minDaysToEmpty ( C , l ) : NEW_LINE INDENT if ( l >= C ) : return C NEW_LINE eq_root = ( math . sqrt ( 1 + 8 * ( C - l ) ) - 1 ) / 2 NEW_LINE return math . ceil ( eq_root ) + l NEW_LINE DEDENT print ( minDaysToEmpty ( 5 , 2 ) ) NEW_LINE print ( minDaysToEmpty ( 6514683 , 4965 ) ) NEW_LINE
K | Program to find kth element from two sorted arrays ; Driver code
def kth ( arr1 , arr2 , m , n , k ) : NEW_LINE INDENT sorted1 = [ 0 ] * ( m + n ) NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE d = 0 NEW_LINE while ( i < m and j < n ) : NEW_LINE INDENT if ( arr1 [ i ] < arr2 [ j ] ) : NEW_LINE INDENT sorted1 [ d ] = arr1 [ i ] NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT sorted1 [ d ] = arr2 [ j ] NEW_LINE j += 1 NEW_LINE DEDENT d += 1 NEW_LINE DEDENT while ( i < m ) : NEW_LINE INDENT sorted1 [ d ] = arr1 [ i ] NEW_LINE d += 1 NEW_LINE i += 1 NEW_LINE DEDENT while ( j < n ) : NEW_LINE INDENT sorted1 [ d ] = arr2 [ j ] NEW_LINE d += 1 NEW_LINE j += 1 NEW_LINE DEDENT return sorted1 [ k - 1 ] NEW_LINE DEDENT arr1 = [ 2 , 3 , 6 , 7 , 9 ] NEW_LINE arr2 = [ 1 , 4 , 8 , 10 ] NEW_LINE k = 5 NEW_LINE print ( kth ( arr1 , arr2 , 5 , 4 , k ) ) NEW_LINE
K | Python program to find k - th element from two sorted arrays
def kth ( arr1 , arr2 , n , m , k ) : NEW_LINE INDENT if n == 1 or m == 1 : NEW_LINE INDENT if m == 1 : NEW_LINE INDENT arr2 , arr1 = arr1 , arr2 NEW_LINE m = n NEW_LINE DEDENT if k == 1 : NEW_LINE INDENT return min ( arr1 [ 0 ] , arr2 [ 0 ] ) NEW_LINE DEDENT elif k == m + 1 : NEW_LINE INDENT return max ( arr1 [ 0 ] , arr2 [ 0 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT if arr2 [ k - 1 ] < arr1 [ 0 ] : NEW_LINE INDENT return arr2 [ k - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT return max ( arr1 [ 0 ] , arr2 [ k - 2 ] ) NEW_LINE DEDENT DEDENT DEDENT mid1 = ( n - 1 ) // 2 NEW_LINE mid2 = ( m - 1 ) // 2 NEW_LINE if mid1 + mid2 + 1 < k : NEW_LINE INDENT if arr1 [ mid1 ] < arr2 [ mid2 ] : NEW_LINE INDENT return kth ( arr1 [ mid1 + 1 : ] , arr2 , n - mid1 - 1 , m , k - mid1 - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return kth ( arr1 , arr2 [ mid2 + 1 : ] , n , m - mid2 - 1 , k - mid2 - 1 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if arr1 [ mid1 ] < arr2 [ mid2 ] : NEW_LINE INDENT return kth ( arr1 , arr2 [ : mid2 + 1 ] , n , mid2 + 1 , k ) NEW_LINE DEDENT else : NEW_LINE INDENT return kth ( arr1 [ : mid1 + 1 ] , arr2 , mid1 + 1 , m , k ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr1 = [ 2 , 3 , 6 , 7 , 9 ] NEW_LINE arr2 = [ 1 , 4 , 8 , 10 ] NEW_LINE k = 5 NEW_LINE print ( kth ( arr1 , arr2 , 5 , 4 , k ) ) NEW_LINE DEDENT
K | Python3 program to find kth element from two sorted arrays ; In case we have reached end of array 1 ; In case we have reached end of array 2 ; k should never reach 0 or exceed sizes of arrays ; Compare first elements of arrays and return ; Size of array 1 is less than k / 2 ; Last element of array 1 is not kth We can directly return the ( k - m ) th element in array 2 ; Size of array 2 is less than k / 2 ; Normal comparison , move starting index of one array k / 2 to the right ; Driver code
def kth ( arr1 , arr2 , m , n , k , st1 = 0 , st2 = 0 ) : NEW_LINE INDENT if ( st1 == m ) : NEW_LINE INDENT return arr2 [ st2 + k - 1 ] NEW_LINE DEDENT if ( st2 == n ) : NEW_LINE INDENT return arr1 [ st1 + k - 1 ] NEW_LINE DEDENT if ( k == 0 or k > ( m - st1 ) + ( n - st2 ) ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( k == 1 ) : NEW_LINE INDENT if ( arr1 [ st1 ] < arr2 [ st2 ] ) : NEW_LINE INDENT return arr1 [ st1 ] NEW_LINE DEDENT else : NEW_LINE INDENT return arr2 [ st2 ] NEW_LINE DEDENT DEDENT curr = int ( k / 2 ) NEW_LINE if ( curr - 1 >= m - st1 ) : NEW_LINE INDENT if ( arr1 [ m - 1 ] < arr2 [ st2 + curr - 1 ] ) : NEW_LINE INDENT return arr2 [ st2 + ( k - ( m - st1 ) - 1 ) ] NEW_LINE DEDENT else : NEW_LINE INDENT return kth ( arr1 , arr2 , m , n , k - curr , st1 , st2 + curr ) NEW_LINE DEDENT DEDENT if ( curr - 1 >= n - st2 ) : NEW_LINE INDENT if ( arr2 [ n - 1 ] < arr1 [ st1 + curr - 1 ] ) : NEW_LINE INDENT return arr1 [ st1 + ( k - ( n - st2 ) - 1 ) ] NEW_LINE DEDENT else : NEW_LINE INDENT return kth ( arr1 , arr2 , m , n , k - curr , st1 + curr , st2 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( arr1 [ curr + st1 - 1 ] < arr2 [ curr + st2 - 1 ] ) : NEW_LINE INDENT return kth ( arr1 , arr2 , m , n , k - curr , st1 + curr , st2 ) NEW_LINE DEDENT else : NEW_LINE INDENT return kth ( arr1 , arr2 , m , n , k - curr , st1 , st2 + curr ) NEW_LINE DEDENT DEDENT DEDENT arr1 = [ 2 , 3 , 6 , 7 , 9 ] NEW_LINE arr2 = [ 1 , 4 , 8 , 10 ] NEW_LINE k = 5 NEW_LINE print ( kth ( arr1 , arr2 , 5 , 4 , k ) ) NEW_LINE
Search element in a sorted matrix | Python3 implementation to search an element in a sorted matrix ; This function does Binary search for x in i - th row . It does the search from mat [ i ] [ j_low ] to mat [ i ] [ j_high ] ; Element found ; Element not found ; Function to perform binary search on the mid values of row to get the desired pair of rows where the element can be found ; Single row matrix ; Do binary search in middle column . Condition to terminate the loop when the 2 desired rows are found ; element found ; If element is present on the mid of the two rows ; search element on 1 st half of 1 st row ; Search element on 2 nd half of 1 st row ; Search element on 1 st half of 2 nd row ; Search element on 2 nd half of 2 nd row ; Driver program to test above
MAX = 100 NEW_LINE def binarySearch ( mat , i , j_low , j_high , x ) : NEW_LINE INDENT while ( j_low <= j_high ) : NEW_LINE INDENT j_mid = ( j_low + j_high ) // 2 NEW_LINE if ( mat [ i ] [ j_mid ] == x ) : NEW_LINE INDENT print ( " Found ▁ at ▁ ( " , i , " , ▁ " , j_mid , " ) " ) NEW_LINE return NEW_LINE DEDENT elif ( mat [ i ] [ j_mid ] > x ) : NEW_LINE INDENT j_high = j_mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT j_low = j_mid + 1 NEW_LINE DEDENT DEDENT print ( " Element ▁ no ▁ found " ) NEW_LINE DEDENT def sortedMatrixSearch ( mat , n , m , x ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT binarySearch ( mat , 0 , 0 , m - 1 , x ) NEW_LINE return NEW_LINE DEDENT i_low = 0 NEW_LINE i_high = n - 1 NEW_LINE j_mid = m // 2 NEW_LINE while ( ( i_low + 1 ) < i_high ) : NEW_LINE INDENT i_mid = ( i_low + i_high ) // 2 NEW_LINE if ( mat [ i_mid ] [ j_mid ] == x ) : NEW_LINE INDENT print ( " Found ▁ at ▁ ( " , i_mid , " , ▁ " , j_mid , " ) " ) NEW_LINE return NEW_LINE DEDENT elif ( mat [ i_mid ] [ j_mid ] > x ) : NEW_LINE INDENT i_high = i_mid NEW_LINE DEDENT else : NEW_LINE INDENT i_low = i_mid NEW_LINE DEDENT DEDENT if ( mat [ i_low ] [ j_mid ] == x ) : NEW_LINE INDENT print ( " Found ▁ at ▁ ( " , i_low , " , " , j_mid , " ) " ) NEW_LINE DEDENT elif ( mat [ i_low + 1 ] [ j_mid ] == x ) : NEW_LINE INDENT print ( " Found ▁ at ▁ ( " , ( i_low + 1 ) , " , ▁ " , j_mid , " ) " ) NEW_LINE DEDENT elif ( x <= mat [ i_low ] [ j_mid - 1 ] ) : NEW_LINE INDENT binarySearch ( mat , i_low , 0 , j_mid - 1 , x ) NEW_LINE DEDENT elif ( x >= mat [ i_low ] [ j_mid + 1 ] and x <= mat [ i_low ] [ m - 1 ] ) : NEW_LINE binarySearch ( mat , i_low , j_mid + 1 , m - 1 , x ) NEW_LINE elif ( x <= mat [ i_low + 1 ] [ j_mid - 1 ] ) : NEW_LINE INDENT binarySearch ( mat , i_low + 1 , 0 , j_mid - 1 , x ) NEW_LINE DEDENT else : NEW_LINE INDENT binarySearch ( mat , i_low + 1 , j_mid + 1 , m - 1 , x ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE m = 5 NEW_LINE x = 8 NEW_LINE mat = [ [ 0 , 6 , 8 , 9 , 11 ] , [ 20 , 22 , 28 , 29 , 31 ] , [ 36 , 38 , 50 , 61 , 63 ] , [ 64 , 66 , 100 , 122 , 128 ] ] NEW_LINE sortedMatrixSearch ( mat , n , m , x ) NEW_LINE DEDENT
Minimum difference between adjacent elements of array which contain elements from each row of a matrix | Return smallest element greater than or equal to the current element . ; Return the minimum absolute difference adjacent elements of array ; arr = [ 0 for i in range ( R ) ] [ for j in range ( C ) ] Sort each row of the matrix . ; For each matrix element ; Search smallest element in the next row which is greater than or equal to the current element ; largest element which is smaller than the current element in the next row must be just before smallest element which is greater than or equal to the current element because rows are sorted . ; Driver Program
def bsearch ( low , high , n , arr ) : NEW_LINE INDENT mid = ( low + high ) / 2 NEW_LINE if ( low <= high ) : NEW_LINE INDENT if ( arr [ mid ] < n ) : NEW_LINE INDENT return bsearch ( mid + 1 , high , n , arr ) ; NEW_LINE DEDENT return bsearch ( low , mid - 1 , n , arr ) ; NEW_LINE DEDENT return low ; NEW_LINE DEDENT def mindiff ( arr , n , m ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT sorted ( arr ) NEW_LINE DEDENT ans = 2147483647 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT p = bsearch ( 0 , m - 1 , arr [ i ] [ j ] , arr [ i + 1 ] ) NEW_LINE ans = min ( ans , abs ( arr [ i + 1 ] [ p ] - arr [ i ] [ j ] ) ) NEW_LINE if ( p - 1 >= 0 ) : NEW_LINE INDENT ans = min ( ans , abs ( arr [ i + 1 ] [ p - 1 ] - arr [ i ] [ j ] ) ) NEW_LINE DEDENT DEDENT DEDENT return ans ; NEW_LINE DEDENT m = [ 8 , 5 ] , [ 6 , 8 ] NEW_LINE print mindiff ( m , 2 , 2 ) NEW_LINE
Find bitonic point in given bitonic sequence | Function to find bitonic point using binary search ; base condition to check if arr [ mid ] is bitonic point or not ; We assume that sequence is bitonic . We go to right subarray if middle point is part of increasing subsequence . Else we go to left subarray . ; Driver Code
def binarySearch ( arr , left , right ) : NEW_LINE INDENT if ( left <= right ) : NEW_LINE INDENT mid = ( left + right ) // 2 ; NEW_LINE if ( arr [ mid - 1 ] < arr [ mid ] and arr [ mid ] > arr [ mid + 1 ] ) : NEW_LINE INDENT return mid ; NEW_LINE DEDENT if ( arr [ mid ] < arr [ mid + 1 ] ) : NEW_LINE INDENT return binarySearch ( arr , mid + 1 , right ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return binarySearch ( arr , left , mid - 1 ) ; NEW_LINE DEDENT DEDENT return - 1 ; NEW_LINE DEDENT arr = [ 6 , 7 , 8 , 11 , 9 , 5 , 2 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE index = binarySearch ( arr , 1 , n - 2 ) ; NEW_LINE if ( index != - 1 ) : NEW_LINE INDENT print ( arr [ index ] ) ; NEW_LINE DEDENT
Find the only repeating element in a sorted array of size n | Returns index of second appearance of a repeating element The function assumes that array elements are in range from 1 to n - 1. ; low = 0 , high = n - 1 ; Check if the mid element is the repeating one ; If mid element is not at its position that means the repeated element is in left ; If mid is at proper position then repeated one is in right . ; Driver code
def findRepeatingElement ( arr , low , high ) : NEW_LINE INDENT if low > high : NEW_LINE INDENT return - 1 NEW_LINE DEDENT mid = ( low + high ) / 2 NEW_LINE if ( arr [ mid ] != mid + 1 ) : NEW_LINE INDENT if ( mid > 0 and arr [ mid ] == arr [ mid - 1 ] ) : NEW_LINE INDENT return mid NEW_LINE DEDENT return findRepeatingElement ( arr , low , mid - 1 ) NEW_LINE DEDENT return findRepeatingElement ( arr , mid + 1 , high ) NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE index = findRepeatingElement ( arr , 0 , n - 1 ) NEW_LINE if ( index is not - 1 ) : NEW_LINE INDENT print arr [ index ] NEW_LINE DEDENT
Find cubic root of a number | Returns the absolute value of n - mid * mid * mid ; Returns cube root of a no n ; Set start and end for binary search ; Set precision ; If error is less than e then mid is our answer so return mid ; If mid * mid * mid is greater than n set end = mid ; If mid * mid * mid is less than n set start = mid ; Driver code
def diff ( n , mid ) : NEW_LINE INDENT if ( n > ( mid * mid * mid ) ) : NEW_LINE INDENT return ( n - ( mid * mid * mid ) ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( ( mid * mid * mid ) - n ) NEW_LINE DEDENT DEDENT def cubicRoot ( n ) : NEW_LINE INDENT start = 0 NEW_LINE end = n NEW_LINE e = 0.0000001 NEW_LINE while ( True ) : NEW_LINE INDENT mid = ( start + end ) / 2 NEW_LINE error = diff ( n , mid ) NEW_LINE if ( error <= e ) : NEW_LINE INDENT return mid NEW_LINE DEDENT if ( ( mid * mid * mid ) > n ) : NEW_LINE INDENT end = mid NEW_LINE DEDENT else : NEW_LINE INDENT start = mid NEW_LINE DEDENT DEDENT DEDENT n = 3 NEW_LINE print ( " Cubic ▁ root ▁ of " , n , " is " , round ( cubicRoot ( n ) , 6 ) ) NEW_LINE
Find frequency of each element in a limited range array in less than O ( n ) time | A recursive function to count number of occurrences for each element in the array without traversing the whole array ; If element at index low is equal to element at index high in the array ; increment the frequency of the element by count of elements between high and low ; Find mid and recurse for left and right subarray ; A wrapper over recursive function findFrequencyUtil ( ) . It print number of occurrences of each element in the array . ; create a empty vector to store frequencies and initialize it by 0. Size of vector is maximum value ( which is last value in sorted array ) plus 1. ; Fill the vector with frequency ; Print the frequencies ; Driver Code
def findFrequencyUtil ( arr , low , high , freq ) : NEW_LINE INDENT if ( arr [ low ] == arr [ high ] ) : NEW_LINE INDENT freq [ arr [ low ] ] += high - low + 1 NEW_LINE DEDENT else : NEW_LINE INDENT mid = int ( ( low + high ) / 2 ) NEW_LINE findFrequencyUtil ( arr , low , mid , freq ) NEW_LINE findFrequencyUtil ( arr , mid + 1 , high , freq ) NEW_LINE DEDENT DEDENT def findFrequency ( arr , n ) : NEW_LINE INDENT freq = [ 0 for i in range ( n - 1 + 1 ) ] NEW_LINE findFrequencyUtil ( arr , 0 , n - 1 , freq ) NEW_LINE for i in range ( 0 , arr [ n - 1 ] + 1 , 1 ) : NEW_LINE INDENT if ( freq [ i ] != 0 ) : NEW_LINE INDENT print ( " Element " , i , " occurs " , freq [ i ] , " times " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE findFrequency ( arr , n ) NEW_LINE DEDENT
Square root of an integer | Returns floor of square root of x ; Base cases ; Starting from 1 , try all numbers until i * i is greater than or equal to x . ; Driver Code
def floorSqrt ( x ) : NEW_LINE INDENT if ( x == 0 or x == 1 ) : NEW_LINE INDENT return x NEW_LINE DEDENT i = 1 ; result = 1 NEW_LINE while ( result <= x ) : NEW_LINE INDENT i += 1 NEW_LINE result = i * i NEW_LINE DEDENT return i - 1 NEW_LINE DEDENT x = 11 NEW_LINE print ( floorSqrt ( x ) ) NEW_LINE
Maximum number of overlapping rectangles with at least one common point | Function to find the maximum number of overlapping rectangles ; Stores the maximum count of overlapping rectangles ; Stores the X and Y coordinates ; Iterate over all pairs of Xs and Ys ; Store the count for the current X and Y ; Update the maximum count of rectangles ; Returns the total count ; Driver Code
def maxOverlappingRectangles ( x1 , y1 , x2 , y2 , N ) : NEW_LINE INDENT max_rectangles = 0 NEW_LINE X = [ ] NEW_LINE Y = [ ] NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT X . append ( x1 [ i ] ) NEW_LINE X . append ( x2 [ i ] - 1 ) NEW_LINE Y . append ( y1 [ i ] ) NEW_LINE Y . append ( y2 [ i ] - 1 ) NEW_LINE DEDENT for i in range ( 0 , len ( X ) ) : NEW_LINE INDENT for j in range ( 0 , len ( Y ) ) : NEW_LINE INDENT cnt = 0 NEW_LINE for k in range ( 0 , N ) : NEW_LINE INDENT if ( X [ i ] >= x1 [ k ] and X [ i ] + 1 <= x2 [ k ] and Y [ j ] >= y1 [ k ] and Y [ j ] + 1 <= y2 [ k ] ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT max_rectangles = max ( max_rectangles , cnt ) NEW_LINE DEDENT DEDENT print ( max_rectangles ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x1 = [ 0 , 50 ] NEW_LINE y1 = [ 0 , 50 ] NEW_LINE x2 = [ 100 , 60 ] NEW_LINE y2 = [ 100 , 60 ] NEW_LINE N = len ( x1 ) NEW_LINE maxOverlappingRectangles ( x1 , y1 , x2 , y2 , N ) NEW_LINE DEDENT
Form a Rectangle from boundary elements of Matrix using Linked List | Python program for above approach Node Class ; Constructor to initialize the node object ; Linked List class ; Constructor to initialize head ; function to form square linked list of matrix . ; initialising A [ 0 ] [ 0 ] as head . ; head is assigned to head . ; i is row index , j is column index ; loop till temp . top become equal to head . ; iterating over first i . e 0 th row and connecting node . ; iterating over last i . e ( m - 1 ) th column and connecting Node . ; iterating over last i . e ( n - 1 ) th row and connecting Node . ; iterating over first i . e 0 th column and connecting Node . ; function to print Linked list . ; printing head of linked list ; loop till temp . top become equal to head ; printing the node ; Driver Code ; n is number of rows ; m is number of column ; creation of object ; Call Quad method to create Linked List . ; Call printList method to print list .
class Node : NEW_LINE INDENT def __init__ ( self , val ) : NEW_LINE INDENT self . data = val NEW_LINE self . next = None NEW_LINE self . prev = None NEW_LINE self . top = None NEW_LINE self . bottom = None NEW_LINE DEDENT DEDENT class LinkedList : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . head = None NEW_LINE DEDENT def Quad ( self , grid , n , m ) : NEW_LINE INDENT self . head = Node ( grid [ 0 ] [ 0 ] ) NEW_LINE temp = self . head NEW_LINE i = 0 NEW_LINE j = 1 NEW_LINE while temp . top != self . head : NEW_LINE INDENT if j < m and i == 0 : NEW_LINE INDENT temp . next = Node ( grid [ i ] [ j ] ) NEW_LINE temp = temp . next NEW_LINE j += 1 NEW_LINE DEDENT elif j == m and i < n - 1 : NEW_LINE INDENT i = i + 1 NEW_LINE temp . bottom = Node ( grid [ i ] [ j - 1 ] ) NEW_LINE temp = temp . bottom NEW_LINE DEDENT elif i == n - 1 and j <= m and j >= 1 : NEW_LINE INDENT if j == m : j = j - 1 NEW_LINE j = j - 1 NEW_LINE temp . prev = Node ( grid [ i ] [ j ] ) NEW_LINE temp = temp . prev NEW_LINE DEDENT elif i <= n - 1 and j == 0 : NEW_LINE INDENT i = i - 1 NEW_LINE temp . top = Node ( grid [ i ] [ j ] ) NEW_LINE temp = temp . top NEW_LINE if i == 1 : NEW_LINE INDENT temp . top = self . head NEW_LINE DEDENT DEDENT DEDENT DEDENT def printList ( self , root ) : NEW_LINE INDENT temp = root NEW_LINE print ( temp . data , end = " ▁ " ) NEW_LINE while temp . top != root : NEW_LINE INDENT if temp . next : NEW_LINE INDENT print ( temp . next . data , end = " ▁ " ) NEW_LINE temp = temp . next NEW_LINE DEDENT if temp . prev : NEW_LINE INDENT print ( temp . prev . data , end = " ▁ " ) NEW_LINE temp = temp . prev NEW_LINE DEDENT if temp . bottom : NEW_LINE INDENT print ( temp . bottom . data , end = " ▁ " ) NEW_LINE temp = temp . bottom NEW_LINE DEDENT if temp . top : NEW_LINE INDENT print ( temp . top . data , end = " ▁ " ) NEW_LINE temp = temp . top NEW_LINE DEDENT DEDENT DEDENT DEDENT grid = [ [ 13 , 42 , 93 , 88 ] , [ 26 , 38 , 66 , 42 ] , [ 75 , 63 , 78 , 12 ] ] NEW_LINE n = len ( grid ) NEW_LINE m = len ( grid [ 0 ] ) NEW_LINE l = LinkedList ( ) NEW_LINE l . Quad ( grid , n , m ) NEW_LINE l . printList ( l . head ) NEW_LINE