codeparrot/xlcost-text-to-code huggingface dataset:
Print pair with maximum AND value in an array | Utility function to check number of elements having set msb as of pattern ; Function for finding maximum and value pair ; iterate over total of 30 bits from msb to lsb ; find the count of element having set msb ; if count >= 2 set particular bit in result ; Find the elements ; print the pair of elements ; inc count value after printing element ; return the result value ; Driver function
def checkBit ( pattern , arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( ( pattern & arr [ i ] ) == pattern ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT def maxAND ( arr , n ) : NEW_LINE INDENT res = 0 NEW_LINE for bit in range ( 31 , - 1 , - 1 ) : NEW_LINE INDENT count = checkBit ( res | ( 1 << bit ) , arr , n ) NEW_LINE if ( count >= 2 ) : NEW_LINE INDENT res |= ( 1 << bit ) NEW_LINE DEDENT DEDENT if ( res == 0 ) : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Pair ▁ = ▁ " , end = " " ) NEW_LINE count = 0 NEW_LINE i = 0 NEW_LINE while ( i < n and count < 2 ) : NEW_LINE INDENT if ( ( arr [ i ] & res ) == res ) : NEW_LINE INDENT count += 1 NEW_LINE print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT arr = [ 4 , 8 , 6 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Maximum AND Value =   " , maxAND ( arr , n ) ) NEW_LINE
Probability of choosing a random pair with maximum sum in an array | Function to get max first and second ; If current element is smaller than first , then update both first and second ; If arr [ i ] is in between first and second then update second ; cnt1 += 1 frequency of first maximum ; cnt2 += 1 frequency of second maximum ; Returns probability of choosing a pair with maximum sum . ; Driver Code
def countMaxSumPairs ( a , n ) : NEW_LINE INDENT first = 0 NEW_LINE second = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] > first ) : NEW_LINE INDENT second = first NEW_LINE first = a [ i ] NEW_LINE DEDENT elif ( a [ i ] > second and a [ i ] != first ) : NEW_LINE INDENT second = a [ i ] NEW_LINE DEDENT DEDENT cnt1 = 0 NEW_LINE cnt2 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] == first ) : NEW_LINE if ( a [ i ] == second ) : NEW_LINE DEDENT if ( cnt1 == 1 ) : NEW_LINE INDENT return cnt2 NEW_LINE DEDENT if ( cnt1 > 1 ) : NEW_LINE INDENT return cnt1 * ( cnt1 - 1 ) / 2 NEW_LINE DEDENT DEDENT def findMaxSumProbability ( a , n ) : NEW_LINE INDENT total = n * ( n - 1 ) / 2 NEW_LINE max_sum_pairs = countMaxSumPairs ( a , n ) NEW_LINE return max_sum_pairs / total NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 2 , 3 ] NEW_LINE n = len ( a ) NEW_LINE print ( findMaxSumProbability ( a , n ) ) NEW_LINE DEDENT
Find if given number is sum of first n natural numbers | Function to find no . of elements to be added from 1 to get n ; Start adding numbers from 1 ; If sum becomes equal to s return n ; Driver code
def findS ( s ) : NEW_LINE INDENT _sum = 0 NEW_LINE n = 1 NEW_LINE while ( _sum < s ) : NEW_LINE INDENT _sum += n NEW_LINE n += 1 NEW_LINE DEDENT n -= 1 NEW_LINE if _sum == s : NEW_LINE INDENT return n NEW_LINE DEDENT return - 1 NEW_LINE DEDENT s = 15 NEW_LINE n = findS ( s ) NEW_LINE if n == - 1 : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( n ) NEW_LINE DEDENT
Find if given number is sum of first n natural numbers | Python3 program of the above approach ; Function to check if the s is the sum of first N natural number ; Solution of Quadratic Equation ; Condition to check if the solution is a integer ; Driver Code ; Function Call
import math NEW_LINE def isvalid ( s ) : NEW_LINE INDENT k = ( - 1 + math . sqrt ( 1 + 8 * s ) ) / 2 NEW_LINE if ( math . ceil ( k ) == math . floor ( k ) ) : NEW_LINE INDENT return int ( k ) NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT s = 15 NEW_LINE print ( isvalid ( s ) ) NEW_LINE
Save from Bishop in chessboard | python program to find total safe position to place your Bishop ; function to calc total safe position ; i , j denotes row and column of position of bishop ; calc distance in four direction ; calc total sum of distance + 1 for unsafe positions ; return total safe positions ; driver function
import math NEW_LINE def calcSafe ( pos ) : NEW_LINE INDENT j = pos % 10 NEW_LINE i = pos / 10 NEW_LINE dis_11 = min ( abs ( 1 - i ) , abs ( 1 - j ) ) NEW_LINE dis_18 = min ( abs ( 1 - i ) , abs ( 8 - j ) ) NEW_LINE dis_81 = min ( abs ( 8 - i ) , abs ( 1 - j ) ) NEW_LINE dis_88 = min ( abs ( 8 - i ) , abs ( 8 - j ) ) NEW_LINE sum = ( dis_11 + dis_18 + dis_81 + dis_88 + 1 ) NEW_LINE return ( 64 - sum ) NEW_LINE DEDENT pos = 34 NEW_LINE print ( " Safe ▁ Positions ▁ = ▁ " , math . ceil ( calcSafe ( pos ) ) ) NEW_LINE
Count number of elements between two given elements in array | Function to count number of elements occurs between the elements . ; Find num1 ; If num1 is not present or present at end ; Find num2 ; If num2 is not present ; return number of elements between the two elements . ; Driver Code
def getCount ( arr , n , num1 , num2 ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] == num1 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( i >= n - 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for j in range ( n - 1 , i + 1 , - 1 ) : NEW_LINE INDENT if ( arr [ j ] == num2 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( j == i ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( j - i - 1 ) NEW_LINE DEDENT arr = [ 3 , 5 , 7 , 6 , 4 , 9 , 12 , 4 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE num1 = 5 NEW_LINE num2 = 4 NEW_LINE print ( getCount ( arr , n , num1 , num2 ) ) NEW_LINE
Best meeting point in 2D binary array | Python program to find best meeting point in 2D array ; Find all members home 's position ; Sort positions so we can find most beneficial point ; middle position will always beneficial for all group members but it will be sorted which we have already done ; Now find total distance from best meeting point ( x , y ) using Manhattan Distance formula ; Driver Code
ROW = 3 NEW_LINE COL = 5 NEW_LINE def minTotalDistance ( grid : list ) -> int : NEW_LINE INDENT if ROW == 0 or COL == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT vertical = [ ] NEW_LINE horizontal = [ ] NEW_LINE for i in range ( ROW ) : NEW_LINE INDENT for j in range ( COL ) : NEW_LINE INDENT if grid [ i ] [ j ] == 1 : NEW_LINE INDENT vertical . append ( i ) NEW_LINE horizontal . append ( j ) NEW_LINE DEDENT DEDENT DEDENT vertical . sort ( ) NEW_LINE horizontal . sort ( ) NEW_LINE size = len ( vertical ) // 2 NEW_LINE x = vertical [ size ] NEW_LINE y = horizontal [ size ] NEW_LINE distance = 0 NEW_LINE for i in range ( ROW ) : NEW_LINE INDENT for j in range ( COL ) : NEW_LINE INDENT if grid [ i ] [ j ] == 1 : NEW_LINE INDENT distance += abs ( x - i ) + abs ( y - j ) NEW_LINE DEDENT DEDENT DEDENT return distance NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT grid = [ [ 1 , 0 , 1 , 0 , 1 ] , [ 0 , 1 , 0 , 0 , 0 ] , [ 0 , 1 , 1 , 0 , 0 ] ] NEW_LINE print ( minTotalDistance ( grid ) ) NEW_LINE DEDENT
Count numbers with difference between number and its digit sum greater than specific value | Utility method to get sum of digits of K ; loop until K is not zero ; method returns count of numbers smaller than N , satisfying difference condition ; binary search while loop ; if difference between number and its sum of digit is smaller than given difference then smallest number will be on left side ; if difference between number and its sum of digit greater than equal to given difference then smallest number will be on right side ; return the difference between ' smallest ▁ number ▁ ▁ found ' and ' N ' as result ; Driver code to test above methods
def sumOfDigit ( K ) : NEW_LINE INDENT sod = 0 NEW_LINE while ( K ) : NEW_LINE INDENT sod = sod + K % 10 NEW_LINE K = K // 10 NEW_LINE DEDENT return sod NEW_LINE DEDENT def totalNumbersWithSpecificDifference ( N , diff ) : NEW_LINE INDENT low = 1 NEW_LINE high = N NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if ( mid - sumOfDigit ( mid ) < diff ) : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT return ( N - high ) NEW_LINE DEDENT N = 13 NEW_LINE diff = 2 NEW_LINE print ( totalNumbersWithSpecificDifference ( N , diff ) ) NEW_LINE
Randomized Binary Search Algorithm | To generate random number between x and y ie . . [ x , y ] ; A iterative randomized binary search function . It returns location of x in given array arr [ l . . r ] if present , otherwise - 1 ; Here we have defined middle as random index between l and r ie . . [ l , r ] ; Check if x is present at mid ; If x greater , ignore left half ; If x is smaller , ignore right half ; if we reach here , then element was not present ; Driver code
from random import randint NEW_LINE def getRandom ( x , y ) : NEW_LINE INDENT return randint ( x , y ) NEW_LINE DEDENT def randomizedBinarySearch ( arr , l , r , x ) : NEW_LINE INDENT while ( l <= r ) : NEW_LINE INDENT m = getRandom ( l , r ) NEW_LINE if ( arr [ m ] == x ) : NEW_LINE INDENT return m NEW_LINE DEDENT if ( arr [ m ] < x ) : NEW_LINE INDENT l = m + 1 NEW_LINE DEDENT else : NEW_LINE INDENT r = m - 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT arr = [ 2 , 3 , 4 , 10 , 40 ] NEW_LINE n = len ( arr ) NEW_LINE x = 10 NEW_LINE result = randomizedBinarySearch ( arr , 0 , n - 1 , x ) NEW_LINE if result == 1 : NEW_LINE INDENT print ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Element ▁ is ▁ present ▁ at ▁ index " , result ) NEW_LINE DEDENT
Number of buildings facing the sun | Returns count buildings that can see sunlight ; Initialuze result ( Note that first building always sees sunlight ) ; Start traversing element ; If curr_element is maximum or current element is equal , update maximum and increment count ; Driver code
def countBuildings ( arr , n ) : NEW_LINE INDENT count = 1 NEW_LINE curr_max = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] > curr_max or arr [ i ] == curr_max ) : NEW_LINE INDENT count += 1 NEW_LINE curr_max = arr [ i ] NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 7 , 4 , 8 , 2 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countBuildings ( arr , n ) ) NEW_LINE
Find index of an extra element present in one sorted array | Returns index of extra . element in arr1 [ ] n is size of arr2 [ ] . Size of arr1 [ ] is n - 1. ; Driver code ; Solve is passed both arrays
def findExtra ( arr1 , arr2 , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr1 [ i ] != arr2 [ i ] ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return n NEW_LINE DEDENT arr1 = [ 2 , 4 , 6 , 8 , 10 , 12 , 13 ] NEW_LINE arr2 = [ 2 , 4 , 6 , 8 , 10 , 12 ] NEW_LINE n = len ( arr2 ) NEW_LINE print ( findExtra ( arr1 , arr2 , n ) ) NEW_LINE
Find index of an extra element present in one sorted array | Returns index of extra element in arr1 [ ] . n is size of arr2 [ ] . Size of arr1 [ ] is n - 1. ; left and right are end points denoting the current range . ; If middle element is same of both arrays , it means that extra element is after mid so we update left to mid + 1 ; If middle element is different of the arrays , it means that the index we are searching for is either mid , or before mid . Hence we update right to mid - 1. ; when right is greater than left our search is complete . ; Driver code ; Solve is passed both arrays
def findExtra ( arr1 , arr2 , n ) : NEW_LINE INDENT left = 0 NEW_LINE right = n - 1 NEW_LINE while ( left <= right ) : NEW_LINE INDENT mid = ( int ) ( ( left + right ) / 2 ) NEW_LINE if ( arr2 [ mid ] == arr1 [ mid ] ) : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT index = mid NEW_LINE right = mid - 1 NEW_LINE DEDENT DEDENT return index NEW_LINE DEDENT arr1 = [ 2 , 4 , 6 , 8 , 10 , 12 , 13 ] NEW_LINE arr2 = [ 2 , 4 , 6 , 8 , 10 , 12 ] NEW_LINE n = len ( arr2 ) NEW_LINE print ( findExtra ( arr1 , arr2 , n ) ) NEW_LINE
Make all array elements equal with minimum cost | Utility method to compute cost , when all values of array are made equal to X ; Method to find minimum cost to make all elements equal ; Setting limits for ternary search by smallest and largest element ; loop until difference between low and high become less than 3 , because after that mid1 and mid2 will start repeating ; mid1 and mid2 are representative array equal values of search space ; if mid2 point gives more total cost , skip third part ; if mid1 point gives more total cost , skip first part ; computeCost gets optimum cost by sending average of low and high as X ; Driver code
def computeCost ( arr , N , X ) : NEW_LINE INDENT cost = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT cost += abs ( arr [ i ] - X ) NEW_LINE DEDENT return cost NEW_LINE DEDENT def minCostToMakeElementEqual ( arr , N ) : NEW_LINE INDENT low = high = arr [ 0 ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( low > arr [ i ] ) : low = arr [ i ] NEW_LINE if ( high < arr [ i ] ) : high = arr [ i ] NEW_LINE DEDENT while ( ( high - low ) > 2 ) : NEW_LINE INDENT mid1 = low + ( high - low ) // 3 NEW_LINE mid2 = high - ( high - low ) // 3 NEW_LINE cost1 = computeCost ( arr , N , mid1 ) NEW_LINE cost2 = computeCost ( arr , N , mid2 ) NEW_LINE if ( cost1 < cost2 ) : NEW_LINE INDENT high = mid2 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid1 NEW_LINE DEDENT DEDENT return computeCost ( arr , N , ( low + high ) // 2 ) NEW_LINE DEDENT arr = [ 1 , 100 , 101 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minCostToMakeElementEqual ( arr , N ) ) NEW_LINE
Minimum number of jumps required to sort numbers placed on a number line | Function to find the minimum number of jumps required to sort the array ; Base Case ; Store the required result ; Stores the current position of elements and their respective maximum jump ; Used to check if a position is already taken by another element ; Stores the sorted array a [ ] ; Traverse the array w [ ] & update positions jumps array a [ ] ; Sort the array a [ ] ; Traverse the array a [ ] over the range [ 1 , N - 1 ] ; Store the index of current element and its just smaller element in array w [ ] ; Iterate until current element position is at most its just smaller element position ; Update the position of the current element ; Print the result ; Driver Code
def minJumps ( w , l , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE return NEW_LINE DEDENT ans = 0 NEW_LINE pos = { } NEW_LINE jump = { } NEW_LINE filled = { } NEW_LINE a = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT pos [ w [ i ] ] = i NEW_LINE filled [ i ] = True NEW_LINE jump [ w [ i ] ] = l [ i ] NEW_LINE a [ i ] = w [ i ] NEW_LINE DEDENT a . sort ( ) NEW_LINE for curr in range ( 1 , n , 1 ) : NEW_LINE INDENT currElementPos = pos [ a [ curr ] ] NEW_LINE prevElementPos = pos [ a [ curr - 1 ] ] NEW_LINE if ( currElementPos > prevElementPos ) : NEW_LINE INDENT continue NEW_LINE DEDENT while ( currElementPos <= prevElementPos or ( currElementPos in filled and filled [ currElementPos ] ) ) : NEW_LINE INDENT currElementPos += jump [ a [ curr ] ] NEW_LINE ans += 1 NEW_LINE DEDENT pos [ a [ curr ] ] = currElementPos NEW_LINE filled [ currElementPos ] = True NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT W = [ 2 , 1 , 4 , 3 ] NEW_LINE L = [ 4 , 1 , 2 , 4 ] NEW_LINE N = len ( W ) NEW_LINE minJumps ( W , L , N ) NEW_LINE DEDENT
Make an array strictly increasing by repeatedly subtracting and adding arr [ i | Function to check if an array can be made strictly increasing ; Traverse the given array arr [ ] ; Update the value of p , arr [ i ] , and arr [ i - 1 ] ; Traverse the given array ; Check if the array arr [ ] is strictly increasing or not ; Otherwise , array is increasing order , prYes ; Driver Code
def check ( arr , n ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i - 1 ] >= ( i - 1 ) ) : NEW_LINE INDENT p = arr [ i - 1 ] - ( i - 1 ) NEW_LINE arr [ i ] += p NEW_LINE arr [ i - 1 ] -= p NEW_LINE DEDENT DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] <= arr [ i - 1 ] ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " Yes " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 5 , 2 , 7 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE check ( arr , N ) NEW_LINE DEDENT
Rearrange array to make decimal equivalents of reversed binary representations of array elements sorted | Function to reverse the bits of a number ; Stores the reversed number ; Divide rev by 2 ; If the value of N is odd ; Update the value of N ; Return the final value of rev ; Function for rearranging the array element according to the given rules ; Stores the new array elements ; Function for rearranging the array ; Stores the new array ; Function to sort the array by reversing the binary representation ; Creating a new array ; Sort the array with the key ; Get arr from newArr ; Print the sorted array ; Driver Code
def keyFunc ( n ) : NEW_LINE INDENT rev = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT rev = rev << 1 NEW_LINE if ( n & 1 == 1 ) : NEW_LINE INDENT rev = rev ^ 1 NEW_LINE DEDENT n = n >> 1 NEW_LINE DEDENT return rev NEW_LINE DEDENT def getNew ( arr ) : NEW_LINE INDENT ans = [ ] NEW_LINE for i in arr : NEW_LINE INDENT ans . append ( [ keyFunc ( i ) , i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def getArr ( arr ) : NEW_LINE INDENT ans = [ ] NEW_LINE for i in arr : NEW_LINE INDENT ans . append ( i [ 1 ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def sortArray ( arr ) : NEW_LINE INDENT newArr = getNew ( arr ) NEW_LINE newArr . sort ( ) NEW_LINE arr = getArr ( newArr ) NEW_LINE print ( arr ) NEW_LINE DEDENT arr = [ 43 , 52 , 61 , 41 ] NEW_LINE sortArray ( arr ) NEW_LINE
Minimize maximum array element by splitting array elements into powers of two at most K times | Function to find the minimum value of the maximum element of the array by splitting at most K array element into perfect powers of 2 ; Sort the array element in the ascending order ; Reverse the array ; If count of 0 is equal to N ; Otherwise , if K is greater than or equal to N ; Otherwise ; Driver Code
def minimumSize ( arr , N , K ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE arr . reverse ( ) NEW_LINE zero = arr . count ( 0 ) NEW_LINE if zero == N : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT elif K >= N : NEW_LINE INDENT print ( 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( arr [ K ] ) NEW_LINE DEDENT DEDENT arr = [ 2 , 4 , 8 , 2 ] NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE minimumSize ( arr , N , K ) NEW_LINE
Check if removal of a subsequence of non | Function to check if it is possible to sort the array or not ; Stores the index if there are two consecutive 1 's in the array ; Traverse the given array ; Check adjacent same elements having values 1 s ; If there are no two consecutive 1 s , then always remove all the 1 s from array & make it sorted ; If two consecutive 0 ' s ▁ are ▁ ▁ present ▁ after ▁ two ▁ consecutive ▁ ▁ 1' s then array can 't be sorted ; Otherwise , print Yes ; Driver Code
def isPossibleToSort ( arr , N ) : NEW_LINE INDENT idx = - 1 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( arr [ i ] == 1 and arr [ i - 1 ] == 1 ) : NEW_LINE INDENT idx = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( idx == - 1 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE return NEW_LINE DEDENT for i in range ( idx + 1 , N , 1 ) : NEW_LINE INDENT if ( arr [ i ] == 0 and arr [ i - 1 ] == 0 ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " YES " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 0 , 1 , 0 , 1 , 1 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE isPossibleToSort ( arr , N ) NEW_LINE DEDENT
Maximize ropes of consecutive length possible by connecting given ropes | Function to find maximized count of ropes of consecutive length ; Stores the maximum count of ropes of consecutive length ; Sort the ropes by their length ; Traverse the array ; If size of the current rope is less than or equal to current maximum possible size + 1 , update the range to curSize + ropes [ i ] ; If a rope of size ( curSize + 1 ) cannot be obtained ; Driver Code ; Input ; Function Call
def maxConsecutiveRopes ( ropes , N ) : NEW_LINE INDENT curSize = 0 NEW_LINE ropes = sorted ( ropes ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( ropes [ i ] <= curSize + 1 ) : NEW_LINE INDENT curSize = curSize + ropes [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return curSize NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE ropes = [ 1 , 2 , 7 , 1 , 1 ] NEW_LINE print ( maxConsecutiveRopes ( ropes , N ) ) NEW_LINE DEDENT
Maximum ranges that can be uniquely represented by any integer from the range | Function to find the maximum number of ranges where each range can be uniquely represented by an integer ; Sort the ranges in ascending order ; Stores the count of ranges ; Stores previously assigned range ; Traverse the vector arr [ ] ; Skip the similar ranges of size 1 ; Find the range of integer available to be assigned ; Check if an integer is available to be assigned ; Update the previously assigned range ; Update the count of range ; Return the maximum count of ranges ; Driver Code
def maxRanges ( arr , N ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE count = 1 NEW_LINE prev = arr [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT last = arr [ i - 1 ] NEW_LINE current = arr [ i ] NEW_LINE if ( last [ 0 ] == current [ 0 ] and last [ 1 ] == current [ 1 ] and current [ 1 ] == current [ 0 ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT start = max ( prev [ 0 ] , current [ 0 ] - 1 ) NEW_LINE end = max ( prev [ 1 ] , current [ 1 ] ) NEW_LINE if ( end - start > 0 ) : NEW_LINE INDENT prev [ 0 ] = 1 + start NEW_LINE prev [ 1 ] = end NEW_LINE count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 1 , 4 ] , [ 4 , 4 ] , [ 2 , 2 ] , [ 3 , 4 ] , [ 1 , 1 ] ] NEW_LINE N = len ( arr ) NEW_LINE print ( maxRanges ( arr , N ) ) NEW_LINE DEDENT