codeparrot/xlcost-text-to-code huggingface dataset:
Minimize difference between maximum and minimum array elements by removing a K | Java program for the above approach ; Function to minimize difference between maximum and minimum array elements by removing a K - length subarray ; Size of array ; Stores the maximum and minimum in the suffix subarray [ i . . N - 1 ] ; Traverse the array ; Stores the maximum and minimum in the prefix subarray [ 0 . . i - 1 ] ; Store the minimum difference ; Traverse the array ; If the suffix doesn 't exceed  the end of the array ; Store the maximum element in array after removing subarray of size K ; Stores the maximum element in array after removing subarray of size K ; Update minimum difference ; Updating the maxPrefix and minPrefix with current element ; Print the minimum difference ; Driver Code
import java . util . * ; class GFG { static void minimiseDifference ( int [ ] arr , int K ) { int N = arr . length ; int [ ] maxSuffix = new int [ N + 1 ] ; int [ ] minSuffix = new int [ N + 1 ] ; maxSuffix [ N ] = - 1000000000 ; minSuffix [ N ] = 1000000000 ; maxSuffix [ N - 1 ] = arr [ N - 1 ] ; minSuffix [ N - 1 ] = arr [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; -- i ) { maxSuffix [ i ] = Math . max ( maxSuffix [ i + 1 ] , arr [ i ] ) ; minSuffix [ i ] = Math . min ( minSuffix [ i + 1 ] , arr [ i ] ) ; } int maxPrefix = arr [ 0 ] ; int minPrefix = arr [ 0 ] ; int minDiff = maxSuffix [ K ] - minSuffix [ K ] ; for ( int i = 1 ; i < N ; ++ i ) { if ( i + K <= N ) { int maximum = Math . max ( maxSuffix [ i + K ] , maxPrefix ) ; int minimum = Math . min ( minSuffix [ i + K ] , minPrefix ) ; minDiff = Math . min ( minDiff , maximum - minimum ) ; } maxPrefix = Math . max ( maxPrefix , arr [ i ] ) ; minPrefix = Math . min ( minPrefix , arr [ i ] ) ; } System . out . print ( minDiff ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 4 , 5 , 8 , 9 , 1 , 2 } ; int K = 2 ; minimiseDifference ( arr , K ) ; } }
Minimize segments required to be removed such that at least one segment intersects with all remaining segments | Java program for the above approach ; Function to find the minimum number of segments required to be deleted ; Stores the start and end points ; Traverse segments and fill the startPoints and endPoints ; Sort the startPoints ; Sort the startPoints ; Store the minimum number of deletions required and initialize with ( N - 1 ) ; Traverse the array segments [ ] ; Store the starting point ; Store the ending point ; Store the number of segments satisfying the first condition of non - intersection ; Store the number of segments satisfying the second condition of non - intersection ; Update answer ; Print the answer ; Driver Code ; Function Call
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static class Pair { int first ; int second ; Pair ( int first , int second ) { this . first = first ; this . second = second ; } } public static int lower_bound ( int arr [ ] , int key ) { int l = - 1 , r = arr . length ; while ( l + 1 < r ) { int m = ( l + r ) >>> 1 ; if ( arr [ m ] >= key ) r = m ; else l = m ; } return r ; } public static int upper_bound ( int arr [ ] , int key ) { int l = - 1 , r = arr . length ; while ( l + 1 < r ) { int m = ( l + r ) >>> 1 ; if ( arr [ m ] <= key ) l = m ; else r = m ; } return l + 1 ; } static void minSegments ( Pair segments [ ] , int n ) { int startPoints [ ] = new int [ n ] ; int endPoints [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { startPoints [ i ] = segments [ i ] . first ; endPoints [ i ] = segments [ i ] . second ; } Arrays . sort ( startPoints ) ; Arrays . sort ( endPoints ) ; int ans = n - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int f = segments [ i ] . first ; int s = segments [ i ] . second ; int leftDelete = lower_bound ( endPoints , f ) ; int rightDelete = Math . max ( 0 , n - ( int ) ( upper_bound ( startPoints , s ) ) ) ; ans = Math . min ( ans , leftDelete + rightDelete ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { Pair arr [ ] = { new Pair ( 1 , 2 ) , new Pair ( 5 , 6 ) , new Pair ( 6 , 7 ) , new Pair ( 7 , 10 ) , new Pair ( 8 , 9 ) } ; int N = arr . length ; minSegments ( arr , N ) ; } }
Longest substring where all the characters appear at least K times | Set 3 | Java program for the above approach ; Function to find the length of the longest subString ; Store the required answer ; Create a frequency map of the characters of the String ; Store the length of the String ; Traverse the String , s ; Increment the frequency of the current character by 1 ; Stores count of unique characters ; Find the number of unique characters in String ; Iterate in range [ 1 , unique ] ; Initialize frequency of all characters as 0 ; Stores the start and the end of the window ; Stores the current number of unique characters and characters occuring atleast K times ; New unique character ; New character which occurs atleast k times ; Expand window by incrementing end by 1 ; Check if this character is present atleast k times ; Check if this character is unique ; Shrink the window by incrementing start by 1 ; If there are curr_unique characters and each character is atleast k times ; Update the overall maximum length ; Print the answer ; Driver Code
import java . util . * ; class GFG { static void longestSubString ( char [ ] s , int k ) { int ans = 0 ; int freq [ ] = new int [ 26 ] ; int n = s . length ; for ( int i = 0 ; i < n ; i ++ ) freq [ s [ i ] - ' a ' ] ++ ; int unique = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) if ( freq [ i ] != 0 ) unique ++ ; for ( int curr_unique = 1 ; curr_unique <= unique ; curr_unique ++ ) { Arrays . fill ( freq , 0 ) ; int start = 0 , end = 0 ; int cnt = 0 , count_k = 0 ; while ( end < n ) { if ( cnt <= curr_unique ) { int ind = s [ end ] - ' a ' ; if ( freq [ ind ] == 0 ) cnt ++ ; freq [ ind ] ++ ; if ( freq [ ind ] == k ) count_k ++ ; end ++ ; } else { int ind = s [ start ] - ' a ' ; if ( freq [ ind ] == k ) count_k -- ; freq [ ind ] -- ; if ( freq [ ind ] == 0 ) cnt -- ; start ++ ; } if ( cnt == curr_unique && count_k == curr_unique ) ans = Math . max ( ans , end - start ) ; } } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { String S = " aabbba " ; int K = 3 ; longestSubString ( S . toCharArray ( ) , K ) ; } }
Print matrix elements using DFS traversal | Java program to implement the above approach ; Direction vectors ; Function to check if current position is valid or not ; Check if the cell is out of bounds ; Check if the cell is visited or not ; Utility function to print matrix elements using DFS Traversal ; Mark the current cell visited ; Print the element at the cell ; Traverse all four adjacent cells of the current element ; Check if x and y is valid index or not ; Function to print the matrix elements ; Initialize a visiting matrix ; Function call to print matrix elements by DFS traversal ; Driver Code ; Given matrix ; Row of the matrix ; Column of the matrix
import java . util . * ; class GFG { static int dRow [ ] = { - 1 , 0 , 1 , 0 } ; static int dCol [ ] = { 0 , 1 , 0 , - 1 } ; static boolean isValid ( boolean [ ] [ ] vis , int row , int col , int COL , int ROW ) { if ( row < 0 col < 0 col > COL - 1 row > ROW - 1 ) return false ; if ( vis [ row ] [ col ] == true ) return false ; return true ; } static void DFSUtil ( int row , int col , int [ ] [ ] grid , boolean [ ] [ ] vis , int M , int N ) { vis [ row ] [ col ] = true ; System . out . print ( grid [ row ] [ col ] + " ▁ " ) ; for ( int i = 0 ; i < 4 ; i ++ ) { int x = row + dRow [ i ] ; int y = col + dCol [ i ] ; if ( isValid ( vis , x , y , M , N ) ) DFSUtil ( x , y , grid , vis , M , N ) ; } } static void DFS ( int row , int col , int [ ] [ ] grid , int M , int N ) { boolean [ ] [ ] vis = new boolean [ M + 1 ] [ N + 1 ] ; for ( int i = 0 ; i < M + 1 ; i ++ ) { for ( int j = 0 ; j < N + 1 ; j ++ ) { vis [ i ] [ j ] = false ; } } DFSUtil ( 0 , 0 , grid , vis , M , N ) ; } public static void main ( String args [ ] ) { int [ ] [ ] grid = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int M = grid . length ; int N = grid [ 0 ] . length ; DFS ( 0 , 0 , grid , M , N ) ; } }
Print matrix elements using DFS traversal | Java program for the above approach ; Direction vectors ; Function to check if curruent position is valid or not ; Check if the cell is out of bounds ; Check if the cell is visited ; Function to print the matrix elements ; Stores if a position in the matrix been visited or not ; Initialize stack to implement DFS ; Push the first position of grid [ ] [ ] in the stack ; Mark the cell ( 0 , 0 ) visited ; Stores top element of stack ; Delete the top ( ) element of stack ; Print element at the cell ; Traverse in all four adjacent sides of current positions ; Check if x and y is valid position and then push the position of current cell in the stack ; Push the current cell ; Mark current cell visited ; Driver Code ; Given matrix ; Row of the matrix ; Column of the matrix
import java . util . * ; public class Main { public static class Pair { int Item1 , Item2 ; public Pair ( int Item1 , int Item2 ) { this . Item1 = Item1 ; this . Item2 = Item2 ; } } static int [ ] dRow = { - 1 , 0 , 1 , 0 } ; static int [ ] dCol = { 0 , 1 , 0 , - 1 } ; static Vector < Vector < Boolean > > vis ; static boolean isValid ( int row , int col , int COL , int ROW ) { if ( row < 0 col < 0 col > COL - 1 row > ROW - 1 ) return false ; if ( vis . get ( row ) . get ( col ) == true ) return false ; return true ; } static void DFS_iterative ( int [ ] [ ] grid , int M , int N ) { vis = new Vector < Vector < Boolean > > ( ) ; for ( int i = 0 ; i < M + 5 ; i ++ ) { vis . add ( new Vector < Boolean > ( ) ) ; for ( int j = 0 ; j < N + 5 ; j ++ ) { vis . get ( i ) . add ( false ) ; } } Vector < Pair > st = new Vector < Pair > ( ) ; st . add ( new Pair ( 0 , 0 ) ) ; vis . get ( 0 ) . set ( 0 , true ) ; while ( st . size ( ) > 0 ) { Pair p = st . get ( st . size ( ) - 1 ) ; st . remove ( st . size ( ) - 1 ) ; int row = p . Item1 ; int col = p . Item2 ; System . out . print ( grid [ row ] [ col ] + " ▁ " ) ; for ( int i = 0 ; i < 4 ; i ++ ) { int x = row + dRow [ i ] ; int y = col + dCol [ i ] ; if ( isValid ( x , y , M , N ) ) { st . add ( new Pair ( x , y ) ) ; vis . get ( x ) . set ( y , true ) ; } } } } public static void main ( String [ ] args ) { int [ ] [ ] grid = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int M = 4 ; int N = 4 ; DFS_iterative ( grid , M , N ) ; } }
Segregate 1 s and 0 s in separate halves of a Binary String | Java program for the above approach ; Function to count the minimum number of operations required to segregate all 1 s and 0 s in a binary string ; Stores the count of unequal pair of adjacent characters ; If an unequal pair of adjacent characters occurs ; For odd count ; For even count ; Driver Code ; Given string ; Length of the string ; Prints the minimum count of operations required
import java . io . * ; import java . util . * ; class GFG { static void minOps ( String s , int N ) { int ans = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( s . charAt ( i ) != s . charAt ( i - 1 ) ) { ans ++ ; } } if ( ans % 2 == 1 ) { System . out . print ( ( ans - 1 ) / 2 ) ; return ; } System . out . print ( ans / 2 ) ; } public static void main ( String [ ] args ) { String str = "01011100" ; int N = str . length ( ) ; minOps ( str , N ) ; } }
Count prime triplets upto N having sum of first two elements equal to the third number | Java program for the above approach ; Boolean array to mark prime numbers ; To count the prime triplets having the sum of the first two numbers equal to the third element ; Function to count prime triplets having sum of the first two elements equal to the third element ; Sieve of Eratosthenes ; Checks for the prime numbers having difference equal to2 ; Update count of triplets ; Print the answer ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int MAX = 1000001 ; static boolean [ ] prime = new boolean [ MAX ] ; static int [ ] cntTriplet = new int [ MAX ] ; static void primeTriplet ( int N ) { Arrays . fill ( prime , true ) ; prime [ 0 ] = prime [ 1 ] = false ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( prime [ i ] ) { for ( int j = i * i ; j <= N ; j += i ) { prime [ j ] = false ; } } } for ( int i = 3 ; i <= N ; i ++ ) { if ( prime [ i ] && prime [ i - 2 ] ) { cntTriplet [ i ] = cntTriplet [ i - 1 ] + 1 ; } else { cntTriplet [ i ] = cntTriplet [ i - 1 ] ; } } System . out . println ( cntTriplet [ N ] ) ; } public static void main ( String [ ] args ) { int N = 7 ; primeTriplet ( N ) ; } }
Count indices where the maximum in the prefix array is less than that in the suffix array | Java program for the above approach ; Function to print the count of indices in which the maximum in prefix arrays is less than that in the suffix array ; If size of array is 1 ; pre [ ] : Prefix array suf [ ] : Suffix array ; Stores the required count ; Find the maximum in prefix array ; Find the maximum in suffix array ; Traverse the array ; If maximum in prefix array is less than maximum in the suffix array ; Print the answer ; Driver code ; Function Call
public class gfg { static void count ( int a [ ] , int n ) { if ( n == 1 ) { System . out . print ( 0 ) ; return ; } int [ ] pre = new int [ n - 1 ] ; int [ ] suf = new int [ n - 1 ] ; int max = a [ 0 ] ; int ans = 0 , i ; pre [ 0 ] = a [ 0 ] ; for ( i = 1 ; i < n - 1 ; i ++ ) { if ( a [ i ] > max ) max = a [ i ] ; pre [ i ] = max ; } max = a [ n - 1 ] ; suf [ n - 2 ] = a [ n - 1 ] ; for ( i = n - 2 ; i >= 1 ; i -- ) { if ( a [ i ] > max ) max = a [ i ] ; suf [ i - 1 ] = max ; } for ( i = 0 ; i < n - 1 ; i ++ ) { if ( pre [ i ] < suf [ i ] ) ans ++ ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 8 , 1 , 4 } ; int N = arr . length ; count ( arr , N ) ; } }
Length of the longest subsequence such that XOR of adjacent elements is equal to K | Java program for above approach ; Function to find maximum length of subsequence ; Stores maximum length of subsequence ; HashMap to store the longest length of subsequence ending at an integer , say X ; Stores the maximum length of subsequence ending at index i ; Base case ; Iterate over the range [ 1 , N - 1 ] ; Retrieve the longest length of subsequence ending at integer a [ ] ^ K ; If dpj is not NULL ; Update dp [ i ] ; Update ans ; Update the maximum length of subsequence ending at element is a [ i ] in HashMap ; Return the ans if ans >= 2. Otherwise , return 0 ; Driver Code ; Input ; Print the length of the longest subsequence
import java . io . * ; import java . util . * ; class GFG { public static int xorSubsequence ( int a [ ] , int n , int k ) { int ans = 0 ; HashMap < Integer , Integer > map = new HashMap < > ( ) ; int dp [ ] = new int [ n ] ; map . put ( a [ 0 ] , 1 ) ; dp [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { Integer dpj = map . get ( a [ i ] ^ k ) ; if ( dpj != null ) dp [ i ] = Math . max ( dp [ i ] , dpj + 1 ) ; ans = Math . max ( ans , dp [ i ] ) ; map . put ( a [ i ] , Math . max ( map . getOrDefault ( a [ i ] , 1 ) , dp [ i ] ) ) ; } return ans >= 2 ? ans : 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 4 , 3 , 5 } ; int N = arr . length ; int K = 1 ; System . out . println ( xorSubsequence ( arr , N , K ) ) ; } }
Sum of subtree depths for every node of a given Binary Tree | Java program for the above approach ; Binary tree node ; Function to allocate a new node with the given data and null in its left and right pointers ; DFS function to calculate the sum of depths of all subtrees depth sum ; Store total number of node in its subtree and total sum of depth in its subtree ; Check if left is not null ; Call recursively the DFS function for left child ; Increment the sum of depths by ptemp . first + p . temp . first ; Increment p . first by count of noded in left subtree ; Check if right is not null ; Call recursively the DFS function for right child ; Increment the sum of depths by ptemp . first + p . temp . first ; Increment p . first by count of nodes in right subtree ; Increment the result by total sum of depth in current subtree ; Return p ; Driver Code ; Given Tree ; Print the result
import java . util . * ; class GFG { static int ans ; static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static class TreeNode { int data ; TreeNode left ; TreeNode right ; } ; static TreeNode newNode ( int data ) { TreeNode Node = new TreeNode ( ) ; Node . data = data ; Node . left = null ; Node . right = null ; return ( Node ) ; } static pair sumofsubtree ( TreeNode root ) { pair p = new pair ( 1 , 0 ) ; if ( root . left != null ) { pair ptemp = sumofsubtree ( root . left ) ; p . second += ptemp . first + ptemp . second ; p . first += ptemp . first ; } if ( root . right != null ) { pair ptemp = sumofsubtree ( root . right ) ; p . second += ptemp . first + ptemp . second ; p . first += ptemp . first ; } ans += p . second ; return p ; } public static void main ( String [ ] args ) { TreeNode root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . left . left . left = newNode ( 8 ) ; root . left . left . right = newNode ( 9 ) ; ans = 0 ; sumofsubtree ( root ) ; System . out . print ( ans ) ; } }
Minimum time required to fill given N slots | Java program for the above approach ; Function to return the minimum time to fill all the slots ; Stores visited slots ; Checks if a slot is visited or not ; Insert all filled slots ; Iterate until queue is not empty ; Iterate through all slots in the queue ; Front index ; If previous slot is present and not visited ; If next slot is present and not visited ; Increment the time at each level ; Print the answer ; Driver Code ; Function Call
import java . io . * ; import java . util . * ; class GFG { public static void minTime ( int arr [ ] , int N , int K ) { Queue < Integer > q = new LinkedList < > ( ) ; boolean vis [ ] = new boolean [ N + 1 ] ; int time = 0 ; for ( int i = 0 ; i < K ; i ++ ) { q . add ( arr [ i ] ) ; vis [ arr [ i ] ] = true ; } while ( q . size ( ) > 0 ) { for ( int i = 0 ; i < q . size ( ) ; i ++ ) { int curr = q . poll ( ) ; if ( curr - 1 >= 1 && ! vis [ curr - 1 ] ) { vis [ curr - 1 ] = true ; q . add ( curr - 1 ) ; } if ( curr + 1 <= N && ! vis [ curr + 1 ] ) { vis [ curr + 1 ] = true ; q . add ( curr + 1 ) ; } } time ++ ; } System . out . println ( time - 1 ) ; } public static void main ( String [ ] args ) { int N = 6 ; int arr [ ] = { 2 , 6 } ; int K = arr . length ; minTime ( arr , N , K ) ; } }
Count of integers having difference with its reverse equal to D | Java program of the above approach ; Maximum digits in N ; Utility function to find count of N such that N + D = reverse ( N ) ; If d is a multiple of 9 , no such value N found ; Divide D by 9 check reverse of number and its sum ; B [ i ] : Stores power of ( 10 , i ) ; Calculate power ( 10 , i ) ; Update B [ i ] ; Stores count of N such that N + D = reverse ( N ) ; Iterate over the range [ 1 , MAXL ] ; Update ans ; Function to find count of possible values of N such that N + D = reverse ( N ) ; Base Case ; If D is not qual to 0 ; Stores count of possible values of N such that N + D = reverse ( N ) ; Update ans ; If l is even ; Update ans ; Stores count of possible values of N such that N + D = reverse ( N ) ; Iterae over the range [ - 9 , 9 ] ; Update ans ; Driver Code ; Function call
import java . util . * ; public class Main { static final int MAXL = 17 ; static int N ; static long [ ] v ; static long findN ( int D ) { if ( D % 9 != 0 ) return 0 ; D /= 9 ; long [ ] B = new long [ MAXL ] ; B [ 0 ] = 1 ; for ( int i = 1 ; i < MAXL ; i ++ ) { B [ i ] = B [ i - 1 ] * 10 ; } long ans = 0 ; for ( int i = 1 ; i <= MAXL ; i ++ ) { N = ( i + 1 ) / 2 ; v = new long [ N ] ; for ( int j = 0 ; j < N ; j ++ ) for ( int k = j ; k < i - j ; k ++ ) v [ j ] += B [ k ] ; ans += count ( D , i , 0 , new int [ N ] ) ; } return ans ; } static long count ( long D , int l , int t , int [ ] x ) { if ( t == N ) { if ( D != 0 ) return 0 ; long ans = 1 ; for ( int i = 0 ; i < N ; i ++ ) { ans *= ( i == 0 ? 9 : 10 ) - Math . abs ( x [ i ] ) ; } if ( l % 2 == 0 ) { ans *= 10 ; } return ans ; } long ans = 0 ; for ( int m = - 9 ; m <= 9 ; m ++ ) { if ( - v [ t ] < D + v [ t ] * m && D + v [ t ] * m < v [ t ] ) { x [ t ] = m ; ans += count ( D + v [ t ] * m , l , t + 1 , x ) ; } } return ans ; } public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int D = 63 ; System . out . println ( findN ( D ) ) ; sc . close ( ) ; } }
Search for an element in a Mountain Array | Java program for the above approach ; Function to find the index of the peak element in the array ; Stores left most index in which the peak element can be found ; Stores right most index in which the peak element can be found ; Stores mid of left and right ; If element at mid is less than element at ( mid + 1 ) ; Update left ; Update right ; Function to perform binary search in an a subarray if elements of the subarray are in an ascending order ; Stores mid of left and right ; If X found at mid ; If X is greater than mid ; Update left ; Update right ; Function to perform binary search in an a subarray if elements of the subarray are in an ascending order ; Stores mid of left and right ; If X found at mid ; Update right ; Update left ; Function to find the smallest index of X ; Stores index of peak element in array ; Stores index of X in the array ; If X greater than or equal to first element of array and less than the peak element ; Update res ; If element not found on left side of peak element ; Update res ; Print res ; Driver Code ; Given X ; Given array ; Function Call
import java . io . * ; import java . util . * ; class GFG { public static int findPeak ( ArrayList < Integer > arr ) { int left = 0 ; int right = arr . size ( ) - 1 ; while ( left < right ) { int mid = left + ( right - left ) / 2 ; if ( arr . get ( mid ) < arr . get ( mid + 1 ) ) { left = mid + 1 ; } else { right = mid ; } } return left ; } static int BS ( int X , int left , int right , ArrayList < Integer > arr ) { while ( left <= right ) { int mid = left + ( right - left ) / 2 ; if ( arr . get ( mid ) == X ) { return mid ; } else if ( X > arr . get ( mid ) ) { left = mid + 1 ; } else { right = mid - 1 ; } } return - 1 ; } static int reverseBS ( int X , int left , int right , ArrayList < Integer > arr ) { while ( left <= right ) { int mid = left + ( right - left ) / 2 ; if ( arr . get ( mid ) == X ) { return mid ; } else if ( X > arr . get ( mid ) ) { right = mid - 1 ; } else { left = mid + 1 ; } } return - 1 ; } static void findInMA ( int X , ArrayList < Integer > mountainArr ) { int peakIndex = findPeak ( mountainArr ) ; int res = - 1 ; if ( X >= mountainArr . get ( 0 ) && X <= mountainArr . get ( peakIndex ) ) { res = BS ( X , 0 , peakIndex , mountainArr ) ; } if ( res == - 1 ) { res = reverseBS ( X , peakIndex + 1 , mountainArr . size ( ) - 1 , mountainArr ) ; } System . out . println ( res ) ; } public static void main ( String [ ] args ) { int X = 3 ; ArrayList < Integer > list = new ArrayList < > ( Arrays . asList ( 1 , 2 , 3 , 4 , 5 , 3 , 1 ) ) ; findInMA ( X , list ) ; } }