codeparrot/xlcost-text-to-code huggingface dataset:
Queries to find the minimum array sum possible by removing elements from either end | Function to find the minimum sum for each query after removing elements from either ends ; Traverse the query array ; Traverse the array from the front ; If element equals val , then break out of loop ; Traverse the array from rear ; If element equals val , break ; Prthe minimum of the two as the answer ; Driver Code ; Function Call
def minSum ( arr , N , Q , M ) : NEW_LINE INDENT for i in range ( M ) : NEW_LINE INDENT val = Q [ i ] NEW_LINE front , rear = 0 , 0 NEW_LINE for j in range ( N ) : NEW_LINE INDENT front += arr [ j ] NEW_LINE if ( arr [ j ] == val ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for j in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT rear += arr [ j ] NEW_LINE if ( arr [ j ] == val ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT print ( min ( front , rear ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 6 , 7 , 4 , 5 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE Q = [ 7 , 6 ] NEW_LINE M = len ( Q ) NEW_LINE minSum ( arr , N , Q , M ) NEW_LINE DEDENT
Frequency of lexicographically Kth smallest character in the a string | Function to find the frequency of the lexicographically Kth smallest character ; Convert the string to array of characters ; Sort the array in ascending order ; Store the Kth character ; Store the frequency of the K - th character ; Count the frequency of the K - th character ; Prthe count ; Driver Code
def KthCharacter ( S , N , K ) : NEW_LINE INDENT strarray = [ char for char in S ] ; NEW_LINE strarray . sort ( ) ; NEW_LINE ch = strarray [ K - 1 ] ; NEW_LINE count = 0 ; NEW_LINE for c in strarray : NEW_LINE INDENT if ( c == ch ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT print ( count ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " geeksforgeeks " ; NEW_LINE N = len ( S ) ; NEW_LINE K = 3 ; NEW_LINE KthCharacter ( S , N , K ) ; NEW_LINE DEDENT
Longest substring with no pair of adjacent characters are adjacent English alphabets | Function to find the longest substring satisfying the given condition ; Stores all temporary substrings ; Stores the longest substring ; Stores the length of the subT ; Stores the first character of S ; Traverse the string ; If the absolute difference is 1 ; Update the length of subT ; Update the longest substring ; Otherwise , stores the current character ; Again checking for longest substring and update accordingly ; Print the longest substring ; Driver Code ; Given string ; Function call to find the longest substring satisfying given condition
def findSubstring ( S ) : NEW_LINE INDENT T = " " NEW_LINE ans = " " NEW_LINE l = 0 NEW_LINE T += S [ 0 ] NEW_LINE for i in range ( 1 , len ( S ) ) : NEW_LINE INDENT if ( abs ( ord ( S [ i ] ) - ord ( S [ i - 1 ] ) ) == 1 ) : NEW_LINE INDENT l = len ( T ) NEW_LINE if ( l > len ( ans ) ) : NEW_LINE INDENT ans = T NEW_LINE DEDENT T = " " NEW_LINE T += S [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT T += S [ i ] NEW_LINE DEDENT DEDENT l = len ( T ) NEW_LINE if ( l > len ( ans ) ) : NEW_LINE INDENT ans = T NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " aabdml " NEW_LINE findSubstring ( S ) NEW_LINE DEDENT
Minimize length of an array by removing similar subarrays from both ends | Function to minimize length of the array by removing similar subarrays from both ends of the array ; Initialize two pointers ; Stores the current integer ; Check if the elements at both ends are same or not ; Move the front pointer ; Move the rear pointer ; Print the minimized length of the array ; Input ; Function call to find the minimized length of the array
def findMinLength ( arr , N ) : NEW_LINE INDENT front = 0 NEW_LINE back = N - 1 NEW_LINE while ( front < back ) : NEW_LINE INDENT x = arr [ front ] NEW_LINE if arr [ front ] != arr [ back ] : NEW_LINE INDENT break NEW_LINE DEDENT while ( arr [ front ] == x and front <= back ) : NEW_LINE INDENT front += 1 NEW_LINE DEDENT while ( arr [ back ] == x and front <= back ) : NEW_LINE INDENT back -= 1 NEW_LINE DEDENT DEDENT print ( back - front + 1 ) NEW_LINE DEDENT arr = [ 1 , 1 , 2 , 3 , 3 , 1 , 2 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE findMinLength ( arr , N ) NEW_LINE
Digits whose alphabetic representations are jumbled in a given string | Function to convert the jumbled into digits ; Strings of digits 0 - 9 ; Initialize vector ; Initialize answer ; Size of the string ; Traverse the string ; Update the elements of the vector ; Print the digits into their original format ; Return answer ; Driver Code
def finddigits ( s ) : NEW_LINE INDENT num = [ " zero " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " ] NEW_LINE arr = [ 0 ] * ( 10 ) NEW_LINE ans = " " NEW_LINE n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == ' z ' ) : NEW_LINE INDENT arr [ 0 ] += 1 NEW_LINE DEDENT if ( s [ i ] == ' w ' ) : NEW_LINE INDENT arr [ 2 ] += 1 NEW_LINE DEDENT if ( s [ i ] == ' g ' ) : NEW_LINE INDENT arr [ 8 ] += 1 NEW_LINE DEDENT if ( s [ i ] == ' x ' ) : NEW_LINE INDENT arr [ 6 ] += 1 NEW_LINE DEDENT if ( s [ i ] == ' v ' ) : NEW_LINE INDENT arr [ 5 ] += 1 NEW_LINE DEDENT if ( s [ i ] == ' o ' ) : NEW_LINE INDENT arr [ 1 ] += 1 NEW_LINE DEDENT if ( s [ i ] == ' s ' ) : NEW_LINE INDENT arr [ 7 ] += 1 NEW_LINE DEDENT if ( s [ i ] == ' f ' ) : NEW_LINE INDENT arr [ 4 ] += 1 NEW_LINE DEDENT if ( s [ i ] == ' h ' ) : NEW_LINE INDENT arr [ 3 ] += 1 NEW_LINE DEDENT if ( s [ i ] == ' i ' ) : NEW_LINE INDENT arr [ 9 ] += 1 NEW_LINE DEDENT DEDENT arr [ 7 ] -= arr [ 6 ] NEW_LINE arr [ 5 ] -= arr [ 7 ] NEW_LINE arr [ 4 ] -= arr [ 5 ] NEW_LINE arr [ 1 ] -= ( arr [ 2 ] + arr [ 4 ] + arr [ 0 ] ) NEW_LINE arr [ 3 ] -= arr [ 8 ] NEW_LINE arr [ 9 ] -= ( arr [ 5 ] + arr [ 6 ] + arr [ 8 ] ) NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT for j in range ( arr [ i ] ) : NEW_LINE INDENT ans += chr ( ( i ) + ord ( '0' ) ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " owoftnuoer " NEW_LINE print ( finddigits ( s ) ) NEW_LINE DEDENT
Check if two binary strings can be made equal by swapping pairs of unequal characters | Function to check if a string s1 can be converted into s2 ; Count of '0' in strings in s1 and s2 ; Iterate both the strings and count the number of occurrences of ; Count is not equal ; Iterating over both the arrays and count the number of occurrences of '0 ; If the count of occurrences of '0' in S2 exceeds that in S1 ; Driver code
def check ( s1 , s2 ) : NEW_LINE INDENT s1_0 = 0 NEW_LINE s2_0 = 0 NEW_LINE for i in range ( len ( s1 ) ) : NEW_LINE INDENT if ( s1 [ i ] == '0' ) : NEW_LINE INDENT s1_0 += 1 NEW_LINE DEDENT if ( s2 [ i ] == '0' ) : NEW_LINE INDENT s2_0 += 1 NEW_LINE DEDENT DEDENT if ( s1_0 != s2_0 ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT Count1 = 0 NEW_LINE Count2 = 0 ; NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT for i in range ( len ( s1 ) ) : NEW_LINE INDENT if ( s1 [ i ] == '0' ) : NEW_LINE INDENT Count1 += 1 NEW_LINE DEDENT if ( s2 [ i ] == '0' ) : NEW_LINE INDENT Count2 += 1 NEW_LINE DEDENT if ( Count1 < Count2 ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " YES " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = "100111" NEW_LINE s2 = "111010" NEW_LINE check ( s1 , s2 ) NEW_LINE s1 = "110100" NEW_LINE s2 = "010101" NEW_LINE check ( s1 , s2 ) NEW_LINE DEDENT
Check if two binary strings can be made equal by swapping 1 s occurring before 0 s | Function to check if it is possible to make two binary strings equal by given operations ; Stores count of 1 ' s ▁ and ▁ 0' s of the string str1 ; Stores count of 1 ' s ▁ and ▁ 0' s of the string str2 ; Stores current count of 1 's  presenty in the string str1 ; Count the number of 1 ' s ▁ and ▁ 0' s present in the strings str1 and str2 ; If the number of 1 ' s ▁ and ▁ 0' s are not same of the strings str1 and str2 then prnot possible ; Traversing through the strings str1 and str2 ; If the str1 character not equals to str2 character ; Swaps 0 with 1 of the string str1 ; Breaks the loop as the count of 1 's is zero. Hence, no swaps possible ; Swaps 1 with 0 in the string str1 ; Prnot possible ; Given Strings ; Function Call
def isBinaryStringsEqual ( list1 , list2 ) : NEW_LINE INDENT str1 = list ( list1 ) NEW_LINE str2 = list ( list2 ) NEW_LINE str1Zeros = 0 NEW_LINE str1Ones = 0 NEW_LINE str2Zeros = 0 NEW_LINE str2Ones = 0 NEW_LINE flag = 0 NEW_LINE curStr1Ones = 0 NEW_LINE for i in range ( len ( str1 ) ) : NEW_LINE INDENT if ( str1 [ i ] == '1' ) : NEW_LINE INDENT str1Ones += 1 NEW_LINE DEDENT elif ( str1 [ i ] == '0' ) : NEW_LINE INDENT str1Zeros += 1 NEW_LINE DEDENT if ( str2 [ i ] == '1' ) : NEW_LINE INDENT str2Ones += 1 NEW_LINE DEDENT elif ( str2 [ i ] == '0' ) : NEW_LINE INDENT str2Zeros += 1 NEW_LINE DEDENT DEDENT if ( str1Zeros != str2Zeros and str1Ones != str2Ones ) : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( len ( str1 ) ) : NEW_LINE INDENT if ( str1 [ i ] != str2 [ i ] ) : NEW_LINE INDENT if ( str1 [ i ] == '0' and curStr1Ones > 0 ) : NEW_LINE INDENT str1 [ i ] = '1' NEW_LINE curStr1Ones -= 1 NEW_LINE DEDENT if ( str1 [ i ] == '0' and curStr1Ones == 0 ) : NEW_LINE INDENT flag += 1 NEW_LINE break NEW_LINE DEDENT if ( str1 [ i ] == '1' and str2 [ i ] == '0' ) : NEW_LINE INDENT str1 [ i ] = '0' NEW_LINE curStr1Ones += 1 NEW_LINE DEDENT DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( " Possible " ) NEW_LINE DEDENT else : NEW_LINE print ( " Not ▁ Possible " ) NEW_LINE DEDENT DEDENT str1 = "0110" NEW_LINE str2 = "0011" NEW_LINE isBinaryStringsEqual ( str1 , str2 ) NEW_LINE
Count array elements whose all distinct digits appear in K | Function to the count of array elements whose distinct digits are a subst of the digits of K ; Stores distinct digits of K ; Iterate over all the digits of K ; Insert current digit into st ; Update K ; Stores the count of array elements whose distinct digits are a subst of the digits of K ; Traverse the array , arr [ ] ; Stores current element ; Check if all the digits of arr [ i ] are present in K or not ; Iterate over all the digits of arr [ i ] ; Stores current digit ; If digit not present in the st ; Update flag ; Update no ; If all the digits of arr [ i ] present in st ; Update count ; Driver Code
def noOfValidKbers ( K , arr ) : NEW_LINE INDENT st = { } NEW_LINE while ( K != 0 ) : NEW_LINE INDENT if ( K % 10 in st ) : NEW_LINE INDENT st [ K % 10 ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT st [ K % 10 ] = st . get ( K % 10 , 0 ) + 1 NEW_LINE DEDENT K = K // 10 NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT no = arr [ i ] NEW_LINE flag = True NEW_LINE while ( no != 0 ) : NEW_LINE INDENT digit = no % 10 NEW_LINE if ( digit not in st ) : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT no = no // 10 NEW_LINE DEDENT if ( flag == True ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 12 NEW_LINE arr = [ 1 , 12 , 1222 , 13 , 2 ] NEW_LINE print ( noOfValidKbers ( K , arr ) ) NEW_LINE DEDENT
Smallest Subtree with all the Deepest Nodes | Structure of a Node ; Function to return depth of the Tree from root ; If current node is a leaf node ; Function to find the root of the smallest subtree consisting of all deepest nodes ; Stores height of left subtree ; Stores height of right subtree ; If height of left subtree exceeds that of the right subtree ; Traverse left subtree ; If height of right subtree exceeds that of the left subtree ; Otherwise ; Return current node ; Driver Code
class TreeNode : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . val = x NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def find_ht ( root ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( root . left == None and root . right == None ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return max ( find_ht ( root . left ) , find_ht ( root . right ) ) + 1 NEW_LINE DEDENT def find_node ( root ) : NEW_LINE INDENT global req_node NEW_LINE if ( not root ) : NEW_LINE INDENT return NEW_LINE DEDENT left_ht = find_ht ( root . left ) NEW_LINE right_ht = find_ht ( root . right ) NEW_LINE if ( left_ht > right_ht ) : NEW_LINE INDENT find_node ( root . left ) NEW_LINE DEDENT elif ( right_ht > left_ht ) : NEW_LINE INDENT find_node ( root . right ) NEW_LINE DEDENT else : NEW_LINE INDENT req_node = root NEW_LINE return NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = TreeNode ( 1 ) NEW_LINE root . left = TreeNode ( 2 ) NEW_LINE root . right = TreeNode ( 3 ) NEW_LINE req_node = None NEW_LINE find_node ( root ) NEW_LINE print ( req_node . val ) NEW_LINE DEDENT
Maximum even numbers present in any subarray of size K | Function to find the maximum count of even numbers from all the subarrays of size K ; Stores the maximum count of even numbers from all the subarrays of size K ; Generate all subarrays of size K ; Store count of even numbers in current subarray of size K ; Traverse the current subarray ; If current element is an even number ; Update the answer ; Return answer ; Driver Code ; Size of the input array
def maxEvenIntegers ( arr , N , K ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( N - K + 1 ) : NEW_LINE INDENT cnt = 0 NEW_LINE for j in range ( 0 , K ) : NEW_LINE INDENT if arr [ i + j ] % 2 == 0 : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT ans = max ( cnt , ans ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 5 , 4 , 7 , 6 ] NEW_LINE K = 3 NEW_LINE N = len ( arr ) NEW_LINE print ( maxEvenIntegers ( arr , N , K ) ) NEW_LINE DEDENT
Count maximum concatenation of pairs from given array that are divisible by 3 | Function to count pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Stores count of array elements whose remainder is 0 by taking modulo by 3 ; Stores count of array elements whose remainder is 1 by taking modulo by 3 ; Stores count of array elements whose remainder is 2 by taking modulo by 3 ; Traverse the array ; Stores sum of digits of arr [ i ] ; Update digitSum ; If remainder of digitSum by by taking modulo 3 is 0 ; Update rem0 ; If remainder of digitSum by by taking modulo 3 is 1 ; Update rem1 ; Update rem2 ; Driver Code ; To display the result
def countDiv ( arr ) : NEW_LINE INDENT rem0 = 0 NEW_LINE rem1 = 0 NEW_LINE rem2 = 0 NEW_LINE for i in arr : NEW_LINE INDENT digitSum = 0 NEW_LINE for digit in str ( i ) : NEW_LINE INDENT digitSum += int ( digit ) NEW_LINE DEDENT if digitSum % 3 == 0 : NEW_LINE INDENT rem0 += 1 NEW_LINE DEDENT elif digitSum % 3 == 1 : NEW_LINE INDENT rem1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT rem2 += 1 NEW_LINE DEDENT DEDENT return ( rem0 // 2 + min ( rem1 , rem2 ) ) NEW_LINE DEDENT arr = [ 5 , 3 , 2 , 8 , 7 ] NEW_LINE print ( countDiv ( arr ) ) NEW_LINE
Maximum Sum Subsequence | Function to print the maximum non - emepty subsequence sum ; Stores the maximum non - emepty subsequence sum in an array ; Stores the largest element in the array ; Traverse the array ; If a [ i ] is greater than 0 ; Update sum ; Driver Code
def MaxNonEmpSubSeq ( a , n ) : NEW_LINE INDENT sum = 0 NEW_LINE maxm = max ( a ) NEW_LINE if ( maxm <= 0 ) : NEW_LINE INDENT return maxm NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] > 0 ) : NEW_LINE INDENT sum += a [ i ] NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 2 , 11 , - 4 , 2 , - 3 , - 10 ] NEW_LINE N = len ( arr ) NEW_LINE print ( MaxNonEmpSubSeq ( arr , N ) ) NEW_LINE DEDENT
Count numbers up to N having Kth bit set | Function to return the count of number of 1 's at ith bit in a range [1, n - 1] ; Store count till nearest power of 2 less than N ; If K - th bit is set in N ; Add to result the nearest power of 2 less than N ; Return result ; Driver Code ; Function Call
def getcount ( n , k ) : NEW_LINE INDENT res = ( n >> ( k + 1 ) ) << k NEW_LINE if ( ( n >> k ) & 1 ) : NEW_LINE INDENT res += n & ( ( 1 << k ) - 1 ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 14 NEW_LINE K = 2 NEW_LINE print ( getcount ( N + 1 , K ) ) NEW_LINE DEDENT
Check if a string can be split into two substrings such that one substring is a substring of the other | Function to check if a can be divided into two substrings such that one subis subof the other ; Store the last character of S ; Traverse the characters at indices [ 0 , N - 2 ] ; Check if the current character is equal to the last character ; If true , set f = 1 ; Break out of the loop ; Driver Code ; Given string , S ; Store the size of S ; Function Call
def splitString ( S , N ) : NEW_LINE INDENT c = S [ N - 1 ] NEW_LINE f = 0 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if ( S [ i ] == c ) : NEW_LINE INDENT f = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( f ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " abcdab " NEW_LINE N = len ( S ) NEW_LINE splitString ( S , N ) NEW_LINE DEDENT
Find the light bulb with maximum glowing time | Python3 program for the above approach ; Function to find the bulb having maximum glow ; Initialize variables ; Traverse the array consisting of glowing time of the bulbs ; For 1 st bulb ; Calculate the glowing time ; Update the maximum glow ; Find lexicographically largest bulb ; Bulb with maximum time ; Return the resultant bulb ; Driver Code ; Function call
import sys NEW_LINE INT_MIN = ( sys . maxsize - 1 ) NEW_LINE def longestLastingBulb ( onTime , s ) : NEW_LINE INDENT n = len ( onTime ) NEW_LINE maxDur = INT_MIN NEW_LINE maxPos = INT_MIN NEW_LINE currentDiff = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT currentDiff = onTime [ i ] NEW_LINE maxDur = currentDiff NEW_LINE maxPos = i NEW_LINE DEDENT else : NEW_LINE INDENT currentDiff = onTime [ i ] - onTime [ i - 1 ] NEW_LINE if ( maxDur < currentDiff ) : NEW_LINE INDENT maxDur = currentDiff NEW_LINE maxPos = i NEW_LINE DEDENT else : NEW_LINE INDENT if ( maxDur == currentDiff ) : NEW_LINE INDENT one = s [ i ] NEW_LINE two = s [ maxPos ] NEW_LINE if ( one > two ) : NEW_LINE INDENT maxDur = currentDiff NEW_LINE maxPos = i NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT ans = s [ maxPos ] NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " spuda " NEW_LINE arr = [ 12 , 23 , 36 , 46 , 62 ] NEW_LINE print ( longestLastingBulb ( arr , S ) ) NEW_LINE DEDENT
Find a pair of intersecting ranges from a given array | Function to find a pair of intersecting ranges ; Stores ending po of every range ; Stores the maximum ending poobtained ; Iterate from 0 to N - 1 ; Starting point of the current range ; End point of the current range ; Push pairs into tup ; Sort the tup vector ; Iterate over the ranges ; If starting points are equal ; Prthe indices of the intersecting ranges ; If no such pair of segments exist ; Driver Code ; Given N ; Given 2d ranges [ ] [ ] array ; Function call
def findIntersectingRange ( tup , N , ranges ) : NEW_LINE INDENT curr = 0 NEW_LINE currPos = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = ranges [ i ] [ 0 ] NEW_LINE y = ranges [ i ] [ 1 ] NEW_LINE tup . append ( [ [ x , y ] , i + 1 ] ) NEW_LINE DEDENT tup = sorted ( tup ) NEW_LINE curr = tup [ 0 ] [ 0 ] [ 1 ] NEW_LINE currPos = tup [ 0 ] [ 1 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT Q = tup [ i - 1 ] [ 0 ] [ 0 ] NEW_LINE R = tup [ i ] [ 0 ] [ 0 ] NEW_LINE if ( Q == R ) : NEW_LINE INDENT if ( tup [ i - 1 ] [ 0 ] [ 1 ] < tup [ i ] [ 0 ] [ 1 ] ) : NEW_LINE INDENT print ( tup [ i - 1 ] [ 1 ] , tup [ i ] [ 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( tup [ i ] [ 1 ] , tup [ i - 1 ] [ 1 ] ) NEW_LINE DEDENT return NEW_LINE DEDENT T = tup [ i ] [ 0 ] [ 1 ] NEW_LINE if ( T <= curr ) : NEW_LINE INDENT print ( tup [ i ] [ 1 ] , currPos ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT curr = T NEW_LINE currPos = tup [ i ] [ 1 ] NEW_LINE DEDENT DEDENT print ( " - 1 ▁ - 1" ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE ranges = [ [ 1 , 5 ] , [ 2 , 10 ] , [ 3 , 10 ] , [ 2 , 2 ] , [ 2 , 15 ] ] NEW_LINE findIntersectingRange ( [ ] , N , ranges ) NEW_LINE DEDENT
Find the next greater element in a Circular Array | Set 2 | Function to find the NGE for the given circular array arr [ ] ; create stack list ; Initialize nge [ ] array to - 1 ; Traverse the array ; If stack is not empty and current element is greater than top element of the stack ; Assign next greater element for the top element of the stack ; Pop the top element of the stack ; Print the nge [ ] array ; Driver Code ; Function Call
def printNGE ( arr , n ) : NEW_LINE INDENT s = [ ] ; NEW_LINE nge = [ - 1 ] * n ; NEW_LINE i = 0 ; NEW_LINE while ( i < 2 * n ) : NEW_LINE INDENT while ( len ( s ) != 0 and arr [ i % n ] > arr [ s [ - 1 ] ] ) : NEW_LINE INDENT nge [ s [ - 1 ] ] = arr [ i % n ] ; NEW_LINE s . pop ( ) ; NEW_LINE DEDENT s . append ( i % n ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( nge [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , - 2 , 5 , 8 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE printNGE ( arr , N ) ; NEW_LINE DEDENT
Count array elements whose product of digits is a Composite Number | Python3 program for the above approach ; Function to generate prime numbers using Sieve of Eratosthenes ; Set 0 and 1 as non - prime ; If p is a prime ; Set all multiples of p as non - prime ; Function to calculate the product of digits of the given number ; Stores the product of digits ; Extract digits and add to the sum ; Return the product of its digits ; Function to print number of distinct values with digit product as composite ; Initialize set ; Initialize boolean array ; Pre - compute primes ; Traverse array ; of the current array element ; If Product of digits is less than or equal to 1 ; If Product of digits is not a prime ; Print the answer ; Driver Code ; Given array ; Given size ; Function call
N = 100005 NEW_LINE from math import sqrt NEW_LINE def SieveOfEratosthenes ( prime , p_size ) : NEW_LINE INDENT prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , int ( sqrt ( p_size ) ) , 1 ) : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT for i in range ( p * 2 , p_size + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def digitProduct ( number ) : NEW_LINE INDENT res = 1 NEW_LINE while ( number > 0 ) : NEW_LINE INDENT res *= ( number % 10 ) NEW_LINE number //= 10 NEW_LINE DEDENT return res NEW_LINE DEDENT def DistinctCompositeDigitProduct ( arr , n ) : NEW_LINE INDENT output = set ( ) NEW_LINE prime = [ True for i in range ( N + 1 ) ] NEW_LINE SieveOfEratosthenes ( prime , N ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans = digitProduct ( arr [ i ] ) NEW_LINE if ( ans <= 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( prime [ ans ] == False ) : NEW_LINE INDENT output . add ( ans ) NEW_LINE DEDENT DEDENT print ( len ( output ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 13 , 55 , 7 , 13 , 11 , 71 , 233 , 233 , 144 , 89 ] NEW_LINE n = len ( arr ) NEW_LINE DistinctCompositeDigitProduct ( arr , n ) NEW_LINE DEDENT