codeparrot/xlcost-text-to-code huggingface dataset:
Minimum time required to schedule K processes | Function to find minimum required time to schedule all process ; Stores max element from A [ ] ; Find the maximum element ; Stores frequency of each element ; Stores minimum time required to schedule all process ; Count frequencies of elements ; Find the minimum time ; Decrease the value of K ; Increment tmp [ i / 2 ] ; Increment the count ; Return count , if all process are scheduled ; Increment count ; Return the count ; If it is not possible to schedule all process ; Driver code
def minTime ( A , n , K ) : NEW_LINE INDENT max_ability = A [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT max_ability = max ( max_ability , A [ i ] ) NEW_LINE DEDENT tmp = [ 0 for i in range ( max_ability + 1 ) ] NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT tmp [ A [ i ] ] += 1 NEW_LINE DEDENT i = max_ability NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( tmp [ i ] != 0 ) : NEW_LINE INDENT if ( tmp [ i ] * i < K ) : NEW_LINE INDENT K -= ( i * tmp [ i ] ) NEW_LINE tmp [ i // 2 ] += tmp [ i ] NEW_LINE count += tmp [ i ] NEW_LINE if ( K <= 0 ) : NEW_LINE INDENT return count NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( K % i != 0 ) : NEW_LINE INDENT count += ( K // i ) + 1 NEW_LINE DEDENT else : NEW_LINE INDENT count += ( K // i ) NEW_LINE DEDENT return count NEW_LINE DEDENT DEDENT i -= 1 NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 1 , 7 , 2 , 4 ] NEW_LINE N = 5 NEW_LINE K = 15 NEW_LINE print ( minTime ( arr , N , K ) ) NEW_LINE DEDENT
Minimum length of a rod that can be split into N equal parts that can further be split into given number of equal parts | Function to find GCD of two numbers a and b ; Base Case ; Find GCD recursively ; Function to find the LCM of the resultant array ; Initialize a variable ans as the first element ; Traverse the array ; Update LCM ; Return the minimum length of the rod ; Function to find the minimum length of the rod that can be divided into N equals parts and each part can be further divided into arr [ i ] equal parts ; Print the result ; 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 = arr [ 0 ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans = ( ( ( arr [ i ] * ans ) ) / ( gcd ( arr [ i ] , ans ) ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def minimumRod ( A , N ) : NEW_LINE INDENT print ( int ( N * findlcm ( A , N ) ) ) NEW_LINE DEDENT arr = [ 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE minimumRod ( arr , N ) NEW_LINE
Check if N can be represented as sum of positive integers containing digit D at least once | Function to check if N contains digit D in it ; Iterate until N is positive ; Find the last digit ; If the last digit is the same as digit D ; Return false ; Function to check if the value of N can be represented as sum of integers having digit d in it ; Iterate until N is positive ; Check if N contains digit D or not ; Subtracting D from N ; Return false ; Driver Code
def findDigit ( N , D ) : NEW_LINE INDENT while ( N > 0 ) : NEW_LINE INDENT a = N % 10 NEW_LINE if ( a == D ) : NEW_LINE INDENT return True NEW_LINE DEDENT N /= 10 NEW_LINE DEDENT return False NEW_LINE DEDENT def check ( N , D ) : NEW_LINE INDENT while ( N > 0 ) : NEW_LINE INDENT if ( findDigit ( N , D ) == True ) : NEW_LINE INDENT return True NEW_LINE DEDENT N -= D NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 24 NEW_LINE D = 7 NEW_LINE if ( check ( N , D ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Number of relations that are neither Reflexive nor Irreflexive on a Set | Python program for the above approach ; Function to calculate x ^ y modulo 10 ^ 9 + 7 in O ( log y ) ; Stores the result of ( x ^ y ) ; Update x , if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with res ; Divide y by 2 ; Update the value of x ; Return the value of x ^ y ; Function to count the number of relations that are neither reflexive nor irreflexive ; Return the resultant count ; Driver Code
mod = 1000000007 NEW_LINE def power ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % mod NEW_LINE if ( x == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( y > 0 ) : NEW_LINE INDENT if ( y % 2 == 1 ) : NEW_LINE INDENT res = ( res * x ) % mod NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % mod NEW_LINE DEDENT return res NEW_LINE DEDENT def countRelations ( N ) : NEW_LINE INDENT print ( ( power ( 2 , N ) - 2 ) * power ( 2 , N * N - N ) ) NEW_LINE DEDENT N = 2 NEW_LINE countRelations ( N ) NEW_LINE
Minimum operations required to make all elements in an array of first N odd numbers equal | Function to find the minimum number of operations required to make the array elements equal ; Stores the array elements ; Stores the sum of the array ; Iterate over the range [ 0 , N ] ; Update the value arr [ i ] ; Increment the sum by the value arr [ i ] ; Stores the middle element ; If N is even ; Otherwise ; Stores the result ; Traverse the range [ 0 , N / 2 ] ; Update the value of ans ; Return the result ; Driver Code
def minOperations ( N ) : NEW_LINE INDENT arr = [ 0 ] * N NEW_LINE sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT arr [ i ] = ( 2 * i ) + 1 NEW_LINE sum = sum + arr [ i ] NEW_LINE DEDENT mid = 0 NEW_LINE if N % 2 == 0 : NEW_LINE INDENT mid = sum / N NEW_LINE DEDENT else : NEW_LINE INDENT mid = arr [ int ( N / 2 ) ] NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( int ( N / 2 ) ) : NEW_LINE INDENT ans += mid - arr [ i ] NEW_LINE DEDENT return int ( ans ) NEW_LINE DEDENT N = 6 NEW_LINE print ( minOperations ( N ) ) NEW_LINE
Minimum operations required to make all elements in an array of first N odd numbers equal | Function to find the minimum number of operations required to make the array elements equal ; If the value of N is even ; Return the value ; Otherwise , N is odd ; Return the value ; Driver Code
def minOperation ( N ) : NEW_LINE INDENT if ( N % 2 == 0 ) : NEW_LINE INDENT return ( N / 2 ) * ( N / 2 ) NEW_LINE DEDENT k = ( N - 1 ) / 2 NEW_LINE return ( k * ( k + 1 ) ) NEW_LINE DEDENT N = 6 NEW_LINE print ( int ( minOperation ( N ) ) ) NEW_LINE
Bitwise XOR of Bitwise AND of all pairs from two given arrays | Function to find the Bitwise XOR of Bitwise AND of all pairs from the arrays arr1 [ ] and arr2 [ ] ; Stores the result ; Iterate over the range [ 0 , N - 1 ] ; Iterate over the range [ 0 , M - 1 ] ; Stores Bitwise AND of the pair { arr1 [ i ] , arr2 [ j ] } ; Update res ; Return the res ; Driver Code ; Input
def findXORS ( arr1 , arr2 , N , M ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT temp = arr1 [ i ] & arr2 [ j ] NEW_LINE res ^= temp NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 1 , 2 , 3 ] NEW_LINE arr2 = [ 6 , 5 ] NEW_LINE N = len ( arr1 ) NEW_LINE M = len ( arr2 ) NEW_LINE print ( findXORS ( arr1 , arr2 , N , M ) ) NEW_LINE DEDENT
Bitwise XOR of Bitwise AND of all pairs from two given arrays | Function to find the Bitwise XOR of Bitwise AND of all pairs from the arrays arr1 [ ] and arr2 [ ] ; Stores XOR of array arr1 [ ] ; Stores XOR of array arr2 [ ] ; Traverse the array arr1 [ ] ; Traverse the array arr2 [ ] ; Return the result ; Driver Code ; Input
def findXORS ( arr1 , arr2 , N , M ) : NEW_LINE INDENT XORS1 = 0 NEW_LINE XORS2 = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT XORS1 ^= arr1 [ i ] NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT XORS2 ^= arr2 [ i ] NEW_LINE DEDENT return XORS1 and XORS2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 1 , 2 , 3 ] NEW_LINE arr2 = [ 6 , 5 ] NEW_LINE N = len ( arr1 ) NEW_LINE M = len ( arr2 ) NEW_LINE print ( findXORS ( arr1 , arr2 , N , M ) ) NEW_LINE DEDENT
Count numbers up to N that cannot be expressed as sum of at least two consecutive positive integers | Function to check if a number can be expressed as a power of 2 ; if N is power of two ; Function to count numbers that cannot be expressed as sum of two or more consecutive + ve integers ; Stores the resultant count of integers ; Iterate over the range [ 1 , N ] ; Check if i is power of 2 ; Increment the count if i is not power of 2 ; Print the value of count ; Driver Code
def isPowerof2 ( n ) : NEW_LINE INDENT return ( ( n & ( n - 1 ) ) and n ) NEW_LINE DEDENT def countNum ( N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT flag = isPowerof2 ( i ) NEW_LINE if ( not flag ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 100 NEW_LINE countNum ( N ) NEW_LINE DEDENT
Count numbers up to N that cannot be expressed as sum of at least two consecutive positive integers | Python 3 program for the above approach ; Function to count numbers that cannot be expressed as sum of two or more consecutive + ve integers ; Stores the count of such numbers ; Driver Code
import math NEW_LINE def countNum ( N ) : NEW_LINE INDENT ans = int ( math . log2 ( N ) ) + 1 NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 100 NEW_LINE countNum ( N ) NEW_LINE DEDENT
Replace array elements that contains K as a digit with the nearest power of K | Python3 program for the above approach ; Function to calculate the power of base nearest to x ; Stores logX to the base K ; Function to replace array elements with nearest power of K ; Traverse the array ; Convert integer into a string ; If K is found , then replace with the nearest power of K ; Print the array ; Given array ; Given value of K ; Function call to replace array elements with nearest power of K
import math NEW_LINE def nearestPow ( x , base ) : NEW_LINE INDENT k = int ( math . log ( x , base ) ) NEW_LINE if abs ( base ** k - x ) < abs ( base ** ( k + 1 ) - x ) : NEW_LINE INDENT return base ** k NEW_LINE DEDENT else : NEW_LINE INDENT return base ** ( k + 1 ) NEW_LINE DEDENT DEDENT def replaceWithNearestPowerOfK ( arr , K ) : NEW_LINE INDENT for i in range ( len ( arr ) ) : NEW_LINE INDENT strEle = str ( arr [ i ] ) NEW_LINE for c in strEle : NEW_LINE INDENT if int ( c ) == K : NEW_LINE INDENT arr [ i ] = nearestPow ( arr [ i ] , K ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT print ( arr ) NEW_LINE DEDENT arr = [ 432 , 953 , 232 , 333 ] NEW_LINE K = 3 NEW_LINE replaceWithNearestPowerOfK ( arr , K ) NEW_LINE
Difference between ceil of array sum divided by K and sum of ceil of array elements divided by K | Python3 program for the above approach ; Function to find absolute difference between array sum divided by x and sum of ceil of array elements divided by x ; Stores the total sum ; Stores the sum of ceil of array elements divided by x ; Traverse the array ; Adding each array element ; Add the value ceil of arr [ i ] / x ; Find the ceil of the total sum divided by x ; Return absolute difference ; Driver Code
from math import ceil NEW_LINE def ceilDifference ( arr , n , x ) : NEW_LINE INDENT totalSum = 0 NEW_LINE perElementSum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT totalSum += arr [ i ] NEW_LINE perElementSum += ceil ( arr [ i ] / x ) NEW_LINE DEDENT totalCeilSum = ceil ( totalSum / x ) NEW_LINE return abs ( perElementSum - totalCeilSum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE K = 4 NEW_LINE N = len ( arr ) NEW_LINE print ( ceilDifference ( arr , N , K ) ) NEW_LINE DEDENT
Remove trailing zeros from the sum of two numbers ( Using Stack ) | Function to remove trailing zeros from the sum of two numbers ; Stores the sum of A and B ; Stores the digits ; Stores the equivalent string of integer N ; Traverse the string ; Push the digit at i in the stack ; While top element is '0 ; Pop the top element ; Stores the resultant number without tailing 0 's ; While s is not empty ; Append top element of S in res ; Pop the top element of S ; Reverse the string res ; Driver Code ; Input ; Function Call
def removeTailing ( A , B ) : NEW_LINE INDENT N = A + B NEW_LINE s = [ ] NEW_LINE strsum = str ( N ) NEW_LINE for i in range ( len ( strsum ) ) : NEW_LINE INDENT s . append ( strsum [ i ] ) NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT while ( s [ - 1 ] == '0' ) : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT res = " " NEW_LINE while ( len ( s ) != 0 ) : NEW_LINE INDENT res = res + ( s [ - 1 ] ) NEW_LINE s . pop ( ) NEW_LINE DEDENT res = list ( res ) NEW_LINE res . reverse ( ) NEW_LINE res = ' ' . join ( res ) NEW_LINE return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 130246 NEW_LINE B = 450164 NEW_LINE print ( removeTailing ( A , B ) ) NEW_LINE DEDENT
Count number of triplets with product not exceeding a given number | Function to count the number of triplets whose product is at most N ; Stores the count of triplets ; Iterate over the range [ 0 , N ] ; Iterate over the range [ 0 , N ] ; If the product of pairs exceeds N ; Increment the count of possible triplets ; Return the total count ; Driver Code
def countTriplets ( N ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( i * j > N ) : NEW_LINE INDENT break NEW_LINE DEDENT ans += N // ( i * j ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 10 NEW_LINE print ( countTriplets ( N ) ) NEW_LINE DEDENT
Sum of an Infinite Geometric Progression ( GP ) | Function to calculate the sum of an infinite Geometric Progression ; Case for Infinite Sum ; Store the sum of GP Series ; Print the value of sum ; Driver Code
def findSumOfGP ( a , r ) : NEW_LINE INDENT if ( abs ( r ) >= 1 ) : NEW_LINE INDENT print ( " Infinite " ) NEW_LINE return NEW_LINE DEDENT sum = a / ( 1 - r ) NEW_LINE print ( int ( sum ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A , R = 1 , 0.5 NEW_LINE findSumOfGP ( A , R ) NEW_LINE DEDENT
Number of Relations that are both Irreflexive and Antisymmetric on a Set | Python 3 program for the above approach ; Function to calculate x ^ y % mod in O ( log y ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the value of x ^ y ; Function to count relations that are irreflexive and antisymmetric in a set consisting of N elements ; Return the resultant count ; Driver Code
mod = 1000000007 NEW_LINE def power ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % mod NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % mod NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % mod NEW_LINE DEDENT return res NEW_LINE DEDENT def numberOfRelations ( N ) : NEW_LINE INDENT return power ( 3 , ( N * N - N ) // 2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 NEW_LINE print ( numberOfRelations ( N ) ) NEW_LINE DEDENT
Count of numbers up to N having at least one prime factor common with N | Python 3 program for the above approach ; Function to count all the numbers in the range [ 1 , N ] having common factor with N other than 1 ; Stores the count of numbers having more than 1 factor with N ; Iterate over the range [ 1 , N ] ; If gcd is not 1 then increment the count ; Print the resultant count ; Driver Code
import math NEW_LINE def countNumbers ( N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( math . gcd ( i , N ) != 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE countNumbers ( N ) NEW_LINE DEDENT
Count of numbers up to N having at least one prime factor common with N | Function to calculate the value of Euler 's totient function ; Initialize result with N ; Find all prime factors of N and subtract their multiples ; Check if p is a prime factor ; If found to be true , then update N and result ; If N has a prime factor greater than sqrt ( N ) , then there can be at - most one such prime factor ; Function to count all the numbers in the range [ 1 , N ] having common factor with N other than 1 ; Stores the resultant count ; Print the count ; Driver Code
def phi ( N ) : NEW_LINE INDENT result = N NEW_LINE for p in range ( 2 , int ( pow ( N , 1 / 2 ) ) + 1 ) : NEW_LINE INDENT if ( N % p == 0 ) : NEW_LINE INDENT while ( N % p == 0 ) : NEW_LINE INDENT N = N / p NEW_LINE DEDENT result -= result // p NEW_LINE DEDENT DEDENT if ( N > 1 ) : NEW_LINE INDENT result -= result // N NEW_LINE DEDENT return result NEW_LINE DEDENT def countNumbers ( N ) : NEW_LINE INDENT count = N - phi ( N ) NEW_LINE print ( count ) NEW_LINE DEDENT N = 5 NEW_LINE countNumbers ( N ) NEW_LINE
Queries to update array by adding or multiplying array elements and print the element present at specified index | Function to modify the array by performing given queries ; Stores the multiplication of all integers of type 1 ; Stores the value obtained after performing queries of type 1 & 2 ; Iterate over the queries ; Query of type 0 ; Update the value of add ; Query of type 1 ; Update the value of mul ; Update the value of add ; Otherwise ; Store the element at index Q [ i ] [ 1 ] ; Prthe result for the current query ; Driver Code
def Query ( arr , N , Q ) : NEW_LINE INDENT mul = 1 NEW_LINE add = 0 NEW_LINE for i in range ( len ( Q ) ) : NEW_LINE INDENT if ( Q [ i ] [ 0 ] == 0 ) : NEW_LINE INDENT add = add + Q [ i ] [ 1 ] NEW_LINE DEDENT elif ( Q [ i ] [ 0 ] == 1 ) : NEW_LINE INDENT mul = mul * Q [ i ] [ 1 ] NEW_LINE add = add * Q [ i ] [ 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT ans = arr [ Q [ i ] [ 1 ] ] * mul + add NEW_LINE print ( ans , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 1 , 23 , 45 , 100 ] NEW_LINE N = len ( arr ) NEW_LINE Q = [ [ 1 , 2 ] , [ 0 , 10 ] , [ 2 , 3 ] , [ 1 , 5 ] , [ 2 , 4 ] ] NEW_LINE Query ( arr , N , Q ) NEW_LINE DEDENT
Minimize cost of converting all array elements to Fibonacci Numbers | Python program for the above approach ; Function to find the N - th Fibonacci Number ; Find the value of a , b , and r ; Find the N - th Fibonacci ; Return the result ; Function to find the Fibonacci number which is nearest to X ; Calculate the value of n for X ; Return the nearest Fibonacci Number ; Function to find the minimum cost to convert all array elements to Fibonacci Numbers ; Stores the total minimum cost ; Traverse the given array arr [ ] ; Find the nearest Fibonacci Number ; Add the cost ; Return the final cost ; Driver Code
import math NEW_LINE def nthFibo ( n ) : NEW_LINE INDENT a = ( 5 ** ( 1 / 2 ) + 1 ) / 2 NEW_LINE b = ( - 5 ** ( 1 / 2 ) + 1 ) / 2 NEW_LINE r = 5 ** ( 1 / 2 ) NEW_LINE ans = ( a ** n - b ** n ) / r NEW_LINE return int ( ans ) NEW_LINE DEDENT def nearFibo ( X ) : NEW_LINE INDENT a = ( 5 ** ( 1 / 2 ) + 1 ) / 2 NEW_LINE n = int ( math . log ( ( 5 ** ( 1 / 2 ) ) * X ) / math . log ( a ) ) NEW_LINE nth = nthFibo ( n ) NEW_LINE nplus = nthFibo ( n + 1 ) NEW_LINE if abs ( X - nth ) < abs ( X - nplus ) : NEW_LINE INDENT return nth NEW_LINE DEDENT else : NEW_LINE INDENT return nplus NEW_LINE DEDENT DEDENT def getCost ( arr ) : NEW_LINE INDENT cost = 0 NEW_LINE for i in arr : NEW_LINE INDENT fibo = nearFibo ( i ) NEW_LINE cost += abs ( i - fibo ) NEW_LINE DEDENT return cost NEW_LINE DEDENT arr = [ 56 , 34 , 23 , 98 , 7 ] NEW_LINE print ( getCost ( arr ) ) NEW_LINE
Maximum sum of pairs that are at least K distance apart in an array | Function to find the largest sum pair that are K distant apart ; Stores the prefix maximum array ; Base Case ; Traverse the array and update the maximum value upto index i ; Stores the maximum sum of pairs ; Iterate over the range [ K , N ] ; Find the maximum value of the sum of valid pairs ; Return the resultant sum ; Driver Code
def getMaxPairSum ( arr , N , K ) : NEW_LINE INDENT preMax = [ 0 ] * N NEW_LINE preMax [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT preMax [ i ] = max ( preMax [ i - 1 ] , arr [ i ] ) NEW_LINE DEDENT res = - 10 ** 8 NEW_LINE for i in range ( K , N ) : NEW_LINE INDENT res = max ( res , arr [ i ] + preMax [ i - K ] ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 8 , 6 , 3 ] NEW_LINE K = 3 NEW_LINE N = len ( arr ) NEW_LINE print ( getMaxPairSum ( arr , N , K ) ) NEW_LINE DEDENT
Program to calculate sum of an Infinite Arithmetic | Function to find the sum of the infinite AGP ; Stores the sum of infinite AGP ; Print the required sum ; Driver Code
def sumOfInfiniteAGP ( a , d , r ) : NEW_LINE INDENT ans = a / ( 1 - r ) + ( d * r ) / ( 1 - r * r ) ; NEW_LINE print ( round ( ans , 6 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a , d , r = 0 , 1 , 0.5 NEW_LINE sumOfInfiniteAGP ( a , d , r ) NEW_LINE DEDENT
Count pairs from an array having GCD equal to the minimum element in the pair | Function to count pairs from an array having GCD equal to minimum element of that pair ; Stores the resultant count ; Iterate over the range [ 0 , N - 2 ] ; Iterate over the range [ i + 1 , N ] ; If arr [ i ] % arr [ j ] is 0 or arr [ j ] % arr [ i ] is 0 ; Increment count by 1 ; Return the resultant count ; Driver Code
def countPairs ( arr , N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( arr [ i ] % arr [ j ] == 0 or arr [ j ] % arr [ i ] == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countPairs ( arr , N ) ) NEW_LINE DEDENT
Count pairs from an array having GCD equal to the minimum element in the pair | Python3 program for the above approach ; Function to count pairs from an array having GCD equal to minimum element of that pair ; Stores the resultant count ; Stores the frequency of each array element ; Traverse the array arr [ ] ; Iterate over the Map mp ; Stores the array element ; Stores the count of array element x ; If x is 1 ; Increment res by N - 1 ; Increment res by yC2 ; Iterate over the range [ 2 , sqrt ( x ) ] ; If x is divisible by j ; Increment the value of res by mp [ j ] ; If j is not equal to x / j ; Increment res by mp [ x / j ] ; Return the resultant count ; Driver Code
from math import sqrt NEW_LINE def CountPairs ( arr , N ) : NEW_LINE INDENT res = 0 NEW_LINE mp = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] in mp ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT for key , value in mp . items ( ) : NEW_LINE INDENT x = key NEW_LINE y = value NEW_LINE if ( x == 1 ) : NEW_LINE INDENT res += N - 1 NEW_LINE continue NEW_LINE DEDENT res += ( y * ( y - 1 ) ) // 2 NEW_LINE for j in range ( 2 , int ( sqrt ( x ) ) + 1 , 1 ) : NEW_LINE INDENT if ( x % j == 0 ) : NEW_LINE INDENT res += mp [ j ] NEW_LINE if ( j != x // j ) : NEW_LINE INDENT res += mp [ x // j ] NEW_LINE DEDENT DEDENT DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE print ( CountPairs ( arr , N ) ) NEW_LINE DEDENT