codeparrot/xlcost-text-to-code huggingface dataset:
Level order traversal with direction change after every two levels | JAVA program to print Zig - Zag traversal in groups of size 2. ; A Binary Tree Node ; Utility function to create a new tree node ; Function to print the level order of given binary tree . Direction of printing level order traversal of binary tree changes after every two levels ; Run this while loop till queue got empty ; For printing nodes from right to left , push the nodes to stack instead of printing them . ; for printing the nodes in order from right to left ; Change the direction of traversal . ; Driver code ; Let us create binary tree
import java . util . * ; class GFG { static final int LEFT = 0 ; static final int RIGHT = 1 ; static int ChangeDirection ( int Dir ) { Dir = 1 - Dir ; return Dir ; } static class node { int data ; node left , right ; } ; static node newNode ( int data ) { node temp = new node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static void modifiedLevelOrder ( node root ) { if ( root == null ) return ; int dir = LEFT ; node temp ; Queue < node > Q = new LinkedList < > ( ) ; Stack < node > S = new Stack < > ( ) ; S . add ( root ) ; while ( ! Q . isEmpty ( ) || ! S . isEmpty ( ) ) { while ( ! S . isEmpty ( ) ) { temp = S . peek ( ) ; S . pop ( ) ; System . out . print ( temp . data + " ▁ " ) ; if ( dir == LEFT ) { if ( temp . left != null ) Q . add ( temp . left ) ; if ( temp . right != null ) Q . add ( temp . right ) ; } else { if ( temp . right != null ) Q . add ( temp . right ) ; if ( temp . left != null ) Q . add ( temp . left ) ; } } System . out . println ( ) ; while ( ! Q . isEmpty ( ) ) { temp = Q . peek ( ) ; Q . remove ( ) ; System . out . print ( temp . data + " ▁ " ) ; if ( dir == LEFT ) { if ( temp . left != null ) S . add ( temp . left ) ; if ( temp . right != null ) S . add ( temp . right ) ; } else { if ( temp . right != null ) S . add ( temp . right ) ; if ( temp . left != null ) S . add ( temp . left ) ; } } System . out . println ( ) ; dir = ChangeDirection ( dir ) ; } } public static void main ( String [ ] args ) { node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . left . left . left = newNode ( 8 ) ; root . left . left . right = newNode ( 9 ) ; root . left . right . left = newNode ( 3 ) ; root . left . right . right = newNode ( 1 ) ; root . right . left . left = newNode ( 4 ) ; root . right . left . right = newNode ( 2 ) ; root . right . right . left = newNode ( 7 ) ; root . right . right . right = newNode ( 2 ) ; root . left . right . left . left = newNode ( 16 ) ; root . left . right . left . right = newNode ( 17 ) ; root . right . left . right . left = newNode ( 18 ) ; root . right . right . left . right = newNode ( 19 ) ; modifiedLevelOrder ( root ) ; } }
Insertion in a Binary Tree in level order | Java program to insert element in binary tree ; A binary tree node has key , pointer to left child and a pointer to right child ; constructor ; Inorder traversal of a binary tree ; function to insert element in binary tree ; Do level order traversal until we find an empty place . ; Driver code
import java . util . LinkedList ; import java . util . Queue ; public class GFG { static class Node { int key ; Node left , right ; Node ( int key ) { this . key = key ; left = null ; right = null ; } } static Node root ; static Node temp = root ; static void inorder ( Node temp ) { if ( temp == null ) return ; inorder ( temp . left ) ; System . out . print ( temp . key + " ▁ " ) ; inorder ( temp . right ) ; } static void insert ( Node temp , int key ) { if ( temp == null ) { root = new Node ( key ) ; return ; } Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( temp ) ; while ( ! q . isEmpty ( ) ) { temp = q . peek ( ) ; q . remove ( ) ; if ( temp . left == null ) { temp . left = new Node ( key ) ; break ; } else q . add ( temp . left ) ; if ( temp . right == null ) { temp . right = new Node ( key ) ; break ; } else q . add ( temp . right ) ; } } public static void main ( String args [ ] ) { root = new Node ( 10 ) ; root . left = new Node ( 11 ) ; root . left . left = new Node ( 7 ) ; root . right = new Node ( 9 ) ; root . right . left = new Node ( 15 ) ; root . right . right = new Node ( 8 ) ; System . out . print ( " Inorder ▁ traversal ▁ before ▁ insertion : " ) ; inorder ( root ) ; int key = 12 ; insert ( root , key ) ; System . out . print ( " Inorder traversal after insertion : "); inorder ( root ) ; } }
Tug of War | Java program for Tug of war ; function that tries every possible solution by calling itself recursively ; checks whether the it is going out of bound ; checks that the numbers of elements left are not less than the number of elements required to form the solution ; consider the cases when current element is not included in the solution ; add the current element to the solution ; checks if a solution is formed ; checks if the solution formed is better than the best solution so far ; consider the cases where current element is included in the solution ; removes current element before returning to the caller of this function ; main function that generate an arr ; the boolean array that contains the inclusion and exclusion of an element in current set . The number excluded automatically form the other set ; The inclusion / exclusion array for final solution ; Find the solution using recursive function TOWUtil ( ) ; Print the solution ; Driver program to test above functions
import java . util . * ; import java . lang . * ; import java . io . * ; class TugOfWar { public int min_diff ; void TOWUtil ( int arr [ ] , int n , boolean curr_elements [ ] , int no_of_selected_elements , boolean soln [ ] , int sum , int curr_sum , int curr_position ) { if ( curr_position == n ) return ; if ( ( n / 2 - no_of_selected_elements ) > ( n - curr_position ) ) return ; TOWUtil ( arr , n , curr_elements , no_of_selected_elements , soln , sum , curr_sum , curr_position + 1 ) ; no_of_selected_elements ++ ; curr_sum = curr_sum + arr [ curr_position ] ; curr_elements [ curr_position ] = true ; if ( no_of_selected_elements == n / 2 ) { if ( Math . abs ( sum / 2 - curr_sum ) < min_diff ) { min_diff = Math . abs ( sum / 2 - curr_sum ) ; for ( int i = 0 ; i < n ; i ++ ) soln [ i ] = curr_elements [ i ] ; } } else { TOWUtil ( arr , n , curr_elements , no_of_selected_elements , soln , sum , curr_sum , curr_position + 1 ) ; } curr_elements [ curr_position ] = false ; } void tugOfWar ( int arr [ ] ) { int n = arr . length ; boolean [ ] curr_elements = new boolean [ n ] ; boolean [ ] soln = new boolean [ n ] ; min_diff = Integer . MAX_VALUE ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; curr_elements [ i ] = soln [ i ] = false ; } TOWUtil ( arr , n , curr_elements , 0 , soln , sum , 0 , 0 ) ; System . out . print ( " The ▁ first ▁ subset ▁ is : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( soln [ i ] == true ) System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . print ( " The second subset is : "); for ( int i = 0 ; i < n ; i ++ ) { if ( soln [ i ] == false ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 23 , 45 , - 34 , 12 , 0 , 98 , - 99 , 4 , 189 , - 1 , 4 } ; TugOfWar a = new TugOfWar ( ) ; a . tugOfWar ( arr ) ; } }
The Knight 's tour problem | Backtracking | Java program for Knight Tour problem ; A utility function to check if i , j are valid indexes for N * N chessboard ; A utility function to print solution matrix sol [ N ] [ N ] ; This function solves the Knight Tour problem using Backtracking . This function mainly uses solveKTUtil ( ) to solve the problem . It returns false if no complete tour is possible , otherwise return true and prints the tour . Please note that there may be more than one solutions , this function prints one of the feasible solutions . ; Initialization of solution matrix ; xMove [ ] and yMove [ ] define next move of Knight . xMove [ ] is for next value of x coordinate yMove [ ] is for next value of y coordinate ; Since the Knight is initially at the first block ; Start from 0 , 0 and explore all tours using solveKTUtil ( ) ; A recursive utility function to solve Knight Tour problem ; Try all next moves from the current coordinate x , y ; backtracking ; Driver Code ; Function Call
class KnightTour { static int N = 8 ; static boolean isSafe ( int x , int y , int sol [ ] [ ] ) { return ( x >= 0 && x < N && y >= 0 && y < N && sol [ x ] [ y ] == - 1 ) ; } static void printSolution ( int sol [ ] [ ] ) { for ( int x = 0 ; x < N ; x ++ ) { for ( int y = 0 ; y < N ; y ++ ) System . out . print ( sol [ x ] [ y ] + " ▁ " ) ; System . out . println ( ) ; } } static boolean solveKT ( ) { int sol [ ] [ ] = new int [ 8 ] [ 8 ] ; for ( int x = 0 ; x < N ; x ++ ) for ( int y = 0 ; y < N ; y ++ ) sol [ x ] [ y ] = - 1 ; int xMove [ ] = { 2 , 1 , - 1 , - 2 , - 2 , - 1 , 1 , 2 } ; int yMove [ ] = { 1 , 2 , 2 , 1 , - 1 , - 2 , - 2 , - 1 } ; sol [ 0 ] [ 0 ] = 0 ; if ( ! solveKTUtil ( 0 , 0 , 1 , sol , xMove , yMove ) ) { System . out . println ( " Solution ▁ does ▁ not ▁ exist " ) ; return false ; } else printSolution ( sol ) ; return true ; } static boolean solveKTUtil ( int x , int y , int movei , int sol [ ] [ ] , int xMove [ ] , int yMove [ ] ) { int k , next_x , next_y ; if ( movei == N * N ) return true ; for ( k = 0 ; k < 8 ; k ++ ) { next_x = x + xMove [ k ] ; next_y = y + yMove [ k ] ; if ( isSafe ( next_x , next_y , sol ) ) { sol [ next_x ] [ next_y ] = movei ; if ( solveKTUtil ( next_x , next_y , movei + 1 , sol , xMove , yMove ) ) return true ; else sol [ next_x ] [ next_y ] = - 1 ; } } return false ; } public static void main ( String args [ ] ) { solveKT ( ) ; } }
Rat in a Maze | Backtracking | Java program to solve Rat in a Maze problem using backtracking ; Size of the maze ; A utility function to print solution matrix sol [ N ] [ N ] ; A utility function to check if x , y is valid index for N * N maze ; if ( x , y outside maze ) return false ; This function solves the Maze problem using Backtracking . It mainly uses solveMazeUtil ( ) to solve the problem . It returns false if no path is possible , otherwise return true and prints the path in the form of 1 s . Please note that there may be more than one solutions , this function prints one of the feasible solutions . ; A recursive utility function to solve Maze problem ; if ( x , y is goal ) return true ; Check if maze [ x ] [ y ] is valid ; Check if the current block is already part of solution path . ; mark x , y as part of solution path ; Move forward in x direction ; If moving in x direction doesn 't give  solution then Move down in y direction  ; If moving in y direction doesn 't give  solution then Move backwards in x direction  ; If moving backwards in x direction doesn 't give  solution then Move upwards in y direction  ; If none of the above movements works then BACKTRACK : unmark x , y as part of solution path ; driver program to test above function
public class RatMaze { static int N ; void printSolution ( int sol [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . print ( " ▁ " + sol [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } boolean isSafe ( int maze [ ] [ ] , int x , int y ) { return ( x >= 0 && x < N && y >= 0 && y < N && maze [ x ] [ y ] == 1 ) ; } boolean solveMaze ( int maze [ ] [ ] ) { int sol [ ] [ ] = new int [ N ] [ N ] ; if ( solveMazeUtil ( maze , 0 , 0 , sol ) == false ) { System . out . print ( " Solution ▁ doesn ' t ▁ exist " ) ; return false ; } printSolution ( sol ) ; return true ; } boolean solveMazeUtil ( int maze [ ] [ ] , int x , int y , int sol [ ] [ ] ) { if ( x == N - 1 && y == N - 1 && maze [ x ] [ y ] == 1 ) { sol [ x ] [ y ] = 1 ; return true ; } if ( isSafe ( maze , x , y ) == true ) { if ( sol [ x ] [ y ] == 1 ) return false ; sol [ x ] [ y ] = 1 ; if ( solveMazeUtil ( maze , x + 1 , y , sol ) ) return true ; if ( solveMazeUtil ( maze , x , y + 1 , sol ) ) return true ; if ( solveMazeUtil ( maze , x - 1 , y , sol ) ) return true ; if ( solveMazeUtil ( maze , x , y - 1 , sol ) ) return true ; sol [ x ] [ y ] = 0 ; return false ; } return false ; } public static void main ( String args [ ] ) { RatMaze rat = new RatMaze ( ) ; int maze [ ] [ ] = { { 1 , 0 , 0 , 0 } , { 1 , 1 , 0 , 1 } , { 0 , 1 , 0 , 0 } , { 1 , 1 , 1 , 1 } } ; N = maze . length ; rat . solveMaze ( maze ) ; } }
m Coloring Problem | Backtracking |  ; Number of vertices in the graph ; A utility function to print solution ; check if the colored graph is safe or not ; check for every edge ; This function solves the m Coloring problem using recursion . It returns false if the m colours cannot be assigned , otherwise , return true and prints assignments of colours to all vertices . Please note that there may be more than one solutions , this function prints one of the feasible solutions . ; if current index reached end ; if coloring is safe ; Print the solution ; Assign each color from 1 to m ; Recur of the rest vertices ; Driver code ; Create following graph and test whether it is 3 colorable ( 3 ) -- - ( 2 ) | / | | / | | / | ( 0 ) -- - ( 1 ) ; Number of colors ; Initialize all color values as 0. This initialization is needed correct functioning of isSafe ( )
public class GFG { static int V = 4 ; static void printSolution ( int [ ] color ) { System . out . println ( " Solution ▁ Exists : " + " ▁ Following ▁ are ▁ the ▁ assigned ▁ colors ▁ " ) ; for ( int i = 0 ; i < V ; i ++ ) System . out . print ( " ▁ " + color [ i ] ) ; System . out . println ( ) ; } static boolean isSafe ( boolean [ ] [ ] graph , int [ ] color ) { for ( int i = 0 ; i < V ; i ++ ) for ( int j = i + 1 ; j < V ; j ++ ) if ( graph [ i ] [ j ] && color [ j ] == color [ i ] ) return false ; return true ; } static boolean graphColoring ( boolean [ ] [ ] graph , int m , int i , int [ ] color ) { if ( i == V ) { if ( isSafe ( graph , color ) ) { printSolution ( color ) ; return true ; } return false ; } for ( int j = 1 ; j <= m ; j ++ ) { color [ i ] = j ; if ( graphColoring ( graph , m , i + 1 , color ) ) return true ; color [ i ] = 0 ; } return false ; } public static void main ( String [ ] args ) { boolean [ ] [ ] graph = { { false , true , true , true } , { true , false , true , false } , { true , true , false , true } , { true , false , true , false } , } ; int m = 3 ; int [ ] color = new int [ V ] ; for ( int i = 0 ; i < V ; i ++ ) color [ i ] = 0 ; if ( ! graphColoring ( graph , m , 0 , color ) ) System . out . println ( " Solution ▁ does ▁ not ▁ exist " ) ; } }
m Coloring Problem | Backtracking | Java program for the above approach ; A node class which stores the color and the edges connected to the node ; Create a visited array of n nodes , initialized to zero ; maxColors used till now are 1 as all nodes are painted color 1 ; Do a full BFS traversal from all unvisited starting points ; If the starting point is unvisited , mark it visited and push it in queue ; BFS Travel starts here ; Checking all adjacent nodes to " top " edge in our queue ; IMPORTANT : If the color of the adjacent node is same , increase it by 1 ; If number of colors used shoots m , return 0 ; If the adjacent node is not visited , mark it visited and push it in queue ; Driver code ; Number of colors ; Create a vector of n + 1 nodes of type " node " The zeroth position is just dummy ( 1 to n to be used ) ; Add edges to each node as per given input ; Connect the undirected graph ; Display final answer
import java . io . * ; import java . util . * ; class Node { int color = 1 ; Set < Integer > edges = new HashSet < Integer > ( ) ; } class GFG { static int canPaint ( ArrayList < Node > nodes , int n , int m ) { ArrayList < Integer > visited = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { visited . add ( 0 ) ; } int maxColors = 1 ; for ( int sv = 1 ; sv <= n ; sv ++ ) { if ( visited . get ( sv ) > 0 ) { continue ; } visited . set ( sv , 1 ) ; Queue < Integer > q = new LinkedList < > ( ) ; q . add ( sv ) ; while ( q . size ( ) != 0 ) { int top = q . peek ( ) ; q . remove ( ) ; for ( int it : nodes . get ( top ) . edges ) { if ( nodes . get ( top ) . color == nodes . get ( it ) . color ) { nodes . get ( it ) . color += 1 ; } maxColors = Math . max ( maxColors , Math . max ( nodes . get ( top ) . color , nodes . get ( it ) . color ) ) ; if ( maxColors > m ) return 0 ; if ( visited . get ( it ) == 0 ) { visited . set ( it , 1 ) ; q . remove ( it ) ; } } } } return 1 ; } public static void main ( String [ ] args ) { int n = 4 ; int [ ] [ ] graph = { { 0 , 1 , 1 , 1 } , { 1 , 0 , 1 , 0 } , { 1 , 1 , 0 , 1 } , { 1 , 0 , 1 , 0 } } ; int m = 3 ; ArrayList < Node > nodes = new ArrayList < Node > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { nodes . add ( new Node ( ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( graph [ i ] [ j ] > 0 ) { nodes . get ( i ) . edges . add ( i ) ; nodes . get ( j ) . edges . add ( j ) ; } } } System . out . println ( canPaint ( nodes , n , m ) ) ; } }
Maximize pair decrements required to reduce all array elements except one to 0 | Java program to implement the above approach ; Function to count maximum number of steps to make ( N - 1 ) array elements to 0 ; Stores maximum count of steps to make ( N - 1 ) elements equal to 0 ; Stores array elements ; Traverse the array ; Insert arr [ i ] into PQ ; Extract top 2 elements from the array while ( N - 1 ) array elements become 0 ; Stores top element of PQ ; Pop the top element of PQ . ; Stores top element of PQ ; Pop the top element of PQ . ; Update X ; Update Y ; If X is not equal to 0 ; Insert X into PQ ; if Y is not equal to 0 ; Insert Y into PQ ; Update cntOp ; Driver code
import java . util . * ; class GFG { static int cntMaxOperationToMakeN_1_0 ( int [ ] arr , int N ) { int cntOp = 0 ; PriorityQueue < Integer > PQ = new PriorityQueue < Integer > ( ( a , b ) -> b - a ) ; for ( int i = 0 ; i < N ; i ++ ) { PQ . add ( arr [ i ] ) ; } while ( PQ . size ( ) > 1 ) { int X = PQ . peek ( ) ; PQ . remove ( ) ; int Y = PQ . peek ( ) ; PQ . remove ( ) ; X -- ; Y -- ; if ( X != 0 ) { PQ . add ( X ) ; } if ( Y != 0 ) { PQ . add ( Y ) ; } cntOp += 1 ; } return cntOp ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . length ; System . out . print ( cntMaxOperationToMakeN_1_0 ( arr , N ) ) ; } }
Flip all K | Java program for the above approach ; Function to flip all K - bits of an unsigned number N ; Stores ( 2 ^ K ) - 1 ; Update N ; Print the answer ; Driver Code
import java . util . * ; class GFG { static void flippingBits ( long N , long K ) { long X = ( 1 << ( K - 1 ) ) - 1 ; N = X - N ; System . out . print ( N ) ; } public static void main ( String [ ] args ) { long N = 1 , K = 8 ; flippingBits ( N , K ) ; } }
Generate a matrix having even sum of all diagonals in each 2 x 2 submatrices | Java program for the above approach ; Function to construct a matrix such that the sum elements in both diagonals of every 2 * 2 matrices is even ; Stores odd numbers ; Stores even numbers ; Store matrix elements such that sum of elements in both diagonals of every 2 * 2 submatrices is even ; Fill all the values of matrix elements ; Update odd ; Update even ; Print the matrix ; Driver Code
import java . io . * ; class GFG { static void generateMatrix ( int N ) { int odd = 1 ; int even = 2 ; int [ ] [ ] mat = new int [ N + 1 ] [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { if ( ( i + j ) % 2 == 0 ) { mat [ i ] [ j ] = odd ; odd += 2 ; } else { mat [ i ] [ j ] = even ; even += 2 ; } } } for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int N = 4 ; generateMatrix ( N ) ; } }
Queries to calculate sum by alternating signs of array elements in a given range | Java program to implement the above approach ; Function to build the segment tree ; If current node is a leaf node of the segment tree ; Update tree [ index ] ; Update tree [ index ] ; Divide the segment tree ; Update on L segment tree ; Update on R segment tree ; Find the sum from L subtree and R subtree ; Function to update elements at index pos by val in the segment tree ; If current node is a leaf node ; If current index is even ; Update tree [ index ] ; Update tree [ index ] ; Divide the segment tree elements into L and R subtree ; If element lies in L subtree ; Update tree [ index ] ; Function to find the sum of array elements in the range [ L , R ] ; If start and end not lies in the range [ L , R ] ; If start and end comleately lies in the range [ L , R ] ; Stores sum from left subtree ; Stores sum from right subtree ; Driver Code
import java . util . * ; class GFG { static void build ( int tree [ ] , int arr [ ] , int start , int end , int index ) { if ( start == end ) { if ( start % 2 == 0 ) { tree [ index ] = arr [ start ] ; } else { tree [ index ] = - arr [ start ] ; } return ; } int mid = start + ( end - start ) / 2 ; build ( tree , arr , start , mid , 2 * index + 1 ) ; build ( tree , arr , mid + 1 , end , 2 * index + 2 ) ; tree [ index ] = tree [ 2 * index + 1 ] + tree [ 2 * index + 2 ] ; } static void update ( int tree [ ] , int index , int start , int end , int pos , int val ) { if ( start == end ) { if ( start % 2 == 0 ) { tree [ index ] = val ; } else { tree [ index ] = - val ; } return ; } int mid = start + ( end - start ) / 2 ; if ( mid >= pos ) { update ( tree , 2 * index + 1 , start , mid , pos , val ) ; } else { update ( tree , 2 * index + 2 , mid + 1 , end , pos , val ) ; } tree [ index ] = tree [ 2 * index + 1 ] + tree [ 2 * index + 2 ] ; } static int FindSum ( int tree [ ] , int start , int end , int L , int R , int index ) { if ( L > end R < start ) { return 0 ; } if ( L <= start && R >= end ) { return tree [ index ] ; } int mid = start + ( end - start ) / 2 ; int X = FindSum ( tree , start , mid , L , R , 2 * index + 1 ) ; int Y = FindSum ( tree , mid + 1 , end , L , R , 2 * index + 2 ) ; return X + Y ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; int tree [ ] = new int [ 4 * N + 5 ] ; Arrays . fill ( tree , 0 ) ; build ( tree , arr , 0 , N - 1 , 0 ) ; int Q [ ] [ ] = { { 2 , 0 , 3 } , { 1 , 1 , 5 } , { 2 , 1 , 2 } } ; int cntQuey = 3 ; for ( int i = 0 ; i < cntQuey ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) { update ( tree , 0 , 0 , N - 1 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] ) ; } else { if ( Q [ i ] [ 1 ] % 2 == 0 ) { System . out . print ( FindSum ( tree , 0 , N - 1 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , 0 ) + " ▁ " ) ; } else { System . out . print ( - FindSum ( tree , 0 , N - 1 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , 0 ) + " ▁ " ) ; } } } } }