codeparrot/xlcost-text-to-code huggingface dataset:
Split array into two subsequences having minimum count of pairs with sum equal to X | Function to split the array into two subsequences ; Stores the two subsequences ; Flag to set / reset to split arrays elements alternately into two arrays ; Traverse the given array ; If 2 * arr [ i ] is less than X ; Push element into the first array ; If 2 * arr [ i ] is greater than X ; Push element into the second array ; If 2 * arr [ i ] is equal to X ; Alternatively place the elements into the two arrays ; Print both the arrays ; Driver Code ; Size of the array ; Function Call
def solve ( arr , N , X ) : NEW_LINE INDENT A = [ ] NEW_LINE B = [ ] NEW_LINE c = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( ( 2 * arr [ i ] ) < X ) : NEW_LINE INDENT A . append ( arr [ i ] ) NEW_LINE DEDENT elif ( ( 2 * arr [ i ] ) > X ) : NEW_LINE INDENT B . append ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( c % 2 == 0 ) : NEW_LINE INDENT A . append ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT B . append ( arr [ i ] ) NEW_LINE DEDENT c += 1 NEW_LINE DEDENT DEDENT print ( " The ▁ First ▁ Array ▁ is ▁ - ▁ " , end = " ▁ " ) NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT print ( A [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE print ( " The ▁ Second ▁ Array ▁ is ▁ - ▁ " , end = " ▁ " ) NEW_LINE for i in range ( len ( B ) ) : NEW_LINE INDENT print ( B [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 5 , 4 , 3 , 6 , 2 , 4 , 3 ] NEW_LINE X = 7 NEW_LINE N = len ( arr ) NEW_LINE solve ( arr , N , X ) NEW_LINE DEDENT
Split array into minimum number of subsets having maximum pair sum at most K | Function to get the minimum count of subsets that satisfy the given condition ; Store the minimum count of subsets that satisfy the given condition ; Stores start index of the sorted array . ; Stores end index of the sorted array ; Sort the given array ; Traverse the array ; If only two elements of sorted array left ; If only one elements left in the array ; Driver Code
def cntMinSub ( arr , N , K ) : NEW_LINE INDENT res = 0 NEW_LINE start = 0 NEW_LINE end = N - 1 NEW_LINE arr = sorted ( arr ) NEW_LINE while ( end - start > 1 ) : NEW_LINE INDENT if ( arr [ start ] + arr [ end ] <= K ) : NEW_LINE INDENT start += 1 NEW_LINE DEDENT else : NEW_LINE INDENT res += 1 NEW_LINE end -= 1 NEW_LINE DEDENT DEDENT if ( end - start == 1 ) : NEW_LINE INDENT if ( arr [ start ] + arr [ end ] <= K ) : NEW_LINE INDENT res += 1 NEW_LINE start += 1 NEW_LINE end -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT res += 1 NEW_LINE end -= 1 NEW_LINE DEDENT DEDENT if ( start == end ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 6 , 8 , 10 , 20 , 25 ] NEW_LINE N = len ( arr ) NEW_LINE K = 26 NEW_LINE print ( cntMinSub ( arr , N , K ) ) NEW_LINE DEDENT
Count possible values of K such that X | Function to count integers K satisfying given equation ; Calculate the absoluter difference between a and b ; Iterate till sqrt of the difference ; Return the count ; Driver Code
def condition ( a , b ) : NEW_LINE INDENT d = abs ( a - b ) NEW_LINE count = 0 NEW_LINE for i in range ( 1 , d + 1 ) : NEW_LINE INDENT if i * i > d : NEW_LINE INDENT break NEW_LINE DEDENT if ( d % i == 0 ) : NEW_LINE INDENT if ( d // i == i ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count += 2 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 2 NEW_LINE y = 6 NEW_LINE print ( condition ( x , y ) ) NEW_LINE DEDENT
Count pairs in an array whose product is composite number | Function to check if a number is prime or not ; Check if N is multiple of i or not . ; If N is multiple of i . ; Function to get the count of pairs whose product is a composite number . ; Stores the count of pairs whose product is a composite number ; Generate all possible pairs ; Stores the product of element of current pair ; If prod is a composite number ; Driver Code
def isComposite ( N ) : NEW_LINE INDENT for i in range ( 2 , N + 1 ) : NEW_LINE INDENT if i * i > N : NEW_LINE INDENT break NEW_LINE DEDENT if ( N % i == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def compositePair ( arr , N ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT prod = arr [ i ] * arr [ j ] NEW_LINE if ( isComposite ( prod ) ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 2 , 8 ] NEW_LINE N = len ( arr ) NEW_LINE print ( compositePair ( arr , N ) ) NEW_LINE DEDENT
Count N | Python3 program for the above approach ; Function for calculate ( x ^ y ) % mod in O ( log y ) ; Base Condition ; Transition state of power Function ; Function for counting total numbers that can be formed such that digits X , Y are present in each number ; Calculate the given expression ; Return the final answer ; Driver Code
mod = 1e9 + 7 NEW_LINE def power ( x , y ) : NEW_LINE INDENT if ( y == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT p = power ( x , y // 2 ) % mod NEW_LINE p = ( p * p ) % mod NEW_LINE if ( y & 1 ) : NEW_LINE INDENT p = ( x * p ) % mod NEW_LINE DEDENT return p NEW_LINE DEDENT def TotalNumber ( N ) : NEW_LINE INDENT ans = ( power ( 10 , N ) - 2 * power ( 9 , N ) + power ( 8 , N ) + 2 * mod ) % mod NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 10 NEW_LINE X = 3 NEW_LINE Y = 4 NEW_LINE print ( TotalNumber ( N ) ) NEW_LINE DEDENT
Array obtained by repeatedly reversing array after every insertion from given array | Python3 program of the above approach ; Function to generate the array by inserting array elements one by one followed by reversing the array ; Doubly ended Queue ; Iterate over the array ; Push array elements alternately to the front and back ; If size of list is odd ; Reverse the list ; Print the elements of the array ; Driver Code
from collections import deque NEW_LINE def generateArray ( arr , n ) : NEW_LINE INDENT ans = deque ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i & 1 != 0 ) : NEW_LINE INDENT ans . appendleft ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT ans . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT if ( n & 1 != 0 ) : NEW_LINE INDENT ans . reverse ( ) NEW_LINE DEDENT for x in ans : NEW_LINE INDENT print ( x , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT n = 4 NEW_LINE arr = [ 1 , 2 , 3 , 4 ] NEW_LINE generateArray ( arr , n ) NEW_LINE
Find the greater number closest to N having at most one non | Function to get closest greater number with at most non zero digit ; Stores the closest greater number with at most one non - zero digit ; Stores length of str ; Append 10 to the end of resultant String ; Append n - 1 times '0' to the end of resultant String ; Driver Code
def closestgtNum ( str ) : NEW_LINE INDENT res = " " ; NEW_LINE n = len ( str ) ; NEW_LINE if ( str [ 0 ] < '9' ) : NEW_LINE INDENT res += ( chr ) ( ord ( str [ 0 ] ) + 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT res += ( chr ) ( '1' ) ; NEW_LINE res += ( chr ) ( '0' ) ; NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT res += ( '0' ) ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "120" ; NEW_LINE print ( closestgtNum ( str ) ) ; NEW_LINE DEDENT
Count maximum non | Function to count maximum number of non - overlapping subarrays with sum equals to the target ; Stores the final count ; Next subarray should start from index >= availIdx ; Tracks the prefix sum ; Map to store the prefix sum for respective indices ; Check if cur_sum - target is present in the array or not ; Update the index of current prefix sum ; Return the count of subarrays ; Driver Code ; Given array arr [ ] ; Given sum target ; Function call
def maximumSubarrays ( arr , N , target ) : NEW_LINE INDENT ans = 0 NEW_LINE availIdx = - 1 NEW_LINE cur_sum = 0 NEW_LINE mp = { } NEW_LINE mp [ 0 ] = - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT cur_sum += arr [ i ] NEW_LINE if ( ( cur_sum - target ) in mp and mp [ cur_sum - target ] >= availIdx ) : NEW_LINE INDENT ans += 1 NEW_LINE availIdx = i NEW_LINE DEDENT mp [ cur_sum ] = i NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , - 1 , 4 , 3 , 6 , 4 , 5 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE target = 6 NEW_LINE print ( maximumSubarrays ( arr , N , target ) ) NEW_LINE DEDENT
Maximize array sum by replacing equal adjacent pairs by their sum and X respectively | Function to calculate x ^ y ; Base Case ; Find the value in temp ; If y is even ; Function that find the maximum possible sum of the array ; Print the result using the formula ; Driver Code ; Function call
def power ( x , y ) : NEW_LINE INDENT if ( y == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT temp = power ( x , y // 2 ) NEW_LINE if ( y % 2 == 0 ) : NEW_LINE INDENT return temp * temp NEW_LINE DEDENT else : NEW_LINE INDENT return x * temp * temp NEW_LINE DEDENT DEDENT def maximumPossibleSum ( N , X ) : NEW_LINE INDENT print ( X * ( power ( 2 , N ) - 1 ) ) NEW_LINE DEDENT N = 3 NEW_LINE X = 5 NEW_LINE maximumPossibleSum ( N , X ) NEW_LINE
Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively | Function to return the count of possible pairs of A and B whose Bitwise XOR is X and Y respectively ; Stores the count of pairs ; Iterate till any bit are set ; Extract i - th bit of X and Y ; Divide X and Y by 2 ; If Xi = 1 and Yi = 2 , multiply counter by 2 ; Increase required count ; If Xi = 1 and Yi = 1 ; No answer exists ; Return the final count ; Given X and Y ; Function call
def countOfPairs ( x , y ) : NEW_LINE INDENT counter = 1 NEW_LINE while ( x or y ) : NEW_LINE INDENT bit1 = x % 2 NEW_LINE bit2 = y % 2 NEW_LINE x >>= 1 NEW_LINE y >>= 1 NEW_LINE if ( bit1 == 1 and bit2 == 0 ) : NEW_LINE INDENT counter *= 2 NEW_LINE continue NEW_LINE DEDENT if ( bit1 & bit2 ) : NEW_LINE INDENT counter = 0 NEW_LINE break NEW_LINE DEDENT DEDENT return counter NEW_LINE DEDENT X = 2 NEW_LINE Y = 5 NEW_LINE print ( countOfPairs ( X , Y ) ) NEW_LINE
Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum | Function to return gcd of a and b ; Base Case ; Recursive call ; Function to find gcd of array ; Initialise the result ; Traverse the array arr [ ] ; Update result as gcd of the result and arr [ i ] ; Return the resultant GCD ; Given array arr [ ] ; Function call
def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT def findGCD ( arr , N ) : NEW_LINE INDENT result = 0 NEW_LINE for element in arr : NEW_LINE INDENT result = gcd ( result , element ) NEW_LINE if ( result == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT arr = [ 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findGCD ( arr , N ) ) NEW_LINE
Check if any permutation of array contains sum of every adjacent pair not divisible by 3 | Function to checks if any permutation of the array exists whose sum of adjacent pairs is not divisible by 3 ; Count remainder 0 ; Count remainder 1 ; Count remainder 2 ; Condition for valid arrangements ; Given array arr [ ] ; Function call
def factorsOf3 ( arr , N ) : NEW_LINE INDENT a = 0 NEW_LINE b = 0 NEW_LINE c = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] % 3 == 0 ) : NEW_LINE INDENT a += 1 NEW_LINE DEDENT elif ( arr [ i ] % 3 == 1 ) : NEW_LINE INDENT b += 1 NEW_LINE DEDENT elif ( arr [ i ] % 3 == 2 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT if ( a >= 1 and a <= b + c + 1 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT elif ( a == 0 and b == 0 and c > 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT elif ( a == 0 and c == 0 and b > 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE factorsOf3 ( arr , N ) NEW_LINE
Check if a number is a perfect square having all its digits as a perfect square | Python3 program for the above approach ; Function to check if digits of N is a perfect square or not ; Iterate over the digits ; Extract the digit ; Check if digit is a perfect square or not ; Divide N by 10 ; Return true ; Function to check if N is a perfect square or not ; If floor and ceil of n is not same ; Function to check if N satisfies the required conditions or not ; If both the conditions are satisfied ; Driver Code ; Function call
import math NEW_LINE def check_digits ( N ) : NEW_LINE INDENT while ( N > 0 ) : NEW_LINE INDENT n = N % 10 NEW_LINE if ( ( n != 0 ) and ( n != 1 ) and ( n != 4 ) and ( n != 9 ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT N = N // 10 NEW_LINE DEDENT return 1 NEW_LINE DEDENT def is_perfect ( N ) : NEW_LINE INDENT n = math . sqrt ( N ) NEW_LINE if ( math . floor ( n ) != math . ceil ( n ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return 1 NEW_LINE DEDENT def isFullSquare ( N ) : NEW_LINE INDENT if ( is_perfect ( N ) and check_digits ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT N = 144 NEW_LINE isFullSquare ( N ) NEW_LINE
Count substrings with different first and last characters | Function to count the substrings having different first & last character ; Stores frequency of each char ; Loop to store frequency of the characters in a Map ; To store final result ; Traversal of string ; Store answer for every iteration ; Map traversal ; Compare current char ; Print the final count ; Given string ; Length of the string ; Function Call
def countSubstring ( s , n ) : NEW_LINE INDENT m = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT if s [ i ] in m : NEW_LINE INDENT m [ s [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ s [ i ] ] = 1 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT cnt = 0 NEW_LINE if s [ i ] in m : NEW_LINE INDENT m [ s [ i ] ] -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ s [ i ] ] = - 1 NEW_LINE DEDENT for value in m : NEW_LINE INDENT if ( value == s [ i ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT cnt += m [ value ] NEW_LINE DEDENT DEDENT ans += cnt NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT S = " abcab " NEW_LINE N = 5 NEW_LINE countSubstring ( S , N ) NEW_LINE
Maximize count of empty water bottles from N filled bottles | Function to find the maximum bottles that can be emptied ; Iterate until a is non - zero ; Add the number of bottles that are emptied ; Update a after exchanging empty bottles ; Stores the number of bottles left after the exchange ; Return the answer ; Driver Code ; Function call
def maxBottles ( n , e ) : NEW_LINE INDENT s = 0 NEW_LINE b = 0 NEW_LINE a = n NEW_LINE while ( a != 0 ) : NEW_LINE INDENT s = s + a NEW_LINE a = ( a + b ) // e NEW_LINE b = n - ( a * e ) NEW_LINE n = a + b NEW_LINE DEDENT return s NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 9 NEW_LINE e = 3 NEW_LINE s = maxBottles ( n , e ) NEW_LINE print ( s ) NEW_LINE DEDENT
Count all N digit numbers whose digits are multiple of X | Function to calculate x ^ n using binary - exponentiation ; Stores the resultant power ; Stores the value of x ^ ( n / 2 ) ; Function to count aN - digit numbers whose digits are multiples of x ; Count adigits which are multiples of x ; Check if current number is a multiple of X ; Increase count of multiples ; Check if it 's a 1 digit number ; Count the total numbers ; Return the total numbers ; Driver Code ; Given N and X ; Function call
def power ( x , n ) : NEW_LINE INDENT temp = [ ] NEW_LINE if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT temp = power ( x , n // 2 ) NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT return temp * temp NEW_LINE DEDENT else : NEW_LINE INDENT return x * temp * temp NEW_LINE DEDENT DEDENT def count_Total_Numbers ( n , x ) : NEW_LINE INDENT total , multiples = 0 , 0 NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT if ( i % x == 0 ) : NEW_LINE INDENT multiples += 1 NEW_LINE DEDENT DEDENT if ( n == 1 ) : NEW_LINE INDENT return multiples NEW_LINE DEDENT total = ( ( multiples - 1 ) * power ( multiples , n - 1 ) ) NEW_LINE return total NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 1 NEW_LINE X = 3 NEW_LINE print ( count_Total_Numbers ( N , X ) ) NEW_LINE DEDENT
Find the vertex diagonally opposite to the vertex M from an N | Function to return the required vertex ; Case 1 : ; Case 2 : ; Driver Code
def getPosition ( N , M ) : NEW_LINE INDENT if ( M > ( N // 2 ) ) : NEW_LINE INDENT return ( M - ( N // 2 ) ) NEW_LINE DEDENT return ( M + ( N // 2 ) ) NEW_LINE DEDENT N = 8 NEW_LINE M = 5 NEW_LINE print ( getPosition ( N , M ) ) NEW_LINE
Maximize count of strings of length 3 that can be formed from N 1 s and M 0 s | Function that counts the number of strings of length three that can be made with given m 0 s and n 1 s ; Iterate until N & M are positive ; Case 1 : ; Case 2 : ; Print the count of strings ; Driver code ; Given count of 1 s and 0 s ; Function call
def number_of_strings ( N , M ) : NEW_LINE INDENT ans = 0 NEW_LINE while ( N > 0 and M > 0 ) : NEW_LINE INDENT if ( N > M ) : NEW_LINE INDENT if ( N >= 2 ) : NEW_LINE INDENT N -= 2 NEW_LINE M -= 1 NEW_LINE ans += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if M >= 2 : NEW_LINE INDENT M -= 2 NEW_LINE N -= 1 NEW_LINE ans += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE M = 19 NEW_LINE number_of_strings ( N , M ) NEW_LINE DEDENT
Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum | Function that returns the minimum length of the array after merging unequal adjacent elements ; Stores the first element and its frequency ; Traverse the array ; If all elements are equal ; No merge - pair operations can be performed ; Otherwise ; Given array ; Length of the array ; Function call
def minLength ( A , N ) : NEW_LINE INDENT elem = A [ 0 ] NEW_LINE count = 1 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( A [ i ] == elem ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( count == N ) : NEW_LINE INDENT return N NEW_LINE DEDENT else : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT arr = [ 2 , 1 , 3 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minLength ( arr , N ) ) NEW_LINE
Reduce N to 0 or less by given X and Y operations | Function to check if N can be reduced to <= 0 or not ; Update N to N / 2 + 10 at most X times ; Update N to N - 10 Y times ; Driver Code
def NegEqu ( N , X , Y ) : NEW_LINE INDENT while ( X and N > N // 2 + 10 ) : NEW_LINE INDENT N = N // 2 + 10 NEW_LINE X -= 1 NEW_LINE DEDENT while ( Y ) : NEW_LINE INDENT N = N - 10 NEW_LINE Y -= 1 NEW_LINE DEDENT if ( N <= 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 100 NEW_LINE X = 3 NEW_LINE Y = 4 NEW_LINE if ( NegEqu ( N , X , Y ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Split array into minimum number of subarrays having GCD of its first and last element exceeding 1 | Python3 program for the above approach ; Function to find the minimum number of subarrays ; Right pointer ; Left pointer ; Count of subarrays ; Find GCD ( left , right ) ; Found a valid large subarray between arr [ left , right ] ; Searched the arr [ 0. . right ] and found no subarray having size > 1 and having gcd ( left , right ) > 1 ; Driver Code ; Function call
from math import gcd NEW_LINE def minSubarrays ( arr , n ) : NEW_LINE INDENT right = n - 1 NEW_LINE left = 0 NEW_LINE subarrays = 0 NEW_LINE while ( right >= 0 ) : NEW_LINE INDENT for left in range ( right + 1 ) : NEW_LINE INDENT if ( gcd ( arr [ left ] , arr [ right ] ) > 1 ) : NEW_LINE INDENT subarrays += 1 NEW_LINE right = left - 1 NEW_LINE break NEW_LINE DEDENT if ( left == right and __gcd ( arr [ left ] , arr [ right ] ) == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT DEDENT return subarrays NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE arr = [ 2 , 3 , 4 , 4 , 4 , 3 ] NEW_LINE print ( minSubarrays ( arr , N ) ) NEW_LINE DEDENT
Find the largest possible value of K such that K modulo X is Y | Python3 program for the above approach ; Function to find the largest positive integer K such that K % x = y ; Stores the minimum solution ; Return the maximum possible value ; Driver Code
import sys NEW_LINE def findMaxSoln ( n , x , y ) : NEW_LINE INDENT ans = - sys . maxsize NEW_LINE for k in range ( n + 1 ) : NEW_LINE INDENT if ( k % x == y ) : NEW_LINE INDENT ans = max ( ans , k ) NEW_LINE DEDENT DEDENT return ( ans if ( ans >= 0 and ans <= n ) else - 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 15 NEW_LINE x = 10 NEW_LINE y = 5 NEW_LINE print ( findMaxSoln ( n , x , y ) ) NEW_LINE DEDENT
Count all numbers up to N having M as the last digit | Function to count the numbers ending with M ; Stores the count of numbers required ; Calculate count upto nearest power of 10 ; Computing the value of x ; Adding the count of numbers ending at M from x to N ; Driver Code ; Function call
def getCount ( N , M ) : NEW_LINE INDENT total_count = 0 NEW_LINE total_count += N // 10 NEW_LINE x = ( N // 10 ) * 10 NEW_LINE if ( ( N - x ) >= M ) : NEW_LINE INDENT total_count = total_count + 1 NEW_LINE DEDENT return total_count NEW_LINE DEDENT N = 100 NEW_LINE M = 1 NEW_LINE print ( getCount ( N , M ) ) NEW_LINE
Count of Right | Python3 program for the above approach ; Function to find the number of right angled triangle that are formed from given N points whose perpendicular or base is parallel to X or Y axis ; To store the number of points has same x or y coordinates ; Store the total count of triangle ; Iterate to check for total number of possible triangle ; Add the count of triangles formed ; Total possible triangle ; Driver Code ; Given N points ; Function call
from collections import defaultdict NEW_LINE def RightAngled ( a , n ) : NEW_LINE INDENT xpoints = defaultdict ( lambda : 0 ) NEW_LINE ypoints = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT xpoints [ a [ i ] [ 0 ] ] += 1 NEW_LINE ypoints [ a [ i ] [ 1 ] ] += 1 NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( xpoints [ a [ i ] [ 0 ] ] >= 1 and ypoints [ a [ i ] [ 1 ] ] >= 1 ) : NEW_LINE INDENT count += ( ( xpoints [ a [ i ] [ 0 ] ] - 1 ) * ( ypoints [ a [ i ] [ 1 ] ] - 1 ) ) NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT N = 5 NEW_LINE arr = [ [ 1 , 2 ] , [ 2 , 1 ] , [ 2 , 2 ] , [ 2 , 3 ] , [ 3 , 2 ] ] NEW_LINE print ( RightAngled ( arr , N ) ) NEW_LINE