text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Pre - calculate the frequency array | preCalculate ( string , n ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT print ( firstNonRepeating ( string , n , queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ) NEW_LINE DEDENT |
Python3 implementation of the above approach | import sys NEW_LINE |
Function to return the length of the longest sub string having frequency of a character greater than half of the length of the sub string | def maxLength ( s , n ) : NEW_LINE INDENT ans = - ( sys . maxsize + 1 ) ; NEW_LINE A , L , R = [ ] , [ ] , [ ] ; NEW_LINE freq = [ 0 ] * ( n + 5 ) ; NEW_LINE DEDENT |
for each of the character ' a ' to 'z | ' NEW_LINE INDENT for i in range ( 26 ) : NEW_LINE INDENT count = 0 ; NEW_LINE DEDENT DEDENT |
finding frequency prefix array of the character | for j in range ( n ) : NEW_LINE INDENT if ( ord ( s [ j ] ) - ord ( ' a ' ) == i ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT freq [ j ] = count ; NEW_LINE DEDENT |
Finding the r [ ] and l [ ] arrays . | for j in range ( n ) : NEW_LINE INDENT L . append ( ( 2 * freq [ j - 1 ] ) - j ) ; NEW_LINE R . append ( ( 2 * freq [ j ] ) - j ) ; NEW_LINE DEDENT max_len = - ( sys . maxsize + 1 ) ; NEW_LINE min_val = sys . maxsize ; NEW_LINE |
for each j from 0 to n | for j in range ( n ) : NEW_LINE INDENT min_val = min ( min_val , L [ j ] ) ; NEW_LINE A . append ( min_val ) ; NEW_LINE l = 0 ; r = j ; NEW_LINE DEDENT |
Finding the lower bound of i . | while ( l <= r ) : NEW_LINE INDENT mid = ( l + r ) >> 1 ; NEW_LINE if ( A [ mid ] <= R [ j ] ) : NEW_LINE INDENT max_len = max ( max_len , j - mid + 1 ) ; NEW_LINE r = mid - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT l = mid + 1 ; NEW_LINE DEDENT DEDENT |
storing the maximum value of i - j + 1 | ans = max ( ans , max_len ) ; NEW_LINE |
clearing all the vector so that it can be used for other characters . | A . clear ( ) ; NEW_LINE R . clear ( ) ; NEW_LINE L . clear ( ) ; NEW_LINE return ans ; NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " ababbbacbcbcca " ; NEW_LINE n = len ( s ) ; NEW_LINE print ( maxLength ( s , n ) ) ; NEW_LINE DEDENT |
Python3 program to find first repeating character | NO_OF_CHARS = 256 NEW_LINE |
The function returns index of the first repeating character in a string . If all characters are repeating then returns - 1 | def firstRepeating ( string ) : NEW_LINE |
Mark all characters as not visited | visited = [ False ] * NO_OF_CHARS ; NEW_LINE for i in range ( NO_OF_CHARS ) : NEW_LINE INDENT visited [ i ] = False ; NEW_LINE DEDENT |
Traverse from right and update res as soon as we see a visited character . | res = - 1 ; NEW_LINE for i in range ( len ( string ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( visited [ string . index ( string [ i ] ) ] == False ) : NEW_LINE INDENT visited [ string . index ( string [ i ] ) ] = True ; NEW_LINE DEDENT else : NEW_LINE INDENT res = i ; NEW_LINE DEDENT DEDENT return res ; NEW_LINE |
Driver program to test above function | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " geeksforgeeks " ; NEW_LINE index = firstRepeating ( string ) ; NEW_LINE if ( index == - 1 ) : NEW_LINE INDENT print ( " Either β all β characters β are " , " distinct β or β string β is β empty " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " First β Repeating β character β is : " , string [ index ] ) ; NEW_LINE DEDENT DEDENT |
Function to return the maximum sum for every possible sequence such that a [ i ] + a [ i + k ] + a [ i + 2 k ] + ... + a [ i + qk ] is maximized | def maxSum ( arr , n , K ) : NEW_LINE |
Initialize the maximum with the smallest value | maximum = - 2 ** 32 ; NEW_LINE |
Initialize the sum array with zero | sum = [ 0 ] * n NEW_LINE |
Iterate from the right | for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE |
Update the sum starting at the current element | if ( i + K < n ) : NEW_LINE INDENT sum [ i ] = sum [ i + K ] + arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT sum [ i ] = arr [ i ] ; NEW_LINE DEDENT |
Update the maximum so far | maximum = max ( maximum , sum [ i ] ) NEW_LINE return maximum ; NEW_LINE |
Driver code | arr = [ 3 , 6 , 4 , 7 , 2 ] NEW_LINE n = len ( arr ) ; NEW_LINE K = 2 NEW_LINE print ( maxSum ( arr , n , K ) ) NEW_LINE |
Function to return the count of valid indices pairs | def countPairs ( s1 , n1 , s2 , n2 ) : NEW_LINE |
To store the frequencies of characters of string s1 and s2 | freq1 = [ 0 ] * 26 ; NEW_LINE freq2 = [ 0 ] * 26 ; NEW_LINE |
To store the count of valid pairs | count = 0 ; NEW_LINE |
Update the frequencies of the characters of string s1 | for i in range ( n1 ) : NEW_LINE INDENT freq1 [ ord ( s1 [ i ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE DEDENT |
Update the frequencies of the characters of string s2 | for i in range ( n2 ) : NEW_LINE INDENT freq2 [ ord ( s2 [ i ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE DEDENT |
Find the count of valid pairs | for i in range ( 26 ) : NEW_LINE INDENT count += min ( freq1 [ i ] , freq2 [ i ] ) ; NEW_LINE DEDENT return count ; NEW_LINE |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " geeksforgeeks " ; NEW_LINE s2 = " platformforgeeks " ; NEW_LINE n1 = len ( s1 ) ; NEW_LINE n2 = len ( s2 ) ; NEW_LINE print ( countPairs ( s1 , n1 , s2 , n2 ) ) ; NEW_LINE DEDENT |
Python 3 implementation of the approach | def findpair ( l , r ) : NEW_LINE INDENT c = 0 NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT for j in range ( i + 1 , r + 1 ) : NEW_LINE INDENT if ( j % i == 0 and j != i ) : NEW_LINE INDENT print ( i , " , β " , j ) NEW_LINE c = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( c == 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 1 NEW_LINE r = 10 NEW_LINE findpair ( l , r ) NEW_LINE DEDENT |
Function that returns true if the array can be reduced to 0 s with the given operation performed given number of times | def check ( arr , N , K ) : NEW_LINE |
Set to store unique elements | unique = dict ( ) NEW_LINE |
Add every element of the array to the set | for i in range ( N ) : NEW_LINE INDENT unique [ arr [ i ] ] = 1 NEW_LINE DEDENT |
Count of all the unique elements in the array | if len ( unique ) == K : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE |
Driver code | 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 |
Function to find the sum pairs that occur the most | def findSumPairs ( a , n ) : NEW_LINE |
Hash - table | 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 DEDENT |
Keep a count of sum pairs | mpp [ a [ i ] + a [ j ] ] += 1 NEW_LINE |
Variables to store maximum occurrence | occur = 0 NEW_LINE |
Iterate in the hash table | for key , value in mpp . items ( ) : NEW_LINE INDENT if ( value > occur ) : NEW_LINE INDENT occur = value NEW_LINE DEDENT DEDENT |
Print all sum pair which occur maximum number of times | for key , value in mpp . items ( ) : NEW_LINE INDENT if ( value == occur ) : NEW_LINE INDENT print ( key ) NEW_LINE DEDENT DEDENT |
Driver code | 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 |
Function to return the minimum required index | def minIndex ( arr , n , pos ) : NEW_LINE INDENT num = arr [ pos ] NEW_LINE DEDENT |
Start from arr [ pos - 1 ] | 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 |
All elements are equal from arr [ i + 1 ] to arr [ pos ] | return i + 1 NEW_LINE |
Driver code | arr = [ 2 , 1 , 1 , 1 , 5 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE pos = 4 NEW_LINE |
Function Call | print ( minIndex ( arr , n , pos ) ) NEW_LINE |
Function to return the minimum required index | 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 DEDENT DEDENT DEDENT |
Short - circuit more comparisions as found the border point | break NEW_LINE |
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 . | return low if arr [ low ] == arr [ pos ] else i NEW_LINE |
Driver code | arr = [ 2 , 1 , 1 , 1 , 5 , 2 ] NEW_LINE |
Python3 implementation of the approach | import math as mt NEW_LINE |
Function to return the count of the required strings | def findAnswer ( str1 , str2 , n ) : NEW_LINE INDENT l , r = 0 , 0 NEW_LINE ans = 2 NEW_LINE DEDENT |
Searching index after longest common prefix ends | 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 |
Searching index before longest common suffix ends | 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 |
If only 1 character is different in both the strings | elif ( l == r ) : NEW_LINE INDENT return ans NEW_LINE DEDENT else : NEW_LINE |
Checking remaining part of string for equality | 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 |
Searching in right of string h ( g to h ) | 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 |
Driver code | str1 = " toy " NEW_LINE str2 = " try " NEW_LINE n = len ( str1 ) NEW_LINE print ( findAnswer ( str1 , str2 , n ) ) NEW_LINE |
Function to return the minimum required difference | def findMinDifference ( arr , n ) : NEW_LINE |
Sort the given array | arr . sort ( ) NEW_LINE |
When minimum element is removed | diff1 = arr [ n - 1 ] - arr [ 1 ] NEW_LINE |
When maximum element is removed | diff2 = arr [ n - 2 ] - arr [ 0 ] NEW_LINE |
Return the minimum of diff1 and diff2 | return min ( diff1 , diff2 ) NEW_LINE |
Driver Code | 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 |
Function to return the minimum required difference | 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 DEDENT |
If current element is greater than max | if ( arr [ i ] > max__ ) : NEW_LINE |
max will become secondMax | secondMax = max__ NEW_LINE |
Update the max | max__ = arr [ i ] NEW_LINE |
If current element is greater than secondMax but smaller than max | elif ( arr [ i ] > secondMax ) : NEW_LINE |
Update the secondMax | secondMax = arr [ i ] NEW_LINE |
If current element is smaller than min | elif ( arr [ i ] < min__ ) : NEW_LINE |
min will become secondMin | secondMin = min__ NEW_LINE |
Update the min | min__ = arr [ i ] NEW_LINE |
If current element is smaller than secondMin but greater than min | elif ( arr [ i ] < secondMin ) : NEW_LINE |
Update the secondMin | secondMin = arr [ i ] NEW_LINE |
Minimum of the two possible differences | diff = min ( max__ - secondMin , secondMax - min__ ) NEW_LINE return diff NEW_LINE |
Driver code | 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 |
Boolean function to check distinct digits of a number | def checkDistinct ( x ) : NEW_LINE |
Take last digit | last = x % 10 NEW_LINE |
Check if all other digits are same as last digit | while ( x ) : NEW_LINE INDENT if ( x % 10 != last ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT |
Remove last digit | x = x // 10 NEW_LINE return True NEW_LINE |
Function to return the count of integers that are composed of a single distinct digit only | def findCount ( L , R ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE DEDENT |
If i has single distinct digit | if ( checkDistinct ( i ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT return count NEW_LINE |
Driver code | L = 10 NEW_LINE R = 50 NEW_LINE print ( findCount ( L , R ) ) NEW_LINE |
Python3 program to print the sum of the minimum pair | import sys NEW_LINE |
Function to return the sum of the minimum pair from the array | 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 DEDENT |
If found new minimum | if ( a [ j ] < min ) : NEW_LINE |
Minimum now becomes second minimum | secondMin = min NEW_LINE |
Update minimum | min = a [ j ] NEW_LINE |
If current element is > min and < secondMin | elif ( ( a [ j ] < secondMin ) and a [ j ] != min ) : NEW_LINE |
Update secondMin | secondMin = a [ j ] NEW_LINE |
Return the sum of the minimum pair | return ( secondMin + min ) NEW_LINE |
Driver code | 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 |
function to find longest subarray | def longestsubarray ( arr , n , k ) : NEW_LINE INDENT current_count = 0 NEW_LINE DEDENT |
this will contain length of longest subarray found | 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 |
Driver code | 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 |
Python3 program to remove the elements which appear strictly less than k times from the array . | def removeElements ( arr , n , k ) : NEW_LINE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.