text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Possibility 3 | if a [ i ] + b [ i ] == x : NEW_LINE INDENT a [ i ] += b [ i ] NEW_LINE b [ i ] = 0 NEW_LINE continue NEW_LINE DEDENT |
Possibility 4 | if i + 1 < n and a [ i ] + b [ i + 1 ] == x : NEW_LINE INDENT a [ i ] += b [ i + 1 ] NEW_LINE b [ i + 1 ] = 0 NEW_LINE continue NEW_LINE DEDENT |
If a ( i ) can not be made equal to x even after adding all possible elements from b ( i ) then print - 1. | return - 1 NEW_LINE |
check whether all elements of b are used . | for i in range ( 0 , n ) : NEW_LINE INDENT if b [ i ] != 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT |
Return the new array element value . | return x NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 6 , 14 , 21 , 1 ] NEW_LINE b = [ 15 , 7 , 10 , 10 ] NEW_LINE n = len ( a ) NEW_LINE print ( solve ( a , b , n ) ) NEW_LINE DEDENT |
function to find the minimum days | def survival ( S , N , M ) : NEW_LINE |
If we can not buy at least a week supply of food during the first week OR We can not buy a day supply of food on the first day then we can 't survive. | if ( ( ( N * 6 ) < ( M * 7 ) and S > 6 ) or M > N ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT else : NEW_LINE |
If we can survive then we can buy ceil ( A / N ) times where A is total units of food required . | days = ( M * S ) / N NEW_LINE if ( ( ( M * S ) % N ) != 0 ) : NEW_LINE INDENT days += 1 NEW_LINE DEDENT print ( " Yes β " ) , NEW_LINE print ( days ) NEW_LINE |
Driver code | S = 10 ; N = 16 ; M = 2 NEW_LINE survival ( S , N , M ) NEW_LINE |
Find lexicographically largest subsequence of s [ 0. . n - 1 ] such that every character appears at least k times . The result is filled in t [ ] | def subsequence ( s , t , n , k ) : NEW_LINE INDENT last = 0 NEW_LINE cnt = 0 NEW_LINE new_last = 0 NEW_LINE size = 0 NEW_LINE string = ' zyxwvutsrqponmlkjihgfedcba ' NEW_LINE DEDENT |
Starting from largest charter ' z ' to 'a | ' NEW_LINE INDENT for ch in string : NEW_LINE INDENT cnt = 0 NEW_LINE DEDENT DEDENT |
Counting the frequency of the character | for i in range ( last , n ) : NEW_LINE INDENT if s [ i ] == ch : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT |
If frequency is greater than k | if cnt >= k : NEW_LINE |
From the last point we leave | for i in range ( last , n ) : NEW_LINE |
check if string contain ch | if s [ i ] == ch : NEW_LINE |
If yes , append to output string | t [ size ] = ch NEW_LINE new_last = i NEW_LINE size += 1 NEW_LINE |
Update the last point . | last = new_last NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = [ ' b ' , ' a ' , ' n ' , ' a ' , ' n ' , ' a ' ] NEW_LINE n = len ( s ) NEW_LINE k = 2 NEW_LINE t = [ ' ' ] * n NEW_LINE subsequence ( s , t , n - 1 , k ) NEW_LINE t = ' ' . join ( t ) NEW_LINE print ( t ) NEW_LINE DEDENT |
Function to find the largest permutation after k swaps | def bestpermutation ( arr , k , n ) : NEW_LINE |
Storing the elements and their index in map | h = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT h [ arr [ i ] ] = i NEW_LINE DEDENT |
If number of swaps allowed are equal to number of elements then the resulting permutation will be descending order of given permutation . | if ( n <= k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE arr . reverse ( ) NEW_LINE DEDENT else : NEW_LINE INDENT for j in range ( n , 0 , - 1 ) : NEW_LINE INDENT if ( k > 0 ) : NEW_LINE INDENT initial_index = h [ j ] NEW_LINE best_index = n - j NEW_LINE DEDENT DEDENT DEDENT |
If j is not at it 's best index | if ( initial_index != best_index ) : NEW_LINE INDENT h [ j ] = best_index NEW_LINE DEDENT |
Change the index of the element which was at position 0. Swap the element basically . | element = arr [ best_index ] NEW_LINE h [ element ] = initial_index NEW_LINE arr [ best_index ] , arr [ initial_index ] = ( arr [ initial_index ] , arr [ best_index ] ) NEW_LINE |
Decrement number of swaps | k -= 1 NEW_LINE |
Driver Code | arr = [ 3 , 1 , 4 , 2 , 5 ] NEW_LINE |
K is the number of swaps | k = 10 NEW_LINE |
n is the size of the array | n = len ( arr ) NEW_LINE |
Function calling | bestpermutation ( arr , k , n ) NEW_LINE print ( " Largest β possible β permutation β after " , k , " swaps β is " , end = " β " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " β " ) NEW_LINE DEDENT |
Function to allocate memory to blocks as per Best fit algorithm | def bestFit ( blockSize , m , processSize , n ) : NEW_LINE |
Stores block id of the block allocated to a process | allocation = [ - 1 ] * n NEW_LINE |
pick each process and find suitable blocks according to its size ad assign to it | for i in range ( n ) : NEW_LINE |
Find the best fit block for current process | bestIdx = - 1 NEW_LINE for j in range ( m ) : NEW_LINE INDENT if blockSize [ j ] >= processSize [ i ] : NEW_LINE INDENT if bestIdx == - 1 : NEW_LINE INDENT bestIdx = j NEW_LINE DEDENT elif blockSize [ bestIdx ] > blockSize [ j ] : NEW_LINE INDENT bestIdx = j NEW_LINE DEDENT DEDENT DEDENT |
If we could find a block for current process | if bestIdx != - 1 : NEW_LINE |
allocate block j to p [ i ] process | allocation [ i ] = bestIdx NEW_LINE |
Reduce available memory in this block . | blockSize [ bestIdx ] -= processSize [ i ] NEW_LINE print ( " Process β No . β Process β Size β Block β no . " ) NEW_LINE for i in range ( n ) : NEW_LINE print ( i + 1 , " β " , processSize [ i ] , end = " β " ) NEW_LINE if allocation [ i ] != - 1 : NEW_LINE print ( allocation [ i ] + 1 ) NEW_LINE else : NEW_LINE print ( " Not β Allocated " ) NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT blockSize = [ 100 , 500 , 200 , 300 , 600 ] NEW_LINE processSize = [ 212 , 417 , 112 , 426 ] NEW_LINE m = len ( blockSize ) NEW_LINE n = len ( processSize ) NEW_LINE bestFit ( blockSize , m , processSize , n ) NEW_LINE DEDENT |
Returns number of bins required using first fit online algorithm | def firstFit ( weight , n , c ) : NEW_LINE |
Initialize result ( Count of bins ) | res = 0 ; NEW_LINE |
Create an array to store remaining space in bins there can be at most n bins | bin_rem = [ 0 ] * n ; NEW_LINE |
Place items one by one | for i in range ( n ) : NEW_LINE |
Find the first bin that can accommodate weight [ i ] | j = 0 ; NEW_LINE |
Initialize minimum space left and index of best bin | min = c + 1 ; NEW_LINE bi = 0 ; NEW_LINE for j in range ( res ) : NEW_LINE INDENT if ( bin_rem [ j ] >= weight [ i ] and bin_rem [ j ] - weight [ i ] < min ) : NEW_LINE INDENT bi = j ; NEW_LINE min = bin_rem [ j ] - weight [ i ] ; NEW_LINE DEDENT DEDENT |
If no bin could accommodate weight [ i ] , create a new bin | if ( min == c + 1 ) : NEW_LINE INDENT bin_rem [ res ] = c - weight [ i ] ; NEW_LINE res += 1 ; NEW_LINE DEDENT |
else : Assign the item to best bin | bin_rem [ bi ] -= weight [ i ] ; NEW_LINE return res ; NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT weight = [ 2 , 5 , 4 , 7 , 1 , 3 , 8 ] ; NEW_LINE c = 10 ; NEW_LINE n = len ( weight ) ; NEW_LINE print ( " Number β of β bins β required β in β First β Fit β : β " , firstFit ( weight , n , c ) ) ; NEW_LINE DEDENT |
Utility function to get maximum element in job [ 0. . n - 1 ] | def getMax ( arr , n ) : NEW_LINE INDENT result = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if arr [ i ] > result : NEW_LINE INDENT result = arr [ i ] NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT |
Returns true if it is possible to finish jobs [ ] within given time 'time | ' NEW_LINE def isPossible ( time , K , job , n ) : NEW_LINE |
cnt is count of current assignees required for jobs | cnt = 1 NEW_LINE |
time assigned to current assignee | curr_time = 0 NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE |
If time assigned to current assignee exceeds max , increment count of assignees . | if curr_time + job [ i ] > time : NEW_LINE INDENT curr_time = 0 NEW_LINE cnt += 1 NEW_LINE DEDENT else : NEW_LINE |
Else add time of job to current time and move to next job . | curr_time += job [ i ] NEW_LINE i += 1 NEW_LINE |
Returns true if count is smaller than k | return cnt <= K NEW_LINE |
Returns minimum time required to finish given array of jobs k -- > number of assignees T -- > Time required by every assignee to finish 1 unit m -- > Number of jobs | def findMinTime ( K , T , job , n ) : NEW_LINE |
Set start and end for binary search end provides an upper limit on time | end = 0 NEW_LINE start = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT end += job [ i ] NEW_LINE DEDENT |
Find the job that takes maximum time | job_max = getMax ( job , n ) NEW_LINE |
Do binary search for minimum feasible time | while start <= end : NEW_LINE INDENT mid = int ( ( start + end ) / 2 ) NEW_LINE DEDENT |
If it is possible to finish jobs in mid time | if mid >= job_max and isPossible ( mid , K , job , n ) : NEW_LINE |
ans = min ( ans , mid ) Update answer | end = mid - 1 NEW_LINE else : NEW_LINE start = mid + 1 NEW_LINE return ans * T NEW_LINE |
Driver program | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT job = [ 10 , 7 , 8 , 12 , 6 , 8 ] NEW_LINE n = len ( job ) NEW_LINE k = 4 NEW_LINE T = 5 NEW_LINE print ( findMinTime ( k , T , job , n ) ) NEW_LINE DEDENT |
Function to calculate longest substring with odd or even elements | def LongestOddEvenSubarray ( A , N ) : NEW_LINE |
Initializing dp [ ] | dp = [ 0 for i in range ( N ) ] NEW_LINE |
Initializing dp [ 0 ] with 1 | dp [ 0 ] = 1 NEW_LINE |
ans will store the final answer | ans = 1 NEW_LINE |
Traversing the array from index 1 to N - 1 | for i in range ( 1 , N , 1 ) : NEW_LINE |
Checking both current and previous element is even or odd | if ( ( A [ i ] % 2 == 0 and A [ i - 1 ] % 2 == 0 ) or ( A [ i ] % 2 != 0 and A [ i - 1 ] % 2 != 0 ) ) : NEW_LINE |
Updating dp [ i ] with dp [ i - 1 ] + 1 | dp [ i ] = dp [ i - 1 ] + 1 NEW_LINE else : NEW_LINE dp [ i ] = 1 NEW_LINE for i in range ( N ) : NEW_LINE |
Storing max element to ans | ans = max ( ans , dp [ i ] ) NEW_LINE |
Returning the final answer | return ans NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Input | A = [ 2 , 5 , 7 , 2 , 4 , 6 , 8 , 3 ] NEW_LINE N = len ( A ) NEW_LINE |
Function call | print ( LongestOddEvenSubarray ( A , N ) ) NEW_LINE |
Function to count the subarrays with maximum not greater than K | def totalSubarrays ( arr , n , k ) : NEW_LINE INDENT ans = 0 NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE DEDENT |
If arr [ i ] > k then arr [ i ] cannot be a part of any subarray . | if ( arr [ i ] > k ) : NEW_LINE INDENT i += 1 NEW_LINE continue NEW_LINE DEDENT count = 0 NEW_LINE |
Count the number of elements where arr [ i ] is not greater than k . | while ( i < n and arr [ i ] <= k ) : NEW_LINE INDENT i += 1 NEW_LINE count += 1 NEW_LINE DEDENT |
Summation of all possible subarrays in the variable ans . | ans += ( ( count * ( count + 1 ) ) // 2 ) NEW_LINE return ans NEW_LINE |
Function to count the subarrays with maximum value is equal to K | def countSubarrays ( arr , n , k ) : NEW_LINE |
Stores count of subarrays with max <= k - 1. | count1 = totalSubarrays ( arr , n , k - 1 ) NEW_LINE |
Stores count of subarrays with max >= k + 1. | count2 = totalSubarrays ( arr , n , k ) NEW_LINE |
Stores count of subarrays with max = k . | ans = count2 - count1 NEW_LINE return ans NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Given Input | n = 4 NEW_LINE k = 3 NEW_LINE arr = [ 2 , 1 , 3 , 4 ] NEW_LINE |
Function Call | print ( countSubarrays ( arr , n , k ) ) NEW_LINE |
Function to calculate the maximum subsequence sum such that no three elements are consecutive | def maxSumWO3Consec ( A , N ) : NEW_LINE |
when N is 1 , answer would be the only element present | if ( N == 1 ) : NEW_LINE INDENT return A [ 0 ] NEW_LINE DEDENT |
when N is 2 , answer would be sum of elements | if ( N == 2 ) : NEW_LINE INDENT return A [ 0 ] + A [ 1 ] NEW_LINE DEDENT |
variable to store sum up to i - 3 | third = A [ 0 ] NEW_LINE |
variable to store sum up to i - 2 | second = third + A [ 1 ] NEW_LINE |
variable to store sum up to i - 1 | first = max ( second , A [ 1 ] + A [ 2 ] ) NEW_LINE |
variable to store the final sum of the subsequence | sum = max ( max ( third , second ) , first ) NEW_LINE for i in range ( 3 , N , 1 ) : NEW_LINE |
find the maximum subsequence sum up to index i | sum = max ( max ( first , second + A [ i ] ) , third + A [ i ] + A [ i - 1 ] ) NEW_LINE |
update first , second and third | third = second NEW_LINE second = first NEW_LINE first = sum NEW_LINE |
return ans ; | return sum NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Input | A = [ 3000 , 2000 , 1000 , 3 , 10 ] NEW_LINE N = len ( A ) NEW_LINE |
Function call | print ( maxSumWO3Consec ( A , N ) ) NEW_LINE |
Function to find the minimum number of operations required to reduce n to 2 | def findMinOperations ( n ) : NEW_LINE |
Initialize a dp array | dp = [ 0 for i in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT dp [ i ] = 999999 NEW_LINE DEDENT |
Handle the base case | dp [ 2 ] = 0 NEW_LINE |
Iterate in the range [ 2 , n ] | for i in range ( 2 , n + 1 ) : NEW_LINE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.