text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Finding the index from where the even numbers will be stored | if ( n % 2 == 0 ) : NEW_LINE INDENT pos = n // 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT pos = ( n // 2 ) + 1 ; NEW_LINE DEDENT |
Return the kth element | if ( k <= pos ) : NEW_LINE INDENT return ( k * 2 - 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( ( k - pos ) * 2 ) ; NEW_LINE DEDENT |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 8 ; k = 5 ; NEW_LINE print ( getNumber ( n , k ) ) ; NEW_LINE DEDENT |
Function to find all unique combination of given elements such that their sum is K | def unique_combination ( l , sum , K , local , A ) : NEW_LINE |
If a unique combination is found | if ( sum == K ) : NEW_LINE INDENT print ( " { " , end = " " ) NEW_LINE for i in range ( len ( local ) ) : NEW_LINE INDENT if ( i != 0 ) : NEW_LINE INDENT print ( " β " , end = " " ) NEW_LINE DEDENT print ( local [ i ] , end = " " ) NEW_LINE if ( i != len ( local ) - 1 ) : NEW_LINE INDENT print ( " , β " , end = " " ) NEW_LINE DEDENT DEDENT print ( " } " ) NEW_LINE return NEW_LINE DEDENT |
For all other combinations | for i in range ( l , len ( A ) , 1 ) : NEW_LINE |
Check if the sum exceeds K | if ( sum + A [ i ] > K ) : NEW_LINE INDENT continue NEW_LINE DEDENT |
Check if it is repeated or not | if ( i > l and A [ i ] == A [ i - 1 ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT |
Take the element into the combination | local . append ( A [ i ] ) NEW_LINE |
Recursive call | unique_combination ( i + 1 , sum + A [ i ] , K , local , A ) NEW_LINE |
Remove element from the combination | local . remove ( local [ len ( local ) - 1 ] ) NEW_LINE |
Function to find all combination of the given elements | def Combination ( A , K ) : NEW_LINE |
Sort the given elements | A . sort ( reverse = False ) NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 10 , 1 , 2 , 7 , 6 , 1 , 5 ] NEW_LINE K = 8 NEW_LINE DEDENT |
Function call | Combination ( A , K ) NEW_LINE |
Function that returns true if string s can be made up of by other two string from the array after concatenating one after another | def canbuildword ( s , isoriginalword , mp ) : NEW_LINE |
If current string has been processed before | if s in mp and mp [ s ] == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT |
If current string is found in the map and it is not the string under consideration | if s in mp and mp [ s ] == 1 and isoriginalword == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( 1 , len ( s ) ) : NEW_LINE |
Split the string into two contiguous sub - strings | left = s [ : i ] NEW_LINE right = s [ i : ] NEW_LINE |
If left sub - string is found in the map and the right sub - string can be made from the strings from the given array | if left in mp and mp [ left ] == 1 and canbuildword ( right , 0 , mp ) : NEW_LINE INDENT return True NEW_LINE DEDENT |
If everything failed , we return false | mp [ s ] = 0 NEW_LINE return False NEW_LINE |
Function to return the longest string that can made be made up from the other string of the given array | def printlongestword ( listofwords ) : NEW_LINE |
Put all the strings in the map | mp = dict ( ) NEW_LINE for i in listofwords : NEW_LINE INDENT mp [ i ] = 1 NEW_LINE DEDENT |
Sort the string in decreasing order of their lengths | listofwords . sort ( key = lambda x : len ( x ) , reverse = True ) NEW_LINE |
Starting from the longest string | for i in listofwords : NEW_LINE |
If current string can be made up from other strings | if canbuildword ( i , 1 , mp ) : NEW_LINE INDENT return i NEW_LINE DEDENT return " - 1" NEW_LINE |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT listofwords = [ " geeks " , " for " , " geeksfor " , " geeksforgeeks " ] NEW_LINE print ( printlongestword ( listofwords ) ) NEW_LINE DEDENT |
Python3 implementation of the above approach | import sys NEW_LINE |
Function to return the sum of a triplet which is closest to x | def solution ( arr , x ) : NEW_LINE |
To store the closest sum | closestSum = sys . maxsize NEW_LINE |
Run three nested loops each loop for each element of triplet | for i in range ( len ( arr ) ) : NEW_LINE INDENT for j in range ( i + 1 , len ( arr ) ) : NEW_LINE INDENT for k in range ( j + 1 , len ( arr ) ) : NEW_LINE DEDENT DEDENT |
Update the closestSum | if ( abs ( x - closestSum ) > abs ( x - ( arr [ i ] + arr [ j ] + arr [ k ] ) ) ) : NEW_LINE INDENT closestSum = ( arr [ i ] + arr [ j ] + arr [ k ] ) NEW_LINE DEDENT |
Return the closest sum found | return closestSum NEW_LINE |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ - 1 , 2 , 1 , - 4 ] NEW_LINE x = 1 NEW_LINE print ( solution ( arr , x ) ) NEW_LINE DEDENT |
Python3 implementation of the approach | from collections import deque NEW_LINE adj = [ [ ] for i in range ( 100 ) ] NEW_LINE |
Function to add edge to graph | def addEdge ( u , v ) : NEW_LINE INDENT adj [ u ] . append ( v ) NEW_LINE DEDENT |
Function to calculate indegrees of all the vertices | def getindeg ( V , indeg ) : NEW_LINE |
If there is an edge from i to x then increment indegree of x | for i in range ( V ) : NEW_LINE INDENT for x in adj [ i ] : NEW_LINE INDENT indeg [ x ] += 1 NEW_LINE DEDENT DEDENT |
Function to perform topological sort | def topo ( V , indeg ) : NEW_LINE INDENT q = deque ( ) NEW_LINE DEDENT |
Push every node to the queue which has no incoming edge | for i in range ( V ) : NEW_LINE INDENT if ( indeg [ i ] == 0 ) : NEW_LINE INDENT q . appendleft ( i ) NEW_LINE DEDENT DEDENT res = [ ] NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT u = q . popleft ( ) NEW_LINE res . append ( u ) NEW_LINE DEDENT |
Since edge u is removed , update the indegrees of all the nodes which had an incoming edge from u | for x in adj [ u ] : NEW_LINE INDENT indeg [ x ] -= 1 NEW_LINE if ( indeg [ x ] == 0 ) : NEW_LINE INDENT q . appendleft ( x ) NEW_LINE DEDENT DEDENT return res NEW_LINE |
Function to generate the array from the given sub - sequences | def makearray ( v , V ) : NEW_LINE |
Create the graph from the input sub - sequences | for i in range ( len ( v ) ) : NEW_LINE INDENT for j in range ( len ( v [ i ] ) - 1 ) : NEW_LINE DEDENT |
Add edge between every two consecutive elements of the given sub - sequences | addEdge ( v [ i ] [ j ] , v [ i ] [ j + 1 ] ) NEW_LINE |
Get the indegrees for all the vertices | indeg = [ 0 for i in range ( V ) ] NEW_LINE getindeg ( V , indeg ) NEW_LINE |
Get the topological order of the created graph | res = topo ( V , indeg ) NEW_LINE return res NEW_LINE |
Size of the required array | n = 10 NEW_LINE |
Given sub - sequences of the array | subseqs = [ [ 9 , 1 , 2 , 8 , 3 ] , [ 6 , 1 , 2 ] , [ 9 , 6 , 3 , 4 ] , [ 5 , 2 , 7 ] , [ 0 , 9 , 5 , 4 ] ] NEW_LINE |
Get the resultant array as vector | res = makearray ( subseqs , n ) NEW_LINE |
Printing the array | for x in res : NEW_LINE INDENT print ( x , end = " β " ) NEW_LINE DEDENT |
Vector to store the primes | pr = [ ] NEW_LINE |
Create a boolean array " prime [ 0 . . n ] " | prime = [ 1 for i in range ( 10000000 + 1 ) ] NEW_LINE def sieve ( n ) : NEW_LINE |
Initialize along prime values to be true | for p in range ( 2 , n ) : NEW_LINE INDENT if p * p > n : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT |
If prime [ p ] is not changed then it is a prime | if ( prime [ p ] == True ) : NEW_LINE |
Update amultiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked | for i in range ( 2 * p , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT |
Praprime numbers | for p in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT pr . append ( p ) NEW_LINE DEDENT DEDENT |
Function to return the semi - prime sum | def SemiPrimeSum ( N ) : NEW_LINE |
Variable to store the sum of semi - primes | ans = 0 NEW_LINE |
Iterate over the prime values | for i in range ( len ( pr ) ) : NEW_LINE INDENT for j in range ( i , len ( pr ) ) : NEW_LINE DEDENT |
Break the loop once the product exceeds N | if ( pr [ i ] * pr [ j ] > N ) : NEW_LINE INDENT break NEW_LINE DEDENT |
Add valid products which are less than or equal to N each product is a semi - prime number | ans += pr [ i ] * pr [ j ] NEW_LINE return ans NEW_LINE |
Driver code | N = 6 NEW_LINE sieve ( N ) NEW_LINE print ( SemiPrimeSum ( N ) ) NEW_LINE |
Function to compress the array ranges | def compressArr ( arr , n ) : NEW_LINE INDENT i = 0 ; NEW_LINE j = 0 ; NEW_LINE arr . sort ( ) ; NEW_LINE while ( i < n ) : NEW_LINE DEDENT |
start iteration from the ith array element | j = i ; NEW_LINE |
loop until arr [ i + 1 ] == arr [ i ] and increment j | while ( ( j + 1 < n ) and ( arr [ j + 1 ] == arr [ j ] + 1 ) ) : NEW_LINE INDENT j += 1 ; NEW_LINE DEDENT |
if the program do not enter into the above while loop this means that ( i + 1 ) th element is not consecutive to i th element | if ( i == j ) : NEW_LINE INDENT print ( arr [ i ] , end = " β " ) ; NEW_LINE DEDENT |
increment i for next iteration | i += 1 ; NEW_LINE else : NEW_LINE |
print the consecutive range found | print ( arr [ i ] , " - " , arr [ j ] , end = " β " ) ; NEW_LINE |
move i jump directly to j + 1 | i = j + 1 ; NEW_LINE |
Driver code | n = 7 ; NEW_LINE arr = [ 1 , 3 , 4 , 5 , 6 , 9 , 10 ] ; NEW_LINE compressArr ( arr , n ) ; NEW_LINE |
Function to sort the array by removing misplaced elements | def removeElements ( arr , n ) : NEW_LINE |
brr [ ] is used to store the sorted array elements | brr = [ 0 ] * n ; l = 1 ; NEW_LINE brr [ 0 ] = arr [ 0 ] ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( brr [ l - 1 ] <= arr [ i ] ) : NEW_LINE INDENT brr [ l ] = arr [ i ] ; NEW_LINE l += 1 ; NEW_LINE DEDENT DEDENT |
Print the sorted array | for i in range ( l ) : NEW_LINE INDENT print ( brr [ i ] , end = " β " ) ; NEW_LINE DEDENT |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 12 , 9 , 10 , 2 , 13 , 14 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE removeElements ( arr , n ) ; NEW_LINE DEDENT |
Function to sort the array by removing misplaced elements | def removeElements ( arr , n ) : NEW_LINE |
l stores the index | l = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ l - 1 ] <= arr [ i ] ) : NEW_LINE INDENT arr [ l ] = arr [ i ] NEW_LINE l += 1 NEW_LINE DEDENT DEDENT |
Print the sorted array | for i in range ( l ) : NEW_LINE INDENT print ( arr [ i ] , end = " β " ) NEW_LINE DEDENT |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 12 , 9 , 10 , 2 , 13 , 14 ] NEW_LINE n = len ( arr ) NEW_LINE removeElements ( arr , n ) NEW_LINE DEDENT |
Python3 implementation of the approach Function that returns X | import math NEW_LINE |
Function that returns X | def findX ( list , int ) : NEW_LINE |
Sort the given array | list . sort ( ) NEW_LINE |
Get the possible X | x = list [ 0 ] * list [ int - 1 ] NEW_LINE |
Container to store divisors | vec = [ ] NEW_LINE |
Find the divisors of x | i = 2 NEW_LINE while ( i * i <= x ) : NEW_LINE |
Check if divisor | if ( x % i == 0 ) : NEW_LINE INDENT vec . append ( i ) NEW_LINE if ( ( x // i ) != i ) : NEW_LINE INDENT vec . append ( x // i ) NEW_LINE DEDENT DEDENT i += 1 NEW_LINE |
sort the vec because a is sorted and we have to compare all the elements | vec . sort ( ) NEW_LINE |
if size of both vectors is not same then we are sure that both vectors can 't be equal | if ( len ( vec ) != int ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE |
Check if a and vec have same elements in them | j = 0 NEW_LINE for it in range ( int ) : NEW_LINE INDENT if ( a [ j ] != vec [ it ] ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT j += 1 NEW_LINE DEDENT DEDENT return x NEW_LINE |
Driver code | a = [ 2 , 5 , 4 , 10 ] NEW_LINE n = len ( a ) NEW_LINE |
Function call | print ( findX ( a , n ) ) NEW_LINE |
Function to print the Pendulum arrangement of the given array | def pendulumArrangement ( arr , n ) : NEW_LINE |
Sort the array | arr . sort ( reverse = False ) NEW_LINE |
pos stores the index of the last element of the array | pos = n - 1 NEW_LINE |
odd stores the last odd index in the array | if ( n % 2 == 0 ) : NEW_LINE INDENT odd = n - 1 NEW_LINE DEDENT else : NEW_LINE INDENT odd = n - 2 NEW_LINE DEDENT |
Move all odd index positioned elements to the right | while ( odd > 0 ) : NEW_LINE INDENT temp = arr [ odd ] NEW_LINE in1 = odd NEW_LINE DEDENT |
Shift the elements by one position from odd to pos | while ( in1 != pos ) : NEW_LINE INDENT arr [ in1 ] = arr [ in1 + 1 ] NEW_LINE in1 += 1 NEW_LINE DEDENT arr [ in1 ] = temp NEW_LINE odd = odd - 2 NEW_LINE pos = pos - 1 NEW_LINE |
Reverse the element from 0 to ( n - 1 ) / 2 | start = 0 NEW_LINE end = int ( ( n - 1 ) / 2 ) NEW_LINE while ( start < end ) : NEW_LINE INDENT temp = arr [ start ] NEW_LINE arr [ start ] = arr [ end ] NEW_LINE arr [ end ] = temp NEW_LINE start += 1 NEW_LINE end -= 1 NEW_LINE DEDENT |
Printing the pendulum arrangement | for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " β " ) NEW_LINE DEDENT |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 11 , 2 , 4 , 55 , 6 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE pendulumArrangement ( arr , n ) NEW_LINE DEDENT |
A binary tree node | class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.