codeparrot/xlcost-text-to-code huggingface dataset:
Check if the given array can be reduced to zeros with the given operation performed given number of times | Function that returns true if the array can be reduced to 0 s with the given operation performed given number of times ; Set to store unique elements ; Add every element of the array to the set ; Count of all the unique elements in the array ; Driver code
def check ( arr , N , K ) : NEW_LINE INDENT unique = dict ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT unique [ arr [ i ] ] = 1 NEW_LINE DEDENT if len ( unique ) == K : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT arr = [ 1 , 1 , 2 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE if ( check ( arr , N , K ) == True ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Print all the sum pairs which occur maximum number of times | Function to find the sum pairs that occur the most ; Hash - table ; Keep a count of sum pairs ; Variables to store maximum occurrence ; Iterate in the hash table ; Print all sum pair which occur maximum number of times ; Driver code
def findSumPairs ( a , n ) : NEW_LINE INDENT mpp = { i : 0 for i in range ( 21 ) } NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n , 1 ) : NEW_LINE INDENT mpp [ a [ i ] + a [ j ] ] += 1 NEW_LINE DEDENT DEDENT occur = 0 NEW_LINE for key , value in mpp . items ( ) : NEW_LINE INDENT if ( value > occur ) : NEW_LINE INDENT occur = value NEW_LINE DEDENT DEDENT for key , value in mpp . items ( ) : NEW_LINE INDENT if ( value == occur ) : NEW_LINE INDENT print ( key ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 8 , 3 , 11 , 4 , 9 , 2 , 7 ] NEW_LINE n = len ( a ) NEW_LINE findSumPairs ( a , n ) NEW_LINE DEDENT
Minimum index i such that all the elements from index i to given index are equal | Function to return the minimum required index ; Start from arr [ pos - 1 ] ; All elements are equal from arr [ i + 1 ] to arr [ pos ] ; Driver code ; Function Call
def minIndex ( arr , n , pos ) : NEW_LINE INDENT num = arr [ pos ] NEW_LINE i = pos - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( arr [ i ] != num ) : NEW_LINE INDENT break NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT return i + 1 NEW_LINE DEDENT arr = [ 2 , 1 , 1 , 1 , 5 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE pos = 4 NEW_LINE print ( minIndex ( arr , n , pos ) ) NEW_LINE
Minimum index i such that all the elements from index i to given index are equal | Function to return the minimum required index ; Short - circuit more comparisions as found the border point ; For cases were high = low + 1 and arr [ high ] will match with arr [ pos ] but not arr [ low ] or arr [ mid ] . In such iteration the if condition will satisfy and loop will break post that low will be updated . Hence i will not point to the correct index . ; Driver code
def minIndex ( arr , pos ) : NEW_LINE INDENT low = 0 NEW_LINE high = pos NEW_LINE i = pos NEW_LINE while low < high : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if arr [ mid ] != arr [ pos ] : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE i = mid NEW_LINE if mid > 0 and arr [ mid - 1 ] != arr [ pos ] : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT return low if arr [ low ] == arr [ pos ] else i NEW_LINE DEDENT arr = [ 2 , 1 , 1 , 1 , 5 , 2 ] NEW_LINE
Count of strings that become equal to one of the two strings after one removal | Python3 implementation of the approach ; Function to return the count of the required strings ; Searching index after longest common prefix ends ; Searching index before longest common suffix ends ;  ; If only 1 character is different in both the strings ; Checking remaining part of string for equality ; Searching in right of string h ( g to h ) ; Driver code
import math as mt NEW_LINE def findAnswer ( str1 , str2 , n ) : NEW_LINE INDENT l , r = 0 , 0 NEW_LINE ans = 2 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( str1 [ i ] != str2 [ i ] ) : NEW_LINE INDENT l = i NEW_LINE break NEW_LINE DEDENT DEDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( str1 [ i ] != str2 [ i ] ) : NEW_LINE INDENT r = i NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT / * If str1 = str2 * / NEW_LINE INDENT if ( r < l ) : NEW_LINE INDENT return 26 * ( n + 1 ) NEW_LINE DEDENT elif ( l == r ) : NEW_LINE INDENT return ans NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( l + 1 , r + 1 ) : NEW_LINE INDENT if ( str1 [ i ] != str2 [ i - 1 ] ) : NEW_LINE INDENT ans -= 1 NEW_LINE break NEW_LINE DEDENT DEDENT for i in range ( l + 1 , r + 1 ) : NEW_LINE INDENT if ( str1 [ i - 1 ] != str2 [ i ] ) : NEW_LINE INDENT ans -= 1 NEW_LINE break NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT DEDENT str1 = " toy " NEW_LINE str2 = " try " NEW_LINE n = len ( str1 ) NEW_LINE print ( findAnswer ( str1 , str2 , n ) ) NEW_LINE
Minimize the maximum minimum difference after one removal from array | Function to return the minimum required difference ; Sort the given array ; When minimum element is removed ; When maximum element is removed ; Return the minimum of diff1 and diff2 ; Driver Code
def findMinDifference ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE diff1 = arr [ n - 1 ] - arr [ 1 ] NEW_LINE diff2 = arr [ n - 2 ] - arr [ 0 ] NEW_LINE return min ( diff1 , diff2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMinDifference ( arr , n ) ) NEW_LINE DEDENT
Minimize the maximum minimum difference after one removal from array | Function to return the minimum required difference ; If current element is greater than max ; max will become secondMax ; Update the max ; If current element is greater than secondMax but smaller than max ; Update the secondMax ; If current element is smaller than min ; min will become secondMin ; Update the min ; If current element is smaller than secondMin but greater than min ; Update the secondMin ; Minimum of the two possible differences ; Driver code
def findMinDifference ( arr , n ) : NEW_LINE INDENT if ( arr [ 0 ] < arr [ 1 ] ) : NEW_LINE INDENT min__ = secondMax = arr [ 0 ] NEW_LINE DEDENT else : NEW_LINE INDENT min__ = secondMax = arr [ 1 ] NEW_LINE DEDENT if ( arr [ 0 ] < arr [ 1 ] ) : NEW_LINE INDENT max__ = secondMin = arr [ 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT max__ = secondMin = arr [ 0 ] NEW_LINE DEDENT for i in range ( 2 , n ) : NEW_LINE INDENT if ( arr [ i ] > max__ ) : NEW_LINE INDENT secondMax = max__ NEW_LINE max__ = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] > secondMax ) : NEW_LINE INDENT secondMax = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] < min__ ) : NEW_LINE INDENT secondMin = min__ NEW_LINE min__ = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] < secondMin ) : NEW_LINE INDENT secondMin = arr [ i ] NEW_LINE DEDENT DEDENT diff = min ( max__ - secondMin , secondMax - min__ ) NEW_LINE return diff NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMinDifference ( arr , n ) ) NEW_LINE DEDENT
Integers from the range that are composed of a single distinct digit | Boolean function to check distinct digits of a number ; Take last digit ; Check if all other digits are same as last digit ; Remove last digit ; Function to return the count of integers that are composed of a single distinct digit only ; If i has single distinct digit ; Driver code
def checkDistinct ( x ) : NEW_LINE INDENT last = x % 10 NEW_LINE while ( x ) : NEW_LINE INDENT if ( x % 10 != last ) : NEW_LINE INDENT return False NEW_LINE DEDENT x = x // 10 NEW_LINE DEDENT return True NEW_LINE DEDENT def findCount ( L , R ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT if ( checkDistinct ( i ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT L = 10 NEW_LINE R = 50 NEW_LINE print ( findCount ( L , R ) ) NEW_LINE
Smallest Pair Sum in an array | Python3 program to print the sum of the minimum pair ; Function to return the sum of the minimum pair from the array ; If found new minimum ; Minimum now becomes second minimum ; Update minimum ; If current element is > min and < secondMin ; Update secondMin ; Return the sum of the minimum pair ; Driver code
import sys NEW_LINE def smallest_pair ( a , n ) : NEW_LINE INDENT min = sys . maxsize NEW_LINE secondMin = sys . maxsize NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( a [ j ] < min ) : NEW_LINE INDENT secondMin = min NEW_LINE min = a [ j ] NEW_LINE DEDENT elif ( ( a [ j ] < secondMin ) and a [ j ] != min ) : NEW_LINE INDENT secondMin = a [ j ] NEW_LINE DEDENT DEDENT return ( secondMin + min ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( smallest_pair ( arr , n ) ) NEW_LINE DEDENT
Longest subarray with elements divisible by k | function to find longest subarray ; this will contain length of longest subarray found ; Driver code
def longestsubarray ( arr , n , k ) : NEW_LINE INDENT current_count = 0 NEW_LINE max_count = 0 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( arr [ i ] % k == 0 ) : NEW_LINE INDENT current_count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT current_count = 0 NEW_LINE DEDENT max_count = max ( current_count , max_count ) NEW_LINE DEDENT return max_count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 5 , 11 , 32 , 64 , 88 ] NEW_LINE n = len ( arr ) NEW_LINE k = 8 NEW_LINE print ( longestsubarray ( arr , n , k ) ) NEW_LINE DEDENT
Remove elements that appear strictly less than k times | Python3 program to remove the elements which appear strictly less than k times from the array . ; Hash map which will store the frequency of the elements of the array . ; Incrementing the frequency of the element by 1. ; Print the element which appear more than or equal to k times . ; Driver Code
def removeElements ( arr , n , k ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ arr [ i ] ] = mp . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] in mp and mp [ arr [ i ] ] >= k ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 1 , 2 , 2 , 3 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE removeElements ( arr , n , k ) NEW_LINE
Check if a string contains a palindromic sub | function to check if two consecutive same characters are present ; Driver Code
def check ( s ) : NEW_LINE INDENT for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == s [ i + 1 ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT s = " xzyyz " NEW_LINE if ( check ( s ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Remove elements from the array which appear more than k times | Python 3 program to remove the elements which appear more than k times from the array . ; Hash map which will store the frequency of the elements of the array . ; Incrementing the frequency of the element by 1. ; Print the element which appear less than or equal to k times . ; Driver Code
def RemoveElements ( arr , n , k ) : NEW_LINE INDENT mp = { i : 0 for i in range ( len ( arr ) ) } NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( mp [ arr [ i ] ] <= k ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 3 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE RemoveElements ( arr , n , k ) NEW_LINE DEDENT
Find the smallest after deleting given elements | Python3 program to find the smallest number from the array after n deletions ; Returns maximum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Initializing the SmallestElement ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else compare it SmallestElement ; Driver code
import math as mt NEW_LINE def findSmallestAfterDel ( arr , m , dell , n ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if dell [ i ] in mp . keys ( ) : NEW_LINE INDENT mp [ dell [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ dell [ i ] ] = 1 NEW_LINE DEDENT DEDENT SmallestElement = 10 ** 9 NEW_LINE for i in range ( m ) : NEW_LINE INDENT if ( arr [ i ] in mp . keys ( ) ) : NEW_LINE INDENT mp [ arr [ i ] ] -= 1 NEW_LINE if ( mp [ arr [ i ] ] == 0 ) : NEW_LINE INDENT mp . pop ( arr [ i ] ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT SmallestElement = min ( SmallestElement , arr [ i ] ) NEW_LINE DEDENT DEDENT return SmallestElement NEW_LINE DEDENT array = [ 5 , 12 , 33 , 4 , 56 , 12 , 20 ] NEW_LINE m = len ( array ) NEW_LINE dell = [ 12 , 4 , 56 , 5 ] NEW_LINE n = len ( dell ) NEW_LINE print ( findSmallestAfterDel ( array , m , dell , n ) ) NEW_LINE
Find the largest after deleting the given elements | Python3 program to find the largest number from the array after n deletions ; Returns maximum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Initializing the largestElement ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else compare it largestElement ; Driver code
import math as mt NEW_LINE def findlargestAfterDel ( arr , m , dell , n ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if dell [ i ] in mp . keys ( ) : NEW_LINE INDENT mp [ dell [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ dell [ i ] ] = 1 NEW_LINE DEDENT DEDENT largestElement = - 10 ** 9 NEW_LINE for i in range ( m ) : NEW_LINE INDENT if ( arr [ i ] in mp . keys ( ) ) : NEW_LINE INDENT mp [ arr [ i ] ] -= 1 NEW_LINE if ( mp [ arr [ i ] ] == 0 ) : NEW_LINE INDENT mp . pop ( arr [ i ] ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT largestElement = max ( largestElement , arr [ i ] ) NEW_LINE DEDENT DEDENT return largestElement NEW_LINE DEDENT array = [ 5 , 12 , 33 , 4 , 56 , 12 , 20 ] NEW_LINE m = len ( array ) NEW_LINE dell = [ 12 , 33 , 56 , 5 ] NEW_LINE n = len ( dell ) NEW_LINE print ( findlargestAfterDel ( array , m , dell , n ) ) NEW_LINE
Number of anomalies in an array | A simple Python3 solution to count anomalies in an array . ; Driver Code
def countAnomalies ( arr , n , k ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT j = 0 NEW_LINE while j < n : NEW_LINE INDENT if i != j and abs ( arr [ i ] - arr [ j ] ) <= k : NEW_LINE INDENT break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if j == n : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 7 , 1 , 8 ] NEW_LINE k = 5 NEW_LINE n = len ( arr ) NEW_LINE print ( countAnomalies ( arr , n , k ) ) NEW_LINE DEDENT
Count majority element in a matrix | Function to find count of all majority elements in a Matrix ; Store frequency of elements in matrix ; loop to iteratre through map ; check if frequency is greater than or equal to ( N * M ) / 2 ; Driver Code
def majorityInMatrix ( arr ) : NEW_LINE INDENT mp = { i : 0 for i in range ( 7 ) } NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT for j in range ( len ( arr ) ) : NEW_LINE INDENT mp [ arr [ i ] [ j ] ] += 1 NEW_LINE DEDENT DEDENT countMajority = 0 NEW_LINE for key , value in mp . items ( ) : NEW_LINE INDENT if ( value >= ( int ( ( N * M ) / 2 ) ) ) : NEW_LINE INDENT countMajority += 1 NEW_LINE DEDENT DEDENT return countMajority NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 1 , 2 , 2 ] , [ 1 , 3 , 2 ] , [ 1 , 2 , 6 ] ] NEW_LINE print ( majorityInMatrix ( mat ) ) NEW_LINE DEDENT
Find pair with maximum difference in any column of a Matrix | Function to find the column with max difference ; Traverse matrix column wise ; Insert elements of column to vector ; calculating difference between maximum and minimum ; Driver Code
def colMaxDiff ( mat ) : NEW_LINE INDENT max_diff = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT max_val = mat [ 0 ] [ i ] NEW_LINE min_val = mat [ 0 ] [ i ] NEW_LINE for j in range ( 1 , N ) : NEW_LINE INDENT max_val = max ( max_val , mat [ j ] [ i ] ) NEW_LINE min_val = min ( min_val , mat [ j ] [ i ] ) NEW_LINE DEDENT max_diff = max ( max_diff , max_val - min_val ) NEW_LINE DEDENT return max_diff NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT mat = [ [ 1 , 2 , 3 , 4 , 5 ] , [ 5 , 3 , 5 , 4 , 0 ] , [ 5 , 6 , 7 , 8 , 9 ] , [ 0 , 6 , 3 , 4 , 12 ] , [ 9 , 7 , 12 , 4 , 3 ] ] NEW_LINE print ( " Max ▁ difference ▁ : " , colMaxDiff ( mat ) ) NEW_LINE DEDENT
Find the Missing Number in a sorted array | A binary search based program to find the only missing number in a sorted in a sorted array of distinct elements within limited range ; Driver Code
def search ( ar , size ) : NEW_LINE INDENT a = 0 NEW_LINE b = size - 1 NEW_LINE mid = 0 NEW_LINE while b > a + 1 : NEW_LINE INDENT mid = ( a + b ) // 2 NEW_LINE if ( ar [ a ] - a ) != ( ar [ mid ] - mid ) : NEW_LINE INDENT b = mid NEW_LINE DEDENT elif ( ar [ b ] - b ) != ( ar [ mid ] - mid ) : NEW_LINE INDENT a = mid NEW_LINE DEDENT DEDENT return ar [ a ] + 1 NEW_LINE DEDENT a = [ 1 , 2 , 3 , 4 , 5 , 6 , 8 ] NEW_LINE n = len ( a ) NEW_LINE print ( " Missing ▁ number : " , search ( a , n ) ) NEW_LINE
Delete array element in given index range [ L | Function to delete L to R element ; Return size of Array after delete element ; Driver Code
def deleteElement ( A , L , R , N ) : NEW_LINE INDENT j = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if i <= L or i >= R : NEW_LINE INDENT A [ j ] = A [ i ] NEW_LINE j += 1 NEW_LINE DEDENT DEDENT return j NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 5 , 8 , 11 , 15 , 26 , 14 , 19 , 17 , 10 , 14 ] NEW_LINE L , R = 2 , 7 NEW_LINE n = len ( A ) NEW_LINE res_size = deleteElement ( A , L , R , n ) NEW_LINE for i in range ( res_size ) : NEW_LINE INDENT print ( A [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Longest subarray having average greater than or equal to x | Function to find index in preSum list of tuples upto which all prefix sum values are less than or equal to val . ; Starting and ending index of search space . ; To store required index value ; If middle value is less than or equal to val then index can lie in mid + 1. . n else it lies in 0. . mid - 1 ; Function to find Longest subarray having average greater than or equal to x . ; Update array by subtracting x from each element ; Length of Longest subarray . ; list to store pair of prefix sum and corresponding ending index value . ; To store current value of prefix sum . ; To store minimum index value in range 0. . i of preSum vector . ; Insert values in preSum vector ; Update minInd array . ; If sum is greater than or equal to 0 , then answer is i + 1 ; If sum is less than 0 , then find if there is a prefix array having sum that needs to be added to current sum to make its value greater than or equal to 0. If yes , then compare length of updated subarray with maximum length found so far ; Driver Code
def findInd ( preSum , n , val ) : NEW_LINE INDENT l = 0 NEW_LINE h = n - 1 NEW_LINE ans = - 1 NEW_LINE while ( l <= h ) : NEW_LINE INDENT mid = ( l + h ) // 2 NEW_LINE if preSum [ mid ] [ 0 ] <= val : NEW_LINE INDENT ans = mid NEW_LINE l = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT h = mid - 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def LongestSub ( arr , n , x ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT arr [ i ] -= x NEW_LINE DEDENT maxlen = 0 NEW_LINE preSum = [ ] NEW_LINE total = 0 NEW_LINE minInd = [ None ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT total += arr [ i ] NEW_LINE preSum . append ( ( total , i ) ) NEW_LINE DEDENT preSum = sorted ( preSum ) NEW_LINE minInd [ 0 ] = preSum [ 0 ] [ 1 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT minInd [ i ] = min ( minInd [ i - 1 ] , preSum [ i ] [ 1 ] ) NEW_LINE DEDENT total = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT total += arr [ i ] NEW_LINE if total >= 0 : NEW_LINE INDENT maxlen = i + 1 NEW_LINE DEDENT else : NEW_LINE INDENT ind = findInd ( preSum , n , total ) NEW_LINE if ( ind != - 1 ) & ( minInd [ ind ] < i ) : NEW_LINE INDENT maxlen = max ( maxlen , i - minInd [ ind ] ) NEW_LINE DEDENT DEDENT DEDENT return maxlen NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 2 , 1 , 6 , - 3 ] NEW_LINE n = len ( arr ) NEW_LINE x = 3 NEW_LINE print ( LongestSub ( arr , n , x ) ) NEW_LINE DEDENT
Find the only missing number in a sorted array | PYTHON 3 program to find the only missing element . ; If this is the first element which is not index + 1 , then missing element is mid + 1 ; if this is not the first missing element search in left side ; if it follows index + 1 property then search in right side ; if no element is missing ; Driver code
def findmissing ( ar , N ) : NEW_LINE INDENT l = 0 NEW_LINE r = N - 1 NEW_LINE while ( l <= r ) : NEW_LINE INDENT mid = ( l + r ) / 2 NEW_LINE mid = int ( mid ) NEW_LINE if ( ar [ mid ] != mid + 1 and ar [ mid - 1 ] == mid ) : NEW_LINE INDENT return ( mid + 1 ) NEW_LINE DEDENT elif ( ar [ mid ] != mid + 1 ) : NEW_LINE INDENT r = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT l = mid + 1 NEW_LINE DEDENT DEDENT return ( - 1 ) NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT ar = [ 1 , 2 , 3 , 4 , 5 , 7 , 8 ] NEW_LINE N = len ( ar ) NEW_LINE res = findmissing ( ar , N ) NEW_LINE print ( res ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT main ( ) NEW_LINE DEDENT
Find index of first occurrence when an unsorted array is sorted | Python3 program to find index of first occurrence of x when array is sorted . ; lower_bound returns iterator pointing to first element that does not compare less to x . ; If x is not present return - 1. ;  ; Driver Code
import math NEW_LINE def findFirst ( arr , n , x ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE ptr = lowerBound ( arr , 0 , n , x ) NEW_LINE return 1 if ( ptr != x ) else ( ptr - arr ) NEW_LINE DEDENT def lowerBound ( a , low , high , element ) : NEW_LINE INDENT while ( low < high ) : NEW_LINE INDENT middle = low + ( high - low ) // 2 NEW_LINE if ( element > a [ middle ] ) : NEW_LINE INDENT low = middle + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = middle NEW_LINE DEDENT DEDENT return low NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 20 NEW_LINE arr = [ 10 , 30 , 20 , 50 , 20 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findFirst ( arr , n , x ) ) NEW_LINE DEDENT
Find index of first occurrence when an unsorted array is sorted | Python 3 program to find index of first occurrence of x when array is sorted . ; Driver Code
def findFirst ( arr , n , x ) : NEW_LINE INDENT count = 0 NEW_LINE isX = False NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == x ) : NEW_LINE INDENT isX = True NEW_LINE DEDENT elif ( arr [ i ] < x ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return - 1 if ( isX == False ) else count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = 20 NEW_LINE arr = [ 10 , 30 , 20 , 50 , 20 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findFirst ( arr , n , x ) ) NEW_LINE DEDENT
Find duplicate in an array in O ( n ) and by using O ( 1 ) extra space | Function to find duplicate ; Find the intersection point of the slow and fast . ; Find the " entrance " to the cycle . ; Driver code
def findDuplicate ( arr ) : NEW_LINE INDENT slow = arr [ 0 ] NEW_LINE fast = arr [ 0 ] NEW_LINE while True : NEW_LINE INDENT slow = arr [ slow ] NEW_LINE fast = arr [ arr [ fast ] ] NEW_LINE if slow == fast : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT ptr1 = arr [ 0 ] NEW_LINE ptr2 = slow NEW_LINE while ptr1 != ptr2 : NEW_LINE INDENT ptr1 = arr [ ptr1 ] NEW_LINE ptr2 = arr [ ptr2 ] NEW_LINE DEDENT return ptr1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 2 , 1 ] NEW_LINE print ( findDuplicate ( arr ) ) NEW_LINE DEDENT