codeparrot/xlcost-text-to-code huggingface dataset:
Rearrange given array to obtain positive prefix sums at exactly X indices | Java program for the above approach ; Function to rearrange the array according to the given condition ; Using 2 nd operation making all values positive ; Sort the array ; Assign K = N - K ; Count number of zeros ; If number of zeros if greater ; Using 2 nd operation convert it into one negative ; Using 2 nd operation convert it into one negative ; Print the array ; Driver Code ; Function Call
import java . util . * ; class GFG { static int count ( int [ ] a ) { int counter = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] == 0 ) { counter ++ ; } } return counter ; } static void rearrange ( int [ ] a , int n , int x ) { for ( int i = 0 ; i < n ; i ++ ) a [ i ] = Math . abs ( a [ i ] ) ; Arrays . sort ( a ) ; x = n - x ; int z = count ( a ) ; if ( x > n - z ) { System . out . println ( " - 1" ) ; return ; } for ( int i = 0 ; i < n && x > 0 ; i += 2 ) { a [ i ] = - a [ i ] ; x -- ; } for ( int i = n - 1 ; i >= 0 && x > 0 ; i -- ) { if ( a [ i ] > 0 ) { a [ i ] = - a [ i ] ; x -- ; } } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( a [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 0 , - 2 , 4 , 5 , - 3 } ; int K = 3 ; int N = arr . length ; rearrange ( arr , N , K ) ; } }
Maximize product of a strictly increasing or decreasing subarray | Java program to implement the above approach ; Function to find the maximum product of subarray in the array , arr [ ] ; Maximum positive product ending at the i - th index ; Minimum negative product ending at the current index ; Maximum product up to i - th index ; Check if an array element is positive or not ; Traverse the array ; If current element is positive ; Update max_ending_here ; Update min_ending_here ; Update flag ; If current element is 0 , reset the start index of subarray ; Update max_ending_here ; Update min_ending_here ; If current element is negative ; Stores max_ending_here ; Update max_ending_here ; Update min_ending_here ; Update max_so_far , if needed ; If no array elements is positive and max_so_far is 0 ; Function to find the maximum product of either increasing subarray or the decreasing subarray ; Stores start index of either increasing subarray or the decreasing subarray ; Initially assume maxProd to be 1 ; Traverse the array ; Store the longest either increasing subarray or the decreasing subarray whose start index is i ; Check for increasing subarray ; Insert elements of increasing subarray ; Check for decreasing subarray ; Insert elements of decreasing subarray ; Stores maximum subarray product of current increasing or decreasing subarray ; Update maxProd ; Update i ; Finally print maxProd ; Driver Code
import java . util . * ; class GFG { static int maxSubarrayProduct ( Vector < Integer > arr , int n ) { int max_ending_here = 1 ; int min_ending_here = 1 ; int max_so_far = 0 ; int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr . get ( i ) > 0 ) { max_ending_here = max_ending_here * arr . get ( i ) ; min_ending_here = Math . min ( min_ending_here * arr . get ( i ) , 1 ) ; flag = 1 ; } else if ( arr . get ( i ) == 0 ) { max_ending_here = 1 ; min_ending_here = 1 ; } else { int temp = max_ending_here ; max_ending_here = Math . max ( min_ending_here * arr . get ( i ) , 1 ) ; min_ending_here = temp * arr . get ( i ) ; } if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; } if ( flag == 0 && max_so_far == 0 ) return 0 ; return max_so_far ; } static int findMaxProduct ( int [ ] a , int n ) { int i = 0 ; int maxProd = 1 ; while ( i < n ) { Vector < Integer > v = new Vector < > ( ) ; v . add ( a [ i ] ) ; if ( i < n - 1 && a [ i ] < a [ i + 1 ] ) { while ( i < n - 1 && a [ i ] < a [ i + 1 ] ) { v . add ( a [ i + 1 ] ) ; i += 1 ; } } else if ( i < n - 1 && a [ i ] > a [ i + 1 ] ) { while ( i < n - 1 && a [ i ] > a [ i + 1 ] ) { v . add ( a [ i + 1 ] ) ; i += 1 ; } } int prod = maxSubarrayProduct ( v , v . size ( ) ) ; maxProd = Math . max ( maxProd , prod ) ; i ++ ; } return maxProd ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 10 , 8 , 1 , 100 , 101 } ; int N = arr . length ; System . out . print ( findMaxProduct ( arr , N ) ) ; } }
Number of connected components of a graph ( using Disjoint Set Union ) | Java program for the above approach ; Stores the parent of each vertex ; Function to find the topmost parent of vertex a ; If current vertex is the topmost vertex ; Otherwise , set topmost vertex of its parent as its topmost vertex ; Function to connect the component having vertex a with the component having vertex b ; Connect edges ; Function to find unique top most parents ; Traverse all vertices ; Insert all topmost vertices obtained ; Print count of connected components ; Function to print answer ; Setting parent to itself ; Traverse all edges ; Print answer ; Driver Code ; Given N ; Given edges ; Function call
import java . util . * ; class GFG { static int [ ] parent = new int [ 1000000 ] ; static int root ( int a ) { if ( a == parent [ a ] ) { return a ; } return parent [ a ] = root ( parent [ a ] ) ; } static void connect ( int a , int b ) { a = root ( a ) ; b = root ( b ) ; if ( a != b ) { parent [ b ] = a ; } } static void connectedComponents ( int n ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { s . add ( parent [ i ] ) ; } System . out . println ( s . size ( ) ) ; } static void printAnswer ( int N , int [ ] [ ] edges ) { for ( int i = 0 ; i <= N ; i ++ ) { parent [ i ] = i ; } for ( int i = 0 ; i < edges . length ; i ++ ) { connect ( edges [ i ] [ 0 ] , edges [ i ] [ 1 ] ) ; } connectedComponents ( N ) ; } public static void main ( String [ ] args ) { int N = 8 ; int [ ] [ ] edges = { { 1 , 0 } , { 0 , 2 } , { 5 , 3 } , { 3 , 4 } , { 6 , 7 } } ; printAnswer ( N , edges ) ; } }
Remove all zero | Java program for the above approach ; Function to remove the rows or columns from the matrix which contains all 0 s elements ; Stores count of rows ; col [ i ] : Stores count of 0 s in current column ; row [ i ] : Stores count of 0 s in current row ; Traverse the matrix ; Stores count of 0 s in current row ; Update col [ j ] ; Update count ; Update row [ i ] ; Traverse the matrix ; If all elements of current row is 0 ; If all elements of current column is 0 ; Driver Code ; Function Call
class GFG { static void removeZeroRowCol ( int arr [ ] [ ] ) { int n = arr . length ; int col [ ] = new int [ n + 1 ] ; int row [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i < n ; ++ i ) { int count = 0 ; for ( int j = 0 ; j < n ; ++ j ) { if ( arr [ i ] [ j ] == 1 ) col [ j ] += 1 ; else col [ j ] += 0 ; if ( arr [ i ] [ j ] == 1 ) count += 1 ; else count += 0 ; } row [ i ] = count ; } for ( int i = 0 ; i < n ; ++ i ) { if ( row [ i ] == 0 ) { continue ; } for ( int j = 0 ; j < n ; ++ j ) { if ( col [ j ] != 0 ) System . out . print ( arr [ i ] [ j ] ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 1 , 0 , 1 } , { 0 , 0 , 0 , 0 } , { 1 , 1 , 0 , 1 } , { 0 , 1 , 0 , 1 } } ; removeZeroRowCol ( arr ) ; } }
Find a pair of overlapping intervals from a given Set | Java program to implement the above approach ; Function to find a pair ( i , j ) such that i - th interval lies within the j - th interval ; Store interval and index of the interval in the form of { { l , r } , index } ; Traverse the array , arr [ ] [ ] ; Stores l - value of the interval ; Stores r - value of the interval ; Push current interval and index into tup ; Sort the vector based on l - value of the intervals ; Stores r - value of current interval ; Stores index of current interval ; Traverse the vector , tup [ ] ; Stores l - value of previous interval ; Stores l - value of current interval ; If Q and R are equal ; Print the index of interval ; Stores r - value of current interval ; If T is less than or equal to curr ; Update curr ; Update currPos ; If such intervals found ; Driver code ; Given l - value of segments ; Given r - value of segments ; Given size ; Function Call
import java . util . * ; import java . lang . * ; class pair { int l , r , index ; pair ( int l , int r , int index ) { this . l = l ; this . r = r ; this . index = index ; } } class GFG { static void findOverlapSegement ( int N , int [ ] a , int [ ] b ) { ArrayList < pair > tup = new ArrayList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int x , y ; x = a [ i ] ; y = b [ i ] ; tup . add ( new pair ( x , y , i ) ) ; } Collections . sort ( tup , ( aa , bb ) -> ( aa . l != bb . l ) ? aa . l - bb . l : aa . r - bb . r ) ; int curr = tup . get ( 0 ) . r ; int currPos = tup . get ( 0 ) . index ; for ( int i = 1 ; i < N ; i ++ ) { int Q = tup . get ( i - 1 ) . l ; int R = tup . get ( i ) . l ; if ( Q == R ) { if ( tup . get ( i - 1 ) . r < tup . get ( i ) . r ) System . out . print ( tup . get ( i - 1 ) . index + " ▁ " + tup . get ( i ) . index ) ; else System . out . print ( tup . get ( i ) . index + " ▁ " + tup . get ( i - 1 ) . index ) ; return ; } int T = tup . get ( i ) . r ; if ( T <= curr ) { System . out . print ( tup . get ( i ) . index + " ▁ " + currPos ) ; return ; } else { curr = T ; currPos = tup . get ( i ) . index ; } } System . out . print ( " - 1 ▁ - 1" ) ; } public static void main ( String [ ] args ) { int [ ] a = { 1 , 2 , 3 , 2 , 2 } ; int [ ] b = { 5 , 10 , 10 , 2 , 15 } ; int N = a . length ; findOverlapSegement ( N , a , b ) ; } }
Count maximum concatenation of pairs from given array that are divisible by 3 | Java program to implement the above approach ; Function to count pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Stores count pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Check if an element present in any pair or not ; Generate all possible pairs ; If the element already present in a pair ; If the element already present in a pair ; If concatenation of elements is divisible by 3 ; Update ans ; Mark i is True ; Mark j is True ; Driver Code ; To display the result
import java . io . * ; import java . util . Arrays ; class GFG { public static int countDivBy3InArray ( int [ ] arr ) { int ans = 0 ; boolean [ ] taken = new boolean [ arr . length ] ; Arrays . fill ( taken , false ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( taken [ i ] == true ) { continue ; } for ( int j = i + 1 ; j < arr . length ; j ++ ) { if ( taken [ j ] == true ) { continue ; } if ( Integer . parseInt ( Integer . toString ( arr [ i ] ) + Integer . toString ( arr [ j ] ) ) % 3 == 0 || Integer . parseInt ( Integer . toString ( arr [ j ] ) + Integer . toString ( arr [ i ] ) ) % 3 == 0 ) { ans += 1 ; taken [ i ] = true ; taken [ j ] = true ; } } } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 3 , 2 , 8 , 7 } ; System . out . println ( countDivBy3InArray ( arr ) ) ; } }
Maximum average of subtree values in a given Binary Tree | Java program for the above approach ; Structure of the Tree node ; Stores the result ; Function for finding maximum subtree average ; Checks if current node is not null and doesn 't have any children ; Stores sum of its subtree in index 0 and count number of nodes in index 1 ; Traverse all children of the current node ; Recursively calculate max average of subtrees among its children ; Increment sum by sum of its child 's subtree ; Increment number of nodes by its child 's node ; Increment sum by current node 's value ; Increment number of nodes by one ; Take maximum of ans and current node 's average ; Finally return pair of { sum , count } ; Driver Code ; Given tree ; Function call ; Print answer
import java . util . * ; class TreeNode { public int val ; public List < TreeNode > children ; public TreeNode ( int v ) { val = v ; children = new ArrayList < TreeNode > ( ) ; } } class GFG { static double ans = 0.0 ; public static double [ ] MaxAverage ( TreeNode root ) { if ( root . children != null && root . children . size ( ) == 0 ) { ans = Math . max ( ans , root . val ) ; return new double [ ] { root . val , 1 } ; } double [ ] childResult = new double [ 2 ] ; for ( TreeNode child : root . children ) { double [ ] childTotal = MaxAverage ( child ) ; childResult [ 0 ] = childResult [ 0 ] + childTotal [ 0 ] ; childResult [ 1 ] = childResult [ 1 ] + childTotal [ 1 ] ; } double sum = childResult [ 0 ] + root . val ; double count = childResult [ 1 ] + 1 ; ans = Math . max ( ans , sum / count ) ; return new double [ ] { sum , count } ; } public static void main ( String [ ] args ) { TreeNode root = new TreeNode ( 20 ) ; TreeNode left = new TreeNode ( 12 ) ; TreeNode right = new TreeNode ( 18 ) ; root . children . add ( left ) ; root . children . add ( right ) ; left . children . add ( new TreeNode ( 11 ) ) ; left . children . add ( new TreeNode ( 3 ) ) ; right . children . add ( new TreeNode ( 15 ) ) ; right . children . add ( new TreeNode ( 8 ) ) ; MaxAverage ( root ) ; System . out . println ( ans ) ; } }
Check if a Binary String can be converted to another by reversing substrings consisting of even number of 1 s | Java program for the above approach ; Function to check if string A can be transformed to string B by reversing substrings of A having even number of 1 s ; Store the size of string A ; Store the size of string B ; Store the count of 1 s in A and B ; Stores cntA for string A and cntB for string B ; Traverse the string A ; If current character is 1 ; Increment 1 s count ; Otherwise , update odd1A or even1A depending whether count1A is odd or even ; Traverse the string B ; If current character is 1 ; Increment 1 s count ; Otherwise , update odd1B or even1B depending whether count1B is odd or even ; If the condition is satisfied ; If true , print Yes ; Otherwise , print No ; Driver Code ; Function Call
class GFG { public static void canTransformStrings ( String A , String B ) { int n1 = A . length ( ) ; int n2 = B . length ( ) ; int count1A = 0 , count1B = 0 ; int odd1A = 0 , odd1B = 0 ; int even1A = 0 , even1B = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) { if ( A . charAt ( i ) == '1' ) count1A ++ ; else { if ( ( count1A & 1 ) == 1 ) odd1A ++ ; else even1A ++ ; } } for ( int i = 0 ; i < n2 ; i ++ ) { if ( B . charAt ( i ) == '1' ) count1B ++ ; else { if ( ( count1B & 1 ) == 1 ) odd1B ++ ; else even1B ++ ; } } if ( count1A == count1B && odd1A == odd1B && even1A == even1B ) { System . out . print ( " Yes " ) ; } else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { String A = "10011" , B = "11100" ; canTransformStrings ( A , B ) ; } }
Minimize remaining array element by removing pairs and replacing them by their absolute difference | Java program for above approach ; Function to find the smallest element left in the array by the given operations ; Base Case ; If this subproblem has occurred previously ; Including i - th array element into the first subset ; If i - th array element is not selected ; Update dp [ i ] [ sum ] ; Utility function to find smallest element left in the array by the given operations ; Stores sum of the array elements ; Traverse the array ; Update total ; Stores overlapping subproblems ; Driver function
import java . util . * ; import java . lang . * ; class GFG { static int smallestLeft ( int arr [ ] , int total , int sum , int i , int [ ] [ ] dp ) { if ( i == 0 ) { return Math . abs ( total - 2 * sum ) ; } if ( dp [ i ] [ sum ] != - 1 ) return dp [ i ] [ sum ] ; int X = smallestLeft ( arr , total , sum + arr [ i - 1 ] , i - 1 , dp ) ; int Y = smallestLeft ( arr , total , sum , i - 1 , dp ) ; return dp [ i ] [ sum ] = Math . min ( X , Y ) ; } static void UtilSmallestElement ( int arr [ ] , int N ) { int total = 0 ; for ( int i = 0 ; i < N ; i ++ ) { total += arr [ i ] ; } int [ ] [ ] dp = new int [ N + 1 ] [ total ] ; for ( int [ ] k : dp ) Arrays . fill ( k , - 1 ) ; System . out . println ( smallestLeft ( arr , total , 0 , N , dp ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 7 , 4 , 1 , 8 , 1 } ; int N = arr . length ; UtilSmallestElement ( arr , N ) ; } }
Minimize remaining array element by removing pairs and replacing them by their absolute difference | Java program to implement the above approach ; Function to find minimize the remaining array element by removing pairs and replacing them by their absolute difference ; Stores sum of array elements ; Traverse the array ; Update totalSum ; Stores half of totalSum ; dp [ i ] : True if sum i can be obtained as a subset sum ; Base case ; Stores closest sum that can be obtained as a subset sum ; Traverse the array ; Iterate over all possible value of sum ; Update dp [ j ] ; If sum i can be obtained from array elements ; Update reach ; Driver Code
import java . util . * ; class GFG { static int SmallestElementLeft ( int arr [ ] , int N ) { int totalSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalSum += arr [ i ] ; } int req = totalSum / 2 ; boolean [ ] dp = new boolean [ req + 1 ] ; Arrays . fill ( dp , false ) ; dp [ 0 ] = true ; int reach = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = req ; j - arr [ i ] >= 0 ; j -- ) { dp [ j ] = dp [ j ] || dp [ j - arr [ i ] ] ; if ( dp [ j ] ) { reach = Math . max ( reach , j ) ; } } } return totalSum - ( 2 * reach ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 2 } ; int N = arr . length ; System . out . print ( SmallestElementLeft ( arr , N ) ) ; } }
Replace even | Java program to implement the above approach ; Function to count the minimum number of substrings of str1 such that replacing even - indexed characters of those substrings converts the string str1 to str2 ; Stores length of str1 ; Convert str1 to character array ; Stores minimum count of operations to convert str1 to str2 ; Traverse both the given string ; If current character in both the strings are equal ; Stores current index of the string ; If current character in both the string are not equal ; Replace str1 [ ptr ] by str2 [ ptr ] ; Update ptr ; Update cntOp ; Driver Code
import java . io . * ; class GFG { static int min_Operations ( String str1 , String str2 ) { int N = str1 . length ( ) ; char [ ] str = str1 . toCharArray ( ) ; int cntOp = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == str2 . charAt ( i ) ) { continue ; } int ptr = i ; while ( ptr < N && str [ ptr ] != str2 . charAt ( ptr ) ) { str [ ptr ] = str2 . charAt ( ptr ) ; ptr += 2 ; } cntOp ++ ; } return cntOp ; } public static void main ( String [ ] args ) { String str1 = " abcdef " ; String str2 = " ffffff " ; System . out . println ( min_Operations ( str1 , str2 ) ) ; } }
Check if GCD of all Composite Numbers in an array divisible by K is a Fibonacci Number or not | Java Program for the above approach ; Function to check if a number is composite or not ; Corner cases ; Check if the number is divisible by 2 or 3 or not ; Check if n is a multiple of any other prime number ; Function to check if a number is a Perfect Square or not ; Function to check if a number is a Fibonacci number or not ; If 5 * n ^ 2 + 4 or 5 * n ^ 2 - 4 or both are perfect square ; Function to check if GCD of composite numbers from the array a [ ] which are divisible by k is a Fibonacci number or not ; Traverse the array ; If array element is composite and divisible by k ; Calculate GCD of all elements in compositeset ; If GCD is Fibonacci ; Recursive function to return gcd of a and b ; Driver Code
import java . util . * ; class GFG { static boolean isComposite ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } static boolean isPerfectSquare ( int x ) { int s = ( int ) Math . sqrt ( x ) ; return ( s * s == x ) ; } static boolean isFibonacci ( int n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } static void ifgcdFibonacci ( int a [ ] , int n , int k ) { Vector < Integer > compositeset = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( isComposite ( a [ i ] ) && a [ i ] % k == 0 ) { compositeset . add ( a [ i ] ) ; } } int gcd = compositeset . get ( 0 ) ; for ( int i = 1 ; i < compositeset . size ( ) ; i ++ ) { gcd = __gcd ( gcd , compositeset . get ( i ) ) ; if ( gcd == 1 ) { break ; } } if ( isFibonacci ( gcd ) ) { System . out . print ( " Yes " ) ; return ; } System . out . print ( " No " ) ; return ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 34 , 2 , 4 , 8 , 5 , 7 , 11 } ; int n = arr . length ; int k = 2 ; ifgcdFibonacci ( arr , n , k ) ; } }
Longest subarray in which all elements are a factor of K | Java program to implement the above approach ; Function to find the length of the longest subarray in which all elements are a factor of K ; Stores length of the longest subarray in which all elements are a factor of K ; Stores length of the current subarray ; Traverse the array arr [ ] ; If A [ i ] is a factor of K ; Update Len ; Update MaxLen ; If A [ i ] is not a factor of K ; Update Len ; Driver Code
import java . util . * ; class GFG { static int find_longest_subarray ( int [ ] A , int N , int K ) { int MaxLen = 0 ; int Len = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( K % A [ i ] == 0 ) { Len ++ ; MaxLen = Math . max ( MaxLen , Len ) ; } else { Len = 0 ; } } return MaxLen ; } public static void main ( String [ ] args ) { int [ ] A = { 2 , 8 , 3 , 10 , 6 , 7 , 4 , 9 } ; int N = A . length ; int K = 60 ; System . out . println ( find_longest_subarray ( A , N , K ) ) ; } }
Length of smallest meeting that can be attended | Java program to implement the above approach ; Function to find the minimum time to attend exactly one meeting ; Stores minimum time to attend exactly one meeting ; Sort entrance [ ] array ; Sort exit [ ] time ; Traverse meeting [ ] [ ] ; Stores start time of current meeting ; Stores end time of current meeting ; Find just greater value of u in entrance [ ] ; Find just greater or equal value of u in entrance [ ] ; System . out . println ( exit . size ( ) ) ; Stores enter time to attend the current meeting ; Stores exist time after attending the meeting ; Update start lies in range [ 0 , m - 1 ] and end lies in the range [ 0 , p - 1 ] ; Update ans ; Return answer ; vec . clear ( ) ; ; Driver Code ; Stores interval of meeting ; Stores entrance timings ; Stores exit timings ; Stores total count of meetings ; Stores total entrance timings ; Stores total exit timings ; Minimum time
import java . util . * ; class GFG { static Vector < Integer > exit = new Vector < > ( ) ; static int minTime ( int meeting [ ] [ ] , int n , int [ ] entrance , int m , int p ) { int ans = Integer . MAX_VALUE ; Arrays . sort ( entrance ) ; Collections . sort ( exit ) ; for ( int i = 0 ; i < n ; i ++ ) { int u = meeting [ i ] [ 0 ] ; int v = meeting [ i ] [ 1 ] ; int it1 = upper_bound ( entrance , 0 , entrance . length , u ) ; int it2 = lowerBound ( exit , 0 , exit . size ( ) , v ) ; int start = it1 - 1 ; int end = it2 ; if ( start >= 0 && start < m && end >= 0 && end < p ) ans = Math . min ( ans , exit . get ( end ) - entrance [ start ] ) ; } return ans >= Integer . MAX_VALUE ? - 1 : ans ; } static int upper_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } static int lowerBound ( Vector < Integer > vec , int low , int high , int element ) { int [ ] array = new int [ vec . size ( ) ] ; int k = 0 ; for ( Integer val : vec ) { array [ k ] = val ; k ++ ; } while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > array [ middle ] ) { low = middle + 1 ; } else { high = middle ; } } return low ; } public static void main ( String [ ] args ) { int meeting [ ] [ ] = { { 15 , 19 } , { 5 , 10 } , { 7 , 25 } } ; int [ ] entrance = { 4 , 13 , 25 , 2 } ; exit . add ( 10 ) ; exit . add ( 25 ) ; int n = meeting . length ; int m = entrance . length ; int p = exit . size ( ) ; System . out . print ( minTime ( meeting , n , entrance , m , p ) + "NEW_LINE"); } }