codeparrot/xlcost-text-to-code huggingface dataset:
Number of cycles formed by joining vertices of n sided polygon at the center | Function to calculate number of cycles ; BigInteger is used here if N = 10 ^ 9 then multiply will result into value greater than 10 ^ 18 ; BigInteger multiply function ; Return the final result ; Driver Code ; Given N ; Function Call
def findCycles ( N ) : NEW_LINE INDENT res = 0 NEW_LINE finalResult = 0 NEW_LINE val = 2 * N - 1 ; NEW_LINE s = val NEW_LINE res = ( N - 1 ) * ( N - 2 ) NEW_LINE finalResult = res + s ; NEW_LINE return finalResult ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 ; NEW_LINE print ( findCycles ( N ) ) ; NEW_LINE DEDENT
Split a given array into K subarrays minimizing the difference between their maximum and minimum | Function to find the subarray ; Add the difference to vectors ; Sort vector to find minimum k ; Initialize result ; Adding first k - 1 values ; Return the minimized sum ; Driver code ; Length of array ; Given K ; Function Call
def find ( a , n , k ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT v . append ( a [ i - 1 ] - a [ i ] ) NEW_LINE DEDENT v . sort ( ) NEW_LINE res = a [ n - 1 ] - a [ 0 ] NEW_LINE for i in range ( k - 1 ) : NEW_LINE INDENT res += v [ i ] NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 4 , 8 , 15 , 16 , 23 , 42 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE print ( find ( arr , N , K ) ) NEW_LINE
Last digit of a number raised to last digit of N factorial | Function to find a ^ b using binary exponentiation ; Initialise result ; If b is odd then , multiply result by a ; b must be even now Change b to b / 2 ; Change a = a ^ 2 ; Function to find the last digit of the given equation ; To store cyclicity ; Store cyclicity from 1 - 10 ; Observation 1 ; Observation 3 ; To store the last digits of factorial 2 , 3 , and 4 ; Find the last digit of X ; Step 1 ; Divide a [ N ] by cyclicity of v ; If remainder is 0 ; Step 1.1 ; Step 1.2 ; Step 1.3 ; If r is non - zero , then return ( l ^ r ) % 10 ; Else return 0 ; Else return 1 ; Driver Code ; Given numbers ; Function call ; Print the result
def power ( a , b , c ) : NEW_LINE INDENT result = 1 NEW_LINE while ( b > 0 ) : NEW_LINE INDENT if ( ( b & 1 ) == 1 ) : NEW_LINE INDENT result = ( result * a ) % c NEW_LINE DEDENT b //= 2 NEW_LINE a = ( a * a ) % c NEW_LINE DEDENT return result NEW_LINE DEDENT def calculate ( X , N ) : NEW_LINE INDENT a = 10 * [ 0 ] NEW_LINE cyclicity = 11 * [ 0 ] NEW_LINE cyclicity [ 1 ] = 1 NEW_LINE cyclicity [ 2 ] = 4 NEW_LINE cyclicity [ 3 ] = 4 NEW_LINE cyclicity [ 4 ] = 2 NEW_LINE cyclicity [ 5 ] = 1 NEW_LINE cyclicity [ 6 ] = 1 NEW_LINE cyclicity [ 7 ] = 4 NEW_LINE cyclicity [ 8 ] = 4 NEW_LINE cyclicity [ 9 ] = 2 NEW_LINE cyclicity [ 10 ] = 1 NEW_LINE if ( N == 0 or N == 1 ) : NEW_LINE INDENT return ( X % 10 ) NEW_LINE DEDENT elif ( N == 2 or N == 3 or N == 4 ) : NEW_LINE INDENT temp = 1e18 ; NEW_LINE a [ 2 ] = 2 NEW_LINE a [ 3 ] = 6 NEW_LINE a [ 4 ] = 4 NEW_LINE v = X % 10 NEW_LINE if ( v != 0 ) : NEW_LINE INDENT u = cyclicity [ v ] NEW_LINE r = a [ N ] % u NEW_LINE if ( r == 0 ) : NEW_LINE INDENT if ( v == 2 or v == 4 or v == 6 or v == 8 ) : NEW_LINE INDENT return 6 NEW_LINE DEDENT elif ( v == 5 ) : NEW_LINE INDENT return 5 NEW_LINE DEDENT elif ( v == 1 or v == 3 or v == 7 or v == 9 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return ( power ( v , r , temp ) % 10 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT return 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = 18 NEW_LINE N = 4 NEW_LINE result = calculate ( X , N ) NEW_LINE print ( result ) NEW_LINE DEDENT
Maximize 3 rd element sum in quadruplet sets formed from given Array | Function to find the maximum possible value of Y ; Pairs contain count of minimum elements that will be utilized at place of Z . It is equal to count of possible pairs that is size of array divided by 4 ; Sorting the array in descending order so as to bring values with minimal difference closer to arr [ i ] ; Here , i + 2 acts as a pointer that points to the third value of every possible quadruplet ; Returning the optimally maximum possible value ; Array declaration ; Size of array
def formQuadruplets ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE pairs = 0 NEW_LINE pairs = n // 4 NEW_LINE arr . sort ( reverse = True ) NEW_LINE for i in range ( 0 , n - pairs , 3 ) : NEW_LINE INDENT ans += arr [ i + 2 ] NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 2 , 1 , 7 , 5 , 5 , 4 , 1 , 1 , 3 , 3 , 2 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( formQuadruplets ( arr , n ) ) NEW_LINE
Minimum concatenation required to get strictly LIS for array with repetitive elements | Set | Python3 implementation to Find the minimum concatenation required to get strictly Longest Increasing Subsequence for the given array with repetitive elements ; ordered map containing value and a vector containing index of it 's occurrences  <int, vector<int> > m; ; Mapping index with their values in ordered map ; k refers to present minimum index ; Stores the number of concatenation required ; Iterate over map m ; it . second . back refers to the last element of corresponding vector ; find the index of next minimum element in the sequence ; Return the final answer ; Driver code
from bisect import bisect_left NEW_LINE def LIS ( arr , n ) : NEW_LINE INDENT m = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ arr [ i ] ] = m . get ( arr [ i ] , [ ] ) NEW_LINE m [ arr [ i ] ] . append ( i ) NEW_LINE DEDENT k = n NEW_LINE ans = 1 NEW_LINE for key , value in m . items ( ) : NEW_LINE INDENT if ( value [ len ( value ) - 1 ] < k ) : NEW_LINE INDENT k = value [ 0 ] NEW_LINE ans += 1 NEW_LINE DEDENT else : NEW_LINE INDENT k = bisect_left ( value , k ) NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 2 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE LIS ( arr , n ) NEW_LINE DEDENT
Two Balls Reachability Game | Recursive function to return gcd of a and b ; Function returns if it 's possible to have X white and Y black balls or not. ; Finding gcd of ( x , y ) and ( a , b ) ; If gcd is same , it 's always  possible to reach (x, y) ; Here it 's never possible  if gcd is not same ; Driver Code
def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT return gcd ( b , a % b ) ; NEW_LINE DEDENT def IsPossible ( a , b , x , y ) : NEW_LINE INDENT final = gcd ( x , y ) ; NEW_LINE initial = gcd ( a , b ) ; NEW_LINE if ( initial == final ) : NEW_LINE INDENT print ( " POSSIBLE " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NOT ▁ POSSIBLE " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 1 ; B = 2 ; X = 4 ; Y = 11 ; NEW_LINE IsPossible ( A , B , X , Y ) ; NEW_LINE A = 2 ; B = 2 ; X = 3 ; Y = 6 ; NEW_LINE IsPossible ( A , B , X , Y ) ; NEW_LINE DEDENT
Number of ways to color N | Python3 program for the above approach ; Function to count the ways to color block ; For storing powers of 2 ; For storing binomial coefficient values ; Calculating binomial coefficient using DP ; Calculating powers of 2 ; Sort the indices to calculate length of each section ; Initialise answer to 1 ; Find the length of each section ; Merge this section ; Return the final count ; Driver Code ; Number of blocks ; Number of colored blocks ; Function call
mod = 1000000007 NEW_LINE def waysToColor ( arr , n , k ) : NEW_LINE INDENT global mod NEW_LINE powOf2 = [ 0 for i in range ( 500 ) ] NEW_LINE c = [ [ 0 for i in range ( 500 ) ] for j in range ( 500 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT c [ i ] [ 0 ] = 1 ; NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT c [ i ] [ j ] = ( c [ i - 1 ] [ j ] + c [ i - 1 ] [ j - 1 ] ) % mod ; NEW_LINE DEDENT DEDENT powOf2 [ 0 ] = 1 NEW_LINE powOf2 [ 1 ] = 1 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT powOf2 [ i ] = ( powOf2 [ i - 1 ] * 2 ) % mod ; NEW_LINE DEDENT rem = n - k ; NEW_LINE arr [ k ] = n + 1 ; NEW_LINE k += 1 NEW_LINE arr . sort ( ) NEW_LINE answer = 1 ; NEW_LINE for i in range ( k ) : NEW_LINE INDENT x = 0 NEW_LINE if i - 1 >= 0 : NEW_LINE INDENT x = arr [ i ] - arr [ i - 1 ] - 1 NEW_LINE DEDENT else : NEW_LINE INDENT x = arr [ i ] - 1 NEW_LINE DEDENT answer = answer * ( c [ rem ] [ x ] % mod ) * ( ( powOf2 [ x ] if ( i != 0 and i != k - 1 ) else 1 ) ) % mod NEW_LINE rem -= x ; NEW_LINE DEDENT return answer ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 ; NEW_LINE K = 3 ; NEW_LINE arr = [ 1 , 2 , 6 , 0 ] NEW_LINE print ( waysToColor ( arr , N , K ) ) NEW_LINE DEDENT
Rearrange array such that difference of adjacent elements is in descending order | Function to print array in given order ; Sort the array ; Check elements till the middle index ; Check if length is odd print the middle index at last ; Print the remaining elements in the described order ; Driver code ; Array declaration ; Size of array
def printArray ( a , n ) : NEW_LINE INDENT a . sort ( ) ; NEW_LINE i = 0 ; NEW_LINE j = n - 1 ; NEW_LINE while ( i <= j ) : NEW_LINE INDENT if ( i == j ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( a [ j ] , end = " ▁ " ) ; NEW_LINE print ( a [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT i = i + 1 ; NEW_LINE j = j - 1 ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr1 = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; NEW_LINE n1 = len ( arr1 ) ; NEW_LINE printArray ( arr1 , n1 ) ; NEW_LINE DEDENT
Find the Smallest number that divides X ^ X | Function to find the required smallest number ; Finding smallest number that divides n ; i divides n and return this value immediately ; If n is a prime number then answer should be n , As we can 't take 1 as our answer. ; Driver Code
def SmallestDiv ( n ) : NEW_LINE INDENT i = 2 NEW_LINE while i * i <= n : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return i NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return n NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = 385 NEW_LINE ans = SmallestDiv ( X ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Find an array of size N that satisfies the given conditions | Utility function to print the contents of an array ; Function to generate and print the required array ; Initially all the positions are empty ; To store the count of positions i such that arr [ i ] = s ; To store the final array elements ; Set arr [ i ] = s and the gap between them is exactly 2 so in for loop we use i += 2 ; Mark the i 'th position as visited  as we put arr[i] = s ; Increment the count ; Finding the next odd number after s ; If the i 'th position is not visited  it means we did not put any value  at position i so we put 1 now ; Print the final array ; Driver code
def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT def findArray ( n , k , s ) : NEW_LINE INDENT vis = [ 0 ] * n ; NEW_LINE cnt = 0 ; NEW_LINE arr = [ 0 ] * n ; NEW_LINE i = 0 ; NEW_LINE while ( i < n and cnt < k ) : NEW_LINE INDENT arr [ i ] = s ; NEW_LINE vis [ i ] = 1 ; NEW_LINE cnt += 1 ; NEW_LINE i += 2 ; NEW_LINE DEDENT val = s ; NEW_LINE if ( s % 2 == 0 ) : NEW_LINE INDENT val += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT val = val + 2 ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( vis [ i ] == 0 ) : NEW_LINE INDENT arr [ i ] = val ; NEW_LINE DEDENT DEDENT printArr ( arr , n ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 8 ; k = 3 ; s = 12 ; NEW_LINE findArray ( n , k , s ) ; NEW_LINE DEDENT
Sum of numbers in a range [ L , R ] whose count of divisors is prime | Python3 implementation of the approach ; divi [ i ] stores the count of divisors of i ; sum [ i ] will store the sum of all the integers from 0 to i whose count of divisors is prime ; Function for Sieve of Eratosthenes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be 0 if i is Not a prime , else true . prime [ i ] stores 1 if i is prime ; 0 and 1 is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to count the divisors ; For each number i we will go to each of the multiple of i and update the count of divisor of its multiple j as i is one of the factor of j ; Function for pre - computation ; If count of divisors of i is prime ; taking prefix sum ; Driver code ; Find all the prime numbers till N ; Update the count of divisors of all the numbers till N ; Precomputation for the prefix sum array ; Perform query
from math import sqrt NEW_LINE N = 100000 ; NEW_LINE divi = [ 0 ] * N ; NEW_LINE sum = [ 0 ] * N ; NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT prime [ i ] = 1 ; NEW_LINE DEDENT DEDENT prime = [ 1 ] * N ; NEW_LINE INDENT prime [ 0 ] = prime [ 1 ] = 0 ; NEW_LINE for p in range ( 2 , int ( sqrt ( N ) ) + 1 ) : NEW_LINE INDENT if ( prime [ p ] == 1 ) : NEW_LINE INDENT for i in range ( p * p , N , p ) : NEW_LINE INDENT prime [ i ] = 0 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def DivisorCount ( ) : NEW_LINE INDENT for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( i , N , i ) : NEW_LINE INDENT divi [ j ] += 1 ; NEW_LINE DEDENT DEDENT DEDENT def pre ( ) : NEW_LINE INDENT for i in range ( 1 , N ) : NEW_LINE INDENT if ( prime [ divi [ i ] ] == 1 ) : NEW_LINE INDENT sum [ i ] = i ; NEW_LINE DEDENT DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT sum [ i ] += sum [ i - 1 ] ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 5 ; r = 8 ; NEW_LINE SieveOfEratosthenes ( ) ; NEW_LINE DivisorCount ( ) ; NEW_LINE pre ( ) ; NEW_LINE print ( sum [ r ] - sum [ l - 1 ] ) ; NEW_LINE DEDENT
Count total number of even sum sequences | Python3 implementation of the approach ; Iterative function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is greater than or equal to p ; If y is odd then multiply x with the result ; y must be even now y = y >> 1 y = y / 2 ; Function to return n ^ ( - 1 ) mod p ; Function to return ( nCr % p ) using Fermat 's little theorem ; Base case ; Fill factorial array so that we can find all factorial of r , n and n - r ; Function to return the count of odd numbers from 1 to n ; Function to return the count of even numbers from 1 to n ; Function to return the count of the required sequences ; Take i even and n - i odd numbers ; Number of odd numbers must be even ; Total ways of placing n - i odd numbers in the sequence of n numbers ; Add this number to the final answer ; Driver code
M = 1000000007 NEW_LINE def power ( x , y , p ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % p NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT x = ( x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT def modInverse ( n , p ) : NEW_LINE INDENT return power ( n , p - 2 , p ) NEW_LINE DEDENT def nCrModPFermat ( n , r , p ) : NEW_LINE INDENT if ( r == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT fac = [ 0 ] * ( n + 1 ) NEW_LINE fac [ 0 ] = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT fac [ i ] = fac [ i - 1 ] * i % p NEW_LINE DEDENT return ( fac [ n ] * modInverse ( fac [ r ] , p ) % p * modInverse ( fac [ n - r ] , p ) % p ) % p NEW_LINE DEDENT def countOdd ( n ) : NEW_LINE INDENT x = n // 2 NEW_LINE if ( n % 2 == 1 ) : NEW_LINE INDENT x += 1 NEW_LINE DEDENT return x NEW_LINE DEDENT def counteEven ( n ) : NEW_LINE INDENT x = n // 2 NEW_LINE return x NEW_LINE DEDENT def CountEvenSumSequences ( n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT even = i NEW_LINE odd = n - i NEW_LINE if ( odd % 2 == 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT tot = ( power ( countOdd ( n ) , odd , M ) * nCrModPFermat ( n , odd , M ) ) % M NEW_LINE tot = ( tot * power ( counteEven ( n ) , i , M ) ) % M NEW_LINE count += tot NEW_LINE count %= M NEW_LINE DEDENT return count NEW_LINE DEDENT n = 5 NEW_LINE print ( CountEvenSumSequences ( n ) ) NEW_LINE
Find the sum of prime numbers in the Kth array | Python3 implementation of the approach ; Function for Sieve of Eratosthenes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to return the sum of primes in the Kth array ; Update vector v to store all the prime numbers upto MAX ; To store the sum of primes in the kth array ; Count of primes which are in the arrays from 1 to k - 1 ; k is the number of primes in the kth array ; A prime has been added to the sum ; Driver code
from math import sqrt NEW_LINE MAX = 1000000 NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT for p in range ( 2 , int ( sqrt ( MAX ) ) + 1 ) : NEW_LINE DEDENT prime = [ True ] * MAX NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT for i in range ( p * p , MAX , p ) : NEW_LINE INDENT prime [ i ] = False ; NEW_LINE DEDENT DEDENT DEDENT def sumPrime ( k ) : NEW_LINE INDENT SieveOfEratosthenes ( ) ; NEW_LINE v = [ ] ; NEW_LINE for i in range ( 2 , MAX ) : NEW_LINE INDENT if ( prime [ i ] ) : NEW_LINE INDENT v . append ( i ) ; NEW_LINE DEDENT DEDENT sum = 0 ; NEW_LINE skip = ( k * ( k - 1 ) ) // 2 ; NEW_LINE while ( k > 0 ) : NEW_LINE INDENT sum += v [ skip ] ; NEW_LINE skip += 1 ; NEW_LINE k -= 1 ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT k = 3 ; NEW_LINE print ( sumPrime ( k ) ) ; NEW_LINE DEDENT
Count all substrings having character K | Function to return the index of the next occurrence of character ch in strr starting from the given index ; Return the index of the first occurrence of ch ; No occurrence found ; Function to return the count of all the substrings of strr which contain the character ch at least one ; To store the count of valid substrings ; Index of the first occurrence of ch in strr ; No occurrence of ch after index i in strr ; Substrings starting at index i and ending at indices j , j + 1 , ... , n - 1 are all valid substring ; Driver code
def nextOccurrence ( strr , n , start , ch ) : NEW_LINE INDENT for i in range ( start , n ) : NEW_LINE INDENT if ( strr [ i ] == ch ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def countSubStr ( strr , n , ch ) : NEW_LINE INDENT cnt = 0 NEW_LINE j = nextOccurrence ( strr , n , 0 , ch ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT while ( j != - 1 and j < i ) : NEW_LINE INDENT j = nextOccurrence ( strr , n , j + 1 , ch ) NEW_LINE DEDENT if ( j == - 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT cnt += ( n - j ) NEW_LINE DEDENT return cnt NEW_LINE DEDENT strr = " geeksforgeeks " NEW_LINE n = len ( strr ) NEW_LINE ch = ' k ' NEW_LINE print ( countSubStr ( strr , n , ch ) ) NEW_LINE
Check whether bitwise AND of N numbers is Even or Odd | Function to check if the bitwise AND of the array elements is even or odd ; If at least an even element is present then the bitwise AND of the array elements will be even ; Driver code
def checkEvenOdd ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT print ( " Even " , end = " " ) ; NEW_LINE return ; NEW_LINE DEDENT DEDENT print ( " Odd " , end = " " ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 12 , 20 , 36 , 38 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE checkEvenOdd ( arr , n ) ; NEW_LINE DEDENT
Find number of magical pairs of string of length L | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is >= p ; If y is odd , multiply x with result ; Y must be even now y = y >> 1 ; y = y / 2 ; Driver Code
def power ( x , y , p ) : NEW_LINE INDENT res = 1 ; NEW_LINE x = x % p ; NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y % 2 == 1 ) : NEW_LINE INDENT res = ( res * x ) % p ; NEW_LINE DEDENT x = ( x * x ) % p ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT L = 2 ; P = pow ( 10 , 9 ) ; NEW_LINE ans = power ( 325 , L , P ) ; NEW_LINE print ( ans ) ; NEW_LINE
Longest substring of only 4 's from the first N characters of the infinite string | Python 3 implementation of the approach ; Function to return the length of longest contiguous string containing only 4 aTMs from the first N characters of the string ; Initialize prefix sum array of characters and product variable ; Preprocessing of prefix sum array ; Finding the string length where N belongs to ; Driver Code
MAXN = 30 NEW_LINE def countMaxLength ( N ) : NEW_LINE INDENT pre = [ 0 for i in range ( MAXN ) ] NEW_LINE p = 1 NEW_LINE pre [ 0 ] = 0 NEW_LINE for i in range ( 1 , MAXN , 1 ) : NEW_LINE INDENT p *= 2 NEW_LINE pre [ i ] = pre [ i - 1 ] + i * p NEW_LINE DEDENT for i in range ( 1 , MAXN , 1 ) : NEW_LINE INDENT if ( pre [ i ] >= N ) : NEW_LINE INDENT ind = i NEW_LINE break NEW_LINE DEDENT DEDENT x = N - pre [ ind - 1 ] NEW_LINE y = 2 * ind - 1 NEW_LINE if ( x >= y ) : NEW_LINE INDENT res = min ( x , y ) NEW_LINE DEDENT else : NEW_LINE INDENT res = max ( x , 2 * ( ind - 2 ) + 1 ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 25 NEW_LINE print ( countMaxLength ( N ) ) NEW_LINE DEDENT
Difference between Recursion and Iteration | -- -- - Recursion -- -- - method to find factorial of given number ; recursion call ; -- -- - Iteration -- -- - Method to find the factorial of a given number ; using iteration ; Driver method
def factorialUsingRecursion ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT return n * factorialUsingRecursion ( n - 1 ) ; NEW_LINE DEDENT def factorialUsingIteration ( n ) : NEW_LINE INDENT res = 1 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res *= i ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT num = 5 ; NEW_LINE print ( " Factorial ▁ of " , num , " using ▁ Recursion ▁ is : " , factorialUsingRecursion ( 5 ) ) ; NEW_LINE print ( " Factorial ▁ of " , num , " using ▁ Iteration ▁ is : " , factorialUsingIteration ( 5 ) ) ; NEW_LINE
In | Function to reverse arr [ ] from start to end ; Create a copy array and store reversed elements ; Now copy reversed elements back to arr [ ] ; Driver code
def revereseArray ( arr , n ) : NEW_LINE INDENT rev = n * [ 0 ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT rev [ n - i - 1 ] = arr [ i ] NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT arr [ i ] = rev [ i ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( * arr ) NEW_LINE revereseArray ( arr , n ) ; NEW_LINE print ( " Reversed ▁ array ▁ is " ) NEW_LINE print ( * arr ) NEW_LINE DEDENT
In | Function to reverse arr [ ] from start to end ; Driver code
def revereseArray ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , int ( n / 2 ) ) : NEW_LINE INDENT arr [ i ] , arr [ n - i - 1 ] = arr [ n - i - 1 ] , arr [ i ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( * arr ) NEW_LINE revereseArray ( arr , n ) NEW_LINE print ( " Reversed ▁ array ▁ is " ) NEW_LINE print ( * arr ) NEW_LINE DEDENT