text
stringlengths
1
636
code
stringlengths
8
1.89k
Stores current element in string format
strEle = str ( arr [ i ] ) NEW_LINE
Left - shift digits of current element in all possible ways
for idx in range ( len ( strEle ) ) : NEW_LINE
Left - shift digits of current element by idx
temp = int ( strEle [ idx : ] + strEle [ : idx ] ) NEW_LINE
If temp greater than or equal to prev and temp less than optEle
if temp >= prev and temp < optEle : NEW_LINE INDENT optEle = temp NEW_LINE DEDENT
Update arr [ i ]
arr [ i ] = optEle NEW_LINE
Update prev
prev = arr [ i ] NEW_LINE
If arr is in increasing order
if isIncreasing ( arr ) : NEW_LINE INDENT return arr NEW_LINE DEDENT
Otherwise
else : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 511 , 321 , 323 , 432 , 433 ] NEW_LINE res = sortArr ( arr ) NEW_LINE for i in res : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Python3 program for the above approach
from bisect import bisect_left NEW_LINE
Function to find maximum shops that can be visited by K persons
def maximumShops ( opening , closing , n , k ) : NEW_LINE
Store opening and closing time of shops
a = [ [ 0 , 0 ] for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT a [ i ] [ 0 ] = opening [ i ] NEW_LINE a [ i ] [ 1 ] = closing [ i ] NEW_LINE DEDENT
Sort the pair of array
a = sorted ( a ) NEW_LINE
Stores the result
count = 1 NEW_LINE
Stores current number of persons visiting some shop with their ending time
st = { } NEW_LINE for i in range ( n ) : NEW_LINE
Check if current shop can be assigned to a person who 's already visiting any other shop
flag = False NEW_LINE if ( len ( st ) == 0 ) : NEW_LINE INDENT ar = list ( st . keys ( ) ) NEW_LINE it = bisect_left ( ar , a [ i ] [ 0 ] ) NEW_LINE if ( it != 0 ) : NEW_LINE INDENT it -= 1 NEW_LINE DEDENT DEDENT
Checks if there is any person whose closing time <= current shop opening time
if ( ar [ it ] <= a [ i ] [ 0 ] ) : NEW_LINE
Erase previous shop visited by the person satisfying the condition
del st [ it ] NEW_LINE
Insert new closing time of current shop for the person satisfying a1he condition
st [ a [ i ] [ 1 ] ] = 1 NEW_LINE
Increment the count by one
count += 1 NEW_LINE flag = True NEW_LINE
In case if no person have closing time <= current shop opening time but there are some persons left
if ( len ( st ) < k and flag == False ) : NEW_LINE INDENT st [ a [ i ] [ 1 ] ] = 1 NEW_LINE count += 1 NEW_LINE DEDENT
Finally pr the ans
return count NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Given starting and ending time
S = [ 1 , 8 , 3 , 2 , 6 ] NEW_LINE E = [ 5 , 10 , 6 , 5 , 9 ] NEW_LINE
Given K and N
K , N = 2 , len ( S ) NEW_LINE
Function call
print ( maximumShops ( S , E , N , K ) ) NEW_LINE
Function to find the maximum subarray sum possible by swapping elements from array arr [ ] with that from array brr [ ]
def maxSum ( arr , brr , N , K ) : NEW_LINE
Stores elements from the arrays arr [ ] and brr [ ]
crr = [ ] NEW_LINE
Store elements of array arr [ ] and brr [ ] in the vector crr
for i in range ( N ) : NEW_LINE INDENT crr . append ( arr [ i ] ) NEW_LINE DEDENT for i in range ( K ) : NEW_LINE INDENT crr . append ( brr [ i ] ) NEW_LINE DEDENT
Sort the vector crr in descending order
crr = sorted ( crr ) [ : : - 1 ] NEW_LINE
Stores maximum sum
sum = 0 NEW_LINE
Calculate the sum till the last index in crr [ ] which is less than N which contains a positive element
for i in range ( N ) : NEW_LINE INDENT if ( crr [ i ] > 0 ) : NEW_LINE INDENT sum += crr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT
Print the sum
print ( sum ) NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Given arrays and respective lengths
arr = [ 7 , 2 , - 1 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE brr = [ 1 , 2 , 3 , 2 ] NEW_LINE K = len ( brr ) NEW_LINE
Calculate maximum subarray sum
maxSum ( arr , brr , N , K ) NEW_LINE
Function that counts the pairs whose Bitwise XOR is greater than both the elements of pair
def countPairs ( A , N ) : NEW_LINE
Stores the count of pairs
count = 0 NEW_LINE
Generate all possible pairs
for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE DEDENT
Find the Bitwise XOR
xo = ( A [ i ] ^ A [ j ] ) NEW_LINE
Find the maximum of two
mx = max ( A [ i ] , A [ j ] ) NEW_LINE
If xo < mx , increment count
if ( xo > mx ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT
Print the value of count
print ( count ) NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE DEDENT
Function Call
countPairs ( arr , N ) NEW_LINE
Function to find the arrangement of seating
def findTheOrder ( arr , s , N ) : NEW_LINE
Stores the row in which the ith person sits
ans = [ ] NEW_LINE
Stores the width of seats along with their index or row number
A = [ [ ] for i in range ( N ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT A [ i ] = [ arr [ i ] , i + 1 ] NEW_LINE DEDENT
Sort the array
A = sorted ( A ) NEW_LINE
Store the seats and row for boy 's seat
q = [ ] NEW_LINE
Stores the index of row upto which boys have taken seat
index = 0 NEW_LINE
Iterate the string
for i in range ( 2 * N ) : NEW_LINE INDENT if ( s [ i ] == '0' ) : NEW_LINE DEDENT
Push the row number at index in vector and heap
ans . append ( A [ index ] [ 1 ] ) NEW_LINE q . append ( A [ index ] ) NEW_LINE
Increment the index to let the next boy in the next minimum width vacant row
index += 1 NEW_LINE
Otherwise
else : NEW_LINE
If girl then take top of element of the max heap
ans . append ( q [ - 1 ] [ 1 ] ) NEW_LINE
Pop from queue
del q [ - 1 ] NEW_LINE q = sorted ( q ) NEW_LINE
Print the values
for i in ans : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Given N
N = 3 NEW_LINE
Given arr [ ]
arr = [ 2 , 1 , 3 ] NEW_LINE
Given string
s = "001011" NEW_LINE
Function Call
findTheOrder ( arr , s , N ) NEW_LINE
Function to find the absolute difference between sum of first K maximum even and odd numbers
def evenOddDiff ( a , n , k ) : NEW_LINE
Stores index from where odd number starts
j = - 1 NEW_LINE even = [ ] NEW_LINE odd = [ ] NEW_LINE
Segregate even and odd number
for i in range ( n ) : NEW_LINE
If current element is even
if ( a [ i ] % 2 == 0 ) : NEW_LINE INDENT even . append ( a [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT odd . append ( a [ i ] ) NEW_LINE DEDENT j += 1 NEW_LINE
Sort in decreasing order even part
even . sort ( ) NEW_LINE even . reverse ( ) NEW_LINE
Sort in decreasing order odd part
odd . sort ( ) NEW_LINE odd . reverse ( ) NEW_LINE evenSum , oddSum = 0 , 0 NEW_LINE
Calculate sum of k maximum even number
for i in range ( k ) : NEW_LINE INDENT evenSum += even [ i ] NEW_LINE DEDENT
Calculate sum of k maximum odd number
for i in range ( k ) : NEW_LINE INDENT oddSum += odd [ i ] NEW_LINE DEDENT
Print the absolute difference
print ( abs ( evenSum - oddSum ) ) NEW_LINE
Given array [ ] arr
arr = [ 1 , 8 , 3 , 4 , 5 ] NEW_LINE
Size of array
N = len ( arr ) NEW_LINE K = 2 NEW_LINE
Function Call
evenOddDiff ( arr , N , K ) NEW_LINE
Function to rearrange array that satisfies the given condition
def rearrangeArr ( arr , N ) : NEW_LINE
Stores sum of elements of the given array
totalSum = 0 NEW_LINE
Calculate totalSum
for i in range ( N ) : NEW_LINE INDENT totalSum += arr [ i ] NEW_LINE DEDENT
If the totalSum is equal to 0
if ( totalSum == 0 ) : NEW_LINE
No possible way to rearrange array
print ( - 1 ) NEW_LINE
If totalSum exceeds 0
elif ( totalSum > 0 ) : NEW_LINE
Rearrange the array in descending order
arr . sort ( reverse = True ) NEW_LINE print ( * arr , sep = ' ▁ ' ) NEW_LINE
Otherwise
else : NEW_LINE
Rearrange the array in ascending order
arr . sort ( ) NEW_LINE print ( * arr , sep = ' ▁ ' ) NEW_LINE
Driver Code
arr = [ 1 , - 1 , - 2 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE rearrangeArr ( arr , N ) ; NEW_LINE
Python3 program for the above approach
from collections import defaultdict NEW_LINE
Function to create the frequency array of the given array arr [ ]
def findSubsets ( arr ) : NEW_LINE
Hashmap to store the frequencies
M = defaultdict ( int ) NEW_LINE
Store freq for each element
for i in range ( len ( arr ) ) : NEW_LINE INDENT M [ arr [ i ] ] += 1 NEW_LINE DEDENT
Get the total frequencies
subsets = [ 0 ] * len ( M ) NEW_LINE i = 0 NEW_LINE
Store frequencies in subset [ ] array
for j in M : NEW_LINE INDENT subsets [ i ] = M [ j ] NEW_LINE i += 1 NEW_LINE DEDENT
Return frequency array
return subsets NEW_LINE
Function to check is sum N / 2 can be formed using some subset
def subsetSum ( subsets , target ) : NEW_LINE
dp [ i ] [ j ] store the answer to form sum j using 1 st i elements
dp = [ [ 0 for x in range ( target + 1 ) ] for y in range ( len ( subsets ) + 1 ) ] NEW_LINE
Initialize dp [ ] [ ] with true
for i in range ( len ( dp ) ) : NEW_LINE INDENT dp [ i ] [ 0 ] = True NEW_LINE DEDENT
Fill the subset table in the bottom up manner
for i in range ( 1 , len ( subsets ) + 1 ) : NEW_LINE INDENT for j in range ( 1 , target + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT
If current element is less than j
if ( j >= subsets [ i - 1 ] ) : NEW_LINE
Update current state
dp [ i ] [ j ] |= ( dp [ i - 1 ] [ j - subsets [ i - 1 ] ] ) NEW_LINE
Return the result
return dp [ len ( subsets ) ] [ target ] NEW_LINE
Function to check if the given array can be split into required sets
def divideInto2Subset ( arr ) : NEW_LINE