codeparrot/xlcost-text-to-code huggingface dataset:
Reverse tree path | Java program to Reverse Tree path ; A Binary Tree Node ; class for int values ; ' data ' is input . We need to reverse path from root to data . ' level ' is current level . ' temp ' that stores path nodes . ' nextpos ' used to pick next item for reversing . ; return null if root null ; Final condition if the node is found then ; store the value in it 's level ; change the root value with the current next element of the map ; increment in k for the next element ; store the data in perticular level ; We go to right only when left does not contain given data . This way we make sure that correct path node is stored in temp [ ] ; If current node is part of the path , then do reversing . ; return null if not element found ; Reverse Tree path ; store per level data ; it is for replacing the data ; reverse tree path ; INORDER ; Utility function to create a new tree node ; Driver program to test above functions ; Let us create binary tree shown in above diagram ; 7 / \ 6 5 / \ / \ 4 3 2 1 ; Reverse Tree Path ; Traverse inorder
import java . util . * ; class solution { static class Node { int data ; Node left , right ; } ; static class INT { int data ; } ; static Node reverseTreePathUtil ( Node root , int data , Map < Integer , Integer > temp , int level , INT nextpos ) { if ( root == null ) return null ; if ( data == root . data ) { temp . put ( level , root . data ) ; root . data = temp . get ( nextpos . data ) ; nextpos . data ++ ; return root ; } temp . put ( level , root . data ) ; Node left , right = null ; left = reverseTreePathUtil ( root . left , data , temp , level + 1 , nextpos ) ; if ( left == null ) right = reverseTreePathUtil ( root . right , data , temp , level + 1 , nextpos ) ; if ( left != null right != null ) { root . data = temp . get ( nextpos . data ) ; nextpos . data ++ ; return ( left != null ? left : right ) ; } return null ; } static void reverseTreePath ( Node root , int data ) { Map < Integer , Integer > temp = new HashMap < Integer , Integer > ( ) ; INT nextpos = new INT ( ) ; nextpos . data = 0 ; reverseTreePathUtil ( root , data , temp , 0 , nextpos ) ; } static void inorder ( Node root ) { if ( root != null ) { inorder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; inorder ( root . right ) ; } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public static void main ( String args [ ] ) { Node root = newNode ( 7 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 2 ) ; root . right . right = newNode ( 1 ) ; int data = 4 ; reverseTreePath ( root , data ) ; inorder ( root ) ; } }
Longest Subarray with first element greater than or equal to Last element | Java program to find length of the longest array with first element smaller than or equal to last element . ; Search function for searching the first element of the subarray which is greater or equal to the last element ( num ) ; Returns length of the longest array with first element smaller than the last element . ; Search space for the potential first elements . ; It will store the Indexes of the elements of search space in the original array . ; Initially the search space is empty . ; We will add an ith element in the search space if the search space is empty or if the ith element is greater than the last element of the search space . ; we will search for the index first element in the search space and we will use it find the index of it in the original array . ; Update the answer if the length of the subarray is greater than the previously calculated lengths . ; Driver Code
class GFG { static int binarySearch ( int [ ] searchSpace , int s , int e , int num ) { int ans = 0 ; while ( s <= e ) { int mid = ( s + e ) / 2 ; if ( searchSpace [ mid ] >= num ) { ans = mid ; e = mid - 1 ; } else s = mid + 1 ; } return ans ; } static int longestSubArr ( int [ ] arr , int n ) { int [ ] searchSpace = new int [ n ] ; int [ ] index = new int [ n ] ; int j = 0 ; int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( j == 0 searchSpace [ j - 1 ] < arr [ i ] ) { searchSpace [ j ] = arr [ i ] ; index [ j ] = i ; j ++ ; } int idx = binarySearch ( searchSpace , 0 , j - 1 , arr [ i ] ) ; ans = Math . max ( ans , i - index [ idx ] + 1 ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { - 5 , - 1 , 7 , 5 , 1 , - 2 } ; int n = arr . length ; System . out . println ( longestSubArr ( arr , n ) ) ; } }
Print all pairs with given sum | C ++ code to implement the above approach ; Driver code
import java . util . Arrays ; public class SumOfPairs { public void pairedElements ( int arr [ ] , int sum ) { int low = 0 ; int high = arr . length - 1 ; while ( low < high ) { if ( arr [ low ] + arr [ high ] == sum ) { System . out . println ( " The ▁ pair ▁ is ▁ : ▁ ( " + arr [ low ] + " , ▁ " + arr [ high ] + " ) " ) ; } if ( arr [ low ] + arr [ high ] > sum ) { high -- ; } else { low ++ ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , - 2 , 6 , 8 , 9 , 11 } ; Arrays . sort ( arr ) ; SumOfPairs sp = new SumOfPairs ( ) ; sp . pairedElements ( arr , 6 ) ; } }
Check if a string is suffix of another | Java program to find if a string is suffix of another ; Driver code ; Test case - sensitive implementation of endsWith function
class GFG { static boolean isSuffix ( String s1 , String s2 ) { int n1 = s1 . length ( ) , n2 = s2 . length ( ) ; if ( n1 > n2 ) return false ; for ( int i = 0 ; i < n1 ; i ++ ) if ( s1 . charAt ( n1 - i - 1 ) != s2 . charAt ( n2 - i - 1 ) ) return false ; return true ; } public static void main ( String [ ] args ) { String s1 = " geeks " , s2 = " geeksforgeeks " ; boolean result = isSuffix ( s1 , s2 ) ; if ( result ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Check if all occurrences of a character appear together | Java program to find if all occurrences of a character appear together in a string . ; function to find if all occurrences of a character appear together in a string . ; To indicate if one or more occurrences of ' c ' are seen or not . ; Traverse given string ; If current character is same as c , we first check if c is already seen . ; If this is very first appearance of c , we traverse all consecutive occurrences . ; To indicate that character is seen once . ; Driver Code
import java . io . * ; class GFG { static boolean checkIfAllTogether ( String s , char c ) { boolean oneSeen = false ; int i = 0 , n = s . length ( ) ; while ( i < n ) { if ( s . charAt ( i ) == c ) { if ( oneSeen == true ) return false ; while ( i < n && s . charAt ( i ) == c ) i ++ ; oneSeen = true ; } else i ++ ; } return true ; } public static void main ( String [ ] args ) { String s = "110029" ; if ( checkIfAllTogether ( s , '1' ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Front and Back Search in unsorted array | Java program to implement front and back search ; Start searching from both ends ; Keep searching while two indexes do not cross . ; driver code
class GFG { static boolean search ( int arr [ ] , int n , int x ) { int front = 0 , back = n - 1 ; while ( front <= back ) { if ( arr [ front ] == x arr [ back ] == x ) return true ; front ++ ; back -- ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 20 , 80 , 30 , 60 , 50 , 110 , 100 , 130 , 170 } ; int x = 130 ; int n = arr . length ; if ( search ( arr , n , x ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Even | Java program to find max ( X , Y ) / min ( X , Y ) after P turns ; Driver code ; 1 st test case ; 2 nd test case
import java . util . * ; class Even_odd { public static int findValue ( int X , int Y , int P ) { if ( P % 2 == 0 ) return ( Math . max ( X , Y ) / Math . min ( X , Y ) ) ; else return ( Math . max ( 2 * X , Y ) / Math . min ( 2 * X , Y ) ) ; } public static void main ( String [ ] args ) { int X = 1 , Y = 2 , P = 1 ; System . out . println ( findValue ( X , Y , P ) ) ; X = 3 ; Y = 7 ; P = 2 ; System . out . print ( findValue ( X , Y , P ) ) ; } }
The painter 's partition problem | A DP based Java program for painter 's partition problem ; function to calculate sum between two indices in array ; bottom up tabular dp ; initialize table ; base cases k = 1 ; n = 1 ; 2 to k partitions for ( int i = 2 ; i <= k ; i ++ ) { 2 to n boards ; track minimum ; i - 1 th separator before position arr [ p = 1. . j ] ; required ; Driver code ; Calculate size of array .
import java . util . * ; import java . io . * ; class GFG { static int sum ( int arr [ ] , int from , int to ) { int total = 0 ; for ( int i = from ; i <= to ; i ++ ) total += arr [ i ] ; return total ; } static int findMax ( int arr [ ] , int n , int k ) { int dp [ ] [ ] = new int [ k + 1 ] [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) dp [ 1 ] [ i ] = sum ( arr , 0 , i - 1 ) ; for ( int i = 1 ; i <= k ; i ++ ) dp [ i ] [ 1 ] = arr [ 0 ] ; for ( int j = 2 ; j <= n ; j ++ ) { int best = Integer . MAX_VALUE ; for ( int p = 1 ; p <= j ; p ++ ) best = Math . min ( best , Math . max ( dp [ i - 1 ] [ p ] , sum ( arr , p , j - 1 ) ) ) ; dp [ i ] [ j ] = best ; } } return dp [ k ] [ n ] ; } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 20 , 60 , 50 , 30 , 40 } ; int n = arr . length ; int k = 3 ; System . out . println ( findMax ( arr , n , k ) ) ; } }
Counting cross lines in an array | Java program to count cross line in array ; function return count of cross line in an array ; Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position ; increment cross line by one ; Driver Code
class GFG { static int countCrossLine ( int arr [ ] , int n ) { int count_crossline = 0 ; int i , key , j ; for ( i = 1 ; i < n ; i ++ ) { key = arr [ i ] ; j = i - 1 ; while ( j >= 0 && arr [ j ] > key ) { arr [ j + 1 ] = arr [ j ] ; j = j - 1 ; count_crossline ++ ; } arr [ j + 1 ] = key ; } return count_crossline ; } public static void main ( String args [ ] ) { int arr [ ] = new int [ ] { 4 , 3 , 1 , 2 } ; int n = arr . length ; System . out . print ( countCrossLine ( arr , n ) ) ; } }
Recursive Programs to find Minimum and Maximum elements of array | Recursive Java program to find maximum ; function to return maximum element using recursion ; if size = 0 means whole array has been traversed ; Driver code ; Function calling
import java . util . * ; class GFG { public static int findMaxRec ( int A [ ] , int n ) { if ( n == 1 ) return A [ 0 ] ; return Math . max ( A [ n - 1 ] , findMaxRec ( A , n - 1 ) ) ; } public static void main ( String args [ ] ) { int A [ ] = { 1 , 4 , 45 , 6 , - 50 , 10 , 2 } ; int n = A . length ; System . out . println ( findMaxRec ( A , n ) ) ; } }
Program to remove vowels from a String | Java program to remove vowels from a String ; Driver Code
import java . util . Arrays ; import java . util . List ; class GFG { static String remVowel ( String str ) { return str . replaceAll ( " [ aeiouAEIOU ] " , " " ) ; } public static void main ( String [ ] args ) { String str = " GeeeksforGeeks ▁ - ▁ A ▁ Computer ▁ Science ▁ Portal ▁ for ▁ Geeks " ; System . out . println ( remVowel ( str ) ) ; } }
Maximum money that can be collected by both the players in a game of removal of coins | Java program for the above approach ; Function to find the maximum score obtained by the players A and B ; Sort the array in descending order ; Stores the maximum amount of money obtained by A ; Stores the maximum amount of money obtained by B ; If the value of N is 1 ; Update the amountA ; Print the amount of money obtained by both players ; Update the amountA ; Update the amountB ; Traverse the array arr [ ] ; If i is an odd number ; Update the amountB ; Update the amountA ; Print the amount of money obtained by both the players ; Driver code
import java . util . * ; class GFG { static void findAmountPlayers ( int [ ] arr , int N ) { Arrays . sort ( arr ) ; reverse ( arr , N ) ; int amountA = 0 ; int amountB = 0 ; if ( N == 1 ) { amountA += arr [ 0 ] ; System . out . println ( " ( A ▁ : ▁ " + amountA + " ) , ▁ " + " ( B ▁ : ▁ " + amountB + " ) " ) ; return ; } amountA = arr [ 0 ] ; amountB = arr [ 1 ] ; for ( int i = 2 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { amountB += arr [ i ] ; } else { amountA += arr [ i ] ; } } System . out . println ( " ( A ▁ : ▁ " + amountA + " ) " ) ; System . out . println ( " ( B ▁ : ▁ " + amountB + " ) " ) ; } static void reverse ( int a [ ] , int n ) { int [ ] b = new int [ n ] ; int j = n ; for ( int i = 0 ; i < n ; i ++ ) { b [ j - 1 ] = a [ i ] ; j = j - 1 ; } } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 } ; int N = arr . length ; findAmountPlayers ( arr , N ) ; } }
Minimum number of swaps required to minimize sum of absolute differences between adjacent array elements | Java program for the above approach ; Pair class ; Function to find the minimum number of swaps required to sort the array in increasing order ; Stores the array elements with its index ; Sort the array in the increasing order ; Keeps the track of visited elements ; Stores the count of swaps required ; Traverse array elements ; If the element is already swapped or at correct position ; Find out the number of nodes in this cycle ; Update the value of j ; Move to the next element ; Increment cycle_size ; Update the ans by adding current cycle ; Function to find the minimum number of swaps required to sort the array in decreasing order ; Stores the array elements with its index ; Sort the array in the descending order ; Keeps track of visited elements ; Stores the count of resultant swap required ; Traverse array elements ; If the element is already swapped or at correct position ; Find out the number of node in this cycle ; Update the value of j ; Move to the next element ; Increment the cycle_size ; Update the ans by adding current cycle size ; Function to find minimum number of swaps required to minimize the sum of absolute difference of adjacent elements ; Sort in ascending order ; Sort in descending order ; Return the minimum value ; Driver code
import java . lang . * ; import java . util . * ; class pair { int first , second ; pair ( int first , int second ) { this . first = first ; this . second = second ; } } class GFG { static int minSwapsAsc ( int [ ] arr , int n ) { pair [ ] arrPos = new pair [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arrPos [ i ] = new pair ( arr [ i ] , i ) ; } Arrays . sort ( arrPos , ( a , b ) -> a . first - b . first ) ; boolean [ ] vis = new boolean [ n ] ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( vis [ i ] arrPos [ i ] . second == i ) continue ; int cycle_size = 0 ; int j = i ; while ( ! vis [ j ] ) { vis [ j ] = true ; j = arrPos [ j ] . second ; cycle_size ++ ; } if ( cycle_size > 0 ) { ans += ( cycle_size - 1 ) ; } } return ans ; } static int minSwapsDes ( int [ ] arr , int n ) { pair [ ] arrPos = new pair [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arrPos [ i ] = new pair ( arr [ i ] , i ) ; } Arrays . sort ( arrPos , ( a , b ) -> b . first - a . first ) ; boolean [ ] vis = new boolean [ n ] ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( vis [ i ] arrPos [ i ] . second == i ) continue ; int cycle_size = 0 ; int j = i ; while ( ! vis [ j ] ) { vis [ j ] = true ; j = arrPos [ j ] . second ; cycle_size ++ ; } if ( cycle_size > 0 ) { ans += ( cycle_size - 1 ) ; } } return ans ; } static int minimumSwaps ( int [ ] arr ) { int S1 = minSwapsAsc ( arr , arr . length ) ; int S2 = minSwapsDes ( arr , arr . length ) ; return Math . min ( S1 , S2 ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 4 , 2 , 5 , 1 } ; System . out . println ( minimumSwaps ( arr ) ) ; } }
Sort an array of 0 s , 1 s , 2 s and 3 s | Java program for the above approach ; Function to sort the array having array element only 0 , 1 , 2 , and 3 ; Iterate until mid <= j ; If arr [ mid ] is 0 ; Swap integers at indices i and mid ; Increment i ; Increment mid ; Otherwise if the value of arr [ mid ] is 3 ; Swap arr [ mid ] and arr [ j ] ; Decrement j ; Otherwise if the value of arr [ mid ] is either 1 or 2 ; Increment the value of mid ; Iterate until i <= j ; If arr [ i ] the value of is 2 ; Swap arr [ i ] and arr [ j ] ; Decrement j ; Otherwise , increment i ; Print the sorted array arr [ ] ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void sortArray ( int [ ] arr , int N ) { int i = 0 , j = N - 1 , mid = 0 ; while ( mid <= j ) { if ( arr [ mid ] == 0 ) { int temp = arr [ i ] ; arr [ i ] = arr [ mid ] ; arr [ mid ] = temp ; i ++ ; mid ++ ; } else if ( arr [ mid ] == 3 ) { int temp = arr [ mid ] ; arr [ mid ] = arr [ j ] ; arr [ j ] = temp ; j -- ; } else if ( arr [ mid ] == 1 arr [ mid ] == 2 ) { mid ++ ; } } while ( i <= j ) { if ( arr [ i ] == 2 ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; j -- ; } else { i ++ ; } } for ( int k = 0 ; k < N ; k ++ ) { System . out . print ( arr [ k ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 , 0 , 2 , 3 , 1 , 0 } ; int N = arr . length ; sortArray ( arr , N ) ; } }
Check whether an array can be made strictly increasing by removing at most one element | Java program for the above approach ; Function to find if is it possible to make the array strictly increasing by removing at most one element ; Stores the count of numbers that are needed to be removed ; Store the index of the element that needs to be removed ; Traverse the range [ 1 , N - 1 ] ; If arr [ i - 1 ] is greater than or equal to arr [ i ] ; Increment the count by 1 ; Update index ; If count is greater than one ; If no element is removed ; If only the last or the first element is removed ; If a [ index ] is removed ; If a [ index - 1 ] is removed ; Driver Code
class GFG { public static boolean check ( int arr [ ] , int n ) { int count = 0 ; int index = - 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i - 1 ] >= arr [ i ] ) { count ++ ; index = i ; } } if ( count > 1 ) return false ; if ( count == 0 ) return true ; if ( index == n - 1 index == 1 ) return true ; if ( arr [ index - 1 ] < arr [ index + 1 ] ) return true ; if ( index - 2 >= 0 && arr [ index - 2 ] < arr [ index ] ) return true ; if ( index < 0 ) return true ; return false ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 2 , 5 , 3 , 5 } ; int N = arr . length ; if ( check ( arr , N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Modify string by rearranging vowels in alphabetical order at their respective indices | Java program for the above approach ; Function to arrange the vowels in sorted order in the string at their respective places ; Store the size of the string ; Stores vowels of string S ; Traverse the string , S and push all the vowels to string vow ; If vow is empty , then print S and return ; Convert vow to char array ; Sort vow in alphabetical order ; Traverse the string , S ; Replace S [ i ] with vow [ j ] iif S [ i ] is a vowel , and increment j by 1 ; Print the string ; Driver Code
import java . util . Arrays ; class GFG { static void sortVowels ( String S ) { int n = S . length ( ) ; String vow = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( S . charAt ( i ) == ' a ' || S . charAt ( i ) == ' e ' || S . charAt ( i ) == ' i ' || S . charAt ( i ) == ' o ' || S . charAt ( i ) == ' u ' ) { vow = vow . substring ( 0 , vow . length ( ) ) + S . charAt ( i ) ; } } if ( vow . length ( ) == 0 ) { System . out . print ( S ) ; return ; } char tempArray [ ] = vow . toCharArray ( ) ; Arrays . sort ( tempArray ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( S . charAt ( i ) == ' a ' || S . charAt ( i ) == ' e ' || S . charAt ( i ) == ' i ' || S . charAt ( i ) == ' o ' || S . charAt ( i ) == ' u ' ) { S = S . substring ( 0 , i ) + tempArray [ j ++ ] + S . substring ( i + 1 , n ) ; } } System . out . print ( S ) ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; sortVowels ( S ) ; } }
Maximum number of buckets that can be filled | Java program for the above approach ; Function to find the maximum number of buckets that can be filled with the amount of water available ; Find the total available water ; Sort the array in ascending order ; Check if bucket can be filled with available water ; Print count of buckets ; Driver code
import java . util . * ; public class GFG { static void getBuckets ( int [ ] arr , int N ) { int availableWater = N * ( N - 1 ) / 2 ; Arrays . sort ( arr ) ; int i = 0 , sum = 0 ; while ( sum <= availableWater ) { sum += arr [ i ] ; i ++ ; } System . out . println ( i - 1 ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 3 , 4 , 7 , 9 } ; int N = arr . length ; getBuckets ( arr , N ) ; } }
Minimize the number of strictly increasing subsequences in an array | Set 2 | Java program to implement the above approach ; Function to find the number of strictly increasing subsequences in an array ; Sort the array ; Stores final count of subsequences ; Traverse the array ; Stores current element ; Stores frequency of the current element ; Count frequency of the current element ; If current element frequency is greater than count ; Print the final count ; Driver Code ; Given array ; Size of the array ; Function call to find the number of strictly increasing subsequences
import java . util . * ; class GFG { static void minimumIncreasingSubsequences ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; int count = 0 ; int i = 0 ; while ( i < N ) { int x = arr [ i ] ; int freqX = 0 ; while ( i < N && arr [ i ] == x ) { freqX ++ ; i ++ ; } count = Math . max ( count , freqX ) ; } System . out . print ( count ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 1 , 2 , 1 , 4 , 3 } ; int N = arr . length ; minimumIncreasingSubsequences ( arr , N ) ; } }
Count triplets from an array which can form quadratic equations with real roots | Java program for the above approach ; Function to count the number of triplets ( a , b , c ) such that the equation ax ^ 2 + bx + c = 0 has real roots ; Sort the array in ascending order ; Stores count of triplets ( a , b , c ) such that ax ^ 2 + bx + c = 0 has real roots ; Base case ; Traverse the given array ; If values of a and c are equal to b ; Increment a ; Decrement c ; Condition for having real roots for a quadratic equation ; If b lies in between a and c ; Update count ; Update count ; Increment a ; Decrement c ; For each pair two values are possible of a and c ; Driver Code ; Function Call
import java . util . * ; class GFG { static int getCount ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; int count = 0 ; if ( N < 3 ) return 0 ; for ( int b = 0 ; b < N ; b ++ ) { int a = 0 , c = N - 1 ; int d = arr [ b ] * arr [ b ] / 4 ; while ( a < c ) { if ( a == b ) { a ++ ; continue ; } if ( c == b ) { c -- ; continue ; } if ( arr [ a ] * arr <= d ) { if ( a < b && b < c ) { count += c - a - 1 ; } else { count += c - a ; } a ++ ; } else { c -- ; } } } return count * 2 ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 6 , 10 , 13 , 21 } ; int N = arr . length ; System . out . print ( getCount ( arr , N ) ) ; } }