codeparrot/xlcost-text-to-code huggingface dataset:
Last remaining character after repeated removal of the first character and flipping of characters of a Binary String | Function to find the last removed character from the string ; Stores length of the string ; Base Case : ; If the second last character is '0 ; If the second last character is '1 ; Driver Code
def lastRemovedCharacter ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE if ( n == 1 ) : NEW_LINE INDENT return ord ( str [ 0 ] ) NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( str [ n - 2 ] == '0' ) : NEW_LINE INDENT return ( ord ( '1' ) - ord ( str [ n - 1 ] ) + ord ( '0' ) ) NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT else : NEW_LINE INDENT return ord ( str [ n - 1 ] ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "10010" NEW_LINE print ( chr ( lastRemovedCharacter ( str ) ) ) NEW_LINE DEDENT
Find the triplet from given Bitwise XOR and Bitwise AND values of all its pairs | Function to find the triplet with given Bitwise XOR and Bitwise AND values of all possible pairs of the triplet ; Stores values of a triplet ; Stores a + b ; Stores a + c ; Stores b + c ; Calculate aSUMb ; Calculate aSUMc ; Calculate bSUMc ; Calculate a ; Calculate b ; Calculate c ; Pra ; Prb ; Prc ; Driver Code
def findNumbers ( aXORb , aANDb , aXORc , aANDc , bXORc , bANDc ) : NEW_LINE INDENT a , b , c = 0 , 0 , 0 ; NEW_LINE aSUMb = 0 ; NEW_LINE aSUMc = 0 ; NEW_LINE bSUMc = 0 ; NEW_LINE aSUMb = aXORb + aANDb * 2 ; NEW_LINE aSUMc = aXORc + aANDc * 2 ; NEW_LINE bSUMc = bXORc + bANDc * 2 ; NEW_LINE a = ( aSUMb - bSUMc + aSUMc ) // 2 ; NEW_LINE b = aSUMb - a ; NEW_LINE c = aSUMc - a ; NEW_LINE print ( " a ▁ = ▁ " , a , end = " " ) ; NEW_LINE print ( " , ▁ b ▁ = ▁ " , b , end = " " ) ; NEW_LINE print ( " , ▁ c ▁ = ▁ " , c , end = " " ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT aXORb = 30 ; aANDb = 0 ; aXORc = 20 ; aANDc = 10 ; bXORc = 10 ; bANDc = 20 ; NEW_LINE findNumbers ( aXORb , aANDb , aXORc , aANDc , bXORc , bANDc ) ; NEW_LINE DEDENT
Find N distinct numbers whose Bitwise XOR is equal to K | Function to find N integers having Bitwise XOR equal to K ; Base Cases ; Assign values to P and Q ; Stores Bitwise XOR of the first ( N - 3 ) elements ; Print the first N - 3 elements ; Calculate Bitwise XOR of first ( N - 3 ) elements ; Driver Code ; Function Call
def findArray ( N , K ) : NEW_LINE INDENT if ( N == 1 ) : NEW_LINE INDENT print ( K , end = " ▁ " ) NEW_LINE return NEW_LINE DEDENT if ( N == 2 ) : NEW_LINE INDENT print ( "0" , end = " ▁ " ) NEW_LINE print ( K , end = " ▁ " ) NEW_LINE return NEW_LINE DEDENT P = N - 2 NEW_LINE Q = N - 1 NEW_LINE VAL = 0 NEW_LINE for i in range ( 1 , N - 2 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE VAL ^= i NEW_LINE DEDENT if ( VAL == K ) : NEW_LINE INDENT print ( P , end = " ▁ " ) NEW_LINE print ( Q , end = " ▁ " ) NEW_LINE print ( P ^ Q , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( "0" , end = " ▁ " ) NEW_LINE print ( P , end = " ▁ " ) NEW_LINE print ( P ^ K ^ VAL , end = " ▁ " ) NEW_LINE DEDENT DEDENT N = 4 NEW_LINE X = 6 NEW_LINE findArray ( N , X ) NEW_LINE
Flip consecutive set bits starting from LSB of a given number | Function to find the number after converting 1 s from end to 0 s ; Count of 1 s ; AND operation of N and 1 ; Left shift N by count ; Driver Code ; Function Call
def findNumber ( N ) : NEW_LINE INDENT count = 0 NEW_LINE while ( ( N & 1 ) == 1 ) : NEW_LINE INDENT N = N >> 1 NEW_LINE count += 1 NEW_LINE DEDENT return N << count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 39 NEW_LINE print ( findNumber ( N ) ) NEW_LINE DEDENT
Flip consecutive set bits starting from LSB of a given number | Function to find the number after converting 1 s from end to 0 s ; Return the logical AND of N and ( N + 1 ) ; Driver Code ; Function Call
def findNumber ( N ) : NEW_LINE INDENT return N & ( N + 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 39 NEW_LINE print ( findNumber ( N ) ) NEW_LINE DEDENT
Count of substrings of a string containing another given string as a substring | Function to store all substrings of S ; Stores the substrings of S ; Pick start point in outer loop and lengths of different strings for a given starting point ; Return the array containing substrings of S ; Function to check if a is present in another string ; Check if target is in the str or not ; Function to count the subof S containing T in it as substring ; Store all substrings of S in the array v [ ] ; Store required count of substrings ; Iterate through all the substrings of S ; If T is present in the current substring , then increment the ans ; Print the answer ; Driver code ; Function Call
def subString ( s , n ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for len in range ( 1 , n - i + 1 ) : NEW_LINE INDENT find = s [ i : i + len ] NEW_LINE v . append ( find ) NEW_LINE DEDENT DEDENT return v NEW_LINE DEDENT def IsPresent ( str , target ) : NEW_LINE INDENT if ( target in str ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return - 1 NEW_LINE DEDENT def countSubstrings ( S , T ) : NEW_LINE INDENT v = subString ( S , len ( S ) ) NEW_LINE ans = 0 NEW_LINE for it in v : NEW_LINE INDENT if ( IsPresent ( it , T ) != - 1 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " dabc " NEW_LINE T = " ab " NEW_LINE countSubstrings ( S , T ) NEW_LINE DEDENT
Minimum flips of odd indexed elements from odd length subarrays to make two given arrays equal | Function to find the minimum flip of subarrays required at alternate index to make binary arrays equals ; Stores count of total operations ; Stores count of consecutive unequal elements ; Loop to run on odd positions ; Incrementing the global counter ; Change count to 0 ; If all last elements are equal ; Loop to run on even positions ; Incrementing the global counter ; Change count to 0 ; Print minimum operations ; Driver Code ; Function Call
def minOperation ( X , Y , n ) : NEW_LINE INDENT C = 0 ; NEW_LINE count = 0 ; NEW_LINE for i in range ( 1 , n , 2 ) : NEW_LINE INDENT if ( X [ i ] != Y [ i ] ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( count != 0 ) : NEW_LINE INDENT C += 1 ; NEW_LINE DEDENT count = 0 ; NEW_LINE DEDENT DEDENT if ( count != 0 ) : NEW_LINE INDENT C += 1 ; NEW_LINE DEDENT count = 0 ; NEW_LINE for i in range ( 0 , n , 2 ) : NEW_LINE INDENT if ( X [ i ] != Y [ i ] ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( count != 0 ) : NEW_LINE INDENT C += 1 ; NEW_LINE DEDENT count = 0 ; NEW_LINE DEDENT DEDENT if ( count != 0 ) : NEW_LINE INDENT C += 1 ; NEW_LINE DEDENT print ( C ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = [ 1 , 0 , 0 , 0 , 0 , 1 ] ; NEW_LINE Y = [ 1 , 1 , 0 , 1 , 1 , 1 ] ; NEW_LINE N = len ( X ) ; NEW_LINE minOperation ( X , Y , N ) ; NEW_LINE DEDENT
Minimum subarray reversals required to make given binary array alternating | Function to count minimum subarray reversal operations required to make array alternating ; Stores minimum count of operations required to make array alternating ; Traverse the array ; If arr [ i ] is greater than arr [ i + 1 ] ; Update cntOp ; Driver Code
def minimumcntOperationReq ( arr , N ) : NEW_LINE INDENT cntOp = 0 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if ( arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT cntOp += 1 NEW_LINE DEDENT DEDENT return ( cntOp + 1 ) // 2 NEW_LINE DEDENT arr = [ 1 , 1 , 1 , 0 , 1 , 0 , 0 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minimumcntOperationReq ( arr , N ) ) NEW_LINE
Modify sequence of first N natural numbers to a given array by replacing pairs with their GCD | Function to check if array arr [ ] can be obtained from first N natural numbers or not ; Driver Code ; Function Call
def isSequenceValid ( B , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT if ( ( i + 1 ) % B [ i ] != 0 ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " Yes " ) NEW_LINE DEDENT N = 4 NEW_LINE arr = [ 1 , 2 , 3 , 2 ] NEW_LINE isSequenceValid ( arr , N ) NEW_LINE
Construct an array of first N natural numbers such that every adjacent pair is coprime | Function to construct an arrary with adjacent elements as co - prime numbers ; Iterate over the range [ 1 , N ] ; Print i ; Driver Code
def ConstArrayAdjacentCoprime ( N ) : NEW_LINE INDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 NEW_LINE ConstArrayAdjacentCoprime ( N ) NEW_LINE DEDENT
Minimum cost to convert one given string to another using swap , insert or delete operations | Function to find the minimum cost to convert a to b ; Stores the frequency of string a and b respectively ; Store the frequencies of characters in a ; Store the frequencies of characters in b ; Minimum cost to convert A to B ; Find the minimum cost ; Print the minimum cost ; Driver Code ; Function Call
def minimumCost ( a , b ) : NEW_LINE INDENT fre1 = [ 0 ] * ( 256 ) NEW_LINE fre2 = [ 0 ] * ( 256 ) NEW_LINE for c in a : NEW_LINE INDENT fre1 [ ord ( c ) ] += 1 NEW_LINE DEDENT for c in b : NEW_LINE INDENT fre2 [ ord ( c ) ] += 1 NEW_LINE DEDENT mincost = 0 NEW_LINE for i in range ( 256 ) : NEW_LINE INDENT mincost += abs ( fre1 [ i ] - fre2 [ i ] ) NEW_LINE DEDENT print ( mincost ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = "1AB + - " NEW_LINE B = " cc " NEW_LINE minimumCost ( A , B ) NEW_LINE DEDENT
Minimum cost required to move all elements to the same position | Function to find the minimum cost required to place all elements in the same position ; Stores the count of even and odd elements ; Traverse the array arr [ ] ; Count even elements ; Count odd elements ; Print the minimum count ; Driver Code ; Given array ; Function Call
def minCost ( arr ) : NEW_LINE INDENT odd = 0 NEW_LINE even = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT even += 1 NEW_LINE DEDENT else : NEW_LINE INDENT odd += 1 NEW_LINE DEDENT DEDENT print ( min ( even , odd ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 ] NEW_LINE minCost ( arr ) NEW_LINE DEDENT
Maximize array sum by alternating the signs of adjacent elements | Function to find the maximum sum by alternating the signs of adjacent elements of the array ; Stores count of negative elements in the array ; Stores maximum sum by alternating the signs of adjacent elements ; Stores smallest absolute value of array elements ; Stores sum of absolute value of array elements ; Traverse the array ; If arr [ i ] is a negative number ; Update cntNeg ; Update sum ; Update SmValue ; Update MaxAltSum ; If cntNeg is an odd number ; Update MaxAltSum ; Driver Code
def findMaxSumByAlternatingSign ( arr , N ) : NEW_LINE INDENT cntNeg = 0 NEW_LINE MaxAltSum = 0 NEW_LINE SmValue = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT cntNeg += 1 NEW_LINE DEDENT sum += abs ( arr [ i ] ) NEW_LINE SmValue = min ( SmValue , abs ( arr [ i ] ) ) NEW_LINE DEDENT MaxAltSum = sum NEW_LINE if ( cntNeg & 1 ) : NEW_LINE INDENT MaxAltSum -= 2 * SmValue NEW_LINE DEDENT return MaxAltSum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , - 2 , - 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findMaxSumByAlternatingSign ( arr , N ) ) NEW_LINE DEDENT
Construct array with sum of product of same indexed elements in the given array equal to zero | Function to generate a new array with product of same indexed elements with arr [ ] equal to 0 ; Stores sum of same indexed array elements of arr and new array ; Traverse the array ; If i is an even number ; Insert arr [ i + 1 ] into the new array newArr [ ] ; Insert - arr [ i - 1 ] into the new array newArr [ ] ; Print new array elements ; Driver code
def constructNewArraySumZero ( arr , N ) : NEW_LINE INDENT newArr = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT newArr [ i ] = arr [ i + 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT newArr [ i ] = - arr [ i - 1 ] NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT print ( newArr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , - 5 , - 6 , 8 ] NEW_LINE N = len ( arr ) NEW_LINE constructNewArraySumZero ( arr , N ) NEW_LINE
Generate Bitonic Sequence of length N from integers in a given range | Python3 program for the above approach ; Function to construct bitonic sequence of length N from integers in the range [ L , R ] ; If sequence is not possible ; Store the resultant list ; If size of deque < n ; Add elements from start ; Print the stored in the list ; Driver Code ; Function Call
from collections import deque NEW_LINE def bitonicSequence ( num , lower , upper ) : NEW_LINE INDENT if ( num > ( upper - lower ) * 2 + 1 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT ans = deque ( ) NEW_LINE for i in range ( min ( upper - lower + 1 , num - 1 ) ) : NEW_LINE INDENT ans . append ( upper - i ) NEW_LINE DEDENT for i in range ( num - len ( ans ) ) : NEW_LINE INDENT ans . appendleft ( upper - i - 1 ) NEW_LINE DEDENT print ( list ( ans ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE L = 3 NEW_LINE R = 10 NEW_LINE bitonicSequence ( N , L , R ) NEW_LINE DEDENT
Count substrings of same length differing by a single character from two given strings | Function to count the number of substrings of equal length which differ by a single character ; Stores the count of pairs of substrings ; Traverse the string s ; Traverse the string t ; Different character ; Increment the answer ; Count equal substrings from next index ; Increment the count ; Increment q ; Check the condition ; Increment k ; Add q to count ; Decrement z ; Return the final count ; Driver Code ; Function Call
def countSubstrings ( s , t ) : NEW_LINE INDENT answ = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT for j in range ( len ( t ) ) : NEW_LINE INDENT if t [ j ] != s [ i ] : NEW_LINE INDENT answ += 1 NEW_LINE k = 1 NEW_LINE z = - 1 NEW_LINE q = 1 NEW_LINE while ( j + z >= 0 <= i + z and s [ i + z ] == t [ j + z ] ) : NEW_LINE INDENT z -= 1 NEW_LINE answ += 1 NEW_LINE q += 1 NEW_LINE DEDENT while ( len ( s ) > i + k and j + k < len ( t ) and s [ i + k ] == t [ j + k ] ) : NEW_LINE INDENT k += 1 NEW_LINE answ += q NEW_LINE z = - 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return answ NEW_LINE DEDENT S = " aba " NEW_LINE T = " baba " NEW_LINE print ( countSubstrings ( S , T ) ) NEW_LINE
Minimum flips to make all 1 s in right and 0 s in left | Function to find the minimum count of flips required to make all 1 s on the right and all 0 s on the left of the given string ; Stores length of str ; Store count of 0 s in the string ; Traverse the string ; If current character is 0 ; Update zeros ; If count of 0 s in the string is 0 or n ; Store minimum count of flips required to make all 0 s on the left and all 1 s on the right ; Stores count of 1 s on the left of each index ; Stores count of flips required to make all 0 s on the left and all 1 s on the right ; Traverse the string ; If current character is 1 ; Update currOnes ; Update flips ; Update the minimum count of flips ; Driver Code
def minimumCntOfFlipsRequired ( str ) : NEW_LINE INDENT n = len ( str ) ; NEW_LINE zeros = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( str [ i ] == '0' ) : NEW_LINE INDENT zeros += 1 ; NEW_LINE DEDENT DEDENT if ( zeros == 0 or zeros == n ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT minFlips = 10000001 ; NEW_LINE currOnes = 0 ; NEW_LINE flips = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( str [ i ] == '1' ) : NEW_LINE INDENT currOnes += 1 ; NEW_LINE DEDENT flips = currOnes + ( zeros - ( i + 1 - currOnes ) ) ; NEW_LINE minFlips = min ( minFlips , flips ) ; NEW_LINE DEDENT return minFlips ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "100101" ; NEW_LINE print ( minimumCntOfFlipsRequired ( str ) ) ; NEW_LINE DEDENT
Maximize length of longest non | Function to find the length of the longest non - decreasing array that can be generated ; Stores the length of the longest non - decreasing array that can be generated from the array ; Stores index of start pointer ; Stores index of end pointer ; Stores previously inserted element into the new array ; Traverse the array ; If A [ start ] is less than or equal to A [ end ] ; If no element inserted into the newly generated array ; Update prev ; Update res ; Update start ; If A [ start ] is greater than or equal to prev ; Update res ; Update prev ; Update start ; If A [ end ] is greater than or equal to prev ; Update res ; Update prev ; Update end ; If A [ end ] is greater than A [ start ] ; If no element inserted into the newly generated array ; Update prev ; Update res ; Update end ; If A [ end ] is greater than or equal to prev ; Update res ; Update prev ; Update end ; If A [ start ] is greater than or equal to prev ; Update res ; Update prev ; Update start ; Driver Code ; Function call
def findLongestNonDecreasing ( A , N ) : NEW_LINE INDENT res = 0 ; NEW_LINE start = 0 ; NEW_LINE end = N - 1 ; NEW_LINE prev = - 1 ; NEW_LINE while ( start <= end ) : NEW_LINE INDENT if ( A [ start ] <= A [ end ] ) : NEW_LINE INDENT if ( prev == - 1 ) : NEW_LINE INDENT prev = A [ start ] ; NEW_LINE res += 1 NEW_LINE start += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( A [ start ] >= prev ) : NEW_LINE INDENT res += 1 NEW_LINE prev = A [ start ] ; NEW_LINE start += 1 NEW_LINE DEDENT elif ( A [ end ] >= prev ) : NEW_LINE INDENT res += 1 NEW_LINE prev = A [ end ] ; NEW_LINE end -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT if ( prev == - 1 ) : NEW_LINE INDENT prev = A [ end ] ; NEW_LINE res += 1 NEW_LINE end -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( A [ end ] >= prev ) : NEW_LINE INDENT res += 1 NEW_LINE prev = A [ end ] ; NEW_LINE end -= 1 NEW_LINE DEDENT elif ( A [ start ] >= prev ) : NEW_LINE INDENT res += 1 NEW_LINE prev = A [ start ] ; NEW_LINE start += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 1 , 3 , 5 , 4 , 3 , 6 , 2 , 1 ] NEW_LINE N = len ( A ) NEW_LINE print ( findLongestNonDecreasing ( A , N ) ) ; NEW_LINE DEDENT
Non | Function to print all pairs whose sum of Bitwise OR and AND is N ; Iterate from i = 0 to N ; Print pairs ( i , N - i ) ; Driver code
def findPairs ( N ) : NEW_LINE INDENT for i in range ( 0 , N + 1 ) : NEW_LINE INDENT print ( " ( " , i , " , " , N - i , " ) , ▁ " , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE findPairs ( N ) NEW_LINE DEDENT
Minimum index to split array into subarrays with co | Function to find the GCD of 2 numbers ; Base Case ; Find the GCD recursively ; Function to find the minimum partition index K s . t . product of both subarrays around that partition are co - prime ; Stores the prefix and suffix array product ; Update the prefix array ; Update the suffix array ; Iterate the given array ; Check if prefix [ k ] and suffix [ k + 1 ] are co - prime ; If no index for partition exists , then return - 1 ; Driver Code ; Function call
def GCD ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return GCD ( b , a % b ) NEW_LINE DEDENT def findPartition ( nums , N ) : NEW_LINE INDENT prefix = [ 0 ] * N NEW_LINE suffix = [ 0 ] * N NEW_LINE prefix [ 0 ] = nums [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT prefix [ i ] = ( prefix [ i - 1 ] * nums [ i ] ) NEW_LINE DEDENT suffix [ N - 1 ] = nums [ N - 1 ] NEW_LINE for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT suffix [ i ] = ( suffix [ i + 1 ] * nums [ i ] ) NEW_LINE DEDENT for k in range ( N - 1 ) : NEW_LINE INDENT if ( GCD ( prefix [ k ] , suffix [ k + 1 ] ) == 1 ) : NEW_LINE INDENT return k NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findPartition ( arr , N ) ) NEW_LINE DEDENT
Minimum subarray reversals required such that sum of all pairs of adjacent elements is odd | Function to count reversals to separate elements with same parity ; Traverse the given array ; Count size of subarray having integers with same parity only ; Otherwise ; Reversals required is equal to one less than subarray size ; Return the total reversals ; Function to print the array elements ; Function to count the minimum reversals required to make make sum of all adjacent elements odd ; Stores operations required for separating adjacent odd elements ; Stores operations required for separating adjacent even elements ; Maximum of two is the return ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
def separate ( arr , n , parity ) : NEW_LINE INDENT count = 1 NEW_LINE res = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( ( ( arr [ i ] + parity ) & 1 ) and ( ( arr [ i - 1 ] + parity ) & 1 ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( count > 1 ) : NEW_LINE INDENT res += count - 1 NEW_LINE DEDENT count = 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def requiredOps ( arr , N ) : NEW_LINE INDENT res1 = separate ( arr , N , 0 ) NEW_LINE res2 = separate ( arr , N , 1 ) NEW_LINE print ( max ( res1 , res2 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 13 , 2 , 6 , 8 , 3 , 5 , 7 , 10 , 14 , 15 ] NEW_LINE N = len ( arr ) NEW_LINE requiredOps ( arr , N ) NEW_LINE DEDENT
Count prime pairs whose difference is also a Prime Number | Python3 program to implement the above approach ; Function to find all prime numbers in the range [ 1 , N ] ; isPrime [ i ] : Stores if i is a prime number or not ; Calculate all prime numbers up to Max using Sieve of Eratosthenes ; If P is a prime number ; Set all multiple of P as non - prime ; Update isPrime ; Function to count pairs of prime numbers in the range [ 1 , N ] whose difference is prime ; Function to count pairs of prime numbers whose difference is also a prime number ; isPrime [ i ] : Stores if i is a prime number or not ; Iterate over the range [ 2 , N ] ; If i and i - 2 is a prime number ; Update cntPairs ; Driver Code
from math import sqrt NEW_LINE 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 for p in range ( 2 , int ( sqrt ( N ) ) + 1 , 1 ) : NEW_LINE INDENT if ( isPrime [ p ] ) : NEW_LINE INDENT for i in range ( p * p , N + 1 , p ) : NEW_LINE INDENT isPrime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT return isPrime NEW_LINE DEDENT def cntPairsdiffOfPrimeisPrime ( N ) : NEW_LINE INDENT cntPairs = 0 NEW_LINE isPrime = SieveOfEratosthenes ( N ) NEW_LINE for i in range ( 2 , N + 1 , 1 ) : NEW_LINE INDENT if ( isPrime [ i ] and isPrime [ i - 2 ] ) : NEW_LINE INDENT cntPairs += 2 NEW_LINE DEDENT DEDENT return cntPairs NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE print ( cntPairsdiffOfPrimeisPrime ( N ) ) NEW_LINE DEDENT
Length of longest subsequence consisting of distinct adjacent elements | Function that finds the length of longest subsequence having different adjacent elements ; Stores the length of the longest subsequence ; Traverse the array ; If previous and current element are not same ; Increment the count ; Print the maximum length ; Driver Code ; Size of Array ; Function Call
def longestSubsequence ( arr , N ) : NEW_LINE INDENT count = 1 NEW_LINE for i in range ( 1 , N , 1 ) : NEW_LINE INDENT if ( arr [ i ] != arr [ i - 1 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 8 , 1 , 2 , 2 , 5 , 5 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE longestSubsequence ( arr , N ) NEW_LINE DEDENT