codeparrot/xlcost-text-to-code huggingface dataset:
Maximize the sum of array by multiplying prefix of array with | Java implementation of the approach ; To store sum ; To store ending indices of the chosen prefix array vect ; Adding the absolute value of a [ i ] ; If i == 0 then there is no index to be flipped in ( i - 1 ) position ; print the maximised sum ; print the ending indices of the chosen prefix arrays ; Driver Code
import java . util . * ; class GFG { static void maxSum ( int [ ] a , int n ) { Vector < Integer > l = new Vector < Integer > ( ) ; int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { s += Math . abs ( a [ i ] ) ; if ( a [ i ] >= 0 ) continue ; if ( i == 0 ) l . add ( i + 1 ) ; else { l . add ( i + 1 ) ; l . add ( i ) ; } } System . out . println ( s ) ; for ( int i = 0 ; i < l . size ( ) ; i ++ ) System . out . print ( l . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 4 ; int a [ ] = { 1 , - 2 , - 3 , 4 } ; maxSum ( a , n ) ; } }
Find the longest common prefix between two strings after performing swaps on second string | Java program to find the longest common prefix between two strings after performing swaps on the second string ; int a = x . length ( ) ; length of x int b = y . length ( ) ; length of y ; creating frequency array of characters of y ; storing the length of longest common prefix ; checking if the frequency of the character at position i in x in b is greater than zero or not if zero we increase the prefix count by 1 ; Driver Code
public class GFG { static void LengthLCP ( String x , String y ) { int fr [ ] = new int [ 26 ] ; for ( int i = 0 ; i < b ; i ++ ) { fr [ y . charAt ( i ) - 97 ] += 1 ; } int c = 0 ; for ( int i = 0 ; i < a ; i ++ ) { if ( fr [ x . charAt ( i ) - 97 ] > 0 ) { c += 1 ; fr [ x . charAt ( i ) - 97 ] -= 1 ; } else break ; } System . out . println ( ( c ) ) ; } public static void main ( String args [ ] ) { String x = " here " , y = " there " ; LengthLCP ( x , y ) ; } }
All possible co | Java implementation of the approach ; Function to count possible pairs ; total count of numbers in range ; printing count of pairs ; Driver code
import java . util . * ; class solution { static void CountPair ( int L , int R ) { int x = ( R - L + 1 ) ; System . out . println ( x / 2 + "NEW_LINE"); } public static void main ( String args [ ] ) { int L , R ; L = 1 ; R = 8 ; CountPair ( L , R ) ; } }
Problems not solved at the end of Nth day | Java program to find problems not solved at the end of Nth day ; Function to find problems not solved at the end of Nth day ; Driver Code
class Gfg { public static int problemsLeft ( int K , int P , int N ) { if ( K <= P ) return 0 ; else return ( ( K - P ) * N ) ; } public static void main ( String args [ ] ) { int K , P , N ; K = 4 ; P = 1 ; N = 10 ; System . out . println ( problemsLeft ( K , P , N ) ) ; } }
Kruskal 's Algorithm (Simple Implementation for Adjacency Matrix) | Simple Java implementation for Kruskal 's algorithm ; Find set of vertex i ; Does union of i and j . It returns false if i and j are already in same set . ; Finds MST using Kruskal 's algorithm ; Initialize sets of disjoint sets . ; Include minimum weight edges one by one ; Driver code ; Let us create the following graph 2 3 ( 0 ) -- ( 1 ) -- ( 2 ) | / \ | 6 | 8 / \ 5 | 7 | / \ | ( 3 ) -- -- -- - ( 4 ) 9 ; Print the solution
import java . util . * ; class GFG { static int V = 5 ; static int [ ] parent = new int [ V ] ; static int INF = Integer . MAX_VALUE ; static int find ( int i ) { while ( parent [ i ] != i ) i = parent [ i ] ; return i ; } static void union1 ( int i , int j ) { int a = find ( i ) ; int b = find ( j ) ; parent [ a ] = b ; } static void kruskalMST ( int cost [ ] [ ] ) { for ( int i = 0 ; i < V ; i ++ ) parent [ i ] = i ; int edge_count = 0 ; while ( edge_count < V - 1 ) { int min = INF , a = - 1 , b = - 1 ; for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { if ( find ( i ) != find ( j ) && cost [ i ] [ j ] < min ) { min = cost [ i ] [ j ] ; a = i ; b = j ; } } } union1 ( a , b ) ; System . out . printf ( "Edge %d:(%d, %d) cost:%d NEW_LINE", edge_count++, a, b, min); mincost += min ; } System . out . printf ( " Minimum cost = % d ", mincost); } public static void main ( String [ ] args ) { int cost [ ] [ ] = { { INF , 2 , INF , 6 , INF } , { 2 , INF , 3 , 8 , 5 } , { INF , 3 , INF , INF , 7 } , { 6 , 8 , INF , INF , 9 } , { INF , 5 , 7 , 9 , INF } , } ; kruskalMST ( cost ) ; } }
Number of chocolates left after k iterations | Java program to find remaining chocolates after k iterations ; Function to find the chocolates left ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int results ( int n , int k ) { return ( int ) Math . round ( Math . pow ( n , ( 1.0 / Math . pow ( 2.0 , k ) ) ) ) ; } public static void main ( String args [ ] ) { int k = 3 , n = 100000000 ; System . out . print ( " Chocolates ▁ left ▁ after ▁ " + k + " ▁ iterations ▁ are ▁ " + results ( n , k ) ) ; } }
Subarray whose absolute sum is closest to K | Java code to find sub - array whose sum shows the minimum deviation ; Starting index , ending index , Deviation ; Iterate i and j to get all subarrays ; Found sub - array with less sum ; Exactly same sum ; Driver Code ; Array to store return values
class GFG { public static int [ ] getSubArray ( int [ ] arr , int n , int K ) { int i = - 1 ; int j = - 1 ; int currSum = 0 ; int [ ] result = { i , j , Math . abs ( K - Math . abs ( currSum ) ) } ; for ( i = 0 ; i < n ; i ++ ) { currSum = 0 ; for ( j = i ; j < n ; j ++ ) { currSum += arr [ j ] ; int currDev = Math . abs ( K - Math . abs ( currSum ) ) ; if ( currDev < result [ 2 ] ) { result [ 0 ] = i ; result [ 1 ] = j ; result [ 2 ] = currDev ; } if ( currDev == 0 ) return result ; } } return result ; } public static void main ( String [ ] args ) { int [ ] arr = { 15 , - 3 , 5 , 2 , 7 , 6 , 34 , - 6 } ; int n = arr . length ; int K = 50 ; int [ ] ans = getSubArray ( arr , n , K ) ; if ( ans [ 0 ] == - 1 ) { System . out . println ( " The ▁ empty ▁ array ▁ " + " shows ▁ minimum ▁ Deviation " ) ; } else { for ( int i = ans [ 0 ] ; i <= ans [ 1 ] ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } } }
Longest subsequence whose average is less than K | Java program to perform Q queries to find longest subsequence whose average is less than K ; Function to print the length for evey query ; sort array of N elements ; Array to store average from left ; Sort array of average ; number of queries ; print answer to every query using binary search ; Driver Code ; 4 queries
import java . util . Arrays ; class GFG { static void longestSubsequence ( int a [ ] , int n , int q [ ] , int m ) { Arrays . sort ( a ) ; int sum = 0 ; int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; double av = ( double ) ( sum ) / ( double ) ( i + 1 ) ; b [ i ] = ( ( int ) ( av + 1 ) ) ; } Arrays . sort ( b ) ; for ( int i = 0 ; i < m ; i ++ ) { int k = q [ i ] ; int longest = upperBound ( b , 0 , n , k ) ; System . out . println ( " Answer ▁ to ▁ Query " + ( i + 1 ) + " : ▁ " + longest ) ; } } private static int upperBound ( 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 ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 2 , 5 , 4 } ; int n = a . length ; int q [ ] = { 4 , 2 , 1 , 5 } ; int m = q . length ; longestSubsequence ( a , n , q , m ) ; } }
Make array elements equal in Minimum Steps | Java program to make the array elements equal in minimum steps ; Returns the minimum steps required to make an array of N elements equal , where the first array element equals M ; Corner Case 1 : When N = 1 ; Corner Case 2 : When N = 2 else if ( N == 2 ) corner case 2 ; Driver Code
import java . io . * ; class GFG { static int steps ( int N , int M ) { if ( N == 1 ) return 0 ; return M ; return 2 * M + ( N - 3 ) ; } public static void main ( String [ ] args ) { int N = 4 , M = 4 ; System . out . print ( steps ( N , M ) ) ; } }
Minimum increment / decrement to make array non | Java code to count the change required to convert the array into non - increasing array ; min heap ; Here in the loop we will check that whether the upcoming element of array is less than top of priority queue . If yes then we calculate the difference . After that we will remove that element and push the current element in queue . And the sum is incremented by the value of difference ; Driver Code
import java . util . PriorityQueue ; class GFG { public static int DecreasingArray ( int a [ ] , int n ) { int sum = 0 , dif = 0 ; PriorityQueue < Integer > pq = new PriorityQueue < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! pq . isEmpty ( ) && pq . element ( ) < a [ i ] ) { dif = a [ i ] - pq . element ( ) ; sum += dif ; pq . remove ( ) ; } pq . add ( a [ i ] ) ; } return sum ; } public static void main ( String [ ] args ) { int [ ] a = { 3 , 1 , 2 , 1 } ; int n = a . length ; System . out . println ( DecreasingArray ( a , n ) ) ; } }
Schedule jobs so that each server gets equal load | Java program to schedule jobs so that each server gets equal load . ; Function to find new array a ; find sum S of both arrays a and b . ; Single element case . ; This checks whether sum s can be divided equally between all array elements . i . e . whether all elements can take equal value or not . ; Compute possible value of new array elements . ; Possibility 1 ; ensuring that all elements of array b are used . ; If a ( i ) already updated to x move to next element in array a . ; Possibility 2 ; Possibility 3 ; Possibility 4 ; If a ( i ) can not be made equal to x even after adding all possible elements from b ( i ) then print - 1. ; check whether all elements of b are used . ; Return the new array element value . ; Driver code
class GFG { static int solve ( int a [ ] , int b [ ] , int n ) { int i ; int s = 0 ; for ( i = 0 ; i < n ; i ++ ) s += ( a [ i ] + b [ i ] ) ; if ( n == 1 ) return a [ 0 ] + b [ 0 ] ; if ( s % n != 0 ) return - 1 ; int x = s / n ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] > x ) return - 1 ; if ( i > 0 ) { a [ i ] += b [ i - 1 ] ; b [ i - 1 ] = 0 ; } if ( a [ i ] == x ) continue ; int y = a [ i ] + b [ i ] ; if ( i + 1 < n ) y += b [ i + 1 ] ; if ( y == x ) { a [ i ] = y ; b [ i ] = 0 ; continue ; } if ( a [ i ] + b [ i ] == x ) { a [ i ] += b [ i ] ; b [ i ] = 0 ; continue ; } if ( i + 1 < n && a [ i ] + b [ i + 1 ] == x ) { a [ i ] += b [ i + 1 ] ; b [ i + 1 ] = 0 ; continue ; } return - 1 ; } for ( i = 0 ; i < n ; i ++ ) if ( b [ i ] != 0 ) return - 1 ; return x ; } public static void main ( String [ ] args ) { int a [ ] = { 6 , 14 , 21 , 1 } ; int b [ ] = { 15 , 7 , 10 , 10 } ; int n = a . length ; System . out . println ( solve ( a , b , n ) ) ; } }
Check if it is possible to survive on Island | Java program to find the minimum days on which you need to buy food from the shop so that you can survive the next S days ; function to find the minimum days ; If we can not buy at least a week supply of food during the first week OR We can not buy a day supply of food on the first day then we can 't survive. ; If we can survive then we can buy ceil ( A / N ) times where A is total units of food required . ; Driver code
import java . io . * ; class GFG { static void survival ( int S , int N , int M ) { if ( ( ( N * 6 ) < ( M * 7 ) && S > 6 ) M > N ) System . out . println ( " No " ) ; else { int days = ( M * S ) / N ; if ( ( ( M * S ) % N ) != 0 ) days ++ ; System . out . println ( " Yes ▁ " + days ) ; } } public static void main ( String [ ] args ) { int S = 10 , N = 16 , M = 2 ; survival ( S , N , M ) ; } }
Lexicographically largest subsequence such that every character occurs at least k times | Java program to find lexicographically largest subsequence where every character appears at least k times . ; Find lexicographically largest subsequence of s [ 0. . n - 1 ] such that every character appears at least k times . The result is filled in t [ ] ; Starting from largest charter ' z ' to ' a ' ; Counting the frequency of the character ; If frequency is greater than k ; From the last point we leave ; check if string contain ch ; If yes , append to output string ; Update the last point . ; Driver code
import java . util . Arrays ; class GFG { static void subsequence ( char s [ ] , char t [ ] , int n , int k ) { int last = 0 , cnt = 0 , new_last = 0 , size = 0 ; for ( char ch = ' z ' ; ch >= ' a ' ; ch -- ) { cnt = 0 ; for ( int i = last ; i < n ; i ++ ) { if ( s [ i ] == ch ) cnt ++ ; } if ( cnt >= k ) { for ( int i = last ; i < n ; i ++ ) { if ( s [ i ] == ch ) { t [ size ++ ] = ch ; new_last = i ; } } last = new_last ; } } t [ size ] = ' \0' ; } public static void main ( String [ ] args ) { char s [ ] = { ' b ' , ' a ' , ' n ' , ' a ' , ' n ' , ' a ' } ; int n = s . length ; int k = 2 ; char t [ ] = new char [ n ] ; subsequence ( s , t , n - 1 , k ) ; for ( int i = 0 ; i < t . length ; i ++ ) if ( t [ i ] != 0 ) System . out . print ( t [ i ] ) ; } }
Program for Least Recently Used ( LRU ) Page Replacement algorithm | Java implementation of above algorithm ; Method to find page faults using indexes ; To represent set of current pages . We use an unordered_set so that we quickly check if a page is present in set or not ; To store least recently used indexes of pages . ; Start from initial page ; Check if the set can hold more pages ; Insert it into set if not present already which represents page fault ; increment page fault ; Store the recently used index of each page ; If the set is full then need to perform lru i . e . remove the least recently used page and insert the current page ; Check if current page is not already present in the set ; Find the least recently used pages that is present in the set ; Remove the indexes page ; remove lru from hashmap ; insert the current page ; Increment page faults ; Update the current page index ; Driver method
import java . util . HashMap ; import java . util . HashSet ; import java . util . Iterator ; class Test { static int pageFaults ( int pages [ ] , int n , int capacity ) { HashSet < Integer > s = new HashSet < > ( capacity ) ; HashMap < Integer , Integer > indexes = new HashMap < > ( ) ; int page_faults = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . size ( ) < capacity ) { if ( ! s . contains ( pages [ i ] ) ) { s . add ( pages [ i ] ) ; page_faults ++ ; } indexes . put ( pages [ i ] , i ) ; } else { if ( ! s . contains ( pages [ i ] ) ) { int lru = Integer . MAX_VALUE , val = Integer . MIN_VALUE ; Iterator < Integer > itr = s . iterator ( ) ; while ( itr . hasNext ( ) ) { int temp = itr . next ( ) ; if ( indexes . get ( temp ) < lru ) { lru = indexes . get ( temp ) ; val = temp ; } } s . remove ( val ) ; indexes . remove ( val ) ; s . add ( pages [ i ] ) ; page_faults ++ ; } indexes . put ( pages [ i ] , i ) ; } } return page_faults ; } public static void main ( String args [ ] ) { int pages [ ] = { 7 , 0 , 1 , 2 , 0 , 3 , 0 , 4 , 2 , 3 , 0 , 3 , 2 } ; int capacity = 4 ; System . out . println ( pageFaults ( pages , pages . length , capacity ) ) ; } }
Largest permutation after at most k swaps | Java Program to find the largest permutation after at most k swaps using unordered_map . ; Function to find the largest permutation after k swaps ; Storing the elements and their index in map ; If number of swaps allowed are equal to number of elements then the resulting permutation will be descending order of given permutation . ; if j is not at it 's best index ; Change the index of the element which was at position 0. Swap the element basically . ; decrement number of swaps ; Driver code ; K is the number of swaps ; n is the size of the array ; Function calling
import java . util . * ; class GFG { static void bestpermutation ( ArrayList < Integer > arr , int k , int n ) { HashMap < Integer , Integer > h = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { h . put ( arr . get ( i ) , i ) ; } if ( n <= k ) { Collections . sort ( arr , Collections . reverseOrder ( ) ) ; } else { for ( int j = n ; j >= 1 ; j -- ) { if ( k > 0 ) { int initial_index = h . get ( j ) ; int best_index = n - j ; if ( initial_index != best_index ) { h . put ( j , best_index ) ; int element = arr . get ( best_index ) ; h . put ( element , initial_index ) ; int temp = arr . get ( best_index ) ; arr . set ( best_index , arr . get ( initial_index ) ) ; arr . set ( initial_index , temp ) ; k -- ; } } } } } public static void main ( String [ ] args ) { ArrayList < Integer > arr = new ArrayList < Integer > ( ) ; arr . add ( 3 ) ; arr . add ( 1 ) ; arr . add ( 4 ) ; arr . add ( 2 ) ; arr . add ( 5 ) ; int k = 10 ; int n = arr . size ( ) ; bestpermutation ( arr , k , n ) ; System . out . print ( " Largest ▁ possible ▁ permutation ▁ after ▁ " + k + " ▁ swaps ▁ is ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr . get ( i ) + " ▁ " ) ; } }
Program for Best Fit algorithm in Memory Management | Java implementation of Best - Fit algorithm ; Method to allocate memory to blocks as per Best fit algorithm ; Stores block id of the block allocated to a process ; Initially no block is assigned to any process ; pick each process and find suitable blocks according to its size ad assign to it ; Find the best fit block for current process ; If we could find a block for current process ; allocate block j to p [ i ] process ; Reduce available memory in this block . ; Driver Method
public class GFG { static void bestFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) { int allocation [ ] = new int [ n ] ; for ( int i = 0 ; i < allocation . length ; i ++ ) allocation [ i ] = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int bestIdx = - 1 ; for ( int j = 0 ; j < m ; j ++ ) { if ( blockSize [ j ] >= processSize [ i ] ) { if ( bestIdx == - 1 ) bestIdx = j ; else if ( blockSize [ bestIdx ] > blockSize [ j ] ) bestIdx = j ; } } if ( bestIdx != - 1 ) { allocation [ i ] = bestIdx ; blockSize [ bestIdx ] -= processSize [ i ] ; } } System . out . println ( " Process No . Process Size Block no . "); for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( " ▁ " + ( i + 1 ) + " TABSYMBOL TABSYMBOL " + processSize [ i ] + " TABSYMBOL TABSYMBOL " ) ; if ( allocation [ i ] != - 1 ) System . out . print ( allocation [ i ] + 1 ) ; else System . out . print ( " Not ▁ Allocated " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int blockSize [ ] = { 100 , 500 , 200 , 300 , 600 } ; int processSize [ ] = { 212 , 417 , 112 , 426 } ; int m = blockSize . length ; int n = processSize . length ; bestFit ( blockSize , m , processSize , n ) ; } }
Bin Packing Problem ( Minimize number of used Bins ) | Java program to find number of bins required using Best fit algorithm . ; Returns number of bins required using best fit online algorithm ; Initialize result ( Count of bins ) ; Create an array to store remaining space in bins there can be at most n bins ; Place items one by one ; Find the best bin that can accommodate weight [ i ] ; Initialize minimum space left and index of best bin ; If no bin could accommodate weight [ i ] , create a new bin ; else Assign the item to best bin ; Driver code
class GFG { static int bestFit ( int weight [ ] , int n , int c ) { int res = 0 ; int [ ] bin_rem = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int j ; int min = c + 1 , bi = 0 ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] && bin_rem [ j ] - weight [ i ] < min ) { bi = j ; min = bin_rem [ j ] - weight [ i ] ; } } if ( min == c + 1 ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; } bin_rem [ bi ] -= weight [ i ] ; } return res ; } public static void main ( String [ ] args ) { int [ ] weight = { 2 , 5 , 4 , 7 , 1 , 3 , 8 } ; int c = 10 ; int n = weight . length ; System . out . print ( " Number ▁ of ▁ bins ▁ required ▁ in ▁ Best ▁ Fit ▁ : ▁ " + bestFit ( weight , n , c ) ) ; } }
Bin Packing Problem ( Minimize number of used Bins ) | Java program to find number of bins required using Worst fit algorithm . ; Returns number of bins required using worst fit online algorithm ; Initialize result ( Count of bins ) ; Create an array to store remaining space in bins there can be at most n bins ; Place items one by one ; Find the best bin that ca \ n accommodate weight [ i ] ; Initialize maximum space left and index of worst bin ; If no bin could accommodate weight [ i ] , create a new bin ; else Assign the item to best bin ; Driver program
class GFG { static int worstFit ( int weight [ ] , int n , int c ) { int res = 0 ; int bin_rem [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int j ; int mx = - 1 , wi = 0 ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] && bin_rem [ j ] - weight [ i ] > mx ) { wi = j ; mx = bin_rem [ j ] - weight [ i ] ; } } if ( mx == - 1 ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; } bin_rem [ wi ] -= weight [ i ] ; } return res ; } public static void main ( String [ ] args ) { int weight [ ] = { 2 , 5 , 4 , 7 , 1 , 3 , 8 } ; int c = 10 ; int n = weight . length ; System . out . print ( " Number ▁ of ▁ bins ▁ required ▁ in ▁ Worst ▁ Fit ▁ : ▁ " + worstFit ( weight , n , c ) ) ; } }
Find minimum time to finish all jobs with given constraints | Java program to find minimum time to finish all jobs with given number of assignees ; Utility function to get maximum element in job [ 0. . n - 1 ] ; Returns true if it is possible to finish jobs [ ] within given time ' time ' ; cnt is count of current assignees required for jobs ; time assigned to current assignee ; If time assigned to current assignee exceeds max , increment count of assignees . ; Else add time of job to current time and move to next job . ; Returns true if count is smaller than k ; Returns minimum time required to finish given array of jobs k -- > number of assignees T -- > Time required by every assignee to finish 1 unit m -- > Number of jobs ; Set start and end for binary search end provides an upper limit on time ; Initialize answer ; Find the job that takes maximum time ; Do binary search for minimum feasible time ; If it is possible to finish jobs in mid time ; Update answer ; Driver program
class GFG { static int getMax ( int arr [ ] , int n ) { int result = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > result ) result = arr [ i ] ; return result ; } static boolean isPossible ( int time , int K , int job [ ] , int n ) { int cnt = 1 ; int curr_time = 0 ; for ( int i = 0 ; i < n ; ) { if ( curr_time + job [ i ] > time ) { curr_time = 0 ; cnt ++ ; } else { curr_time += job [ i ] ; i ++ ; } } return ( cnt <= K ) ; } static int findMinTime ( int K , int T , int job [ ] , int n ) { int end = 0 , start = 0 ; for ( int i = 0 ; i < n ; ++ i ) end += job [ i ] ; int ans = end ; int job_max = getMax ( job , n ) ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( mid >= job_max && isPossible ( mid , K , job , n ) ) { ans = Math . min ( ans , mid ) ; end = mid - 1 ; } else start = mid + 1 ; } return ( ans * T ) ; } public static void main ( String arg [ ] ) { int job [ ] = { 10 , 7 , 8 , 12 , 6 , 8 } ; int n = job . length ; int k = 4 , T = 5 ; System . out . println ( findMinTime ( k , T , job , n ) ) ; } }
Longest subarray with all even or all odd elements | Java program for the above approach ; Function to calculate longest substring with odd or even elements ; Initializing dp [ ] ; Initializing dp [ 0 ] with 1 ; ans will store the final answer ; Traversing the array from index 1 to N - 1 ; Checking both current and previous element is even or odd ; Updating dp [ i ] with dp [ i - 1 ] + 1 ; Storing max element to ans ; Returning the final answer ; Driver Code ; Input ; Function call
import java . io . * ; class GFG { static int LongestOddEvenSubarray ( int A [ ] , int N ) { int dp [ ] = new int [ N ] ; dp [ 0 ] = 1 ; int ans = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( ( A [ i ] % 2 == 0 && A [ i - 1 ] % 2 == 0 ) || ( A [ i ] % 2 != 0 && A [ i - 1 ] % 2 != 0 ) ) { dp [ i ] = dp [ i - 1 ] + 1 ; } else dp [ i ] = 1 ; } for ( int i = 0 ; i < N ; i ++ ) ans = Math . max ( ans , dp [ i ] ) ; return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 5 , 7 , 2 , 4 , 6 , 8 , 3 } ; int N = A . length ; System . out . println ( LongestOddEvenSubarray ( A , N ) ) ; } }