codeparrot/xlcost-text-to-code huggingface dataset:
Minimize cost to color all the vertices of an Undirected Graph using given operation | Java program to find the minimum cost to color all vertices of an Undirected Graph ; Function to add edge in the given graph ; Function to perform DFS traversal and find the node with minimum cost ; Update the minimum cost ; Recur for all connected nodes ; Function to calculate and return the minimum cost of coloring all vertices of the Undirected Graph ; Marks if a vertex is visited or not ; Perform DFS traversal ; If vertex is not visited ; Update minimum cost ; Return the final cost ; Driver Code
import java . util . * ; class GFG { static final int MAX = 10 ; @ SuppressWarnings ( " unchecked " ) static Vector < Integer > [ ] adj = new Vector [ MAX ] ; static int min_cost_node ; static void addEdge ( int u , int v ) { adj [ u ] . add ( v ) ; adj [ v ] . add ( u ) ; } static void dfs ( int v , int cost [ ] , boolean vis [ ] ) { vis [ v ] = true ; min_cost_node = Math . min ( min_cost_node , cost [ v - 1 ] ) ; for ( int i = 0 ; i < adj [ v ] . size ( ) ; i ++ ) { if ( vis [ adj [ v ] . get ( i ) ] == false ) { dfs ( adj [ v ] . get ( i ) , cost , vis ) ; } } } static int minimumCost ( int V , int cost [ ] ) { boolean [ ] vis = new boolean [ V + 1 ] ; Arrays . fill ( vis , false ) ; int min_cost = 0 ; for ( int i = 1 ; i <= V ; i ++ ) { if ( ! vis [ i ] ) { min_cost_node = Integer . MAX_VALUE ; dfs ( i , cost , vis ) ; min_cost += min_cost_node ; } } return min_cost ; } public static void main ( String [ ] args ) { int V = 6 , E = 5 ; int cost [ ] = { 12 , 25 , 8 , 11 , 10 , 7 } ; for ( int i = 0 ; i < adj . length ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 3 , 2 ) ; addEdge ( 2 , 5 ) ; addEdge ( 4 , 6 ) ; int min_cost = minimumCost ( V , cost ) ; System . out . print ( min_cost + "NEW_LINE"); } }
Maximize count of set bits in a root to leaf path in a binary tree | Java Program to implement the above approach ; Node structure ; Pointers to left and right child ; Initialize constructor ; Function to find the maximum count of setbits in a root to leaf ; Check if root is not null ; Update the maximum count of setbits ; Traverse left of binary tree ; Traverse right of the binary tree ; Driver Code
import java . util . * ; class GFG { static int maxm = 0 ; static class Node { int val ; Node left , right ; Node ( int x ) { val = x ; left = null ; right = null ; } } ; static void maxm_setbits ( Node root , int ans ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) { ans += Integer . bitCount ( root . val ) ; maxm = Math . max ( ans , maxm ) ; return ; } maxm_setbits ( root . left , ans + Integer . bitCount ( root . val ) ) ; maxm_setbits ( root . right , ans + Integer . bitCount ( root . val ) ) ; } public static void main ( String [ ] args ) { Node root = new Node ( 15 ) ; root . left = new Node ( 3 ) ; root . right = new Node ( 7 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 1 ) ; root . right . left = new Node ( 31 ) ; root . right . right = new Node ( 9 ) ; maxm_setbits ( root , 0 ) ; System . out . print ( maxm + "NEW_LINE"); } }
Minimize count of divisions by D to obtain at least K equal array elements | Java program to implement the above approach ; Function to return minimum number of moves required ; Stores the number of moves required to obtain respective values from the given array ; Traverse the array ; Insert 0 into V [ a [ i ] ] as it is the initial state ; Insert the moves required to obtain current a [ i ] ; Traverse v [ ] to obtain minimum count of moves ; Check if there are at least K equal elements for v [ i ] ; Add the sum of minimum K moves ; Update answer ; Return the final answer ; Driver Code
import java . util . * ; class GFG { @ SuppressWarnings ( " unchecked " ) static int getMinimumMoves ( int n , int k , int d , int [ ] a ) { int MAX = 100000 ; Vector < Integer > [ ] v = new Vector [ MAX ] ; for ( int i = 0 ; i < v . length ; i ++ ) v [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = 0 ; v [ a [ i ] ] . add ( 0 ) ; while ( a [ i ] > 0 ) { a [ i ] /= d ; cnt ++ ; v [ a [ i ] ] . add ( cnt ) ; } } int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( v [ i ] . size ( ) >= k ) { int move = 0 ; Collections . sort ( v [ i ] ) ; for ( int j = 0 ; j < k ; j ++ ) { move += v [ i ] . get ( j ) ; } ans = Math . min ( ans , move ) ; } } return ans ; } public static void main ( String [ ] args ) { int N = 5 , K = 3 , D = 2 ; int [ ] A = { 1 , 2 , 3 , 4 , 5 } ; System . out . print ( getMinimumMoves ( N , K , D , A ) ) ; } }
Longest subarray with odd product | Java program to find the longest subarray with odd product ; Function to return length of longest subarray with odd product ; If even element is encountered ; Update maximum ; Driver Code
import java . util . * ; class GFG { static int Maxlen ( int arr [ ] , int n ) { int ans = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) count = 0 ; else count ++ ; ans = Math . max ( ans , count ) ; } return ans ; } public static void main ( String s [ ] ) { int arr [ ] = { 1 , 7 , 2 } ; int n = arr . length ; System . out . println ( Maxlen ( arr , n ) ) ; } }
Min difference between maximum and minimum element in all Y size subarrays | Java program for the above approach ; Function to get the maximum of all the subarrays of size Y ; ith index of maxarr array will be the index upto which Arr [ i ] is maximum ; Stack is used to find the next larger element and keeps track of index of current iteration ; Loop for remaining indexes ; j < i used to keep track whether jth element is inside or outside the window ; Return submax ; Function to get the minimum for all subarrays of size Y ; ith index of minarr array will be the index upto which Arr [ i ] is minimum ; Stack is used to find the next smaller element and keeping track of index of current iteration ; Loop for remaining indexes ; j < i used to keep track whether jth element is inside or outside the window ; Return submin ; Function to get minimum difference ; Create submin and submax arrays ; Store initial difference ; Calculate temporary difference ; Final minimum difference ; Driver code ; Given array arr [ ] ; Given subarray size ; Function Call
import java . util . * ; class GFG { static Vector < Integer > get_submaxarr ( int [ ] arr , int n , int y ) { int j = 0 ; Stack < Integer > stk = new Stack < Integer > ( ) ; int [ ] maxarr = new int [ n ] ; Arrays . fill ( maxarr , 0 ) ; stk . push ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { while ( stk . size ( ) != 0 && arr [ i ] > arr [ stk . peek ( ) ] ) { maxarr [ stk . peek ( ) ] = i - 1 ; stk . pop ( ) ; } stk . push ( i ) ; } while ( stk . size ( ) != 0 ) { maxarr [ stk . size ( ) ] = n - 1 ; stk . pop ( ) ; } Vector < Integer > submax = new Vector < Integer > ( ) ; for ( int i = 0 ; i <= n - y ; i ++ ) { while ( maxarr [ j ] < i + y - 1 j < i ) { j ++ ; } submax . add ( arr [ j ] ) ; } return submax ; } static Vector < Integer > get_subminarr ( int [ ] arr , int n , int y ) { int j = 0 ; Stack < Integer > stk = new Stack < Integer > ( ) ; int [ ] minarr = new int [ n ] ; Arrays . fill ( minarr , 0 ) ; stk . push ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { while ( stk . size ( ) != 0 && arr [ i ] < arr [ stk . peek ( ) ] ) { minarr [ stk . peek ( ) ] = i ; stk . pop ( ) ; } stk . push ( i ) ; } while ( stk . size ( ) != 0 ) { minarr [ stk . peek ( ) ] = n ; stk . pop ( ) ; } Vector < Integer > submin = new Vector < Integer > ( ) ; for ( int i = 0 ; i <= n - y ; i ++ ) { while ( minarr [ j ] <= i + y - 1 j < i ) { j ++ ; } submin . add ( arr [ j ] ) ; } return submin ; } static void getMinDifference ( int [ ] Arr , int N , int Y ) { Vector < Integer > submin = get_subminarr ( Arr , N , Y ) ; Vector < Integer > submax = get_submaxarr ( Arr , N , Y ) ; int minn = submax . get ( 0 ) - submin . get ( 0 ) ; int b = submax . size ( ) ; for ( int i = 1 ; i < b ; i ++ ) { int dif = submax . get ( i ) - submin . get ( i ) + 1 ; minn = Math . min ( minn , dif ) ; } System . out . print ( minn ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 3 , 2 , 2 } ; int N = arr . length ; int Y = 4 ; getMinDifference ( arr , N , Y ) ; } }
Least root of given quadratic equation for value greater than equal to K | Java program for the above approach ; Function to calculate value of quadratic equation for some x ; Function to calculate the minimum value of x such that F ( x ) >= K using binary search ; Start and end value for binary search ; Binary Search ; Computing F ( mid ) and F ( mid - 1 ) ; If F ( mid ) >= K and F ( mid - 1 ) < K return mid ; If F ( mid ) < K go to mid + 1 to end ; If F ( mid ) > K go to start to mid - 1 ; If no such value exist ; Driver code ; Given coefficients of Equations ; Find minimum value of x
import java . util . * ; class GFG { static int func ( int A , int B , int C , int x ) { return ( A * x * x + B * x + C ) ; } static int findMinx ( int A , int B , int C , int K ) { int start = 1 ; int end = ( int ) Math . ceil ( Math . sqrt ( K ) ) ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; int x = func ( A , B , C , mid ) ; int Y = func ( A , B , C , mid - 1 ) ; if ( x >= K && Y < K ) { return mid ; } else if ( x < K ) { start = mid + 1 ; } else { end = mid - 1 ; } } return - 1 ; } public static void main ( String [ ] args ) { int A = 3 , B = 4 , C = 5 , K = 6 ; System . out . println ( findMinx ( A , B , C , K ) ) ; } }
Find the node at the center of an N | Java implementation of the above approach ; To create tree ; Function to store the path from given vertex to the target vertex in a vector path ; If the target node is found , push it into path vector ; To prevent visiting a node already visited ; Recursive call to the neighbours of current node inorder to get the path ; Function to obtain and return the farthest node from a given vertex ; If the current height is maximum so far , then save the current node ; Iterate over all the neighbours of current node ; This is to prevent visiting a already visited node ; Next call will be at 1 height higher than our current height ; Function to add edges ; Reset to - 1 ; Reset to - 1 ; Stores one end of the diameter ; Reset the maxHeight ; Stores the second end of the diameter ; Store the diameter into the vector path ; Diameter is equal to the path between the two farthest nodes leaf1 and leaf2 ; Driver code
import java . util . * ; class GFG { static Map < Integer , ArrayList < Integer > > tree ; static ArrayList < Integer > path ; static int maxHeight , maxHeightNode ; static boolean getDiameterPath ( int vertex , int targetVertex , int parent , ArrayList < Integer > path ) { if ( vertex == targetVertex ) { path . add ( vertex ) ; return true ; } for ( Integer i : tree . get ( vertex ) ) { if ( i == parent ) continue ; if ( getDiameterPath ( i , targetVertex , vertex , path ) ) { path . add ( vertex ) ; return true ; } } return false ; } static void farthestNode ( int vertex , int parent , int height ) { if ( height > maxHeight ) { maxHeight = height ; maxHeightNode = vertex ; } if ( tree . get ( vertex ) != null ) for ( Integer i : tree . get ( vertex ) ) { if ( i == parent ) continue ; farthestNode ( i , vertex , height + 1 ) ; } } static void addedge ( int a , int b ) { if ( tree . get ( a ) == null ) tree . put ( a , new ArrayList < > ( ) ) ; tree . get ( a ) . add ( b ) ; if ( tree . get ( b ) == null ) tree . put ( b , new ArrayList < > ( ) ) ; tree . get ( b ) . add ( a ) ; } static void FindCenter ( int n ) { maxHeight = - 1 ; maxHeightNode = - 1 ; farthestNode ( 0 , - 1 , 0 ) ; int leaf1 = maxHeightNode ; maxHeight = - 1 ; farthestNode ( maxHeightNode , - 1 , 0 ) ; int leaf2 = maxHeightNode ; path = new ArrayList < > ( ) ; getDiameterPath ( leaf1 , leaf2 , - 1 , path ) ; int pathSize = path . size ( ) ; if ( pathSize % 2 == 1 ) { System . out . println ( path . get ( pathSize / 2 ) ) ; } else { System . out . println ( path . get ( pathSize / 2 ) + " , ▁ " + path . get ( ( pathSize - 1 ) / 2 ) ) ; } } public static void main ( String [ ] args ) { int N = 4 ; tree = new HashMap < > ( ) ; addedge ( 1 , 0 ) ; addedge ( 1 , 2 ) ; addedge ( 1 , 3 ) ; FindCenter ( N ) ; } }
K | Java program to find k - th term of N merged Arithmetic Progressions ; Function to count and return the number of values less than equal to N present in the set ; Check whether j - th bit is set bit or not ; Function to implement Binary Search to find K - th element ; Find middle index of the array ; Search in the left half ; Search in the right half ; If exactly K elements are present ; Driver Code
import java . util . * ; class GFG { static final int maxm = 1000000000 ; static int count ( int [ ] v , int n ) { int i , odd = 0 , even = 0 ; int j , d , count ; int t = ( int ) 1 << v . length ; int size = v . length ; for ( i = 1 ; i < t ; i ++ ) { d = 1 ; count = 0 ; for ( j = 0 ; j < size ; j ++ ) { if ( ( i & ( 1 << j ) ) > 0 ) { d *= v [ j ] ; count ++ ; } } if ( count % 2 == 1 ) odd += n / d ; else even += n / d ; } return ( odd - even ) ; } static int BinarySearch ( int l , int r , int [ ] v , int key ) { int mid ; while ( r - l > 1 ) { mid = ( l + r ) / 2 ; if ( key <= count ( v , mid ) ) { r = mid ; } else { l = mid ; } } if ( key == count ( v , l ) ) return l ; else return r ; } public static void main ( String [ ] args ) { int N = 2 , K = 10 ; int [ ] v = { 2 , 3 } ; System . out . print ( BinarySearch ( 1 , maxm , v , K ) + "NEW_LINE"); } }
Find integral points with minimum distance from given set of integers using BFS | Java implementation of above approach ; Function to find points at minimum distance ; Hash to store points that are encountered ; Queue to store initial set of points ; List to store integral points ; Using bfs to visit nearest points from already visited points ; Get first element from queue ; Check if ( x - 1 ) is not encountered so far ; Update hash with this new element ; Insert ( x - 1 ) into queue ; Push ( x - 1 ) as new element ; Decrement counter by 1 ; Check if ( x + 1 ) is not encountered so far ; Update hash with this new element ; Insert ( x + 1 ) into queue ; Push ( x + 1 ) as new element ; Decrement counter by 1 ; Print result array ; Driver code
import java . util . HashMap ; import java . util . LinkedList ; import java . util . Map ; import java . util . Queue ; class Geeks { static void minDistancePoints ( int A [ ] , int K , int n ) { Map < Integer , Boolean > m = new HashMap < Integer , Boolean > ( ) ; Queue < Integer > q = new LinkedList < Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { m . put ( A [ i ] , true ) ; q . add ( A [ i ] ) ; } LinkedList < Integer > ans = new LinkedList < Integer > ( ) ; while ( K > 0 ) { int x = q . poll ( ) ; if ( ! m . containsKey ( x - 1 ) && K > 0 ) { m . put ( x - 1 , true ) ; q . add ( x - 1 ) ; ans . add ( x - 1 ) ; K -- ; } if ( ! m . containsKey ( x + 1 ) && K > 0 ) { m . put ( x + 1 , true ) ; q . add ( x + 1 ) ; ans . add ( x + 1 ) ; K -- ; } } for ( Integer i : ans ) System . out . print ( i + " ▁ " ) ; } public static void main ( String [ ] args ) { int A [ ] = new int [ ] { - 1 , 4 , 6 } ; int K = 3 ; int n = A . length ; minDistancePoints ( A , K , n ) ; } }
Leftmost Column with atleast one 1 in a row | Java implementation to find the Leftmost Column with atleast a 1 in a sorted binary matrix ; Function to search for the leftmost column of the matrix with atleast a 1 in sorted binary matrix ; Loop to iterate over all the rows of the matrix ; Binary Search to find the leftmost occurence of the 1 ; Condition if the column contains the 1 at this position of matrix ; If there is a better solution then update the answer ; Condition if the solution doesn 't exist in the matrix ; Driver Code
import java . util . * ; class GFG { static final int N = 3 ; static int search ( int mat [ ] [ ] , int n , int m ) { int i , a = Integer . MAX_VALUE ; for ( i = 0 ; i < n ; i ++ ) { int low = 0 ; int high = m - 1 ; int mid ; int ans = Integer . MAX_VALUE ; while ( low <= high ) { mid = ( low + high ) / 2 ; if ( mat [ i ] [ mid ] == 1 ) { if ( mid == 0 ) { ans = 0 ; break ; } else if ( mat [ i ] [ mid - 1 ] == 0 ) { ans = mid ; break ; } } if ( mat [ i ] [ mid ] == 1 ) high = mid - 1 ; else low = mid + 1 ; } if ( ans < a ) a = ans ; } if ( a == Integer . MAX_VALUE ) return - 1 ; return a + 1 ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 0 , 0 } , { 0 , 0 , 1 } , { 0 , 1 , 1 } } ; System . out . print ( search ( mat , 3 , 3 ) ) ; } }
Largest index for each distinct character in given string with frequency K | Java implementation of the approach ; Function to find largest index for each distinct character occuring exactly K times . ; finding all characters present in S ; Finding all distinct characters in S ; vector to store result for each character ; loop through each lower case English character ; if current character is absent in s ; getting current character ; finding count of character ch in S ; to store max Index encountred so far ; printing required result ; If no such character exists , print - 1 ; Driver code
import java . util . * ; class GFG { static class pair { char first ; int second ; public pair ( char first , int second ) { this . first = first ; this . second = second ; } } static void maxSubString ( char [ ] S , int K , int N ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < N ; ++ i ) { freq [ S [ i ] - ' a ' ] = 1 ; } Vector < pair > answer = new Vector < pair > ( ) ; for ( int i = 0 ; i < 26 ; ++ i ) { if ( freq [ i ] == 0 ) continue ; char ch = ( char ) ( i + 97 ) ; int count = 0 ; int index = - 1 ; for ( int j = 0 ; j < N ; ++ j ) { if ( S [ j ] == ch ) count ++ ; if ( count == K ) index = j ; } answer . add ( new pair ( ch , index ) ) ; } int flag = 0 ; for ( int i = 0 ; i < ( int ) answer . size ( ) ; ++ i ) { if ( answer . get ( i ) . second > - 1 ) { flag = 1 ; System . out . print ( answer . get ( i ) . first + " ▁ " + answer . get ( i ) . second + "NEW_LINE"); } } if ( flag == 0 ) System . out . print ( " - 1" + "NEW_LINE"); } public static void main ( String [ ] args ) { String S = " cbaabaacbcd " ; int K = 2 ; int N = S . length ( ) ; maxSubString ( S . toCharArray ( ) , K , N ) ; } }
Smallest number greater than n that can be represented as a sum of distinct power of k | Java implementation of the above approach ; Function to find the smallest number greater than or equal to n represented as the sum of distinct powers of k ; Vector P to store the base k representation of the number ; If the representation is >= 2 , then this power of k has to be added once again and then increase the next power of k and make the current power 0 ; Reduce all the lower power of k to 0 ; Check if the most significant bit also satisfy the above conditions ; Converting back from the k - nary representation to decimal form . ; Driver code
class GFG { static void greaterK ( int n , int k ) { int [ ] p = new int [ String . valueOf ( n ) . length ( ) + 2 ] ; int index = 0 ; int x = n ; while ( x > 0 ) { p [ index ] = ( int ) ( x % k ) ; x /= k ; index ++ ; } int idx = 0 ; for ( int i = 0 ; i < p . length - 1 ; ++ i ) { if ( p [ i ] >= 2 ) { p [ i ] = 0 ; p [ i + 1 ] ++ ; for ( int j = idx ; j < i ; ++ j ) { p [ j ] = 0 ; } idx = i + 1 ; } if ( p [ i ] == k ) { p [ i ] = 0 ; p [ i + 1 ] ++ ; } } int j = p . length - 1 ; if ( p [ j ] >= 2 ) { p [ index ] = 1 ; index ++ ; } int ans = 0 ; for ( int i = p . length - 1 ; i >= 0 ; -- i ) { ans = ans * k + p [ i ] ; } System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int n = 29 , k = 7 ; greaterK ( n , k ) ; } }
Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2 | Java program to toggle K - th bit of a number N ; Function that returns true if the string can be balanced ; Count to check the difference between the frequencies of ' ( ' and ' ) ' and count_1 is to find the minimum value of freq ( ' ( ' ) - freq ( ' ) ' ) ; Traverse the given string ; Increase the count ; Decrease the count ; Find the minimum value of freq ( ' ( ' ) - freq ( ' ) ' ) ; If the minimum difference is greater than or equal to - 1 and the overall difference is zero ; Driver code
class GFG { static boolean canBeBalanced ( String s , int n ) { int count = 0 , count_1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' ( ' ) count ++ ; else count -- ; count_1 = Math . min ( count_1 , count ) ; } if ( count_1 >= - 1 && count == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { String s = " ( ) ) ( ) ( " ; int n = s . length ( ) ; if ( canBeBalanced ( s , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Absolute difference between the XOR of Non | Java implementation of the approach ; Function to return max_element from an array ; Function to find the absolute difference between the XOR of non - primes and the XOR of primes in the given array ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store the XOR of primes in X1 and the XOR of non primes in X2 ; The number is prime ; The number is non - prime ; Return the absolute difference ; Driver Code ; Find the absolute difference
import java . util . Arrays ; class GFG { static int max_element ( int [ ] arr ) { int max = arr [ 0 ] ; for ( int ele : arr ) if ( max < ele ) max = ele ; return max ; } static int calculateDifference ( int [ ] arr , int n ) { int max_val = max_element ( arr ) ; boolean [ ] prime = new boolean [ max_val + 1 ] ; Arrays . fill ( prime , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int x1 = 1 , x2 = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) x1 ^= arr [ i ] ; else if ( arr [ i ] != 1 ) x2 ^= arr [ i ] ; } return Math . abs ( x1 - x2 ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 5 , 10 , 15 , 7 } ; int n = arr . length ; System . out . println ( calculateDifference ( arr , n ) ) ; } }
Count duplicates in a given linked list | Java implementation of the approach ; Representation of node ; Function to insert a node at the beginning ; Function to count the number of duplicate nodes in the linked list ; Starting from the next node ; If some duplicate node is found ; Return the count of duplicate nodes ; Driver code
class GFG { static class Node { int data ; Node next ; } ; static Node insert ( Node head , int item ) { Node temp = new Node ( ) ; temp . data = item ; temp . next = head ; head = temp ; return head ; } static int countNode ( Node head ) { int count = 0 ; while ( head . next != null ) { Node ptr = head . next ; while ( ptr != null ) { if ( head . data == ptr . data ) { count ++ ; break ; } ptr = ptr . next ; } head = head . next ; } return count ; } public static void main ( String args [ ] ) { Node head = null ; head = insert ( head , 5 ) ; head = insert ( head , 7 ) ; head = insert ( head , 5 ) ; head = insert ( head , 1 ) ; head = insert ( head , 7 ) ; System . out . println ( countNode ( head ) ) ; } }