codeparrot/xlcost-text-to-code huggingface dataset:
Sort string of characters using Stack | Java program to sort string of characters using stack ; Function to print the characters in sorted order ; Primary stack ; Secondary stack ; Append first character ; Iterate for all character in string ; i - th character ASCII ; Stack 's top element ASCII  ; If greater or equal to top element then push to stack ; If smaller , then push all element to the temporary stack ; Push all greater elements ; Push operation ; Push till the stack is not - empty ; Push the i - th character ; Push the tempstack back to stack ; Print the stack in reverse order ; Driver Code
import java . io . * ; import java . util . * ; class GFG { public static void printSorted ( String s , int l ) { Stack < Character > stack = new Stack < Character > ( ) ; Stack < Character > tempstack = new Stack < Character > ( ) ; stack . push ( s . charAt ( 0 ) ) ; for ( int i = 1 ; i < l ; i ++ ) { int a = s . charAt ( i ) ; int b = ( int ) ( ( char ) stack . peek ( ) ) ; if ( ( a - b ) >= 1 || ( a == b ) ) { stack . push ( s . charAt ( i ) ) ; } else if ( ( b - a ) >= 1 ) { while ( ( b - a ) >= 1 ) { tempstack . push ( stack . peek ( ) ) ; stack . pop ( ) ; if ( stack . size ( ) > 0 ) { b = ( int ) ( ( char ) stack . peek ( ) ) ; } else { break ; } } stack . push ( s . charAt ( i ) ) ; while ( tempstack . size ( ) > 0 ) { stack . push ( tempstack . peek ( ) ) ; tempstack . pop ( ) ; } } } String answer = " " ; while ( stack . size ( ) > 0 ) { answer = stack . peek ( ) + answer ; stack . pop ( ) ; } System . out . println ( answer ) ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; int l = s . length ( ) ; printSorted ( s , l ) ; } }
Check whether an array can be fit into another array rearranging the elements in the array | Java Program to check whether an array can be fit into another array with given condition . ; Returns true if the array A can be fit into array B , otherwise false ; Sort both the arrays ; Iterate over the loop and check whether every array element of A is less than or equal to its corresponding array element of B ; Driver Code
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static boolean checkFittingArrays ( int [ ] A , int [ ] B , int N ) { Arrays . sort ( A ) ; Arrays . sort ( B ) ; for ( int i = 0 ; i < N ; i ++ ) if ( A [ i ] > B [ i ] ) return false ; return true ; } public static void main ( String [ ] args ) { int A [ ] = { 7 , 5 , 3 , 2 } ; int B [ ] = { 5 , 4 , 8 , 7 } ; int N = A . length ; if ( checkFittingArrays ( A , B , N ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }
Maximise the number of toys that can be purchased with amount K | Java Program to maximize the number of toys with K amount ; This functions returns the required number of toys ; sort the cost array ; Check if we can buy ith toy or not ; Increment count ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int maximum_toys ( int cost [ ] , int N , int K ) { int count = 0 , sum = 0 ; Arrays . sort ( cost ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( sum + cost [ i ] <= K ) { sum = sum + cost [ i ] ; count ++ ; } } return count ; } public static void main ( String [ ] args ) { int K = 50 ; int cost [ ] = { 1 , 12 , 5 , 111 , 200 , 1000 , 10 , 9 , 12 , 15 } ; int N = cost . length ; System . out . print ( maximum_toys ( cost , N , K ) ) ; } }
Find if k bookings possible with given arrival and departure times | Java code implementation of the above approach ; Driver Code
import java . util . * ; class GFG { static String areBookingsPossible ( int [ ] A , int [ ] B , int K ) { Arrays . sort ( A ) ; Arrays . sort ( B ) ; for ( int i = 0 ; i < A . length ; i ++ ) { if ( i + K < A . length && A [ i + K ] < B [ i ] ) { return " No " ; } } return " Yes " ; } public static void main ( String [ ] s ) { int [ ] arrival = { 1 , 2 , 3 } ; int [ ] departure = { 2 , 3 , 4 } ; int K = 1 ; System . out . print ( areBookingsPossible ( arrival , departure , K ) ) ; } }
Insertion Sort by Swapping Elements | Recursive Java program to sort an array by swapping elements ; Utility function to print a Vector ; Function performs insertion sort on vector V ; General Case Sort V till second last element and then insert last element into V ; Insertion step ; Insert V [ i ] into list 0. . i - 1 ; Swap V [ j ] and V [ j - 1 ] ; Decrement j by 1 ; Driver code
import java . io . * ; import java . util . * ; class GFG { static void printVector ( Vector < Integer > V ) { for ( int i = 0 ; i < V . size ( ) ; i ++ ) { System . out . print ( V . get ( i ) + " ▁ " ) ; } System . out . println ( ) ; } static void insertionSortRecursive ( Vector < Integer > V , int N ) { if ( N <= 1 ) return ; insertionSortRecursive ( V , N - 1 ) ; int j = N - 1 ; while ( j > 0 && V . get ( j ) < V . get ( j - 1 ) ) { int temp = V . get ( j ) ; V . set ( j , V . get ( j - 1 ) ) ; V . set ( j - 1 , temp ) ; j -= 1 ; } } public static void main ( String [ ] args ) { Vector < Integer > A = new Vector < Integer > ( ) ; A . add ( 0 , 9 ) ; A . add ( 1 , 8 ) ; A . add ( 2 , 7 ) ; A . add ( 3 , 5 ) ; A . add ( 4 , 2 ) ; A . add ( 5 , 1 ) ; A . add ( 6 , 2 ) ; A . add ( 7 , 3 ) ; System . out . print ( " Array : ▁ " ) ; printVector ( A ) ; System . out . print ( " After ▁ Sorting ▁ : " ) ; insertionSortRecursive ( A , A . size ( ) ) ; printVector ( A ) ; } }
Most frequent word in an array of strings | Java implementation ; Function returns word with highest frequency ; Create HashMap to store word and it 's frequency ; Iterate through array of words ; If word already exist in HashMap then increase it 's count by 1 ; Otherwise add word to HashMap ; Create set to iterate over HashMap ; Check for word having highest frequency ; Return word having highest frequency ; Driver code ; Print word having highest frequency
import java . util . * ; class GKG { static String findWord ( String [ ] arr ) { HashMap < String , Integer > hs = new HashMap < String , Integer > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( hs . containsKey ( arr [ i ] ) ) { hs . put ( arr [ i ] , hs . get ( arr [ i ] ) + 1 ) ; } else { hs . put ( arr [ i ] , 1 ) ; } } Set < Map . Entry < String , Integer > > set = hs . entrySet ( ) ; String key = " " ; int value = 0 ; for ( Map . Entry < String , Integer > me : set ) { if ( me . getValue ( ) > value ) { value = me . getValue ( ) ; key = me . getKey ( ) ; } } return key ; } public static void main ( String [ ] args ) { String arr [ ] = { " geeks " , " for " , " geeks " , " a " , " portal " , " to " , " learn " , " can " , " be " , " computer " , " science " , " zoom " , " yup " , " fire " , " in " , " be " , " data " , " geeks " } ; String sol = findWord ( arr ) ; System . out . println ( sol ) ; } }
Check if given array is almost sorted ( elements are at | JAVA Code to check if given array is almost sorted or not ; function for checking almost sort ; One by one compare adjacents . ; check whether resultant is sorted or not ; is resultant is sorted return true ; Driver program to test above function
import java . util . * ; class GFG { public static boolean almostSort ( int A [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( A [ i ] > A [ i + 1 ] ) { int temp = A [ i ] ; A [ i ] = A [ i + 1 ] ; A [ i + 1 ] = temp ; i ++ ; } } for ( int i = 0 ; i < n - 1 ; i ++ ) if ( A [ i ] > A [ i + 1 ] ) return false ; return true ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 3 , 2 , 4 , 6 , 5 } ; int n = A . length ; if ( almostSort ( A , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Efficiently merging two sorted arrays with O ( 1 ) extra space | Java program for Merging two sorted arrays with O ( 1 ) extra space ; Function to find next gap . ; comparing elements in the first array . ; comparing elements in both arrays . ; comparing elements in the second array . ; Driver Code ; Function Call
public class MergeTwoSortedArrays { private static int nextGap ( int gap ) { if ( gap <= 1 ) return 0 ; return ( gap / 2 ) + ( gap % 2 ) ; } private static void merge ( int [ ] arr1 , int [ ] arr2 , int n , int m ) { int i , j , gap = n + m ; for ( gap = nextGap ( gap ) ; gap > 0 ; gap = nextGap ( gap ) ) { for ( i = 0 ; i + gap < n ; i ++ ) if ( arr1 [ i ] > arr1 [ i + gap ] ) { int temp = arr1 [ i ] ; arr1 [ i ] = arr1 [ i + gap ] ; arr1 [ i + gap ] = temp ; } for ( j = gap > n ? gap - n : 0 ; i < n && j < m ; i ++ , j ++ ) if ( arr1 [ i ] > arr2 [ j ] ) { int temp = arr1 [ i ] ; arr1 [ i ] = arr2 [ j ] ; arr2 [ j ] = temp ; } if ( j < m ) { for ( j = 0 ; j + gap < m ; j ++ ) if ( arr2 [ j ] > arr2 [ j + gap ] ) { int temp = arr2 [ j ] ; arr2 [ j ] = arr2 [ j + gap ] ; arr2 [ j + gap ] = temp ; } } } } public static void main ( String [ ] args ) { int [ ] a1 = { 10 , 27 , 38 , 43 , 82 } ; int [ ] a2 = { 3 , 9 } ; merge ( a1 , a2 , a1 . length , a2 . length ) ; System . out . print ( " First ▁ Array : ▁ " ) ; for ( int i = 0 ; i < a1 . length ; i ++ ) { System . out . print ( a1 [ i ] + " ▁ " ) ; } System . out . println ( ) ; System . out . print ( " Second ▁ Array : ▁ " ) ; for ( int i = 0 ; i < a2 . length ; i ++ ) { System . out . print ( a2 [ i ] + " ▁ " ) ; } } }
Merge two sorted arrays | Java program to merge two sorted arrays ; Merge arr1 [ 0. . n1 - 1 ] and arr2 [ 0. . n2 - 1 ] into arr3 [ 0. . n1 + n2 - 1 ] ; Traverse both array ; Check if current element of first array is smaller than current element of second array . If yes , store first array element and increment first array index . Otherwise do same with second array ; Store remaining elements of first array ; Store remaining elements of second array ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class MergeTwoSorted { public static void mergeArrays ( int [ ] arr1 , int [ ] arr2 , int n1 , int n2 , int [ ] arr3 ) { int i = 0 , j = 0 , k = 0 ; while ( i < n1 && j < n2 ) { if ( arr1 [ i ] < arr2 [ j ] ) arr3 [ k ++ ] = arr1 [ i ++ ] ; else arr3 [ k ++ ] = arr2 [ j ++ ] ; } while ( i < n1 ) arr3 [ k ++ ] = arr1 [ i ++ ] ; while ( j < n2 ) arr3 [ k ++ ] = arr2 [ j ++ ] ; } public static void main ( String [ ] args ) { int [ ] arr1 = { 1 , 3 , 5 , 7 } ; int n1 = arr1 . length ; int [ ] arr2 = { 2 , 4 , 6 , 8 } ; int n2 = arr2 . length ; int [ ] arr3 = new int [ n1 + n2 ] ; mergeArrays ( arr1 , arr2 , n1 , n2 , arr3 ) ; System . out . println ( " Array ▁ after ▁ merging " ) ; for ( int i = 0 ; i < n1 + n2 ; i ++ ) System . out . print ( arr3 [ i ] + " ▁ " ) ; } }
Sort array after converting elements to their squares | Java program to Sort square of the numbers of the array ; Function to sort an square array ; First convert each array elements into its square ; Sort an array using " inbuild ▁ sort ▁ function " in Arrays class . ; Driver program to test above function
import java . util . * ; import java . io . * ; class GFG { public static void sortSquares ( int arr [ ] ) { int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = arr [ i ] * arr [ i ] ; Arrays . sort ( arr ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 6 , - 3 , - 1 , 2 , 4 , 5 } ; int n = arr . length ; System . out . println ( " Before ▁ sort ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; sortSquares ( arr ) ; System . out . println ( " " ) ; System . out . println ( " After ▁ Sort ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
Sort an array when two halves are sorted | Java program to Merge Two Sorted Halves Of Array Into Single Sorted Array ; Merge two sorted halves of Array into single sorted array ; int half_i = 0 ; starting index of second half ; Temp Array store sorted resultant array ; First Find the point where array is divide into two half ; If Given array is all - ready sorted ; Merge two sorted arrays in single sorted array ; Copy the remaining elements of A [ i to half_ ! ] ; Copy the remaining elements of A [ half_ ! to n ] ; Driver code ; Print sorted Array
import java . io . * ; class GFG { static void mergeTwoHalf ( int [ ] A , int n ) { int i ; int [ ] temp = new int [ n ] ; for ( i = 0 ; i < n - 1 ; i ++ ) { if ( A [ i ] > A [ i + 1 ] ) { half_i = i + 1 ; break ; } } if ( half_i == 0 ) return ; i = 0 ; int j = half_i ; int k = 0 ; while ( i < half_i && j < n ) { if ( A [ i ] < A [ j ] ) temp [ k ++ ] = A [ i ++ ] ; else temp [ k ++ ] = A [ j ++ ] ; } while ( i < half_i ) temp [ k ++ ] = A [ i ++ ] ; while ( j < n ) temp [ k ++ ] = A [ j ++ ] ; for ( i = 0 ; i < n ; i ++ ) A [ i ] = temp [ i ] ; } static public void main ( String [ ] args ) { int [ ] A = { 2 , 3 , 8 , - 1 , 7 , 10 } ; int n = A . length ; mergeTwoHalf ( A , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( A [ i ] + " ▁ " ) ; } }
Chocolate Distribution Problem | JAVA Code For Chocolate Distribution Problem ; arr [ 0. . n - 1 ] represents sizes of packets . m is number of students . Returns minimum difference between maximum and minimum values of distribution . ; if there are no chocolates or number of students is 0 ; Sort the given packets ; Number of students cannot be more than number of packets ; Largest number of chocolates ; Find the subarray of size m such that difference between last ( maximum in case of sorted ) and first ( minimum in case of sorted ) elements of subarray is minimum . ; Driver program to test above function ; int m = 7 ; Number of students
import java . util . * ; class GFG { static int findMinDiff ( int arr [ ] , int n , int m ) { if ( m == 0 n == 0 ) return 0 ; Arrays . sort ( arr ) ; if ( n < m ) return - 1 ; int min_diff = Integer . MAX_VALUE ; for ( int i = 0 ; i + m - 1 < n ; i ++ ) { int diff = arr [ i + m - 1 ] - arr [ i ] ; if ( diff < min_diff ) min_diff = diff ; } return min_diff ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 4 , 7 , 9 , 2 , 23 , 25 , 41 , 30 , 40 , 28 , 42 , 30 , 44 , 48 , 43 , 50 } ; int n = arr . length ; System . out . println ( " Minimum ▁ difference ▁ is ▁ " + findMinDiff ( arr , n , m ) ) ; } }
Absolute distinct count in a sorted array | java code to find absolute distinct count of an array in O ( n ) time . ; The function returns number of distinct absolute values among the elements of the array ; Note that set keeps only one copy even if we try to insert multiple values ; Driver code
import java . util . * ; class GFG { static int distinctCount ( int arr [ ] , int n ) { Set < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) s . add ( Math . abs ( arr [ i ] ) ) ; return s . size ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 2 , - 1 , 0 , 1 , 1 } ; int n = arr . length ; System . out . println ( " Count ▁ of ▁ absolute ▁ distinct ▁ values ▁ : ▁ " + distinctCount ( arr , n ) ) ; } }
Absolute distinct count in a sorted array | Java program to find absolute distinct count of an array using O ( 1 ) space . ; The function returns return number of distinct absolute values among the elements of the array ; initialize count as number of elements ; Remove duplicate elements from the left of the current window ( i , j ) and also decrease the count ; Remove duplicate elements from the right of the current window ( i , j ) and also decrease the count ; break if only one element is left ; Now look for the zero sum pair in current window ( i , j ) ; decrease the count if ( positive , negative ) pair is encountered ; Driver code
import java . io . * ; class GFG { static int distinctCount ( int arr [ ] , int n ) { int count = n ; int i = 0 , j = n - 1 , sum = 0 ; while ( i < j ) { while ( i != j && arr [ i ] == arr [ i + 1 ] ) { count -- ; i ++ ; } while ( i != j && arr [ j ] == arr [ j - 1 ] ) { count -- ; j -- ; } if ( i == j ) break ; sum = arr [ i ] + arr [ j ] ; if ( sum == 0 ) { count -- ; i ++ ; j -- ; } else if ( sum < 0 ) i ++ ; else j -- ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { - 2 , - 1 , 0 , 1 , 1 } ; int n = arr . length ; System . out . println ( " Count ▁ of ▁ absolute ▁ distinct ▁ values ▁ : ▁ " + distinctCount ( arr , n ) ) ; } }
Sorting Strings using Bubble Sort | Java implementation ; Sorting strings using bubble sort ; Driver code
class GFG { static int MAX = 100 ; public static void sortStrings ( String [ ] arr , int n ) { String temp ; for ( int j = 0 ; j < n - 1 ; j ++ ) { for ( int i = j + 1 ; i < n ; i ++ ) { if ( arr [ j ] . compareTo ( arr [ i ] ) > 0 ) { temp = arr [ j ] ; arr [ j ] = arr [ i ] ; arr [ i ] = temp ; } } } } public static void main ( String [ ] args ) { String [ ] arr = { " GeeksforGeeks " , " Quiz " , " Practice " , " Gblogs " , " Coding " } ; int n = arr . length ; sortStrings ( arr , n ) ; System . out . println ( " Strings ▁ in ▁ sorted ▁ order ▁ are ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . println ( " String ▁ " + ( i + 1 ) + " ▁ is ▁ " + arr [ i ] ) ; } }
Sort an almost sorted array where only two elements are swapped | Java program to sort using one swap ; This function sorts an array that can be sorted by single swap ; Traverse the given array from rightmost side ; Check if arr [ i ] is not in order ; Find the other element to be swapped with arr [ i ] ; Swap the pair ; A utility function to print an array of size n ; Driver Code
import java . io . * ; class GFG { static void sortByOneSwap ( int arr [ ] , int n ) { for ( int i = n - 1 ; i > 0 ; i -- ) { if ( arr [ i ] < arr [ i - 1 ] ) { int j = i - 1 ; while ( j >= 0 && arr [ i ] < arr [ j ] ) j -- ; int temp = arr [ i ] ; arr [ i ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; break ; } } } static void printArray ( int arr [ ] , int n ) { int i ; for ( i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 30 , 20 , 40 , 50 , 60 , 70 } ; int n = arr . length ; System . out . println ( " Given ▁ array ▁ is ▁ " ) ; printArray ( arr , n ) ; sortByOneSwap ( arr , n ) ; System . out . println ( " Sorted ▁ array ▁ is ▁ " ) ; printArray ( arr , n ) ; } }
Pancake sorting | Java program to sort array using pancake sort ; Reverses arr [ 0. . i ] ; Returns index of the maximum element in arr [ 0. . n - 1 ] ; The main function that sorts given array using flip operations ; Start from the complete array and one by one reduce current size by one ; Find index of the maximum element in arr [ 0. . curr_size - 1 ] ; Move the maximum element to end of current array if it 's not already at  the end ; To move at the end , first move maximum number to beginning ; Now move the maximum number to end by reversing current array ; Utility function to print array arr [ ] ; Driver function to check for above functions
import java . io . * ; class PancakeSort { static void flip ( int arr [ ] , int i ) { int temp , start = 0 ; while ( start < i ) { temp = arr [ start ] ; arr [ start ] = arr [ i ] ; arr [ i ] = temp ; start ++ ; i -- ; } } static int findMax ( int arr [ ] , int n ) { int mi , i ; for ( mi = 0 , i = 0 ; i < n ; ++ i ) if ( arr [ i ] > arr [ mi ] ) mi = i ; return mi ; } static int pancakeSort ( int arr [ ] , int n ) { for ( int curr_size = n ; curr_size > 1 ; -- curr_size ) { int mi = findMax ( arr , curr_size ) ; if ( mi != curr_size - 1 ) { flip ( arr , mi ) ; flip ( arr , curr_size - 1 ) ; } } return 0 ; } static void printArray ( int arr [ ] , int arr_size ) { for ( int i = 0 ; i < arr_size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( " " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 23 , 10 , 20 , 11 , 12 , 6 , 7 } ; int n = arr . length ; pancakeSort ( arr , n ) ; System . out . println ( " Sorted ▁ Array : ▁ " ) ; printArray ( arr , n ) ; } }
Modify given string such that odd and even indices is lexicographically largest and smallest | Java program for the above approach ; Function to modify the given String satisfying the given criteria ; Traverse the String S ; If i is even ; If the S [ i ] is ' a ' , then change S [ i ] to ' b ' ; Otherwise , change S [ i ] to ' a ' ; If S [ i ] is ' z ' , then change S [ i ] to ' y ' ; Otherwise , change S [ i ] to ' z ' ; Return the result ; Driver Code
import java . util . * ; class GFG { static String performOperation ( char [ ] S , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { if ( S [ i ] == ' a ' ) { S [ i ] = ' b ' ; } else { S [ i ] = ' a ' ; } } else { if ( S [ i ] == ' z ' ) { S [ i ] = ' y ' ; } else { S [ i ] = ' z ' ; } } } return String . valueOf ( S ) ; } public static void main ( String [ ] args ) { String S = " giad " ; int N = S . length ( ) ; System . out . print ( performOperation ( S . toCharArray ( ) , N ) ) ; } }
Count of values chosen for X such that N is reduced to 0 after given operations | Java program for the above approach ; Function to check if the value of X reduces N to 0 or not ; Update the value of N as N - x ; Check if x is a single digit integer ; Function to find the number of values X such that N can be reduced to 0 after performing the given operations ; Number of digits in N ; Stores the count of value of X ; Iterate over all possible value of X ; Check if x follow the conditions ; Return total count ; Driver Code
import java . util . * ; class GFG { static boolean check ( int x , int N ) { while ( true ) { N -= x ; if ( x < 10 ) break ; int temp2 = 0 ; while ( x > 0 ) { temp2 += ( x % 10 ) ; x = ( int ) x / 10 ; } x = temp2 ; } if ( ( x < 10 ) && ( N == 0 ) ) { return true ; } return false ; } static int countNoOfsuchX ( int N ) { int k = ( int ) ( Math . log10 ( N ) ) + 1 ; int count = 1 ; for ( int x = ( N - ( k * ( k + 1 ) * 5 ) ) ; x <= N ; x ++ ) { if ( check ( x , N ) ) { count += 1 ; } } return count ; } public static void main ( String [ ] args ) { int N = 9399 ; System . out . println ( countNoOfsuchX ( N ) ) ; } }
Maximize subarrays count containing the maximum and minimum Array element after deleting at most one element | Java implementation of the above approach ; Returns the count of subarrays which contains both the maximum and minimum elements in the given list ; Initialize the low and high of array ; If current element is less than least element ; If current element is more than highest element ; If current element is equal to low or high then update the pointers ; Update number of subarrays ; Return the result ; Function to find the maximum count of subarrays ; Iterate the array to find the maximum and minimum element ; List after removing the minimum element ; Using assignment operator to copy one vector to other ; List after removing the maximum element ; Driver Code ; Given array ; Function Call
import java . util . * ; import java . lang . * ; class GFG { static long proc ( List < Integer > v ) { int n = v . size ( ) ; int low = v . get ( n - 1 ) , high = v . get ( n - 1 ) ; int p1 = n , p2 = n ; long ans = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int x = v . get ( i ) ; if ( x < low ) { low = x ; ans = 0 ; } else if ( x > high ) { high = x ; ans = 0 ; } if ( x == low ) p1 = i ; if ( x == high ) p2 = i ; ans += n - Math . max ( p1 , p2 ) ; } return ans ; } static long subarray ( List < Integer > v ) { int n = v . size ( ) ; if ( n <= 1 ) return n ; long ans = proc ( v ) ; int low = v . get ( 0 ) , pos_low = 0 ; int high = v . get ( 0 ) , pos_high = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int x = v . get ( i ) ; if ( x < low ) { low = x ; pos_low = i ; } else if ( x > high ) { high = x ; pos_high = i ; } } List < Integer > u = new ArrayList < > ( Collections . nCopies ( n , 0 ) ) ; Collections . copy ( u , v ) ; u . remove ( pos_low ) ; ans = Math . max ( ans , proc ( u ) ) ; List < Integer > w = new ArrayList < > ( Collections . nCopies ( n , 0 ) ) ; Collections . copy ( w , v ) ; w . remove ( pos_high ) ; return Math . max ( ans , proc ( w ) ) ; } public static void main ( String [ ] args ) { List < Integer > arr = Arrays . asList ( 7 , 2 , 5 , 4 , 3 , 1 ) ; System . out . println ( subarray ( arr ) ) ; } }