codeparrot/xlcost-text-to-code huggingface dataset:
Greatest number that can be formed from a pair in a given Array | Python3 implementation to find the greatest number from the given pairs of the array ; Function to find the greatest number formed from the pairs ; first append Y at the end of X ; then append X at the end of Y ; Now see which of the two formed numbers is greater than other ; Function to find pairs from array ; Iterate through all pairs ; Driver code
import sys ; NEW_LINE def getNumber ( a , b ) : NEW_LINE INDENT X = str ( a ) ; NEW_LINE Y = str ( b ) ; NEW_LINE XY = X + Y ; NEW_LINE YX = Y + X ; NEW_LINE if ( XY > YX ) : NEW_LINE INDENT return XY ; NEW_LINE DEDENT else : NEW_LINE INDENT return YX ; NEW_LINE DEDENT DEDENT def printMaxPair ( arr , n ) : NEW_LINE INDENT largest = - sys . maxsize - 1 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT number = int ( getNumber ( arr [ i ] , arr [ j ] ) ) ; NEW_LINE largest = max ( largest , number ) ; NEW_LINE DEDENT DEDENT print ( largest ) ; NEW_LINE DEDENT a = [ 23 , 14 , 16 , 25 , 3 , 9 ] ; NEW_LINE n = len ( a ) ; NEW_LINE printMaxPair ( a , n ) ; NEW_LINE
Check whether two numbers are in golden ratio | Function to check that two numbers are in golden ratio ; Swapping the numbers such that A contains the maximum number between these numbers ; First Ratio ; Second Ratio ; Condition to check that two numbers are in golden ratio ; Driver Code ; Function Call
def checkGoldenRatio ( a , b ) : NEW_LINE INDENT a , b = max ( a , b ) , min ( a , b ) NEW_LINE ratio1 = round ( a / b , 3 ) NEW_LINE ratio2 = round ( ( a + b ) / a , 3 ) NEW_LINE if ratio1 == ratio2 and ratio1 == 1.618 : NEW_LINE INDENT print ( " Yes " ) NEW_LINE return True NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE return False NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 0.618 NEW_LINE b = 1 NEW_LINE checkGoldenRatio ( a , b ) NEW_LINE DEDENT
Find the minimum number to be added to N to make it a power of K | Python3 program to find the minimum number to be added to N to make it a power of K ; Function to return the minimum number to be added to N to make it a power of K . ; Computing the difference between then next greater power of K and N ; Driver code
import math NEW_LINE def minNum ( n , k ) : NEW_LINE INDENT x = int ( ( math . log ( n ) // math . log ( k ) ) ) + 1 NEW_LINE mn = pow ( k , x ) - n NEW_LINE return mn NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 20 NEW_LINE k = 5 NEW_LINE print ( minNum ( n , k ) ) NEW_LINE DEDENT
Previous perfect square and cube number smaller than number N | Python3 implementation to find the previous perfect square and cube smaller than the given number ; Function to find the previous perfect square of the number N ; If N is already a perfect square decrease prevN by 1. ; Function to find the previous perfect cube ; If N is already a perfect cube decrease prevN by 1. ; Driver Code
import math NEW_LINE import numpy as np NEW_LINE def previousPerfectSquare ( N ) : NEW_LINE INDENT prevN = math . floor ( math . sqrt ( N ) ) ; NEW_LINE if ( prevN * prevN == N ) : NEW_LINE INDENT prevN -= 1 ; NEW_LINE DEDENT return prevN * prevN ; NEW_LINE DEDENT def previousPerfectCube ( N ) : NEW_LINE INDENT prevN = math . floor ( np . cbrt ( N ) ) ; NEW_LINE if ( prevN * prevN * prevN == N ) : NEW_LINE INDENT prevN -= 1 ; NEW_LINE DEDENT return prevN * prevN * prevN ; NEW_LINE DEDENT n = 30 ; NEW_LINE print ( previousPerfectSquare ( n ) ) ; NEW_LINE print ( previousPerfectCube ( n ) ) ; NEW_LINE
Find the count of even odd pairs in a given Array | Function to count the pairs in array of the form ( even , odd ) ; Variable to store count of such pairs ; Iterate through all pairs ; Increment count if condition is satisfied ; Driver code
def findCount ( arr , n ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( ( arr [ i ] % 2 == 0 ) and ( arr [ j ] % 2 == 1 ) ) : NEW_LINE INDENT res = res + 1 NEW_LINE DEDENT DEDENT DEDENT return res NEW_LINE DEDENT a = [ 5 , 4 , 1 , 2 , 3 ] NEW_LINE n = len ( a ) NEW_LINE print ( findCount ( a , n ) ) NEW_LINE
Find the count of even odd pairs in a given Array | Function to count the pairs in array of the form ( even , odd ) ; Check if number is even or not ; Driver code
def findCount ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE ans = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans = ans + count NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT a = [ 5 , 4 , 1 , 2 , 3 ] NEW_LINE n = len ( a ) NEW_LINE print ( findCount ( a , n ) ) NEW_LINE
Product of all non repeating Subarrays of an Array | Function to find the product of all non - repeating Subarrays of an Array ; Finding the occurrence of every element ; Iterating through the array and finding the product ; We are taking the power of each element in array with the occurrence and then taking product of those . ; Driver code
def product ( arr ) : NEW_LINE INDENT occurrence = pow ( 2 , len ( arr ) - 1 ) ; NEW_LINE product = 1 ; NEW_LINE for i in range ( 0 , len ( arr ) ) : NEW_LINE INDENT product *= pow ( arr [ i ] , occurrence ) ; NEW_LINE DEDENT return product ; NEW_LINE DEDENT arr = [ 10 , 3 , 7 ] ; NEW_LINE print ( product ( arr ) ) ; NEW_LINE
Sum of all N | Python 3 program for the above approach ; Function to find a ^ b efficiently ; Base Case ; To store the value of a ^ b ; Multiply temp by a until b is not 0 ; Return the final ans a ^ b ; Function to find sum of all N - digit palindromic number divisible by 9 ; Base Case ; If N is even , decrease ways by 1 ; Find the total number of ways ; Iterate over [ 1 , N ] and find the sum at each index ; Print the final Sum ; Driver Code
MOD = 1000000007 NEW_LINE def power ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT temp = power ( a , b // 2 ) NEW_LINE temp = ( temp * temp ) % MOD NEW_LINE if ( b % 2 != 0 ) : NEW_LINE INDENT temp = ( temp * a ) % MOD NEW_LINE DEDENT return temp NEW_LINE DEDENT def palindromicSum ( N ) : NEW_LINE INDENT sum = 0 NEW_LINE if ( N == 1 ) : NEW_LINE INDENT print ( "9" ) NEW_LINE return NEW_LINE DEDENT if ( N == 2 ) : NEW_LINE INDENT print ( "99" ) NEW_LINE return NEW_LINE DEDENT ways = N // 2 NEW_LINE if ( N % 2 == 0 ) : NEW_LINE INDENT ways -= 1 NEW_LINE DEDENT res = power ( 9 , ways - 1 ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum = sum * 10 + 45 * res NEW_LINE sum %= MOD NEW_LINE DEDENT print ( sum ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE palindromicSum ( N ) NEW_LINE DEDENT
Maximize array sum by concatenating corresponding elements of given two arrays | Function to join the two numbers ; Loop to reverse the digits of the one number ; Loop to join two numbers ; Function to find the maximum array sum ; Loop to iterate over the two elements of the array ; Find the array sum ; Return the array sum ; Driver Code
def joinNumbers ( numA , numB ) : NEW_LINE INDENT revB = 0 NEW_LINE while ( numB > 0 ) : NEW_LINE INDENT revB = revB * 10 + ( numB % 10 ) NEW_LINE numB = numB // 10 NEW_LINE DEDENT while ( revB > 0 ) : NEW_LINE INDENT numA = numA * 10 + ( revB % 10 ) NEW_LINE revB = revB // 10 NEW_LINE DEDENT return numA NEW_LINE DEDENT def findMaxSum ( A , B , n ) : NEW_LINE INDENT maxArr = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT X = joinNumbers ( A [ i ] , B [ i ] ) NEW_LINE Y = joinNumbers ( B [ i ] , A [ i ] ) NEW_LINE mx = max ( X , Y ) NEW_LINE maxArr [ i ] = mx NEW_LINE DEDENT maxAns = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT maxAns += maxArr [ i ] NEW_LINE DEDENT return maxAns NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE A = [ 11 , 23 , 38 , 43 , 59 ] NEW_LINE B = [ 36 , 24 , 17 , 40 , 56 ] NEW_LINE print ( findMaxSum ( A , B , N ) ) NEW_LINE DEDENT
Split N natural numbers into two sets having GCD of their sums greater than 1 | Function to create and print the two sets ; For n <= 2 such sets can never be formed ; Check if N is even or odd and store the element which divides the sum of N natural numbers accordingly ; First set ; Print elements of second set ; Driver code
def createSets ( n ) : NEW_LINE INDENT if ( n <= 2 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE return ; NEW_LINE DEDENT else : NEW_LINE INDENT x = ( n // 2 ) if ( n % 2 == 0 ) else ( ( n + 1 ) // 2 ) ; NEW_LINE print ( x , " ▁ " ) ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( i == x ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT print ( i , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT return ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 7 ; NEW_LINE createSets ( N ) ; NEW_LINE DEDENT
Count the factors of K present in the given Array | Function to find the count of factors of K present in array ; Loop to consider every element of array ; Driver Code ; Function Call
def calcCount ( arr , n , k ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( k % arr [ i ] == 0 ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 1 , 2 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE k = 6 NEW_LINE print ( calcCount ( arr , n , k ) ) NEW_LINE
Multiply N complex numbers given as strings | Function which returns the in digit format ; a : real b : imaginary ; sa : sign of a sb : sign of b ; Extract the real number ; Extract the imaginary part ; If size == 1 means we reached at result ; Extract the first two elements ; Remove them ; Calculate and store the real part ; Calculate and store the imaginary part ; Append the real part ; Append the imaginary part ; Insert into vector ; Driver code
def findnum ( s1 ) : NEW_LINE INDENT v = [ ] NEW_LINE a = 0 NEW_LINE b = 0 NEW_LINE sa = 0 NEW_LINE sb = 0 NEW_LINE i = 0 NEW_LINE if ( s1 [ 0 ] == ' - ' ) : NEW_LINE INDENT sa = 1 NEW_LINE i = 1 NEW_LINE DEDENT while ( s1 [ i ] . isdigit ( ) ) : NEW_LINE INDENT a = a * 10 + ( int ( s1 [ i ] ) ) NEW_LINE i += 1 NEW_LINE DEDENT if ( s1 [ i ] == ' + ' ) : NEW_LINE INDENT sb = 0 NEW_LINE i += 1 NEW_LINE DEDENT if ( s1 [ i ] == ' - ' ) : NEW_LINE INDENT sb = 1 NEW_LINE i += 1 NEW_LINE DEDENT while ( i < len ( s1 ) and s1 [ i ] . isdigit ( ) ) : NEW_LINE INDENT b = b * 10 + ( int ( s1 [ i ] ) ) NEW_LINE i += 1 NEW_LINE DEDENT if ( sa ) : NEW_LINE INDENT a *= - 1 NEW_LINE DEDENT if ( sb ) : NEW_LINE INDENT b *= - 1 NEW_LINE DEDENT v . append ( a ) NEW_LINE v . append ( b ) NEW_LINE return v NEW_LINE DEDENT def complexNumberMultiply ( v ) : NEW_LINE INDENT while ( len ( v ) != 1 ) : NEW_LINE INDENT v1 = findnum ( v [ 0 ] ) NEW_LINE v2 = findnum ( v [ 1 ] ) NEW_LINE del v [ 0 ] NEW_LINE del v [ 0 ] NEW_LINE r = ( v1 [ 0 ] * v2 [ 0 ] - v1 [ 1 ] * v2 [ 1 ] ) NEW_LINE img = v1 [ 0 ] * v2 [ 1 ] + v1 [ 1 ] * v2 [ 0 ] NEW_LINE res = " " NEW_LINE res += str ( r ) NEW_LINE res += ' + ' NEW_LINE res += str ( img ) + ' i ' NEW_LINE v . insert ( 0 , res ) NEW_LINE DEDENT return v [ 0 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE v = [ "3 + 1i " , "2 + 1i " , " - 5 + - 7i " ] NEW_LINE print ( complexNumberMultiply ( v ) ) NEW_LINE DEDENT
Length of largest subarray whose all elements are Perfect Number | Function that returns true if n is perfect ; To store sum of divisors ; Find all divisors and add them ; check if the sum of divisors is equal to n , then n is a perfect number ; Function to return the length of the largest sub - array of an array every element of whose is a perfect number ; check if arr [ i ] is a perfect number ; Driver code
def isPerfect ( n ) : NEW_LINE INDENT sum = 1 NEW_LINE i = 2 NEW_LINE while i * i <= n : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT sum = sum + i + n / i NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return ( True if sum == n and n != 1 else False ) NEW_LINE DEDENT def contiguousPerfectNumber ( arr , n ) : NEW_LINE INDENT current_length = 0 NEW_LINE max_length = 0 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( isPerfect ( arr [ i ] ) ) : NEW_LINE INDENT current_length += 1 NEW_LINE DEDENT else : NEW_LINE INDENT current_length = 0 NEW_LINE DEDENT max_length = max ( max_length , current_length ) NEW_LINE DEDENT return max_length NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 7 , 36 , 4 , 6 , 28 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( contiguousPerfectNumber ( arr , n ) ) NEW_LINE DEDENT
Length of largest subarray whose all elements Powerful number | Python 3 program to find the length of the largest sub - array of an array every element of whose is a powerful number ; function to check if the number is powerful ; First divide the number repeatedly by 2 ; If only 2 ^ 1 divides n ( not higher powers ) , then return false ; If n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n ( not higher powers ) , then return false ; n must be 1 now if it is not a prime numenr . Since prime numbers are not powerful , we return false if n is not 1. ; Function to return the length of the largest sub - array of an array every element of whose is a powerful number ; If arr [ i ] is a Powerful number ; Driver code
import math NEW_LINE def isPowerful ( n ) : NEW_LINE INDENT while ( n % 2 == 0 ) : NEW_LINE INDENT power = 0 NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT n = n // 2 NEW_LINE power = power + 1 NEW_LINE DEDENT if ( power == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT for factor in range ( 3 , int ( math . sqrt ( n ) ) + 1 , 2 ) : NEW_LINE INDENT power = 0 NEW_LINE while ( n % factor == 0 ) : NEW_LINE INDENT n = n // factor NEW_LINE power = power + 1 NEW_LINE DEDENT if ( power == 1 ) : NEW_LINE INDENT return false NEW_LINE DEDENT DEDENT return ( n == 1 ) NEW_LINE DEDENT def contiguousPowerfulNumber ( arr , n ) : NEW_LINE INDENT current_length = 0 NEW_LINE max_length = 0 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( isPowerful ( arr [ i ] ) ) : NEW_LINE INDENT current_length += 1 NEW_LINE DEDENT else : NEW_LINE INDENT current_length = 0 NEW_LINE DEDENT max_length = max ( max_length , current_length ) NEW_LINE DEDENT return max_length NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 7 , 36 , 4 , 6 , 28 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( contiguousPowerfulNumber ( arr , n ) ) NEW_LINE DEDENT
Maximize sum of K corner elements in Array | Function to return maximum sum ; Base case ; Pick the start index ; Pick the end index ; Recursive function call ; Return the final answer ; Function to find the maximized sum ; Driver code
def maxSum ( arr , K , start , end , max_sum ) : NEW_LINE INDENT if ( K == 0 ) : NEW_LINE INDENT return max_sum NEW_LINE DEDENT max_sum_start = max_sum + arr [ start ] NEW_LINE max_sum_end = max_sum + arr [ end ] NEW_LINE ans = max ( maxSum ( arr , K - 1 , start + 1 , end , max_sum_start ) , maxSum ( arr , K - 1 , start , end - 1 , max_sum_end ) ) NEW_LINE return ans NEW_LINE DEDENT def maximizeSum ( arr , K , n ) : NEW_LINE INDENT max_sum = 0 NEW_LINE start = 0 NEW_LINE end = n - 1 NEW_LINE print ( maxSum ( arr , K , start , end , max_sum ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 8 , 4 , 4 , 8 , 12 , 3 , 2 , 9 ] NEW_LINE K = 3 NEW_LINE n = len ( arr ) NEW_LINE maximizeSum ( arr , K , n ) NEW_LINE DEDENT
Maximize sum of K corner elements in Array | Function to return maximum sum ; Initialize variables ; Iterate over first K elements of array and update the value for curr_points ; Update value for max_points ; j points to the end of the array ; Return the final result ; Driver code
def maxPointCount ( arr , K , size ) : NEW_LINE INDENT curr_points = 0 NEW_LINE max_points = 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT curr_points += arr [ i ] NEW_LINE DEDENT max_points = curr_points NEW_LINE j = size - 1 NEW_LINE for i in range ( K - 1 , - 1 , - 1 ) : NEW_LINE INDENT curr_points = ( curr_points + arr [ j ] - arr [ i ] ) NEW_LINE max_points = max ( curr_points , max_points ) NEW_LINE j -= 1 NEW_LINE DEDENT return max_points NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 8 , 4 , 4 , 8 , 12 , 3 , 2 , 9 ] NEW_LINE K = 3 NEW_LINE n = len ( arr ) NEW_LINE print ( maxPointCount ( arr , K , n ) ) NEW_LINE DEDENT
Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9 | Function for finding count of N digits palindrome which are divisible by 9 ; If N is odd ; If N is even ; Function for finding sum of N digits palindrome which are divisible by 9 ; Count the possible number of palindrome ; Driver Code
def countPalindrome ( n ) : NEW_LINE INDENT count = 0 NEW_LINE if ( n % 2 == 1 ) : NEW_LINE INDENT count = pow ( 9 , ( n - 1 ) // 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT count = pow ( 9 , ( n - 2 ) // 2 ) NEW_LINE DEDENT return count NEW_LINE DEDENT def sumPalindrome ( n ) : NEW_LINE INDENT count = countPalindrome ( n ) NEW_LINE res = 0 NEW_LINE if ( n == 1 ) : NEW_LINE INDENT return 9 NEW_LINE DEDENT if ( n == 2 ) : NEW_LINE INDENT return 99 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT res = res * 10 + count * 5 NEW_LINE DEDENT return res NEW_LINE DEDENT n = 3 NEW_LINE print ( sumPalindrome ( n ) ) NEW_LINE
Find the total count of numbers up to N digits in a given base B | Python3 implementation to find the count of natural numbers up to N digits ; Function to return the count of natural numbers upto N digits ; Loop to iterate from 1 to N and calculating number of natural numbers for every ' i ' th digit . ; Driver Code
from math import pow NEW_LINE def count ( N , B ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT sum += ( B - 1 ) * pow ( B , i - 1 ) NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE B = 10 NEW_LINE print ( int ( count ( N , B ) ) ) NEW_LINE DEDENT
Find smallest perfect square number A such that N + A is also a perfect square number | Python3 code to find out the smallest perfect square X which when added to N yields another perfect square number . ; X is the smallest perfect square number ; Loop from 1 to square root of N ; Condition to check whether i is factor of N or not ; Condition to check whether factors satisfies the equation or not ; Stores minimum value ; Return if X * X if X is not equal to 1e9 else return - 1 ; Driver code
import math NEW_LINE def SmallestPerfectSquare ( N ) : NEW_LINE INDENT X = 1e9 NEW_LINE for i in range ( 1 , int ( math . sqrt ( N ) ) + 1 ) : NEW_LINE INDENT if N % i == 0 : NEW_LINE INDENT a = i NEW_LINE b = N // i NEW_LINE if b - a != 0 and ( b - a ) % 2 == 0 : NEW_LINE INDENT X = min ( X , ( b - a ) // 2 ) NEW_LINE DEDENT DEDENT DEDENT return ( X * X if X != 1e9 else - 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE print ( SmallestPerfectSquare ( N ) ) NEW_LINE DEDENT
Minimum Cost Path to visit all nodes situated at the Circumference of Circular Road | Function to find the minimum cost ; Sort the given array ; Initialise a new array of double size ; Fill the array elements ; Find the minimum path cost ; Return the final result ; Driver code
def minCost ( arr , n , circumference ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE arr2 = [ 0 ] * ( 2 * n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr2 [ i ] = arr [ i ] NEW_LINE arr2 [ i + n ] = arr [ i ] + circumference NEW_LINE DEDENT res = 9999999999999999999 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT res = min ( res , arr2 [ i + ( n - 1 ) ] - arr2 [ i ] ) ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT arr = [ 19 , 3 , 6 ] ; NEW_LINE n = len ( arr ) NEW_LINE circumference = 20 ; NEW_LINE print ( minCost ( arr , n , circumference ) ) NEW_LINE
Unique element in an array where all elements occur K times except one | Set 2 | Function to find single occurrence element ; By shifting 1 to left ith time and taking and with 1 will give us that ith bit of a [ j ] is 1 or 0 ; Taking modulo of p with k ; Generate result ; Loop for negative numbers ; Check if the calculated value res is present in array , then mark c = 1 and if c = 1 return res else res must be - ve ; Driver code ; Function call
def findunique ( a , k ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( 32 ) : NEW_LINE INDENT p = 0 NEW_LINE for j in range ( len ( a ) ) : NEW_LINE INDENT if ( abs ( a [ j ] ) & ( 1 << i ) ) != 0 : NEW_LINE INDENT p += 1 NEW_LINE DEDENT DEDENT p %= k NEW_LINE res += pow ( 2 , i ) * p NEW_LINE DEDENT c = 0 NEW_LINE for x in a : NEW_LINE INDENT if ( x == res ) : NEW_LINE INDENT c = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if c == 1 : NEW_LINE INDENT return res NEW_LINE DEDENT else : NEW_LINE INDENT return - res NEW_LINE DEDENT DEDENT a = [ 12 , 12 , 2 , 2 , 3 ] NEW_LINE k = 2 NEW_LINE print ( findunique ( a , k ) ) NEW_LINE
Find minimum GCD of all pairs in an array | Python3 program to find the minimum GCD of any pair in the array ; Function returns the Minimum GCD of any pair ; Finding GCD of all the elements in the array . ; Driver code
from math import gcd NEW_LINE def MinimumGCD ( arr , n ) : NEW_LINE INDENT g = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT g = gcd ( g , arr [ i ] ) NEW_LINE DEDENT return g NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 6 , 8 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE print ( MinimumGCD ( arr , N ) ) NEW_LINE DEDENT
Minimum steps to reach the Nth stair in jumps of perfect power of 2 | Function to count the number of jumps required to reach Nth stairs . ; Till N becomes 0 ; Removes the set bits from the right to left ; Driver Code ; Number of stairs ; Function Call
def stepRequired ( N ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE while ( N > 0 ) : NEW_LINE INDENT N = N & ( N - 1 ) ; NEW_LINE cnt += 1 ; NEW_LINE DEDENT return cnt ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 23 ; NEW_LINE print ( stepRequired ( N ) ) ; NEW_LINE DEDENT
Count of total subarrays whose sum is a Fibonacci Numbers | Python3 program for the above approach ; Function to check whether a number is perfect square or not ; Function to check whether a number is fibonacci number or not ; If 5 * n * n + 4 or 5 * n * n - 5 is a perfect square , then the number is Fibonacci ; Function to count the subarray with sum fibonacci number ; Traverse the array arr [ ] to find the sum of each subarray ; To store the sum ; Check whether sum of subarray between [ i , j ] is fibonacci or not ; Driver Code ; Function Call
import math NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT s = int ( math . sqrt ( x ) ) NEW_LINE if s * s == x : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def isFibonacci ( n ) : NEW_LINE INDENT return ( isPerfectSquare ( 5 * n * n + 4 ) or isPerfectSquare ( 5 * n * n - 4 ) ) NEW_LINE DEDENT def fibonacciSubarrays ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT sum += arr [ j ] NEW_LINE if ( isFibonacci ( sum ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT print ( count ) NEW_LINE DEDENT arr = [ 6 , 7 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE fibonacciSubarrays ( arr , n ) NEW_LINE