codeparrot/xlcost-text-to-code huggingface dataset:
Count of common subarrays in two different permutations of 1 to N | Python3 implementation of above approach ; Initialising Map for Index Mapping ; Mapping elements of A ; Modify elements of B according to Map ; Changing B [ i ] as the index of B [ i ] in A ; Count of common subarrays ; Traversing array B ; While consecutive elements are found , we increment K ; Add number of subarrays with length K to total count ; Driver code
def commonSubarrays ( A , B , N ) : NEW_LINE INDENT Map = [ 0 for i in range ( N + 1 ) ] 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 B [ i ] = Map [ B [ i ] ] NEW_LINE DEDENT count = 0 NEW_LINE i = 0 NEW_LINE while i < N : NEW_LINE INDENT K = 1 NEW_LINE i += 1 NEW_LINE while i < N and B [ i ] == B [ i - 1 ] + 1 : NEW_LINE INDENT i += 1 NEW_LINE K += 1 NEW_LINE DEDENT count = count + ( ( K ) * ( K + 1 ) ) // 2 NEW_LINE DEDENT return count NEW_LINE DEDENT N = 3 NEW_LINE A = [ 1 , 2 , 3 ] NEW_LINE B = [ 2 , 3 , 1 ] NEW_LINE print ( commonSubarrays ( A , B , N ) ) NEW_LINE N = 5 NEW_LINE A = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE B = [ 2 , 3 , 1 , 4 , 5 ] NEW_LINE print ( commonSubarrays ( A , B , N ) ) NEW_LINE
Represent N as sum of K even or K odd numbers with repetitions allowed | Function to find the array with all the even / odd elements ; First let 's check  kth is odd or even ; if last element is also an odd number then we can choose odd elements for our answer ; Add 1 in the array ( k - 1 ) times ; Add last odd element ; If array of even elements would be the answer then k - 1 elements would be 2 ; if last element is also an even number then we can choose even elements for our answer ; Add 2 in the array ( k - 1 ) times ; Add last even element ; Printing the array ; Driver Code
def getArrayOfSizeK ( n , k ) : NEW_LINE INDENT ans = [ ] NEW_LINE odd = n - ( ( k - 1 ) * 1 ) NEW_LINE if ( odd > 0 and odd % 2 != 0 ) : NEW_LINE INDENT for i in range ( k - 1 ) : NEW_LINE INDENT ans . append ( 1 ) NEW_LINE DEDENT ans . append ( odd ) NEW_LINE DEDENT even = n - ( ( k - 1 ) * 2 ) NEW_LINE if ( even > 0 and even % 2 == 0 and len ( ans ) == 0 ) : NEW_LINE INDENT for i in range ( k - 1 ) : NEW_LINE INDENT ans . append ( 2 ) NEW_LINE DEDENT ans . append ( even ) NEW_LINE DEDENT if ( len ( ans ) > 0 ) : NEW_LINE INDENT for i in range ( k ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , k = 10 , 3 NEW_LINE getArrayOfSizeK ( n , k ) NEW_LINE DEDENT
Check whether a given number N is a Nude Number or not | Check if all digits of num divide num ; Array to store all digits of the given number ; If any of the condition is true for any digit then N is not a nude number ; Driver code
def checkDivisbility ( num ) : NEW_LINE INDENT digit = 0 NEW_LINE N = num NEW_LINE while ( num != 0 ) : NEW_LINE INDENT digit = num % 10 NEW_LINE num = num // 10 NEW_LINE if ( digit == 0 or N % digit != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 128 NEW_LINE result = checkDivisbility ( N ) NEW_LINE if ( result ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Hensel 's Lemma | Function to find the modular inverse of a modulo m ; Apply the Euclidean algorithm , to find the modular inverse ; Function to find the derivative of f ( x ) and f '(x) = 3 * (x ^ 2) ; Function to find the image of x in f ( x ) = x ^ 3 - k . ; Function to find the next power of the number ; Next power of prime for which solution is to be found ; Using Hensel 's recursion to  find the solution(next_a) for  next power of prime ; If next_a < 0 return equivalent positive remainder modulo p ; Return the next power of a ; Function to find the solution of the required exponent of prime ; The lemma does not work for derivative of f ( x ) at a1 ; Looping from 1 to power of prime whose solution is to be found ; Final answer after evaluating all the exponents up till the required exponent ; Driver Code ; Function Call
def inv ( a , m ) : NEW_LINE INDENT m0 = m NEW_LINE x0 = 0 NEW_LINE x1 = 1 NEW_LINE if ( m == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( a > 1 ) : NEW_LINE INDENT q = a // m NEW_LINE t = m NEW_LINE m = a % m NEW_LINE a = t NEW_LINE t = x0 NEW_LINE x0 = x1 - q * x0 NEW_LINE x1 = t NEW_LINE DEDENT if ( x1 < 0 ) : NEW_LINE INDENT x1 += m0 NEW_LINE DEDENT return x1 NEW_LINE DEDENT def derivative ( x ) : NEW_LINE INDENT return 3 * x * x NEW_LINE DEDENT def Image ( x , k ) : NEW_LINE INDENT return x * x * x - k NEW_LINE DEDENT def next_power ( a_t , t , a1 , prime , k ) : NEW_LINE INDENT power_p = int ( pow ( prime , t + 1 ) ) NEW_LINE next_a = ( a_t - Image ( a_t , k ) * inv ( derivative ( a1 ) , prime ) ) % power_p NEW_LINE if ( next_a < 0 ) : NEW_LINE INDENT next_a += power_p NEW_LINE return next_a NEW_LINE DEDENT return next_a NEW_LINE DEDENT def powerOfPrime ( prime , power , k , a1 ) : NEW_LINE INDENT if ( derivative ( a1 ) != 0 ) : NEW_LINE INDENT a_t = a1 NEW_LINE for p in range ( 1 , power ) : NEW_LINE INDENT a_t = next_power ( a_t , p , a1 , prime , k ) NEW_LINE DEDENT return a_t NEW_LINE DEDENT return - 1 NEW_LINE DEDENT prime = 7 NEW_LINE a1 = 3 NEW_LINE power = 2 NEW_LINE k = 3 NEW_LINE print ( powerOfPrime ( prime , power , k , a1 ) ) NEW_LINE
Count of numbers with all digits same in a given range | Python3 program to count the total numbers in the range L and R which have all the digit same ; Function that count the total numbersProgram between L and R which have all the digit same ; length of R ; tmp has all digits as 1 ; For each multiple of tmp in range 1 to 9 , check if it present in range [ L , R ] ; Increment the required count ; Driver Code
import math NEW_LINE def count_same_digit ( L , R ) : NEW_LINE INDENT tmp = 0 ; ans = 0 ; NEW_LINE n = int ( math . log10 ( R ) + 1 ) ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT tmp = tmp * 10 + 1 ; NEW_LINE for j in range ( 1 , 9 ) : NEW_LINE INDENT if ( L <= ( tmp * j ) and ( tmp * j ) <= R ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT DEDENT DEDENT return ans ; NEW_LINE DEDENT L = 12 ; R = 68 ; NEW_LINE print ( count_same_digit ( L , R ) ) NEW_LINE
Minimum LCM of all pairs in a given array | Python3 program to find the pair having minimum LCM ; function that return pair having minimum LCM ; find max element in the array as the gcd of two elements from the array can 't greater than max element. ; created a 2D array to store minimum two multiple of any particular i . ; we already found two smallest multiple ; iterating over all gcd ; iterating over its multiple ; if we already found the two smallest multiple of i ; choosing smallest two multiple ; calculating lcm ; return final answer ; Driver code
import sys NEW_LINE def minLCM ( arr , n ) : NEW_LINE INDENT mx = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT mx = max ( mx , arr [ i ] ) NEW_LINE DEDENT mul = [ [ ] for i in range ( mx + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( len ( mul [ arr [ i ] ] ) > 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT mul [ arr [ i ] ] . append ( arr [ i ] ) NEW_LINE DEDENT for i in range ( 1 , mx + 1 ) : NEW_LINE INDENT for j in range ( i + i , mx + 1 , i ) : NEW_LINE INDENT if ( len ( mul [ i ] ) > 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT for k in mul [ j ] : NEW_LINE INDENT if ( len ( mul [ i ] ) > 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT mul [ i ] . append ( k ) NEW_LINE DEDENT DEDENT DEDENT ans = sys . maxsize NEW_LINE for i in range ( 1 , mx + 1 ) : NEW_LINE INDENT if ( len ( mul [ i ] ) <= 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT a , b = mul [ i ] [ 0 ] , mul [ i ] [ 1 ] NEW_LINE lcm = ( a * b ) // i NEW_LINE ans = min ( ans , lcm ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 2 , 4 , 3 , 6 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minLCM ( arr , n ) ) NEW_LINE
Count of triplets of numbers 1 to N such that middle element is always largest | Function to find Number of triplets for given Number N such that middle element is always greater than left and right side element . ; Check if arrangement is possible or Not ; Else return total ways ; Driver code .
def findArrangement ( N ) : NEW_LINE INDENT if ( N < 3 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT return ( ( N ) * ( N - 1 ) * ( N - 2 ) ) // 3 ; NEW_LINE DEDENT N = 10 ; NEW_LINE print ( findArrangement ( N ) ) ; NEW_LINE
Lexicographically smallest array formed by at most one swap for every pair of adjacent indices | Function to find the lexicographically smallest array ; Maximum swaps possible ; Hash to store swaps performed ; Let current element be the minimum possible ; Find actual position of the minimum element ; Update minimum element and its position ; Perform swaps if possible ; Insert current swap in hash ; Print the required array ; Driver code
def findSmallestArray ( A , n ) : NEW_LINE INDENT count = n - 1 NEW_LINE mp = { ' ' } NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( count <= 0 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT mn = A [ i ] NEW_LINE pos = i NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( A [ j ] < mn ) : NEW_LINE INDENT mn = A [ j ] NEW_LINE pos = j NEW_LINE DEDENT DEDENT while ( pos > i and count > 0 and ( ( pos - 1 , pos ) not in mp ) ) : NEW_LINE INDENT mp . add ( ( pos - 1 , pos ) ) NEW_LINE A [ pos ] , A [ pos - 1 ] = A [ pos - 1 ] , A [ pos ] NEW_LINE pos -= 1 NEW_LINE count -= 1 NEW_LINE DEDENT DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( A [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT A = [ 2 , 1 , 4 , 3 , 6 , 5 ] NEW_LINE n = len ( A ) NEW_LINE findSmallestArray ( A , n ) NEW_LINE
Sum of all perfect square divisors of numbers from 1 to N | Python3 program to find the sum of all perfect square divisors of numbers from 1 to N ; Function for finding inverse of a number iteratively Here we will find the inverse of 6 , since it appears as denominator in the formula of sum of squares from 1 to N ; Store the value of the inverse of 6 once as we don 't need to call the function again and again ; Formula for finding the sum of first n squares ; No perfect square exists which is less than 4 ; Starting from 2 , present value of start is denoted here as curStart ; Finding end of the segment for which the contribution will be same ; Using the above mentioned formula to find ans % MOD ; Now for mthe next iteration start will become end + 1 ; Finally return the answer ; Driver Code ; Here we are adding x because we have not counted 1 as perfect squares so if u want to add it you can just add that number to the ans
from math import * NEW_LINE MOD = 1000000007 NEW_LINE def inv ( a ) : NEW_LINE INDENT o = 1 NEW_LINE p = MOD - 2 NEW_LINE while ( p > 0 ) : NEW_LINE INDENT if ( p % 2 == 1 ) : NEW_LINE INDENT o = ( o * a ) % MOD NEW_LINE DEDENT a = ( a * a ) % MOD NEW_LINE p >>= 1 NEW_LINE DEDENT return o NEW_LINE DEDENT inv6 = inv ( 6 ) NEW_LINE def sumOfSquares ( n ) : NEW_LINE INDENT n %= MOD NEW_LINE return ( ( ( n * ( n + 1 ) ) % MOD * ( 2 * n + 1 ) ) % MOD * inv6 ) % MOD NEW_LINE DEDENT def sums ( n ) : NEW_LINE INDENT if ( n < 4 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT curStart = 2 NEW_LINE ans = 0 NEW_LINE sqrtN = int ( sqrt ( n ) ) NEW_LINE while ( curStart <= n // curStart ) : NEW_LINE INDENT V = n // ( curStart * curStart ) NEW_LINE end = int ( sqrt ( n // V ) ) NEW_LINE ans += ( ( n // ( curStart * curStart ) % MOD * ( sumOfSquares ( end ) + MOD - sumOfSquares ( curStart - 1 ) ) ) % MOD ) NEW_LINE if ( ans >= MOD ) : NEW_LINE INDENT ans -= MOD NEW_LINE DEDENT curStart = end + 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Input = [ 5 ] NEW_LINE for x in Input : NEW_LINE INDENT print ( " sum ▁ of ▁ all ▁ perfect ▁ " \ " square ▁ " , end = ' ' ) NEW_LINE print ( " divisors ▁ from ▁ 1 ▁ to " , x , " is : ▁ " , end = ' ' ) NEW_LINE print ( x + sums ( x ) ) NEW_LINE DEDENT DEDENT
Check whether the binary equivalent of a number ends with "001" or not | Function returns true if s1 is suffix of s2 ; Function to check if binary equivalent of a number ends in "001" or not ; To store the binary number ; Count used to store exponent value ; Driver code
def isSuffix ( s1 , s2 ) : NEW_LINE INDENT n1 = len ( s1 ) ; NEW_LINE n2 = len ( s2 ) ; NEW_LINE if ( n1 > n2 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT for i in range ( n1 ) : NEW_LINE INDENT if ( s1 [ n1 - i - 1 ] != s2 [ n2 - i - 1 ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def CheckBinaryEquivalent ( N ) : NEW_LINE INDENT B_Number = 0 ; NEW_LINE cnt = 0 ; NEW_LINE while ( N != 0 ) : NEW_LINE INDENT rem = N % 2 ; NEW_LINE c = 10 ** cnt ; NEW_LINE B_Number += rem * c ; NEW_LINE N //= 2 ; NEW_LINE cnt += 1 ; NEW_LINE DEDENT bin = str ( B_Number ) ; NEW_LINE return isSuffix ( "001" , bin ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 9 ; NEW_LINE if ( CheckBinaryEquivalent ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Check whether the binary equivalent of a number ends with "001" or not | Function to check if binary equivalent of a number ends in "001" or not ; To check if binary equivalent of a number ends in "001" or not ; Driver code
def CheckBinaryEquivalent ( N ) : NEW_LINE INDENT return ( N - 1 ) % 8 == 0 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 9 ; NEW_LINE if ( CheckBinaryEquivalent ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Panarithmic numbers within a given range | Python3 program to print Practical Numbers in given range ; Function to compute divisors of a number ; Vector to store divisors ; 1 will always be a divisor ; Check if i is squareroot of A then only one time insert it in ans ; Function to check that a number can be represented as summ of distinct divisor or not ; The value of subsett [ i ] [ j ] will be True if there is a subsett of sett [ 0. . j - 1 ] with summ equal to i ; If summ is 0 , then answer is True ; If summ is not 0 and sett is empty , then answer is False ; Fill the subsett table in bottom up manner ; Return the possibility of given summ ; Function to check a number is Practical or not ; Vector to store divisors ; If all numbers can be represented as summ of unique divisors ; Function to prPractical Numbers in a range ; Driver code
import math NEW_LINE def get_divisors ( A ) : NEW_LINE INDENT ans = [ ] NEW_LINE ans . append ( 1 ) NEW_LINE for i in range ( 2 , math . floor ( math . sqrt ( A ) ) + 1 ) : NEW_LINE INDENT if ( A % i == 0 ) : NEW_LINE INDENT ans . append ( i ) NEW_LINE if ( ( i * i ) != A ) : NEW_LINE INDENT ans . append ( A // i ) NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT def summ_Possible ( sett , summ ) : NEW_LINE INDENT n = len ( sett ) NEW_LINE subsett = [ [ 0 for i in range ( summ + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT subsett [ i ] [ 0 ] = True NEW_LINE DEDENT for i in range ( 1 , summ + 1 ) : NEW_LINE INDENT subsett [ 0 ] [ i ] = False NEW_LINE DEDENT for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( summ + 1 ) : NEW_LINE INDENT if ( j < sett [ i - 1 ] ) : NEW_LINE INDENT subsett [ i ] [ j ] = subsett [ i - 1 ] [ j ] NEW_LINE DEDENT if ( j >= sett [ i - 1 ] ) : NEW_LINE INDENT subsett [ i ] [ j ] = ( subsett [ i - 1 ] [ j ] or subsett [ i - 1 ] [ j - sett [ i - 1 ] ] ) NEW_LINE DEDENT DEDENT DEDENT return subsett [ n ] [ summ ] NEW_LINE DEDENT def Is_Practical ( A ) : NEW_LINE INDENT divisors = [ ] NEW_LINE divisors = get_divisors ( A ) NEW_LINE for i in range ( 2 , A ) : NEW_LINE INDENT if ( summ_Possible ( divisors , i ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def print_practica_No ( A , B ) : NEW_LINE INDENT for i in range ( A , B + 1 ) : NEW_LINE INDENT if ( Is_Practical ( i ) == True ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT A = 1 NEW_LINE B = 100 NEW_LINE print_practica_No ( A , B ) NEW_LINE
Maximize the division result of Array using given operations | Function to find the max result ; Sort the array in descending order ; Loop to divide in this order arr [ 0 ] / ( arr [ 1 ] / arr [ 2 ] / ... . arr [ n - 2 ] / arr [ n - 1 ] ) ; Return the final result ; Driver code
def maxDivision ( arr , n ) : NEW_LINE INDENT arr . sort ( reverse = True ) NEW_LINE mxdiv = arr [ 1 ] NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT mxdiv = mxdiv / arr [ i ] NEW_LINE DEDENT return arr [ 0 ] / mxdiv NEW_LINE DEDENT arr = [ 100 , 1000 , 10 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxDivision ( arr , n ) ) NEW_LINE
Print all distinct Coprime sets possible from 1 to N | Function to prall co - prime sets ; Check if n is less than 4 then simply prall values till n ; For all the values of n > 3 ; Check if n is even then every set will contain 2 adjacent elements up - to n ; If n is odd then every set will contain 2 adjacent element except the last set which will have last three elements ; Last element for odd case ; Driver code
def coPrimeSet ( n ) : NEW_LINE INDENT firstadj = 0 ; NEW_LINE secadj = 0 ; NEW_LINE if ( n < 4 ) : NEW_LINE INDENT print ( " ( ▁ " ) ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( i + " , ▁ " ) ; NEW_LINE DEDENT print ( " ) " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT for i in range ( 0 , n / 2 ) : NEW_LINE INDENT firstadj = 2 * i + 1 ; NEW_LINE secadj = 2 * i + 2 ; NEW_LINE print ( " ( " , firstadj , " , ▁ " , secadj , " ) " ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for i in range ( 0 , int ( n / 2 ) - 1 ) : NEW_LINE INDENT firstadj = 2 * i + 1 ; NEW_LINE secadj = 2 * i + 2 ; NEW_LINE print ( " ( " , firstadj , " , ▁ " , secadj , " ) " ) ; NEW_LINE DEDENT print ( " ( " , ( n - 2 ) , " , ▁ " , ( n - 1 ) , " , ▁ " , n , " ) " ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 ; NEW_LINE coPrimeSet ( n ) ; NEW_LINE DEDENT
Find the sequence number of a triangular number | Python3 code to print sequence number of a triangular number ; if N is not tringular number
import math NEW_LINE N = 21 NEW_LINE A = math . sqrt ( 2 * N + 0.25 ) - 0.5 NEW_LINE B = int ( A ) NEW_LINE if B != A : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( B ) NEW_LINE DEDENT
Count subarrays with sum equal to its XOR value | Function to count the number of subarrays such that Xor of all the elements of that subarray is equal to sum of the elements ; Maintain two pointers left and right ; Iterating through the array ; Calculate the window where the above condition is satisfied ; Count will be ( right - left ) ; Remove the previous element as it is already included ; Driver code
def operation ( arr , N ) : NEW_LINE INDENT right = 0 ; ans = 0 ; NEW_LINE num = 0 ; NEW_LINE for left in range ( 0 , N ) : NEW_LINE INDENT while ( right < N and num + arr [ right ] == ( num ^ arr [ right ] ) ) : NEW_LINE INDENT num += arr [ right ] ; NEW_LINE right += 1 ; NEW_LINE DEDENT ans += right - left ; NEW_LINE if ( left == right ) : NEW_LINE INDENT right += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT num -= arr [ left ] ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE N = len ( arr ) NEW_LINE print ( operation ( arr , N ) ) ; NEW_LINE
Count of pairs in an Array whose sum is Prime | Function for Sieve Of Eratosthenes ; Function to count total number of pairs of elements whose sum is prime ; Driver code
def sieveOfEratosthenes ( N ) : NEW_LINE INDENT isPrime = [ True for i in range ( N + 1 ) ] NEW_LINE isPrime [ 0 ] = False NEW_LINE isPrime [ 1 ] = False NEW_LINE i = 2 NEW_LINE while ( ( i * i ) <= N ) : NEW_LINE INDENT if ( isPrime [ i ] ) : NEW_LINE INDENT j = 2 NEW_LINE while ( i * j <= N ) : NEW_LINE INDENT isPrime [ i * j ] = False NEW_LINE j += 1 NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT return isPrime NEW_LINE DEDENT def numPairsWithPrimeSum ( arr , n ) : NEW_LINE INDENT N = 2 * 1000000 NEW_LINE isPrime = sieveOfEratosthenes ( N ) NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT sum = arr [ i ] + arr [ j ] NEW_LINE if ( isPrime [ sum ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( numPairsWithPrimeSum ( arr , n ) ) NEW_LINE DEDENT
Count of numbers in Array ending with digits of number N | Array to keep the track of digits occurred Initially all are 0 ( false ) ; Function to initialize true if the digit is present ; Variable to store the last digit ; Loop to iterate through every digit of the number N ; Updating the array according to the presence of the digit in n at the array index ; Function to check if the numbers in the array end with the digits of the number N ; Variable to store the count ; Variable to store the last digit ; Checking the presence of the last digit in N ; Function to find the required count ; Driver code ; Preprocessing
digit = [ 0 ] * 10 NEW_LINE def digitsPresent ( n ) : NEW_LINE INDENT lastDigit = 0 ; NEW_LINE while ( n != 0 ) : NEW_LINE INDENT lastDigit = n % 10 ; NEW_LINE digit [ int ( lastDigit ) ] = 1 ; NEW_LINE n /= 10 ; NEW_LINE DEDENT DEDENT def checkLastDigit ( num ) : NEW_LINE INDENT count = 0 ; NEW_LINE lastDigit = 0 ; NEW_LINE lastDigit = num % 10 ; NEW_LINE if ( digit [ int ( lastDigit ) ] == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT return count ; NEW_LINE DEDENT def findCount ( N , K , arr ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( K ) : NEW_LINE INDENT if checkLastDigit ( arr [ i ] ) == 1 : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT N = 1731 ; NEW_LINE digitsPresent ( N ) ; NEW_LINE K = 5 ; NEW_LINE arr = [ 57 , 6786 , 1111 , 3 , 9812 ] ; NEW_LINE findCount ( N , K , arr ) ; NEW_LINE
Check if count of even divisors of N is equal to count of odd divisors | Function to check if count of even and odd divisors are equal ; If ( n - 2 ) % 4 is an integer , then return true else return false ; Given Number ; Function Call
def divisorsSame ( n ) : NEW_LINE INDENT return ( n - 2 ) % 4 == 0 ; NEW_LINE DEDENT N = 6 ; NEW_LINE if ( divisorsSame ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Nth term of a sequence formed by sum of current term with product of its largest and smallest digit | Function to find integer ; Because 1 st integer is K itself ; Initialize min_d and max_d ; Updating min_d and max_d ; Break if min digit is 0 ; Driver code
def find ( K , N ) : NEW_LINE INDENT N = N - 1 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT curr_term = K NEW_LINE min_d = 9 NEW_LINE max_d = 0 NEW_LINE while curr_term > 0 : NEW_LINE INDENT r = int ( curr_term % 10 ) NEW_LINE min_d = min ( min_d , r ) NEW_LINE max_d = max ( max_d , r ) NEW_LINE curr_term = int ( curr_term / 10 ) NEW_LINE DEDENT if min_d == 0 : NEW_LINE break NEW_LINE K = K + min_d * max_d NEW_LINE return K NEW_LINE DEDENT DEDENT K = 487 NEW_LINE N = 2 NEW_LINE print ( find ( K , N ) ) NEW_LINE