codeparrot/xlcost-text-to-code huggingface dataset:
Probability of obtaining pairs from two arrays such that element from the first array is smaller than that of the second array | Function to find probability such that x < y and X belongs to arr1 [ ] and Y belongs to arr2 [ ] ; Stores the length of arr1 ; Stores the length of arr2 ; Stores the result ; Traverse the arr1 [ ] ; Stores the count of elements in arr2 that are greater than arr [ i ] ; Traverse the arr2 [ ] ; If arr2 [ j ] is greater than arr1 [ i ] ; Increment res by y ; Update the value of res ; Return resultant probability ; Driver Code
def probability ( arr1 , arr2 ) : NEW_LINE INDENT N = len ( arr1 ) NEW_LINE M = len ( arr2 ) NEW_LINE res = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT y = 0 NEW_LINE for j in range ( M ) : NEW_LINE INDENT if ( arr2 [ j ] > arr1 [ i ] ) : NEW_LINE INDENT y += 1 NEW_LINE DEDENT DEDENT res += y NEW_LINE DEDENT res = res / ( N * M ) NEW_LINE return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr1 = [ 5 , 2 , 6 , 1 ] NEW_LINE arr2 = [ 1 , 6 , 10 , 1 ] NEW_LINE print ( probability ( arr1 , arr2 ) ) NEW_LINE DEDENT
Probability of obtaining pairs from two arrays such that element from the first array is smaller than that of the second array | Function to find probability such that x < y and X belongs to arr1 [ ] & Y belongs to arr2 [ ] ; Stores the length of arr1 ; Stores the length of arr2 ; Stores the result ; Sort the arr2 [ ] in the ascending order ; Traverse the arr1 [ ] ; Stores the count of elements in arr2 that are greater than arr [ i ] ; Increment res by y ; Update the resultant probability ; Return the result ; Function to return the count of elements from the array which are greater than k ; Stores the index of the leftmost element from the array which is at least k ; Finds number of elements greater than k ; If mid element is at least K , then update the value of leftGreater and r ; Update leftGreater ; Update r ; If mid element is at most K , then update the value of l ; Return the count of elements greater than k ; Driver code
def probability ( arr1 , arr2 ) : NEW_LINE INDENT n = len ( arr1 ) NEW_LINE m = len ( arr2 ) NEW_LINE res = 0 NEW_LINE arr2 . sort ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT y = countGreater ( arr2 , arr1 [ i ] ) NEW_LINE res += y NEW_LINE DEDENT res /= ( n * m ) NEW_LINE return res NEW_LINE DEDENT def countGreater ( arr , k ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE l = 0 NEW_LINE r = n - 1 NEW_LINE leftGreater = n NEW_LINE while l <= r : NEW_LINE INDENT m = ( l + r ) // 2 NEW_LINE if ( arr [ m ] > k ) : NEW_LINE INDENT leftGreater = m NEW_LINE r = m - 1 NEW_LINE DEDENT else : NEW_LINE INDENT l = m + 1 NEW_LINE DEDENT DEDENT return n - leftGreater NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 5 , 2 , 6 , 1 ] NEW_LINE arr2 = [ 1 , 6 , 10 , 1 ] NEW_LINE print ( probability ( arr1 , arr2 ) ) NEW_LINE DEDENT
Minimize cost to cover floor using tiles of dimensions 1 * 1 and 1 * 2 | Function to find the minimum cost of flooring with the given tiles ; Store the size of the 2d array ; Stores the minimum cost of flooring ; Traverse the 2d array row - wise ; If the current character is ' * ' , then skip it ; Choose the 1 * 1 tile if j is m - 1 ; If consecutive ' . ' are present , the greedily choose tile with the minimum cost ; Otherwise choose the 1 * 1 tile ; Print the minimum cost ; Driver Code
def minCost ( arr , A , B ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE m = len ( arr [ 0 ] ) NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT j = 0 NEW_LINE while j < m : NEW_LINE INDENT if ( arr [ i ] [ j ] == ' * ' ) : NEW_LINE INDENT j += 1 NEW_LINE continue NEW_LINE DEDENT if ( j == m - 1 ) : NEW_LINE INDENT ans += A NEW_LINE DEDENT else : NEW_LINE INDENT if ( arr [ i ] [ j + 1 ] == ' . ' ) : NEW_LINE INDENT ans += min ( 2 * A , B ) NEW_LINE j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans += A NEW_LINE DEDENT DEDENT j += 1 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ ' . ' , ' . ' , ' * ' ] , [ ' . ' , ' * ' , ' * ' ] ] NEW_LINE A , B = 2 , 10 NEW_LINE minCost ( arr , A , B ) NEW_LINE DEDENT
Count inversions in a permutation of first N natural numbers | Python3 program for the above approach ; Function to count number of inversions in a permutation of first N natural numbers ; Store array elements in sorted order ; Store the count of inversions ; Traverse the array ; Store the index of first occurrence of arr [ i ] in vector V ; Add count of smaller elements than current element ; Erase current element from vector and go to next index ; Print the result ; Driver Code ; Given Input ; Function Call
from bisect import bisect_left NEW_LINE def countInversions ( arr , n ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( 1 , n + 1 , 1 ) : NEW_LINE INDENT v . append ( i ) NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT itr = bisect_left ( v , arr [ i ] ) NEW_LINE ans += itr NEW_LINE v = v [ : itr ] + v [ itr + 1 : ] NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 1 , 5 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE countInversions ( arr , n ) NEW_LINE DEDENT
Find all possible pairs with given Bitwise OR and Bitwise XOR values | Function to find pairs with XOR equal to A and OR equal to B ; Iterate from 1 to B ; Check if ( i OR y ) is B ; Driver Code
def findPairs ( A , B ) : NEW_LINE INDENT for i in range ( 1 , B + 1 ) : NEW_LINE INDENT y = A ^ i NEW_LINE if ( y > 0 and ( i y ) == B ) : NEW_LINE INDENT print ( i , " ▁ " , y ) NEW_LINE DEDENT DEDENT DEDENT A = 8 NEW_LINE B = 10 NEW_LINE findPairs ( A , B ) NEW_LINE
Count distinct elements from a range of a sorted sequence from a given frequency array | Function to find the first index with value is at least element ; Update the value of left ; Binary search for the element ; Find the middle element ; Check if the value lies between the elements at index mid - 1 and mid ; Check in the right subarray ; Update the value of left ; Check in left subarray ; Update the value of right ; Function to count the number of distinct elements over the range [ L , R ] in the sorted sequence ; Stores the count of distinct elements ; Create the prefix sum array ; Update the value of count ; Update the value of pref [ i ] ; Calculating the first index of L and R using binary search ; Print the resultant count ; Driver Code
def binarysearch ( array , right , element ) : NEW_LINE INDENT left = 1 NEW_LINE while ( left <= right ) : NEW_LINE INDENT mid = ( left + right // 2 ) NEW_LINE if ( array [ mid ] == element ) : NEW_LINE INDENT return mid NEW_LINE DEDENT if ( mid - 1 > 0 and array [ mid ] > element and array [ mid - 1 ] < element ) : NEW_LINE INDENT return mid NEW_LINE DEDENT elif ( array [ mid ] < element ) : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT right = mid - 1 NEW_LINE DEDENT DEDENT return 1 NEW_LINE DEDENT def countDistinct ( arr , L , R ) : NEW_LINE INDENT count = 0 NEW_LINE pref = [ 0 ] * ( len ( arr ) + 1 ) NEW_LINE for i in range ( 1 , len ( arr ) + 1 ) : NEW_LINE INDENT count += arr [ i - 1 ] NEW_LINE pref [ i ] = count NEW_LINE DEDENT left = binarysearch ( pref , len ( arr ) + 1 , L ) NEW_LINE right = binarysearch ( pref , len ( arr ) + 1 , R ) NEW_LINE print ( right - left + 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 6 , 7 , 1 , 8 ] NEW_LINE L = 3 NEW_LINE R = 7 NEW_LINE countDistinct ( arr , L , R ) NEW_LINE DEDENT
Find Unique ID and Domain Name of a Website from a string | Function to check if a character is alphabet or not ; Function to check if a character is a numeric or not ; Function to find ID and Domain name from a given ; Stores ID and the domain names ; Stores the words of S ; Stores the temporary word ; Traverse the S ; If the current character is space ; Push the curr in words ; Update the curr ; Otherwise ; If curr is not empty ; If length of ss is 10 ; Traverse the ss ; If j is in the range [ 5 , 9 ) ; If current character is not numeric ; Mark flag 1 ; Otherwise ; If current character is not alphabet ; Mark flag 1 ; If flag is false ; Assign ss to ID ; If sub formed by the first 3 character is " www " and last 3 character is " moc " ; Update the domain name ; Print ID and Domain ; Driver Code
def ischar ( x ) : NEW_LINE INDENT if ( ( x >= ' A ' and x <= ' Z ' ) or ( x >= ' a ' and x <= ' z ' ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT def isnum ( x ) : NEW_LINE INDENT if ( x >= '0' and x <= '9' ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT def findIdandDomain ( S , N ) : NEW_LINE INDENT ID , Domain = " " , " " NEW_LINE words = [ ] NEW_LINE curr = " " NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] == ' ▁ ' ) : NEW_LINE INDENT words . append ( curr ) NEW_LINE curr = " " NEW_LINE DEDENT else : NEW_LINE INDENT if ( S [ i ] == ' . ' ) : NEW_LINE INDENT if ( i + 1 == N or ( i + 1 < N and S [ i + 1 ] == ' ▁ ' ) ) : NEW_LINE INDENT continue NEW_LINE DEDENT DEDENT curr += S [ i ] NEW_LINE DEDENT DEDENT if ( len ( curr ) ) : NEW_LINE INDENT words . append ( curr ) NEW_LINE DEDENT for ss in words : NEW_LINE INDENT if ( len ( ss ) == 10 ) : NEW_LINE INDENT flag = 0 NEW_LINE for j in range ( 10 ) : NEW_LINE INDENT if ( j >= 5 and j < 9 ) : NEW_LINE INDENT if ( isnum ( ss [ j ] ) == 0 ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( ischar ( ss [ j ] ) == 0 ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT DEDENT DEDENT if ( not flag ) : NEW_LINE INDENT ID = ss NEW_LINE DEDENT DEDENT if ( ss [ 0 : 3 ] == " www " and ss [ len ( ss ) - 3 : ] == " com " ) : NEW_LINE INDENT Domain = ss [ 4 : len ( ss ) ] NEW_LINE DEDENT DEDENT print ( " ID ▁ = " , ID ) NEW_LINE print ( " Domain ▁ = " , Domain ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " We ▁ thank ▁ ABCDE1234F ▁ for ▁ visiting ▁ us ▁ " " and ▁ buying ▁ products ▁ item ▁ AMZrr @ ! k . ▁ " N = len ( S ) NEW_LINE findIdandDomain ( S , N ) NEW_LINE DEDENT
Capacity To Ship Packages Within D Days | Function to check if the weights can be delivered in D days or not ; Stores the count of days required to ship all the weights if the maximum capacity is mx ; Traverse all the weights ; If total weight is more than the maximum capacity ; If days are more than D , then return false ; Return true for the days < D ; Function to find the least weight capacity of a boat to ship all the weights within D days ; Stores the total weights to be shipped ; Find the sum of weights ; Stores the maximum weight in the array that has to be shipped ; Store the ending value for the search space ; Store the required result ; Perform binary search ; Store the middle value ; If mid can be shipped , then update the result and end value of the search space ; Search for minimum value in the right part ; Print the result ; Driver Code
def isValid ( weight , n , D , mx ) : NEW_LINE INDENT st = 1 NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += weight [ i ] NEW_LINE if ( sum > mx ) : NEW_LINE INDENT st += 1 NEW_LINE sum = weight [ i ] NEW_LINE DEDENT if ( st > D ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def shipWithinDays ( weight , D , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += weight [ i ] NEW_LINE DEDENT s = weight [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT s = max ( s , weight [ i ] ) NEW_LINE DEDENT e = sum NEW_LINE res = - 1 NEW_LINE while ( s <= e ) : NEW_LINE INDENT mid = s + ( e - s ) // 2 NEW_LINE if ( isValid ( weight , n , D , mid ) ) : NEW_LINE INDENT res = mid NEW_LINE e = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT s = mid + 1 NEW_LINE DEDENT DEDENT print ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT weight = [ 9 , 8 , 10 ] NEW_LINE D = 3 NEW_LINE N = len ( weight ) NEW_LINE shipWithinDays ( weight , D , N ) NEW_LINE DEDENT
Find a point whose sum of distances from all given points on a line is K | Function to find the sum of distances of all points from a given point ; Stores sum of distances ; Traverse the array ; Return the sum ; Function to find such a point having sum of distances of all other points from this point equal to K ; If N is odd keep left as arr [ n / 2 ] else keep left as arr [ n / 2 - 1 ] + 1 ; ; Keep right as arr [ N - 1 ] ; Perform binary search in the right half ; Calculate the mid index of the range ; If temp is equal to K ; Print the value of mid ; If the value of K < temp ; Update right to mid - 1 ; If the value of K > temp ; Update left to mid + 1 ; Update the value of left ; Update the value of right ; Perform binary search on the left half ; Calculate the mid index of the range ; If temp is equal to K ; Print mid ; if K > temp ; Update right to mid - 1 ; If K < temp ; Update left to mid + 1 ; If no such point found ; Driver Code
def findSum ( arr , N , pt ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += abs ( arr [ i ] - pt ) NEW_LINE DEDENT return sum NEW_LINE DEDENT def findPoint ( arr , N , K ) : NEW_LINE INDENT left = 0 NEW_LINE if ( N % 2 ) : NEW_LINE INDENT left = arr [ N // 2 ] NEW_LINE DEDENT else : NEW_LINE INDENT left = arr [ N // 2 - 1 ] + 1 NEW_LINE DEDENT right = arr [ N - 1 ] NEW_LINE while ( left <= right ) : NEW_LINE INDENT mid = ( left + right ) // 2 NEW_LINE temp = findSum ( arr , N , mid ) NEW_LINE if ( temp == K ) : NEW_LINE INDENT print ( mid ) NEW_LINE return NEW_LINE DEDENT elif ( K < temp ) : NEW_LINE INDENT right = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT DEDENT left = arr [ 0 ] NEW_LINE right = arr [ N // 2 ] - 1 NEW_LINE while ( left <= right ) : NEW_LINE INDENT mid = ( left + right ) // 2 NEW_LINE temp = findSum ( arr , N , mid ) NEW_LINE if ( temp == K ) : NEW_LINE INDENT print ( mid ) NEW_LINE return NEW_LINE DEDENT elif ( K > temp ) : NEW_LINE INDENT right = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT DEDENT print ( " - 1" ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 6 , 7 , 11 ] NEW_LINE K = 18 NEW_LINE N = len ( arr ) NEW_LINE findPoint ( arr , N , K ) NEW_LINE DEDENT
Generate an array consisting of most frequent greater elements present on the right side of each array element | Function to generate an array containing the most frequent greater element on the right side of each array element ; Stores the generated array ; Traverse the array arr [ ] ; Store the result for the current index and its frequency ; Iterate over the right subarray ; Store the frequency of the current array element ; If the frequencies are equal ; Update ans to smaller of the two elements ; If count of new element is more than count of ans ; Insert answer in the array ; Print the resultant array ; Driver Code ; Given Input
def findArray ( arr , n ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans = - 1 NEW_LINE old_c = 0 NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ j ] > arr [ i ] ) : NEW_LINE INDENT curr_c = arr [ j : n + 1 ] . count ( arr [ j ] ) NEW_LINE if ( curr_c == old_c ) : NEW_LINE INDENT if ( arr [ j ] < ans ) : NEW_LINE INDENT ans = arr [ j ] NEW_LINE DEDENT DEDENT if ( curr_c > old_c ) : NEW_LINE INDENT ans = arr [ j ] NEW_LINE old_c = curr_c NEW_LINE DEDENT DEDENT DEDENT v . append ( ans ) NEW_LINE DEDENT for i in range ( len ( v ) ) : NEW_LINE INDENT print ( v [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 5 , 2 , 25 , 10 , 5 , 10 , 3 , 10 , 5 ] NEW_LINE size = len ( arr ) NEW_LINE findArray ( arr , size ) NEW_LINE DEDENT
Smallest index that splits an array into two subarrays with equal product | Function to find the smallest index that splits the array into two subarrays with equal product ; Stores the product of the array ; Traverse the given array ; Stores the product of left and the right subarrays ; Traverse the given array ; Update the products ; Check if product is equal ; Print resultant index ; If no partition exists , then print - 1. ; Driver Code
def prodEquilibrium ( arr , N ) : NEW_LINE INDENT product = 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT product *= arr [ i ] NEW_LINE DEDENT left = 1 NEW_LINE right = product NEW_LINE for i in range ( N ) : NEW_LINE INDENT left = left * arr [ i ] NEW_LINE right = right // arr [ i ] NEW_LINE if ( left == right ) : NEW_LINE INDENT print ( i + 1 ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( - 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 3 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE prodEquilibrium ( arr , N ) NEW_LINE DEDENT
Count triplets from a sorted array having difference between adjacent elements equal to D | Function to count the number of triplets having difference between adjacent elements equal to D ; Stores the frequency of array elements ; Stores the count of resultant triplets ; Traverse the array ; Check if arr [ i ] - D and arr [ i ] - 2 * D exists in the Hashmap or not ; Update the value of ans ; Increase the frequency of the current element ; Return the resultant count ; Driver Code
def countTriplets ( D , arr ) : NEW_LINE INDENT freq = { } NEW_LINE ans = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( ( ( arr [ i ] - D ) in freq ) and ( arr [ i ] - 2 * D ) in freq ) : NEW_LINE INDENT ans += ( freq [ arr [ i ] - D ] * freq [ arr [ i ] - 2 * D ] ) NEW_LINE DEDENT freq [ arr [ i ] ] = freq . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 5 , 7 , 8 , 10 ] NEW_LINE D = 1 NEW_LINE print ( countTriplets ( D , arr ) ) NEW_LINE DEDENT
Find the array element having equal sum of Prime Numbers on its left and right | Python3 program for the above approach ; Function to find an index in the array having sum of prime numbers to its left and right equal ; Stores the maximum value present in the array ; Stores all positive elements which are <= max_value ; If 1 is present ; Remove 1 ; Sieve of Eratosthenes to store all prime numbers which are <= max_value in the Map ; Erase non - prime numbers ; Stores the sum of prime numbers from left ; Stores the sum of prime numbers to the left of each index ; Stores the sum of prime numbers to the left of the current index ; Add current value to the prime sum if the current value is prime ; Stores the sum of prime numbers from right ; Stores the sum of prime numbers to the right of each index ; Stores the sum of prime numbers to the right of the current index ; Add current value to the prime sum if the current value is prime ; Traverse through the two arrays to find the index ; Compare the values present at the current index ; Return the index where both the values are same ; No index is found . ; Driver Code ; Given array arr [ ] ; Size of Array ; Function Call
from math import sqrt NEW_LINE def find_index ( arr , N ) : NEW_LINE INDENT max_value = - 10 ** 9 NEW_LINE for i in range ( N ) : NEW_LINE INDENT max_value = max ( max_value , arr [ i ] ) NEW_LINE DEDENT store = { } NEW_LINE for i in range ( 1 , max_value + 1 ) : NEW_LINE INDENT store [ i ] = store . get ( i , 0 ) + 1 NEW_LINE DEDENT if ( 1 in store ) : NEW_LINE INDENT del store [ 1 ] NEW_LINE DEDENT for i in range ( 2 , int ( sqrt ( max_value ) ) + 1 ) : NEW_LINE INDENT multiple = 2 NEW_LINE while ( ( i * multiple ) <= max_value ) : NEW_LINE INDENT if ( i * multiple in store ) : NEW_LINE INDENT del store [ i * multiple ] NEW_LINE DEDENT multiple += 1 NEW_LINE DEDENT DEDENT prime_sum_from_left = 0 NEW_LINE first_array = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT first_array [ i ] = prime_sum_from_left NEW_LINE if arr [ i ] in store : NEW_LINE INDENT prime_sum_from_left += arr [ i ] NEW_LINE DEDENT DEDENT prime_sum_from_right = 0 NEW_LINE second_array = [ 0 ] * N NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT second_array [ i ] = prime_sum_from_right NEW_LINE if ( arr [ i ] in store ) : NEW_LINE INDENT prime_sum_from_right += arr [ i ] NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT if ( first_array [ i ] == second_array [ i ] ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 11 , 4 , 7 , 6 , 13 , 1 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE print ( find_index ( arr , N ) ) NEW_LINE DEDENT
Convert an array to reduced form | Set 3 ( Binary Search ) | Function to find the reduced form of the given array arr [ ] ; Stores the sorted form of the the given array arr [ ] ; Sort the array brr [ ] ; Traverse the given array arr [ ] ; Perform the Binary Search ; Calculate the value of mid ; Prthe current index and break ; Update the value of l ; Update the value of r ; Driver Code
def convert ( arr , n ) : NEW_LINE INDENT brr = [ i for i in arr ] NEW_LINE brr = sorted ( brr ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT l , r , mid = 0 , n - 1 , 0 NEW_LINE while ( l <= r ) : NEW_LINE INDENT mid = ( l + r ) // 2 NEW_LINE if ( brr [ mid ] == arr [ i ] ) : NEW_LINE INDENT print ( mid , end = " ▁ " ) NEW_LINE break NEW_LINE DEDENT elif ( brr [ mid ] < arr [ i ] ) : NEW_LINE INDENT l = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT r = mid - 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 20 , 15 , 12 , 11 , 50 ] NEW_LINE N = len ( arr ) NEW_LINE convert ( arr , N ) NEW_LINE DEDENT
Find the array element having equal count of Prime Numbers on its left and right | Python 3 program for the above approach ; Function to find the index of the array such that the count of prime numbers to its either ends are same ; Store the maximum value in the array ; Traverse the array arr [ ] ; / Stores all the numbers ; Iterate over the range [ 1 , Max ] ; Increment the value of st [ i ] ; Removes 1 from the map St ; Perform Sieve of Prime Numbers ; While i * j is less than the maxValue ; If i * j is in map St ; Erase the value ( i * j ) ; Increment the value of j ; Stores the count of prime from index 0 to i ; Stores the count of prime numbers ; Traverse the array arr [ ] ; If arr [ i ] is present in the map st ; Stores the count of prime from index i to N - 1 ; Stores the count of prime numbers ; Iterate over the range [ 0 , N - 1 ] in reverse order ; If arr [ i ] is in map st ; Iterate over the range [ 0 , N - 1 ] ; If prefix [ i ] is equal to the Suffix [ i ] ; Return - 1 if no such index is present ; Driver Code
from collections import defaultdict NEW_LINE import sys NEW_LINE import math NEW_LINE def findIndex ( arr , N ) : NEW_LINE INDENT maxValue = - sys . maxsize - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT maxValue = max ( maxValue , arr [ i ] ) NEW_LINE DEDENT St = defaultdict ( int ) NEW_LINE for i in range ( 1 , maxValue + 1 ) : NEW_LINE INDENT St [ i ] += 1 NEW_LINE DEDENT if ( 1 in St ) : NEW_LINE INDENT St . pop ( 1 ) NEW_LINE DEDENT for i in range ( 2 , int ( math . sqrt ( maxValue ) ) + 1 ) : NEW_LINE INDENT j = 2 NEW_LINE while ( ( i * j ) <= maxValue ) : NEW_LINE INDENT if ( i * j ) in St : NEW_LINE INDENT St . pop ( i * j ) NEW_LINE DEDENT j += 1 NEW_LINE DEDENT DEDENT LeftCount = 0 NEW_LINE Prefix = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT Prefix [ i ] = LeftCount NEW_LINE if ( arr [ i ] in St ) : NEW_LINE INDENT LeftCount += 1 NEW_LINE DEDENT DEDENT RightCount = 0 NEW_LINE Suffix = [ 0 ] * N NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT Suffix [ i ] = RightCount NEW_LINE if arr [ i ] in St : NEW_LINE INDENT RightCount += 1 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT if ( Prefix [ i ] == Suffix [ i ] ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 7 , 5 , 10 , 1 , 8 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findIndex ( arr , N ) ) NEW_LINE DEDENT