text
stringlengths
1
636
code
stringlengths
8
1.89k
To stores the index of mismatched element
ind = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] != b [ i ] ) : NEW_LINE DEDENT
If there is more than one mismatch then return False
if ( fl == True ) : NEW_LINE INDENT return False NEW_LINE DEDENT fl = True NEW_LINE ind = i NEW_LINE
If there is no mismatch or the difference between the mismatching elements is <= k then return true
if ( ind == - 1 or abs ( a [ ind ] - b [ ind ] ) <= k ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE
Driver code
n , k = 2 , 4 NEW_LINE a = [ 1 , 5 ] NEW_LINE b = [ 1 , 1 ] NEW_LINE if ( check ( n , k , a , b ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Function to return sum of width of all subsets
def SubseqWidths ( A ) : NEW_LINE
Sort the array
MOD = 10 ** 9 + 7 NEW_LINE N = len ( A ) NEW_LINE A . sort ( ) NEW_LINE pow2 = [ 1 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT pow2 . append ( pow2 [ - 1 ] * 2 % MOD ) NEW_LINE DEDENT ans = 0 NEW_LINE for i , x in enumerate ( A ) : NEW_LINE INDENT ans = ( ans + ( pow2 [ i ] - pow2 [ N - 1 - i ] ) * x ) % MOD NEW_LINE DEDENT return ans NEW_LINE
Driver program
A = [ 5 , 6 , 4 , 3 , 8 ] NEW_LINE print ( SubseqWidths ( A ) ) NEW_LINE
Function to find value for covering maximum array elements
def maxArrayCover ( a , n , x ) : NEW_LINE
sort the students in ascending based on the candies
a . sort ( ) NEW_LINE
To store the number of happy students
cc = 0 NEW_LINE
To store the running sum
s = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT s += a [ i ] NEW_LINE DEDENT
If the current student can 't be made happy
if ( s > x ) : NEW_LINE INDENT break NEW_LINE DEDENT
increment the count if we can make the ith student happy
cc += 1 NEW_LINE
If the sum = x then answer is n
if ( sum ( a ) == x ) : NEW_LINE INDENT return n NEW_LINE DEDENT else : NEW_LINE
If the count is equal to n then the answer is n - 1
if ( cc == n ) : NEW_LINE INDENT return n - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return cc NEW_LINE DEDENT
Driver function
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n , x = 3 , 70 NEW_LINE a = [ 10 , 20 , 30 ] NEW_LINE print ( maxArrayCover ( a , n , x ) ) NEW_LINE DEDENT
Linked List node
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT start = None NEW_LINE
Function to check if linked list is pairwise sorted
def isPairWiseSorted ( head ) : NEW_LINE INDENT flag = True NEW_LINE temp = head NEW_LINE DEDENT
Traverse further only if there are at - least two nodes left
while ( temp != None and temp . next != None ) : NEW_LINE INDENT if ( temp . data > temp . next . data ) : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT temp = temp . next . next NEW_LINE DEDENT return flag NEW_LINE
Function to add a node at the beginning of Linked List
def push ( head_ref , new_data ) : NEW_LINE INDENT global start NEW_LINE DEDENT
allocate node
new_node = Node ( 0 ) NEW_LINE
put in the data
new_node . data = new_data NEW_LINE
link the old list off the new node
new_node . next = head_ref NEW_LINE
move the head to point to the new node
head_ref = new_node NEW_LINE start = head_ref NEW_LINE
Driver Code
start = None NEW_LINE
The constructed linked list is : 10.15 . 9.9 .1 .5
push ( start , 5 ) NEW_LINE push ( start , 1 ) NEW_LINE push ( start , 9 ) NEW_LINE push ( start , 9 ) NEW_LINE push ( start , 15 ) NEW_LINE push ( start , 10 ) NEW_LINE if ( isPairWiseSorted ( start ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Function that calculates maximum sum of products of two arrays
def maximumSOP ( a , b ) : NEW_LINE
Variable to store the sum of products of array elements
sop = 0 NEW_LINE
length of the arrays
n = len ( a ) NEW_LINE
Sorting both the arrays
a . sort ( ) NEW_LINE b . sort ( ) NEW_LINE
Traversing both the arrays and calculating sum of product
for i in range ( n ) : NEW_LINE INDENT sop += a [ i ] * b [ i ] NEW_LINE DEDENT return sop NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 2 , 3 ] NEW_LINE B = [ 4 , 5 , 1 ] NEW_LINE print ( maximumSOP ( A , B ) ) NEW_LINE DEDENT
Function to count such triplets
def countTriplets ( arr , n , m ) : NEW_LINE INDENT count = 0 NEW_LINE DEDENT
Sort the array
arr . sort ( ) NEW_LINE
three pointer technique
for end in range ( n - 1 , 1 , - 1 ) : NEW_LINE INDENT start = 0 NEW_LINE mid = end - 1 NEW_LINE while ( start < mid ) : NEW_LINE DEDENT
Calculate the product of a triplet
prod = ( arr [ end ] * arr [ start ] * arr [ mid ] ) NEW_LINE
Check if that product is greater than m , decrement mid
if ( prod > m ) : NEW_LINE INDENT mid -= 1 NEW_LINE DEDENT
Check if that product is smaller than m , increment start
elif ( prod < m ) : NEW_LINE INDENT start += 1 NEW_LINE DEDENT
Check if that product is equal to m , decrement mid , increment start and increment the count of pairs
elif ( prod == m ) : NEW_LINE INDENT count += 1 NEW_LINE mid -= 1 NEW_LINE start += 1 NEW_LINE DEDENT return count NEW_LINE
Drivers code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 1 , 1 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE m = 1 NEW_LINE print ( countTriplets ( arr , n , m ) ) NEW_LINE DEDENT
Python3 program to equally divide n elements into two sets such that second set has maximum distinct elements .
def distribution ( arr , n ) : NEW_LINE INDENT resources = set ( ) NEW_LINE DEDENT
Insert all the resources in the set There will be unique resources in the set
for i in range ( n ) : NEW_LINE INDENT resources . add ( arr [ i ] ) ; NEW_LINE DEDENT
return minimum of distinct resources and n / 2
return min ( len ( resources ) , n // 2 ) ; NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 1 , 3 , 4 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( distribution ( arr , n ) , " " ) ; NEW_LINE DEDENT
Python3 program to sort an array of size 3
def sort3 ( arr ) : NEW_LINE
Insert arr [ 1 ]
if ( arr [ 1 ] < arr [ 0 ] ) : NEW_LINE INDENT arr [ 0 ] , arr [ 1 ] = arr [ 1 ] , arr [ 0 ] NEW_LINE DEDENT
Insert arr [ 2 ]
if ( arr [ 2 ] < arr [ 1 ] ) : NEW_LINE INDENT arr [ 1 ] , arr [ 2 ] = arr [ 2 ] , arr [ 1 ] NEW_LINE if ( arr [ 1 ] < arr [ 0 ] ) : NEW_LINE INDENT arr [ 1 ] , arr [ 0 ] = arr [ 0 ] , arr [ 1 ] NEW_LINE DEDENT DEDENT
Driver code
a = [ 10 , 12 , 5 ] NEW_LINE sort3 ( a ) NEW_LINE for i in range ( 3 ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT
Python3 program to sort an array using merge sort such that merge operation takes O ( 1 ) extra space .
def merge ( arr , beg , mid , end , maxele ) : NEW_LINE INDENT i = beg NEW_LINE j = mid + 1 NEW_LINE k = beg NEW_LINE while ( i <= mid and j <= end ) : NEW_LINE INDENT if ( arr [ i ] % maxele <= arr [ j ] % maxele ) : NEW_LINE INDENT arr [ k ] = arr [ k ] + ( arr [ i ] % maxele ) * maxele NEW_LINE k += 1 NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ k ] = arr [ k ] + ( arr [ j ] % maxele ) * maxele NEW_LINE k += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT while ( i <= mid ) : NEW_LINE INDENT arr [ k ] = arr [ k ] + ( arr [ i ] % maxele ) * maxele NEW_LINE k += 1 NEW_LINE i += 1 NEW_LINE DEDENT while ( j <= end ) : NEW_LINE INDENT arr [ k ] = arr [ k ] + ( arr [ j ] % maxele ) * maxele NEW_LINE k += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT
Obtaining actual values
for i in range ( beg , end + 1 ) : NEW_LINE INDENT arr [ i ] = arr [ i ] // maxele NEW_LINE DEDENT
Recursive merge sort with extra parameter , naxele
def mergeSortRec ( arr , beg , end , maxele ) : NEW_LINE INDENT if ( beg < end ) : NEW_LINE INDENT mid = ( beg + end ) // 2 NEW_LINE mergeSortRec ( arr , beg , mid , maxele ) NEW_LINE mergeSortRec ( arr , mid + 1 , end , maxele ) NEW_LINE merge ( arr , beg , mid , end , maxele ) NEW_LINE DEDENT DEDENT
This functions finds max element and calls recursive merge sort .
def mergeSort ( arr , n ) : NEW_LINE INDENT maxele = max ( arr ) + 1 NEW_LINE mergeSortRec ( arr , 0 , n - 1 , maxele ) NEW_LINE DEDENT
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 999 , 612 , 589 , 856 , 56 , 945 , 243 ] NEW_LINE n = len ( arr ) NEW_LINE mergeSort ( arr , n ) NEW_LINE print ( " Sorted ▁ array " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Python3 program to print triplets with sum smaller than a given value
def printTriplets ( arr , n , sum ) : NEW_LINE
Sort input array
arr . sort ( ) NEW_LINE
Every iteration of loop counts triplet with first element as arr [ i ] .
for i in range ( n - 2 ) : NEW_LINE
Initialize other two elements as corner elements of subarray arr [ j + 1. . k ]
( j , k ) = ( i + 1 , n - 1 ) NEW_LINE
Use Meet in the Middle concept
while ( j < k ) : NEW_LINE
If sum of current triplet is more or equal , move right corner to look for smaller values
if ( arr [ i ] + arr [ j ] + arr [ k ] >= sum ) : NEW_LINE INDENT k -= 1 NEW_LINE DEDENT
Else move left corner
else : NEW_LINE
This is important . For current i and j , there are total k - j third elements .
for x in range ( j + 1 , k + 1 ) : NEW_LINE INDENT print ( str ( arr [ i ] ) + " , ▁ " + str ( arr [ j ] ) + " , ▁ " + str ( arr [ x ] ) ) NEW_LINE DEDENT j += 1 NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 1 , 3 , 4 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE sum = 12 NEW_LINE printTriplets ( arr , n , sum ) ; NEW_LINE DEDENT
function to print the characters in sorted order
def printSorted ( s , l ) : NEW_LINE
primary stack
stack = [ ] NEW_LINE
secondary stack
tempstack = [ ] NEW_LINE
append first character
stack . append ( s [ 0 ] ) NEW_LINE
iterate for all character in string
for i in range ( 1 , l ) : NEW_LINE
i - th character ASCII
a = ord ( s [ i ] ) NEW_LINE
stack 's top element ASCII
b = ord ( stack [ - 1 ] ) NEW_LINE
if greater or equal to top element then push to stack
if ( ( a - b ) >= 1 or ( a == b ) ) : NEW_LINE INDENT stack . append ( s [ i ] ) NEW_LINE DEDENT
if smaller , then push all element to the temporary stack
elif ( ( b - a ) >= 1 ) : NEW_LINE
push all greater elements
while ( ( b - a ) >= 1 ) : NEW_LINE
push operation
tempstack . append ( stack . pop ( ) ) NEW_LINE
push till the stack is not - empty
if ( len ( stack ) > 0 ) : NEW_LINE INDENT b = ord ( stack [ - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT
push the i - th character
stack . append ( s [ i ] ) NEW_LINE
push the tempstack back to stack
while ( len ( tempstack ) > 0 ) : NEW_LINE INDENT stack . append ( tempstack . pop ( ) ) NEW_LINE DEDENT
print the stack in reverse order
print ( ' ' . join ( stack ) ) NEW_LINE
Driver Code
s = " geeksforgeeks " NEW_LINE l = len ( s ) NEW_LINE printSorted ( s , l ) NEW_LINE
Returns true if the array A can be fit into array B , otherwise false
def checkFittingArrays ( A , B , N ) : NEW_LINE
Sort both the arrays
A = sorted ( A ) NEW_LINE B = sorted ( B ) NEW_LINE
Iterate over the loop and check whether every array element of A is less than or equal to its corresponding array element of B
for i in range ( N ) : NEW_LINE INDENT if ( A [ i ] > B [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE
Driver Code
A = [ 7 , 5 , 3 , 2 ] NEW_LINE B = [ 5 , 4 , 8 , 7 ] NEW_LINE N = len ( A ) NEW_LINE if ( checkFittingArrays ( A , B , N ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
This functions returns the required number of toys
def maximum_toys ( cost , N , K ) : NEW_LINE INDENT count = 0 NEW_LINE sum = 0 NEW_LINE DEDENT
sort the cost array
cost . sort ( reverse = False ) NEW_LINE for i in range ( 0 , N , 1 ) : NEW_LINE
Check if we can buy ith toy or not
if ( sum + cost [ i ] <= K ) : NEW_LINE INDENT sum = sum + cost [ i ] NEW_LINE DEDENT
Increment the count variable
count += 1 NEW_LINE return count NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 50 NEW_LINE cost = [ 1 , 12 , 5 , 111 , 200 , 1000 , 10 , 9 , 12 , 15 ] NEW_LINE N = len ( cost ) NEW_LINE print ( maximum_toys ( cost , N , K ) ) NEW_LINE DEDENT
Python Code Implementation of the above approach
def areBookingsPossible ( A , B , K ) : NEW_LINE INDENT A . sort ( ) NEW_LINE B . sort ( ) NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT if i + K < len ( A ) and A [ i + K ] < B [ i ] : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT return " Yes " NEW_LINE DEDENT
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arrival = [ 1 , 2 , 3 ] NEW_LINE departure = [ 2 , 3 , 4 ] NEW_LINE K = 1 NEW_LINE print areBookingsPossible ( arrival , departure , K ) NEW_LINE DEDENT
Recursive python program to sort an array by swapping elements
import math NEW_LINE
Utility function to print a Vector
def printVector ( V ) : NEW_LINE INDENT for i in V : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT print ( " ▁ " ) NEW_LINE DEDENT
Function to perform Insertion Sort recursively
def insertionSortRecursive ( V , N ) : NEW_LINE INDENT if ( N <= 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT
General Case Sort V till second last element and then insert last element into V
insertionSortRecursive ( V , N - 1 ) NEW_LINE
Insertion step
j = N - 1 NEW_LINE
Insert V [ i ] into list 0. . i - 1
while ( j > 0 and V [ j ] < V [ j - 1 ] ) : NEW_LINE
Swap V [ j ] and V [ j - 1 ]
temp = V [ j ] ; NEW_LINE V [ j ] = V [ j - 1 ] ; NEW_LINE V [ j - 1 ] = temp ; NEW_LINE
Decrement j
j -= 1 NEW_LINE
Driver method
A = [ 9 , 8 , 7 , 5 , 2 , 1 , 2 , 3 ] NEW_LINE n = len ( A ) NEW_LINE print ( " Array " ) NEW_LINE printVector ( A ) NEW_LINE print ( " After ▁ Sorting ▁ : " ) NEW_LINE insertionSortRecursive ( A , n ) NEW_LINE printVector ( A ) NEW_LINE
Function for checking almost sort
def almostSort ( A , n ) : NEW_LINE
One by one compare adjacents .
i = 0 NEW_LINE while i < n - 1 : NEW_LINE INDENT if A [ i ] > A [ i + 1 ] : NEW_LINE INDENT A [ i ] , A [ i + 1 ] = A [ i + 1 ] , A [ i ] NEW_LINE i += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT