codeparrot/xlcost-text-to-code huggingface dataset:
Generate K co | Python3 implementation of the above approach ; Function prints the required pairs ; First co - prime pair ; As a pair ( 1 n ) has already been Printed ; If i is a factor of N ; Since ( i , i ) won 't form  a coprime pair ; Driver Code
from math import sqrt NEW_LINE def FindPairs ( n , k ) : NEW_LINE INDENT print ( 1 , n ) NEW_LINE k -= 1 NEW_LINE for i in range ( 2 , int ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT print ( 1 , i ) NEW_LINE k -= 1 NEW_LINE if ( k == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( i != n // i ) : NEW_LINE INDENT print ( 1 , n // i ) NEW_LINE k -= 1 NEW_LINE DEDENT if ( k == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 100 NEW_LINE K = 5 NEW_LINE FindPairs ( N , K ) NEW_LINE DEDENT
Count of subarrays which forms a permutation from given Array elements | Function returns the required count ; Store the indices of the elements present in A [ ] . ; Store the maximum and minimum index of the elements from 1 to i . ; Update maxi and mini , to store minimum and maximum index for permutation of elements from 1 to i + 1 ; If difference between maxi and mini is equal to i ; Increase count ; Return final count ; Driver Code
def PermuteTheArray ( A , n ) : NEW_LINE INDENT arr = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ A [ i ] - 1 ] = i NEW_LINE DEDENT mini = n NEW_LINE maxi = 0 NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT mini = min ( mini , arr [ i ] ) NEW_LINE maxi = max ( maxi , arr [ i ] ) NEW_LINE if ( maxi - mini == i ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 4 , 5 , 1 , 3 , 2 , 6 ] NEW_LINE print ( PermuteTheArray ( A , 6 ) ) NEW_LINE DEDENT
Find Second largest element in an array | Set 2 | Function to find the largest element in the array arr [ ] ; Base Condition ; Initialize an empty list ; Divide the array into two equal length subarrays and recursively find the largest among the two ; Store length of compared1 [ ] in the first index ; Store the maximum element ; Return compared1 which contains the maximum element ; Store length of compared2 [ ] in the first index ; Store the maximum element ; Return compared2 [ ] which contains the maximum element ; Function to print the second largest element in the array arr [ ] ; Find the largest element in arr [ ] ; Find the second largest element in arr [ ] ; Print the second largest element ; Driver Code
def findLargest ( beg , end , arr , n ) : NEW_LINE INDENT if ( beg == end ) : NEW_LINE INDENT compared = [ 0 ] * n NEW_LINE compared [ 0 ] = 1 NEW_LINE compared [ 1 ] = arr [ beg ] NEW_LINE return compared NEW_LINE DEDENT compared1 = findLargest ( beg , ( beg + end ) // 2 , arr , n ) NEW_LINE compared2 = findLargest ( ( beg + end ) // 2 + 1 , end , arr , n ) NEW_LINE if ( compared1 [ 1 ] > compared2 [ 1 ] ) : NEW_LINE INDENT k = compared1 [ 0 ] + 1 NEW_LINE compared1 [ 0 ] = k NEW_LINE compared1 [ k ] = compared2 [ 1 ] NEW_LINE return compared1 NEW_LINE DEDENT else : NEW_LINE INDENT k = compared2 [ 0 ] + 1 NEW_LINE compared2 [ 0 ] = k NEW_LINE compared2 [ k ] = compared1 [ 1 ] NEW_LINE return compared2 NEW_LINE DEDENT DEDENT def findSecondLargest ( end , arr ) : NEW_LINE INDENT compared1 = findLargest ( 0 , end - 1 , arr , end ) NEW_LINE compared2 = findLargest ( 2 , compared1 [ 0 ] + 2 , compared1 , compared1 [ 0 ] ) NEW_LINE print ( compared2 [ 1 ] ) NEW_LINE DEDENT N = 10 NEW_LINE arr = [ 20 , 1990 , 12 , 1110 , 1 , 59 , 12 , 15 , 120 , 1110 ] NEW_LINE findSecondLargest ( N , arr ) NEW_LINE
Count of Reverse Bitonic Substrings in a given String | Function to calculate the number of reverse bitonic substrings ; Stores the count ; All possible lengths of substrings ; Starting poof a substring ; Ending poof a substring ; Condition for reverse bitonic substrings of length 1 ; Check for decreasing sequence ; If end of substring is reache ; For increasing sequence ; If end of substring is reached ; Return the number of bitonic substrings ; Driver Code
def CountsubString ( strr , n ) : NEW_LINE INDENT c = 0 NEW_LINE for len in range ( n + 1 ) : NEW_LINE INDENT for i in range ( n - len ) : NEW_LINE INDENT j = i + len - 1 NEW_LINE temp = strr [ i ] NEW_LINE f = 0 NEW_LINE if ( j == i ) : NEW_LINE INDENT c += 1 NEW_LINE continue NEW_LINE DEDENT k = i + 1 NEW_LINE while ( k <= j and temp > strr [ k ] ) : NEW_LINE INDENT temp = strr [ k ] NEW_LINE k += 1 NEW_LINE DEDENT if ( k > j ) : NEW_LINE INDENT c += 1 NEW_LINE f = 2 NEW_LINE DEDENT while ( k <= j and f != 2 and temp < strr [ k ] ) : NEW_LINE INDENT temp = strr [ k ] NEW_LINE k += 1 NEW_LINE DEDENT if ( k > j and f != 2 ) : NEW_LINE INDENT c += 1 NEW_LINE f = 0 NEW_LINE DEDENT DEDENT DEDENT return c NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT strr = " bade " NEW_LINE print ( CountsubString ( strr , len ( strr ) ) ) NEW_LINE DEDENT
Maximum subsequence sum from a given array which is a perfect square | Python3 program to implement the above approach ; If sum is 0 , then answer is true ; If sum is not 0 and arr [ ] is empty , then answer is false ; Fill the subset table in bottom up manner ; Function to find the sum ; Find sum of all values ; Return the value ; ; Driver Code
import math NEW_LINE def isSubsetSum ( arr , n , sum ) : NEW_LINE INDENT subset = [ [ True for x in range ( sum + 1 ) ] for y in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT subset [ i ] [ 0 ] = True NEW_LINE DEDENT for i in range ( 1 , sum + 1 ) : NEW_LINE INDENT subset [ 0 ] [ i ] = False NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , sum + 1 ) : NEW_LINE INDENT if ( j < arr [ i - 1 ] ) : NEW_LINE INDENT subset [ i ] [ j ] = subset [ i - 1 ] [ j ] NEW_LINE DEDENT if ( j >= arr [ i - 1 ] ) : NEW_LINE INDENT subset [ i ] [ j ] = ( subset [ i - 1 ] [ j ] or subset [ i - 1 ] [ j - arr [ i - 1 ] ] ) NEW_LINE DEDENT DEDENT DEDENT return subset [ n ] [ sum ] NEW_LINE DEDENT def findSum ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT val = int ( math . sqrt ( sum ) ) NEW_LINE for i in range ( val , - 1 , - 1 ) : NEW_LINE INDENT if ( isSubsetSum ( arr , n , i * i ) ) : NEW_LINE INDENT return i * i NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findSum ( arr , n ) ) NEW_LINE DEDENT
Smallest subarray whose product leaves remainder K when divided by size of the array | Function to find the subarray of minimum length ; Initialize the minimum subarray size to N + 1 ; Generate all possible subarray ; Initialize the product ; Find the product ; Return the minimum size of subarray ; Driver code ; Given array
def findsubArray ( arr , N , K ) : NEW_LINE INDENT res = N + 1 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT curr_prad = 1 NEW_LINE for j in range ( i , N ) : NEW_LINE INDENT curr_prad = curr_prad * arr [ j ] NEW_LINE if ( curr_prad % N == K and res > ( j - i + 1 ) ) : NEW_LINE INDENT res = min ( res , j - i + 1 ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if res == N + 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return res NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 2 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE K = 1 NEW_LINE answer = findsubArray ( arr , N , K ) NEW_LINE if ( answer != 0 ) : NEW_LINE INDENT print ( answer ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT
Count of array elements whose order of deletion precedes order of insertion | Python3 Program to implement the above approach ; Function returns maximum number of required elements ; Insert the elements of array B in the queue and set ; Stores the answer ; If A [ i ] is already processed ; Until we find A [ i ] in the queue ; Remove elements from the queue ; Increment the count ; Remove the current element A [ i ] from the queue and set . ; Return total count ; Driver code
import queue NEW_LINE def maximumCount ( A , B , n ) : NEW_LINE INDENT q = queue . Queue ( ) NEW_LINE s = set ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT s . add ( B [ i ] ) NEW_LINE q . put ( B [ i ] ) NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( A [ i ] not in s ) : NEW_LINE INDENT continue NEW_LINE DEDENT while ( q . qsize ( ) > 0 and q . queue [ 0 ] != A [ i ] ) : NEW_LINE INDENT s . remove ( q . queue [ 0 ] ) ; NEW_LINE q . get ( ) NEW_LINE count += 1 NEW_LINE DEDENT if ( A [ i ] == q . queue [ 0 ] ) : NEW_LINE INDENT q . get ( ) NEW_LINE s . remove ( A [ i ] ) NEW_LINE DEDENT if ( q . qsize ( ) == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT N = 4 NEW_LINE A = [ 1 , 2 , 3 , 4 ] NEW_LINE B = [ 1 , 2 , 4 , 3 ] NEW_LINE maximumCount ( A , B , N ) NEW_LINE
Check if a Matrix is Bitonic or not | Python3 program to check if a matrix is Bitonic or not ; Function to check if an array is Bitonic or not ; Check for increasing sequence ; Check for decreasing sequence ; Function to check whether given matrix is bitonic or not ; Check row wise ; Check column wise ; Generate an array consisting of elements of current column ; Driver Code
N = 3 NEW_LINE M = 3 NEW_LINE def checkBitonic ( arr , n ) : NEW_LINE INDENT i , j , f = 0 , 0 , 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i - 1 ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( arr [ i ] == arr [ i - 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT f = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( i == n ) : NEW_LINE INDENT return True NEW_LINE DEDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ j ] < arr [ j - 1 ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( arr [ i ] == arr [ i - 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT if ( f == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT def check ( arr ) : NEW_LINE INDENT f = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( not checkBitonic ( arr [ i ] , M ) ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE return NEW_LINE DEDENT DEDENT i = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT temp = [ 0 ] * N NEW_LINE for j in range ( N ) : NEW_LINE INDENT temp [ j ] = arr [ j ] [ i ] NEW_LINE DEDENT if ( not checkBitonic ( temp , N ) ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " YES " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT m = [ [ 1 , 2 , 3 ] , [ 3 , 4 , 5 ] , [ 2 , 6 , 4 ] ] NEW_LINE check ( m ) NEW_LINE DEDENT
Maximum possible GCD for a pair of integers with product N | Python3 program to implement the above approach ; Function to return the maximum GCD ; To find all divisors of N ; If i is a factor ; Store the pair of factors ; Store the maximum GCD ; Return the maximum GCD ; Driver Code
import sys NEW_LINE import math NEW_LINE def getMaxGcd ( N ) : NEW_LINE INDENT maxGcd = - sys . maxsize - 1 NEW_LINE for i in range ( 1 , int ( math . sqrt ( N ) ) + 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT A = i NEW_LINE B = N // i NEW_LINE maxGcd = max ( maxGcd , math . gcd ( A , B ) ) NEW_LINE DEDENT DEDENT return maxGcd NEW_LINE DEDENT N = 18 NEW_LINE print ( getMaxGcd ( N ) ) NEW_LINE
Maximum of minimum difference of all pairs from subsequences of given size | Function to check a subsequence can be formed with min difference mid ; If a subsequence of size B with min diff = mid is possible return true else false ; Function to find the maximum of all minimum difference of pairs possible among the subsequence ; Sort the Array ; Stores the boundaries of the search space ; Store the answer ; Binary Search ; If subsequence can be formed with min diff mid and size B ; Right half ; Left half ; Driver code
def can_place ( A , n , B , mid ) : NEW_LINE INDENT count = 1 NEW_LINE last_position = A [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( A [ i ] - last_position >= mid ) : NEW_LINE INDENT last_position = A [ i ] NEW_LINE count = count + 1 NEW_LINE if ( count == B ) : NEW_LINE INDENT return bool ( True ) NEW_LINE DEDENT DEDENT DEDENT return bool ( False ) NEW_LINE DEDENT def find_min_difference ( A , n , B ) : NEW_LINE INDENT A . sort ( ) NEW_LINE s = 0 NEW_LINE e = A [ n - 1 ] - A [ 0 ] NEW_LINE ans = 0 NEW_LINE while ( s <= e ) : NEW_LINE INDENT mid = ( int ) ( ( s + e ) / 2 ) NEW_LINE if ( can_place ( A , n , B , mid ) ) : NEW_LINE INDENT ans = mid NEW_LINE s = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT e = mid - 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT A = [ 1 , 2 , 3 , 5 ] NEW_LINE n = len ( A ) NEW_LINE B = 3 NEW_LINE min_difference = find_min_difference ( A , n , B ) NEW_LINE print ( min_difference ) NEW_LINE
Count of triplets in an Array such that A [ i ] * A [ j ] = A [ k ] and i < j < k | Python3 program for the above approach ; Returns total number of valid triplets possible ; Stores the count ; Map to store frequency of array elements ; Increment the frequency of A [ j + 1 ] as it can be a valid A [ k ] ; If target exists in the map ; Return the final count ; Driver code
from collections import defaultdict NEW_LINE def countTriplets ( A , N ) : NEW_LINE INDENT ans = 0 NEW_LINE map = defaultdict ( lambda : 0 ) NEW_LINE for j in range ( N - 2 , 0 , - 1 ) : NEW_LINE INDENT map [ A [ j + 1 ] ] += 1 NEW_LINE for i in range ( j ) : NEW_LINE INDENT target = A [ i ] * A [ j ] NEW_LINE if ( target in map . keys ( ) ) : NEW_LINE INDENT ans += map [ target ] NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE A = [ 2 , 3 , 4 , 6 , 12 ] NEW_LINE print ( countTriplets ( A , N ) ) NEW_LINE DEDENT
Missing vertex among N axis | Python3 program for the above approach ; Driver code ; Number of rectangles ; Stores the coordinates ; Insert the coordinates
from collections import defaultdict NEW_LINE def MissingPoint ( V , N ) : NEW_LINE INDENT mp = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( len ( V ) ) : NEW_LINE INDENT mp [ V [ i ] [ 0 ] ] += 1 NEW_LINE DEDENT for it in mp . keys ( ) : NEW_LINE INDENT if ( mp [ it ] % 2 == 1 ) : NEW_LINE INDENT x = it NEW_LINE break NEW_LINE DEDENT DEDENT del mp NEW_LINE mp = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( len ( V ) ) : NEW_LINE INDENT mp [ V [ i ] [ 1 ] ] += 1 NEW_LINE DEDENT for it in mp . keys ( ) : NEW_LINE INDENT if ( mp [ it ] % 2 == 1 ) : NEW_LINE INDENT y = it NEW_LINE break NEW_LINE DEDENT DEDENT print ( x , y ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE V = [ ] NEW_LINE V . append ( [ 1 , 1 ] ) NEW_LINE V . append ( [ 1 , 2 ] ) NEW_LINE V . append ( [ 4 , 6 ] ) NEW_LINE V . append ( [ 2 , 1 ] ) NEW_LINE V . append ( [ 9 , 6 ] ) NEW_LINE V . append ( [ 9 , 3 ] ) NEW_LINE V . append ( [ 4 , 3 ] ) NEW_LINE MissingPoint ( V , N ) NEW_LINE DEDENT
Longest alternating subsequence with maximum sum | Set 2 | Function to check the sign of the element ; Function to calculate and return the maximum sum of longest alternating subsequence ; Iterate through the array ; Stores the first element of a sequence of same sign ; Traverse until an element with opposite sign is encountered ; Update the maximum ; Update the maximum sum ; Update i ; Return the maximum sum ; Driver Code
def sign ( x ) : NEW_LINE INDENT if ( x > 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT def findMaxSum ( arr , size ) : NEW_LINE INDENT max_sum = 0 NEW_LINE i = 0 NEW_LINE while i < size : NEW_LINE INDENT pres = arr [ i ] NEW_LINE j = i NEW_LINE while ( j < size and ( sign ( arr [ i ] ) == sign ( arr [ j ] ) ) ) : NEW_LINE INDENT pres = max ( pres , arr [ j ] ) NEW_LINE j += 1 NEW_LINE DEDENT max_sum = max_sum + pres NEW_LINE i = j - 1 NEW_LINE i += 1 NEW_LINE DEDENT return max_sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ - 2 , 8 , 3 , 8 , - 4 , - 15 , 5 , - 2 , - 3 , 1 ] NEW_LINE size = len ( arr ) NEW_LINE print ( findMaxSum ( arr , size ) ) NEW_LINE DEDENT
Check if an array can be split into subsets of K consecutive elements | Python3 program to implement the above approach ; Function to check if a given array can be split into subsets of K consecutive elements ; Stores the frequencies of array elements ; Traverse the map ; Check if all its occurrences can be grouped into K subsets ; Traverse next K elements ; If the element is not present in the array ; If it cannot be split into required number of subsets ; Driver Code
from collections import defaultdict NEW_LINE def groupInKConsecutive ( arr , K ) : NEW_LINE INDENT count = defaultdict ( int ) NEW_LINE for h in arr : NEW_LINE INDENT count [ h ] += 1 NEW_LINE DEDENT for key , value in count . items ( ) : NEW_LINE INDENT cur = key NEW_LINE n = value NEW_LINE if ( n > 0 ) : NEW_LINE INDENT for i in range ( 1 , K ) : NEW_LINE INDENT if ( ( cur + i ) not in count ) : NEW_LINE INDENT return False NEW_LINE DEDENT count [ cur + i ] -= n NEW_LINE if ( count [ cur + i ] < 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 6 , 2 , 3 , 4 , 7 , 8 ] NEW_LINE k = 3 NEW_LINE if ( groupInKConsecutive ( arr , k ) ) : NEW_LINE INDENT print ( " True " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " False " ) NEW_LINE DEDENT DEDENT
Count of substrings of a given Binary string with all characters same | Function to count number of sub - strings of a given binary string that contains only 1 ; Iterate untill L and R cross each other ; Check if reached the end of string ; Check if encountered '1' then extend window ; Check if encountered '0' then add number of strings of current window and change the values for both l and r ; Return the answer ; Function to flip the bits of string ; Function to count number of sub - strings of a given binary string that contains only 0 s & 1 s ; count of substring which contains only 1 s ; Flip the character of string s 0 to 1 and 1 to 0 to count the substring with consecutive 0 s ; count of substring which contains only 0 s ; Driver Code ; Given string str ; Function Call
def countSubAllOnes ( s ) : NEW_LINE INDENT l , r , ans = 0 , 0 , 0 NEW_LINE while ( l <= r ) : NEW_LINE INDENT if ( r == len ( s ) ) : NEW_LINE INDENT ans += ( ( r - l ) * ( r - l + 1 ) ) // 2 NEW_LINE break NEW_LINE DEDENT if ( s [ r ] == '1' ) : NEW_LINE INDENT r += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans += ( ( r - l ) * ( r - l + 1 ) ) // 2 NEW_LINE l = r + 1 NEW_LINE r += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def flip ( s ) : NEW_LINE INDENT arr = list ( s ) NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( arr [ i ] == '1' ) : NEW_LINE INDENT arr [ i ] = '0' NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = '1' NEW_LINE DEDENT DEDENT s = ' ' . join ( arr ) NEW_LINE return s NEW_LINE DEDENT def countSubAllZerosOnes ( s ) : NEW_LINE INDENT only_1s = countSubAllOnes ( s ) NEW_LINE s = flip ( s ) NEW_LINE only_0s = countSubAllOnes ( s ) NEW_LINE return only_0s + only_1s NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "011" NEW_LINE print ( countSubAllZerosOnes ( s ) ) NEW_LINE DEDENT
Count of primes in a given range that can be expressed as sum of perfect squares | Function to check if a prime number satisfies the condition to be expressed as sum of two perfect squares ; Function to check if a number is prime or not ; Corner cases ; Function to return the count of primes in the range which can be expressed as the sum of two squares ; If i is a prime ; If i can be expressed as the sum of two squares ; Return the count ; Driver code
def sumsquare ( p ) : NEW_LINE INDENT return ( p - 1 ) % 4 == 0 NEW_LINE DEDENT def isprime ( n ) : NEW_LINE INDENT if n <= 1 : NEW_LINE INDENT return False NEW_LINE DEDENT if n <= 3 : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 ) or ( n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = 5 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( ( n % i == 0 ) or ( n % ( i + 2 ) == 0 ) ) : NEW_LINE return False NEW_LINE i += 6 NEW_LINE DEDENT return True NEW_LINE DEDENT def countOfPrimes ( L , R ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT if ( isprime ( i ) ) : NEW_LINE if sumsquare ( i ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 5 NEW_LINE R = 41 NEW_LINE print ( countOfPrimes ( L , R ) ) NEW_LINE DEDENT
Count of array elements which are greater than all elements on its left | Function to return the count of array elements with all elements to its left smaller than it ; Stores the count ; Stores the maximum ; Iterate over the array ; If an element greater than maximum is obtained ; Increase count ; Update maximum ; Driver Code
def count_elements ( arr ) : NEW_LINE INDENT count = 1 NEW_LINE max = arr [ 0 ] NEW_LINE for i in range ( 1 , len ( arr ) ) : NEW_LINE INDENT if arr [ i ] > max : NEW_LINE INDENT count += 1 NEW_LINE max = arr [ i ] NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 2 , 1 , 4 , 6 , 3 ] NEW_LINE print ( count_elements ( arr ) ) NEW_LINE
Count of bitonic substrings from the given string | Function to find all the bitonic sub strings ; Pick starting po ; Iterate till length of the string ; Pick ending pofor string ; Substring from i to j is obtained ; Substrings of length 1 ; Increase count ; For increasing sequence ; Check for strictly increasing ; Increase count ; Check for decreasing sequence ; Increase count ; Print the result ; Given string ; Function Call
def subString ( str , n ) : NEW_LINE INDENT c = 0 ; NEW_LINE for len in range ( 1 , n + 1 ) : NEW_LINE INDENT for i in range ( 0 , n - len + 1 ) : NEW_LINE INDENT j = i + len - 1 ; NEW_LINE temp = str [ i ] NEW_LINE f = 0 ; NEW_LINE if ( j == i ) : NEW_LINE INDENT c += 1 NEW_LINE continue ; NEW_LINE DEDENT k = i + 1 ; NEW_LINE while ( k <= j and temp < str [ k ] ) : NEW_LINE INDENT temp = str [ k ] ; NEW_LINE k += 1 ; NEW_LINE f = 2 ; NEW_LINE DEDENT if ( k > j ) : NEW_LINE INDENT c += 1 ; NEW_LINE f = 2 ; NEW_LINE DEDENT while ( k <= j and temp > str [ k ] and f != 2 ) : NEW_LINE INDENT k += 1 ; NEW_LINE f = 0 ; NEW_LINE DEDENT if ( k > j and f != 2 ) : NEW_LINE INDENT c += 1 ; NEW_LINE f = 0 ; NEW_LINE DEDENT DEDENT DEDENT print ( c ) NEW_LINE DEDENT str = " bade " ; NEW_LINE subString ( str , len ( str ) ) NEW_LINE
Check if ceil of number divided by power of two exist in sorted array | Function to find there exist a number or not in the array ; Loop to check if there exist a number by divided by power of 2 ; Binary Search ; Condition to check the number is found in the array or not ; Otherwise divide the number by increasing the one more power of 2 ; Driver Code
def findNumberDivByPowerofTwo ( ar , k , n ) : NEW_LINE INDENT found = - 1 NEW_LINE m = k NEW_LINE while ( m > 0 ) : NEW_LINE INDENT l = 0 NEW_LINE r = n - 1 NEW_LINE while ( l <= r ) : NEW_LINE INDENT mid = ( l + r ) // 2 NEW_LINE if ( ar [ mid ] == m ) : NEW_LINE INDENT found = m NEW_LINE break NEW_LINE DEDENT elif ( ar [ mid ] > m ) : NEW_LINE INDENT r = mid - 1 NEW_LINE DEDENT elif ( ar [ mid ] < m ) : NEW_LINE INDENT l = mid + 1 NEW_LINE DEDENT DEDENT if ( found != - 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT m = m // 2 NEW_LINE DEDENT return found NEW_LINE DEDENT arr = [ 3 , 5 , 7 , 8 , 10 ] NEW_LINE k = 4 NEW_LINE n = 5 NEW_LINE print ( findNumberDivByPowerofTwo ( arr , k , n ) ) NEW_LINE