codeparrot/xlcost-text-to-code huggingface dataset:
Reorder an array according to given indexes | A O ( n ) time and O ( 1 ) extra space Java program to sort an array according to given indexes ; Method to reorder elements of arr [ ] according to index [ ] ; Fix all elements one by one ; While index [ i ] and arr [ i ] are not fixed ; Store values of the target ( or correct ) position before placing arr [ i ] there ; Place arr [ i ] at its target ( or correct ) position . Also copy corrected index for new position ; Copy old target values to arr [ i ] and index [ i ] ; Driver method to test the above function
import java . util . Arrays ; class Test { static int arr [ ] = new int [ ] { 50 , 40 , 70 , 60 , 90 } ; static int index [ ] = new int [ ] { 3 , 0 , 4 , 1 , 2 } ; static void reorder ( ) { for ( int i = 0 ; i < arr . length ; i ++ ) { while ( index [ i ] != i ) { int oldTargetI = index [ index [ i ] ] ; char oldTargetE = ( char ) arr [ index [ i ] ] ; arr [ index [ i ] ] = arr [ i ] ; index [ index [ i ] ] = index [ i ] ; index [ i ] = oldTargetI ; arr [ i ] = oldTargetE ; } } } public static void main ( String [ ] args ) { reorder ( ) ; System . out . println ( " Reordered ▁ array ▁ is : ▁ " ) ; System . out . println ( Arrays . toString ( arr ) ) ; System . out . println ( " Modified ▁ Index ▁ array ▁ is : " ) ; System . out . println ( Arrays . toString ( index ) ) ; } }
Stooge Sort | Java program to implement stooge sort ; Function to implement stooge sort ; If first element is smaller than last , swap them ; If there are more than 2 elements in the array ; Recursively sort first 2 / 3 elements ; Recursively sort last 2 / 3 elements ; Recursively sort first 2 / 3 elements again to confirm ; Driver Code ; Calling Stooge Sort function to sort the array ; Display the sorted array
import java . io . * ; public class stooge { static void stoogesort ( int arr [ ] , int l , int h ) { if ( l >= h ) return ; if ( arr [ l ] > arr [ h ] ) { int t = arr [ l ] ; arr [ l ] = arr [ h ] ; arr [ h ] = t ; } if ( h - l + 1 > 2 ) { int t = ( h - l + 1 ) / 3 ; stoogesort ( arr , l , h - t ) ; stoogesort ( arr , l + t , h ) ; stoogesort ( arr , l , h - t ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 4 , 5 , 3 , 1 } ; int n = arr . length ; stoogesort ( arr , 0 , n - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
Reorder an array according to given indexes | Java code to reorder an array according to given indices ; left child in 0 based indexing ; right child in 1 based indexing ; Find largest index from root , left and right child ; swap arr whenever index is swapped ; Build heap ; Swap the largest element of index ( first element ) with the last element ; swap arr whenever index is swapped ; Driver code
class GFG { static int heapSize ; public static void heapify ( int arr [ ] , int index [ ] , int i ) { int largest = i ; int left = 2 * i + 1 ; int right = 2 * i + 2 ; if ( left < heapSize && index [ left ] > index [ largest ] ) { largest = left ; } if ( right < heapSize && index [ right ] > index [ largest ] ) { largest = right ; } if ( largest != i ) { int temp = arr [ largest ] ; arr [ largest ] = arr [ i ] ; arr [ i ] = temp ; temp = index [ largest ] ; index [ largest ] = index [ i ] ; index [ i ] = temp ; heapify ( arr , index , largest ) ; } } public static void heapSort ( int arr [ ] , int index [ ] , int n ) { for ( int i = ( n - 1 ) / 2 ; i >= 0 ; i -- ) { heapify ( arr , index , i ) ; } for ( int i = n - 1 ; i > 0 ; i -- ) { int temp = index [ 0 ] ; index [ 0 ] = index [ i ] ; index [ i ] = temp ; temp = arr [ 0 ] ; arr [ 0 ] = arr [ i ] ; arr [ i ] = temp ; heapSize -- ; heapify ( arr , index , 0 ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 50 , 40 , 70 , 60 , 90 } ; int index [ ] = { 3 , 0 , 4 , 1 , 2 } ; int n = arr . length ; heapSize = n ; heapSort ( arr , index , n ) ; System . out . println ( " Reordered ▁ array ▁ is : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; System . out . println ( " Modified ▁ Index ▁ array ▁ is : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( index [ i ] + " ▁ " ) ; } }
Sum of all the parent nodes having child node x | Java implementation to find the sum of all the parent nodes having child node x ; sum ; Node of a binary tree ; function to get a new node ; allocate memory for the node ; put in the data ; function to find the sum of all the parent nodes having child node x ; if root == NULL ; if left or right child of root is ' x ' , then add the root ' s ▁ data ▁ to ▁ ' sum '  ; recursively find the required parent nodes in the left and right subtree ; utility function to find the sum of all the parent nodes having child node x ; required sum of parent nodes ; Driver Code ; binary tree formation 4 ; / \ ; 2 5 ; / \ / \ ; 7 2 2 3
class GFG { static int sum = 0 ; static class Node { int data ; Node left , right ; } ; static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = newNode . right = null ; return newNode ; } static void sumOfParentOfX ( Node root , int x ) { if ( root == null ) return ; if ( ( root . left != null && root . left . data == x ) || ( root . right != null && root . right . data == x ) ) sum += root . data ; sumOfParentOfX ( root . left , x ) ; sumOfParentOfX ( root . right , x ) ; } static int sumOfParentOfXUtil ( Node root , int x ) { sum = 0 ; sumOfParentOfX ( root , x ) ; return sum ; } public static void main ( String args [ ] ) { Node root = getNode ( 4 ) ; root . left = getNode ( 2 ) ; root . right = getNode ( 5 ) ; root . left . left = getNode ( 7 ) ; root . left . right = getNode ( 2 ) ; root . right . left = getNode ( 2 ) ; root . right . right = getNode ( 3 ) ; int x = 2 ; System . out . println ( " Sum ▁ = ▁ " + sumOfParentOfXUtil ( root , x ) ) ; } }
Sort even | Program to sort even - placed elements in increasing and odd - placed in decreasing order with constant space complexity ; first odd index ; last index ; if last index is odd ; decrement j to even index ; swapping till half of array ; Sort first half in increasing ; Sort second half in decreasing ; Reverse the second half ; Driver Program
import java . util . Arrays ; class GFG { static void bitonicGenerator ( int arr [ ] , int n ) { int i = 1 ; int j = n - 1 ; if ( j % 2 != 0 ) j -- ; while ( i < j ) { arr = swap ( arr , i , j ) ; i += 2 ; j -= 2 ; } Arrays . sort ( arr , 0 , ( n + 1 ) / 2 ) ; Arrays . sort ( arr , ( n + 1 ) / 2 , n ) ; int low = ( n + 1 ) / 2 , high = n - 1 ; while ( low < high ) { Integer temp = arr [ low ] ; arr [ low ] = arr [ high ] ; arr [ high ] = temp ; low ++ ; high -- ; } } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 } ; int n = arr . length ; bitonicGenerator ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
Count minimum number of subsets ( or subsequences ) with consecutive numbers | Java program to find number of subset containing consecutive numbers ; Returns count of subsets with consecutive numbers ; Sort the array so that elements which are consecutive in nature became consecutive in the array . ; Initialize result ; Check if there is beginning of another subset of consecutive number ; Driven Program
import java . util . * ; class GFG { static int numofsubset ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int count = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] + 1 != arr [ i + 1 ] ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 100 , 56 , 5 , 6 , 102 , 58 , 101 , 57 , 7 , 103 , 59 } ; int n = arr . length ; System . out . println ( numofsubset ( arr , n ) ) ; } }
Gnome Sort | Java Program to implement Gnome Sort ; A function to sort the algorithm using gnome sort ; Driver program to test above functions .
import java . util . Arrays ; public class GFG { static void gnomeSort ( int arr [ ] , int n ) { int index = 0 ; while ( index < n ) { if ( index == 0 ) index ++ ; if ( arr [ index ] >= arr [ index - 1 ] ) index ++ ; else { int temp = 0 ; temp = arr [ index ] ; arr [ index ] = arr [ index - 1 ] ; arr [ index - 1 ] = temp ; index -- ; } } return ; } public static void main ( String [ ] args ) { int arr [ ] = { 34 , 2 , 10 , - 9 } ; gnomeSort ( arr , arr . length ) ; System . out . print ( " Sorted ▁ sequence ▁ after ▁ applying ▁ Gnome ▁ sort : ▁ " ) ; System . out . println ( Arrays . toString ( arr ) ) ; } }
Cocktail Sort | Sorts arrar a [ 0. . n - 1 ] using Cocktail sort ; reset the swapped flag on entering the loop , because it might be true from a previous iteration . ; loop from bottom to top same as the bubble sort ; if nothing moved , then array is sorted . ; otherwise , reset the swapped flag so that it can be used in the next stage ; move the end point back by one , because item at the end is in its rightful spot ; from top to bottom , doing the same comparison as in the previous stage ; increase the starting point , because the last stage would have moved the next smallest number to its rightful spot . ; Prints the array ; Driver code
public class CocktailSort { void cocktailSort ( int a [ ] ) { boolean swapped = true ; int start = 0 ; int end = a . length ; while ( swapped == true ) { swapped = false ; for ( int i = start ; i < end - 1 ; ++ i ) { if ( a [ i ] > a [ i + 1 ] ) { int temp = a [ i ] ; a [ i ] = a [ i + 1 ] ; a [ i + 1 ] = temp ; swapped = true ; } } if ( swapped == false ) break ; swapped = false ; end = end - 1 ; for ( int i = end - 1 ; i >= start ; i -- ) { if ( a [ i ] > a [ i + 1 ] ) { int temp = a [ i ] ; a [ i ] = a [ i + 1 ] ; a [ i + 1 ] = temp ; swapped = true ; } } start = start + 1 ; } } void printArray ( int a [ ] ) { int n = a . length ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { CocktailSort ob = new CocktailSort ( ) ; int a [ ] = { 5 , 1 , 4 , 2 , 8 , 0 , 2 } ; ob . cocktailSort ( a ) ; System . out . println ( " Sorted ▁ array " ) ; ob . printArray ( a ) ; } }
Find the point where maximum intervals overlap | Java Program to find maximum guest at any time in a party ; Sort arrival and exit arrays ; guests_in indicates number of guests at a time ; Similar to merge in merge sort to process all events in sorted order ; If next event in sorted order is arrival , increment count of guests ; Update max_guests if needed ; increment index of arrival array ; If event is exit , decrement count ; of guests . ; Driver program to test above function
import java . util . * ; class GFG { static void findMaxGuests ( int arrl [ ] , int exit [ ] , int n ) { Arrays . sort ( arrl ) ; Arrays . sort ( exit ) ; int guests_in = 1 , max_guests = 1 , time = arrl [ 0 ] ; int i = 1 , j = 0 ; while ( i < n && j < n ) { if ( arrl [ i ] <= exit [ j ] ) { guests_in ++ ; if ( guests_in > max_guests ) { max_guests = guests_in ; time = arrl [ i ] ; } i ++ ; } else { guests_in -- ; j ++ ; } } System . out . println ( " Maximum ▁ Number ▁ of ▁ Guests ▁ = ▁ " + max_guests + " ▁ at ▁ time ▁ " + time ) ; } public static void main ( String [ ] args ) { int arrl [ ] = { 1 , 2 , 10 , 5 , 5 } ; int exit [ ] = { 4 , 5 , 12 , 9 , 12 } ; int n = arrl . length ; findMaxGuests ( arrl , exit , n ) ; } }
Find the point where maximum intervals overlap |  ; Finding maximum starting time ; Finding maximum ending time ; reating an auxiliary array ; Lazy addition ; Lazily Calculating value at index i ; Driver code
import java . io . * ; import java . util . * ; import java . text . * ; import java . math . * ; import java . util . regex . * ; class GFG { public static void maxOverlap ( int [ ] start , int [ ] end , int n ) { int maxa = Arrays . stream ( start ) . max ( ) . getAsInt ( ) ; int maxb = Arrays . stream ( end ) . max ( ) . getAsInt ( ) ; int maxc = Math . max ( maxa , maxb ) ; int [ ] x = new int [ maxc + 2 ] ; Arrays . fill ( x , 0 ) ; int cur = 0 , idx = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ++ x [ start [ i ] ] ; -- x [ end [ i ] + 1 ] ; } int maxy = Integer . MIN_VALUE ; for ( int i = 0 ; i <= maxc ; i ++ ) { cur += x [ i ] ; if ( maxy < cur ) { maxy = cur ; idx = i ; } } System . out . println ( " Maximum ▁ value ▁ is : " + maxy + " ▁ at ▁ position : ▁ " + idx + " " ) ; } public static void main ( String [ ] args ) { int [ ] start = new int [ ] { 13 , 28 , 29 , 14 , 40 , 17 , 3 } ; int [ ] end = new int [ ] { 107 , 95 , 111 , 105 , 70 , 127 , 74 } ; int n = start . length ; maxOverlap ( start , end , n ) ; } }
Rearrange an array such that ' arr [ j ] ' becomes ' i ' if ' arr [ i ] ' is ' j ' | Set 1 | A simple Java program to rearrange contents of arr [ ] such that arr [ j ] becomes j if arr [ i ] is j ; A simple method to rearrange ' arr [ 0 . . n - 1 ] ' so that ' arr [ j ] ' becomes ' i ' if ' arr [ i ] ' is ' j ' ; Create an auxiliary array of same size ; Store result in temp [ ] ; Copy temp back to arr [ ] ; A utility function to print contents of arr [ 0. . n - 1 ] ; Driver program to test above functions
class RearrangeArray { void rearrangeNaive ( int arr [ ] , int n ) { int temp [ ] = new int [ n ] ; int i ; for ( i = 0 ; i < n ; i ++ ) temp [ arr [ i ] ] = i ; for ( i = 0 ; i < n ; i ++ ) arr [ i ] = temp [ i ] ; } void printArray ( int arr [ ] , int n ) { int i ; for ( i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . println ( " " ) ; } public static void main ( String [ ] args ) { RearrangeArray arrange = new RearrangeArray ( ) ; int arr [ ] = { 1 , 3 , 0 , 2 } ; int n = arr . length ; System . out . println ( " Given ▁ array ▁ is ▁ " ) ; arrange . printArray ( arr , n ) ; arrange . rearrangeNaive ( arr , n ) ; System . out . println ( " Modified ▁ array ▁ is ▁ " ) ; arrange . printArray ( arr , n ) ; } }
QuickSort | Java implementation of QuickSort ; A utility function to swap two elements ; This function takes last element as pivot , places the pivot element at its correct position in sorted array , and places all smaller ( smaller than pivot ) to left of pivot and all greater elements to right of pivot ; pivot ; Index of smaller element and indicates the right position of pivot found so far ; If current element is smaller than the pivot ; Increment index of smaller element ; The main function that implements QuickSort arr [ ] -- > Array to be sorted , low -- > Starting index , high -- > Ending index ; pi is partitioning index , arr [ p ] is now at right place ; Separately sort elements before partition and after partition ; Function to print an array ; Driver Code
import java . io . * ; class GFG { static void swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } static int partition ( int [ ] arr , int low , int high ) { int pivot = arr [ high ] ; int i = ( low - 1 ) ; for ( int j = low ; j <= high - 1 ; j ++ ) { if ( arr [ j ] < pivot ) { i ++ ; swap ( arr , i , j ) ; } } swap ( arr , i + 1 , high ) ; return ( i + 1 ) ; } static void quickSort ( int [ ] arr , int low , int high ) { if ( low < high ) { int pi = partition ( arr , low , high ) ; quickSort ( arr , low , pi - 1 ) ; quickSort ( arr , pi + 1 , high ) ; } } static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 7 , 8 , 9 , 1 , 5 } ; int n = arr . length ; quickSort ( arr , 0 , n - 1 ) ; System . out . println ( " Sorted ▁ array : ▁ " ) ; printArray ( arr , n ) ; } }
Rearrange an array such that ' arr [ j ] ' becomes ' i ' if ' arr [ i ] ' is ' j ' | Set 1 | A simple JAVA program to rearrange contents of arr [ ] such that arr [ j ] becomes j if arr [ i ] is j ; A simple method to rearrange ' arr [ 0 . . n - 1 ] ' so that ' arr [ j ] ' becomes ' i ' if ' arr [ i ] ' is ' j ' ; retrieving old value and storing with the new one ; retrieving new value ; A utility function to print contents of arr [ 0. . n - 1 ] ; Driver code
class GFG { static void rearrange ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { arr [ arr [ i ] % n ] += i * n ; } for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] /= n ; } } static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 0 , 1 , 4 , 5 , 3 } ; int n = arr . length ; System . out . println ( " Given ▁ array ▁ is ▁ : ▁ " ) ; printArray ( arr , n ) ; rearrange ( arr , n ) ; System . out . println ( " Modified ▁ array ▁ is ▁ : " ) ; printArray ( arr , n ) ; } }
Rearrange an array in maximum minimum form | Set 1 | Java program to rearrange an array in minimum maximum form ; Prints max at first position , min at second position second max at third position , second min at fourth position and so on . ; Auxiliary array to hold modified array ; Indexes of smallest and largest elements from remaining array . ; To indicate whether we need to copy rmaining largest or remaining smallest at next position ; Store result in temp [ ] ; Driver code
import java . util . Arrays ; public class GFG { static void rearrange ( int [ ] arr , int n ) { int temp [ ] = arr . clone ( ) ; int small = 0 , large = n - 1 ; boolean flag = true ; for ( int i = 0 ; i < n ; i ++ ) { if ( flag ) arr [ i ] = temp [ large -- ] ; else arr [ i ] = temp [ small ++ ] ; flag = ! flag ; } } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 1 , 2 , 3 , 4 , 5 , 6 } ; System . out . println ( " Original ▁ Array ▁ " ) ; System . out . println ( Arrays . toString ( arr ) ) ; rearrange ( arr , arr . length ) ; System . out . println ( " Modified ▁ Array ▁ " ) ; System . out . println ( Arrays . toString ( arr ) ) ; } }
Minimum number of coins needed to remove all the elements of the array based on given rules | Java implementation for the above approach ; Function to calculate minimum number of coins needed ; Consider the first element separately , add 1 to the total if it 's of type 1 ; Iterate from the second element ; If the current element is of type 2 then any Player can remove the element ; Second pointer to reach end of type 1 elements ; Increment j until arr [ j ] is equal to 1 and j is not out of bounds ; Number of type 1 elements in a continious chunk ; From next iteration i pointer will start from index of j ; Return the minimum count of coins ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int minimumcoins ( int arr [ ] , int N ) { int coins = 0 ; int j = 0 ; if ( arr [ 0 ] == 1 ) coins ++ ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == 2 ) continue ; j = i ; while ( j < N && arr [ j ] == 1 ) { j ++ ; } int x = ( j - i ) ; coins += x / 3 ; i = j - 1 ; } return coins ; } public static void main ( String [ ] args ) { int N = 8 ; int arr [ ] = { 1 , 2 , 1 , 1 , 2 , 1 , 1 , 1 } ; System . out . println ( minimumcoins ( arr , N ) ) ; } }
Rearrange an array in maximum minimum form | Set 2 ( O ( 1 ) extra space ) | Java program to rearrange an array in minimum maximum form ; Prints max at first position , min at second position second max at third position , second min at fourth position and so on . ; initialize index of first minimum and first maximum element ; store maximum element of array ; traverse array elements ; at even index : we have to put maximum element ; at odd index : we have to put minimum element ; array elements back to it 's original form ; Driver code
public class Main { public static void rearrange ( int arr [ ] , int n ) { int max_idx = n - 1 , min_idx = 0 ; int max_elem = arr [ n - 1 ] + 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { arr [ i ] += ( arr [ max_idx ] % max_elem ) * max_elem ; max_idx -- ; } else { arr [ i ] += ( arr [ min_idx ] % max_elem ) * max_elem ; min_idx ++ ; } } for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = arr [ i ] / max_elem ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . length ; System . out . println ( " Original ▁ Array " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; rearrange ( arr , n ) ; System . out . print ( " Modified Array "); for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
Rearrange an array in maximum minimum form | Set 2 ( O ( 1 ) extra space ) | Java program to rearrange an array in minimum maximum form ; Prints max at first position , min at second position second max at third position , second min at fourth position and so on . ; initialize index of first minimum and first maximum element ; traverse array elements ; at even index : we have to put maximum element ; at odd index : we have to put minimum element ; Driver code
public class Main { public static void rearrange ( int arr [ ] , int n ) { int max_ele = arr [ n - 1 ] ; int min_ele = arr [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { arr [ i ] = max_ele ; max_ele -= 1 ; } else { arr [ i ] = min_ele ; min_ele += 1 ; } } } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . length ; System . out . println ( " Original ▁ Array " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; rearrange ( arr , n ) ; System . out . print ( " Modified Array "); for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
Find sum of all left leaves in a given Binary Tree | A binary tree Node has key , pointer to left and right children ; A utility function to check if a given node is leaf or not ; This function returns sum of all left leaves in a given binary tree ; Initialize result ; Update result if root is not NULL ; If left of root is NULL , then add key of left child ; Else recur for left child of root ; Recur for right child of root and update res ; return result ; Driver program
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; boolean isLeaf ( Node node ) { if ( node == null ) return false ; if ( node . left == null && node . right == null ) return true ; return false ; } int leftLeavesSum ( Node node ) { int res = 0 ; if ( node != null ) { if ( isLeaf ( node . left ) ) res += node . left . data ; else res += leftLeavesSum ( node . left ) ; res += leftLeavesSum ( node . right ) ; } return res ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 20 ) ; tree . root . left = new Node ( 9 ) ; tree . root . right = new Node ( 49 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . left . left = new Node ( 5 ) ; tree . root . right . left = new Node ( 23 ) ; tree . root . right . right = new Node ( 52 ) ; tree . root . left . right . right = new Node ( 12 ) ; tree . root . right . right . left = new Node ( 50 ) ; System . out . println ( " The ▁ sum ▁ of ▁ leaves ▁ is ▁ " + tree . leftLeavesSum ( tree . root ) ) ; } }
Move all negative numbers to beginning and positive to end with constant extra space | Java program to put all negative numbers before positive numbers ; A utility function to print an array ; Driver code
import java . io . * ; class GFG { static void rearrange ( int arr [ ] , int n ) { int j = 0 , temp ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) { if ( i != j ) { temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } j ++ ; } } } static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { - 1 , 2 , - 3 , 4 , 5 , 6 , - 7 , 8 , 9 } ; int n = arr . length ; rearrange ( arr , n ) ; printArray ( arr , n ) ; } }