codeparrot/xlcost-text-to-code huggingface dataset:
Lexicographical concatenation of all substrings of a string | Java Program to create concatenation of all substrings in lexicographic order . ; Creating an array to store substrings ; finding all substrings of string ; Sort all substrings in lexicographic order ; Concatenating all substrings ; Driver code
import java . util . * ; class GFG { static String lexicographicSubConcat ( String s ) { int n = s . length ( ) ; int sub_count = n * ( n + 1 ) / 2 ; String [ ] arr = new String [ sub_count ] ; int index = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int len = 1 ; len <= n - i ; len ++ ) { arr [ index ++ ] = s . substring ( i , i + len ) ; } Arrays . sort ( arr ) ; String res = " " ; for ( int i = 0 ; i < sub_count ; i ++ ) res += arr [ i ] ; return res ; } public static void main ( String [ ] args ) { String s = " abc " ; System . out . println ( lexicographicSubConcat ( s ) ) ; } }
Insertion Sort by Swapping Elements | Iterative Java program to sort an array by swapping elements ; Utility function to print a Vector ; Function performs insertion sort on vector V ; 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 insertionSort ( Vector < Integer > V ) { int N = V . size ( ) ; int i , j , key ; for ( i = 1 ; i < N ; i ++ ) { j = i ; 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 ▁ : " ) ; insertionSort ( A ) ; printVector ( A ) ; } }
Program to sort string in descending order | Java program to sort a string in descending order using library function ; Driver code ; descOrder ( s ) ; function call
import java . util . * ; class GFG { static void descOrder ( char [ ] s ) { Arrays . sort ( s ) ; reverse ( s ) ; } static void reverse ( char [ ] a ) { int i , n = a . length ; char t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } public static void main ( String [ ] args ) { char [ ] s = " geeksforgeeks " . toCharArray ( ) ; System . out . println ( String . valueOf ( s ) ) ; } }
Sort string of characters | Java program to sort a string of characters ; function to print string in sorted order ; Hash array to keep count of characters . Initially count of all charters is initialized to zero . ; Traverse string and increment count of characters ; ' a ' - ' a ' will be 0 , ' b ' - ' a ' will be 1 , so for location of character in count array we will do str [ i ] - ' a ' . ; Traverse the hash array and print characters ; Driver program to test above function
public class SortString { static final int MAX_CHAR = 26 ; static void sortString ( String str ) { int letters [ ] = new int [ MAX_CHAR ] ; for ( char x : str . toCharArray ( ) ) { letters [ x - ' a ' ] ++ ; } for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { for ( int j = 0 ; j < letters [ i ] ; j ++ ) { System . out . print ( ( char ) ( i + ' a ' ) ) ; } } } public static void main ( String [ ] args ) { sortString ( " geeksforgeeks " ) ; } }
Smallest element in an array that is repeated exactly ' k ' times . | Java program to find smallest number in array that is repeated exactly ' k ' times . ; finds the smallest number in arr [ ] that is repeated k times ; Computing frequencies of all elements ; Finding the smallest element with frequency as k ; If frequency of any of the number is equal to k starting from 0 then return the number ; Driver code
public class GFG { static final int MAX = 1000 ; static int findDuplicate ( int arr [ ] , int n , int k ) { int [ ] freq = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 1 && arr [ i ] > MAX ) { System . out . println ( " Out ▁ of ▁ range " ) ; return - 1 ; } freq [ arr [ i ] ] += 1 ; } for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] == k ) return i ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 1 , 3 , 1 } ; int k = 2 ; int n = arr . length ; System . out . println ( findDuplicate ( arr , n , k ) ) ; } }
Find the Sub | Java program to find subarray with sum closest to 0 ; Returns subarray with sum closest to 0. ; To consider the case of subarray starting from beginning of the array ; Store prefix sum with index ; Sort on the basis of sum ; Find two consecutive elements with minimum difference ; Update minimum difference and starting and ending indexes ; Return starting and ending indexes ; Driver code
import java . util . * ; class Prefix { int sum , index ; } class Pair { int first , second ; Pair ( int a , int b ) { first = a ; second = b ; } } class GFG { static Pair findSubArray ( int arr [ ] , int n ) { int start = - 1 , end = - 1 , min_diff = Integer . MAX_VALUE ; Prefix pre_sum [ ] = new Prefix [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) pre_sum [ i ] = new Prefix ( ) ; pre_sum [ 0 ] . sum = 0 ; pre_sum [ 0 ] . index = - 1 ; for ( int i = 1 ; i <= n ; i ++ ) { pre_sum [ i ] . sum = pre_sum [ i - 1 ] . sum + arr [ i - 1 ] ; pre_sum [ i ] . index = i - 1 ; } Arrays . sort ( pre_sum , ( ( a , b ) -> a . sum - b . sum ) ) ; for ( int i = 1 ; i <= n ; i ++ ) { int diff = pre_sum [ i ] . sum - pre_sum [ i - 1 ] . sum ; if ( min_diff > diff ) { min_diff = diff ; start = pre_sum [ i - 1 ] . index ; end = pre_sum [ i ] . index ; } } Pair p = new Pair ( start + 1 , end ) ; return p ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , - 4 , - 1 , 6 } ; int n = arr . length ; Pair point = findSubArray ( arr , n ) ; System . out . print ( " Subarray ▁ starting ▁ from ▁ " ) ; System . out . println ( point . first + " ▁ to ▁ " + point . second ) ; } }
Check whether a given array is a k sorted array or not | Java implementation to check whether the given array is a k sorted array or not ; function to check whether the given array is a ' k ' sorted array or not ; auxiliary array ' aux ' ; copy elements of ' arr ' to ' aux ' ; sort ' aux ' ; for every element of ' arr ' at index ' i ' , find its index ' j ' in ' aux ' ; index of arr [ i ] in sorted array ' aux ' ; if abs ( i - j ) > k , then that element is not at - most k distance away from its target position . Thus , ' arr ' is not a k sorted array ; ' arr ' is a k sorted array ; Driver method
import java . util . Arrays ; class Test { static String isKSortedArray ( int arr [ ] , int n , int k ) { int aux [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) aux [ i ] = arr [ i ] ; Arrays . sort ( aux ) ; for ( int i = 0 ; i < n ; i ++ ) { int j = Arrays . binarySearch ( aux , arr [ i ] ) ; if ( Math . abs ( i - j ) > k ) return " No " ; } return " Yes " ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 2 , 1 , 5 , 6 , 4 } ; int k = 2 ; System . out . println ( " Is ▁ it ▁ a ▁ k ▁ sorted ▁ array ▁ ? : ▁ " + isKSortedArray ( arr , arr . length , k ) ) ; } }
TimSort | Java program to perform TimSort . ; Becomes 1 if any 1 bits are shifted off ; This function sorts array from left index to to right index which is of size atmost RUN ; Merge function merges the sorted runs ; Original array is broken in two parts left and right array ; After comparing , we merge those two array in larger sub array ; Copy remaining elements of left , if any ; Copy remaining element of right , if any ; Iterative Timsort function to sort the array [ 0. . . n - 1 ] ( similar to merge sort ) ; Sort individual subarrays of size RUN ; Start merging from size RUN ( or 32 ) . It will merge to form size 64 , then 128 , 256 and so on ... . ; Pick starting point of left sub array . We are going to merge arr [ left . . left + size - 1 ] and arr [ left + size , left + 2 * size - 1 ] After every merge , we increase left by 2 * size ; Find ending point of left sub array mid + 1 is starting point of right sub array ; Merge sub array arr [ left ... . . mid ] & arr [ mid + 1. ... right ] ; Utility function to print the Array ; Driver code ; Function Call
class GFG { static int MIN_MERGE = 32 ; public static int minRunLength ( int n ) { assert n >= 0 ; int r = 0 ; while ( n >= MIN_MERGE ) { r |= ( n & 1 ) ; n >>= 1 ; } return n + r ; } public static void insertionSort ( int [ ] arr , int left , int right ) { for ( int i = left + 1 ; i <= right ; i ++ ) { int temp = arr [ i ] ; int j = i - 1 ; while ( j >= left && arr [ j ] > temp ) { arr [ j + 1 ] = arr [ j ] ; j -- ; } arr [ j + 1 ] = temp ; } } public static void merge ( int [ ] arr , int l , int m , int r ) { int len1 = m - l + 1 , len2 = r - m ; int [ ] left = new int [ len1 ] ; int [ ] right = new int [ len2 ] ; for ( int x = 0 ; x < len1 ; x ++ ) { left [ x ] = arr [ l + x ] ; } for ( int x = 0 ; x < len2 ; x ++ ) { right [ x ] = arr [ m + 1 + x ] ; } int i = 0 ; int j = 0 ; int k = l ; while ( i < len1 && j < len2 ) { if ( left [ i ] <= right [ j ] ) { arr [ k ] = left [ i ] ; i ++ ; } else { arr [ k ] = right [ j ] ; j ++ ; } k ++ ; } while ( i < len1 ) { arr [ k ] = left [ i ] ; k ++ ; i ++ ; } while ( j < len2 ) { arr [ k ] = right [ j ] ; k ++ ; j ++ ; } } public static void timSort ( int [ ] arr , int n ) { int minRun = minRunLength ( MIN_MERGE ) ; for ( int i = 0 ; i < n ; i += minRun ) { insertionSort ( arr , i , Math . min ( ( i + MIN_MERGE - 1 ) , ( n - 1 ) ) ) ; } for ( int size = minRun ; size < n ; size = 2 * size ) { for ( int left = 0 ; left < n ; left += 2 * size ) { int mid = left + size - 1 ; int right = Math . min ( ( left + 2 * size - 1 ) , ( n - 1 ) ) ; if ( mid < right ) merge ( arr , left , mid , right ) ; } } } public static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . print ( "NEW_LINE"); } public static void main ( String [ ] args ) { int [ ] arr = { - 2 , 7 , 15 , - 14 , 0 , 15 , 0 , 7 , - 7 , - 4 , - 13 , 5 , 8 , - 14 , 12 } ; int n = arr . length ; System . out . println ( " Given ▁ Array ▁ is " ) ; printArray ( arr , n ) ; timSort ( arr , n ) ; System . out . println ( " After ▁ Sorting ▁ Array ▁ is " ) ; printArray ( arr , n ) ; } }
Program to print an array in Pendulum Arrangement | Java program for pendulum arrangement of numbers ; Prints pendulum arrangement of arr [ ] ; sorting the elements ; Auxiliary array to store output ; calculating the middle index ; storing the minimum element in the middle i is index for output array and j is for input array . ; adjustment for when no . of elements is even ; Printing the pendulum arrangement ; Driver method ; input Array ; calling pendulum function
import java . util . Arrays ; class Test { static void pendulumArrangement ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int op [ ] = new int [ n ] ; int mid = ( n - 1 ) / 2 ; int j = 1 , i = 1 ; op [ mid ] = arr [ 0 ] ; for ( i = 1 ; i <= mid ; i ++ ) { op [ mid + i ] = arr [ j ++ ] ; op [ mid - i ] = arr [ j ++ ] ; } if ( n % 2 == 0 ) op [ mid + i ] = arr [ j ] ; System . out . println ( " Pendulum ▁ arrangement : " ) ; for ( i = 0 ; i < n ; i ++ ) System . out . print ( op [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 14 , 6 , 19 , 21 , 12 } ; pendulumArrangement ( arr , arr . length ) ; } }
Maximum width of a binary tree | A binary tree node has data , pointer to left child and a pointer to right child ; Function to get the maximum width of a binary tree ; Create an array that will store count of nodes at each level ; Fill the count array using preorder traversal ; Return the maximum value from count array ; A function that fills count array with count of nodes at every level of given binary tree ; Compute the " height " of a tree -- the number of nodes along the longest path from the root node down to the farthest leaf node . ; compute the height of each subtree ; use the larger one ; Return the maximum value from count array ; Driver program to test above functions ; Constructed bunary tree is : 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; int getMaxWidth ( Node node ) { int width ; int h = height ( node ) ; int count [ ] = new int [ 10 ] ; int level = 0 ; getMaxWidthRecur ( node , count , level ) ; return getMax ( count , h ) ; } void getMaxWidthRecur ( Node node , int count [ ] , int level ) { if ( node != null ) { count [ level ] ++ ; getMaxWidthRecur ( node . left , count , level + 1 ) ; getMaxWidthRecur ( node . right , count , level + 1 ) ; } } int height ( Node node ) { if ( node == null ) return 0 ; else { int lHeight = height ( node . left ) ; int rHeight = height ( node . right ) ; return ( lHeight > rHeight ) ? ( lHeight + 1 ) : ( rHeight + 1 ) ; } } int getMax ( int arr [ ] , int n ) { int max = arr [ 0 ] ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } return max ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . right = new Node ( 8 ) ; tree . root . right . right . left = new Node ( 6 ) ; tree . root . right . right . right = new Node ( 7 ) ; System . out . println ( " Maximum ▁ width ▁ is ▁ " + tree . getMaxWidth ( tree . root ) ) ; } }
Minimize the sum of product of two arrays with permutations allowed | Java program to calculate minimum sum of product of two arrays . ; Returns minimum sum of product of two arrays with permutations allowed ; Sort A and B so that minimum and maximum value can easily be fetched . ; Multiplying minimum value of A and maximum value of B ; Driven Code
import java . io . * ; import java . util . * ; class GFG { static long minValue ( int A [ ] , int B [ ] , int n ) { Arrays . sort ( A ) ; Arrays . sort ( B ) ; long result = 0 ; for ( int i = 0 ; i < n ; i ++ ) result += ( A [ i ] * B [ n - i - 1 ] ) ; return result ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 1 , 1 } ; int B [ ] = { 6 , 5 , 4 } ; int n = A . length ; ; System . out . println ( minValue ( A , B , n ) ) ; } }
Count of numbers in range [ L , R ] which can be represented as sum of two perfect powers | Java program for the above approach ; Function to find the number of numbers that can be expressed in the form of the sum of two perfect powers ; Stores all possible powers ; Push 1 and 0 in it ; Iterate over all the exponents ; Iterate over all possible numbers ; This loop will run for a maximum of sqrt ( R ) times ; Push this power in the array pows [ ] ; Increase the number ; Stores if i can be expressed as the sum of perfect power or not ; Iterate over all possible pairs of the array pows [ ] ; The number is valid ; Find the prefix sum of the array ok [ ] ; Return the count of required number ; Driver Code
import java . util . * ; class GFG { static int TotalPerfectPowerSum ( int L , int R ) { ArrayList < Integer > pows = new ArrayList < Integer > ( ) ; pows . add ( 0 ) ; pows . add ( 1 ) ; for ( int p = 2 ; p < 25 ; p ++ ) { int num = 2 ; while ( ( int ) ( Math . pow ( num , p ) + 0.5 ) <= R ) { pows . add ( ( int ) ( Math . pow ( num , p ) + 0.5 ) ) ; num ++ ; } } int [ ] ok = new int [ R + 2 ] ; for ( int i = 0 ; i < pows . size ( ) ; i ++ ) { for ( int j = 0 ; j < pows . size ( ) ; j ++ ) { if ( pows . get ( i ) + pows . get ( j ) <= R && pows . get ( i ) + pows . get ( j ) >= L ) { ok [ pows . get ( i ) + pows . get ( j ) ] = 1 ; } } } for ( int i = 1 ; i <= R ; i ++ ) { ok [ i ] += ok [ i - 1 ] ; } return ok [ R ] - ok [ L - 1 ] ; } public static void main ( String args [ ] ) { int L = 5 , R = 8 ; System . out . print ( TotalPerfectPowerSum ( L , R ) ) ; } }
Minimize absolute difference between sum of subtrees formed after splitting Binary tree into two | Java implementation for the above approach ; Structure of Node ; Function to calculate minimum absolute difference of subtrees after splitting the tree into two parts ; Reference variable to store the answer ; Function to store sum of values of current node , left subtree and right subtree in place of current node 's value ; Function to perform every possible split and calculate absolute difference of subtrees ; Absolute difference in subtrees if left edge is broken ; Absolute difference in subtrees if right edge is broken ; Update minDiff if a difference lesser than it is found ; Driver code ; Construct the tree ; Print the output
import java . lang . Math ; import java . io . * ; class GFG { static class Node { Node left , right ; int val ; public Node ( int val ) { this . val = val ; this . left = this . right = null ; } } } ; public static int minAbsDiff ( Node root ) { int [ ] minDiff = new int [ 1 ] ; minDiff [ 0 ] = Integer . MAX_VALUE ; postOrder ( root ) ; preOrder ( root , minDiff ) ; return minDiff [ 0 ] ; } public static int postOrder ( Node root ) { if ( root == null ) return 0 ; root . val += postOrder ( root . left ) + postOrder ( root . right ) ; return root . val ; } public static void preOrder ( Node root , int [ ] minDiff ) { if ( root == null ) return ; int leftDiff = Math . abs ( root . left . val - ( root . val - root . left . val ) ) ; int rightDiff = Math . abs ( root . right . val - ( root . val - root . right . val ) ) ; minDiff [ 0 ] = Math . min ( minDiff [ 0 ] , Math . min ( leftDiff , rightDiff ) ) ; return ; } public static void main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; System . out . println ( minAbsDiff ( root ) ) ; } }
Vertical width of Binary tree | Set 1 | Java program to print vertical width of a tree ; A Binary Tree Node ; get vertical width ; traverse left ; if curr is decrease then get value in minimum ; if curr is increase then get value in maximum ; traverse right ; 1 is added to include root in the width ; Utility function to create a new tree node ; Driver Code
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static int maximum = 0 , minimum = 0 ; static void lengthUtil ( Node root , int curr ) { if ( root == null ) return ; lengthUtil ( root . left , curr - 1 ) ; if ( minimum > curr ) minimum = curr ; if ( maximum < curr ) maximum = curr ; lengthUtil ( root . right , curr + 1 ) ; } static int getLength ( Node root ) { maximum = 0 ; minimum = 0 ; lengthUtil ( root , 0 ) ; return ( Math . abs ( minimum ) + maximum ) + 1 ; } static Node newNode ( int data ) { Node curr = new Node ( ) ; curr . data = data ; curr . left = curr . right = null ; return curr ; } public static void main ( String [ ] args ) { Node root = newNode ( 7 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 2 ) ; root . right . right = newNode ( 1 ) ; System . out . println ( getLength ( root ) ) ; } }
Maximize Array sum after changing sign of any elements for exactly M times | Java implementation of the above approach ; Function to find the maximum sum with M flips ; Declare a priority queue i . e . min heap ; Declare the sum as zero ; Push all elements of the array in it ; Iterate for M times ; Get the top element ; Flip the sign of the top element ; Remove the top element ; Update the sum ; Push the temp into the queue ; Driver Code ; Given input
import java . util . * ; import java . lang . * ; import java . lang . Math ; class GFG { static void findMaximumSumWithMflips ( int arr [ ] , int N , int M ) { PriorityQueue < Integer > minHeap = new PriorityQueue < Integer > ( ) ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { minHeap . add ( arr [ i ] ) ; sum += arr [ i ] ; } while ( M -- > 0 ) { sum -= minHeap . peek ( ) ; int temp = - 1 * minHeap . peek ( ) ; minHeap . remove ( ) ; sum += temp ; minHeap . add ( temp ) ; } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 3 , 7 , - 1 , - 5 , - 3 } ; int M = 4 , N = 5 ; findMaximumSumWithMflips ( arr , N , M ) ; } }
Minimum operations to make Array equal by repeatedly adding K from an element and subtracting K from other | Java Program for the above approach ; Function to find the minimum number of operations to make all the elements of the array equal ; Store the sum of the array arr [ ] ; Traverse through the array ; If it is not possible to make all array element equal ; Store the minimum number of operations needed ; Traverse through the array ; Finally , print the minimum number operation to make array elements equal ; Driver Code ; Given Input ; Function Call
import java . io . * ; class GFG { static void miniOperToMakeAllEleEqual ( int arr [ ] , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } if ( sum % n != 0 ) { System . out . println ( - 1 ) ; return ; } int valueAfterDivision = sum / n ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( Math . abs ( valueAfterDivision - arr [ i ] ) % k != 0 ) { System . out . println ( - 1 ) ; return ; } count += Math . abs ( valueAfterDivision - arr [ i ] ) / k ; } System . out . println ( ( int ) count / 2 ) ; } public static void main ( String [ ] args ) { int n = 3 , k = 3 ; int arr [ ] = { 5 , 8 , 11 } ; miniOperToMakeAllEleEqual ( arr , n , k ) ; } }
Minimum operations to make product of adjacent element pair of prefix sum negative | Java code for the above approach ; Function to find minimum operations needed to make the product of any two adjacent elements in prefix sum array negative ; Stores the minimum operations ; Stores the prefix sum and number of operations ; Traverse the array ; Update the value of sum ; Check if i + r is odd ; Check if prefix sum is not positive ; Update the value of ans and sum ; Check if prefix sum is not negative ; Update the value of ans and sum ; Update the value of res ; Print the value of res ; Driver Code
import java . util . * ; class GFG { static void minOperations ( ArrayList < Integer > a ) { int res = Integer . MAX_VALUE ; int N = a . size ( ) ; for ( int r = 0 ; r < 2 ; r ++ ) { int sum = 0 , ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += a . get ( i ) ; if ( ( i + r ) % 2 == 1 ) { if ( sum <= 0 ) { ans += - sum + 1 ; sum = 1 ; } } else { if ( sum >= 0 ) { ans += sum + 1 ; sum = - 1 ; } } } res = Math . min ( res , ans ) ; } System . out . print ( res ) ; } public static void main ( String args [ ] ) { ArrayList < Integer > a = new ArrayList < Integer > ( ) ; a . add ( 1 ) ; a . add ( - 3 ) ; a . add ( 1 ) ; a . add ( 0 ) ; minOperations ( a ) ; } }