codeparrot/xlcost-text-to-code huggingface dataset:
Longest subset of nested elements from a given array | Function to find length of longest subset such that subset { arr [ i ] , arr [ arr [ i ] ] , . . } ; Stores length of the longest subset that satisfy the condition ; Traverse in the array , arr [ ] ; If arr [ i ] equals to i ; Update res ; Count of elements in a subset ; Stores index of elements in the current subset ; Calculate length of a subset that satisfy the condition ; Make visited the current index ; Update curr_index ; Update count ; Update res ; Driver Code
def arrayNesting ( arr ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if arr [ i ] == i : NEW_LINE INDENT res = max ( res , 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT count = 0 NEW_LINE curr_index = i NEW_LINE while arr [ curr_index ] != curr_index : NEW_LINE INDENT next_index = arr [ curr_index ] NEW_LINE arr [ curr_index ] = curr_index NEW_LINE curr_index = next_index NEW_LINE count += 1 NEW_LINE DEDENT DEDENT res = max ( res , count ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 4 , 0 , 3 , 1 , 6 , 2 ] NEW_LINE res = arrayNesting ( arr ) NEW_LINE print ( res ) NEW_LINE DEDENT
Count numbers from a given range having exactly 5 distinct factors | Python3 implementation of the above approach ; Stores all prime numbers up to 2 * 10 ^ 5 ; Function to generate all prime numbers up to 2 * 10 ^ 5 using Sieve of Eratosthenes ; Mark 0 and 1 as non - prime ; If i is prime ; Mark all its factors as non - prime ; If current number is prime ; Store the prime ; Function to count numbers in the range [ L , R ] having exactly 5 factors ; Stores the required count ; Driver Code
N = 2 * 100000 NEW_LINE prime = [ 0 ] * N NEW_LINE def Sieve ( ) : NEW_LINE INDENT p = [ True ] * ( N + 1 ) NEW_LINE p [ 0 ] = p [ 1 ] = False NEW_LINE i = 2 NEW_LINE while ( i * i <= N ) : NEW_LINE INDENT if ( p [ i ] == True ) : NEW_LINE INDENT for j in range ( i * i , N , i ) : NEW_LINE INDENT p [ j ] = False NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( p [ i ] != False ) : NEW_LINE INDENT prime . append ( pow ( i , 4 ) ) NEW_LINE DEDENT DEDENT DEDENT def countNumbers ( L , R ) : NEW_LINE INDENT Count = 0 NEW_LINE for p in prime : NEW_LINE INDENT if ( p >= L and p <= R ) : NEW_LINE INDENT Count += 1 NEW_LINE DEDENT DEDENT print ( Count ) NEW_LINE DEDENT L = 16 NEW_LINE R = 85000 NEW_LINE Sieve ( ) NEW_LINE countNumbers ( L , R ) NEW_LINE
Minimize remaining array sizes by removing equal pairs of first array elements | Function to count the remaining elements in the arrays ; Stores the count of 1 s in L1 ; Store the count of 0 s in L2 ; Traverse the array L1 ; If condition is True ; Increment one by 1 ; Increment zero by 1 ; Stores the index after which no further removals are possible ; Traverse the array L2 ; If condition is True ; Decrement one by 1 ; If one < 0 , then break out of the loop ; Decrement zero by 1 ; If zero < 0 , then break out of loop ; Print the answer ; Driver Code ; Function Call
def countRemainingElements ( L1 , L2 , n ) : NEW_LINE INDENT one = 0 ; NEW_LINE zero = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( L1 [ i ] == 1 ) : NEW_LINE INDENT one += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT zero += 1 ; NEW_LINE DEDENT DEDENT ans = n ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( L2 [ i ] == 1 ) : NEW_LINE INDENT one -= 1 ; NEW_LINE if ( one < 0 ) : NEW_LINE INDENT ans = i ; NEW_LINE break ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT zero -= 1 ; NEW_LINE if ( zero < 0 ) : NEW_LINE INDENT ans = i ; NEW_LINE break ; NEW_LINE DEDENT DEDENT DEDENT print ( n - ans ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L1 = [ 1 , 1 , 0 , 0 ] ; NEW_LINE L2 = [ 0 , 0 , 0 , 1 ] ; NEW_LINE N = len ( L1 ) ; NEW_LINE countRemainingElements ( L1 , L2 , N ) ; NEW_LINE DEDENT
Count pairs of similar rectangles possible from a given array | Function to calculate the count of similar rectangles ; Driver Code ; Input
def getCount ( rows , columns , A ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( rows ) : NEW_LINE INDENT for j in range ( i + 1 , rows , 1 ) : NEW_LINE INDENT if ( A [ i ] [ 0 ] * A [ j ] [ 1 ] == A [ i ] [ 1 ] * A [ j ] [ 0 ] ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ [ 4 , 8 ] , [ 10 , 20 ] , [ 15 , 30 ] , [ 3 , 6 ] ] NEW_LINE columns = 2 NEW_LINE rows = len ( A ) NEW_LINE print ( getCount ( rows , columns , A ) ) NEW_LINE DEDENT
Find the index with minimum score from a given array | Function to find the index with minimum score ; Stores the score of current index ; Traverse the array in reverse ; Update minimum score ; Print the index with minimum score ; Driver Code
def Min_Score_Index ( N , A ) : NEW_LINE INDENT Score = [ 0 ] * N NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if A [ i ] + i < N : NEW_LINE INDENT Score [ i ] = A [ i ] * Score [ A [ i ] + i ] NEW_LINE DEDENT else : NEW_LINE INDENT Score [ i ] = A [ i ] NEW_LINE DEDENT DEDENT min_value = min ( Score ) NEW_LINE ind = Score . index ( min_value ) NEW_LINE print ( ind ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE A = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE Min_Score_Index ( N , A ) NEW_LINE DEDENT
Minimize length of a string by removing occurrences of another string from it as a substring | Function to minimize length of string S after removing all occurrences of string T as substring ; Count of characters required to be removed ; Iterate over the string ; Insert the current character to temp ; Check if the last M characters forms t or not ; Getting the last M characters . If they are equal to t then remove the last M characters from the temp string ; Incrementing subtract by M ; Removing last M characters from the string ; Prthe final answer ; Driver Code ; Given string S & T ; Length of string S ; Length of string T ; Prints the count of operations required
def minLength ( S , T , N , M ) : NEW_LINE INDENT temp = " " ; NEW_LINE subtract = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT temp += S [ i ] ; NEW_LINE if ( len ( temp ) >= M ) : NEW_LINE INDENT if ( T == ( temp [ len ( temp ) - M : len ( temp ) ] ) ) : NEW_LINE INDENT subtract += M ; NEW_LINE cnt = 0 ; NEW_LINE while ( cnt != M ) : NEW_LINE INDENT temp = temp [ 0 : len ( temp ) - 1 ] ; NEW_LINE cnt += 1 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT print ( ( N - subtract ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " aabcbcbd " ; NEW_LINE T = " abc " ; NEW_LINE N = len ( S ) ; NEW_LINE M = len ( T ) ; NEW_LINE minLength ( S , T , N , M ) ; NEW_LINE DEDENT
Minimize swaps between two arrays such that sum of the first array exceeds sum of the second array | Function to find the minimum count of swaps required between the two arrays to make the sum of arr1 [ ] greater than that of arr2 [ ] ; Stores the sum of the two arrays ; Calculate sum of arr1 [ ] ; Calculate sum of arr2 [ ] ; Sort the arrays arr1 [ ] and arr2 [ ] ; Traverse the array arr [ ] ; If the sum1 is less than or equal to sum2 ; Swapping the elements ; Update the sum1 and sum2 ; Increment the count ; Return the final count ; Driver Code ; Function Call
def maximumCount ( arr1 , arr2 , s1 , s2 ) : NEW_LINE INDENT sum1 = 0 NEW_LINE sum2 = 0 NEW_LINE for i in range ( s1 ) : NEW_LINE INDENT sum1 += arr1 [ i ] NEW_LINE DEDENT for j in range ( s2 ) : NEW_LINE INDENT sum2 += arr2 [ j ] NEW_LINE DEDENT len = 0 NEW_LINE if ( s1 >= s2 ) : NEW_LINE INDENT lenn = s2 NEW_LINE DEDENT else : NEW_LINE INDENT lenn = s1 NEW_LINE DEDENT arr1 . sort ( ) ; NEW_LINE arr2 . sort ( ) ; NEW_LINE j = 0 NEW_LINE k = s2 - 1 NEW_LINE count = 0 NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT if ( sum1 <= sum2 ) : NEW_LINE INDENT if ( arr2 [ k ] >= arr1 [ i ] ) : NEW_LINE INDENT dif1 = arr1 [ j ] NEW_LINE dif2 = arr2 [ k ] NEW_LINE sum1 -= dif1 NEW_LINE sum1 += dif2 NEW_LINE sum2 -= dif2 NEW_LINE sum2 += dif1 NEW_LINE j += 1 NEW_LINE k -= 1 NEW_LINE count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr1 = [ 1 , 3 , 2 , 4 ] NEW_LINE arr2 = [ 6 , 7 , 8 ] NEW_LINE N = len ( arr1 ) NEW_LINE M = len ( arr2 ) NEW_LINE print ( maximumCount ( arr1 , arr2 , N , M ) ) NEW_LINE
Count array elements whose all distinct digits appear in K | Function to check a digit occurs in the digit of K or not ; Iterate over all possible digits of K ; If current digit equal to digit ; Update K ; Function to find the count of array elements whose distinct digits are a subset of digits of K ; Stores count of array elements whose distinct digits are subset of digits of K ; Traverse the array , [ ] arr ; Stores the current element ; Check if all the digits arr [ i ] is a subset of the digits of K or not ; Iterate over all possible digits of arr [ i ] ; Stores current digit ; If current digit does not appear in K ; Update flag ; Update no ; If all the digits arr [ i ] appear in K ; Update count ; Finally print count ; Driver Code
def isValidDigit ( digit , K ) : NEW_LINE INDENT while ( K != 0 ) : NEW_LINE INDENT if ( K % 10 == digit ) : NEW_LINE INDENT return True NEW_LINE DEDENT K = K // 10 NEW_LINE DEDENT return False NEW_LINE DEDENT def noOfValidNumbers ( K , arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT no = arr [ i ] NEW_LINE flag = True NEW_LINE while ( no != 0 ) : NEW_LINE INDENT digit = no % 10 NEW_LINE if ( not isValidDigit ( digit , K ) ) : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT no = no // 10 NEW_LINE DEDENT if ( flag == True ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT K = 12 NEW_LINE arr = [ 1 , 12 , 1222 , 13 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( noOfValidNumbers ( K , arr , n ) ) NEW_LINE DEDENT
Queries to find the minimum index in given array having at least value X | Python3 program to implement the above approachf ; Function to find the smallest index of an array element whose value is less than or equal to Q [ i ] ; Stores size of array ; Stores coun of queries ; Store array elements along with the index ; Store smallest index of an array element whose value is greater than or equal to i ; Traverse the array ; Insert { arr [ i ] , i } into storeArrIdx [ ] ; Sort the array ; Sort the storeArrIdx ; Stores index of arr [ N - 1 ] in sorted order ; Traverse the array storeArrIdx [ ] ; Update minIdx [ i ] ; Traverse the array Q [ ] ; Store the index of lower_bound of Q [ i ] ; If no index found whose value greater than or equal to arr [ i ] ; Prsmallest index whose value greater than or equal to Q [ i ] ; Driver Code
from bisect import bisect_left NEW_LINE def minimumIndex ( arr , Q ) : NEW_LINE INDENT N = len ( arr ) NEW_LINE M = len ( Q ) NEW_LINE storeArrIdx = [ ] NEW_LINE minIdx = [ 0 ] * ( N ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT storeArrIdx . append ( [ arr [ i ] , i ] ) NEW_LINE DEDENT arr = sorted ( arr ) NEW_LINE storeArrIdx = sorted ( storeArrIdx ) NEW_LINE minIdx [ N - 1 ] = storeArrIdx [ N - 1 ] [ 1 ] NEW_LINE for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT minIdx [ i ] = min ( minIdx [ i + 1 ] , storeArrIdx [ i ] [ 1 ] ) NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT pos = bisect_left ( arr , Q [ i ] ) NEW_LINE if ( pos == N ) : NEW_LINE INDENT print ( - 1 , end = " ▁ " ) NEW_LINE continue NEW_LINE DEDENT print ( minIdx [ pos ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 9 ] NEW_LINE Q = [ 7 , 10 , 0 ] NEW_LINE minimumIndex ( arr , Q ) NEW_LINE DEDENT
Find a K | Utility function to check if subarray of size K exits whose XOR of elements equal to XOR ofremaning array elements ; Find XOR of whole array ; Find XOR of first K elements ; Adding XOR of next element ; Removing XOR of previous element ; Check if XOR of current subarray matches with the XOR of remaining elements or not ; Function to check if subarray of size K exits whose XOR of elements equal to XOR ofremaning array elements ; Driver Code ; Given array ; Size of the array ; Given K ; Function Call
def isSubarrayExistUtil ( arr , K , N ) : NEW_LINE INDENT totalXOR = 0 NEW_LINE SubarrayXOR = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT totalXOR ^= arr [ i ] NEW_LINE DEDENT for i in range ( K ) : NEW_LINE INDENT SubarrayXOR ^= arr [ i ] NEW_LINE DEDENT if ( SubarrayXOR == ( totalXOR ^ SubarrayXOR ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( K , N ) : NEW_LINE INDENT SubarrayXOR ^= arr [ i ] NEW_LINE SubarrayXOR ^= arr [ i - 1 ] NEW_LINE if ( SubarrayXOR == ( totalXOR ^ SubarrayXOR ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def isSubarrayExist ( arr , K , N ) : NEW_LINE INDENT if ( isSubarrayExistUtil ( arr , K , N ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 3 , 5 , 7 , 7 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE K = 5 NEW_LINE isSubarrayExist ( arr , K , N ) NEW_LINE DEDENT
Count pairs from a given range having even sum | Function to find the count of ordered pairs having even sum ; Stores count of even numbers in the range [ L , R ] ; If L is even ; Update count_even ; Update count_odd ; Stores count of odd numbers in the range [ L , R ] ; Update count_odd ; Update count_odd ; Stores count of pairs whose sum is even and both elements of the pairs are also even ; Stores count of pairs whose sum is even and both elements of the pairs are odd ; Print total ordered pairs whose sum is even ; Driver Code ; Given L & R ; Function Call
def countPairs ( L , R ) : NEW_LINE INDENT count_even = 0 NEW_LINE if ( L % 2 == 0 ) : NEW_LINE INDENT count_even = ( ( R // 2 ) - ( L // 2 ) + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT count_even = ( ( R // 2 ) - ( L // 2 ) ) NEW_LINE DEDENT count_odd = 0 NEW_LINE if ( L % 2 == 0 ) : NEW_LINE INDENT count_odd = ( ( ( R + 1 ) // 2 ) - ( ( L + 1 ) // 2 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT count_odd = ( ( ( R + 1 ) // 2 ) - ( ( L + 1 ) // 2 ) + 1 ) NEW_LINE DEDENT count_even *= count_even NEW_LINE count_odd *= count_odd NEW_LINE print ( count_even + count_odd ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L , R = 1 , 3 NEW_LINE countPairs ( L , R ) NEW_LINE DEDENT
Count of array elements that can be found using Randomized Binary Search on every array element | Python3 program for the above approach ; Function to find minimum count of array elements found by repeatedly applying Randomized Binary Search ; Stores count of array elements ; smallestRight [ i ] : Stores the smallest array element on the right side of i ; Update smallestRight [ 0 ] ; Traverse the array from right to left ; Update smallestRight [ i ] ; Stores the largest element upto i - th index ; Stores the minimum count of elements found by repeatedly applying Randomized Binary Search ; If largest element on left side is less than smallest element on right side ; Update ans ; Update mn ; Given array ; Function Call
import sys NEW_LINE def getDefiniteFinds ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE smallestRight = [ 0 ] * ( n + 1 ) NEW_LINE smallestRight [ n ] = sys . maxsize NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT smallestRight [ i ] = min ( smallestRight [ i + 1 ] , arr [ i ] ) NEW_LINE DEDENT mn = - sys . maxsize - 1 NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( mn < arr [ i ] and arr [ i ] < smallestRight [ i + 1 ] ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT mn = max ( arr [ i ] , mn ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 5 , 4 , 9 ] NEW_LINE print ( getDefiniteFinds ( arr ) ) NEW_LINE
Count numbers from a given range having same first and last digits in their binary representation | Function to count numbers in range [ L , R ] having same first and last digit in the binary representation ; Given range [ L , R ] ; Function Call
def Count_numbers ( L , R ) : NEW_LINE INDENT count = ( R - L ) // 2 NEW_LINE if ( R % 2 != 0 or L % 2 != 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT L = 6 NEW_LINE R = 30 NEW_LINE Count_numbers ( L , R ) NEW_LINE
Find the index having sum of elements on its left equal to reverse of the sum of elements on its right | Function to check if a number is equal to the reverse of digits of other number ; Stores reverse of digits of rightSum ; Stores rightSum ; Calculate reverse of digits of temp ; Update rev ; Update temp ; If reverse of digits of rightSum equal to leftSum ; Function to find the index that satisfies the condition ; Stores sum of array elements on right side of each index ; Stores sum of array elements on left side of each index ; Traverse the array ; Update rightSum ; Traverse the array ; Update rightSum ; If leftSum equal to reverse of digits of rightSum ; Update leftSum ; Driver Code
def checkReverse ( leftSum , rightSum ) : NEW_LINE INDENT rev = 0 NEW_LINE temp = rightSum NEW_LINE while ( temp != 0 ) : NEW_LINE INDENT rev = ( rev * 10 ) + ( temp % 10 ) NEW_LINE temp //= 10 NEW_LINE DEDENT if ( rev == leftSum ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def findIndex ( arr , N ) : NEW_LINE INDENT rightSum = 0 NEW_LINE leftSum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT rightSum += arr [ i ] NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT rightSum -= arr [ i ] NEW_LINE if ( checkReverse ( leftSum , rightSum ) ) : NEW_LINE INDENT return i NEW_LINE DEDENT leftSum += arr [ i ] NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 7 , 3 , 6 , 4 , 9 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findIndex ( arr , N ) ) NEW_LINE DEDENT
Print distinct absolute differences of all possible pairs from a given array | Python3 program for the above approach ; Function to find all distinct absolute difference of all possible pairs of the array ; bset [ i ] : Check if i is present in the array or not ; diff [ i ] : Check if there exists a pair whose absolute difference is i ; Traverse the array , arr [ ] ; Iterate over the range [ 0 , Max ] ; If i - th bit is set ; Insert the absolute difference of all possible pairs whose first element is arr [ i ] ; Stores count of set bits ; If there is at least one duplicate element in arr [ ] ; Printing the distinct absolute differences of all possible pairs ; If i - th bit is set ; Driver Code ; Given array ; Given size ; Function Call
Max = 100005 NEW_LINE def printUniqDif ( n , a ) : NEW_LINE INDENT bset = [ 0 for i in range ( 33 ) ] NEW_LINE diff = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT bset [ a [ i ] ] = 1 NEW_LINE DEDENT d = 0 NEW_LINE for i in range ( 1 , 33 ) : NEW_LINE INDENT d = d | ( bset [ i ] << i ) NEW_LINE DEDENT for i in range ( 33 ) : NEW_LINE INDENT if ( bset [ i ] ) : NEW_LINE INDENT diff = diff | d >> i NEW_LINE DEDENT DEDENT X , Y = bset . count ( 1 ) , str ( bin ( diff ) [ 2 : ] ) NEW_LINE if ( X != n ) : NEW_LINE INDENT print ( 0 , end = " ▁ " ) NEW_LINE DEDENT for i in range ( 1 , len ( Y ) ) : NEW_LINE INDENT if ( Y [ i ] == '1' ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 4 , 6 ] NEW_LINE n = len ( a ) NEW_LINE printUniqDif ( n , a ) NEW_LINE DEDENT
Longest Common Subsequence of two arrays out of which one array consists of distinct elements only | Python3 program to to implement the above approach ; Function to find the Longest Common Subsequence between the two arrays ; Maps elements of firstArr [ ] to their respective indices ; Traverse the array firstArr [ ] ; Stores the indices of common elements between firstArr [ ] and secondArr [ ] ; Traverse the array secondArr [ ] ; If current element exists in the Map ; Stores lIS from tempArr [ ] ; Driver Code
from bisect import bisect_left NEW_LINE def LCS ( firstArr , secondArr ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( len ( firstArr ) ) : NEW_LINE INDENT mp [ firstArr [ i ] ] = i + 1 NEW_LINE DEDENT tempArr = [ ] NEW_LINE for i in range ( len ( secondArr ) ) : NEW_LINE INDENT if ( secondArr [ i ] in mp ) : NEW_LINE INDENT tempArr . append ( mp [ secondArr [ i ] ] ) NEW_LINE DEDENT DEDENT tail = [ ] NEW_LINE tail . append ( tempArr [ 0 ] ) NEW_LINE for i in range ( 1 , len ( tempArr ) ) : NEW_LINE INDENT if ( tempArr [ i ] > tail [ - 1 ] ) : NEW_LINE INDENT tail . append ( tempArr [ i ] ) NEW_LINE DEDENT elif ( tempArr [ i ] < tail [ 0 ] ) : NEW_LINE INDENT tail [ 0 ] = tempArr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT it = bisect_left ( tail , tempArr [ i ] ) NEW_LINE it = tempArr [ i ] NEW_LINE DEDENT DEDENT return len ( tail ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT firstArr = [ 3 , 5 , 1 , 8 ] NEW_LINE secondArr = [ 3 , 3 , 5 , 3 , 8 ] NEW_LINE print ( LCS ( firstArr , secondArr ) ) NEW_LINE DEDENT
Count non | Function to find the total count of triplets ( i , j , k ) such that i < j < k and ( j - i ) != ( k - j ) ; Stores indices of 0 s ; Stores indices of 1 s ; Stores indices of 2 s ; Traverse the array ; If current array element is 0 ; If current array element is 1 ; If current array element is 2 ; Total count of triplets ; Traverse the array zero_i [ ] ; Traverse the array one_i [ ] ; Stores index of 0 s ; Stores index of 1 s ; Stores third element of triplets that does not satisfy the condition ; If r present in the map ; Update r ; If r present in the map ; Update r ; If r present in the map and equidistant ; Print the obtained count ; Driver Code
def countTriplets ( arr , N ) : NEW_LINE INDENT zero_i = [ ] NEW_LINE one_i = [ ] NEW_LINE mp = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT zero_i . append ( i + 1 ) NEW_LINE DEDENT elif ( arr [ i ] == 1 ) : NEW_LINE INDENT one_i . append ( i + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT mp [ i + 1 ] = 1 NEW_LINE DEDENT DEDENT total = len ( zero_i ) * len ( one_i ) * len ( mp ) NEW_LINE for i in range ( len ( zero_i ) ) : NEW_LINE INDENT for j in range ( len ( one_i ) ) : NEW_LINE INDENT p = zero_i [ i ] NEW_LINE q = one_i [ j ] NEW_LINE r = 2 * p - q NEW_LINE if ( r in mp ) : NEW_LINE INDENT total -= 1 NEW_LINE DEDENT r = 2 * q - p NEW_LINE if ( r in mp ) : NEW_LINE INDENT total -= 1 NEW_LINE DEDENT r = ( p + q ) // 2 NEW_LINE if ( ( r in mp ) and abs ( r - p ) == abs ( r - q ) ) : NEW_LINE INDENT total -= 1 NEW_LINE DEDENT DEDENT DEDENT print ( total ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 1 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE countTriplets ( arr , N ) NEW_LINE DEDENT
Print prime factors of a given integer in decreasing order using Stack | Function to print prime factors of N in decreasing order ; Stores prime factors of N in decreasing order ; Insert i into stack ; Update N ; Update i ; Print value of stack st ; Driver Code ; function Call
def PrimeFactors ( N ) : NEW_LINE INDENT st = [ ] NEW_LINE i = 2 NEW_LINE while ( N != 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT st . append ( i ) NEW_LINE while ( N % i == 0 ) : NEW_LINE INDENT N = N // i NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT while ( len ( st ) != 0 ) : NEW_LINE INDENT print ( st [ - 1 ] ) NEW_LINE st . pop ( ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 8 NEW_LINE PrimeFactors ( N ) NEW_LINE DEDENT