codeparrot/xlcost-text-to-code huggingface dataset:
Minimum peak elements from an array by their repeated removal at every iteration of the array | Python3 program for the above approach ; Function to return the list of minimum peak elements ; Length of original list ; Initialize resultant list ; Traverse each element of list ; Length of original list after removing the peak element ; Traverse new list after removal of previous min peak element ; Update min and index , if first element of list > next element ; Update min and index , if last element of list > previous one ; Update min and index , if list has single element ; Update min and index , if current element > adjacent elements ; Remove current min peak element from list ; Insert min peak into resultant list ; Print resultant list ; Driver Code ; Given array arr [ ] ; Function call
import sys NEW_LINE def minPeaks ( list1 ) : NEW_LINE INDENT n = len ( list1 ) NEW_LINE result = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT min = sys . maxsize NEW_LINE index = - 1 NEW_LINE size = len ( list1 ) NEW_LINE for j in range ( size ) : NEW_LINE INDENT if ( j == 0 and j + 1 < size ) : NEW_LINE INDENT if ( list1 [ j ] > list1 [ j + 1 ] and min > list1 [ j ] ) : NEW_LINE INDENT min = list1 [ j ] ; NEW_LINE index = j ; NEW_LINE DEDENT DEDENT elif ( j == size - 1 and j - 1 >= 0 ) : NEW_LINE INDENT if ( list1 [ j ] > list1 [ j - 1 ] and min > list1 [ j ] ) : NEW_LINE INDENT min = list1 [ j ] NEW_LINE index = j NEW_LINE DEDENT DEDENT elif ( size == 1 ) : NEW_LINE INDENT min = list1 [ j ] NEW_LINE index = j NEW_LINE DEDENT elif ( list1 [ j ] > list1 [ j - 1 ] and list1 [ j ] > list1 [ j + 1 ] and min > list1 [ j ] ) : NEW_LINE INDENT min = list1 [ j ] NEW_LINE index = j NEW_LINE DEDENT DEDENT list1 . pop ( index ) NEW_LINE result . append ( min ) NEW_LINE DEDENT print ( result ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 9 , 7 , 8 , 2 , 6 ] NEW_LINE minPeaks ( arr ) NEW_LINE DEDENT
Search an element in a reverse sorted array | Function to search if element X is present in reverse sorted array ; Store the first index of the subarray in which X lies ; Store the last index of the subarray in which X lies ; Store the middle index of the subarray ; Check if value at middle index of the subarray equal to X ; Element is found ; If X is smaller than the value at middle index of the subarray ; Search in right half of subarray ; Search in left half of subarray ; If X not found ; Driver Code
def binarySearch ( arr , N , X ) : NEW_LINE INDENT start = 0 NEW_LINE end = N NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = start + ( end - start ) // 2 NEW_LINE if ( X == arr [ mid ] ) : NEW_LINE INDENT return mid NEW_LINE DEDENT elif ( X < arr [ mid ] ) : NEW_LINE INDENT start = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT end = mid - 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 4 , 3 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE X = 5 NEW_LINE print ( binarySearch ( arr , N , X ) ) NEW_LINE DEDENT
Construct an AP series consisting of A and B having minimum possible Nth term | Python3 program for the above approach ; Function to check if both a and b are present in the AP series or not ; Iterate over the array arr [ ] ; If a is present ; If b is present ; If both are present ; Otherwise ; Function to print all the elements of the Arithmetic Progression ; Function to construct AP series consisting of A and B with minimum Nth term ; Stores the resultant series ; Initialise ans [ i ] as INT_MAX ; Maintain a smaller than b ; Swap a and b ; Difference between a and b ; Check for all possible combination of start and common difference d ; Initialise arr [ 0 ] as start ; Check if both a and b are present or not and the Nth term is the minimum or not ; Update the answer ; Print the resultant array ; Driver Code ; Function call
import sys NEW_LINE def check_both_present ( arr , N , a , b ) : NEW_LINE INDENT f1 = False NEW_LINE f2 = False NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if arr [ i ] == a : NEW_LINE INDENT f1 = True NEW_LINE DEDENT if arr [ i ] == b : NEW_LINE INDENT f2 = True NEW_LINE DEDENT DEDENT if f1 and f2 : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def print_array ( ans , N ) : NEW_LINE INDENT for i in range ( 0 , N ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def build_AP ( N , a , b ) : NEW_LINE INDENT INT_MAX = sys . maxsize NEW_LINE arr = [ None for i in range ( N ) ] NEW_LINE ans = [ INT_MAX for i in range ( N ) ] NEW_LINE flag = 0 NEW_LINE if a > b : NEW_LINE INDENT a , b = b , a NEW_LINE DEDENT diff = b - a NEW_LINE for start in range ( 1 , a + 1 ) : NEW_LINE INDENT for d in range ( 1 , diff + 1 ) : NEW_LINE INDENT arr [ 0 ] = start NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT arr [ i ] = arr [ i - 1 ] + d NEW_LINE DEDENT if ( ( check_both_present ( arr , N , a , b ) and arr [ N - 1 ] < ans [ N - 1 ] ) ) : NEW_LINE INDENT for i in range ( 0 , N ) : NEW_LINE INDENT ans [ i ] = arr [ i ] NEW_LINE DEDENT DEDENT DEDENT DEDENT print_array ( ans , N ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE A = 20 NEW_LINE B = 50 NEW_LINE build_AP ( N , A , B ) NEW_LINE DEDENT
Find the word from a given sentence having given word as prefix | Function to find the position of the string having word as prefix ; Initialize an List ; Extract words from the sentence ; Traverse each word ; Traverse the characters of word ; If prefix does not match ; Otherwise ; Return the word ; Return - 1 if not present ; Driver code
def isPrefixOfWord ( sentence , word ) : NEW_LINE INDENT a = sentence . split ( " ▁ " ) NEW_LINE v = [ ] NEW_LINE for i in a : NEW_LINE INDENT v . append ( i ) NEW_LINE DEDENT for i in range ( len ( v ) ) : NEW_LINE INDENT for j in range ( len ( v [ i ] ) ) : NEW_LINE INDENT if ( v [ i ] [ j ] != word [ j ] ) : NEW_LINE INDENT break NEW_LINE DEDENT elif ( j == len ( word ) - 1 ) : NEW_LINE INDENT return v [ i ] NEW_LINE DEDENT DEDENT DEDENT return - 1 NEW_LINE DEDENT s = " Welcome ▁ to ▁ Geeksforgeeks " NEW_LINE word = " Gee " NEW_LINE print ( isPrefixOfWord ( s , word ) ) NEW_LINE
Paths requiring minimum number of jumps to reach end of array | Python3 program to implement the above approach ; Pair Class instance ; Stores the current index ; Stores the path travelled so far ; Stores the minimum jumps required to reach the last index from current index ; Constructor ; Minimum jumps required to reach end of the array ; Stores the maximum number of steps that can be taken from the current index ; Checking if index stays within bounds ; Stores the minimum number of jumps required to jump from ( i + j ) - th index ; Function to find all possible paths to reach end of array requiring minimum number of steps ; Storing the neighbours of current index element ; Function to find the minimum steps and corresponding paths to reach end of an array ; Stores the minimum jumps from each position to last position ; Driver Code
from queue import Queue NEW_LINE import sys NEW_LINE class Pair ( object ) : NEW_LINE INDENT idx = 0 NEW_LINE psf = " " NEW_LINE jmps = 0 NEW_LINE def __init__ ( self , idx , psf , jmps ) : NEW_LINE INDENT self . idx = idx NEW_LINE self . psf = psf NEW_LINE self . jmps = jmps NEW_LINE DEDENT DEDENT def minJumps ( arr ) : NEW_LINE INDENT MAX_VALUE = sys . maxsize NEW_LINE dp = [ MAX_VALUE for i in range ( len ( arr ) ) ] NEW_LINE n = len ( dp ) NEW_LINE dp [ n - 1 ] = 0 NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT steps = arr [ i ] NEW_LINE minimum = MAX_VALUE NEW_LINE for j in range ( 1 , steps + 1 , 1 ) : NEW_LINE INDENT if i + j >= n : NEW_LINE INDENT break NEW_LINE DEDENT if ( ( dp [ i + j ] != MAX_VALUE ) and ( dp [ i + j ] < minimum ) ) : NEW_LINE INDENT minimum = dp [ i + j ] NEW_LINE DEDENT DEDENT if minimum != MAX_VALUE : NEW_LINE INDENT dp [ i ] = minimum + 1 NEW_LINE DEDENT DEDENT return dp NEW_LINE DEDENT def possiblePath ( arr , dp ) : NEW_LINE INDENT queue = Queue ( maxsize = 0 ) NEW_LINE p1 = Pair ( 0 , "0" , dp [ 0 ] ) NEW_LINE queue . put ( p1 ) NEW_LINE while queue . qsize ( ) > 0 : NEW_LINE INDENT tmp = queue . get ( ) NEW_LINE if tmp . jmps == 0 : NEW_LINE INDENT print ( tmp . psf ) NEW_LINE continue NEW_LINE DEDENT for step in range ( 1 , arr [ tmp . idx ] + 1 , 1 ) : NEW_LINE INDENT if ( ( tmp . idx + step < len ( arr ) ) and ( tmp . jmps - 1 == dp [ tmp . idx + step ] ) ) : NEW_LINE INDENT p2 = Pair ( tmp . idx + step , tmp . psf + " ▁ - > ▁ " + str ( ( tmp . idx + step ) ) , tmp . jmps - 1 ) NEW_LINE queue . put ( p2 ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def Solution ( arr ) : NEW_LINE INDENT dp = minJumps ( arr ) NEW_LINE possiblePath ( arr , dp ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 3 , 0 , 2 , 1 , 2 , 4 , 2 , 0 , 0 ] NEW_LINE size = len ( arr ) NEW_LINE Solution ( arr ) NEW_LINE DEDENT
Length of longest increasing absolute even subsequence | Function to find the longest increasing absolute even subsequence ; Length of arr ; Stores length of required subsequence ; Traverse the array ; Traverse prefix of current array element ; Check if the subsequence is LIS and have even absolute difference of adjacent pairs ; Update lis [ ] ; Stores maximum length ; Find the length of longest absolute even subsequence ; Return the maximum length of absolute even subsequence ; Given arr [ ] ; Function Call
def EvenLIS ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE lis = [ 1 ] * n NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( 0 , i ) : NEW_LINE INDENT if abs ( arr [ i ] ) > abs ( arr [ j ] ) and abs ( arr [ i ] % 2 ) == 0 and abs ( arr [ j ] % 2 ) == 0 and lis [ i ] < lis [ j ] + 1 : NEW_LINE INDENT lis [ i ] = lis [ j ] + 1 NEW_LINE DEDENT DEDENT DEDENT maxlen = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT maxlen = max ( maxlen , lis [ i ] ) NEW_LINE DEDENT print ( maxlen ) NEW_LINE DEDENT arr = [ 11 , - 22 , 43 , - 54 , 66 , 5 ] NEW_LINE EvenLIS ( arr ) NEW_LINE
Count balanced nodes present in a binary tree | Structure of a Tree Node ; Function to get the sum of left subtree and right subtree ; Base case ; Store the sum of left subtree ; Store the sum of right subtree ; Check if node is balanced or not ; Increase count of balanced nodes ; Return subtree sum ; Insert nodes in tree ; Store the count of balanced nodes
class Node : NEW_LINE INDENT def __init__ ( self , val ) : NEW_LINE INDENT self . data = val NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def Sum ( root ) : NEW_LINE INDENT global res NEW_LINE if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT leftSubSum = Sum ( root . left ) NEW_LINE rightSubSum = Sum ( root . right ) NEW_LINE if ( root . left and root . right and leftSubSum == rightSubSum ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT return ( root . data + leftSubSum + rightSubSum ) NEW_LINE DEDENT root = Node ( 9 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . left . left = Node ( - 1 ) NEW_LINE root . left . right = Node ( 3 ) NEW_LINE root . right = Node ( 4 ) NEW_LINE root . right . right = Node ( 0 ) NEW_LINE global res NEW_LINE res = 0 NEW_LINE Sum ( root ) NEW_LINE print ( res ) NEW_LINE
Print alternate nodes from all levels of a Binary Tree | Structure of a Node ; Print alternate nodes of a binary tree ; Store nodes of each level ; Store count of nodes of current level ; Print alternate nodes of the current level ; If left child exists ; Store left child ; If right child exists ; Store right child ; Driver Code ; Create a tree
class newNode : NEW_LINE INDENT def __init__ ( self , val ) : NEW_LINE INDENT self . data = val NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def PrintAlternate ( root ) : NEW_LINE INDENT Q = [ ] NEW_LINE Q . append ( root ) NEW_LINE while ( len ( Q ) ) : NEW_LINE INDENT N = len ( Q ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT temp = Q [ 0 ] NEW_LINE Q . remove ( Q [ 0 ] ) NEW_LINE if ( i % 2 == 0 ) : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE DEDENT if ( temp . left ) : NEW_LINE INDENT Q . append ( temp . left ) NEW_LINE DEDENT if ( temp . right ) : NEW_LINE INDENT Q . append ( temp . right ) NEW_LINE DEDENT DEDENT print ( " " , ▁ end ▁ = ▁ " " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 71 ) NEW_LINE root . left = newNode ( 88 ) NEW_LINE root . right = newNode ( 99 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . right . left = newNode ( 6 ) NEW_LINE root . right . right = newNode ( 7 ) NEW_LINE root . left . left . left = newNode ( 8 ) NEW_LINE root . left . left . right = newNode ( 9 ) NEW_LINE root . left . right . left = newNode ( 10 ) NEW_LINE root . left . right . right = newNode ( 11 ) NEW_LINE root . right . left . right = newNode ( 13 ) NEW_LINE root . right . right . left = newNode ( 14 ) NEW_LINE PrintAlternate ( root ) NEW_LINE DEDENT
Count subarrays for every array element in which they are the minimum | Function to required count subarrays ; For storing count of subarrays ; For finding next smaller element left to a element if there is no next smaller element left to it than taking - 1. ; For finding next smaller element right to a element if there is no next smaller element right to it than taking n . ; Taking exact boundaries in which arr [ i ] is minimum ; Similarly for right side ; Driver code ; Given array arr [ ] ; Function call
def countingSubarray ( arr , n ) : NEW_LINE INDENT a = [ 0 for i in range ( n ) ] NEW_LINE nsml = [ - 1 for i in range ( n ) ] NEW_LINE nsmr = [ n for i in range ( n ) ] NEW_LINE st = [ ] NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ( len ( st ) > 0 and arr [ st [ - 1 ] ] >= arr [ i ] ) : NEW_LINE INDENT del st [ - 1 ] NEW_LINE DEDENT if ( len ( st ) > 0 ) : NEW_LINE INDENT nsmr [ i ] = st [ - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT nsmr [ i ] = n NEW_LINE DEDENT st . append ( i ) NEW_LINE DEDENT while ( len ( st ) > 0 ) : NEW_LINE INDENT del st [ - 1 ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT while ( len ( st ) > 0 and arr [ st [ - 1 ] ] >= arr [ i ] ) : NEW_LINE INDENT del st [ - 1 ] NEW_LINE DEDENT if ( len ( st ) > 0 ) : NEW_LINE INDENT nsml [ i ] = st [ - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT nsml [ i ] = - 1 NEW_LINE DEDENT st . append ( i ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT nsml [ i ] += 1 NEW_LINE nsmr [ i ] -= 1 NEW_LINE r = nsmr [ i ] - i + 1 ; NEW_LINE l = i - nsml [ i ] + 1 ; NEW_LINE a [ i ] = r * l ; NEW_LINE DEDENT return a ; NEW_LINE DEDENT N = 5 NEW_LINE arr = [ 3 , 2 , 4 , 1 , 5 ] NEW_LINE a = countingSubarray ( arr , N ) NEW_LINE print ( a ) NEW_LINE
Lexicographically smallest permutation of a string that contains all substrings of another string | Function to reorder the B to contain all the substrings of A ; Find length of strings ; Initialize array to count the frequencies of the character ; Counting frequencies of character in B ; Find remaining character in B ; Declare the reordered string ; Loop until freq [ j ] > 0 ; Decrement the value from freq array ; Check if A [ j ] > A [ 0 ] ; Check if A [ j ] < A [ 0 ] ; Append the remaining characters to the end of the result ; Push all the values from frequency array in the answer ; Return the answer ; Driver Code ; Given strings A and B ; Function call
def reorderString ( A , B ) : NEW_LINE INDENT size_a = len ( A ) NEW_LINE size_b = len ( B ) NEW_LINE freq = [ 0 ] * 300 NEW_LINE for i in range ( size_b ) : NEW_LINE INDENT freq [ ord ( B [ i ] ) ] += 1 NEW_LINE DEDENT for i in range ( size_a ) : NEW_LINE INDENT freq [ ord ( A [ i ] ) ] -= 1 NEW_LINE DEDENT for j in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) : NEW_LINE INDENT if ( freq [ j ] < 0 ) : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT DEDENT answer = [ ] NEW_LINE for j in range ( ord ( ' a ' ) , ord ( A [ 0 ] ) ) : NEW_LINE INDENT while ( freq [ j ] > 0 ) : NEW_LINE INDENT answer . append ( j ) NEW_LINE freq [ j ] -= 1 NEW_LINE DEDENT DEDENT first = A [ 0 ] NEW_LINE for j in range ( size_a ) : NEW_LINE INDENT if ( A [ j ] > A [ 0 ] ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( A [ j ] < A [ 0 ] ) : NEW_LINE INDENT answer += A NEW_LINE A = " " NEW_LINE break NEW_LINE DEDENT DEDENT while ( freq [ ord ( first ) ] > 0 ) : NEW_LINE INDENT answer . append ( first ) NEW_LINE freq [ ord ( first ) ] -= 1 NEW_LINE DEDENT answer += A NEW_LINE for j in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) : NEW_LINE INDENT while ( freq [ j ] ) : NEW_LINE INDENT answer . append ( chr ( j ) ) NEW_LINE freq [ j ] -= 1 NEW_LINE DEDENT DEDENT return " " . join ( answer ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = " aa " NEW_LINE B = " ababab " NEW_LINE print ( reorderString ( A , B ) ) NEW_LINE DEDENT
Number from a range [ L , R ] having Kth minimum cost of conversion to 1 by given operations | Function to calculate the cost ; Base case ; Even condition ; Odd condition ; Return cost ; Function to find Kth element ; Array to store the costs ; Sort the array based on cost ; Given range and K ; Function Call
def func ( n ) : NEW_LINE INDENT count = 0 NEW_LINE if n == 2 or n == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT if n % 2 == 0 : NEW_LINE INDENT count = 1 + func ( n // 2 ) NEW_LINE DEDENT if n % 2 != 0 : NEW_LINE INDENT count = 1 + func ( n * 3 + 1 ) NEW_LINE DEDENT return count NEW_LINE DEDENT def findKthElement ( l , r , k ) : NEW_LINE INDENT arr = list ( range ( l , r + 1 ) ) NEW_LINE result = [ ] NEW_LINE for i in arr : NEW_LINE INDENT result . append ( [ i , func ( i ) ] ) NEW_LINE DEDENT result . sort ( ) NEW_LINE print ( result [ k - 1 ] [ 0 ] ) NEW_LINE DEDENT l = 12 NEW_LINE r = 15 NEW_LINE k = 2 NEW_LINE findKthElement ( l , r , k ) NEW_LINE
Check if a string represents a hexadecimal number or not | Function to check if the string represents a hexadecimal number ; Iterate over string ; Check if the character is invalid ; Print true if all characters are valid ; Given string ; Function call
def checkHex ( s ) : NEW_LINE INDENT for ch in s : NEW_LINE INDENT if ( ( ch < '0' or ch > '9' ) and ( ch < ' A ' or ch > ' F ' ) ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " Yes " ) NEW_LINE DEDENT s = " BF57C " NEW_LINE checkHex ( s ) NEW_LINE
Longest subsequence forming an Arithmetic Progression ( AP ) | Function that finds the longest arithmetic subsequence having the same absolute difference ; Stores the length of sequences having same difference ; Stores the resultant length ; Iterate over the array ; Update length of subsequence ; Return res ;  ; Given array arr [ ] ; Function Call
def lenghtOfLongestAP ( A , n ) : NEW_LINE INDENT dp = { } NEW_LINE res = 2 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT d = A [ j ] - A [ i ] NEW_LINE if d in dp : NEW_LINE INDENT if i in dp [ d ] : NEW_LINE INDENT dp [ d ] [ j ] = dp [ d ] [ i ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ d ] [ j ] = 2 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT dp [ d ] = { } NEW_LINE dp [ d ] [ j ] = 2 NEW_LINE DEDENT if d in dp : NEW_LINE INDENT if j in dp [ d ] : NEW_LINE INDENT res = max ( res , dp [ d ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT return res NEW_LINE DEDENT / * Driver Code * / NEW_LINE arr = [ 20 , 1 , 15 , 3 , 10 , 5 , 8 ] NEW_LINE N = len ( arr ) NEW_LINE print ( lenghtOfLongestAP ( arr , N ) ) NEW_LINE
Length of longest increasing prime subsequence from a given array | Python3 program for the above approach ; Function to find the prime numbers till 10 ^ 5 using Sieve of Eratosthenes ; False here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function which computes the length of the LIS of Prime Numbers ; Create an array of size n ; Create boolean array to mark prime numbers ; Precompute N primes ; Compute optimized LIS having prime numbers in bottom up manner ; check for LIS and prime ; Return maximum value in lis [ ] ; Driver Code ; Given array ; Size of array ; Function Call
N = 100005 NEW_LINE def SieveOfEratosthenes ( prime , p_size ) : NEW_LINE INDENT prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE p = 2 NEW_LINE while p * p <= p_size : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE for i in range ( p * 2 , p_size + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE p += 1 NEW_LINE DEDENT DEDENT DEDENT def LISPrime ( arr , n ) : NEW_LINE INDENT lisp = [ 0 ] * n NEW_LINE prime = [ True ] * ( N + 1 ) NEW_LINE SieveOfEratosthenes ( prime , N ) NEW_LINE if prime [ arr [ 0 ] ] : NEW_LINE INDENT lisp [ 0 ] = 1 NEW_LINE else : NEW_LINE lisp [ 0 ] = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( not prime [ arr [ i ] ] ) : NEW_LINE lisp [ i ] = 0 NEW_LINE continue NEW_LINE lisp [ i ] = 1 NEW_LINE for j in range ( i ) : NEW_LINE INDENT if ( prime [ arr [ j ] ] and arr [ i ] > arr [ j ] and lisp [ i ] < lisp [ j ] + 1 ) : NEW_LINE lisp [ i ] = lisp [ j ] + 1 NEW_LINE return max ( lisp ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 5 , 3 , 2 , 5 , 1 , 7 ] NEW_LINE M = len ( arr ) NEW_LINE print ( LISPrime ( arr , M ) ) NEW_LINE DEDENT
Check if a given number can be expressed as pair | Python3 program of the above approach ; Function to check if the number is pair - sum of sum of first X natural numbers ; Check if the given number is sum of pair of special numbers ; X is the sum of first i natural numbers ; t = 2 * Y ; Condition to check if Y is a special number ; Driver Code ; Function Call
import math NEW_LINE def checkSumOfNatural ( n ) : NEW_LINE INDENT i = 1 NEW_LINE flag = False NEW_LINE while i * ( i + 1 ) < n * 2 : NEW_LINE INDENT X = i * ( i + 1 ) NEW_LINE t = n * 2 - X NEW_LINE k = int ( math . sqrt ( t ) ) NEW_LINE if k * ( k + 1 ) == t : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if flag : NEW_LINE INDENT print ( ' YES ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' NO ' ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 25 NEW_LINE checkSumOfNatural ( n ) NEW_LINE DEDENT
Count distinct regular bracket sequences which are not N periodic | Function that finds the value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate the value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; Return the C ( n , k ) ; Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; Return C ( 2 n , n ) / ( n + 1 ) ; Function to find possible ways to put balanced parenthesis in an expression of length n ; If n is odd , not possible to create any valid parentheses ; Otherwise return n / 2 th Catalan Number ; Difference between counting ways of 2 * N and N is the result ; Given value of N ; Function call
def binomialCoeff ( n , k ) : NEW_LINE INDENT res = 1 NEW_LINE if ( k > n - k ) : NEW_LINE INDENT k = n - k NEW_LINE DEDENT for i in range ( k ) : NEW_LINE INDENT res = res * ( n - i ) NEW_LINE res = res // ( i + 1 ) NEW_LINE DEDENT return res NEW_LINE DEDENT def catalan ( n ) : NEW_LINE INDENT c = binomialCoeff ( 2 * n , n ) NEW_LINE return c // ( n + 1 ) NEW_LINE DEDENT def findWays ( n ) : NEW_LINE INDENT if ( ( n & 1 ) == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return catalan ( n // 2 ) NEW_LINE DEDENT def countNonNPeriodic ( N ) : NEW_LINE INDENT print ( findWays ( 2 * N ) - findWays ( N ) ) NEW_LINE DEDENT N = 4 NEW_LINE countNonNPeriodic ( N ) NEW_LINE