codeparrot/xlcost-text-to-code huggingface dataset:
Rearrange characters in a sorted string such that no pair of adjacent characters are the same | Java program for the above approach ; Function to check if a String S contains pair of adjacent characters that are equal or not ; Traverse the String S ; If S [ i ] and S [ i + 1 ] are equal ; Otherwise , return false ; Function to rearrange characters of a String such that no pair of adjacent characters are the same ; Initialize 3 variables ; Iterate until k < N ; If S [ i ] is not equal to S [ j ] ; Increment i and j by 1 ; If j equals k and increment the value of K by 1 ; Else ; If S [ j ] equals S [ k ] ; Increment k by 1 ; Else ; Swap ; Increment i and j by 1 ; If j equals k ; Increment k by 1 ; Function to rearrange characters in a String so that no two adjacent characters are same ; If String is already valid ; If size of the String is 2 ; Function Call ; Reversing the String ; Function Call ; If the String is valid ; Otherwise ; Driver Code
import java . util . * ; class GFG { static char [ ] S = " aaabc " . toCharArray ( ) ; static boolean isAdjChar ( ) { for ( int i = 0 ; i < S . length - 1 ; i ++ ) { if ( S [ i ] == S [ i + 1 ] ) return true ; } return false ; } static void rearrangeStringUtil ( int N ) { int i = 0 , j = 1 , k = 2 ; while ( k < N ) { if ( S [ i ] != S [ j ] ) { i ++ ; j ++ ; if ( j == k ) { k ++ ; } } else { if ( S [ j ] == S [ k ] ) { k ++ ; } else { swap ( k , j ) ; i ++ ; j ++ ; if ( j == k ) { k ++ ; } } } } } static void swap ( int i , int j ) { char temp = S [ i ] ; S [ i ] = S [ j ] ; S [ j ] = temp ; } static String rearrangeString ( int N ) { if ( isAdjChar ( ) == false ) { return String . valueOf ( S ) ; } if ( S . length == 2 ) return " - 1" ; rearrangeStringUtil ( N ) ; reverse ( ) ; rearrangeStringUtil ( N ) ; if ( isAdjChar ( ) == false ) { return String . valueOf ( S ) ; } return " - 1" ; } static void reverse ( ) { int l , r = S . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = S [ l ] ; S [ l ] = S [ r ] ; S [ r ] = temp ; } } public static void main ( String [ ] args ) { int N = S . length ; System . out . print ( rearrangeString ( N ) ) ; } }
Lexicographically largest string possible by repeatedly appending first character of two given strings | Java program for the above approach ; Function to make the lexicographically largest string by merging two strings ; Stores the resultant string ; If the string word1 is lexographically greater than or equal to word ; Update the string merge ; Erase the first index of the string word1 ; Otherwise ; Update the string merge ; Erase the first index of the string word2 ; Return the final string ; Driver Code
import java . io . * ; class GFG { static String largestMerge ( String word1 , String word2 ) { String merge = " " ; while ( word1 . length ( ) != 0 || word2 . length ( ) != 0 ) { if ( word1 . compareTo ( word2 ) == 0 || ( word1 . compareTo ( word2 ) > 0 ) ) { merge = merge + word1 . charAt ( 0 ) ; word1 = word1 . substring ( 1 ) ; } else { merge = merge + word2 . charAt ( 0 ) ; word2 = word2 . substring ( 1 ) ; } } return merge ; } public static void main ( String [ ] args ) { String S1 = " xyzxyz " ; String S2 = " xywzxyx " ; System . out . println ( largestMerge ( S1 , S2 ) ) ; } }
Maximum rods to put horizontally such that no two rods overlap on X coordinate | Java program for the above approach ; Function to find the maximum number of rods that can be put horizontally ; Stores the result ; Stores the last occupied point ; Traverse the array arr [ ] ; If the current point can be put on the left side ; Increment the ans by 1 ; Update prev ; Else if the given point can be put on the right side ; Increment the ans by 1 ; Update prev ; Otherwise , ; Update prev ; Return the ans ; Driver Code
class GFG { public static int findMaximumPoints ( int N , int X [ ] , int H [ ] ) { int ans = 0 ; int prev = Integer . MIN_VALUE ; for ( int i = 0 ; i < N ; ++ i ) { if ( prev < ( X [ i ] - H [ i ] ) ) { ++ ans ; prev = X [ i ] ; } else if ( i == N - 1 || ( X [ i ] + H [ i ] ) < X [ i + 1 ] ) { ++ ans ; prev = X [ i ] + H [ i ] ; } else { prev = X [ i ] ; } } return ans ; } public static void main ( String args [ ] ) { int X [ ] = { 1 , 2 , 3 } ; int H [ ] = { 2 , 5 , 5 } ; int N = X . length ; System . out . println ( findMaximumPoints ( N , X , H ) ) ; } }
Maximize the missing values in given time in HH : MM format | Java program for the above approach ; Function to find the maximum time by replacing ' ? ' by any digits ; Convert the string to the character array ; If the 0 th index is ' ? ' ; If the 1 st index is ' ? ' ; If the 3 rd index is ' ? ' ; If the 4 th index is ' ? ' ; Return new string ; Driver Code
public class Main { public static void maxTime ( String S ) { char [ ] s = S . toCharArray ( ) ; if ( s [ 0 ] == ' ? ' ) { if ( s [ 1 ] <= '3' s [ 1 ] == ' ? ' ) s [ 0 ] = '2' ; else s [ 0 ] = '1' ; } if ( s [ 1 ] == ' ? ' ) { if ( s [ 0 ] != '2' ) { s [ 1 ] = 9 ; } else s [ 1 ] = 3 ; } if ( s [ 3 ] == ' ? ' ) s [ 3 ] = '5' ; if ( s [ 4 ] == ' ? ' ) s [ 4 ] = '9' ; System . out . println ( new String ( s ) ) ; } public static void main ( String [ ] args ) { String S = " ? 4:5 ? " ; maxTime ( S ) ; } }
Maximum GCD of two numbers possible by adding same value to them | Java implementation of above approach . ; Function to calculate maximum gcd of two numbers possible by adding same value to both a and b ; Driver Code ; Given Input
import java . io . * ; class GFG { static void maxGcd ( int a , int b ) { System . out . println ( Math . abs ( a - b ) ) ; } public static void main ( String [ ] args ) { int a = 2231 ; int b = 343 ; maxGcd ( a , b ) ; } }
Count the combination of 4 s and / or 5 s required to make each Array element 0 | Java program for the above approach ; Function to print the count of the combination of 4 or 5 required to make the arr [ i ] for each 0 < i < N ; Vector to store the answer ; Iterate in the range [ 0 , N - 1 ] ; Initialize sum to store the count of numbers and cnt for the current factor of 4 ; Iterate in the range [ 0 , arr [ i ] ] with increment of 4 ; Check if arr [ i ] - j ( the current factor of 4 ) is divisible by 5 or not ; If sum is not maximum then answer is found ; Finally , print the required answer ; Driver Code ; Given Input ; Function Call
import java . io . * ; import java . util . Arrays ; class GFG { static void sumOfCombinationOf4OR5 ( int [ ] arr , int N ) { int [ ] ans = new int [ N ] ; Arrays . fill ( ans , - 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 4 ) { continue ; } int sum = Integer . MAX_VALUE ; int cnt = 0 ; for ( int j = 0 ; j <= arr [ i ] ; j += 4 ) { if ( ( arr [ i ] - j ) % 5 == 0 ) { sum = Math . min ( sum , cnt + ( arr [ i ] - j ) / 5 ) ; } cnt ++ ; } if ( sum != Integer . MAX_VALUE ) ans [ i ] = sum ; } for ( int num : ans ) System . out . printf ( num + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 7 , 15 , 17 , 22 } ; int N = arr . length ; sumOfCombinationOf4OR5 ( arr , N ) ; } }
Find an N | Java program for the above approach ; Function to find an N - length binary string having maximum sum of elements from all given ranges ; Iterate over the range [ 1 , N ] ; If i is odd , then print 0 ; Otherwise , print 1 ; Driver Code ; Function Call
import java . io . * ; class GFG { static void printBinaryString ( int arr [ ] [ ] , int N ) { for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 1 ) { System . out . print ( 0 ) ; } else { System . out . print ( 1 ) ; } } } public static void main ( String [ ] args ) { int N = 5 , M = 3 ; int arr [ ] [ ] = { { 1 , 3 } , { 2 , 4 } , { 2 , 5 } } ; printBinaryString ( arr , N ) ; } }
Minimum cost to complete given tasks if cost of 1 , 7 and 30 days are given | Java program for the above approach ; Function to find the minimum cost to hire the workers for the given days in the array days [ ] ; Initialize the array dp ; Minimum Cost for Nth day ; Pointer of the array arr [ ] ; Traverse from right to left ; If worker is hired for 1 day ; If worker is hired for 7 days ; If worker is hired for 30 days ; Update the value of dp [ i ] as minimum of 3 options ; If the day is not at the array arr [ ] ; Return the answer ; Driver Code
public class GFG { static int MinCost ( int days [ ] , int cost [ ] , int N ) { int size = days [ N - 1 ] + 1 ; int [ ] dp = new int [ size ] ; dp [ size - 1 ] = Math . min ( cost [ 0 ] , Math . min ( cost [ 1 ] , cost [ 2 ] ) ) ; int ptr = N - 2 ; for ( int i = size - 2 ; i > 0 ; i -- ) { if ( ptr >= 0 && days [ ptr ] == i ) { int val1 = dp [ i + 1 ] + cost [ 0 ] ; int val2 = cost [ 1 ] + ( ( i + 7 >= size ) ? 0 : dp [ i + 7 ] ) ; int val3 = cost [ 2 ] + ( ( i + 30 >= size ) ? 0 : dp [ i + 30 ] ) ; dp [ i ] = Math . min ( val1 , Math . min ( val2 , val3 ) ) ; ptr -- ; } else { dp [ i ] = dp [ i + 1 ] ; } } return dp [ 1 ] ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 4 , 6 , 7 , 8 , 10 , 17 } ; int cost [ ] = { 3 , 8 , 20 } ; int N = arr . length ; System . out . println ( MinCost ( arr , cost , N ) ) ; } }
Maximum Pairs of Bracket Sequences which can be concatenated to form a Regular Bracket Sequence | Java program for the above approach ; Function to count the number of pairs whose concatenation results in the regular bracket sequence ; Stores the count of opening and closing parenthesis for each string arr [ i ] ; Stores maximum count of pairs ; Traverse the array arr [ ] ; Traverse the string c [ ] ; Opening Bracket ; Otherwise , Closing Bracket ; Count of closing brackets needed to balance string ; Count of opening brackets needed to balance string ; Add the count of balanced sequences ; Traverse the array ; Print the resultant count ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void countPairs ( int N , ArrayList < String > arr ) { int open [ ] = new int [ 100 ] ; int close [ ] = new int [ 100 ] ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { char c [ ] = arr . get ( i ) . toCharArray ( ) ; int d = 0 ; int min = 0 ; for ( int j = 0 ; j < c . length ; j ++ ) { if ( c [ j ] == ' ( ' ) { d ++ ; } else { d -- ; if ( d < min ) min = d ; } } if ( d >= 0 ) { if ( min == 0 ) close [ d ] ++ ; } else if ( d < 0 ) { if ( min == d ) open [ - d ] ++ ; } } ans += close [ 0 ] / 2 ; for ( int i = 1 ; i < 100 ; i ++ ) { ans += Math . min ( open [ i ] , close [ i ] ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 7 ; ArrayList < String > list = new ArrayList < String > ( ) ; list . add ( " ) ( ) ) " ) ; list . add ( " ) " ) ; list . add ( " ( ( " ) ; list . add ( " ( ( " ) ; list . add ( " ( " ) ; list . add ( " ) " ) ; list . add ( " ) " ) ; countPairs ( N , list ) ; } }
Minimum count of elements to be inserted in Array to form all values in [ 1 , K ] using subset sum | Java program for the above approach ; Function to find the count of minimum elements to be inserted to form every number in a range ; Stores the count of numbers needed ; Stores the numbers upto which every numbers can be formed ; Stores the index of the array arr [ ] ; Iterate until requiredSum is less than or equal to K ; If i is less than N and requiredSum is greater than or equal to arr [ i ] ; Increment requiredSum by arr [ i ] ; Increment i by 1 ; Otherwise ; Increment count by 1 ; Increment requiredSum by requiredSum ; Return result ; Driver Code ; Input ; Function Call
import java . io . * ; class GFG { public static int minElements ( int arr [ ] , int N , int K ) { int count = 0 ; long requiredNum = 1 ; int i = 0 ; while ( requiredNum <= K ) { if ( i < N && requiredNum >= arr [ i ] ) { requiredNum += arr [ i ] ; i ++ ; } else { count ++ ; requiredNum += requiredNum ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 } ; int K = 6 ; int N = arr . length ; System . out . println ( minElements ( arr , N , K ) ) ; } }
Maximize the count of adjacent element pairs with even sum by rearranging the Array | package whatever do not write package name here ; Function to find maximum count pair of adjacent elements with even sum ; Stores count of odd numbers ; Stores count of even numbers ; Traverse the array arr [ ] ; If arr [ i ] % 2 is 1 ; Else ; If odd and even both are greater than 0 ; Otherwise ; Driver Code
import java . io . * ; class GFG { static int maximumCount ( int arr [ ] , int N ) { int odd = 0 ; int even = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd ++ ; else even ++ ; } if ( odd > 0 && even > 0 ) return N - 2 ; else return N - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 13 , 15 , 3 , 16 , 9 , 13 , 18 } ; int N = arr . length ; System . out . println ( maximumCount ( arr , N ) ) ; } }
Smallest number possible by repeatedly multiplying with K or 2 exactly N times starting from 1 | Java program for the above approach ; Function to find the minimum value of X after increment X by K or twice value of X in each of N operations ; Iterate over the range [ 1 , N ] ; If the value of X is less than equal to K ; Otherwise ; Return the minimum value of X ; Driver Code
import java . io . * ; class GFG { public static int minPossibleValue ( int N , int K , int X ) { for ( int i = 1 ; i <= N ; i ++ ) { if ( X <= K ) { X = X * 2 ; } else { X = X + K ; } } return X ; } public static void main ( String [ ] args ) { int N = 7 , K = 4 , X = 1 ; System . out . println ( minPossibleValue ( N , K , X ) ) ; } }
Smallest number that can replace all | Java program for the above approach ; Function to find the value of K to minimize the value of maximum absolute difference between adjacent elements ; Stores the maximum and minimum among array elements that are adjacent to " - 1" ; Traverse the given array arr [ ] ; If arr [ i ] is - 1 & arr [ i + 1 ] is not - 1 ; If arr [ i + 1 ] is - 1 & arr [ i ] is not - 1 ; If all array element is - 1 ; Otherwise ; Driver Code
import java . io . * ; class GFG { public static void findMissingValue ( int arr [ ] , int N ) { int minE = Integer . MAX_VALUE , maxE = Integer . MIN_VALUE ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] == - 1 && arr [ i + 1 ] != - 1 ) { minE = Math . min ( minE , arr [ i + 1 ] ) ; maxE = Math . max ( maxE , arr [ i + 1 ] ) ; } if ( arr [ i ] != - 1 && arr [ i + 1 ] == - 1 ) { minE = Math . min ( minE , arr [ i ] ) ; maxE = Math . max ( maxE , arr [ i ] ) ; } } if ( minE == Integer . MAX_VALUE && maxE == Integer . MIN_VALUE ) { System . out . println ( "0" ) ; } else { System . out . println ( ( minE + maxE ) / 2 ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , - 1 , - 1 , - 1 , 5 } ; int N = arr . length ; findMissingValue ( arr , N ) ; } }
Last element of an array after repeatedly removing the first element and appending it to the end of the array twice exactly K times | Java program for the above approach ; Function to find the last element after performing given operations ; Length of the array ; Increment j until condition is satisfied ; In each pair every value is repeating r number of times ; Print the result according to the value of k ; Driver Code ; Given K ; Given arr [ ] ; Function call
import java . io . * ; class GFG { static void findLastElement ( int N , int [ ] A ) { int l = A . length ; int j = 0 ; while ( N > l * ( int ) ( Math . pow ( 2 , j ) ) ) { N = N - l * ( int ) Math . pow ( 2 , j ) ; j += 1 ; } int k = 1 ; int r = ( int ) Math . pow ( 2 , j ) ; for ( int i = 1 ; i < l ; i ++ ) { if ( N > r * i ) k += 1 ; } for ( int i = 0 ; i < l ; i ++ ) { if ( i + 1 == k ) { System . out . print ( A [ i ] ) ; return ; } } } public static void main ( String [ ] args ) { int K = 7 ; int [ ] A = { 1 , 2 , 3 } ; findLastElement ( K , A ) ; } }
Minimum possible value of D which when added to or subtracted from K repeatedly obtains every array element | Java program for the above approach ; Recursive function tox previous gcd of a and b ; Function to find the maximum value of D such that every element in the array can be obtained by performing K + D or K - D ; Traverse the array arr [ ] ; Update arr [ i ] ; Stores GCD of the array ; Iterate over the range [ 1 , N ] ; Update the value of D ; Print the value of D ; Driver Code
import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int findMaxD ( int arr [ ] , int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = Math . abs ( arr [ i ] - K ) ; } int D = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { D = gcd ( D , arr [ i ] ) ; } return D ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 7 , 11 } ; int N = arr . length ; int K = 3 ; System . out . print ( findMaxD ( arr , N , K ) ) ; } }
Maximize the number of times a character can be removed from substring 01 from given Binary String | Java program for the above approach ; Function to find the maximum moves that can be performed on a string ; Stores 0 s in suffix ; Stores 1 s in prefix ; Iterate over the characters of the string ; Iterate until i is greater than or equal to 0 ; If N is equal to x + y ; Return answer ; Driver code ; Input ; Function call
import java . io . * ; class GFG { static int maxOperations ( String S , int N ) { int X = 0 ; int Y = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == '0' ) break ; Y ++ ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( S . charAt ( i ) == '1' ) break ; X ++ ; } if ( N == X + Y ) return 0 ; return N - ( X + Y ) - 1 ; } public static void main ( String [ ] args ) { String S = "001111" ; int N = S . length ( ) ; System . out . println ( maxOperations ( S , N ) ) ; } }
Maximize frequency sum of K chosen characters from given string | Java program for the above approach ; Function to find the maximum sum of frequencies of the exactly K chosen characters from the String S ; Stores the resultant maximum sum ; Stores the frequency of array elements ; Find the frequency of character ; Sort the frequency array in the descending order ; Iterate to choose K elements greedily ; If the freq [ i ] cards are chosen ; K cards have been picked ; Return the resultant sum ; Driver Code
import java . util . * ; class GFG { static int maximumSum ( String S , int N , int K ) { int sum = 0 ; Integer [ ] freq = new Integer [ 256 ] ; Arrays . fill ( freq , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) { freq [ ( int ) S . charAt ( i ) ] += 1 ; } Arrays . sort ( freq , Collections . reverseOrder ( ) ) ; for ( int i = 0 ; i < 256 ; i ++ ) { if ( K > freq [ i ] ) { sum += freq [ i ] * freq [ i ] ; K -= freq [ i ] ; } else { sum += freq [ i ] * K ; break ; } } return sum ; } public static void main ( String args [ ] ) { String S = " GEEKSFORGEEKS " ; int K = 10 ; int N = S . length ( ) ; System . out . print ( maximumSum ( S , N , K ) ) ; } }
Count of N | Java program for the above approach ; Function to count N - digit numbers having absolute difference between adjacent digits in non - increasing order ; If digit = n + 1 , a valid n - digit number has been formed ; If the state has already been computed ; If the current digit is 1 , then any digit from [ 1 - 9 ] can be placed ; If the current digit is 2 , any digit from [ 0 - 9 ] can be placed ; For other digits , any digit i can be placed which satisfies abs ( prev1 - i ) <= abs ( prev1 - prev2 ) ; If absolute difference is less than or equal to diff ; Function to count N - digit numbers with absolute difference between adjacent digits in non increasing order ; Initialize dp table with - 1 ; Function Call ; Driver code
import java . io . * ; class GFG { static int dp [ ] [ ] [ ] = new int [ 100 ] [ 10 ] [ 10 ] ; static int countOfNumbers ( int digit , int prev1 , int prev2 , int n ) { if ( digit == n + 1 ) { return 1 ; } int val = dp [ digit ] [ prev1 ] [ prev2 ] ; if ( val != - 1 ) { return val ; } val = 0 ; if ( digit == 1 ) { for ( int i = ( n == 1 ? 0 : 1 ) ; i <= 9 ; ++ i ) { val += countOfNumbers ( digit + 1 , i , prev1 , n ) ; } } else if ( digit == 2 ) { for ( int i = 0 ; i <= 9 ; ++ i ) { val += countOfNumbers ( digit + 1 , i , prev1 , n ) ; } } else { int diff = Math . abs ( prev2 - prev1 ) ; for ( int i = 0 ; i <= 9 ; ++ i ) { if ( Math . abs ( prev1 - i ) <= diff ) { val += countOfNumbers ( digit + 1 , i , prev1 , n ) ; } } } return val ; } static void countNumbersUtil ( int N ) { for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { for ( int k = 0 ; k < 10 ; k ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } System . out . println ( countOfNumbers ( 1 , 0 , 0 , N ) ) ; } public static void main ( String [ ] args ) { int N = 3 ; countNumbersUtil ( N ) ; } }
Print all numbers that can be obtained by adding A or B to N exactly M times | Java program for the above approach ; Function to find all possible numbers that can be obtained by adding A or B to N exactly N times ; If number of steps is 0 and only possible number is N ; Add A to N and make a recursive call for M - 1 steps ; Add B to N and make a recursive call for M - 1 steps . ; Driver Code ; Given Inputs ; Stores all possible numbers ; Function call ; Print all possible numbers
import java . util . * ; public class MyClass { static void possibleNumbers ( Set < Integer > numbers , int N , int M , int A , int B ) { if ( M == 0 ) { numbers . add ( N ) ; return ; } possibleNumbers ( numbers , N + A , M - 1 , A , B ) ; possibleNumbers ( numbers , N + B , M - 1 , A , B ) ; } public static void main ( String args [ ] ) { int N = 5 , M = 3 , A = 4 , B = 6 ; Set < Integer > numbers = new HashSet < Integer > ( ) ; possibleNumbers ( numbers , N , M , A , B ) ; Iterator < Integer > i = numbers . iterator ( ) ; while ( i . hasNext ( ) ) System . out . print ( i . next ( ) + " ▁ " ) ; } }
Maximum sum of array after removing a positive or negative subarray | Java program for the above approach ; Function to find the maximum sum of array after removing either the contiguous positive or negative elements ; Store the total sum of array ; Store the maximum contiguous negative sum ; Store the sum of current contiguous negative elements ; Store the minimum element of array ; Traverse the array , arr [ ] ; Update the overall sum ; Store minimum element of array ; If arr [ i ] is positive ; Update temp_sum to 0 ; Add arr [ i ] to temp_sum ; Update max_neg ; If no negative element in array then remove smallest positive element ; Print the required sum ; Driver Code ; Given Input ; Function Call
import java . io . * ; class GFG { static void maxSum ( int arr [ ] , int n ) { int sum = 0 ; int max_neg = Integer . MAX_VALUE ; int tempsum = 0 ; int small = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; small = Math . min ( small , arr [ i ] ) ; if ( arr [ i ] > 0 ) { tempsum = 0 ; } else { tempsum += arr [ i ] ; } max_neg = Math . min ( max_neg , tempsum ) ; } if ( max_neg == 0 ) { max_neg = small ; } System . out . println ( sum - max_neg ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int n = arr . length ; maxSum ( arr , n ) ; } }
Lexicographically largest string possible by at most K replacements | Java implementation of the above approach ; Traverse each element of the string ; If the current character can be replaced with ' z ' ; Return the modified string ; Driver code
class GFG { static String largestString ( String s , int k ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) != ' z ' && k > 0 ) { s = s . replace ( s . charAt ( i ) , ' z ' ) ; k -- ; } } return s ; } public static void main ( String args [ ] ) { String s = " dbza " ; int k = 1 ; System . out . println ( largestString ( s , k ) ) ; } }