codeparrot/xlcost-text-to-code huggingface dataset:
Minimize count of unequal elements at corresponding indices between given arrays | Function that count of the mismatched pairs in bot the array ; Create a parent array and initialize it ; Preprocessing of the given pairs of indices ; 1 - based indexing ; If both indices doesn 't belong  to same component ; Insert the indices in same component ; HashMap to get the indices of array A ; If current element is not present in array B then count this as mismatched ; Get the index of the element in array B ; Check if both indices belong to same connected component if not increment the count ; Return answer ; Function that gives the connected component of each index ; Function that creates the connected components ; Find parent of a and b recursively ; Update the parent of a ; Driver Code ; Given arrays A [ ] , B [ ] ; List of indices ; Function call
def countPairs ( A , B , N , M , List ) : NEW_LINE INDENT count = 0 NEW_LINE par = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( N + 1 ) : NEW_LINE INDENT par [ i ] = i NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT index1 = find ( par , List [ i ] [ 0 ] - 1 ) NEW_LINE index2 = find ( par , List [ i ] [ 1 ] - 1 ) NEW_LINE if ( index1 != index2 ) : NEW_LINE INDENT union ( par , index1 , index2 ) NEW_LINE DEDENT DEDENT map = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT map [ A [ i ] ] = i NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( A [ i ] != B [ i ] ) : NEW_LINE INDENT if ( B [ i ] not in map . keys ( ) ) : NEW_LINE INDENT count += 1 NEW_LINE continue NEW_LINE DEDENT j = map [ B [ i ] ] NEW_LINE if ( find ( par , i ) != find ( par , j ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT def find ( par , x ) : NEW_LINE INDENT if ( par [ x ] == x ) : NEW_LINE INDENT return x NEW_LINE DEDENT else : NEW_LINE INDENT par [ x ] = find ( par , par [ x ] ) NEW_LINE return par [ x ] NEW_LINE DEDENT DEDENT def union ( par , a , b ) : NEW_LINE INDENT a = find ( par , a ) NEW_LINE b = find ( par , b ) NEW_LINE if ( a == b ) : NEW_LINE INDENT return NEW_LINE DEDENT par [ a ] = b NEW_LINE DEDENT N = 5 NEW_LINE M = 4 NEW_LINE A = [ 1 , 5 , 9 , 2 , 3 ] NEW_LINE B = [ 2 , 4 , 5 , 1 , 3 ] NEW_LINE List = [ [ 1 , 4 ] , [ 2 , 3 ] , [ 3 , 5 ] , [ 2 , 5 ] ] NEW_LINE print ( countPairs ( A , B , N , M , List ) ) NEW_LINE
Maximum value of expression ( arr [ i ] + arr [ j ] * arr [ k ] ) formed from a valid Triplet | Function that generate all valid triplets and calculate the value of the valid triplets ; Generate all triplets ; Check whether the triplet is valid or not ; Update the value ; Print the maximum value ; Driver Code ; Given array arr ; Function call
def max_valid_triplet ( A , n ) : NEW_LINE INDENT ans = - 1 ; NEW_LINE for i in range ( 0 , n - 2 ) : NEW_LINE INDENT for j in range ( i + 1 , n - 1 ) : NEW_LINE INDENT for k in range ( j + 1 , n ) : NEW_LINE INDENT if ( A [ i ] < A [ j ] and A [ j ] < A [ k ] ) : NEW_LINE INDENT value = A [ i ] + A [ j ] * A [ k ] ; NEW_LINE if ( value > ans ) : NEW_LINE INDENT ans = value ; NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT print ( ans ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 9 , 3 , 8 , 11 , 10 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE max_valid_triplet ( arr , n ) ; NEW_LINE DEDENT
Product of proper divisors of a number for Q queries | Python3 implementation of the above approach ; Function to precompute the product of proper divisors of a number at it 's corresponding index ; Returning the pre - computed values ; Driver code
mod = 1000000007 NEW_LINE ans = [ 1 ] * ( 100002 ) NEW_LINE def preCompute ( ) : NEW_LINE INDENT for i in range ( 2 , 100000 // 2 + 1 ) : NEW_LINE INDENT for j in range ( 2 * i , 100001 , i ) : NEW_LINE INDENT ans [ j ] = ( ans [ j ] * i ) % mod NEW_LINE DEDENT DEDENT DEDENT def productOfProperDivi ( num ) : NEW_LINE INDENT return ans [ num ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT preCompute ( ) NEW_LINE queries = 5 NEW_LINE a = [ 4 , 6 , 8 , 16 , 36 ] NEW_LINE for i in range ( queries ) : NEW_LINE INDENT print ( productOfProperDivi ( a [ i ] ) , end = " , ▁ " ) NEW_LINE DEDENT DEDENT
Highest power of 2 that divides the LCM of first N Natural numbers . | Function to find LCM of first N natural numbers ; Initialize result ; Ans contains LCM of 1 , 2 , 3 , . . i after i 'th iteration ; Function to find the highest power of 2 which divides LCM of first n natural numbers ; Find lcm of first N natural numbers ; To store the highest required power of 2 ; Counting number of consecutive zeros from the end in the given binary String ; Driver code
def findlcm ( n ) : NEW_LINE INDENT ans = 1 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ans = ( ( ( i * ans ) ) // ( __gcd ( i , ans ) ) ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT def highestPower ( n ) : NEW_LINE INDENT lcm = findlcm ( n ) ; NEW_LINE ans = 0 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT x = int ( pow ( 2 , i ) ) ; NEW_LINE if ( lcm % x == 0 ) : NEW_LINE INDENT ans = i ; NEW_LINE DEDENT if ( x > n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT def __gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT else : NEW_LINE INDENT return __gcd ( b , a % b ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 15 ; NEW_LINE print ( highestPower ( n ) ) ; NEW_LINE DEDENT
Highest power of 2 that divides the LCM of first N Natural numbers . | Python3 implementation of the approach ; Function to find the highest power of 2 which divides LCM of first n natural numbers ; Driver code
import math NEW_LINE def highestPower ( n ) : NEW_LINE INDENT return int ( ( math . log ( n ) // math . log ( 2 ) ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 15 ; NEW_LINE print ( highestPower ( n ) ) ; NEW_LINE DEDENT
Check if number can be made prime by deleting a single digit | Function to check if N is prime from builtins import range ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to delete character at index i from given String str ; Deletes character at position 4 ; Function to check if a number becomes prime by deleting any digit ; Converting the number to String ; length of String ; number should not be of single digit ; Loop to find all numbers after deleting a single digit ; Deleting ith character from the String ; converting String to int ; Driver Code
def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT for i in range ( 5 , int ( n ** 1 / 2 ) , 6 ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def deleteIth ( str , i ) : NEW_LINE INDENT str = str [ 0 : i ] + str [ i + 1 : ] ; NEW_LINE return str ; NEW_LINE DEDENT def isPrimePossible ( N ) : NEW_LINE INDENT s = str ( N ) ; NEW_LINE l = len ( s ) ; NEW_LINE if ( l < 2 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT for i in range ( l ) : NEW_LINE INDENT str1 = deleteIth ( s , i ) ; NEW_LINE num = int ( str1 ) ; NEW_LINE if ( isPrime ( num ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT return False ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 610 ; NEW_LINE if ( isPrimePossible ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Check if a number has an odd count of odd divisors and even count of even divisors | Function to find the count of even and odd factors of N ; Loop runs till square root ; Condition to check if the even factors of the number N is is even and count of odd factors is odd ; Driver Code
def checkFactors ( N ) : NEW_LINE INDENT ev_count = 0 ; od_count = 0 ; NEW_LINE for i in range ( 1 , int ( pow ( N , 1 / 2 ) ) + 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT if ( i == N / i ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT ev_count += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT od_count += 1 ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT ev_count += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT od_count += 1 ; NEW_LINE DEDENT if ( ( N / i ) % 2 == 0 ) : NEW_LINE INDENT ev_count += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT od_count += 1 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( ev_count % 2 == 0 and od_count % 2 == 1 ) : NEW_LINE INDENT print ( " Yes " + " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " + " " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 36 ; NEW_LINE checkFactors ( N ) ; NEW_LINE DEDENT
Count of distinct permutations of length N having no similar adjacent characters | Function to print the number of permutations possible ; Driver Code
def countofPermutations ( N ) : NEW_LINE INDENT return int ( ( 3 * pow ( 2 , N - 1 ) ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 ; NEW_LINE print ( countofPermutations ( N ) ) ; NEW_LINE DEDENT
Find two distinct numbers such that their LCM lies in given range | Function to find two distinct numbers X and Y s . t . their LCM lies between L and R and X , Y are minimum possible ; Check if 2 * L lies in range L , R ; Print the answer ; Given value of ranges ; Function call
def answer ( L , R ) : NEW_LINE INDENT if ( 2 * L <= R ) : NEW_LINE INDENT print ( L , " , " , 2 * L ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT L = 3 NEW_LINE R = 8 NEW_LINE answer ( L , R ) NEW_LINE
Maximum count of pairwise co | Function to find the gcd of two numbers ; Function to of pairwise co - prime and common divisors of two numbers ; Initialize answer with 1 , to include 1 in the count ; Count of primes of gcd ( N , M ) ; Finding prime factors of gcd ; Increment count if it is divisible by i ; Return the total count ; Function Call for each pair to calculate the count of pairwise co - prime divisors ; Driver Code ; Given array of pairs ; Function Call
def gcd ( x , y ) : NEW_LINE INDENT if ( x % y == 0 ) : NEW_LINE INDENT return y NEW_LINE DEDENT else : NEW_LINE INDENT return gcd ( y , x % y ) NEW_LINE DEDENT DEDENT def countPairwiseCoprime ( N , M ) : NEW_LINE INDENT answer = 1 NEW_LINE g = gcd ( N , M ) NEW_LINE temp = g NEW_LINE for i in range ( 2 , g + 1 ) : NEW_LINE INDENT if i * i > g : NEW_LINE INDENT break NEW_LINE DEDENT if ( temp % i == 0 ) : NEW_LINE INDENT answer += 1 NEW_LINE while ( temp % i == 0 ) : NEW_LINE INDENT temp //= i NEW_LINE DEDENT DEDENT DEDENT if ( temp != 1 ) : NEW_LINE INDENT answer += 1 NEW_LINE DEDENT return answer NEW_LINE DEDENT def countCoprimePair ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT print ( countPairwiseCoprime ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 12 , 18 ] , [ 420 , 660 ] ] NEW_LINE N = len ( arr ) NEW_LINE countCoprimePair ( arr , N ) NEW_LINE DEDENT
Product of absolute difference of every pair in given Array | Function to return the product of abs diff of all pairs ( x , y ) ; To store product ; Iterate all possible pairs ; Find the product ; Return product ; Driver Code ; Given array arr [ ] ; Function Call
def getProduct ( a , n ) : NEW_LINE INDENT p = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT p *= abs ( a [ i ] - a [ j ] ) NEW_LINE DEDENT DEDENT return p NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( getProduct ( arr , N ) ) NEW_LINE DEDENT
Check whether a number can be represented as difference of two consecutive cubes | Python3 program for the above approach ; Function to print the two consecutive numbers whose difference is N ; Iterate in the range [ 0 , 10 ^ 5 ] ; Function to check if N is a perfect cube ; Find floating povalue of square root of x . ; If square root is an integer ; Function to check whether a number can be represented as difference of two consecutive cubes ; Check if 12 * N - 3 is a perfect square or not ; Given number N
import math NEW_LINE def printt ( N ) : NEW_LINE INDENT for i in range ( 100000 ) : NEW_LINE INDENT if ( pow ( i + 1 , 3 ) - pow ( i , 3 ) == N ) : NEW_LINE INDENT print ( i , ' ' , i + 1 ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT def isPerfectSquare ( x ) : NEW_LINE INDENT sr = math . sqrt ( x ) NEW_LINE return ( ( sr - math . floor ( sr ) ) == 0 ) NEW_LINE DEDENT def diffCube ( N ) : NEW_LINE INDENT return isPerfectSquare ( 12 * N - 3 ) NEW_LINE DEDENT N = 19 NEW_LINE if ( diffCube ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE printt ( N ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Sum of bit differences for numbers from 0 to N | Set 2 | Recursive function to find sum of different bits between consecutive numbers from 0 to N ; Base case ; Calculate the Nth term ; Given number ; Function call
def totalCountDifference ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return n + totalCountDifference ( n // 2 ) NEW_LINE DEDENT N = 5 NEW_LINE print ( totalCountDifference ( N ) ) NEW_LINE
Sum of elements of a Geometric Progression ( GP ) in a given range | Function to find sum in the given range ; Find the value of k ; Find the common difference ; Find the sum ; Driver code
def findSum ( arr , n , left , right ) : NEW_LINE INDENT k = right - left + 1 NEW_LINE d = arr [ 1 ] // arr [ 0 ] NEW_LINE ans = arr [ left - 1 ] NEW_LINE if d == 1 : NEW_LINE INDENT ans = ans * d * k NEW_LINE DEDENT else : NEW_LINE INDENT ans = ans * ( d ** k - 1 ) // ( d - 1 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 ] NEW_LINE queries = 3 NEW_LINE q = [ [ 2 , 4 ] , [ 2 , 6 ] , [ 5 , 8 ] ] NEW_LINE n = len ( arr ) NEW_LINE for i in range ( queries ) : NEW_LINE INDENT print ( findSum ( arr , n , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) ) NEW_LINE DEDENT DEDENT
Find the first and last M digits from K | Python3 program to implement the above approach ; Function to find a ^ b modulo M ; Function to find the first and last M digits from N ^ K ; Calculate Last M digits ; Calculate First M digits ; Extract the number after decimal ; Find 10 ^ y ; Move the Decimal Point M - 1 digits forward ; Print the result ; Driver Code
from math import * NEW_LINE def modPower ( a , b , M ) : NEW_LINE INDENT res = 1 NEW_LINE while ( b ) : NEW_LINE INDENT if ( b & 1 ) : NEW_LINE INDENT res = res * a % M NEW_LINE DEDENT a = a * a % M NEW_LINE b >>= 1 NEW_LINE DEDENT return res NEW_LINE DEDENT def findFirstAndLastM ( N , K , M ) : NEW_LINE INDENT lastM = modPower ( N , K , int ( pow ( 10 , M ) ) ) NEW_LINE firstM = 0 NEW_LINE y = K * log10 ( N * 1.0 ) NEW_LINE y = y - int ( y ) NEW_LINE temp = pow ( 10.0 , y ) NEW_LINE firstM = int ( temp * pow ( 10 , M - 1 ) ) NEW_LINE print ( firstM , lastM ) NEW_LINE DEDENT N = 12 NEW_LINE K = 12 NEW_LINE M = 4 NEW_LINE findFirstAndLastM ( N , K , M ) NEW_LINE
Minimize increment / decrement of Array elements to make each modulo K equal | Python3 program for the above approach ; Function to find the minimum operations required to make the modulo of each element of the array equal to each other ; Variable to store minimum operation required ; To store operation required to make all modulo equal ; Iterating through all possible modulo value ; Iterating through all different modulo obtained so far ; Calculating oprn required to make all modulos equal to x ; Checking the operations that will cost less ; Check operation that will cost less ; Update the minimum number of operations ; Returning the answer ; Function to store different modulos ; Set to store all different modulo ; Map to store count of all different modulo obtained ; Storing all different modulo count ; Insert into the set ; Increment count ; Function call to return value of min oprn required ; Driver Code
import sys NEW_LINE from collections import defaultdict NEW_LINE def Find_min ( diff_mod , count_mod , k ) : NEW_LINE INDENT min_oprn = sys . maxsize NEW_LINE oprn = 0 NEW_LINE for x in range ( k ) : NEW_LINE INDENT oprn = 0 NEW_LINE for w in diff_mod : NEW_LINE INDENT if ( w != x ) : NEW_LINE INDENT if ( w == 0 ) : NEW_LINE INDENT oprn += ( min ( x , k - x ) * count_mod [ w ] ) NEW_LINE DEDENT else : NEW_LINE INDENT oprn += ( min ( abs ( x - w ) , k + x - w ) * count_mod [ w ] ) NEW_LINE DEDENT DEDENT DEDENT if ( oprn < min_oprn ) : NEW_LINE INDENT min_oprn = oprn NEW_LINE DEDENT DEDENT return min_oprn NEW_LINE DEDENT def Cal_min ( arr , n , k ) : NEW_LINE INDENT diff_mod = set ( [ ] ) NEW_LINE count_mod = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT diff_mod . add ( arr [ i ] % k ) NEW_LINE count_mod [ arr [ i ] % k ] += 1 NEW_LINE DEDENT return Find_min ( diff_mod , count_mod , k ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 35 , 48 , 23 , 52 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( Cal_min ( arr , n , k ) ) NEW_LINE DEDENT
Minimize Steps required to obtain Sorted Order of an Array | Function to find GCD of two numbers ; Function to calculate the LCM of array elements ; Initialize result ; Function to find minimum steps required to obtain sorted sequence ; Inititalize dat [ ] array for Direct Address Table . ; Calculating steps required for each element to reach its sorted position ; Calculate LCM of the array ; Driver Code
def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def findlcm ( arr , n ) : NEW_LINE INDENT ans = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ans = ( ( arr [ i ] * ans ) // ( gcd ( arr [ i ] , ans ) ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def minimumSteps ( arr , n ) : NEW_LINE INDENT dat = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT dat [ arr [ i - 1 ] ] = i NEW_LINE DEDENT b = [ 0 ] * ( n + 1 ) NEW_LINE j = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT c = 1 NEW_LINE j = dat [ i ] NEW_LINE while ( j != i ) : NEW_LINE INDENT c += 1 NEW_LINE j = dat [ j ] NEW_LINE DEDENT b [ i ] = c NEW_LINE DEDENT print ( findlcm ( b , n ) ) NEW_LINE DEDENT arr = [ 5 , 1 , 4 , 3 , 2 , 7 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE minimumSteps ( arr , N ) NEW_LINE
Largest Ratio Contiguous subarray | Python3 program for the above approach ; Function to return maximum of two double values ; Check if a is greater than b then return a ; Function that returns the Ratio of max Ratio subarray ; Variable to store the maximum ratio ; Compute the product while traversing for subarrays ; Calculate the ratio ; Update max ratio ; Print the answer ; Driver code
import sys NEW_LINE def maximum ( a , b ) : NEW_LINE INDENT if ( a > b ) : NEW_LINE INDENT return a NEW_LINE DEDENT return b NEW_LINE DEDENT def maxSubarrayRatio ( arr , n ) : NEW_LINE INDENT maxRatio = - sys . maxsize - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT ratio = arr [ i ] NEW_LINE for k in range ( i + 1 , j + 1 ) : NEW_LINE INDENT ratio = ratio // arr [ k ] NEW_LINE DEDENT maxRatio = maximum ( maxRatio , ratio ) NEW_LINE DEDENT DEDENT return int ( maxRatio ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 2 , 4 , - 0.2 , - 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxSubarrayRatio ( arr , n ) ) NEW_LINE DEDENT
Even Perfect Number | Python3 program for the above approach ; Function to check for perfect number ; Find a number close to 2 ^ q - 1 ; Calculate q - 1 ; Condition of perfect number ; Check whether q is prime or not ; Check whether 2 ^ q - 1 is a prime number or not ; Function to check for prime number ; Check whether it is equal to 2 or 3 ; Check if it can be divided by 2 and 3 then it is not prime number ; Check whether the given number be divide by other prime numbers ; Driver Code
import math NEW_LINE def check ( num ) : NEW_LINE INDENT root = ( int ) ( math . sqrt ( num ) ) NEW_LINE poww = ( int ) ( math . log ( root ) / math . log ( 2 ) ) NEW_LINE if ( num == ( int ) ( pow ( 2 , poww ) * ( pow ( 2 , poww + 1 ) - 1 ) ) ) : NEW_LINE INDENT if ( isPrime ( poww + 1 ) ) : NEW_LINE INDENT if ( isPrime ( ( int ) ( pow ( 2 , poww + 1 ) ) - 1 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return bool ( False ) NEW_LINE DEDENT elif ( n == 2 or n == 3 ) : NEW_LINE INDENT return bool ( True ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return bool ( False ) NEW_LINE DEDENT for i in range ( 5 , sqrt ( n + 1 ) + 1 , 6 ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return bool ( False ) NEW_LINE DEDENT DEDENT return bool ( True ) NEW_LINE DEDENT DEDENT num = 6 NEW_LINE check ( num ) NEW_LINE
Twin Pythagorean triplets in an array | Function to check if there exist a twin pythagorean triplet in the given array ; Loop to check if there is a Pythagorean triplet in the array ; Check if there is consecutive triple ; Calculate square of array elements ; Driver Code ; Given array arr [ ] ; Function call
def isTriplet ( ar , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT for k in range ( j + 1 , n ) : NEW_LINE INDENT if ( abs ( ar [ i ] - ar [ j ] ) == 1 or abs ( ar [ j ] - ar [ k ] ) == 1 or abs ( ar [ i ] - ar [ k ] ) == 1 ) : NEW_LINE INDENT x = ar [ i ] * ar [ i ] NEW_LINE y = ar [ j ] * ar [ j ] NEW_LINE z = ar [ k ] * ar [ k ] NEW_LINE if ( x == y + z or y == x + z or z == x + y ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 1 , 4 , 6 , 5 ] NEW_LINE ar_size = len ( arr ) NEW_LINE if isTriplet ( arr , ar_size ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT DEDENT
Pair with min absolute difference and whose product is N + 1 or N + 2 | Python3 program for the above approach ; Function to prpair ( a , b ) such that a * b = N + 1 or N + 2 ; Loop to iterate over the desired possible values ; Check for condition 1 ; Check for condition 2 ; Driver Code ; Given Number ; Function Call
from math import sqrt , ceil , floor NEW_LINE def closestDivisors ( n ) : NEW_LINE INDENT for i in range ( ceil ( sqrt ( n + 2 ) ) , - 1 , - 1 ) : NEW_LINE INDENT if ( ( n + 1 ) % i == 0 ) : NEW_LINE INDENT print ( i , " , " , ( n + 1 ) // i ) NEW_LINE break NEW_LINE DEDENT if ( ( n + 2 ) % i == 0 ) : NEW_LINE INDENT print ( i , " , " , ( n + 2 ) // i ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 123 NEW_LINE closestDivisors ( N ) NEW_LINE DEDENT
Is it possible to reach N and M from 1 and 0 respectively as per given condition | Function that find given x and y is possible or not ; Check if x is less than 2 and y is not equal to 0 ; Perform subtraction ; Check if y is divisible by 2 and greater than equal to 0 ; Driver Code ; Given X and Y ; Function Call
def is_possible ( x , y ) : NEW_LINE INDENT if ( x < 2 and y != 0 ) : NEW_LINE INDENT return false NEW_LINE DEDENT y = y - x + 1 NEW_LINE if ( y % 2 == 0 and y >= 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 5 NEW_LINE y = 2 NEW_LINE if ( is_possible ( x , y ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT