text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Hash map which will store the frequency of the elements of the array . | mp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE |
Incrementing the frequency of the element by 1. | mp [ arr [ i ] ] = mp . get ( arr [ i ] , 0 ) + 1 NEW_LINE for i in range ( n ) : NEW_LINE |
Print the element which appear more than or equal to k times . | if ( arr [ i ] in mp and mp [ arr [ i ] ] >= k ) : NEW_LINE INDENT print ( arr [ i ] , end = " β " ) NEW_LINE DEDENT |
Driver Code | 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 |
function to check if two consecutive same characters are present | 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 |
Driver Code | s = " xzyyz " NEW_LINE if ( check ( s ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT |
Python 3 program to remove the elements which appear more than k times from the array . | def RemoveElements ( arr , n , k ) : NEW_LINE |
Hash map which will store the frequency of the elements of the array . | mp = { i : 0 for i in range ( len ( arr ) ) } NEW_LINE for i in range ( n ) : NEW_LINE |
Incrementing the frequency of the element by 1. | mp [ arr [ i ] ] += 1 NEW_LINE for i in range ( n ) : NEW_LINE |
Print the element which appear less than or equal to k times . | if ( mp [ arr [ i ] ] <= k ) : NEW_LINE INDENT print ( arr [ i ] , end = " β " ) NEW_LINE DEDENT |
Driver Code | 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 |
Python3 program to find the smallest number from the array after n deletions | import math as mt NEW_LINE |
Returns maximum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] | def findSmallestAfterDel ( arr , m , dell , n ) : NEW_LINE |
Hash Map of the numbers to be deleted | mp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE |
Increment the count of del [ i ] | 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 |
Initializing the SmallestElement | SmallestElement = 10 ** 9 NEW_LINE for i in range ( m ) : NEW_LINE |
Search if the element is present | if ( arr [ i ] in mp . keys ( ) ) : NEW_LINE |
Decrement its frequency | mp [ arr [ i ] ] -= 1 NEW_LINE |
If the frequency becomes 0 , erase it from the map | if ( mp [ arr [ i ] ] == 0 ) : NEW_LINE INDENT mp . pop ( arr [ i ] ) NEW_LINE DEDENT |
Else compare it SmallestElement | else : NEW_LINE INDENT SmallestElement = min ( SmallestElement , arr [ i ] ) NEW_LINE DEDENT return SmallestElement NEW_LINE |
Driver code | 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 |
Python3 program to find the largest number from the array after n deletions | import math as mt NEW_LINE |
Returns maximum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] | def findlargestAfterDel ( arr , m , dell , n ) : NEW_LINE |
Hash Map of the numbers to be deleted | mp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE |
Increment the count of del [ i ] | 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 |
Initializing the largestElement | largestElement = - 10 ** 9 NEW_LINE for i in range ( m ) : NEW_LINE |
Search if the element is present | if ( arr [ i ] in mp . keys ( ) ) : NEW_LINE |
Decrement its frequency | mp [ arr [ i ] ] -= 1 NEW_LINE |
If the frequency becomes 0 , erase it from the map | if ( mp [ arr [ i ] ] == 0 ) : NEW_LINE INDENT mp . pop ( arr [ i ] ) NEW_LINE DEDENT |
Else compare it largestElement | else : NEW_LINE INDENT largestElement = max ( largestElement , arr [ i ] ) NEW_LINE DEDENT return largestElement NEW_LINE |
Driver code | 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 |
A simple Python3 solution to count anomalies in an array . | 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 |
Driver Code | 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 |
Function to find count of all majority elements in a Matrix | def majorityInMatrix ( arr ) : NEW_LINE INDENT mp = { i : 0 for i in range ( 7 ) } NEW_LINE DEDENT |
Store frequency of elements in matrix | 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 |
loop to iteratre through map | countMajority = 0 NEW_LINE for key , value in mp . items ( ) : NEW_LINE |
check if frequency is greater than or equal to ( N * M ) / 2 | if ( value >= ( int ( ( N * M ) / 2 ) ) ) : NEW_LINE INDENT countMajority += 1 NEW_LINE DEDENT return countMajority NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 1 , 2 , 2 ] , [ 1 , 3 , 2 ] , [ 1 , 2 , 6 ] ] NEW_LINE print ( majorityInMatrix ( mat ) ) NEW_LINE DEDENT |
Function to find the column with max difference | def colMaxDiff ( mat ) : NEW_LINE INDENT max_diff = 0 NEW_LINE DEDENT |
Traverse matrix column wise | for i in range ( N ) : NEW_LINE |
Insert elements of column to vector | 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 |
calculating difference between maximum and minimum | max_diff = max ( max_diff , max_val - min_val ) NEW_LINE return max_diff NEW_LINE |
Driver Code | 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 |
A binary search based program to find the only missing number in a sorted in a sorted array of distinct elements within limited range | 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 |
Driver Code | a = [ 1 , 2 , 3 , 4 , 5 , 6 , 8 ] NEW_LINE n = len ( a ) NEW_LINE print ( " Missing β number : " , search ( a , n ) ) NEW_LINE |
Function to delete L to R element | 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 DEDENT |
Return size of Array after delete element | return j NEW_LINE |
Driver Code | 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 |
Function to find index in preSum list of tuples upto which all prefix sum values are less than or equal to val . | def findInd ( preSum , n , val ) : NEW_LINE |
Starting and ending index of search space . | l = 0 NEW_LINE h = n - 1 NEW_LINE |
To store required index value | ans = - 1 NEW_LINE |
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 | 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 |
Function to find Longest subarray having average greater than or equal to x . | def LongestSub ( arr , n , x ) : NEW_LINE |
Update array by subtracting x from each element | for i in range ( n ) : NEW_LINE INDENT arr [ i ] -= x NEW_LINE DEDENT |
Length of Longest subarray . | maxlen = 0 NEW_LINE |
list to store pair of prefix sum and corresponding ending index value . | preSum = [ ] NEW_LINE |
To store current value of prefix sum . | total = 0 NEW_LINE |
To store minimum index value in range 0. . i of preSum vector . | minInd = [ None ] * n NEW_LINE |
Insert values in preSum vector | 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 |
Update minInd array . | 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 DEDENT |
If sum is greater than or equal to 0 , then answer is i + 1 | if total >= 0 : NEW_LINE INDENT maxlen = i + 1 NEW_LINE DEDENT |
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 | 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 return maxlen NEW_LINE |
Driver Code | 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 |
PYTHON 3 program to find the only missing element . | 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 DEDENT DEDENT |
If this is the first element which is not index + 1 , then missing element is mid + 1 | if ( ar [ mid ] != mid + 1 and ar [ mid - 1 ] == mid ) : NEW_LINE INDENT return ( mid + 1 ) NEW_LINE DEDENT |
if this is not the first missing element search in left side | elif ( ar [ mid ] != mid + 1 ) : NEW_LINE INDENT r = mid - 1 NEW_LINE DEDENT |
if it follows index + 1 property then search in right side | else : NEW_LINE INDENT l = mid + 1 NEW_LINE DEDENT |
if no element is missing | return ( - 1 ) NEW_LINE |
Driver code | 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 |
Python3 program to find index of first occurrence of x when array is sorted . | import math NEW_LINE def findFirst ( arr , n , x ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE DEDENT |
lower_bound returns iterator pointing to first element that does not compare less to x . | ptr = lowerBound ( arr , 0 , n , x ) NEW_LINE |
If x is not present return - 1. | return 1 if ( ptr != x ) else ( ptr - arr ) NEW_LINE |
Driver Code | 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 |
Python 3 program to find index of first occurrence of x when array is sorted . | 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 |
Driver Code | 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 |
Function to find duplicate | def findDuplicate ( arr ) : NEW_LINE |
Find the intersection point of the slow and fast . | 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 |
Find the " entrance " to the cycle . | 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 |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 2 , 1 ] NEW_LINE print ( findDuplicate ( arr ) ) NEW_LINE DEDENT |
Python3 program to count greater characters on right side of every character . | MAX_CHAR = 26 ; NEW_LINE def printGreaterCount ( str1 ) : NEW_LINE INDENT len1 = len ( str1 ) ; NEW_LINE DEDENT |
Arrays to store result and character counts . | ans = [ 0 ] * len1 ; NEW_LINE count = [ 0 ] * MAX_CHAR ; NEW_LINE |
start from right side of string | for i in range ( len1 - 1 , - 1 , - 1 ) : NEW_LINE INDENT count [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE for j in range ( ord ( str1 [ i ] ) - ord ( ' a ' ) + 1 , MAX_CHAR ) : NEW_LINE INDENT ans [ i ] += count [ j ] ; NEW_LINE DEDENT DEDENT for i in range ( len1 ) : NEW_LINE INDENT print ( ans [ i ] , end = " β " ) ; NEW_LINE DEDENT |
Driver code | str1 = " abcd " ; NEW_LINE printGreaterCount ( str1 ) ; NEW_LINE |
Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to sum | def printPairs ( arr , n , sum ) : NEW_LINE |
Store counts of all elements in a dictionary | mydict = dict ( ) NEW_LINE |
Traverse through all the elements | for i in range ( n ) : NEW_LINE |
Search if a pair can be formed with arr [ i ] | temp = sum - arr [ i ] NEW_LINE if temp in mydict : NEW_LINE INDENT count = mydict [ temp ] NEW_LINE for j in range ( count ) : NEW_LINE INDENT print ( " ( " , temp , " , β " , arr [ i ] , " ) " , sep = " " , end = ' ' ) NEW_LINE DEDENT DEDENT if arr [ i ] in mydict : NEW_LINE INDENT mydict [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mydict [ arr [ i ] ] = 1 NEW_LINE DEDENT |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 5 , 7 , - 1 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE sum = 6 NEW_LINE printPairs ( arr , n , sum ) NEW_LINE DEDENT |
A O ( n ) Python 3 program to find maximum quadruple inan array . | import sys NEW_LINE |
Function to find a maximum product of a quadruple in array of integers of size n | def maxProduct ( arr , n ) : NEW_LINE |
if size is less than 4 , no quadruple exists | if ( n < 4 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT |
Initialize Maximum , second maximum , third maximum and fourth maximum element | maxA = - sys . maxsize - 1 NEW_LINE maxB = - sys . maxsize - 1 NEW_LINE maxC = - sys . maxsize - 1 NEW_LINE maxD = - sys . maxsize - 1 NEW_LINE |
Initialize Minimum , second minimum , third minimum and fourth minimum element | minA = sys . maxsize NEW_LINE minB = sys . maxsize NEW_LINE minC = sys . maxsize NEW_LINE minD = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE |
Update Maximum , second maximum , third maximum and fourth maximum element | if ( arr [ i ] > maxA ) : NEW_LINE INDENT maxD = maxC NEW_LINE maxC = maxB NEW_LINE maxB = maxA NEW_LINE maxA = arr [ i ] NEW_LINE DEDENT |
Update second maximum , third maximum and fourth maximum element | elif ( arr [ i ] > maxB ) : NEW_LINE INDENT maxD = maxC NEW_LINE maxC = maxB NEW_LINE maxB = arr [ i ] NEW_LINE DEDENT |
Update third maximum and fourth maximum element | elif ( arr [ i ] > maxC ) : NEW_LINE INDENT maxD = maxC NEW_LINE maxC = arr [ i ] NEW_LINE DEDENT |
Update fourth maximum element | elif ( arr [ i ] > maxD ) : NEW_LINE INDENT maxD = arr [ i ] NEW_LINE DEDENT |
Update Minimum , second minimum third minimum and fourth minimum element | if ( arr [ i ] < minA ) : NEW_LINE INDENT minD = minC NEW_LINE minC = minB NEW_LINE minB = minA NEW_LINE minA = arr [ i ] NEW_LINE DEDENT |
Update second minimum , third minimum and fourth minimum element | elif ( arr [ i ] < minB ) : NEW_LINE INDENT minD = minC NEW_LINE minC = minB NEW_LINE minB = arr [ i ] NEW_LINE DEDENT |
Update third minimum and fourth minimum element | elif ( arr [ i ] < minC ) : NEW_LINE INDENT minD = minC NEW_LINE minC = arr [ i ] NEW_LINE DEDENT |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.