codeparrot/xlcost-text-to-code huggingface dataset:
Maximum possible sub | Java implementation of the approach ; Function to return the maximum sub - array sum after at most x swaps ; To store the required answer ; For all possible intervals ; Keep current ans as zero ; To store the integers which are not part of the sub - array currently under consideration ; To store elements which are part of the sub - array currently under consideration ; Create two sets ; Swap at most X elements ; Remove the minimum of the taken elements ; Add maximum of the discarded elements ; Update the answer ; Return the maximized sub - array sum ; Driver code .
import java . io . * ; import java . util . * ; class GFG { static int SubarraySum ( int [ ] a , int n , int x ) { int ans = - 10000 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int curans = 0 ; ArrayList < Integer > pq = new ArrayList < Integer > ( ) ; ArrayList < Integer > pq2 = new ArrayList < Integer > ( ) ; for ( int k = 0 ; k < n ; k ++ ) { if ( k >= i && k <= j ) { curans += a [ k ] ; pq2 . add ( a [ k ] ) ; } else pq . add ( a [ k ] ) ; } Collections . sort ( pq ) ; Collections . reverse ( pq ) ; Collections . sort ( pq2 ) ; ans = Math . max ( ans , curans ) ; for ( int k = 1 ; k <= x ; k ++ ) { if ( pq . size ( ) == 0 || pq2 . size ( ) == 0 || pq2 . get ( 0 ) >= pq . get ( 0 ) ) break ; curans -= pq2 . get ( 0 ) ; pq2 . remove ( 0 ) ; curans += pq . get ( 0 ) ; pq . remove ( 0 ) ; ans = Math . max ( ans , curans ) ; } } } return ans ; } public static void main ( String [ ] args ) { int [ ] a = { 5 , - 1 , 2 , 3 , 4 , - 2 , 5 } ; int x = 2 ; int n = a . length ; System . out . println ( SubarraySum ( a , n , x ) ) ; } }
Generate an array of size K which satisfies the given conditions | Java implementation of the approach ; Function to generate and print the required array ; Initializing the array ; Finding r ( from above approach ) ; If r < 0 ; Finding ceiling and floor values ; Fill the array with ceiling values ; Fill the array with floor values ; Add 1 , 2 , 3 , ... with corresponding values ; There is no solution for below cases ; Modify A [ 1 ] and A [ k - 1 ] to get the required array ; Driver Code
class GFG { static void generateArray ( int n , int k ) { int [ ] array = new int [ k ] ; int remaining = n - ( k * ( k + 1 ) / 2 ) ; if ( remaining < 0 ) System . out . print ( " NO " ) ; int right_most = remaining % k ; int high = ( int ) Math . ceil ( remaining / ( k * 1.0 ) ) ; int low = ( int ) Math . floor ( remaining / ( k * 1.0 ) ) ; for ( int i = k - right_most ; i < k ; i ++ ) array [ i ] = high ; for ( int i = 0 ; i < ( k - right_most ) ; i ++ ) array [ i ] = low ; for ( int i = 0 ; i < k ; i ++ ) array [ i ] += i + 1 ; if ( k - 1 != remaining k == 1 ) { for ( int u : array ) System . out . print ( u + " ▁ " ) ; } else if ( k == 2 k == 3 ) System . out . printf ( "-1NEW_LINE"); else { array [ 1 ] -= 1 ; array [ k - 1 ] += 1 ; for ( int u : array ) System . out . print ( u + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 26 , k = 6 ; generateArray ( n , k ) ; } }
Maximize the given number by replacing a segment of digits with the alternate digits given | Java implementation of the approach ; Function to return the maximized number ; Iterate till the end of the string ; Check if it is greater or not ; Replace with the alternate till smaller ; Return original s in case no change took place ; Driver Code
class GFG { static String get_maximum ( char [ ] s , int a [ ] ) { int n = s . length ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] - '0' < a [ s [ i ] - '0' ] ) { int j = i ; while ( j < n && ( s [ j ] - '0' <= a [ s [ j ] - '0' ] ) ) { s [ j ] = ( char ) ( '0' + a [ s [ j ] - '0' ] ) ; j ++ ; } return String . valueOf ( s ) ; } } return String . valueOf ( s ) ; } public static void main ( String [ ] args ) { String s = "1337" ; int a [ ] = { 0 , 1 , 2 , 5 , 4 , 6 , 6 , 3 , 1 , 9 } ; System . out . println ( get_maximum ( s . toCharArray ( ) , a ) ) ; } }
Number of times the largest perfect square number can be subtracted from N | Java implementation of the approach ; Function to return the count of steps ; Variable to store the count of steps ; Iterate while N > 0 ; Get the largest perfect square and subtract it from N ; Increment steps ; Return the required count ; Driver code
import java . lang . Math ; public class GfG { static int countSteps ( int n ) { int steps = 0 ; while ( n > 0 ) { int largest = ( int ) Math . sqrt ( n ) ; n -= ( largest * largest ) ; steps ++ ; } return steps ; } public static void main ( String [ ] args ) { int n = 85 ; System . out . println ( countSteps ( n ) ) ; } }
Maximum array sum that can be obtained after exactly k changes | Java implementation of the above approach ; Utility function to return the sum of the array elements ; Function to return the maximized sum of the array after performing the given operation exactly k times ; Sort the array elements ; Change signs of the negative elements starting from the smallest ; If a single operation has to be performed then it must be performed on the smallest positive element ; To store the index of the minimum element ; Update the minimum index ; Perform remaining operation on the smallest element ; Return the sum of the updated array ; Driver code
import java . util . Arrays ; class GFG { static int sumArr ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } static int maxSum ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int i = 0 ; while ( i < n && k > 0 && arr [ i ] < 0 ) { arr [ i ] *= - 1 ; k -- ; i ++ ; } if ( k % 2 == 1 ) { int min = 0 ; for ( i = 1 ; i < n ; i ++ ) if ( arr [ min ] > arr [ i ] ) min = i ; arr [ min ] *= - 1 ; } return sumArr ( arr , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 5 , 4 , 1 , 3 , 2 } ; int n = arr . length ; int k = 4 ; System . out . println ( maxSum ( arr , n , k ) ) ; } }
Spanning Tree With Maximum Degree ( Using Kruskal 's Algorithm) | Java implementation of the approach ; par and rank will store the parent and rank of particular node in the Union Find Algorithm ; Find function of Union Find Algorithm ; Union function of Union Find Algorithm ; Function to find the required spanning tree ; Initialising parent of a node by itself ; Variable to store the node with maximum degree ; Finding the node with maximum degree ; Union of all edges incident on vertex with maximum degree ; Carrying out normal Kruskal Algorithm ; Driver code ; Number of nodes ; Number of edges ; ArrayList to store the graph ; Array to store the degree of each node in the graph ; Add edges and update degrees
import java . util . * ; public class GFG { static int par [ ] , rank [ ] ; static int find ( int x ) { if ( par [ x ] != x ) par [ x ] = find ( par [ x ] ) ; return par [ x ] ; } static void union ( int u , int v ) { int x = find ( u ) ; int y = find ( v ) ; if ( x == y ) return ; if ( rank [ x ] > rank [ y ] ) par [ y ] = x ; else if ( rank [ x ] < rank [ y ] ) par [ x ] = y ; else { par [ x ] = y ; rank [ y ] ++ ; } } static void findSpanningTree ( int deg [ ] , int n , int m , ArrayList < Integer > g [ ] ) { par = new int [ n + 1 ] ; rank = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) par [ i ] = i ; int max = 1 ; for ( int i = 2 ; i <= n ; i ++ ) if ( deg [ i ] > deg [ max ] ) max = i ; for ( int v : g [ max ] ) { System . out . println ( max + " ▁ " + v ) ; union ( max , v ) ; } for ( int u = 1 ; u <= n ; u ++ ) { for ( int v : g [ u ] ) { int x = find ( u ) ; int y = find ( v ) ; if ( x == y ) continue ; union ( x , y ) ; System . out . println ( u + " ▁ " + v ) ; } } } public static void main ( String args [ ] ) { int n = 5 ; int m = 5 ; ArrayList < Integer > g [ ] = new ArrayList [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) g [ i ] = new ArrayList < > ( ) ; int deg [ ] = new int [ n + 1 ] ; g [ 1 ] . add ( 2 ) ; g [ 2 ] . add ( 1 ) ; deg [ 1 ] ++ ; deg [ 2 ] ++ ; g [ 1 ] . add ( 5 ) ; g [ 5 ] . add ( 1 ) ; deg [ 1 ] ++ ; deg [ 5 ] ++ ; g [ 2 ] . add ( 3 ) ; g [ 3 ] . add ( 2 ) ; deg [ 2 ] ++ ; deg [ 3 ] ++ ; g [ 5 ] . add ( 3 ) ; g [ 3 ] . add ( 5 ) ; deg [ 3 ] ++ ; deg [ 5 ] ++ ; g [ 3 ] . add ( 4 ) ; g [ 4 ] . add ( 3 ) ; deg [ 3 ] ++ ; deg [ 4 ] ++ ; findSpanningTree ( deg , n , m , g ) ; } }
Given count of digits 1 , 2 , 3 , 4 , find the maximum sum possible | Java program to maximum possible sum ; Function to find the maximum possible sum ; To store required sum ; Number of 234 's can be formed ; Sum obtained with 234 s ; Remaining 2 's ; Sum obtained with 12 s ; Return the required sum ; Driver code
class GFG { static int Maxsum ( int c1 , int c2 , int c3 , int c4 ) { int sum = 0 ; int two34 = Math . min ( c2 , Math . min ( c3 , c4 ) ) ; sum = two34 * 234 ; c2 -= two34 ; sum += Math . min ( c2 , c1 ) * 12 ; return sum ; } public static void main ( String [ ] args ) { int c1 = 5 , c2 = 2 , c3 = 3 , c4 = 4 ; System . out . println ( Maxsum ( c1 , c2 , c3 , c4 ) ) ; } }
Replace all elements by difference of sums of positive and negative numbers after that element | Java program to implement above approach ; Function to print the array elements ; Function to replace all elements with absolute difference of absolute sums of positive and negative elements ; calculate difference of both sums ; if i - th element is positive , add it to positive sum ; if i - th element is negative , add it to negative sum ; replace i - th elements with absolute difference ; Driver Code
class GFG { static void printArray ( int N , int arr [ ] ) { for ( int i = 0 ; i < N ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } static void replacedArray ( int N , int arr [ ] ) { int pos_sum , neg_sum , i , j , diff ; pos_sum = 0 ; neg_sum = 0 ; for ( i = N - 1 ; i >= 0 ; i -- ) { diff = Math . abs ( pos_sum ) - Math . abs ( neg_sum ) ; if ( arr [ i ] > 0 ) pos_sum += arr [ i ] ; else neg_sum += arr [ i ] ; arr [ i ] = Math . abs ( diff ) ; } } public static void main ( String [ ] args ) { int N = 5 ; int arr [ ] = { 1 , - 1 , 2 , 3 , - 2 } ; replacedArray ( N , arr ) ; printArray ( N , arr ) ; N = 6 ; int arr1 [ ] = { - 3 , - 4 , - 2 , 5 , 1 , - 2 } ; replacedArray ( N , arr1 ) ; printArray ( N , arr1 ) ; } }
Count of pairs from 1 to a and 1 to b whose sum is divisible by N | Java implementation of above approach ; Function to find the distinct pairs from 1 - a & 1 - b such that their sum is divisible by n . ; pairs from 1 to n * ( a / n ) and 1 to n * ( b / n ) ; pairs from 1 to n * ( a / n ) and n * ( b / n ) to b ; pairs from n * ( a / n ) to a and 1 to n * ( b / n ) ; pairs from n * ( a / n ) to a and n * ( b / n ) to b ; Return answer ; Driver code
import java . io . * ; class GFG { static int findCountOfPairs ( int a , int b , int n ) { int ans = 0 ; ans += n * ( a / n ) * ( b / n ) ; ans += ( a / n ) * ( b % n ) ; ans += ( a % n ) * ( b / n ) ; ans += ( ( a % n ) + ( b % n ) ) / n ; return ans ; } public static void main ( String [ ] args ) { int a = 5 , b = 13 , n = 3 ; System . out . println ( findCountOfPairs ( a , b , n ) ) ; } }
Generate array with minimum sum which can be deleted in P steps | Java implementation of the approach ; Function to find the required array ; calculating minimum possible sum ; Array ; place first P natural elements ; Fill rest of the elements with 1 ; Driver Code
class GFG { static void findArray ( int N , int P ) { int ans = ( P * ( P + 1 ) ) / 2 + ( N - P ) ; int arr [ ] = new int [ N + 1 ] ; for ( int i = 1 ; i <= P ; i ++ ) { arr [ i ] = i ; } for ( int i = P + 1 ; i <= N ; i ++ ) { arr [ i ] = 1 ; } System . out . print ( " The ▁ Minimum ▁ Possible ▁ Sum ▁ is : ▁ " + ans + "NEW_LINE"); System . out . print ( "The Array Elements are: NEW_LINE"); for ( int i = 1 ; i <= N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 5 , P = 3 ; findArray ( N , P ) ; } }
Find Intersection of all Intervals | Java implementation of the approach ; Function to print the intersection ; First interval ; Check rest of the intervals and find the intersection ; If no intersection exists ; Else update the intersection ; Driver code
import java . io . * ; class GFG { static void findIntersection ( int intervals [ ] [ ] , int N ) { int l = intervals [ 0 ] [ 0 ] ; int r = intervals [ 0 ] [ 1 ] ; for ( int i = 1 ; i < N ; i ++ ) { if ( intervals [ i ] [ 0 ] > r intervals [ i ] [ 1 ] < l ) { System . out . println ( - 1 ) ; return ; } else { l = Math . max ( l , intervals [ i ] [ 0 ] ) ; r = Math . min ( r , intervals [ i ] [ 1 ] ) ; } } System . out . println ( " [ " + l + " , ▁ " + r + " ] " ) ; } public static void main ( String [ ] args ) { int intervals [ ] [ ] = { { 1 , 6 } , { 2 , 8 } , { 3 , 10 } , { 5 , 8 } } ; int N = intervals . length ; findIntersection ( intervals , N ) ; } }
Maximum size of sub | Java implementation of the approach ; Function that compares a and b ; Function to return length of longest subarray that satisfies one of the given conditions ; Driver Code ; Print the required answer
class GFG { static int cmp ( int a , int b ) { if ( a > b ) return 1 ; else if ( a == b ) return 0 ; else return - 1 ; } static int maxSubarraySize ( int [ ] arr , int n ) { int ans = 1 ; int anchor = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int c = cmp ( arr [ i - 1 ] , arr [ i ] ) ; if ( c == 0 ) anchor = i ; else if ( i == n - 1 || c * cmp ( arr [ i ] , arr [ i + 1 ] ) != - 1 ) { ans = Math . max ( ans , i - anchor + 1 ) ; anchor = i ; } } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 9 , 4 , 2 , 10 , 7 , 8 , 8 , 1 , 9 } ; int n = arr . length ; System . out . println ( maxSubarraySize ( arr , n ) ) ; } }
Maximum count of sub | Java implementation of the approach ; Function to return the count of the required sub - strings ; Iterate over all characters ; Count with current character ; If the substring has a length k then increment count with current character ; Update max count ; Driver Code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int maxSubStrings ( String s , int k ) { int maxSubStr = 0 , n = s . length ( ) ; for ( int c = 0 ; c < 26 ; c ++ ) { char ch = ( char ) ( ( int ) ' a ' + c ) ; int curr = 0 ; for ( int i = 0 ; i <= n - k ; i ++ ) { if ( s . charAt ( i ) != ch ) continue ; int cnt = 0 ; while ( i < n && s . charAt ( i ) == ch && cnt != k ) { i ++ ; cnt ++ ; } i -- ; if ( cnt == k ) curr ++ ; } maxSubStr = Math . max ( maxSubStr , curr ) ; } return maxSubStr ; } public static void main ( String [ ] args ) { String s = " aaacaabbaa " ; int k = 2 ; System . out . println ( maxSubStrings ( s , k ) ) ; } }
Count valid pairs in the array satisfying given conditions | Java implementation of the approach ; Function to return total valid pairs ; Initialize count of all the elements ; frequency count of all the elements ; Add total valid pairs ; Exclude pairs made with a single element i . e . ( x , x ) ; Driver Code ; Function call to print required answer
class GFG { static int ValidPairs ( int arr [ ] , int n ) { int [ ] count = new int [ 121 ] ; for ( int i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] += 1 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ i ] < arr [ j ] ) continue ; if ( Math . abs ( arr [ i ] - arr [ j ] ) % 2 == 1 ) continue ; ans += count [ arr [ i ] ] * count [ arr [ j ] ] ; if ( arr [ i ] == arr [ j ] ) ans -= count [ arr [ i ] ] ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 16 , 17 , 18 } ; int n = arr . length ; System . out . println ( ValidPairs ( arr , n ) ) ; } }
Distribution of candies according to ages of students | Java implementation of the approach ; Function to check The validity of distribution ; Stroring the max age of all students + 1 ; Stroring the max candy + 1 ; Creating the frequency array of the age of students ; Creating the frequency array of the packets of candies ; Pointer to tell whether we have reached the end of candy frequency array ; Flag to tell if distribution is possible or not ; Flag to tell if we can choose some candy packets for the students with age j ; If the quantity of packets is greater than or equal to the number of students of age j , then we can choose these packets for the students ; Start searching from k + 1 in next operation ; If we cannot choose any packets then the answer is NO ; Driver code
import java . util . * ; class Main { public static void check_distribution ( int n , int k , int age [ ] , int candy [ ] ) { int mxage = age [ 0 ] ; for ( int i = 0 ; i < age . length ; i ++ ) { if ( mxage < age [ i ] ) { mxage = age [ i ] ; } } int mxcandy = candy [ 0 ] ; for ( int i = 0 ; i < candy . length ; i ++ ) { if ( mxcandy < candy [ i ] ) { mxcandy = candy [ i ] ; } } int fr1 [ ] = new int [ mxage + 1 ] ; Arrays . fill ( fr1 , 0 ) ; int fr2 [ ] = new int [ mxcandy + 1 ] ; Arrays . fill ( fr2 , 0 ) ; for ( int j = 0 ; j < n ; j ++ ) { fr1 [ age [ j ] ] += 1 ; } for ( int j = 0 ; j < k ; j ++ ) { fr2 [ candy [ j ] ] += 1 ; } k = 0 ; boolean Tf = true ; for ( int j = 0 ; j < mxage ; j ++ ) { if ( fr1 [ j ] == 0 ) continue ; boolean flag = false ; while ( k < mxcandy ) { if ( fr1 [ j ] <= fr2 [ k ] ) { flag = true ; break ; } k += 1 ; } k = k + 1 ; if ( flag == false ) { Tf = false ; break ; } } if ( Tf ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int age [ ] = { 5 , 15 , 10 } ; int candy [ ] = { 2 , 2 , 2 , 3 , 3 , 4 } ; int n = age . length ; int k = candy . length ; check_distribution ( n , k , age , candy ) ; } }
Find a palindromic string B such that given String A is a subsequense of B | Java program to find a palindromic string B such that given String A is a subsequense of B ; Function to check if a string is palindrome ; Reversing a string ; check if reversed string is equal to given string ; Function to find a palindromic string B such that given String A is a subsequense of B ; Reversing the string A ; If the string A is already a palindrome return A ; else return B ; Swap values of left and right ; Driver Code
class GFG { static boolean checkPalindrome ( String s ) { String x = reverse ( s ) ; return x . equals ( s ) ; } static String findStringB ( String A ) { String B = reverse ( A ) ; B = B + A ; if ( checkPalindrome ( A ) ) { return A ; } return B ; } static String reverse ( String input ) { char [ ] temparray = input . toCharArray ( ) ; int left , right = 0 ; right = temparray . length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . valueOf ( temparray ) ; } public static void main ( String [ ] args ) { String A = " ab " ; System . out . println ( findStringB ( A ) ) ; } }
Minimum number of 1 's to be replaced in a binary array | Java program to find minimum number of 1 ' s ▁ to ▁ be ▁ replaced ▁ to ▁ 0' s ; Function to find minimum number of 1 ' s ▁ to ▁ be ▁ replaced ▁ to ▁ 0' s ; return final answer ; Driver Code
import java . lang . * ; import java . util . * ; class GFG { static int minChanges ( int [ ] A , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n - 2 ; ++ i ) { if ( ( i - 1 >= 0 ) && A [ i - 1 ] == 1 && A [ i + 1 ] == 1 && A [ i ] == 0 ) { A [ i + 1 ] = 0 ; cnt ++ ; } } return cnt ; } public static void main ( String args [ ] ) { int [ ] A = { 1 , 1 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 0 } ; int n = A . length ; System . out . print ( minChanges ( A , n ) ) ; } }
Number of closing brackets needed to complete a regular bracket sequence | Java program to find number of closing brackets needed and complete a regular bracket sequence ; Function to find number of closing brackets and complete a regular bracket sequence ; Finding the length of sequence ; Counting opening brackets ; Counting closing brackets ; Checking if at any position the number of closing bracket is more then answer is impossible ; If possible , print ' s ' and required closing brackets . ; Driver code
class GFG { static void completeSequence ( String s ) { int n = s . length ( ) ; int open = 0 , close = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' ( ' ) open ++ ; else close ++ ; if ( close > open ) { System . out . print ( " IMPOSSIBLE " ) ; return ; } } System . out . print ( s ) ; for ( int i = 0 ; i < open - close ; i ++ ) System . out . print ( " ) " ) ; } public static void main ( String [ ] args ) { String s = " ( ( ) ( ( ) ( " ; completeSequence ( s ) ; } }
Lexicographically smallest permutation with no digits at Original Index | Java program to find the smallest permutation ; Function to print the smallest permutation ; when n is even ; when n is odd ; handling last 3 digit ; add EOL and print result ; Driver Code
class GFG { static void smallestPermute ( int n ) { char res [ ] = new char [ n + 1 ] ; if ( n % 2 == 0 ) { for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) res [ i ] = ( char ) ( 48 + i + 2 ) ; else res [ i ] = ( char ) ( 48 + i ) ; } } else { for ( int i = 0 ; i < n - 2 ; i ++ ) { if ( i % 2 == 0 ) res [ i ] = ( char ) ( 48 + i + 2 ) ; else res [ i ] = ( char ) ( 48 + i ) ; } res [ n - 1 ] = ( char ) ( 48 + n - 2 ) ; res [ n - 2 ] = ( char ) ( 48 + n ) ; res [ n - 3 ] = ( char ) ( 48 + n - 1 ) ; } res [ n ] = ' \0' ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( res [ i ] ) ; } } public static void main ( String [ ] args ) { int n = 7 ; smallestPermute ( n ) ; } }
Minimum array insertions required to make consecutive difference <= K | Java implementation of above approach ; Function to return minimum number of insertions required ; Initialize insertions to 0 ; return total insertions ; Driver program
class GFG { static int minInsertions ( int [ ] H , int n , int K ) { int inser = 0 ; for ( int i = 1 ; i < n ; ++ i ) { float diff = Math . abs ( H [ i ] - H [ i - 1 ] ) ; if ( diff <= K ) continue ; else inser += Math . ceil ( diff / K ) - 1 ; } return inser ; } public static void main ( String [ ] args ) { int [ ] H = new int [ ] { 2 , 4 , 8 , 16 } ; int K = 3 ; int n = H . length ; System . out . println ( minInsertions ( H , n , K ) ) ; } }
Minimum number of operations required to reduce N to 1 | Java implementation of above approach ; Function that returns the minimum number of operations to be performed to reduce the number to 1 ; To stores the total number of operations to be performed ; if n is divisible by 3 then reduce it to n / 3 ; if n modulo 3 is 1 decrement it by 1 ; if n modulo 3 is 2 then increment it by 1 ; update the counter ; Driver code
class GFG { static int count_minimum_operations ( long n ) { int count = 0 ; while ( n > 1 ) { if ( n % 3 == 0 ) n /= 3 ; else if ( n % 3 == 1 ) n -- ; else { if ( n == 2 ) n -- ; else n ++ ; } count ++ ; } return count ; } public static void main ( String [ ] args ) { long n = 4 ; long ans = count_minimum_operations ( n ) ; System . out . println ( ans ) ; } }
Minimum number of operations required to reduce N to 1 | Java implementation of above approach ; Function that returns the minimum number of operations to be performed to reduce the number to 1 ; Base cases ; Driver code
import java . util . * ; class GFG { public static int count_minimum_operations ( int n ) { if ( n == 2 ) { return 1 ; } else if ( n == 1 ) { return 0 ; } if ( n % 3 == 0 ) { return 1 + count_minimum_operations ( n / 3 ) ; } else if ( n % 3 == 1 ) { return 1 + count_minimum_operations ( n - 1 ) ; } else { return 1 + count_minimum_operations ( n + 1 ) ; } } public static void main ( String [ ] args ) { int n = 4 ; int ans = count_minimum_operations ( n ) ; System . out . println ( ans ) ; } }