codeparrot/xlcost-text-to-code huggingface dataset:
Boggle ( Find all possible words in a board of characters ) | Set 1 | Java program for Boggle game ; Let the given dictionary be following ; A given function to check if a given string is present in dictionary . The implementation is naive for simplicity . As per the question dictionary is given to us . ; Linearly search all words ; A recursive function to print all words present on boggle ; Mark current cell as visited and append current character to str ; If str is present in dictionary , then print it ; Traverse 8 adjacent cells of boggle [ i ] [ j ] ; Erase current character from string and mark visited of current cell as false ; Prints all words present in dictionary . ; Mark all characters as not visited ; Initialize current string ; Consider every character and look for all words starting with this character ; Driver program to test above function
class GFG { static final String dictionary [ ] = { " GEEKS " , " FOR " , " QUIZ " , " GUQ " , " EE " } ; static final int n = dictionary . length ; static final int M = 3 , N = 3 ; static boolean isWord ( String str ) { for ( int i = 0 ; i < n ; i ++ ) if ( str . equals ( dictionary [ i ] ) ) return true ; return false ; } static void findWordsUtil ( char boggle [ ] [ ] , boolean visited [ ] [ ] , int i , int j , String str ) { visited [ i ] [ j ] = true ; str = str + boggle [ i ] [ j ] ; if ( isWord ( str ) ) System . out . println ( str ) ; for ( int row = i - 1 ; row <= i + 1 && row < M ; row ++ ) for ( int col = j - 1 ; col <= j + 1 && col < N ; col ++ ) if ( row >= 0 && col >= 0 && ! visited [ row ] [ col ] ) findWordsUtil ( boggle , visited , row , col , str ) ; str = " " + str . charAt ( str . length ( ) - 1 ) ; visited [ i ] [ j ] = false ; } static void findWords ( char boggle [ ] [ ] ) { boolean visited [ ] [ ] = new boolean [ M ] [ N ] ; String str = " " ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) findWordsUtil ( boggle , visited , i , j , str ) ; } public static void main ( String args [ ] ) { char boggle [ ] [ ] = { { ' G ' , ' I ' , ' Z ' } , { ' U ' , ' E ' , ' K ' } , { ' Q ' , ' S ' , ' E ' } } ; System . out . println ( " Following ▁ words ▁ of ▁ dictionary ▁ are ▁ present " ) ; findWords ( boggle ) ; } }
Remove all subtrees consisting only of even valued nodes from a Binary Tree | Java program for the above approach ; Node of the tree ; Function to create a new node ; Function to print tree level wise ; Base Case ; Create an empty queue for level order traversal ; Enqueue Root ; Print front of queue and remove it from queue ; If left child is present ; Otherwise ; If right child is present ; Otherwise ; Function to remove subtrees ; Base Case ; Search for required condition in left and right half ; If the node is even and leaf node ; Driver Code ; Function Call ; Print answer
import java . util . * ; class GFG { static class node { int data ; node left , right ; } ; static node newNode ( int key ) { node temp = new node ( ) ; temp . data = key ; temp . left = temp . right = null ; return ( temp ) ; } static void printLevelOrder ( node root ) { if ( root == null ) return ; Queue < node > q = new LinkedList < > ( ) ; q . add ( root ) ; while ( ! q . isEmpty ( ) ) { node temp = q . peek ( ) ; System . out . print ( temp . data + " ▁ " ) ; q . remove ( ) ; if ( temp . left != null ) { q . add ( temp . left ) ; } else if ( temp . right != null ) { System . out . print ( " null ▁ " ) ; } if ( temp . right != null ) { q . add ( temp . right ) ; } else if ( temp . left != null ) { System . out . print ( " null ▁ " ) ; } } } static node pruneTree ( node root ) { if ( root == null ) { return null ; } root . left = pruneTree ( root . left ) ; root . right = pruneTree ( root . right ) ; if ( root . data % 2 == 0 && root . right == null && root . left == null ) return null ; return root ; } public static void main ( String [ ] args ) { node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . left . left = newNode ( 8 ) ; root . left . right = newNode ( 10 ) ; root . right = newNode ( 7 ) ; root . right . left = newNode ( 12 ) ; root . right . right = newNode ( 5 ) ; node newRoot = pruneTree ( root ) ; printLevelOrder ( newRoot ) ; } }
Count total ways to reach destination from source in an undirected Graph | Java program to count total number of ways to reach destination in a graph ; Utility Function to count total ways ; Base condition When reach to the destination ; Make vertex visited ; Recursive function , for count ways ; Backtracking Make vertex unvisited ; Return total ways ; Function to count total ways to reach destination ; Loop to make all vertex unvisited , Initially ; Make source visited ; Print total ways
class GFG { static int countWays ( int mtrx [ ] [ ] , int vrtx , int i , int dest , boolean visited [ ] ) { if ( i == dest ) { return 1 ; } int total = 0 ; for ( int j = 0 ; j < vrtx ; j ++ ) { if ( mtrx [ i ] [ j ] == 1 && ! visited [ j ] ) { visited [ j ] = true ; total += countWays ( mtrx , vrtx , j , dest , visited ) ; visited [ j ] = false ; } } return total ; } static int totalWays ( int mtrx [ ] [ ] , int vrtx , int src , int dest ) { boolean [ ] visited = new boolean [ vrtx ] ; for ( int i = 0 ; i < vrtx ; i ++ ) { visited [ i ] = false ; } visited [ src ] = true ; return countWays ( mtrx , vrtx , src , dest , visited ) ; } public static void main ( String [ ] args ) { int vrtx = 11 ; int mtrx [ ] [ ] = { { 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 } , { 1 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 1 , 0 } , { 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 } } ; int src = 3 ; int dest = 9 ; System . out . print ( totalWays ( mtrx , vrtx , src - 1 , dest - 1 ) ) ; } }
Print path from given Source to Destination in 2 | Java program for printing a path from given source to destination ; Function to print the path ; Base condition ; Pop stores elements ; Recursive call for printing stack In reverse order ; Function to store the path into The stack , if path exist ; Base condition ; Push current elements ; Condition to check whether reach to the Destination or not ; Increment ' x ' ordinate of source by ( 2 * x + y ) Keeping ' y ' constant ; Increment ' y ' ordinate of source by ( 2 * y + x ) Keeping ' x ' constant ; Pop current elements form stack ; If no path exists ; Utility function to check whether path exist or not ; To store x co - ordinate ; To store y co - ordinate ; Function to find the path ; Print - 1 , if path doesn 't exist ; Driver code ; Function call
import java . util . * ; class GFG { static void printExistPath ( Stack < Integer > sx , Stack < Integer > sy , int last ) { if ( sx . isEmpty ( ) || sy . isEmpty ( ) ) { return ; } int x = sx . peek ( ) ; int y = sy . peek ( ) ; sx . pop ( ) ; sy . pop ( ) ; printExistPath ( sx , sy , last ) ; if ( sx . size ( ) == last - 1 ) { System . out . print ( " ( " + x + " , ▁ " + y + " ) " ) ; } else { System . out . print ( " ( " + x + " , ▁ " + y + " ) - > " ) ; } } static boolean storePath ( int srcX , int srcY , int destX , int destY , Stack < Integer > sx , Stack < Integer > sy ) { if ( srcX > destX srcY > destY ) { return false ; } sx . add ( srcX ) ; sy . add ( srcY ) ; if ( srcX == destX && srcY == destY ) { printExistPath ( sx , sy , sx . size ( ) ) ; return true ; } if ( storePath ( ( 2 * srcX ) + srcY , srcY , destX , destY , sx , sy ) ) { return true ; } if ( storePath ( srcX , ( 2 * srcY ) + srcX , destX , destY , sx , sy ) ) { return true ; } sx . pop ( ) ; sy . pop ( ) ; return false ; } static boolean isPathExist ( int srcX , int srcY , int destX , int destY ) { Stack < Integer > sx = new Stack < Integer > ( ) ; Stack < Integer > sy = new Stack < Integer > ( ) ; return storePath ( srcX , srcY , destX , destY , sx , sy ) ; } static void printPath ( int srcX , int srcY , int destX , int destY ) { if ( ! isPathExist ( srcX , srcY , destX , destY ) ) { System . out . print ( " - 1" ) ; } } public static void main ( String [ ] args ) { int srcX = 5 , srcY = 8 ; int destX = 83 , destY = 21 ; printPath ( srcX , srcY , destX , destY ) ; } }
Count even paths in Binary Tree | Java program for the above approach ; A Tree node ; Utility function to create a new node ; Utility function to count the even path in a given Binary tree ; Base Condition , when node pointer becomes null or node value is odd ; Increment count when encounter leaf node with all node value even ; Left recursive call , and save the value of count ; Right recursive call , and return value of count ; Function to count the even paths in a given Binary tree ; Function call with count = 0 ; Driver Code ; Tree ; Function call
import java . util . * ; class GFG { static class Node { int key ; Node left , right ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return ( temp ) ; } static int evenPaths ( Node node , int count ) { if ( node == null || ( node . key % 2 != 0 ) ) { return count ; } if ( node . left == null && node . right == null ) { count ++ ; } count = evenPaths ( node . left , count ) ; return evenPaths ( node . right , count ) ; } static int countEvenPaths ( Node node ) { return evenPaths ( node , 0 ) ; } public static void main ( String args [ ] ) { Node root = newNode ( 12 ) ; root . left = newNode ( 13 ) ; root . right = newNode ( 12 ) ; root . right . left = newNode ( 14 ) ; root . right . right = newNode ( 16 ) ; root . right . left . left = newNode ( 21 ) ; root . right . left . right = newNode ( 22 ) ; root . right . right . left = newNode ( 22 ) ; root . right . right . right = newNode ( 24 ) ; root . right . right . right . left = newNode ( 8 ) ; System . out . println ( countEvenPaths ( root ) ) ; } }
Sum of subsets of all the subsets of an array | O ( 3 ^ N ) | Java implementation of the approach ; To store the final ans ; Function to sum of all subsets of a given array ; Base case ; Recursively calling subsetSum ; Function to generate the subsets ; Base - case ; Finding the sum of all the subsets of the generated subset ; Recursively accepting and rejecting the current number ; Driver Code
import java . util . * ; class GFG { static Vector < Integer > c = new Vector < > ( ) ; static int ans = 0 ; static void subsetSum ( int i , int curr ) { if ( i == c . size ( ) ) { ans += curr ; return ; } subsetSum ( i + 1 , curr + c . elementAt ( i ) ) ; subsetSum ( i + 1 , curr ) ; } static void subsetGen ( int [ ] arr , int i , int n ) { if ( i == n ) { subsetSum ( 0 , 0 ) ; return ; } subsetGen ( arr , i + 1 , n ) ; c . add ( arr [ i ] ) ; subsetGen ( arr , i + 1 , n ) ; c . remove ( c . size ( ) - 1 ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 1 } ; int n = arr . length ; subsetGen ( arr , 0 , n ) ; System . out . println ( ans ) ; } }
Print the DFS traversal step | Java program to print the complete DFS - traversal of graph using back - tracking ; Function to print the complete DFS - traversal ; Check if all th node is visited or not and count unvisited nodes ; If all the node is visited return ; ; Mark not visited node as visited ; Track the current edge ; Print the node ; Check for not visited node and proceed with it . ; call the DFs function if not visited ; Backtrack through the last visited nodes ; Function to call the DFS function which prints the DFS - traversal stepwise ; Create a array of visited node ; Vector to track last visited road ; Initialize all the node with false ; call the function ; Function to insert edges in Graph ; Driver Code ; number of nodes and edges in the graph ; Function call to create the graph ; Call the function to print
import java . io . * ; import java . util . * ; class GFG { static int N = 1000 ; static ArrayList < ArrayList < Integer > > adj = new ArrayList < ArrayList < Integer > > ( ) ; static void dfsUtil ( int u , int node , boolean visited [ ] , ArrayList < ArrayList < Integer > > road_used , int parent , int it ) { int c = 0 ; for ( int i = 0 ; i < node ; i ++ ) if ( visited [ i ] ) c ++ ; if ( c == node ) return ; visited [ u ] = true ; road_used . add ( new ArrayList < Integer > ( Arrays . asList ( parent , u ) ) ) ; System . out . print ( u + " ▁ " ) ; for ( int x : adj . get ( u ) ) { if ( ! visited [ x ] ) { dfsUtil ( x , node , visited , road_used , u , it + 1 ) ; } } for ( int y = 0 ; y < road_used . size ( ) ; y ++ ) { if ( road_used . get ( y ) . get ( 1 ) == u ) { dfsUtil ( road_used . get ( y ) . get ( 0 ) , node , visited , road_used , u , it + 1 ) ; } } } static void dfs ( int node ) { boolean [ ] visited = new boolean [ node ] ; ArrayList < ArrayList < Integer > > road_used = new ArrayList < ArrayList < Integer > > ( ) ; for ( int i = 0 ; i < node ; i ++ ) { visited [ i ] = false ; } dfsUtil ( 0 , node , visited , road_used , - 1 , 0 ) ; } static void insertEdge ( int u , int v ) { adj . get ( u ) . add ( v ) ; adj . get ( v ) . add ( u ) ; } public static void main ( String [ ] args ) { int node = 11 , edge = 13 ; for ( int i = 0 ; i < N ; i ++ ) { adj . add ( new ArrayList < Integer > ( ) ) ; } insertEdge ( 0 , 1 ) ; insertEdge ( 0 , 2 ) ; insertEdge ( 1 , 5 ) ; insertEdge ( 1 , 6 ) ; insertEdge ( 2 , 4 ) ; insertEdge ( 2 , 9 ) ; insertEdge ( 6 , 7 ) ; insertEdge ( 6 , 8 ) ; insertEdge ( 7 , 8 ) ; insertEdge ( 2 , 3 ) ; insertEdge ( 3 , 9 ) ; insertEdge ( 3 , 10 ) ; insertEdge ( 9 , 10 ) ; dfs ( node ) ; } }
Find Maximum number possible by doing at | Java program to find maximum integer possible by doing at - most K swap operations on its digits . ; Function to find maximum integer possible by doing at - most K swap operations on its digits ; Return if no swaps left ; Consider every digit ; Compare it with all digits after it ; if digit at position i is less than digit at position j , swap it and check for maximum number so far and recurse for remaining swaps ; swap str [ i ] with str [ j ] ; If current num is more than maximum so far ; recurse of the other k - 1 swaps ; Backtrack ; Driver code
import java . util . * ; class GFG { static String max ; static void findMaximumNum ( char [ ] str , int k ) { if ( k == 0 ) return ; int n = str . length ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( str [ i ] < str [ j ] ) { char t = str [ i ] ; str [ i ] = str [ j ] ; str [ j ] = t ; if ( String . valueOf ( str ) . compareTo ( max ) > 0 ) max = String . valueOf ( str ) ; findMaximumNum ( str , k - 1 ) ; char c = str [ i ] ; str [ i ] = str [ j ] ; str [ j ] = c ; } } } } public static void main ( String [ ] args ) { String str = "129814999" ; int k = 4 ; max = str ; findMaximumNum ( str . toCharArray ( ) , k ) ; System . out . print ( max + "NEW_LINE"); } }
Print all possible paths from top left to bottom right of a mXn matrix | Java program to Print all possible paths from top left to bottom right of a mXn matrix ; If we reach the bottom of maze , we can only move right ; path . append ( maze [ i ] [ k ] ) ; If we hit this block , it means one path is completed . Add it to paths list and print ; If we reach to the right most corner , we can only move down ; path . append ( maze [ j ] [ k ] ) If we hit this block , it means one path is completed . Add it to paths list and print ; Add current element to the path list path . append ( maze [ i ] [ j ] ) ; Move down in y direction and call findPathsUtil recursively ; Move down in y direction and call findPathsUtil recursively ;  ; Driver code
import java . io . * ; import java . util . * ; class GFG { static ArrayList < ArrayList < Integer > > allPaths = new ArrayList < ArrayList < Integer > > ( ) ; static void findPathsUtil ( ArrayList < ArrayList < Integer > > maze , int m , int n , int i , int j , ArrayList < Integer > path , int indx ) { if ( i == m - 1 ) { for ( int k = j ; k < n ; k ++ ) { path . set ( indx + k - j , maze . get ( i ) . get ( k ) ) ; } System . out . print ( " [ " + path . get ( 0 ) + " , ▁ " ) ; for ( int z = 1 ; z < path . size ( ) - 1 ; z ++ ) { System . out . print ( path . get ( z ) + " , ▁ " ) ; } System . out . println ( path . get ( path . size ( ) - 1 ) + " ] " ) ; allPaths . add ( path ) ; return ; } if ( j == n - 1 ) { for ( int k = i ; k < m ; k ++ ) { path . set ( indx + k - i , maze . get ( k ) . get ( j ) ) ; } System . out . print ( " [ " + path . get ( 0 ) + " , ▁ " ) ; for ( int z = 1 ; z < path . size ( ) - 1 ; z ++ ) { System . out . print ( path . get ( z ) + " , ▁ " ) ; } System . out . println ( path . get ( path . size ( ) - 1 ) + " ] " ) ; allPaths . add ( path ) ; return ; } path . set ( indx , maze . get ( i ) . get ( j ) ) ; findPathsUtil ( maze , m , n , i + 1 , j , path , indx + 1 ) ; findPathsUtil ( maze , m , n , i , j + 1 , path , indx + 1 ) ; } static void findPaths ( ArrayList < ArrayList < Integer > > maze , int m , int n ) { ArrayList < Integer > path = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < m + n - 1 ; i ++ ) { path . add ( 0 ) ; } findPathsUtil ( maze , m , n , 0 , 0 , path , 0 ) ; } public static void main ( String [ ] args ) { ArrayList < ArrayList < Integer > > maze = new ArrayList < ArrayList < Integer > > ( ) ; maze . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 2 , 3 ) ) ) ; maze . add ( new ArrayList < Integer > ( Arrays . asList ( 4 , 5 , 6 ) ) ) ; maze . add ( new ArrayList < Integer > ( Arrays . asList ( 7 , 8 , 9 ) ) ) ; findPaths ( maze , 3 , 3 ) ; } }
Given an array A [ ] and a number x , check for pair in A [ ] with sum as x | Java program to check if there exists a pair in array whose sum results in x . ; Function to find and print pair ; Driver code
class GFG { static boolean chkPair ( int A [ ] , int size , int x ) { for ( int i = 0 ; i < ( size - 1 ) ; i ++ ) { for ( int j = ( i + 1 ) ; j < size ; j ++ ) { if ( A [ i ] + A [ j ] == x ) { System . out . println ( " Pair ▁ with ▁ a ▁ given ▁ sum ▁ " + x + " ▁ is ▁ ( " + A [ i ] + " , ▁ " + A [ j ] + " ) " ) ; return true ; } } } return false ; } public static void main ( String [ ] args ) { int A [ ] = { 0 , - 1 , 2 , - 3 , 1 } ; int x = - 2 ; int size = A . length ; if ( chkPair ( A , size , x ) ) { System . out . println ( " Valid ▁ pair ▁ exists " ) ; } else { System . out . println ( " No ▁ valid ▁ pair ▁ exists ▁ for ▁ " + x ) ; } } }
Reduce the array by deleting elements which are greater than all elements to its left | Java program for the above approach ; Function to implement merging of arr [ ] ; Function to delete all elements which satisfy the condition A [ i ] > A [ i - 1 ] ; Divide array into its subarray ; Getting back merged array with all its right element greater than left one . ; Driver Code ; Given array arr [ ]
import java . util . ArrayList ; import java . util . Arrays ; class GFG { static ArrayList < Integer > merge ( ArrayList < Integer > x , ArrayList < Integer > y ) { for ( Integer i : y ) { if ( x . get ( x . size ( ) - 1 ) > i ) x . add ( i ) ; } return x ; } static ArrayList < Integer > mergeDel ( ArrayList < Integer > l ) { if ( l . size ( ) == 1 ) return l ; int m = l . size ( ) / 2 ; ArrayList < Integer > temp1 = new ArrayList < > ( l . subList ( 0 , m ) ) ; ArrayList < Integer > temp2 = new ArrayList < > ( l . subList ( m , l . size ( ) ) ) ; return merge ( mergeDel ( temp1 ) , mergeDel ( temp2 ) ) ; } public static void main ( String [ ] args ) { Integer [ ] ar = { 5 , 4 , 3 , 2 , 1 } ; ArrayList < Integer > arr = new ArrayList < > ( Arrays . asList ( ar ) ) ; ArrayList < Integer > ans = mergeDel ( arr ) ; System . out . print ( " [ ▁ " ) ; for ( Integer x : ans ) System . out . print ( x + " , ▁ " ) ; System . out . println ( " ] " ) ; } }
Significant Inversions in an Array | Java implementation of the above approach ; Function that sorts the input array and returns the number of inversions in the array ; Recursive function that sorts the input array and returns the number of inversions in the array ; Divide the array into two parts and call _mergeSortAndCountInv ( ) for each of the parts ; Inversion count will be sum of the inversions in the left - part , the right - part and the number of inversions in merging ; Merge the two parts ; Function that merges the two sorted arrays and returns the inversion count in the arrays ; i is the index for the left subarray ; j is the index for the right subarray ; k is the index for the resultant merged subarray ; First pass to count number of significant inversions ; i is the index for the left subarray ; j is the index for the right subarray ; k is the index for the resultant merged subarray ; Second pass to merge the two sorted arrays ; Copy the remaining elements of the left subarray ( if there are any ) to temp ; Copy the remaining elements of the right subarray ( if there are any ) to temp ; Copy back the merged elements to the original array ; Driver code
class GFG { static int mergeSort ( int arr [ ] , int array_size ) { int temp [ ] = new int [ array_size ] ; return _mergeSort ( arr , temp , 0 , array_size - 1 ) ; } static int _mergeSort ( int arr [ ] , int temp [ ] , int left , int right ) { int mid , inv_count = 0 ; if ( right > left ) { mid = ( right + left ) / 2 ; inv_count = _mergeSort ( arr , temp , left , mid ) ; inv_count += _mergeSort ( arr , temp , mid + 1 , right ) ; inv_count += merge ( arr , temp , left , mid + 1 , right ) ; } return inv_count ; } static int merge ( int arr [ ] , int temp [ ] , int left , int mid , int right ) { int i , j , k ; int inv_count = 0 ; i = left ; j = mid ; k = left ; while ( ( i <= mid - 1 ) && ( j <= right ) ) { if ( arr [ i ] > 2 * arr [ j ] ) { inv_count += ( mid - i ) ; j ++ ; } else { i ++ ; } } i = left ; j = mid ; k = left ; while ( ( i <= mid - 1 ) && ( j <= right ) ) { if ( arr [ i ] <= arr [ j ] ) { temp [ k ++ ] = arr [ i ++ ] ; } else { temp [ k ++ ] = arr [ j ++ ] ; } } while ( i <= mid - 1 ) temp [ k ++ ] = arr [ i ++ ] ; while ( j <= right ) temp [ k ++ ] = arr [ j ++ ] ; for ( i = left ; i <= right ; i ++ ) arr [ i ] = temp [ i ] ; return inv_count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 20 , 6 , 4 , 5 } ; int n = arr . length ; System . out . println ( mergeSort ( arr , n ) ) ; } }
Find the number of different numbers in the array after applying the given operation q times | Java implementation for above approach ; To store the tree in lazy propagation ; To store the different numbers ; Function to update in the range [ x , y ) with given value ; check out of bound ; check for complete overlap ; find the mid number ; check for pending updates ; make lazy [ id ] = 0 , so that it has no pending updates ; call for two child nodes ; Function to find non - zero integers in the range [ l , r ) ; if id contains positive number ; There is no need to see the children , because all the interval have same number ; check for out of bound ; find the middle number ; call for two child nodes ; Driver Code ; size of the array and number of queries ; Update operation for l , r , x , id , 0 , n ; Query operation to get answer in the range [ 0 , n - 1 ] ; Print the count of non - zero elements
import java . util . * ; class geeks { static int N = 100005 ; static int [ ] lazy = new int [ 4 * N ] ; static Set < Integer > se = new HashSet < Integer > ( ) ; public static void update ( int x , int y , int value , int id , int l , int r ) { if ( x >= r l >= y ) return ; if ( x <= l && r <= y ) { lazy [ id ] = value ; return ; } int mid = ( l + r ) / 2 ; if ( lazy [ id ] != 0 ) lazy [ 2 * id ] = lazy [ 2 * id + 1 ] = lazy [ id ] ; lazy [ id ] = 0 ; update ( x , y , value , 2 * id , l , mid ) ; update ( x , y , value , 2 * id + 1 , mid , r ) ; } public static void query ( int id , int l , int r ) { if ( lazy [ id ] != 0 ) { se . add ( lazy [ id ] ) ; return ; } if ( r - l < 2 ) return ; int mid = ( l + r ) / 2 ; query ( 2 * id , l , mid ) ; query ( 2 * id + 1 , mid , r ) ; } public static void main ( String [ ] args ) { int n = 5 , q = 3 ; update ( 1 , 4 , 1 , 1 , 0 , n ) ; update ( 0 , 2 , 2 , 1 , 0 , n ) ; update ( 3 , 4 , 3 , 1 , 0 , n ) ; query ( 1 , 0 , n ) ; System . out . println ( se . size ( ) ) ; } }