codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of steps required to place all 1 s at a single index | Function to print minimum steps required to shift all 1 s to a single index in a binary array ; Size of array ; Used to store cumulative sum ; Initialize count ; Traverse the array in forward direction ; Steps needed to store all previous ones to ith index ; Count number of 1 s present till i - th index ; Initialize count ; Traverse the array in backward direction ; Steps needed to store all 1 s to the right of i at current index ; Count number of 1 s present after i - th index ; Print the number of steps required ; Driver Code
def minsteps ( A ) : NEW_LINE INDENT n = len ( A ) NEW_LINE left , right , res = [ 0 ] * n , [ 0 ] * n , [ 0 ] * n NEW_LINE count = A [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT left [ i ] = left [ i - 1 ] + count NEW_LINE count += A [ i ] NEW_LINE DEDENT count = A [ n - 1 ] NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT right [ i ] = right [ i + 1 ] + count NEW_LINE count += A [ i ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT res [ i ] = left [ i ] + right [ i ] NEW_LINE print ( res [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 0 , 1 , 0 ] NEW_LINE minsteps ( A ) NEW_LINE DEDENT
Minimize flips on K | Function to find the minimum number K - length subarrays required to be flipped to make all array elements 1 ; Stores whether an element can be flipped or not ; Store the required number of flips ; Traverse the array , A [ ] ; Find the prefix sum for the indices i > 0 ; Check if the current element is required to be flipped ; If subarray of size K is not possible , then print - 1 and return ; Increment ans by 1 ; Change the current state of the element ; Decrement isFlipped [ i + K ] ; If subarray of size K is not possible , then print - 1 and return ; Increment ans by 1 ; Change the current state of the element ; Decrement isFlipped [ i + K ] ; Print the result ; Driver Code
def minimumOperations ( A , K ) : NEW_LINE INDENT isflipped = [ 0 ] * ( len ( A ) + 1 ) NEW_LINE ans = 0 NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT if ( i > 0 ) : NEW_LINE INDENT isflipped [ i ] += isflipped [ i - 1 ] NEW_LINE isflipped [ i ] %= 2 NEW_LINE DEDENT if ( A [ i ] == 0 and not isflipped [ i ] ) : NEW_LINE INDENT if ( ( len ( A ) - i + 1 ) <= K ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT ans += 1 NEW_LINE isflipped [ i ] += 1 NEW_LINE isflipped [ i + K ] -= 1 NEW_LINE DEDENT elif ( A [ i ] == 1 and isflipped [ i ] ) : NEW_LINE INDENT if ( ( len ( A ) - i + 1 ) <= K ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT ans += 1 NEW_LINE isflipped [ i ] += 1 NEW_LINE isflipped [ i + K ] -= 1 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 0 , 1 , 0 ] NEW_LINE K = 1 NEW_LINE minimumOperations ( arr , K ) NEW_LINE DEDENT
Calculate sum of scores after N days based on given conditions | Function to c sum of calculate sum of scores after n days ; Store the required sum ; Store the score on previous monday and current day respectively ; Iterate over the range [ 1 , n ] ; If the current day is monday ; Increment score of prev_monday by 1 ; Update score of current day ; Add score of current day and increment score for next day ; Print the result ; Driver Code
def findScoreSum ( n ) : NEW_LINE INDENT total = 0 NEW_LINE prev_monday , curr_day = 0 , 0 NEW_LINE for day in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( day % 7 == 1 ) : NEW_LINE INDENT prev_monday += 1 NEW_LINE curr_day = prev_monday NEW_LINE DEDENT total += curr_day NEW_LINE curr_day += 1 NEW_LINE DEDENT print ( total ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 NEW_LINE findScoreSum ( N ) NEW_LINE DEDENT
Calculate sum of scores after N days based on given conditions | Function to calculate sum of scores after n days ; Store the number of full weeks ; Stores the remaining days in the last week ; Store the sum of scores in the first F full weeks ; Store the sum of scores in the last week ; Print the result ; Driver Code
def findScoreSum ( n ) : NEW_LINE INDENT F = n // 7 NEW_LINE D = n % 7 NEW_LINE fullWeekScore = ( 49 + 7 * F ) * F // 2 NEW_LINE lastNonFullWeekScore = ( 2 * F + D + 1 ) * D // 2 NEW_LINE print ( fullWeekScore + lastNonFullWeekScore ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 NEW_LINE findScoreSum ( N ) NEW_LINE DEDENT
Largest number up to N whose modulus with X is equal to Y modulo X | Function to print the largest number upto N whose modulus with X is same as Y % X ; Stores the required number ; Update num as the result ; Return the resultant number ; Driver Code
def maximumNum ( X , Y , N ) : NEW_LINE INDENT num = 0 NEW_LINE if ( N - N % X + Y <= N ) : NEW_LINE INDENT num = N - N % X + Y NEW_LINE DEDENT else : NEW_LINE INDENT num = N - N % X - ( X - Y ) NEW_LINE DEDENT return num NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 10 NEW_LINE Y = 5 NEW_LINE N = 15 NEW_LINE print ( maximumNum ( X , Y , N ) ) NEW_LINE DEDENT
Number of points lying inside a rectangle as well as a triangle | Function to calculate area of a triangle ; Return the resultant area ; Function to check if a point lies inside a triangle or not ; Calculate area of triangle ABC ; Calculate area of triangle formed by connecting B , C , point ; Calculate area of triangle formed by connecting A , C , point ; Calculate area of triangle formed by connecting A , B , point ; Check if the sum of the areas of above three triangles the same as ABC ; Function to count the number of points lying inside a triangle & rectangle ; Stores the coordinates of the vertices of the triangles ; Stores the number of points lying inside the triangle and rectangle ; Traverse the array of points ; Stores whether the current point lies inside triangle1 or not ; Stores whether the current point lies inside triangle2 or not ; Stores whether the current point lies inside triangle3 or not ; Stores whether the current point lies inside triangle4 or not ; Stores whether the current point lies inside given triangle or not ; If current point lies inside given triangle as well as inside any of the four obtained triangles ; Print the count of points ; Driver Code
def getArea ( x1 , y1 , x2 , y2 , x3 , y3 ) : NEW_LINE INDENT return abs ( ( x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ) / 2 ) NEW_LINE DEDENT def isInside ( triangle , point ) : NEW_LINE INDENT A , B , C = triangle NEW_LINE x , y = point NEW_LINE ABC = getArea ( A [ 0 ] , A [ 1 ] , B [ 0 ] , B [ 1 ] , C [ 0 ] , C [ 1 ] ) NEW_LINE BPC = getArea ( x , y , B [ 0 ] , B [ 1 ] , C [ 0 ] , C [ 1 ] ) NEW_LINE APC = getArea ( A [ 0 ] , A [ 1 ] , x , y , C [ 0 ] , C [ 1 ] ) NEW_LINE APB = getArea ( A [ 0 ] , A [ 1 ] , B [ 0 ] , B [ 1 ] , x , y ) NEW_LINE return ABC == ( APC + APB + BPC ) NEW_LINE DEDENT def countPoints ( rectangle , triangle , points ) : NEW_LINE INDENT triangle1 = rectangle [ 1 : ] NEW_LINE triangle2 = rectangle [ : 3 ] NEW_LINE triangle3 = rectangle [ : 2 ] NEW_LINE triangle3 . append ( rectangle [ 3 ] ) NEW_LINE triangle4 = rectangle [ - 2 : ] NEW_LINE triangle4 . append ( rectangle [ 0 ] ) NEW_LINE ans = 0 NEW_LINE for point in points : NEW_LINE INDENT condOne = isInside ( triangle1 , point ) NEW_LINE condTwo = isInside ( triangle2 , point ) NEW_LINE condThree = isInside ( triangle3 , point ) NEW_LINE condFour = isInside ( triangle4 , point ) NEW_LINE condFive = isInside ( triangle , point ) NEW_LINE if ( condOne or condTwo or condThree   \ or condFour ) and condFive : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT rectangle = [ [ 6 , 5 ] , [ 2 , 2 ] , [ 2 , 1 ] , [ 5 , 5 ] ] NEW_LINE points = [ [ 1 , 1 ] , [ 6 , 1 ] , [ 6 , 6 ] , [ 1 , 6 ] ] NEW_LINE triangle = [ [ 4 , 4 ] , [ 0 , 4 ] , [ 0 , - 2 ] ] NEW_LINE countPoints ( points , triangle , rectangle ) NEW_LINE
Generate an N | Function to calculate GCD of two integers ; Function to calculate GCD of a given array ; Utility function to check for all the possible combinations ; If an N - length sequence is obtained ; If GCD of the sequence is K ; Otherwise ; Add an element from the first array ; Recursively proceed further ; If combination satisfies the necessary condition ; Remove the element from the combination ; Add an element from the second array ; Recursive proceed further ; If combination satisfies the necessary condition ; Remove the element from the combination ; Function to check all the possible combinations ; Stores the subsequence ; If GCD is not equal to K for any combination ; Given arrays ; Given value of K ; Function call to generate the required subsequence
def GCD ( a , b ) : NEW_LINE INDENT if not b : NEW_LINE INDENT return a NEW_LINE DEDENT return GCD ( b , a % b ) NEW_LINE DEDENT def GCDArr ( a ) : NEW_LINE INDENT ans = a [ 0 ] NEW_LINE for i in a : NEW_LINE INDENT ans = GCD ( ans , i ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def findSubseqUtil ( a , b , ans , k , i ) : NEW_LINE INDENT if len ( ans ) == len ( a ) : NEW_LINE INDENT if GCDArr ( ans ) == k : NEW_LINE INDENT print ( ans ) NEW_LINE return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT ans . append ( a [ i ] ) NEW_LINE temp = findSubseqUtil ( a , b , ans , k , i + 1 ) NEW_LINE if temp == True : NEW_LINE INDENT return True NEW_LINE DEDENT ans . pop ( ) NEW_LINE ans . append ( b [ i ] ) NEW_LINE temp = findSubseqUtil ( a , b , ans , k , i + 1 ) NEW_LINE if temp == True : NEW_LINE INDENT return True NEW_LINE DEDENT ans . pop ( ) NEW_LINE DEDENT def findSubseq ( A , B , K , i ) : NEW_LINE INDENT ans = [ ] NEW_LINE findSubseqUtil ( A , B , ans , K , i ) NEW_LINE if not ans : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT A = [ 5 , 3 , 6 , 2 , 9 ] NEW_LINE B = [ 21 , 7 , 14 , 12 , 28 ] NEW_LINE K = 3 NEW_LINE ans = findSubseq ( A , B , K , 0 ) NEW_LINE
Minimize count of swaps of adjacent elements required to make an array increasing | Function to count minimum number of operations required to obtain an increasing array from given array A [ ] ; Store the required result ; Traverse the array A [ ] ; If the current element is not in its correct position ; Check if it is present at index i - 1 ; Check if it is present at index i - 2 ; Otherwise , print - 1 ( Since A [ i ] can not be swapped more than twice ) ; Print the result ; Driver Code ; Given array ; Store the size of the array
def minimumOperations ( A , n ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( A [ i ] != ( i + 1 ) ) : NEW_LINE INDENT if ( ( ( i - 1 ) >= 0 ) and A [ i - 1 ] == ( i + 1 ) ) : NEW_LINE INDENT cnt += 1 NEW_LINE A [ i ] , A [ i - 1 ] = A [ i - 1 ] , A [ i ] NEW_LINE DEDENT elif ( ( ( i - 2 ) >= 0 ) and A [ i - 2 ] == ( i + 1 ) ) : NEW_LINE INDENT cnt += 2 NEW_LINE A [ i - 2 ] = A [ i - 1 ] NEW_LINE A [ i - 1 ] = A [ i ] NEW_LINE A [ i ] = i + 1 NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT print ( cnt ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 7 , 3 , 2 , 1 , 4 ] NEW_LINE n = len ( A ) NEW_LINE minimumOperations ( A , n ) NEW_LINE DEDENT
Count occurrences of an element in a matrix of size N * N generated such that each element is equal to product of its indices | Set | Function to count the occurrences of X in the generated square matrix ; Store the required result ; Iterate over the range [ 1 , N ] ; Check if x is a multiple of i or not ; Check if the other multiple exists in the range or not ; Print the result ; Driver Code
def countOccurrences ( n , x ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT if ( x // i <= n ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT print ( count ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 7 NEW_LINE X = 12 NEW_LINE countOccurrences ( N , X ) NEW_LINE DEDENT
Reduce all array elements to zero by performing given operations thrice | Function to reduce all array elements to zero ; If size of array is 1 ; First operation ; 2 nd Operation ; 3 rd Operation ; Otherwise ; 1 st Operation ; 2 nd Operation ; 3 rd Operation ; Driver Code ; Input ; Function call to make all array elements equal to 0
def ConvertArray ( arr , N ) : NEW_LINE INDENT if ( N == 1 ) : NEW_LINE INDENT print ( " Operation ▁ 1 ▁ : " , 1 , 1 ) NEW_LINE print ( " Added ▁ elements : " , - 1 * arr [ 0 ] ) NEW_LINE print ( " " , end ▁ = ▁ " " ) NEW_LINE print ( " Operation ▁ 2 ▁ : " , 1 , 1 ) NEW_LINE print ( " Added ▁ elements : " , 1 * arr [ 0 ] ) NEW_LINE print ( " " , end ▁ = ▁ " " ) NEW_LINE print ( " Operation ▁ 3 ▁ : " , 1 , 1 ) NEW_LINE print ( " Added ▁ elements : " , - 1 * arr [ 0 ] ) NEW_LINE print ( " " , end ▁ = ▁ " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Operation ▁ 1 ▁ : " , 1 , N ) NEW_LINE print ( " Added ▁ elements : " , end = " ▁ " ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT print ( - 1 * arr [ i ] * N , end = " ▁ " ) NEW_LINE DEDENT print ( " " ) NEW_LINE print ( " Operation ▁ 2 ▁ : " , 1 , N - 1 ) NEW_LINE print ( " Added ▁ elements : " , end = " ▁ " ) NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT print ( arr [ i ] * ( N - 1 ) , end = " ▁ " ) NEW_LINE DEDENT print ( " " ) NEW_LINE print ( " Operation ▁ 3 ▁ : ▁ " , N , N ) NEW_LINE print ( " Added ▁ elements : " , end = " ▁ " ) NEW_LINE print ( arr [ N - 1 ] * ( N - 1 ) ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 2 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE ConvertArray ( arr , N ) NEW_LINE DEDENT
Count pairs from an array with even product of count of distinct prime factors | Python 3 implementation of the above approach ; Function to calculate count of distinct prime factors of a number ; Sieve of Eratosthenes ; Function to count pairs with even product of distinct prime factors ; Stores count of distinct prime factors ; Stores the count of numbers with even prime factors in B [ ] ; Stores the count of numbers with odd prime factors in B [ ] ; Even Product Pairs ; Traverse the array B [ ] ; Since , product has to be positive i . e > 0 ; If count of prime factors is odd ; Increment oddCount by 1 ; Increment evenCount by 1 ; Since , product has to be positive i . e > 0 ; If count of prime factors is odd ; odd * even = even ; If count of prime factors is even ; even * odd = even even * even = even ; Driver Code
MAX = 1000000 NEW_LINE def countOfPrimefactors ( CountDistinct ) : NEW_LINE INDENT global MAX NEW_LINE prime = [ 0 for i in range ( MAX + 1 ) ] NEW_LINE for i in range ( MAX + 1 ) : NEW_LINE INDENT CountDistinct [ i ] = 0 NEW_LINE prime [ i ] = True NEW_LINE DEDENT for i in range ( 2 , MAX + 1 , 1 ) : NEW_LINE INDENT if ( prime [ i ] == True ) : NEW_LINE INDENT CountDistinct [ i ] = 1 NEW_LINE for j in range ( i * 2 , MAX + 1 , i ) : NEW_LINE INDENT CountDistinct [ j ] += 1 NEW_LINE prime [ j ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def CountEvenPair ( A , B , N , M ) : NEW_LINE INDENT global MAX NEW_LINE countDistinct = [ 0 for i in range ( MAX + 1 ) ] NEW_LINE countOfPrimefactors ( countDistinct ) NEW_LINE evenCount = 0 NEW_LINE oddCount = 0 NEW_LINE evenPairs = 0 NEW_LINE for i in range ( M ) : NEW_LINE INDENT if ( countDistinct [ B [ i ] ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( countDistinct [ B [ i ] ] & 1 ) : NEW_LINE INDENT oddCount += 1 NEW_LINE DEDENT else : NEW_LINE INDENT evenCount += 1 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT if ( countDistinct [ A [ i ] ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( countDistinct [ A [ i ] ] & 1 ) : NEW_LINE INDENT evenPairs += ( evenCount ) NEW_LINE DEDENT else : NEW_LINE INDENT evenPairs += evenCount + oddCount NEW_LINE DEDENT DEDENT return evenPairs NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 2 , 3 ] NEW_LINE B = [ 4 , 5 , 6 ] NEW_LINE N = len ( A ) NEW_LINE M = len ( B ) NEW_LINE print ( CountEvenPair ( A , B , N , M ) ) NEW_LINE DEDENT
Minimum array elements required to be subtracted from either end to reduce K to 0 | Function to find the length of longest subarray having sum K ; Stores the index of the prefix sum ; Traverse the given array ; Update sum ; If the subarray starts from index 0 ; Add the current prefix sum with index if it is not present in the map um ; Check if sum - K is present in Map um or not ; Update the maxLength ; Return the required maximum length ; Function to find the minimum removal of array elements required to reduce K to 0 ; Stores the sum of the array ; Traverse the array arr [ ] ; Update sum of the array ; Find maxLen ; If the subarray with sum doesn 't exist ; Otherwise , print the required maximum length ; Driver Code
def longestSubarray ( arr , N , K ) : NEW_LINE INDENT um = { } NEW_LINE sum , maxLen = 0 , 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE if ( sum == K ) : NEW_LINE INDENT maxLen = i + 1 NEW_LINE DEDENT if ( sum not in um ) : NEW_LINE INDENT um [ sum ] = i NEW_LINE DEDENT if ( ( sum - K ) in um ) : NEW_LINE INDENT if ( maxLen < ( i - um [ sum - K ] ) ) : NEW_LINE INDENT maxLen = i - um [ sum - K ] NEW_LINE DEDENT DEDENT DEDENT return maxLen NEW_LINE DEDENT def minRequiredOperation ( arr , N , K ) : NEW_LINE INDENT TotalSum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT TotalSum += arr [ i ] NEW_LINE DEDENT maxLen = longestSubarray ( arr , N , TotalSum - K ) NEW_LINE if ( maxLen == - 1 ) : NEW_LINE INDENT print ( - 1 , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( N - maxLen , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 1 , 1 , 2 ] NEW_LINE K = 4 NEW_LINE N = len ( arr ) NEW_LINE minRequiredOperation ( arr , N , K ) NEW_LINE DEDENT
Minimum number of digits required to be removed to make a number divisible by 4 | Function to count the minimum number of digits required to be removed to make a given number divisible by 4 ; Store the size of the string ; Stores the required result ; Check for every pair of digits if the number formed by them is divisible by 4 or not ; Store s [ i ] in a variable ; If it is divisible by 2 ; Store the number formed by s [ j ] and s [ i ] ; Check if it is divisible by 4 ; Store the number of digits required to be deleted ; Update ans ; If value of ans is unchanged , then check if any s [ i ] is divisible by 4 ; If true , update ans to n - 1 ; Prthe result ; Driver Code
def minimumDeletions ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE ans = n NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT t = ord ( s [ i ] ) - ord ( '0' ) NEW_LINE if ( t % 2 == 0 ) : NEW_LINE INDENT for j in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT num = ( ord ( s [ j ] ) - ord ( '0' ) ) * 10 + t NEW_LINE if ( num % 4 == 0 ) : NEW_LINE INDENT k1 = i - j - 1 NEW_LINE k2 = n - i - 1 NEW_LINE ans = min ( ans , k1 + k2 ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( ans == n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT num = ord ( s [ i ] ) - ord ( '0' ) NEW_LINE if ( num % 4 == 0 ) : NEW_LINE INDENT ans = n - 1 NEW_LINE DEDENT DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "12367" NEW_LINE minimumDeletions ( str ) NEW_LINE DEDENT
Generate a sequence such that float division of array elements is maximized | Function to place the parenthesis such that the result is maximized ; Store the required string ; Add the first integer to string ; If the size of array is 1 ; If the size of array is 2 , print the 1 st integer followed by / operator followed by the next integer ; If size of array is exceeds two , pr1st integer concatenated with operators ' / ' , ' ( ' and next integers with the operator '/ ; Add parenthesis at the end ; Prthe final expression ; Driver Code
def generateSequence ( arr , n ) : NEW_LINE INDENT ans = " " NEW_LINE ans = str ( arr [ 0 ] ) NEW_LINE if ( n == 1 ) : NEW_LINE INDENT print ( ans ) NEW_LINE DEDENT elif ( n == 2 ) : NEW_LINE INDENT print ( ans + " / " + str ( arr [ 1 ] ) ) NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT else : NEW_LINE INDENT ans += " / ( " + str ( arr [ 1 ] ) NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT ans += " / " + str ( arr [ i ] ) NEW_LINE DEDENT ans += " ) " NEW_LINE print ( ans ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1000 , 100 , 10 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE generateSequence ( arr , N ) NEW_LINE DEDENT
Count of pairs having even and odd LCM from an array | Function to find count of distinct pairs having even LCM and odd LCM ; Store the total number of pairs ; Stores the count of odd numbers in the array ; Traverse the array arr [ ] ; Update the count of pairs with odd LCM ; Print the count of required pairs ; Driver Code
def LCMPairs ( arr , N ) : NEW_LINE INDENT total_pairs = ( N * ( N - 1 ) ) / 2 NEW_LINE odd = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT odd += 1 NEW_LINE DEDENT DEDENT odd = ( odd * ( odd - 1 ) ) // 2 NEW_LINE print ( " Even ▁ = " , int ( total_pairs - odd ) , " , " , " ▁ Odd ▁ = " , odd ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 6 , 5 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE LCMPairs ( arr , N ) NEW_LINE DEDENT
Value required to be added to N to obtain the sum of first M multiples of K | Function to print the value to be added to N to obtain sum of first M multiples of K ; Store the sum of the first M multiples of K ; Store the value to be added to obtain N ; Input
def printNumber ( N , K , M ) : NEW_LINE INDENT sum = K * ( M * ( M + 1 ) / 2 ) NEW_LINE return sum - N NEW_LINE DEDENT N = 17 NEW_LINE K = 3 NEW_LINE M = 4 NEW_LINE print ( int ( printNumber ( N , K , M ) ) ) NEW_LINE
Count even and odd Bitwise XORs of consecutive numbers in a range [ L , R ] starting from L | Prcount of even and odd numbers of XOR value from L to R ; Store the number of elements between L and R ; Count of even XOR values ; If L is odd and range % 4 = 3 ; Increment even by 1 ; If L is even and range % 4 != 0 ; Increment even by 1 ; Prthe answer ; Driver Code
def countEvenOdd ( L , R ) : NEW_LINE INDENT range = R - L + 1 ; NEW_LINE even = ( range // 4 ) * 2 ; NEW_LINE if ( ( L & 1 ) != 0 and ( range % 4 == 3 ) ) : NEW_LINE INDENT even += 1 ; NEW_LINE DEDENT elif ( ( L & 1 ) == 0 and ( range % 4 != 0 ) ) : NEW_LINE INDENT even += 1 ; NEW_LINE DEDENT print ( " Even ▁ = ▁ " , even , " , ▁ Odd ▁ = ▁ " , ( range - even ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 2 ; R = 7 ; NEW_LINE countEvenOdd ( L , R ) ; NEW_LINE DEDENT