codeparrot/xlcost-text-to-code huggingface dataset:
Count of triplets till N whose product is at most N | Function to find number of triplets ( A , B , C ) having A * B * C <= N ; Stores the count of triplets ; Iterate a loop fixing the value of A ; Iterate a loop fixing the value of A ; Find the total count of triplets and add it to cnt ; Return the total triplets formed ; Driver Code
def countTriplets ( N ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for A in range ( 1 , N + 1 ) : NEW_LINE INDENT for B in range ( 1 , N // A + 1 ) : NEW_LINE INDENT cnt += N // ( A * B ) ; NEW_LINE DEDENT DEDENT return cnt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 ; NEW_LINE print ( countTriplets ( N ) ) ; NEW_LINE DEDENT
Largest integer upto N having greatest prime factor greater than its square root | python program for the above approach ; Stores the Greatest Prime Factor ; Modified Sieve to find the Greatest Prime Factor of all integers in the range [ 1 , maxn ] ; Initialize the array with 0 ; Iterate through all values of i ; If i is not a prime number ; Update the multiples of i ; Function to find integer in the range [ 1 , N ] such that its Greatest Prime factor is greater than its square root ; Iterate through all values of i in the range [ N , 1 ] ; If greatest prime factor of i is greater than its sqare root ; Return answer ; If no valid integer exist ; Driver Code
import math NEW_LINE maxn = 100001 NEW_LINE gpf = [ 0 for _ in range ( maxn ) ] NEW_LINE def modifiedSieve ( ) : NEW_LINE INDENT gpf [ 0 ] = 0 NEW_LINE gpf [ 1 ] = 1 NEW_LINE for i in range ( 2 , maxn ) : NEW_LINE INDENT if ( gpf [ i ] > 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT for j in range ( i , maxn , i ) : NEW_LINE INDENT gpf [ j ] = max ( i , gpf [ j ] ) NEW_LINE DEDENT DEDENT DEDENT def greatestValidInt ( N ) : NEW_LINE INDENT modifiedSieve ( ) NEW_LINE for i in range ( N , 0 , - 1 ) : NEW_LINE INDENT if ( gpf [ i ] > math . sqrt ( i ) ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 25 NEW_LINE print ( greatestValidInt ( N ) ) NEW_LINE DEDENT
Find subfactorial of a number | Function to find the subfactorial of the number ; Initialize variables ; Iterating over range N ; Fact variable store factorial of the i ; If count is even ; Increase the value of count by 1 ; Driver Code
def subfactorial ( N ) : NEW_LINE INDENT res = 0 NEW_LINE fact = 1 NEW_LINE count = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT fact = fact * i NEW_LINE if ( count % 2 == 0 ) : NEW_LINE INDENT res = res - ( 1 / fact ) NEW_LINE DEDENT else : NEW_LINE INDENT res = res + ( 1 / fact ) NEW_LINE DEDENT count += 1 NEW_LINE DEDENT return fact * ( 1 + res ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE print ( subfactorial ( N ) ) NEW_LINE DEDENT
Minimize operations to delete all elements of permutation A by removing a subsequence having order as array B | Function to find the minimum number of operations to delete all elements of permutation A in order described by B ; Stores the count of operations ; Stores the index of current integer in B to be deleted ; Loop to iterate over all values of B ; Stores the current index in A ; Iterate over all values A ; If current integer of B and A equal , increment the index of the current integer of B ; As the permutation A has been traversed completely , increment the count of operations by 1 ; Return Answer ; Driver Code
def minOperations ( A , B , N ) : NEW_LINE INDENT cnt = 0 NEW_LINE i = 0 NEW_LINE while ( i < N ) : NEW_LINE INDENT j = 0 NEW_LINE while ( j < N and i < N ) : NEW_LINE INDENT if ( B [ i ] == A [ j ] ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT j += 1 NEW_LINE DEDENT cnt += 1 NEW_LINE DEDENT return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 2 , 4 , 6 , 1 , 5 , 3 ] NEW_LINE B = [ 6 , 5 , 4 , 2 , 3 , 1 ] NEW_LINE N = len ( A ) NEW_LINE print ( minOperations ( A , B , N ) ) NEW_LINE DEDENT
Count of pair of integers ( x , y ) such that difference between square of x and y is a perfect square | python program for the above approach ; Function to find number of pairs ( x , y ) such that x ^ 2 - y is a square number ; Stores the count of total pairs ; Iterate q value 1 to sqrt ( N ) ; Maximum possible value of p is min ( 2 * N - q , N / q ) ; P must be greater than or equal to q ; Total number of pairs are ; Adding all valid pairs to res ; Return total no of pairs ( x , y ) ; Driver Code
import math NEW_LINE def countPairs ( N ) : NEW_LINE INDENT res = 0 NEW_LINE for q in range ( 1 , int ( math . sqrt ( N ) ) + 1 ) : NEW_LINE INDENT maxP = min ( 2 * N - q , N // q ) NEW_LINE if ( maxP < q ) : NEW_LINE INDENT continue NEW_LINE DEDENT cnt = maxP - q + 1 NEW_LINE res += ( cnt // 2 + ( cnt & 1 ) ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE print ( countPairs ( N ) ) NEW_LINE DEDENT
Maximum size of subset such that product of all subset elements is a factor of N | Function to find the maximum size of the subset such that the product of subset elements is a factor of N ; Base Case ; Stores maximum size of valid subset ; Traverse the given array ; If N % arr [ i ] = 0 , include arr [ i ] in a subset and recursively call for the remaining array integers ; Return Answer ; Driver Code
def maximizeSubset ( N , arr , M , x = 0 ) : NEW_LINE INDENT if ( x == M ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( x , M ) : NEW_LINE INDENT if ( N % arr [ i ] == 0 ) : NEW_LINE INDENT ans = max ( ans , maximizeSubset ( N // arr [ i ] , arr , M , x + 1 ) + 1 ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT N = 64 NEW_LINE arr = [ 1 , 2 , 4 , 8 , 16 , 32 ] NEW_LINE M = len ( arr ) NEW_LINE print ( maximizeSubset ( N , arr , M ) ) NEW_LINE
Count of distinct sums formed by N numbers taken form range [ L , R ] | Function to find total number of different sums of N numbers in the range [ L , R ] ; To store minimum possible sum with N numbers with all as L ; To store maximum possible sum with N numbers with all as R ; All other numbers in between maxSum and minSum can also be formed so numbers in this range is the final answer ; Driver Code
def countDistinctSums ( N , L , R ) : NEW_LINE INDENT minSum = L * N NEW_LINE maxSum = R * N NEW_LINE return maxSum - minSum + 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 NEW_LINE L = 1 NEW_LINE R = 3 NEW_LINE print ( countDistinctSums ( N , L , R ) ) NEW_LINE DEDENT
Make the Array sum 0 by using either ceil or floor on each element | Python 3 program for the above approach ; Function to modify the array element such that the sum is close to 0 ; Stores the modified elements ; Stores the sum of array element ; Stores minimum size of the array ; Traverse the array and find the sum ; If sum is positive ; Find the minimum number of elements that must be changed ; Iterate until M elements are modified or the array end ; Update the current array elements to its floor ; Print the resultant array ; Driver Code
import sys NEW_LINE from math import ceil , floor NEW_LINE def setSumtoZero ( arr , N ) : NEW_LINE INDENT A = [ 0 for i in range ( N ) ] NEW_LINE sum = 0 NEW_LINE m = - sys . maxsize - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += ceil ( arr [ i ] ) NEW_LINE A [ i ] = ceil ( arr [ i ] ) NEW_LINE DEDENT if ( sum > 0 ) : NEW_LINE INDENT m = min ( sum , N ) NEW_LINE i = 0 NEW_LINE while ( i < N and m > 0 ) : NEW_LINE INDENT A [ i ] = floor ( arr [ i ] ) NEW_LINE if ( A [ i ] != floor ( arr [ i ] ) ) : NEW_LINE INDENT m -= 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT print ( A [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 2 , - 2 , 4.5 ] NEW_LINE N = len ( arr ) NEW_LINE setSumtoZero ( arr , N ) NEW_LINE DEDENT
Find first undeleted integer from K to N in given unconnected Graph after performing Q queries | Function to perform th Get operation of disjoint set union ; Function to perform the union operation of dijoint set union ; Update the graph [ a ] as b ; Function to perform given queries on set of vertices initially not connected ; Stores the vertices ; Mark every vertices rightmost vertex as i ; Traverse the queries array ; Check if it is first type of the givan query ; Get the parent of a ; Print the answer for the second query ; Driver Code
def Get ( graph , a ) : NEW_LINE INDENT if graph [ graph [ a ] ] != graph [ a ] : NEW_LINE INDENT graph [ a ] = Get ( graph , graph [ a ] ) NEW_LINE DEDENT else : NEW_LINE INDENT return graph [ a ] NEW_LINE DEDENT DEDENT def Union ( graph , a , b ) : NEW_LINE INDENT a = Get ( graph , a ) NEW_LINE b = Get ( graph , b ) NEW_LINE graph [ a ] = b NEW_LINE DEDENT def Queries ( queries , N , M ) : NEW_LINE INDENT graph = [ 0 for i in range ( N + 2 ) ] NEW_LINE for i in range ( 1 , N + 2 , 1 ) : NEW_LINE INDENT graph [ i ] = i NEW_LINE DEDENT for query in queries : NEW_LINE INDENT if ( query [ 0 ] == 1 ) : NEW_LINE INDENT Union ( graph , query [ 1 ] , query [ 1 ] + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT a = Get ( graph , query [ 1 ] ) NEW_LINE if ( a == N + 1 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( graph [ a ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE queries = [ [ 2 , 1 ] , [ 1 , 1 ] , [ 2 , 1 ] , [ 2 , 3 ] ] NEW_LINE Q = len ( queries ) NEW_LINE Queries ( queries , N , Q ) NEW_LINE DEDENT
Finding the Nth term in a sequence formed by removing digit K from natural numbers | Python 3 implementation for the above approach ; Denotes the digit place ; Method to convert any number to binary equivalent ; denotes the current digits place ; If current digit is >= K increment its value by 1 ; Else add the digit as it is ; Move to the next digit ; Driver code
def convertToBase9 ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE a = 1 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT ans += ( a * ( n % 9 ) ) NEW_LINE a *= 10 NEW_LINE n //= 9 NEW_LINE DEDENT return ans NEW_LINE DEDENT def getNthnumber ( base9 , K ) : NEW_LINE INDENT ans = 0 NEW_LINE a = 1 NEW_LINE while ( base9 > 0 ) : NEW_LINE INDENT cur = base9 % 10 NEW_LINE if ( cur >= K ) : NEW_LINE INDENT ans += a * ( cur + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT ans += a * cur NEW_LINE DEDENT base9 //= 10 NEW_LINE a *= 10 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 10 NEW_LINE K = 1 NEW_LINE base9 = convertToBase9 ( N ) NEW_LINE print ( getNthnumber ( base9 , K ) ) NEW_LINE DEDENT
Check if given array can be rearranged such that mean is equal to median | Function to return true or false if size of array is odd ; To prevent overflow ; If element is greater than mid , then it can only be present in right subarray ; If element is smaller than mid , then it can only be present in left subarray ; Else the element is present at the middle then return 1 ; when element is not present in array then return 0 ; Function to return true or false if size of array is even ; Calculating Candidate Median ; If Candidate Median if greater than Mean then decrement j ; If Candidate Median if less than Mean then increment i ; If Candidate Median if equal to Mean then return 1 ; when No candidate found for mean ; Function to return true if Mean can be equal to any candidate median otherwise return false ; Calculating Mean ; If N is Odd ; If N is even ; Driver Code
def binarySearch ( arr , size , key ) : NEW_LINE INDENT low = 0 NEW_LINE high = size - 1 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 NEW_LINE if ( key > arr [ mid ] ) : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT elif ( key < arr [ mid ] ) : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT def twoPointers ( arr , N , mean ) : NEW_LINE INDENT i = 0 NEW_LINE j = N - 1 NEW_LINE while ( i < j ) : NEW_LINE INDENT temp = ( arr [ i ] + arr [ j ] ) / 2 NEW_LINE if ( temp > mean ) : NEW_LINE INDENT j = j - 1 NEW_LINE DEDENT elif ( temp < mean ) : NEW_LINE INDENT i = i + 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT def checkArray ( arr , N ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT mean = sum / N NEW_LINE if ( N & 1 ) : NEW_LINE INDENT return binarySearch ( arr , N , mean ) NEW_LINE DEDENT else : NEW_LINE INDENT return twoPointers ( arr , N , mean ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1.0 , 3.0 , 6.0 , 9.0 , 12.0 , 32.0 ] NEW_LINE N = len ( arr ) NEW_LINE if ( checkArray ( arr , N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Count of distinct integers belonging to first N terms of at least one of given GPs | Function to find the count of distinct integers that belong to the first N terms of at least one of them is GP ; Stores the integers that occur in GPs in a set data - structure ; Stores the current integer of the first GP ; Iterate first N terms of first GP ; Insert the ith term of GP in S ; Stores the current integer of the second GP ; Iterate first N terms of second GP ; Return Answer ; Driver Code
def UniqueGeometricTerms ( N , a1 , r1 , a2 , r2 ) : NEW_LINE INDENT S = set ( ) NEW_LINE p1 = a1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT S . add ( p1 ) NEW_LINE p1 = ( p1 * r1 ) NEW_LINE DEDENT p2 = a2 NEW_LINE for i in range ( N ) : NEW_LINE INDENT S . add ( p2 ) NEW_LINE p2 = ( p2 * r2 ) NEW_LINE DEDENT return len ( S ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE a1 = 3 NEW_LINE r1 = 2 NEW_LINE a2 = 2 NEW_LINE r2 = 3 NEW_LINE print ( UniqueGeometricTerms ( N , a1 , r1 , a2 , r2 ) ) NEW_LINE DEDENT
Minimize operations to make all array elements | Function to find minimum the number of operations required to make all the array elements to - 1 ; Stores the array elements with their corresponding indices ; Push the array element and it 's index ; Sort the elements according to it 's first value ; Stores the minimum number of operations required ; Traverse until vp is not empty ; Stores the first value of vp ; Stores the second value of vp ; Update the minCnt ; Pop the back element from the vp until the first value is same as val and difference between indices is less than K ; Return the minCnt ; Driver Code
def minOperations ( arr , N , K ) : NEW_LINE INDENT vp = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT vp . append ( [ arr [ i ] , i ] ) NEW_LINE DEDENT vp . sort ( ) NEW_LINE minCnt = 0 NEW_LINE while ( len ( vp ) != 0 ) : NEW_LINE INDENT val = vp [ - 1 ] [ 0 ] NEW_LINE ind = vp [ - 1 ] [ 1 ] NEW_LINE minCnt += 1 NEW_LINE while ( len ( vp ) != 0 and vp [ - 1 ] [ 0 ] == val and ind - vp [ - 1 ] [ 1 ] + 1 <= K ) : NEW_LINE INDENT vp . pop ( ) NEW_LINE DEDENT DEDENT return minCnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 18 , 11 , 18 , 11 , 18 ] NEW_LINE K = 3 NEW_LINE N = len ( arr ) NEW_LINE print ( minOperations ( arr , N , K ) ) NEW_LINE DEDENT
Find array sum after incrementing by K adjacent elements of every positive element M times | Function to find the nearest non - zero element in the left direction ; Stores the index of the first element greater than 0 from the right side ; Traverse the array in the range [ 1 , N ] ; Check arr [ i ] is greater than 0 ; Update the value of L ; Traverse the array from the left side ; Check arr [ i ] is greater than 0 ; Update the value of L ; Update the value of steps [ i ] ; Function to find the nearest non - zero element in the right direction ; Stores the index of the first element greater than 0 from the left side ; Traverse the array from the left side ; Check arr [ i ] is greater than 0 ; Update the value of R ; Traverse the array from the right side ; Check arr [ i ] is greater than 0 ; Update the value of R ; Update the value of steps [ i ] ; Function to find the sum of the array after the given operation M times ; Stores the distance of the nearest non zero element . ; Stores sum of the initial array arr [ ] ; Traverse the array from the left side ; Update the value of sum ; Print the total sum of the array ; Driver Code
def nearestLeft ( arr , N , steps ) : NEW_LINE INDENT L = - N NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT L = - ( N - i ) NEW_LINE break NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT L = i NEW_LINE DEDENT steps [ i ] = i - L NEW_LINE DEDENT DEDENT def nearestRight ( arr , N , steps ) : NEW_LINE INDENT R = 2 * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT R = N + i NEW_LINE break NEW_LINE DEDENT DEDENT for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT R = i NEW_LINE DEDENT steps [ i ] = min ( steps [ i ] , R - i ) NEW_LINE DEDENT DEDENT def findSum ( arr , N , M , K ) : NEW_LINE INDENT steps = [ 0 ] * N NEW_LINE s = sum ( arr ) NEW_LINE if ( s == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT nearestLeft ( arr , N , steps ) NEW_LINE nearestRight ( arr , N , steps ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT s += 2 * K * max ( 0 , M - steps [ i ] ) NEW_LINE DEDENT return s NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 0 , 1 , 0 , 1 , 0 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE M = 2 NEW_LINE K = 1 NEW_LINE print ( findSum ( arr , N , M , K ) ) NEW_LINE DEDENT
Smallest vertex in the connected components of all the vertices in given undirect graph | Python 3 program for the above approach ; Stores the parent and size of the set of the ith element ; Function to initialize the ith set ; Function to find set of ith vertex ; Base Case ; Recursive call to find set ; Function to unite the set that includes a and the set that includes b ; If a and b are not from same set ; Function to find the smallest vertex in the connected component of the ith vertex for all i in range [ 1 , N ] ; Loop to initialize the ith set ; Loop to unite all vertices connected by edges into the same set ; Stores the minimum vertex value for ith set ; Loop to iterate over all vertices ; If current vertex does not exist in minVal initialize it with i ; Update the minimum value of the set having the ith vertex ; Loop to print required answer ; Driver Code
maxn = 100 NEW_LINE parent = [ 0 ] * maxn NEW_LINE Size = [ 0 ] * maxn NEW_LINE def make_set ( v ) : NEW_LINE INDENT parent [ v ] = v NEW_LINE Size [ v ] = 1 NEW_LINE DEDENT def find_set ( v ) : NEW_LINE INDENT if ( v == parent [ v ] ) : NEW_LINE INDENT return v NEW_LINE DEDENT parent [ v ] = find_set ( parent [ v ] ) NEW_LINE return parent [ v ] NEW_LINE DEDENT def union_sets ( a , b ) : NEW_LINE INDENT a = find_set ( a ) NEW_LINE b = find_set ( b ) NEW_LINE if ( a != b ) : NEW_LINE INDENT if ( Size [ a ] < Size [ b ] ) : NEW_LINE INDENT a , b = b , a NEW_LINE DEDENT parent [ b ] = a NEW_LINE Size [ a ] += Size [ b ] NEW_LINE DEDENT DEDENT def findMinVertex ( N , edges ) : NEW_LINE INDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT make_set ( i ) NEW_LINE DEDENT for i in range ( len ( edges ) ) : NEW_LINE INDENT union_sets ( edges [ i ] [ 0 ] , edges [ i ] [ 1 ] ) NEW_LINE DEDENT minVal = { } NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( find_set ( i ) not in minVal ) : NEW_LINE INDENT minVal [ find_set ( i ) ] = i NEW_LINE DEDENT else : NEW_LINE INDENT minVal [ find_set ( i ) ] = min ( minVal [ find_set ( i ) ] , i ) NEW_LINE DEDENT DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT print ( minVal [ find_set ( i ) ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 NEW_LINE edges = [ [ 1 , 3 ] , [ 2 , 4 ] ] NEW_LINE findMinVertex ( N , edges ) NEW_LINE DEDENT
Count of pairs in given range having their ratio equal to ratio of product of their digits | Function to find the product of digits of the given number ; Function to find the count of pairs ( a , b ) such that a : b = ( product ofdigits of a ) : ( product of digits of b ) ; Stores the count of the valid pairs ; Loop to iterate over all unordered pairs ( a , b ) ; Stores the product of digits of a ; Stores the product of digits of b ; If x != 0 and y != 0 and a : b is equivalent to x : y ; Increment valid pair count ; Return Answer ; Driver code ; Function Call
def getProduct ( n ) : NEW_LINE INDENT product = 1 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT product = product * ( n % 10 ) NEW_LINE n = n // 10 NEW_LINE DEDENT return product NEW_LINE DEDENT def countPairs ( L , R ) : NEW_LINE INDENT cntPair = 0 NEW_LINE for a in range ( L , R + 1 , 1 ) : NEW_LINE INDENT for b in range ( a + 1 , R + 1 , 1 ) : NEW_LINE INDENT x = getProduct ( a ) NEW_LINE y = getProduct ( b ) NEW_LINE if ( x and y and ( a * y ) == ( b * x ) ) : NEW_LINE INDENT cntPair += 1 NEW_LINE DEDENT DEDENT DEDENT return cntPair NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 1 NEW_LINE R = 100 NEW_LINE print ( countPairs ( 1 , 100 ) ) NEW_LINE DEDENT
Maximize matrix sum by flipping the sign of any adjacent pairs | Python 3 program for the above approach ; Function to find the maximum sum of matrix element after flipping the signs of adjacent matrix elements ; Initialize row and column ; Stores the sum of absolute matrix element ; Find the minimum absolute value in the matrix ; Store count of negatives ; Find the smallest absolute value in the matrix ; Increment the count of negative numbers ; Find the absolute sum ; If the count of negatives is even then print the sum ; Subtract minimum absolute element ; Driver Code
import sys NEW_LINE def maxSum ( matrix ) : NEW_LINE INDENT r = len ( matrix ) NEW_LINE c = len ( matrix [ 0 ] ) NEW_LINE sum = 0 NEW_LINE mini = sys . maxsize NEW_LINE count = 0 NEW_LINE for i in range ( r ) : NEW_LINE INDENT for j in range ( c ) : NEW_LINE INDENT k = matrix [ i ] [ j ] NEW_LINE mini = min ( mini , abs ( k ) ) NEW_LINE if ( k < 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT sum += abs ( k ) NEW_LINE DEDENT DEDENT if ( count % 2 == 0 ) : NEW_LINE INDENT return sum NEW_LINE DEDENT else : NEW_LINE INDENT return ( sum - 2 * mini ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT matrix = [ [ 2 , - 2 ] , [ - 2 , 2 ] ] NEW_LINE print ( maxSum ( matrix ) ) NEW_LINE DEDENT
Find the last positive element remaining after repeated subtractions of smallest positive element from all Array elements | Function to calculate last positive element of the array ; Return the first element if N = 1 ; Stores the greatest and the second greatest element ; Traverse the array A [ ] ; If current element is greater than the greatest element ; If current element is greater than second greatest element ; Return the final answer ; Driver Code
def lastPositiveElement ( arr ) : NEW_LINE INDENT N = len ( arr ) ; NEW_LINE if ( N == 1 ) : NEW_LINE INDENT return arr [ 0 ] ; NEW_LINE DEDENT greatest = - 1 ; secondGreatest = - 1 ; NEW_LINE for x in arr : NEW_LINE INDENT if ( x >= greatest ) : NEW_LINE INDENT secondGreatest = greatest ; NEW_LINE greatest = x ; NEW_LINE DEDENT elif ( x >= secondGreatest ) : NEW_LINE INDENT secondGreatest = x ; NEW_LINE DEDENT DEDENT return greatest - secondGreatest ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 5 , 4 , 7 ] ; NEW_LINE print ( lastPositiveElement ( arr ) ) ; NEW_LINE DEDENT