codeparrot/xlcost-text-to-code huggingface dataset:
Check if array can be sorted with one swap | A linear Python 3 program to check if array becomes sorted after one swap ; Find counts and positions of elements that are out of order . ; If there are more than two elements which are out of order . ; If all elements are sorted already ; Cases like { 1 , 5 , 3 , 4 , 2 } We swap 5 and 2. ; Cases like { 1 , 2 , 4 , 3 , 5 } ; Now check if array becomes sorted for cases like { 4 , 1 , 2 , 3 } ; Driver Code
def checkSorted ( n , arr ) : NEW_LINE INDENT first , second = 0 , 0 NEW_LINE count = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if arr [ i ] < arr [ i - 1 ] : NEW_LINE INDENT count += 1 NEW_LINE if first == 0 : NEW_LINE INDENT first = i NEW_LINE DEDENT else : NEW_LINE INDENT second = i NEW_LINE DEDENT DEDENT DEDENT if count > 2 : NEW_LINE INDENT return False NEW_LINE DEDENT if count == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT if count == 2 : NEW_LINE INDENT ( arr [ first - 1 ] , arr [ second ] ) = ( arr [ second ] , arr [ first - 1 ] ) NEW_LINE DEDENT elif count == 1 : NEW_LINE INDENT ( arr [ first - 1 ] , arr [ first ] ) = ( arr [ first ] , arr [ first - 1 ] ) NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT if arr [ i ] < arr [ i - 1 ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 4 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE if checkSorted ( n , arr ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Find the intersection of two Matrices | Python 3 program to find intersection of two matrices ; Function to print the resultant matrix ; print element value for equal elements else * ; Driver Code
N , M = 4 , 4 NEW_LINE def printIntersection ( A , B ) : NEW_LINE INDENT for i in range ( M ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( A [ i ] [ j ] == B [ i ] [ j ] ) : NEW_LINE INDENT print ( A [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " * ▁ " , end = " ▁ " ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ [ 2 , 4 , 6 , 8 ] , [ 1 , 3 , 5 , 7 ] , [ 8 , 6 , 4 , 2 ] , [ 7 , 5 , 3 , 1 ] ] NEW_LINE B = [ [ 2 , 3 , 6 , 8 ] , [ 1 , 3 , 5 , 2 ] , [ 8 , 1 , 4 , 2 ] , [ 3 , 5 , 4 , 1 ] ] NEW_LINE printIntersection ( A , B ) NEW_LINE DEDENT
Count Triplets such that one of the numbers can be written as sum of the other two | Python3 program to count Triplets such that at least one of the numbers can be written as sum of the other two ; Function to count the number of ways to choose the triples ; compute the max value in the array and create frequency array of size max_val + 1. We can also use HashMap to store frequencies . We have used an array to keep remaining code simple . ; Case 1 : 0 , 0 , 0 ; Case 2 : 0 , x , x ; Case 3 : x , x , 2 * x ; Case 4 : x , y , x + y iterate through all pairs ( x , y ) ; Driver code
import math as mt NEW_LINE def countWays ( arr , n ) : NEW_LINE INDENT max_val = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT max_val = max ( max_val , arr [ i ] ) NEW_LINE DEDENT freq = [ 0 for i in range ( max_val + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT ans += ( freq [ 0 ] * ( freq [ 0 ] - 1 ) * ( freq [ 0 ] - 2 ) // 6 ) NEW_LINE for i in range ( 1 , max_val + 1 ) : NEW_LINE INDENT ans += ( freq [ 0 ] * freq [ i ] * ( freq [ i ] - 1 ) // 2 ) NEW_LINE DEDENT for i in range ( 1 , ( max_val + 1 ) // 2 ) : NEW_LINE INDENT ans += ( freq [ i ] * ( freq [ i ] - 1 ) // 2 * freq [ 2 * i ] ) NEW_LINE DEDENT for i in range ( 1 , max_val + 1 ) : NEW_LINE INDENT for j in range ( i + 1 , max_val - i + 1 ) : NEW_LINE INDENT ans += freq [ i ] * freq [ j ] * freq [ i + j ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countWays ( arr , n ) ) NEW_LINE
Count pairs from two arrays having sum equal to K | Function to return the count of pairs having sum equal to K ; Initialize pairs to 0 ; Create dictionary of elements of array A1 ; count total pairs ; Every element can be part of at most one pair ; return total pairs ; Driver Code ; function call to print required answer
def countPairs ( A1 , A2 , n1 , n2 , K ) : NEW_LINE INDENT res = 0 NEW_LINE m = dict ( ) NEW_LINE for i in range ( 0 , n1 ) : NEW_LINE INDENT if A1 [ i ] not in m . keys ( ) : NEW_LINE INDENT m [ A1 [ i ] ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ A1 [ i ] ] = m [ A1 [ i ] ] + 1 NEW_LINE DEDENT DEDENT for i in range ( 0 , n2 ) : NEW_LINE INDENT temp = K - A2 [ i ] NEW_LINE if temp in m . keys ( ) : NEW_LINE INDENT res = res + 1 NEW_LINE m [ temp ] = m [ temp ] - 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT A1 = [ 1 , 1 , 3 , 4 , 5 , 6 , 6 ] NEW_LINE A2 = [ 1 , 4 , 4 , 5 , 7 ] NEW_LINE K = 10 NEW_LINE n1 = len ( A1 ) NEW_LINE n2 = len ( A2 ) NEW_LINE print ( countPairs ( A1 , A2 , n1 , n2 , K ) ) NEW_LINE
Count pairs in an array such that frequency of one is at least value of other | Python3 program to find the number of ordered pairs ; Function to find count of Ordered pairs ; Initialize pairs to 0 ; Store frequencies ; Count total Ordered_pairs ; Driver Code
from collections import defaultdict NEW_LINE def countOrderedPairs ( A , n ) : NEW_LINE INDENT orderedPairs = 0 NEW_LINE m = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT m [ A [ i ] ] += 1 NEW_LINE DEDENT for X , Y in m . items ( ) : NEW_LINE INDENT for j in range ( 1 , Y + 1 ) : NEW_LINE INDENT if m [ j ] >= X : NEW_LINE INDENT orderedPairs += 1 NEW_LINE DEDENT DEDENT DEDENT return orderedPairs NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 1 , 2 , 2 , 3 ] NEW_LINE n = len ( A ) NEW_LINE print ( countOrderedPairs ( A , n ) ) NEW_LINE DEDENT
Longest subarray with elements having equal modulo K | function to find longest sub - array whose elements gives same remainder when divided with K ; Iterate in the array ; check if array element greater then X or not ; Driver code
def LongestSubarray ( arr , n , k ) : NEW_LINE INDENT count = 1 NEW_LINE max_lenght = 1 NEW_LINE prev_mod = arr [ 0 ] % k NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT curr_mod = arr [ i ] % k NEW_LINE if curr_mod == prev_mod : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT max_lenght = max ( max_lenght , count ) NEW_LINE count = 1 NEW_LINE prev_mod = curr_mod NEW_LINE DEDENT DEDENT return max ( max_lenght , count ) NEW_LINE DEDENT arr = [ 4 , 9 , 7 , 18 , 29 , 11 ] NEW_LINE n = len ( arr ) NEW_LINE k = 11 NEW_LINE print ( LongestSubarray ( arr , n , k ) ) NEW_LINE
Search in a sorted 2D matrix ( Stored in row major order ) | Python 3 program to find whether a given element is present in the given 2 - D matrix ; Basic binary search to find an element in a 1 - D array ; if element found return true ; if middle less than K then skip the left part of the array else skip the right part ; if not found return false ; Function to search an element in a matrix based on Divide and conquer approach ; if the element lies in the range of this row then call 1 - D binary search on this row ; if the element is less then the starting element of that row then search in upper rows else search in the lower rows ; if not found ; Driver code
M = 3 NEW_LINE N = 4 NEW_LINE def binarySearch1D ( arr , K ) : NEW_LINE INDENT low = 0 NEW_LINE high = N - 1 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = low + int ( ( high - low ) / 2 ) NEW_LINE if ( arr [ mid ] == K ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( arr [ mid ] < K ) : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def searchMatrix ( matrix , K ) : NEW_LINE INDENT low = 0 NEW_LINE high = M - 1 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = low + int ( ( high - low ) / 2 ) NEW_LINE if ( K >= matrix [ mid ] [ 0 ] and K <= matrix [ mid ] [ N - 1 ] ) : NEW_LINE INDENT return binarySearch1D ( matrix [ mid ] , K ) NEW_LINE DEDENT if ( K < matrix [ mid ] [ 0 ] ) : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT matrix = [ [ 1 , 3 , 5 , 7 ] , [ 10 , 11 , 16 , 20 ] , [ 23 , 30 , 34 , 50 ] ] NEW_LINE K = 3 NEW_LINE if ( searchMatrix ( matrix , K ) ) : NEW_LINE INDENT print ( " Found " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ found " ) NEW_LINE DEDENT DEDENT
Program to find Nth term divisible by a or b | Python 3 program to find nth term divisible by a or b ; Function to return gcd of a and b ; Function to calculate how many numbers from 1 to num are divisible by a or b ; calculate number of terms divisible by a and by b then , remove the terms which are divisible by both a and b ; Binary search to find the nth term divisible by a or b ; set low to 1 and high to max ( a , b ) * n , here we have taken high as 10 ^ 18 ; if the current term is less than n then we need to increase low to mid + 1 ; if current term is greater than equal to n then high = mid ; Driver code
import sys NEW_LINE def gcd ( a , b ) : NEW_LINE INDENT if a == 0 : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT def divTermCount ( a , b , lcm , num ) : NEW_LINE INDENT return num // a + num // b - num // lcm NEW_LINE DEDENT def findNthTerm ( a , b , n ) : NEW_LINE INDENT low = 1 ; high = sys . maxsize NEW_LINE lcm = ( a * b ) // gcd ( a , b ) NEW_LINE while low < high : NEW_LINE INDENT mid = low + ( high - low ) // 2 NEW_LINE if divTermCount ( a , b , lcm , mid ) < n : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid NEW_LINE DEDENT DEDENT return low NEW_LINE DEDENT a = 2 ; b = 5 ; n = 10 NEW_LINE print ( findNthTerm ( a , b , n ) ) NEW_LINE
Replace all occurrences of pi with 3.14 in a given string | Function to replace all occurrences of pi in a given with 3.14 ; Iterate through second last element of the string ; If current and current + 1 alphabets form the word ' pi ' append 3.14 to output ; Append the current letter ; Return the output string ; Driver Code
def replacePi ( input ) : NEW_LINE INDENT output = " " ; NEW_LINE size = len ( input ) ; NEW_LINE for i in range ( size ) : NEW_LINE INDENT if ( i + 1 < size and input [ i ] == ' p ' and input [ i + 1 ] == ' i ' ) : NEW_LINE INDENT output += "3.14" ; NEW_LINE i += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT output += input [ i ] ; NEW_LINE DEDENT DEDENT return output ; NEW_LINE DEDENT input = "2 ▁ * ▁ pi ▁ + ▁ 3 ▁ * ▁ pi ▁ = ▁ 5 ▁ * ▁ pi " ; NEW_LINE print ( replacePi ( input ) ) ; NEW_LINE
Number of elements that can be seen from right side | Python3 program to find number of elements that can be seen from right side ; Driver code
def numberOfElements ( height , n ) : NEW_LINE INDENT max_so_far = 0 NEW_LINE coun = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if height [ i ] > max_so_far : NEW_LINE INDENT max_so_far = height [ i ] NEW_LINE coun = coun + 1 NEW_LINE DEDENT DEDENT return coun NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 6 NEW_LINE height = [ 4 , 8 , 2 , 0 , 0 , 5 ] NEW_LINE print ( numberOfElements ( height , n ) ) NEW_LINE DEDENT
Check if a pair with given product exists in Linked list | Link list node ; Push a new node on the front of the list . ; Checks if pair with given product exists in the list or not ; Check if pair exits ; Driver Code ; Start with the empty list ; Use push ( ) to construct linked list ; function to print the result
class Node : NEW_LINE INDENT def __init__ ( self , data , next ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = next NEW_LINE DEDENT DEDENT class LinkedList : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . head = None NEW_LINE DEDENT def push ( self , new_data ) : NEW_LINE INDENT new_node = Node ( new_data , self . head ) NEW_LINE self . head = new_node NEW_LINE DEDENT def check_pair_product ( self , prod ) : NEW_LINE INDENT p = self . head NEW_LINE s = set ( ) NEW_LINE while p != None : NEW_LINE INDENT curr = p . data NEW_LINE if ( prod % curr == 0 and ( prod // curr ) in s ) : NEW_LINE INDENT print ( curr , prod // curr ) NEW_LINE return True ; NEW_LINE DEDENT s . add ( p . data ) ; NEW_LINE p = p . next ; NEW_LINE DEDENT return False NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT linkedlist = LinkedList ( ) NEW_LINE linkedlist . push ( 1 ) NEW_LINE linkedlist . push ( 2 ) NEW_LINE linkedlist . push ( 1 ) NEW_LINE linkedlist . push ( 12 ) NEW_LINE linkedlist . push ( 1 ) NEW_LINE linkedlist . push ( 18 ) NEW_LINE linkedlist . push ( 47 ) NEW_LINE linkedlist . push ( 16 ) NEW_LINE linkedlist . push ( 12 ) NEW_LINE linkedlist . push ( 14 ) NEW_LINE res = linkedlist . check_pair_product ( 24 ) NEW_LINE if res == False : NEW_LINE INDENT print ( " NO ▁ PAIR ▁ EXIST " ) NEW_LINE DEDENT DEDENT
Largest element in the array that is repeated exactly k times | Function that finds the largest element which is repeated ' k ' times ; sort the array ; if the value of ' k ' is 1 and the largest appears only once in the array ; counter to count the repeated elements ; check if the element at index ' i ' is equal to the element at index ' i + 1' then increase the count ; else set the count to 1 to start counting the frequency of the new number ; if the count is equal to k and the previous element is not equal to this element ; if there is no such element ; Driver code ; find the largest element that is repeated K times
def solve ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE if ( k == 1 and arr [ n - 2 ] != arr [ n - 1 ] ) : NEW_LINE INDENT print ( arr [ n - 1 ] ) NEW_LINE return NEW_LINE DEDENT count = 1 NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count = 1 NEW_LINE DEDENT if ( count == k and ( i == 0 or ( arr [ i - 1 ] != arr [ i ] ) ) ) : NEW_LINE INDENT print ( arr [ i ] ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " No ▁ such ▁ element " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 , 6 , 6 ] NEW_LINE k = 2 NEW_LINE n = len ( arr ) NEW_LINE solve ( arr , n , k ) NEW_LINE DEDENT
Largest element in the array that is repeated exactly k times | Python implementation of above approach ; Function that finds the largest element that occurs exactly ' k ' times ; store the frequency of each element ; to store the maximum element ; if current element has frequency ' k ' and current maximum hasn 't been set ; set the current maximum ; if current element has frequency ' k ' and it is greater than the current maximum ; change the current maximum ; if there is no element with frequency 'k ; print the largest element with frequency 'k ; Driver code ; find the largest element that is repeated K times
import sys NEW_LINE def solve ( arr , n , k ) : NEW_LINE INDENT m = { } ; NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT if ( arr [ i ] in m . keys ( ) ) : NEW_LINE INDENT m [ arr [ i ] ] += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT m [ arr [ i ] ] = 1 ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT max = sys . maxsize ; NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT if ( m [ arr [ i ] ] == k and max == sys . maxsize ) : NEW_LINE INDENT max = arr [ i ] ; NEW_LINE DEDENT elif ( m [ arr [ i ] ] == k and max < arr [ i ] ) : NEW_LINE INDENT max = arr [ i ] ; NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( max == sys . maxsize ) : NEW_LINE INDENT print ( " No ▁ such ▁ element " ) ; NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT else : NEW_LINE INDENT print ( max ) ; NEW_LINE DEDENT DEDENT arr = [ 1 , 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 , 6 , 6 ] NEW_LINE k = 4 ; NEW_LINE n = len ( arr ) NEW_LINE solve ( arr , n , k ) NEW_LINE
Sum and Product of minimum and maximum element of an Array | Function to find minimum element ; Function to find maximum element ; Function to get Sum ; Function to get product ; Driver Code ; Sum of min and max element ; Product of min and max element
def getMin ( arr , n ) : NEW_LINE INDENT res = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT res = min ( res , arr [ i ] ) NEW_LINE DEDENT return res NEW_LINE DEDENT def getMax ( arr , n ) : NEW_LINE INDENT res = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT res = max ( res , arr [ i ] ) NEW_LINE DEDENT return res NEW_LINE DEDENT def findSum ( arr , n ) : NEW_LINE INDENT min = getMin ( arr , n ) NEW_LINE max = getMax ( arr , n ) NEW_LINE return min + max NEW_LINE DEDENT def findProduct ( arr , n ) : NEW_LINE INDENT min = getMin ( arr , n ) NEW_LINE max = getMax ( arr , n ) NEW_LINE return min * max NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 12 , 1234 , 45 , 67 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Sum ▁ = ▁ " , findSum ( arr , n ) ) NEW_LINE print ( " Product ▁ = ▁ " , findProduct ( arr , n ) ) NEW_LINE DEDENT
Count the number of pop operations on stack to get each element of the array | Function to find the count ; Hashmap to store all the elements which are popped once . ; Check if the number is present in the hashmap Or in other words been popped out from the stack before . ; Keep popping the elements while top is not equal to num ; Pop the top ie . equal to num ; Print the number of elements popped . ; Driver code
def countEle ( s , a , N ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT num = a [ i ] NEW_LINE if num in mp : NEW_LINE INDENT print ( "0" , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT cnt = 0 NEW_LINE while s [ - 1 ] != num : NEW_LINE INDENT mp [ s . pop ( ) ] = True NEW_LINE cnt += 1 NEW_LINE DEDENT s . pop ( ) NEW_LINE cnt += 1 NEW_LINE print ( cnt , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE s = [ ] NEW_LINE s . append ( 1 ) NEW_LINE s . append ( 2 ) NEW_LINE s . append ( 3 ) NEW_LINE s . append ( 4 ) NEW_LINE s . append ( 6 ) NEW_LINE a = [ 6 , 3 , 4 , 1 , 2 ] NEW_LINE countEle ( s , a , N ) NEW_LINE DEDENT
Queries to check whether a given digit is present in the given Range | Python3 program to answer Queries to check whether a given digit is present in the given range ; Segment Tree with set at each node ; Funtiom to build the segment tree ; Left child node ; Right child node ; Merging child nodes to get parent node . Since set is used , it will remove redundant digits . ; Function to query a range ; Complete Overlapp condition return true if digit is present . else false . ; No Overlapp condition Return false ; If digit is found in any child return true , else False ; Driver Code ; Build the tree ; Query 1 ; Query 2
N = 6 NEW_LINE Tree = [ 0 ] * ( 6 * N ) NEW_LINE for i in range ( 6 * N ) : NEW_LINE INDENT Tree [ i ] = set ( ) NEW_LINE DEDENT def buildTree ( arr : list , idx : int , s : int , e : int ) -> None : NEW_LINE INDENT global Tree NEW_LINE if s == e : NEW_LINE INDENT Tree [ idx ] . add ( arr [ s ] ) NEW_LINE return NEW_LINE DEDENT mid = ( s + e ) // 2 NEW_LINE buildTree ( arr , 2 * idx , s , mid ) NEW_LINE buildTree ( arr , 2 * idx + 1 , mid + 1 , e ) NEW_LINE for it in Tree [ 2 * idx ] : NEW_LINE INDENT Tree [ idx ] . add ( it ) NEW_LINE DEDENT for it in Tree [ 2 * idx + 1 ] : NEW_LINE INDENT Tree [ idx ] . add ( it ) NEW_LINE DEDENT DEDENT def query ( idx : int , s : int , e : int , qs : int , qe : int , x : int ) -> bool : NEW_LINE INDENT global Tree NEW_LINE if qs <= s and e <= qe : NEW_LINE INDENT if list ( Tree [ idx ] ) . count ( x ) != 0 : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if qe < s or e < qs : NEW_LINE INDENT return False NEW_LINE DEDENT mid = ( s + e ) // 2 NEW_LINE leftAns = query ( 2 * idx , s , mid , qs , qe , x ) NEW_LINE rightAns = query ( 2 * idx + 1 , mid + 1 , e , qs , qe , x ) NEW_LINE return ( leftAns or rightAns ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 3 , 9 , 8 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE buildTree ( arr , 1 , 0 , n - 1 ) NEW_LINE l = 0 NEW_LINE r = 3 NEW_LINE x = 2 NEW_LINE if query ( 1 , 0 , n - 1 , l , r , x ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT l = 2 NEW_LINE r = 5 NEW_LINE x = 3 NEW_LINE if query ( 1 , 0 , n - 1 , l , r , x ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Count characters with same neighbors | Function to count the characters with same adjacent characters ; if length is less than 3 then return length as there will be only two characters ; Traverse the string ; Increment the count if the previous and next character is same ; Return count ; Driver code
def countChar ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE if ( n <= 2 ) : NEW_LINE INDENT return n NEW_LINE DEDENT count = 2 NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( str [ i - 1 ] == str [ i + 1 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " egeeksk " NEW_LINE print ( countChar ( str ) ) NEW_LINE DEDENT
First strictly smaller element in a sorted array in Java | Python3 program to find first element that is strictly smaller than given target ; Minimum size of the array should be 1 ; If target lies beyond the max element , than the index of strictly smaller value than target should be ( end - 1 ) ; Move to the left side if target is smaller ; Move right side ; Driver code
def next ( arr , target ) : NEW_LINE INDENT start = 0 ; NEW_LINE end = len ( arr ) - 1 ; NEW_LINE if ( end == 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT if ( target > arr [ end ] ) : NEW_LINE INDENT return end ; NEW_LINE DEDENT ans = - 1 ; NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = ( start + end ) // 2 ; NEW_LINE if ( arr [ mid ] >= target ) : NEW_LINE INDENT end = mid - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT ans = mid ; NEW_LINE start = mid + 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 5 , 8 , 12 ] ; NEW_LINE print ( next ( arr , 5 ) ) ; NEW_LINE DEDENT
First strictly greater element in a sorted array in Java | Python program to find first element that is strictly greater than given target . ; Move to right side if target is greater . ; Move left side . ; Driver code
def next ( arr , target ) : NEW_LINE INDENT start = 0 ; NEW_LINE end = len ( arr ) - 1 ; NEW_LINE ans = - 1 ; NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = ( start + end ) // 2 ; NEW_LINE if ( arr [ mid ] <= target ) : NEW_LINE INDENT start = mid + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT ans = mid ; NEW_LINE end = mid - 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 5 , 8 , 12 ] ; NEW_LINE print ( next ( arr , 8 ) ) ; NEW_LINE DEDENT
Reallocation of elements based on Locality of Reference | A function to perform sequential search . ; Linearly search the element ; If not found ; Shift elements before one position ; Driver Code
def search ( arr , n , x ) : NEW_LINE INDENT res = - 1 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( x == arr [ i ] ) : NEW_LINE INDENT res = i NEW_LINE DEDENT DEDENT if ( res == - 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT temp = arr [ res ] NEW_LINE i = res NEW_LINE while ( i > 0 ) : NEW_LINE INDENT arr [ i ] = arr [ i - 1 ] NEW_LINE i -= 1 NEW_LINE DEDENT arr [ 0 ] = temp NEW_LINE return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 12 , 25 , 36 , 85 , 98 , 75 , 89 , 15 , 63 , 66 , 64 , 74 , 27 , 83 , 97 ] NEW_LINE q = [ 63 , 63 , 86 , 63 , 78 ] NEW_LINE n = len ( arr ) NEW_LINE m = len ( q ) NEW_LINE for i in range ( 0 , m , 1 ) : NEW_LINE INDENT if ( search ( arr , n , q [ i ] ) ) : NEW_LINE INDENT print ( " Yes " , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT