codeparrot/xlcost-text-to-code huggingface dataset:
Check if a large number is divisible by a number which is a power of 2 | Python3 program for the above approach ; Function to check divisibility ; Calculate the number of digits in num ; Check if the length of the string is less than the powerOf2 then return false ; Check if the powerOf2 is 0 that means the given number is 1 and as every number is divisible by 1 so return true ; Find the number which is formed by the last n digits of the string where n = powerOf2 ; Check if the number formed is divisible by input num or not ; Driver Code ; Given number ; Function Call
from math import log2 NEW_LINE def checkIfDivisible ( string , num ) : NEW_LINE INDENT powerOf2 = int ( log2 ( num ) ) ; NEW_LINE if ( len ( string ) < powerOf2 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT if ( powerOf2 == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT number = 0 ; NEW_LINE length = len ( string ) ; NEW_LINE for i in range ( length - powerOf2 , length ) : NEW_LINE INDENT number += ( ( ord ( string [ i ] ) - ord ( '0' ) ) * ( 10 ** ( powerOf2 - 1 ) ) ) ; NEW_LINE powerOf2 -= 1 ; NEW_LINE DEDENT if ( number % num ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT else : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "213467756564" ; NEW_LINE num = 4 ; NEW_LINE if ( checkIfDivisible ( string , num ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Check if given permutation of 1 to N can be counted in clockwise or anticlockwise direction | Python3 program to check clockwise or counterclockwise order in an array ; Comparing the first and last value of array ; If the count is greater than 1 then it can 't be  represented in required order ; Driver code
def check_order ( arr ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( len ( arr ) - 1 ) : NEW_LINE INDENT if ( abs ( arr [ i + 1 ] - arr [ i ] ) > 1 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT if ( abs ( arr [ 0 ] - arr [ len ( arr ) - 1 ] ) > 1 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT if ( cnt > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT arr = [ 2 , 3 , 4 , 5 , 1 ] NEW_LINE if ( check_order ( arr ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Largest number M less than N such that XOR of M and N is even | Function to find the maximum possible value of M ; Edge case ; M = N - 2 is maximum possible value ; Driver code
def getM ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return n - 2 ; NEW_LINE DEDENT DEDENT n = 10 NEW_LINE print ( getM ( n ) ) NEW_LINE
Maximize profit in buying and selling stocks with Rest condition | Python3 program for the above problem ; If there is only one day for buying and selling no profit can be made ; Array to store Maxprofit by resting on given day ; Array to store Maxprofit by buying or resting on the given day ; Array to store Maxprofit by selling on given day ; Initially there will 0 profit ; Buying on 1 st day results in negative profit ; zero profit since selling before buying isn 't possible ; max of profit on ( i - 1 ) th day by resting and profit on ( i - 1 ) th day by selling . ; max of profit by resting on ith day and buying on ith day . ; max of profit by selling on ith day ; maxprofit ; Driver Code
def maxProfit ( prices , n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT rest = [ 0 ] * n NEW_LINE hold = [ 0 ] * n NEW_LINE sold = [ 0 ] * n NEW_LINE rest [ 0 ] = 0 NEW_LINE hold [ 0 ] = - prices [ 0 ] NEW_LINE sold [ 0 ] = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT rest [ i ] = max ( rest [ i - 1 ] , sold [ i - 1 ] ) NEW_LINE hold [ i ] = max ( hold [ i - 1 ] , rest [ i - 1 ] - prices [ i ] ) NEW_LINE sold [ i ] = hold [ i - 1 ] + prices [ i ] NEW_LINE DEDENT return max ( rest [ n - 1 ] , sold [ n - 1 ] ) NEW_LINE DEDENT price = [ 2 , 4 , 5 , 0 , 2 ] NEW_LINE n = len ( price ) NEW_LINE print ( maxProfit ( price , n ) ) NEW_LINE
Count of subarrays of size K having at least one pair with absolute difference divisible by K | Function to return the required number of subarrays ; Return number of possible subarrays of length K ; Driver Code
def findSubarrays ( arr , N , K ) : NEW_LINE INDENT return N - K + 1 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 5 , 3 , 2 , 17 , 18 ] ; NEW_LINE K = 4 ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( findSubarrays ( arr , N , K ) ) ; NEW_LINE DEDENT
Maximum length of subarray such that all elements are equal in the subarray | Function to find the longest subarray with same element ; Check if the elements are same then we can increment the length ; Reinitialize j ; Compare the maximum length e with j ; Return max length ; Given list arr [ ] ; Function call
def longest_subarray ( arr , d ) : NEW_LINE INDENT ( i , j , e ) = ( 0 , 1 , 0 ) NEW_LINE for i in range ( d - 1 ) : NEW_LINE INDENT if arr [ i ] == arr [ i + 1 ] : NEW_LINE INDENT j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT j = 1 NEW_LINE DEDENT if e < j : NEW_LINE INDENT e = j NEW_LINE DEDENT DEDENT return e NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( longest_subarray ( arr , N ) ) NEW_LINE
Print all Strong numbers less than or equal to N | Store the factorial of all the digits from [ 0 , 9 ] ; Function to return true if number is strong or not ; Converting N to String so that can easily access all it 's digit ; sum will store summation of factorial of all digits of a number N ; Returns true of N is strong number ; Function to print all strong number till N ; Iterating from 1 to N ; Checking if a number is strong then print it ; Driver Code ; Given number ; Function call
factorial = [ 1 , 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 ] NEW_LINE def isStrong ( N ) : NEW_LINE INDENT num = str ( N ) NEW_LINE sum = 0 NEW_LINE for i in range ( len ( num ) ) : NEW_LINE INDENT sum += factorial [ ord ( num [ i ] ) - ord ( '0' ) ] NEW_LINE DEDENT if sum == N : NEW_LINE return True NEW_LINE else : NEW_LINE return False NEW_LINE DEDENT def printStrongNumbers ( N ) : NEW_LINE INDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( isStrong ( i ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 1000 NEW_LINE printStrongNumbers ( N ) NEW_LINE DEDENT
Count of pairs in a given range with sum of their product and sum equal to their concatenated number | Function for counting pairs ; Count possible values of Y ; Driver Code
def countPairs ( A , B ) : NEW_LINE INDENT countY = 0 NEW_LINE countX = ( B - A ) + 1 NEW_LINE next_val = 9 NEW_LINE while ( next_val <= B ) : NEW_LINE INDENT if ( next_val >= A ) : NEW_LINE INDENT countY += 1 NEW_LINE DEDENT next_val = next_val * 10 + 9 NEW_LINE DEDENT return ( countX * countY ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 1 NEW_LINE B = 16 NEW_LINE print ( countPairs ( A , B ) ) NEW_LINE DEDENT
Minimum sprinklers required to water a rectangular park | Function to find the minimum number sprinklers required to water the park . ; General requirements of sprinklers ; if M is odd then add one additional sprinklers ; Driver code
def solve ( N , M ) : NEW_LINE INDENT ans = int ( ( N ) * int ( M / 2 ) ) NEW_LINE if ( M % 2 == 1 ) : NEW_LINE INDENT ans += int ( ( N + 1 ) / 2 ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT N = 5 NEW_LINE M = 3 NEW_LINE solve ( N , M ) NEW_LINE
Queries to find frequencies of a string within specified substrings | Python3 Program to find frequency of a string K in a substring [ L , R ] in S ; Store the frequency of string for each index ; Compute and store frequencies for every index ; Driver Code
max_len = 100005 NEW_LINE cnt = [ 0 ] * max_len NEW_LINE def precompute ( s , K ) : NEW_LINE INDENT n = len ( s ) NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT cnt [ i + 1 ] = cnt [ i ] NEW_LINE if s [ i : len ( K ) + i ] == K : NEW_LINE INDENT cnt [ i + 1 ] += 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " ABCABCABABC " NEW_LINE K = " ABC " NEW_LINE precompute ( s , K ) NEW_LINE Q = [ [ 1 , 6 ] , [ 5 , 11 ] ] NEW_LINE for it in Q : NEW_LINE INDENT print ( cnt [ it [ 1 ] - 1 ] - cnt [ it [ 0 ] - 1 ] ) NEW_LINE DEDENT DEDENT
XOR of elements in a given range with updates using Fenwick Tree | Returns XOR of arr [ 0. . index ] . This function assumes that the array is preprocessed and partial XORs of array elements are stored in BITree [ ] . ; Traverse ancestors of BITree [ index ] ; XOR current element of BIT to ans ; Update index to that of the parent node in getXor ( ) view by subtracting LSB ( Least Significant Bit ) ; Updates the Binary Index Tree by replacing all ancestors of index by their respective XOR with val ; Traverse all ancestors and XOR with ' val ' . ; XOR ' val ' to current node of BIT ; Update index to that of the parent node in updateBit ( ) view by adding LSB ( Least Significant Bit ) ; Constructs and returns a Binary Indexed Tree for the given array ; Create and initialize the Binary Indexed Tree ; Store the actual values in BITree [ ] using update ( ) ; Driver Code ; Create the Binary Indexed Tree ; Solve each query in Q ; Update the values of all ancestors of idx
def getXOR ( BITree , index ) : NEW_LINE INDENT ans = 0 NEW_LINE index += 1 NEW_LINE while ( index > 0 ) : NEW_LINE INDENT ans ^= BITree [ index ] NEW_LINE index -= index & ( - index ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def updateBIT ( BITree , n , index , val ) : NEW_LINE INDENT index = index + 1 NEW_LINE while ( index <= n ) : NEW_LINE INDENT BITree [ index ] ^= val NEW_LINE index += index & ( - index ) NEW_LINE DEDENT DEDENT def constructBITree ( arr , n ) : NEW_LINE INDENT BITree = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT updateBIT ( BITree , n , i , arr [ i ] ) NEW_LINE DEDENT return BITree NEW_LINE DEDENT def rangeXor ( BITree , l , r ) : NEW_LINE INDENT return ( getXOR ( BITree , r ) ^ getXOR ( BITree , l - 1 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 2 , 1 , 1 , 3 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] NEW_LINE n = len ( A ) NEW_LINE q = [ [ 1 , 0 , 9 ] , [ 2 , 3 , 6 ] , [ 2 , 5 , 5 ] , [ 2 , 8 , 1 ] , [ 1 , 0 , 9 ] ] NEW_LINE BITree = constructBITree ( A , n ) NEW_LINE for i in range ( len ( q ) ) : NEW_LINE INDENT id = q [ i ] [ 0 ] NEW_LINE if ( id == 1 ) : NEW_LINE INDENT L = q [ i ] [ 1 ] NEW_LINE R = q [ i ] [ 2 ] NEW_LINE print ( " XOR ▁ of ▁ elements ▁ in ▁ " " given ▁ range ▁ is ▁ " , rangeXor ( BITree , L , R ) ) NEW_LINE DEDENT else : NEW_LINE INDENT idx = q [ i ] [ 1 ] NEW_LINE val = q [ i ] [ 2 ] NEW_LINE A [ idx ] ^= val NEW_LINE updateBIT ( BITree , n , idx , val ) NEW_LINE DEDENT DEDENT DEDENT
Count of Double Prime numbers in a given range L to R | Array to make Sieve where arr [ i ] = 0 indicates non prime and arr [ i ] = 1 indicates prime ; Array to find double prime ; Function to find the number double prime numbers in range ; Assume all numbers as prime ; Check if the number is prime ; Check for multiples of i ; Make all multiples of ith prime as non - prime ; Check if number at ith position is prime then increment count ; Indicates count of numbers from 1 to i that are also prime and hence double prime ; If number is not a double prime ; Finding cumulative sum ; Driver code
arr = [ 0 ] * 1000001 NEW_LINE dp = [ 0 ] * 1000001 NEW_LINE def count ( ) : NEW_LINE INDENT maxN = 1000000 NEW_LINE for i in range ( 0 , maxN ) : NEW_LINE INDENT arr [ i ] = 1 NEW_LINE DEDENT arr [ 0 ] = 0 NEW_LINE arr [ 1 ] = 0 NEW_LINE i = 2 NEW_LINE while ( i * i <= maxN ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT for j in range ( 2 * i , maxN + 1 , i ) : NEW_LINE INDENT arr [ j ] = 0 NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT cnt = 0 NEW_LINE for i in range ( 0 , maxN + 1 ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT if ( arr [ cnt ] == 1 ) : NEW_LINE INDENT dp [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] = 0 NEW_LINE DEDENT DEDENT for i in range ( 0 , maxN + 1 ) : NEW_LINE INDENT dp [ i ] += dp [ i - 1 ] NEW_LINE DEDENT DEDENT L = 4 NEW_LINE R = 12 NEW_LINE count ( ) NEW_LINE print ( dp [ R ] - dp [ L - 1 ] ) NEW_LINE
Count of ungrouped characters after dividing a string into K groups of distinct characters | Python3 code to implement the above approach ; Create array where index represents alphabets ; Fill count of every alphabet to corresponding array index ; Count for every element how much is exceeding from no . of groups then sum them ; Print answer ; Driver code
def findUngroupedElement ( s , k ) : NEW_LINE INDENT n = len ( s ) ; NEW_LINE b = [ 0 ] * 26 NEW_LINE for i in range ( n ) : NEW_LINE INDENT p = s [ i ] NEW_LINE b [ ord ( p ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT sum = 0 ; NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( b [ i ] > k ) : NEW_LINE INDENT sum += b [ i ] - k NEW_LINE DEDENT DEDENT print ( sum ) NEW_LINE DEDENT s = " stayinghomesaveslife " NEW_LINE k = 1 NEW_LINE findUngroupedElement ( s , k ) NEW_LINE
Generate an array of given size with equal count and sum of odd and even numbers | Function to find the array such that the array contains the same count of even and odd elements with equal sum of even and odd elements ; Length of array which is not divisible by 4 is unable to form such array ; Loop to find the resulted array containing the same count of even and odd elements ; Find the total sum of even elements ; Find the total sum of odd elements ; Find the difference between the total sum of even and odd elements ; The difference will be added in the last odd element ; Driver Code
def findSolution ( N ) : NEW_LINE INDENT if ( N % 4 != 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT temp = 0 NEW_LINE sum_odd = 0 NEW_LINE sum_even = 0 NEW_LINE result = [ 0 ] * N NEW_LINE for i in range ( 0 , N , 2 ) : NEW_LINE INDENT temp += 2 NEW_LINE result [ i + 1 ] = temp NEW_LINE sum_even += result [ i + 1 ] NEW_LINE result [ i ] = temp - 1 NEW_LINE sum_odd += result [ i ] NEW_LINE DEDENT diff = sum_even - sum_odd NEW_LINE result [ N - 2 ] += diff NEW_LINE for i in range ( N ) : NEW_LINE INDENT print ( result [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT N = 8 ; NEW_LINE findSolution ( N ) NEW_LINE
Number of ways to place two queens on a N * N chess | Python3 implementation to find the number of ways to place two queens on the N * N chess board ; Function to find number of valid positions for two queens in the N * N chess board ; Driver code ; Function call
import math NEW_LINE def possiblePositions ( n ) : NEW_LINE INDENT term1 = pow ( n , 4 ) ; NEW_LINE term2 = pow ( n , 3 ) ; NEW_LINE term3 = pow ( n , 2 ) ; NEW_LINE term4 = n / 3 ; NEW_LINE ans = ( ( math . ceil ( term1 ) ) / 2 - ( math . ceil ( 5 * term2 ) ) / 3 + ( math . ceil ( 3 * term3 ) ) / 2 - term4 ) ; NEW_LINE return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE ans = possiblePositions ( n ) NEW_LINE print ( int ( ans ) ) NEW_LINE DEDENT
Group all co | Function to group the mutually co - prime numbers into one group ; Loop for the numbers less than the 4 ; Integers 1 , 2 and 3 can be grouped into one group ; Consecutive even and odd numbers ; Driver Code ; Function Call
def mutually_coprime ( n ) : NEW_LINE INDENT if ( n <= 3 ) : NEW_LINE INDENT for j in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( j , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( 1 , 2 , 3 ) NEW_LINE for j in range ( 4 , n , 2 ) : NEW_LINE INDENT print ( j , ( j + 1 ) ) NEW_LINE DEDENT if ( n % 2 == 0 ) : NEW_LINE INDENT print ( n ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 9 NEW_LINE mutually_coprime ( n ) NEW_LINE DEDENT
Maximum sum subset having equal number of positive and negative elements | Function to find maximum sum subset with equal number of positive and negative elements ; Loop to store the positive and negative elements in two different array ; Sort both the array ; Pointers starting from the highest elements ; Find pairs having sum greater than zero ; Driver code
def findMaxSum ( arr , n ) : NEW_LINE INDENT a = [ ] NEW_LINE b = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT a . append ( arr [ i ] ) NEW_LINE DEDENT elif ( arr [ i ] < 0 ) : NEW_LINE INDENT b . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT a . sort ( ) NEW_LINE b . sort ( ) NEW_LINE p = len ( a ) - 1 NEW_LINE q = len ( b ) - 1 NEW_LINE s = 0 NEW_LINE while ( p >= 0 and q >= 0 ) : NEW_LINE INDENT if ( a [ p ] + b [ q ] > 0 ) : NEW_LINE INDENT s = s + a [ p ] + b [ q ] NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT p = p - 1 NEW_LINE q = q - 1 NEW_LINE DEDENT return s NEW_LINE DEDENT arr1 = [ 1 , - 2 , 3 , 4 , - 5 , 8 ] NEW_LINE n1 = len ( arr1 ) NEW_LINE print ( findMaxSum ( arr1 , n1 ) ) NEW_LINE
Print the nodes with a prime degree in given Prufer sequence of a Tree | Function to create Sieve to check primes ; 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 to print the nodes with prime degree in the tree whose Prufer sequence is given ; Hash - table to mark the degree of every node ; Initially let all the degrees be 1 ; Increase the count of the degree ; Print the nodes with prime degree ; Driver Code
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 INDENT for i in range ( p * 2 , p_size + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def PrimeDegreeNodes ( prufer , n ) : NEW_LINE INDENT nodes = n + 2 NEW_LINE prime = [ True ] * ( nodes + 1 ) NEW_LINE SieveOfEratosthenes ( prime , nodes + 1 ) NEW_LINE degree = [ 0 ] * ( n + 2 + 1 ) ; NEW_LINE for i in range ( 1 , nodes + 1 ) : NEW_LINE INDENT degree [ i ] = 1 ; NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT degree [ prufer [ i ] ] += 1 NEW_LINE DEDENT for i in range ( 1 , nodes + 1 ) : NEW_LINE INDENT if prime [ degree [ i ] ] : NEW_LINE INDENT print ( i , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 4 , 1 , 3 , 4 ] NEW_LINE n = len ( a ) NEW_LINE PrimeDegreeNodes ( a , n ) NEW_LINE DEDENT
Find K 'th smallest number such that A + B = A | B | Function to find k 'th smallest number such that A + B = A | B ; res will store final answer ; Skip when j ' th ▁ position ▁ ▁ has ▁ 1 ▁ in ▁ binary ▁ representation ▁ ▁ as ▁ in ▁ res , ▁ j ' th position will be 0. ; j 'th bit is set ; If i ' th ▁ bit ▁ of ▁ k ▁ is ▁ 1 ▁ ▁ and ▁ i ' th bit of j is 0 then set i 'th bit in res. ; Proceed to next bit ; Driver Code
def kthSmallest ( a , k ) : NEW_LINE INDENT res = 0 NEW_LINE j = 0 NEW_LINE for i in range ( 32 ) : NEW_LINE INDENT while ( j < 32 and ( a & ( 1 << j ) ) ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT if ( k & ( 1 << i ) ) : NEW_LINE INDENT res |= ( 1 << j ) NEW_LINE DEDENT j += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT a = 5 NEW_LINE k = 3 NEW_LINE print ( kthSmallest ( a , k ) ) NEW_LINE
Nodes with prime degree in an undirected Graph | Python3 implementation of the above approach ; To store Prime Numbers ; Function to find the prime numbers till 10 ^ 5 ; Traverse all multiple of i and make it false ; Function to print the nodes having prime degree ; To store Adjacency List of a Graph ; Make Adjacency List ; To precompute prime numbers till 10 ^ 5 ; Traverse each vertex ; Find size of Adjacency List ; If length of Adj [ i ] is Prime then print it ; Driver code ; Vertices and Edges ; Edges ; Function Call
n = 10005 ; NEW_LINE Prime = [ True for i in range ( n + 1 ) ] NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT i = 2 NEW_LINE Prime [ 0 ] = Prime [ 1 ] = False ; NEW_LINE while i * i <= 10005 : NEW_LINE INDENT if ( Prime [ i ] ) : NEW_LINE INDENT for j in range ( 2 * i , 10005 ) : NEW_LINE INDENT Prime [ j ] = False NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT DEDENT def primeDegreeNodes ( N , M , edges ) : NEW_LINE INDENT Adj = [ [ ] for i in range ( N + 1 ) ] ; NEW_LINE for i in range ( M ) : NEW_LINE INDENT x = edges [ i ] [ 0 ] ; NEW_LINE y = edges [ i ] [ 1 ] ; NEW_LINE Adj [ x ] . append ( y ) ; NEW_LINE Adj [ y ] . append ( x ) ; NEW_LINE DEDENT SieveOfEratosthenes ( ) ; NEW_LINE for i in range ( N + 1 ) : NEW_LINE INDENT x = len ( Adj [ i ] ) ; NEW_LINE if ( Prime [ x ] ) : NEW_LINE INDENT print ( i , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE M = 6 NEW_LINE edges = [ [ 1 , 2 ] , [ 1 , 3 ] , [ 1 , 4 ] , [ 2 , 3 ] , [ 2 , 4 ] , [ 3 , 4 ] ] ; NEW_LINE primeDegreeNodes ( N , M , edges ) ; NEW_LINE DEDENT
Number of ways to color N | Python3 program for the above approach ; Recursive function to count the ways ; Base case ; Initialise answer to 0 ; Color each uncolored block according to the given condition ; If any block is uncolored ; Check if adjacent blocks are colored or not ; Color the block ; recursively iterate for next uncolored block ; Uncolored for the next recursive call ; Return the final count ; Function to count the ways to color block ; Mark which blocks are colored in each recursive step ; Function call to count the ways ; Driver Code ; Number of blocks ; Number of colored blocks ; Function call
mod = 1000000007 NEW_LINE def countWays ( colored , count , n ) : NEW_LINE INDENT if ( count == n ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT answer = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( colored [ i ] == 0 ) : NEW_LINE INDENT if ( colored [ i - 1 ] == 1 or colored [ i + 1 ] == 1 ) : NEW_LINE INDENT colored [ i ] = 1 NEW_LINE answer = ( ( answer + countWays ( colored , count + 1 , n ) ) % mod ) NEW_LINE colored [ i ] = 0 NEW_LINE DEDENT DEDENT DEDENT return answer NEW_LINE DEDENT def waysToColor ( arr , n , k ) : NEW_LINE INDENT colored = [ 0 ] * ( n + 2 ) NEW_LINE for i in range ( k ) : NEW_LINE INDENT colored [ arr [ i ] ] = 1 NEW_LINE DEDENT return countWays ( colored , k , n ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 NEW_LINE K = 3 NEW_LINE arr = [ 1 , 2 , 6 ] NEW_LINE print ( waysToColor ( arr , N , K ) ) NEW_LINE DEDENT
Shortest Palindromic Substring | Function return the shortest palindromic substring ; Finding the smallest character present in the string ; Driver code
def ShortestPalindrome ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE ans = s [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT ans = min ( ans , s [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT s = " geeksforgeeks " NEW_LINE print ( ShortestPalindrome ( s ) ) NEW_LINE
Maximum length Subsequence with alternating sign and maximum Sum | Function to find the subsequence with alternating sign having maximum size and maximum sum . ; Find whether each element is positive or negative ; Find the required subsequence ; Find the maximum element in the specified range ; print the result ; Driver code ; array declaration ; size of array
def findSubsequence ( arr , n ) : NEW_LINE INDENT sign = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT sign [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT sign [ i ] = - 1 NEW_LINE DEDENT DEDENT k = 0 NEW_LINE result = [ 0 ] * n NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE INDENT cur = arr [ i ] NEW_LINE j = i NEW_LINE while ( j < n and sign [ i ] == sign [ j ] ) : NEW_LINE INDENT cur = max ( cur , arr [ j ] ) NEW_LINE j += 1 NEW_LINE DEDENT result [ k ] = cur NEW_LINE k += 1 NEW_LINE i = j - 1 NEW_LINE i += 1 NEW_LINE DEDENT for i in range ( k ) : NEW_LINE INDENT print ( result [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 4 , 9 , 4 , 11 , - 5 , - 17 , 9 , - 3 , - 5 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE findSubsequence ( arr , n ) NEW_LINE DEDENT