codeparrot/xlcost-text-to-code huggingface dataset:
Uniform Binary Search | Python3 implementation of above approach ; lookup table ; create the lookup table for an array of length n ; power and count variable ; multiply by 2 ; initialize the lookup table ; binary search ; mid point of the array ; count ; if the value is found ; if value is less than the mid value ; if value is greater than the mid value ; main function ; create the lookup table ; print the position of the array
MAX_SIZE = 1000 NEW_LINE lookup_table = [ 0 ] * MAX_SIZE NEW_LINE def create_table ( n ) : NEW_LINE INDENT pow = 1 NEW_LINE co = 0 NEW_LINE while True : NEW_LINE INDENT pow <<= 1 NEW_LINE lookup_table [ co ] = ( n + ( pow >> 1 ) ) // pow NEW_LINE if lookup_table [ co ] == 0 : NEW_LINE INDENT break NEW_LINE DEDENT co += 1 NEW_LINE DEDENT DEDENT def binary ( arr , v ) : NEW_LINE INDENT index = lookup_table [ 0 ] - 1 NEW_LINE co = 0 NEW_LINE while lookup_table [ co ] != 0 : NEW_LINE INDENT if v == arr [ index ] : NEW_LINE INDENT return index NEW_LINE DEDENT elif v < arr [ index ] : NEW_LINE INDENT co += 1 NEW_LINE index -= lookup_table [ co ] NEW_LINE DEDENT else : NEW_LINE INDENT co += 1 NEW_LINE index += lookup_table [ co ] NEW_LINE DEDENT DEDENT DEDENT arr = [ 1 , 3 , 5 , 6 , 7 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE create_table ( n ) NEW_LINE print ( " Position ▁ of ▁ 3 ▁ in ▁ array ▁ = ▁ " , binary ( arr , 3 ) ) NEW_LINE
Find the smallest number X such that X ! contains at least Y trailing zeros . | Function to count the number of factors P in X ! ; Function to find the smallest X such that X ! contains Y trailing zeros ; Driver code
def countFactor ( P , X ) : NEW_LINE INDENT if ( X < P ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT return ( X // P + countFactor ( P , X // P ) ) ; NEW_LINE DEDENT def findSmallestX ( Y ) : NEW_LINE INDENT low = 0 ; NEW_LINE high = 5 * Y ; NEW_LINE N = 0 ; NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( high + low ) // 2 ; NEW_LINE if ( countFactor ( 5 , mid ) < Y ) : NEW_LINE INDENT low = mid + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT N = mid ; NEW_LINE high = mid - 1 ; NEW_LINE DEDENT DEDENT return N ; NEW_LINE DEDENT Y = 10 ; NEW_LINE print ( findSmallestX ( Y ) ) ; NEW_LINE
Find maximum N such that the sum of square of first N natural numbers is not more than X | Function to return the sum of the squares of first N natural numbers ; Function to return the maximum N such that the sum of the squares of first N natural numbers is not more than X ; Driver code
def squareSum ( N ) : NEW_LINE INDENT Sum = ( N * ( N + 1 ) * ( 2 * N + 1 ) ) // 6 NEW_LINE return Sum NEW_LINE DEDENT def findMaxN ( X ) : NEW_LINE INDENT low , high , N = 1 , 100000 , 0 NEW_LINE while low <= high : NEW_LINE INDENT mid = ( high + low ) // 2 NEW_LINE if squareSum ( mid ) <= X : NEW_LINE INDENT N = mid NEW_LINE low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT return N NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = 25 NEW_LINE print ( findMaxN ( X ) ) NEW_LINE DEDENT
Search an element in given N ranges | Function to return the index of the range in which K lies and uses linear search ; Iterate and find the element ; If K lies in the current range ; K doesn 't lie in any of the  given ranges ; Driver code
def findNumber ( a , n , K ) : NEW_LINE INDENT for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( K >= a [ i ] [ 0 ] and K <= a [ i ] [ 1 ] ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ [ 1 , 3 ] , [ 4 , 7 ] , [ 8 , 11 ] ] NEW_LINE n = len ( a ) NEW_LINE k = 6 NEW_LINE index = findNumber ( a , n , k ) NEW_LINE if ( index != - 1 ) : NEW_LINE INDENT print ( index , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 , end = " " ) NEW_LINE DEDENT DEDENT
Search an element in given N ranges | Function to return the index of the range in which K lies and uses binary search ; Binary search ; Find the mid element ; If element is found ; Check in first half ; Check in second half ; Not found ; Driver code
def findNumber ( a , n , K ) : NEW_LINE INDENT low = 0 NEW_LINE high = n - 1 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) >> 1 NEW_LINE if ( K >= a [ mid ] [ 0 ] and K <= a [ mid ] [ 1 ] ) : NEW_LINE INDENT return mid NEW_LINE DEDENT elif ( K < a [ mid ] [ 0 ] ) : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT a = [ [ 1 , 3 ] , [ 4 , 7 ] , [ 8 , 11 ] ] NEW_LINE n = len ( a ) NEW_LINE k = 6 NEW_LINE index = findNumber ( a , n , k ) NEW_LINE if ( index != - 1 ) : NEW_LINE INDENT print ( index ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT
Two equal sum segment range queries | Function to find the required prefix Sum ; Function to hash all the values of prefix Sum array in an unordered map ; Function to check if a range can be divided into two equal parts ; To store the value of Sum of entire range ; If value of Sum is odd ; To store p_arr [ l - 1 ] ; If the value exists in the map ; Driver code ; prefix - Sum array ; Map to store the values of prefix - Sum ; Perform queries
def prefixSum ( p_arr , arr , n ) : NEW_LINE INDENT p_arr [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT p_arr [ i ] = arr [ i ] + p_arr [ i - 1 ] NEW_LINE DEDENT DEDENT def hashPrefixSum ( p_arr , n , q ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT q [ p_arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT def canDivide ( p_arr , n , q , l , r ) : NEW_LINE INDENT Sum = 0 NEW_LINE if ( l == 0 ) : NEW_LINE INDENT Sum = p_arr [ r ] NEW_LINE DEDENT else : NEW_LINE INDENT Sum = p_arr [ r ] - p_arr [ l - 1 ] NEW_LINE DEDENT if ( Sum % 2 == 1 ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT beg = 0 NEW_LINE if ( l != 0 ) : NEW_LINE INDENT beg = p_arr [ l - 1 ] NEW_LINE DEDENT if ( beg + Sum // 2 in q . keys ( ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 1 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE p_arr = [ 0 for i in range ( n ) ] NEW_LINE prefixSum ( p_arr , arr , n ) NEW_LINE q = dict ( ) NEW_LINE hashPrefixSum ( p_arr , n , q ) NEW_LINE canDivide ( p_arr , n , q , 0 , 1 ) NEW_LINE canDivide ( p_arr , n , q , 1 , 3 ) NEW_LINE canDivide ( p_arr , n , q , 1 , 2 ) NEW_LINE
Cost to make a string Panagram | Set 2 | Function to return the cost to make string a Panagram ; Count the occurrences of each lowercase character ; To store the total gain ; If some character is missing , it has to be added at twice the cost ; If some character appears more than once all of its occurrences except 1 can be traded for some gain ; If gain is more than the cost ; Return the total cost if gain < 0 ; Driver code
def costToPanagram ( string , cost ) : NEW_LINE INDENT n = len ( string ) NEW_LINE occurrences = [ 0 ] * 26 NEW_LINE for i in range ( n ) : NEW_LINE INDENT occurrences [ ord ( string [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT gain = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if occurrences [ i ] == 0 : NEW_LINE INDENT gain -= 2 * cost [ i ] NEW_LINE DEDENT elif occurrences [ i ] > 1 : NEW_LINE INDENT gain += cost [ i ] * ( occurrences [ i ] - 1 ) NEW_LINE DEDENT DEDENT if gain >= 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT return gain * - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT cost = [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ] NEW_LINE string = " geeksforgeeks " NEW_LINE print ( costToPanagram ( string , cost ) ) NEW_LINE DEDENT
Weighted sum of the characters of a string in an array | Set 2 | Function to return the required string score ; create a hash map of strings in str ; Store every string in the map along with its position in the array ; If given string is not present in str [ ] ; Multiply sum of alphabets with position ; Driver code
def strScore ( string , s , n ) : NEW_LINE INDENT m = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ string [ i ] ] = i + 1 NEW_LINE DEDENT if s not in m . keys ( ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT score = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT score += ord ( s [ i ] ) - ord ( ' a ' ) + 1 NEW_LINE DEDENT score = score * m [ s ] NEW_LINE return score NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = [ " geeksforgeeks " , " algorithms " , " stack " ] NEW_LINE s = " algorithms " NEW_LINE n = len ( string ) NEW_LINE score = strScore ( string , s , n ) ; NEW_LINE print ( score ) NEW_LINE DEDENT
Number of subarrays have bitwise OR >= K | Function to return the count of required sub - arrays ; Traverse sub - array [ i . . j ] ; Driver code
def countSubArrays ( arr , n , K ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT bitwise_or = 0 NEW_LINE for k in range ( i , j + 1 ) : NEW_LINE INDENT bitwise_or = bitwise_or | arr [ k ] NEW_LINE DEDENT if ( bitwise_or >= K ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE k = 6 NEW_LINE print ( countSubArrays ( arr , n , k ) ) NEW_LINE DEDENT
Number of subarrays have bitwise OR >= K | Python implementation of the approach ; Function to build the segment tree ; Function to return the bitwise OR of segment [ L . . R ] ; Function to return the count of required sub - arrays ; Build segment tree ; Query segment tree for bitwise OR of sub - array [ i . . j ] ; Driver code
N = 100002 NEW_LINE tree = [ 0 ] * ( 4 * N ) NEW_LINE def build ( arr , node , start , end ) : NEW_LINE INDENT if start == end : NEW_LINE INDENT tree [ node ] = arr [ start ] NEW_LINE return NEW_LINE DEDENT mid = ( start + end ) >> 1 NEW_LINE build ( arr , 2 * node , start , mid ) NEW_LINE build ( arr , 2 * node + 1 , mid + 1 , end ) NEW_LINE tree [ node ] = tree [ 2 * node ] | tree [ 2 * node + 1 ] NEW_LINE DEDENT def query ( node , start , end , l , r ) : NEW_LINE INDENT if start > end or start > r or end < l : NEW_LINE INDENT return 0 NEW_LINE DEDENT if start >= l and end <= r : NEW_LINE INDENT return tree [ node ] NEW_LINE DEDENT mid = ( start + end ) >> 1 NEW_LINE q1 = query ( 2 * node , start , mid , l , r ) NEW_LINE q2 = query ( 2 * node + 1 , mid + 1 , end , l , r ) NEW_LINE return q1 or q2 NEW_LINE DEDENT def countSubArrays ( arr , n , K ) : NEW_LINE INDENT build ( arr , 1 , 0 , n - 1 ) NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT bitwise_or = query ( 1 , 0 , n - 1 , i , j ) NEW_LINE if bitwise_or >= K : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE k = 6 NEW_LINE print ( countSubArrays ( arr , n , k ) ) NEW_LINE
Number of subarrays have bitwise OR >= K | Python3 program to implement the above approach ; Function which builds the segment tree ; Function that returns bitwise OR of segment [ L . . R ] ; Function to count requisite number of subarrays ; Check for subarrays starting with index i ; If OR of subarray [ i . . mid ] >= K , then all subsequent subarrays will have OR >= K therefore reduce high to mid - 1 to find the minimal length subarray [ i . . mid ] having OR >= K ; Increase count with number of subarrays having OR >= K and starting with index i ; Driver code ; Build segment tree .
N = 100002 NEW_LINE tree = [ 0 for i in range ( 4 * N ) ] ; NEW_LINE def build ( arr , node , start , end ) : NEW_LINE INDENT if ( start == end ) : NEW_LINE INDENT tree [ node ] = arr [ start ] ; NEW_LINE return ; NEW_LINE DEDENT mid = ( start + end ) >> 1 ; NEW_LINE build ( arr , 2 * node , start , mid ) ; NEW_LINE build ( arr , 2 * node + 1 , mid + 1 , end ) ; NEW_LINE tree [ node ] = tree [ 2 * node ] | tree [ 2 * node + 1 ] ; NEW_LINE DEDENT def query ( node , start , end , l , r ) : NEW_LINE INDENT if ( start > end or start > r or end < l ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( start >= l and end <= r ) : NEW_LINE INDENT return tree [ node ] ; NEW_LINE DEDENT mid = ( start + end ) >> 1 ; NEW_LINE q1 = query ( 2 * node , start , mid , l , r ) ; NEW_LINE q2 = query ( 2 * node + 1 , mid + 1 , end , l , r ) ; NEW_LINE return q1 | q2 ; NEW_LINE DEDENT def countSubArrays ( arr , n , K ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT low = i NEW_LINE high = n - 1 NEW_LINE index = 1000000000 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) >> 1 ; NEW_LINE if ( query ( 1 , 0 , n - 1 , i , mid ) >= K ) : NEW_LINE INDENT index = min ( index , mid ) ; NEW_LINE high = mid - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 ; NEW_LINE DEDENT DEDENT if ( index != 1000000000 ) : NEW_LINE INDENT count += n - index ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE build ( arr , 1 , 0 , n - 1 ) ; NEW_LINE k = 6 ; NEW_LINE print ( countSubArrays ( arr , n , k ) ) NEW_LINE DEDENT
Occurrences of a pattern in binary representation of a number | Function to return the count of occurrence of pat in binary representation of n ; To store decimal value of the pattern ; To store a number that has all ones in its binary representation and length of ones equal to length of the pattern ; Find values of pattern_int and all_ones ; If the pattern occurs in the last digits of n ; Right shift n by 1 bit ; Driver code
def countPattern ( n , pat ) : NEW_LINE INDENT pattern_int = 0 NEW_LINE power_two = 1 NEW_LINE all_ones = 0 NEW_LINE i = len ( pat ) - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT current_bit = ord ( pat [ i ] ) - ord ( '0' ) NEW_LINE pattern_int += ( power_two * current_bit ) NEW_LINE all_ones = all_ones + power_two NEW_LINE power_two = power_two * 2 NEW_LINE i -= 1 NEW_LINE DEDENT count = 0 NEW_LINE while ( n != 0 and n >= pattern_int ) : NEW_LINE INDENT if ( ( n & all_ones ) == pattern_int ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT n = n >> 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 500 NEW_LINE pat = "10" NEW_LINE print ( countPattern ( n , pat ) ) NEW_LINE DEDENT
Pairs with same Manhattan and Euclidean distance | Python3 implementation of the above approach ; Function to return the number of non coincident pairs of points with manhattan distance equal to euclidean distance ; To store frequency of all distinct Xi ; To store Frequency of all distinct Yi ; To store Frequency of all distinct points ( Xi , Yi ) ; Hash xi coordinate ; Hash yi coordinate ; Hash the point ( xi , yi ) ; find pairs with same Xi ; calculate ( ( xFrequency ) C2 ) ; find pairs with same Yi ; calculate ( ( yFrequency ) C2 ) ; find pairs with same ( Xi , Yi ) ; calculate ( ( xyFrequency ) C2 ) ; Driver Code
from collections import defaultdict NEW_LINE def findManhattanEuclidPair ( arr , n ) : NEW_LINE INDENT X = defaultdict ( lambda : 0 ) NEW_LINE Y = defaultdict ( lambda : 0 ) NEW_LINE XY = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT xi = arr [ i ] [ 0 ] NEW_LINE yi = arr [ i ] [ 1 ] NEW_LINE X [ xi ] += 1 NEW_LINE Y [ yi ] += 1 NEW_LINE XY [ tuple ( arr [ i ] ) ] += 1 NEW_LINE DEDENT xAns , yAns , xyAns = 0 , 0 , 0 NEW_LINE for xCoordinatePair in X : NEW_LINE INDENT xFrequency = X [ xCoordinatePair ] NEW_LINE sameXPairs = ( xFrequency * ( xFrequency - 1 ) ) // 2 NEW_LINE xAns += sameXPairs NEW_LINE DEDENT for yCoordinatePair in Y : NEW_LINE INDENT yFrequency = Y [ yCoordinatePair ] NEW_LINE sameYPairs = ( yFrequency * ( yFrequency - 1 ) ) // 2 NEW_LINE yAns += sameYPairs NEW_LINE DEDENT for XYPair in XY : NEW_LINE INDENT xyFrequency = XY [ XYPair ] NEW_LINE samePointPairs = ( xyFrequency * ( xyFrequency - 1 ) ) // 2 NEW_LINE xyAns += samePointPairs NEW_LINE DEDENT return ( xAns + yAns - xyAns ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 1 , 3 ] ] NEW_LINE n = len ( arr ) NEW_LINE print ( findManhattanEuclidPair ( arr , n ) ) NEW_LINE DEDENT
Remove exactly one element from the array such that max | function to calculate max - min ; Driver code
def max_min ( a , n ) : NEW_LINE INDENT a . sort ( ) NEW_LINE return min ( a [ n - 2 ] - a [ 0 ] , a [ n - 1 ] - a [ 1 ] ) NEW_LINE DEDENT a = [ 1 , 3 , 3 , 7 ] NEW_LINE n = len ( a ) NEW_LINE print ( max_min ( a , n ) ) NEW_LINE
Count numbers < = N whose difference with the count of primes upto them is > = K | Python3 implementation of the above approach ; primeUpto [ i ] denotes count of prime numbers upto i ; Function to compute all prime numbers and update primeUpto array ; 0 and 1 are not primes ; If i is prime ; Set all multiples of i as non - prime ; Compute primeUpto array ; Function to return the count of valid numbers ; Compute primeUpto array ; Check if the number is valid , try to reduce it ; ans is the minimum valid number ; Driver Code
MAX = 1000001 NEW_LINE MAX_sqrt = MAX ** ( 0.5 ) NEW_LINE primeUpto = [ 0 ] * ( MAX ) NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT isPrime = [ 1 ] * ( MAX ) NEW_LINE isPrime [ 0 ] , isPrime [ 1 ] = 0 , 0 NEW_LINE for i in range ( 2 , int ( MAX_sqrt ) ) : NEW_LINE INDENT if isPrime [ i ] == 1 : NEW_LINE INDENT for j in range ( i * 2 , MAX , i ) : NEW_LINE INDENT isPrime [ j ] = 0 NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , MAX ) : NEW_LINE INDENT primeUpto [ i ] = primeUpto [ i - 1 ] NEW_LINE if isPrime [ i ] == 1 : NEW_LINE INDENT primeUpto [ i ] += 1 NEW_LINE DEDENT DEDENT DEDENT def countOfNumbers ( N , K ) : NEW_LINE INDENT SieveOfEratosthenes ( ) NEW_LINE low , high , ans = 1 , N , 0 NEW_LINE while low <= high : NEW_LINE INDENT mid = ( low + high ) >> 1 NEW_LINE if mid - primeUpto [ mid ] >= K : NEW_LINE INDENT ans = mid NEW_LINE high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT return ( N - ans + 1 ) if ans else 0 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , K = 10 , 3 NEW_LINE print ( countOfNumbers ( N , K ) ) NEW_LINE DEDENT
Find the element whose multiplication with | Function to find minimum index such that sum becomes 0 when the element is multiplied by - 1 ; Find array sum ; Find element with value equal to sum / 2 ; when sum is equal to 2 * element then this is our required element ; Driver code
def minIndex ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( 2 * arr [ i ] == sum ) : NEW_LINE INDENT return ( i + 1 ) NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT arr = [ 1 , 3 , - 5 , 3 , 4 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( minIndex ( arr , n ) ) NEW_LINE
Ceiling of every element in same array | Python implementation of efficient algorithm to find floor of every element ; Prints greater elements on left side of every element ; Create a sorted copy of arr [ ] ; Traverse through arr [ ] and do binary search for every element ; Find the location of first element that is greater than the given element ; Since arr [ i ] also exists in array , v [ it - 1 ] will be same as arr [ i ] . Let us check v [ it - 2 ] is also same as arr [ i ] . If true , then arr [ i ] exists twice in array , so ceiling is same same as arr [ i ] ; If next element is also same , then there are multiple occurrences , so print it ; Driver code
import bisect NEW_LINE def printPrevGreater ( arr , n ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT v = list ( arr ) NEW_LINE v . sort ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT it = bisect . bisect_right ( v , arr [ i ] ) NEW_LINE if ( it - 1 ) != 0 and v [ it - 2 ] == arr [ i ] : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT elif it <= n - 1 : NEW_LINE INDENT print ( v [ it ] , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 5 , 11 , 10 , 20 , 12 ] NEW_LINE n = len ( arr ) NEW_LINE printPrevGreater ( arr , n ) NEW_LINE DEDENT
Floor of every element in same array | Prints greater elements on left side of every element ; Create a sorted copy of arr ; Traverse through arr [ ] and do binary search for every element . ; Floor of first element is - 1 if there is only one occurrence of it . ; Find the first element that is greater than or or equal to given element ; If next element is also same , then there are multiple occurrences , so print it ; Otherwise print previous element ; Driver Code
def printPrevGreater ( arr , n ) : NEW_LINE INDENT v = arr . copy ( ) NEW_LINE v . sort ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == v [ 0 ] ) : NEW_LINE INDENT if ( arr [ i ] == v [ 1 ] ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 , end = " ▁ " ) NEW_LINE DEDENT continue NEW_LINE DEDENT if v . count ( arr [ i ] ) > 0 : NEW_LINE INDENT it = v [ v . index ( arr [ i ] ) ] NEW_LINE DEDENT else : NEW_LINE INDENT it = v [ n - 1 ] NEW_LINE DEDENT if ( it != v [ n - 1 ] and v [ v . index ( it ) + 1 ] == arr [ i ] ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( v [ v . index ( it ) - 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 6 , 11 , 7 , 8 , 20 , 12 ] NEW_LINE n = len ( arr ) NEW_LINE printPrevGreater ( arr , n ) NEW_LINE DEDENT
Find smallest and largest element from square matrix diagonals | Python3 program to find smallest and largest elements of both diagonals ; Function to find smallest and largest element from principal and secondary diagonal ; take length of matrix ; declare and initialize variables with appropriate value ; take new smallest value ; take new largest value ; Condition for secondary diagonal is mat [ n - 1 - i ] [ i ] take new smallest value ; take new largest value ; Declare and initialize 5 X5 matrix
n = 5 NEW_LINE def diagonalsMinMax ( mat ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT principalMin = mat [ 0 ] [ 0 ] NEW_LINE principalMax = mat [ 0 ] [ 0 ] NEW_LINE secondaryMin = mat [ n - 1 ] [ 0 ] NEW_LINE secondaryMax = mat [ n - 1 ] [ 0 ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( mat [ i ] [ i ] < principalMin ) : NEW_LINE INDENT principalMin = mat [ i ] [ i ] NEW_LINE DEDENT if ( mat [ i ] [ i ] > principalMax ) : NEW_LINE INDENT principalMax = mat [ i ] [ i ] NEW_LINE DEDENT if ( mat [ n - 1 - i ] [ i ] < secondaryMin ) : NEW_LINE INDENT secondaryMin = mat [ n - 1 - i ] [ i ] NEW_LINE DEDENT if ( mat [ n - 1 - i ] [ i ] > secondaryMax ) : NEW_LINE INDENT secondaryMax = mat [ n - 1 - i ] [ i ] NEW_LINE DEDENT DEDENT print ( " Principal ▁ Diagonal ▁ Smallest ▁ Element : ▁ " , principalMin ) NEW_LINE print ( " Principal ▁ Diagonal ▁ Greatest ▁ Element ▁ : ▁ " , principalMax ) NEW_LINE print ( " Secondary ▁ Diagonal ▁ Smallest ▁ Element : ▁ " , secondaryMin ) NEW_LINE print ( " Secondary ▁ Diagonal ▁ Greatest ▁ Element : ▁ " , secondaryMax ) NEW_LINE DEDENT matrix = [ [ 1 , 2 , 3 , 4 , - 10 ] , [ 5 , 6 , 7 , 8 , 6 ] , [ 1 , 2 , 11 , 3 , 4 ] , [ 5 , 6 , 70 , 5 , 8 ] , [ 4 , 9 , 7 , 1 , - 5 ] ] NEW_LINE diagonalsMinMax ( matrix ) NEW_LINE