codeparrot/xlcost-text-to-code huggingface dataset:
Maximize product of subarray sum with its maximum element | Function to find the maximum product of the sum of the subarray with its maximum element ; Traverse the array arr [ ] ; Increment currSum by a [ i ] ; Maximize the value of currMax ; Maximize the value of largestSum ; If currSum goes less than 0 then update currSum = 0 ; Return the resultant value ; Function to maximize the product of the sum of the subarray with its maximum element ; Find the largest sum of the subarray ; Multiply each array element with - 1 ; Find the largest sum of the subarray with negation of all array element ; Return the resultant maximum value ; Driver Code
def Kadane ( arr , n ) : NEW_LINE INDENT largestSum = 0 NEW_LINE currMax = 0 NEW_LINE currSum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT currSum += arr [ i ] NEW_LINE currMax = max ( currMax , arr [ i ] ) NEW_LINE largestSum = max ( largestSum , currMax * currSum ) NEW_LINE if ( currSum < 0 ) : NEW_LINE INDENT currMax = 0 NEW_LINE currSum = 0 NEW_LINE DEDENT DEDENT return largestSum NEW_LINE DEDENT def maximumWeight ( arr , n ) : NEW_LINE INDENT largestSum = Kadane ( arr , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ i ] = - arr [ i ] NEW_LINE DEDENT largestSum = max ( largestSum , Kadane ( arr , n ) ) NEW_LINE return largestSum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , - 3 , 8 , - 2 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE print ( maximumWeight ( arr , N ) ) NEW_LINE DEDENT
Partition an array into two subsets with equal count of unique elements | Function to partition the array into two subsets such that count of unique elements in both subsets is the same ; Stores the subset number for each array elements ; Stores the count of unique array elements ; Stores the frequency of elements ; Traverse the array ; Count of elements having a frequency of 1 ; Check if there exists any element with frequency > 2 ; Count of elements needed to have frequency exactly 1 in each subset ; Initialize all values in the array ans [ ] as 1 ; Traverse the array ans [ ] ; This array element is a part of first subset ; Half array elements with frequency 1 are part of the second subset ; If count of elements is exactly 1 are odd and has no element with frequency > 2 ; If count of elements that occurs exactly once are even ; Print the result ; If the count of elements has exactly 1 frequency are odd and there is an element with frequency greater than 2 ; Print the result ; Driver Codea
def arrayPartition ( a , n ) : NEW_LINE INDENT ans = [ 0 ] * n NEW_LINE cnt = 0 NEW_LINE ind , flag = 0 , 0 NEW_LINE mp = { } NEW_LINE for i in a : NEW_LINE INDENT mp [ i ] = mp . get ( i , 0 ) + 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( ( a [ i ] in mp ) and mp [ a [ i ] ] == 1 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT if ( mp [ a [ i ] ] > 2 and flag == 0 ) : NEW_LINE INDENT flag = 1 NEW_LINE ind = i NEW_LINE DEDENT DEDENT p = ( cnt + 1 ) // 2 NEW_LINE ans1 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans [ i ] = 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( ( a [ i ] in mp ) and mp [ a [ i ] ] == 1 and ans1 < p ) : NEW_LINE INDENT ans [ i ] = 1 NEW_LINE ans1 += 1 NEW_LINE DEDENT elif ( ( a [ i ] in mp ) and mp [ a [ i ] ] == 1 ) : NEW_LINE INDENT ans [ i ] = 2 NEW_LINE DEDENT DEDENT if ( cnt % 2 == 1 and flag == 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT if ( cnt % 2 == 0 ) : NEW_LINE INDENT print ( * ans ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( ind == i ) : NEW_LINE INDENT print ( 2 , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 3 , 4 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE arrayPartition ( arr , N ) NEW_LINE DEDENT
Modify a given array by replacing each element with the sum or product of their digits based on a given condition | Function to modify the given array as per the given conditions ; Traverse the given array arr [ ] ; Initialize the count of even and odd digits ; Initialize temp with the current array element ; For count the number of even digits ; Increment the odd count ; Otherwise ; Divide temp by 10 ; Performe addition ; Performe multiplication ; Otherwise ; Driver Code
def evenOdd ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT even_digits = 0 ; NEW_LINE odd_digits = 0 ; NEW_LINE temp = arr [ i ] ; NEW_LINE while ( temp ) : NEW_LINE INDENT if ( ( temp % 10 ) & 1 ) : NEW_LINE INDENT odd_digits += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT even_digits += 1 ; NEW_LINE DEDENT temp = temp // 10 NEW_LINE DEDENT if ( even_digits > odd_digits ) : NEW_LINE INDENT res = 0 ; NEW_LINE while ( arr [ i ] ) : NEW_LINE INDENT res += arr [ i ] % 10 ; NEW_LINE arr [ i ] = arr [ i ] // 10 ; NEW_LINE DEDENT print ( res , end = " ▁ " ) ; NEW_LINE DEDENT elif ( odd_digits > even_digits ) : NEW_LINE INDENT res = 1 ; NEW_LINE while ( arr [ i ] ) : NEW_LINE INDENT res *= arr [ i ] % 10 ; NEW_LINE arr [ i ] = arr [ i ] // 10 NEW_LINE DEDENT print ( res , end = " ▁ " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT arr = [ 113 , 141 , 214 , 3186 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE evenOdd ( arr , N ) ; NEW_LINE
Find prime factors of Z such that Z is product of all even numbers till N that are product of two distinct prime numbers | Function to print the prime factorization of the product of all numbers <= N that are even and can be expressed as a product of two distinct prime numbers ; Sieve of Eratosthenese ; Store prime numbers in the range [ 3 , N / 2 ] ; Print the coefficient of 2 in the prime factorization ; Print the coefficients of other primes ; Driver code ; Input ; Function calling
def primeFactorization ( N ) : NEW_LINE INDENT sieve = [ 0 for i in range ( N // 2 + 1 ) ] NEW_LINE for i in range ( 2 , N // 2 + 1 , 1 ) : NEW_LINE INDENT if ( sieve [ i ] == 0 ) : NEW_LINE INDENT for j in range ( i * i , N // 2 + 1 , i ) : NEW_LINE INDENT sieve [ j ] = 1 NEW_LINE DEDENT DEDENT DEDENT prime = [ ] NEW_LINE for i in range ( 3 , N // 2 + 1 , 1 ) : NEW_LINE INDENT if ( sieve [ i ] == 0 ) : NEW_LINE INDENT prime . append ( i ) NEW_LINE DEDENT DEDENT x = len ( prime ) NEW_LINE print ( "2 - > " , x ) NEW_LINE for i in prime : NEW_LINE INDENT print ( i , " - > 1" ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 18 NEW_LINE primeFactorization ( N ) NEW_LINE DEDENT
Sum of the first M elements of Array formed by infinitely concatenating given array | Function to find the sum of first M numbers formed by the infinite concatenation of the array A [ ] ; Stores the resultant sum ; Iterate over the range [ 0 , M - 1 ] ; Add the value A [ i % N ] to sum ; Return the resultant sum ; Driver Code
def sumOfFirstM ( A , N , M ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( M ) : NEW_LINE INDENT sum = sum + A [ i % N ] NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 ] NEW_LINE M = 5 NEW_LINE N = len ( arr ) NEW_LINE print ( sumOfFirstM ( arr , N , M ) ) NEW_LINE DEDENT
Check if it is possible to reach M from 0 by given paths | Function to check if it is possible to reach M from 0 ; Stores the farther point that can reach from 1 point ; Stores the farthest point it can go for each index i ; Initialize rightMost [ i ] with 0 ; Traverse the array ; Update the rightMost position reached from a1 ; Find the farthest point it can reach from i ; If point < can be reached ; Driver Code
def canReach0toM ( a , n , m ) : NEW_LINE INDENT rightMost = [ 0 for i in range ( m + 1 ) ] NEW_LINE dp = [ 0 for i in range ( m + 1 ) ] NEW_LINE for i in range ( m + 1 ) : NEW_LINE INDENT rightMost [ i ] = 0 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT a1 = a [ i ] [ 0 ] NEW_LINE b1 = a [ i ] [ 1 ] NEW_LINE rightMost [ a1 ] = max ( rightMost [ a1 ] , b1 ) NEW_LINE DEDENT i = m NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT dp [ i ] = i NEW_LINE j = min ( m , rightMost [ i ] ) NEW_LINE while ( j > i ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i ] , dp [ j ] ) NEW_LINE j -= 1 NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT if ( dp [ 0 ] >= m ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 0 , 2 ] , [ 2 , 2 ] , [ 2 , 5 ] , [ 4 , 5 ] ] NEW_LINE M = 5 NEW_LINE N = len ( arr ) NEW_LINE canReach0toM ( arr , N , M ) NEW_LINE DEDENT
Count of non co | Recursive program to return gcd of two numbers ; Function to count the number of non co - prime pairs for each query ; Traverse the array arr [ ] ; Stores the count of non co - prime pairs ; Iterate over the range [ 1 , x ] ; Iterate over the range [ x , y ] ; If gcd of current pair is greater than 1 ; Driver code ; Given Input ; Function Call
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 countPairs ( arr , N ) : NEW_LINE INDENT for i in range ( 0 , N ) : NEW_LINE INDENT count = 0 NEW_LINE for x in range ( 1 , arr [ i ] + 1 ) : NEW_LINE INDENT for y in range ( x , arr [ i ] + 1 ) : NEW_LINE INDENT if gcd ( x , y ) > 1 : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT print ( count , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 10 , 20 ] NEW_LINE N = len ( arr ) NEW_LINE countPairs ( arr , N ) NEW_LINE DEDENT
Maximum number of multiplication by 3 or division by 2 operations possible on an array | Function to count maximum number of multiplication by 3 or division by 2 operations that can be performed ; Stores the maximum number of operations possible ; Traverse the array arr [ ] ; Iterate until arr [ i ] is even ; Increment count by 1 ; Update arr [ i ] ; Return the value of Count as the answer ; Given Input ; Function Call
def maximumTurns ( arr , N ) : NEW_LINE INDENT Count = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT while ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT Count += 1 NEW_LINE arr [ i ] = arr [ i ] // 2 NEW_LINE DEDENT DEDENT return Count NEW_LINE DEDENT arr = [ 5 , 2 , 4 ] NEW_LINE M = 3 NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE print ( maximumTurns ( arr , N ) ) NEW_LINE
Distribute the white and black objects into maximum groups under certain constraints | Function to check if it is possible to distribute W and B into maximum groups possible ; If W is greater than B , swap them ; Distribution is not possible ; Distribution is possible ; Driver code ; Input ; Function call
def isPossible ( W , B , D ) : NEW_LINE INDENT if ( W > B ) : NEW_LINE INDENT temp = W NEW_LINE W = B NEW_LINE B = temp NEW_LINE DEDENT if ( B > W * ( D + 1 ) ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT W = 2 NEW_LINE B = 5 NEW_LINE D = 2 NEW_LINE isPossible ( W , B , D ) NEW_LINE DEDENT
Find the maximum GCD possible for some pair in a given range [ L , R ] | Function to calculate GCD ; Function to calculate maximum GCD in a range ; Variable to store the answer ; If Z has two multiples in [ L , R ] ; Update ans ; Return the value ; Input ; Function Call
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 maxGCDInRange ( L , R ) : NEW_LINE INDENT ans = 1 NEW_LINE for Z in range ( R , 1 , - 1 ) : NEW_LINE INDENT if ( ( ( R // Z ) - ( L - 1 ) // Z ) > 1 ) : NEW_LINE INDENT ans = Z NEW_LINE break NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT L = 102 NEW_LINE R = 139 NEW_LINE print ( maxGCDInRange ( L , R ) ) NEW_LINE
Number of ways to form a number with maximum Ks in it | Function to calculate number of ways a number can be formed that has the maximum number of Ks ; Convert to string ; Calculate length of subarrays that can contribute to the answer ; Count length of subarray where adjacent digits add up to K ; Current subarray can contribute to the answer only if it is odd ; Return the answer ; Driver code ; Input ; Function call
def noOfWays ( N , K ) : NEW_LINE INDENT S = str ( N ) NEW_LINE ans = 1 NEW_LINE for i in range ( 1 , len ( S ) ) : NEW_LINE INDENT count = 1 NEW_LINE while ( i < len ( S ) and ord ( S [ i ] ) + ord ( S [ i - 1 ] ) - 2 * ord ( '0' ) == K ) : NEW_LINE INDENT count += 1 NEW_LINE i += 1 NEW_LINE DEDENT if ( count % 2 ) : NEW_LINE INDENT ans *= ( count + 1 ) // 2 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 1454781 NEW_LINE K = 9 NEW_LINE print ( noOfWays ( N , K ) ) NEW_LINE DEDENT
Minimum bit swaps between given numbers to make their Bitwise OR equal to Bitwise AND | Function for counting number of set bit ; Function to return the count of minimum operations required ; cnt to count the number of bits set in A and in B ; If odd numbers of total set bits ; one_zero = 1 in A and 0 in B at ith bit similarly for zero_one ; When bitpos is set in B , unset in B ; When bitpos is set in A , unset in B ; Number of moves is half of number pairs of each group ; Odd number pairs ; Driver code ; Input ; Function call to compute the result
def countSetBits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n ) : NEW_LINE INDENT n = n & ( n - 1 ) NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def minOperations ( A , B ) : NEW_LINE INDENT cnt1 = 0 NEW_LINE cnt2 = 0 NEW_LINE cnt1 += countSetBits ( A ) NEW_LINE cnt2 += countSetBits ( B ) NEW_LINE if ( ( cnt1 + cnt2 ) % 2 != 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT oneZero = 0 NEW_LINE zeroOne = 0 NEW_LINE ans = 0 NEW_LINE for i in range ( max ( cnt1 , cnt2 ) ) : NEW_LINE INDENT bitpos = 1 << i NEW_LINE if ( ( not ( bitpos & A ) ) and ( bitpos & B ) ) : NEW_LINE INDENT zeroOne += 1 NEW_LINE DEDENT if ( ( bitpos & A ) and ( not ( bitpos & B ) ) ) : NEW_LINE INDENT oneZero += 1 NEW_LINE DEDENT DEDENT ans = ( zeroOne // 2 ) + ( oneZero // 2 ) NEW_LINE if ( zeroOne % 2 != 0 ) : NEW_LINE INDENT ans += 2 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 27 NEW_LINE B = 5 NEW_LINE print ( minOperations ( A , B ) ) NEW_LINE DEDENT
Largest sum subarray of size K containing consecutive elements | Python3 program for the above approach ; Function to find the largest sum subarray such that it contains K consecutive elements ; Stores sum of subarray having K consecutive elements ; Stores the maximum sum among all subarrays of size K having consecutive elements ; Traverse the array ; Store K elements of one subarray at a time ; Sort the duplicate array in ascending order ; Checks if elements in subarray are consecutive or not ; Traverse the k elements ; If not consecutive , break ; If flag is true update the maximum sum ; Stores the sum of elements of the current subarray ; Update the max_sum ; Reset curr_sum ; Return the result ; Driver Code
import sys NEW_LINE def maximumSum ( A , N , K ) : NEW_LINE INDENT curr_sum = 0 NEW_LINE max_sum = - sys . maxsize - 1 NEW_LINE for i in range ( N - K + 1 ) : NEW_LINE INDENT dupl_arr = A [ i : i + K ] NEW_LINE dupl_arr . sort ( ) NEW_LINE flag = True NEW_LINE for j in range ( 1 , K , 1 ) : NEW_LINE INDENT if ( dupl_arr [ j ] - dupl_arr [ j - 1 ] != 1 ) : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT temp = 0 NEW_LINE curr_sum = temp NEW_LINE curr_sum = sum ( dupl_arr ) NEW_LINE max_sum = max ( max_sum , curr_sum ) NEW_LINE curr_sum = 0 NEW_LINE DEDENT DEDENT return max_sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 12 , 9 , 8 , 10 , 15 , 1 , 3 , 2 ] NEW_LINE K = 3 NEW_LINE N = len ( arr ) NEW_LINE print ( maximumSum ( arr , N , K ) ) NEW_LINE DEDENT
Find the count of Smith Brothers Pairs in a given Array | Python3 program for the above approach ; Array to store all prime less than and equal to MAX ; Utility function for sieve of sundaram ; Main logic of Sundaram . ; Since 2 is a prime number ; only primes are selected ; Function to check whether a number is a smith number . ; Find sum the digits of prime factors of n ; Add its digits of prime factors to pDigitSum . ; One prime factor is still to be summed up ; Now sum the digits of the original number ; Return the answer ; Function to check if X and Y are a Smith Brother Pair ; Function to find pairs from array ; Iterate through all pairs ; Increment count if there is a smith brother pair ; Driver code ; Preprocessing ; Input ; Function call
from math import sqrt NEW_LINE MAX = 10000 NEW_LINE primes = [ ] NEW_LINE def sieveSundaram ( ) : NEW_LINE INDENT marked = [ 0 for i in range ( MAX // 2 + 100 ) ] NEW_LINE j = 0 NEW_LINE for i in range ( 1 , int ( ( sqrt ( MAX ) - 1 ) / 2 ) + 1 , 1 ) : NEW_LINE INDENT for j in range ( ( i * ( i + 1 ) ) << 1 , MAX // 2 + 1 , j + 2 * i + 1 ) : NEW_LINE INDENT marked [ j ] = True NEW_LINE DEDENT DEDENT primes . append ( 2 ) NEW_LINE for i in range ( 1 , MAX // 2 + 1 , 1 ) : NEW_LINE INDENT if ( marked [ i ] == False ) : NEW_LINE INDENT primes . append ( 2 * i + 1 ) NEW_LINE DEDENT DEDENT DEDENT def isSmith ( n ) : NEW_LINE INDENT original_no = n NEW_LINE pDigitSum = 0 NEW_LINE i = 0 NEW_LINE while ( primes [ i ] <= n // 2 ) : NEW_LINE INDENT while ( n % primes [ i ] == 0 ) : NEW_LINE INDENT p = primes [ i ] NEW_LINE n = n // p NEW_LINE while ( p > 0 ) : NEW_LINE INDENT pDigitSum += ( p % 10 ) NEW_LINE p = p // 10 NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT if ( n != 1 and n != original_no ) : NEW_LINE INDENT while ( n > 0 ) : NEW_LINE INDENT pDigitSum = pDigitSum + n % 10 NEW_LINE n = n // 10 NEW_LINE DEDENT DEDENT sumDigits = 0 NEW_LINE while ( original_no > 0 ) : NEW_LINE INDENT sumDigits = sumDigits + original_no % 10 NEW_LINE original_no = original_no // 10 NEW_LINE DEDENT return ( pDigitSum == sumDigits ) NEW_LINE DEDENT def isSmithBrotherPair ( X , Y ) : NEW_LINE INDENT return ( isSmith ( X ) and isSmith ( Y ) and abs ( X - Y ) == 1 ) NEW_LINE DEDENT def countSmithBrotherPairs ( A , N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( i + 1 , N , 1 ) : NEW_LINE INDENT if ( isSmithBrotherPair ( A [ i ] , A [ j ] ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT sieveSundaram ( ) NEW_LINE A = [ 728 , 729 , 28 , 2964 , 2965 ] NEW_LINE N = len ( A ) NEW_LINE print ( countSmithBrotherPairs ( A , N ) ) NEW_LINE DEDENT
Count pair sums that are factors of the sum of the array | Function to find the number of pairs whose sums divides the sum of array ; Initialize the totalSum and count as 0 ; Calculate the total sum of array ; Generate all possible pairs ; If the sum is a factor of totalSum or not ; Increment count by 1 ; Print the total count obtained ; Driver Code
def countPairs ( arr , N ) : NEW_LINE INDENT count = 0 NEW_LINE totalSum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT totalSum += arr [ i ] NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( i + 1 , N , 1 ) : NEW_LINE INDENT if ( totalSum % ( arr [ i ] + arr [ j ] ) == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE countPairs ( arr , N ) NEW_LINE DEDENT
Maximize average of the ratios of N pairs by M increments | Function to find the change in the ratio in pair after applying operation ; Stores the current ratio ; Stores the new ratio ; Stores the increase in ratio ; Returns the change ; Function to find the maximum average of the ratio of the pairs by applying M increments ; Stores the required result ; Declare a priority queue for storing the increments ; Store the increase in the ratio after applying one operation ; Push the increased value and index value in priority queue ; Store the ratio ; Update the value of sum ; Iterate while M > 0 ; Add the maximum change to the sum ; Remove the element from the priority queue ; Increase the pairs elements by 1 on which operation is applied ; Push the updated change of the pair in priority queue ; Decrease the operation count ; Update the value of the sum by dividing it by N ; Return the result ; Driver Code
def change ( pas , total ) : NEW_LINE INDENT currentPassRatio = pas / total NEW_LINE newPassRatio = ( pas + 1 ) / ( total + 1 ) NEW_LINE increase = newPassRatio - currentPassRatio NEW_LINE return increase NEW_LINE DEDENT def maximumAverage ( v , M , N ) : NEW_LINE INDENT sum = 0 NEW_LINE increase , average = 0 , 0 NEW_LINE pq = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT increase = change ( v [ i ] [ 0 ] , v [ i ] [ 1 ] ) NEW_LINE pq . append ( [ increase , i ] ) NEW_LINE average = v [ i ] [ 0 ] * 1.0 / v [ i ] [ 1 ] NEW_LINE sum += average NEW_LINE DEDENT pq = sorted ( pq ) NEW_LINE while ( M > 0 ) : NEW_LINE INDENT sum += pq [ - 1 ] [ 0 ] NEW_LINE i = pq [ - 1 ] [ 1 ] NEW_LINE del pq [ - 1 ] NEW_LINE v [ i ] [ 0 ] += 1 NEW_LINE v [ i ] [ 1 ] += 1 NEW_LINE pq . append ( [ change ( v [ i ] [ 0 ] , v [ i ] [ 1 ] ) , i ] ) NEW_LINE M -= 1 NEW_LINE pq = sorted ( pq ) NEW_LINE DEDENT ans = sum / N NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT V = [ [ 1 , 2 ] , [ 3 , 5 ] , [ 2 , 2 ] ] NEW_LINE M = 2 NEW_LINE N = len ( V ) NEW_LINE print ( round ( maximumAverage ( V , M , N ) , 6 ) ) NEW_LINE DEDENT
Print all numbers that are divisors of N and are co | python 3 program for the above approach ; Function to print all numbers that are divisors of N and are co - prime with the quotient of their division ; Iterate upto square root of N ; If divisors are equal and gcd is 1 , then print only one of them ; Otherwise print both ; Driver Code
from math import sqrt , gcd NEW_LINE def printUnitaryDivisors ( n ) : NEW_LINE INDENT for i in range ( 1 , int ( sqrt ( n ) ) + 1 , 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( n // i == i and gcd ( i , n // i ) == 1 ) : NEW_LINE INDENT print ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( gcd ( i , n // i ) == 1 ) : NEW_LINE INDENT print ( i , n // i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 12 NEW_LINE printUnitaryDivisors ( N ) NEW_LINE DEDENT
Maximize K to make given array Palindrome when each element is replaced by its remainder with K | utility function to calculate the GCD of two numbers ; Function to calculate the largest K , replacing all elements of an array A by their modulus with K , makes A a palindromic array ; check if A is palindrome ; A is not palindromic ; K can be infitely large in this case ; variable to store the largest K that makes A palindromic ; return the required answer ; Driver code ; Input ; Function call
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 def largestK ( A , N ) : NEW_LINE INDENT l , r , flag = 0 , N - 1 , 0 NEW_LINE while ( l < r ) : NEW_LINE INDENT if ( A [ l ] != A [ r ] ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT l += 1 NEW_LINE r -= 1 NEW_LINE DEDENT if ( flag == 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT K = abs ( A [ 0 ] - A [ N - 1 ] ) NEW_LINE for i in range ( 1 , N // 2 ) : NEW_LINE INDENT K = gcd ( K , abs ( A [ i ] - A [ N - i - 1 ] ) ) NEW_LINE DEDENT return K NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 2 , 3 , 2 , 1 ] NEW_LINE N = len ( A ) NEW_LINE print ( largestK ( A , N ) ) NEW_LINE DEDENT