codeparrot/xlcost-text-to-code huggingface dataset:
Count the number of Prime Cliques in an undirected graph | Java implementation to Count the number of Prime Cliques in an undirected graph ; Stores the vertices ; Graph ; Degree of the vertices ; To store the count of prime cliques ; Function to create Sieve to check primes ; False here indicates that it is not prime ; Condition if prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to check if the given set of vertices in store array is a clique or not ; Run a loop for all set of edges ; If any edge is missing ; Function to find the count of all the cliques having prime size ; Check if any vertices from i + 1 can be inserted ; Add the vertex to store ; If the graph is not a clique of size k then it cannot be a clique by adding another edge ; Increase the count of prime cliques if the size of current clique is prime ; Check if another edge can be added ; Driver code
import java . io . * ; import java . util . * ; class GFG { static final int MAX = 100 ; static int [ ] store = new int [ MAX ] ; static int n ; static int [ ] [ ] graph = new int [ MAX ] [ MAX ] ; static int [ ] d = new int [ MAX ] ; static int ans ; static void SieveOfEratosthenes ( boolean prime [ ] , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static boolean is_clique ( int b ) { for ( int i = 1 ; i < b ; i ++ ) { for ( int j = i + 1 ; j < b ; j ++ ) if ( graph [ store [ i ] ] [ store [ j ] ] == 0 ) return false ; } return true ; } static void primeCliques ( int i , int l , boolean prime [ ] ) { for ( int j = i + 1 ; j <= n ; j ++ ) { store [ l ] = j ; if ( is_clique ( l + 1 ) ) { if ( prime [ l ] ) ans ++ ; primeCliques ( j , l + 1 , prime ) ; } } } public static void main ( String [ ] args ) { int edges [ ] [ ] = { { 1 , 2 } , { 2 , 3 } , { 3 , 1 } , { 4 , 3 } , { 4 , 5 } , { 5 , 3 } } ; int size = edges . length ; n = 5 ; boolean [ ] prime = new boolean [ n + 1 ] ; Arrays . fill ( prime , true ) ; SieveOfEratosthenes ( prime , n ) ; for ( int i = 0 ; i < size ; i ++ ) { graph [ edges [ i ] [ 0 ] ] [ edges [ i ] [ 1 ] ] = 1 ; graph [ edges [ i ] [ 1 ] ] [ edges [ i ] [ 0 ] ] = 1 ; d [ edges [ i ] [ 0 ] ] ++ ; d [ edges [ i ] [ 1 ] ] ++ ; } ans = 0 ; primeCliques ( 0 , 1 , prime ) ; System . out . println ( ans ) ; } }
Construct a Doubly linked linked list from 2D Matrix | Java program to construct a Doubly linked linked list from 2D Matrix ; define dimension of matrix ; struct node of doubly linked list with four pointer next , prev , up , down ; function to create a new node ; function to construct the doubly linked list ; Create Node with value contain in matrix at index ( i , j ) ; Assign address of curr into the prev pointer of temp ; Assign address of curr into the up pointer of temp ; Recursive call for next pointer ; Recursive call for down pointer ; Return newly constructed node whose all four node connected at it 's appropriate position ; Function to construct the doubly linked list ; function call for construct the doubly linked list ; function for displaying doubly linked list data ; pointer to move right ; pointer to move down ; loop till node . down is not null ; loop till node . right is not null ; driver code ; initialise matrix
import java . util . * ; class GFG { static int dim = 3 ; static class Node { int data ; Node next ; Node prev ; Node up ; Node down ; } ; static Node createNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . next = null ; temp . prev = null ; temp . up = null ; temp . down = null ; return temp ; } static Node constructDoublyListUtil ( int mtrx [ ] [ ] , int i , int j , Node curr ) { if ( i >= dim j >= dim ) { return null ; } Node temp = createNode ( mtrx [ i ] [ j ] ) ; temp . prev = curr ; temp . up = curr ; temp . next = constructDoublyListUtil ( mtrx , i , j + 1 , temp ) ; temp . down = constructDoublyListUtil ( mtrx , i + 1 , j , temp ) ; return temp ; } static Node constructDoublyList ( int mtrx [ ] [ ] ) { return constructDoublyListUtil ( mtrx , 0 , 0 , null ) ; } static void display ( Node head ) { Node rPtr ; Node dPtr = head ; while ( dPtr != null ) { rPtr = dPtr ; while ( rPtr != null ) { System . out . print ( rPtr . data + " ▁ " ) ; rPtr = rPtr . next ; } System . out . print ( "NEW_LINE"); dPtr = dPtr . down ; } } public static void main ( String args [ ] ) { int mtrx [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; Node list = constructDoublyList ( mtrx ) ; display ( list ) ; } }
Count of exponential paths in a Binary Tree | Java program to find the count exponential paths in Binary Tree ; Structure of a Tree node ; Function to create a new node ; Function to find x ; Take log10 of n ; Log ( n ) with base i ; Raising i to the power p ; Function to check whether the given node equals to x ^ y for some y > 0 ; Take logx ( n ) with base x ; Utility function to count the exponent path in a given Binary tree ; Base Condition , when node pointer becomes null or node value is not a number of pow ( x , y ) ; Increment count when encounter leaf node ; Left recursive call save the value of count ; Right recursive call and return value of count ; Function to count exponential paths ; Driver code ; Create Tree ; Retrieve the value of x ; Function call
import java . util . * ; import java . lang . * ; 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 find_x ( int n ) { if ( n == 1 ) return 1 ; double num , den , p ; num = Math . log10 ( n ) ; int x = 0 , no = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { den = Math . log10 ( i ) ; p = num / den ; no = ( int ) ( Math . pow ( i , ( int ) p ) ) ; if ( Math . abs ( no - n ) < 1e-6 ) { x = i ; break ; } } return x ; } static boolean is_key ( int n , int x ) { double p ; p = Math . log10 ( n ) / Math . log10 ( x ) ; int no = ( int ) ( Math . pow ( x , ( int ) p ) ) ; if ( n == no ) return true ; return false ; } static int evenPaths ( Node node , int count , int x ) { if ( node == null || ! is_key ( node . key , x ) ) { return count ; } if ( node . left == null && node . right == null ) { count ++ ; } count = evenPaths ( node . left , count , x ) ; return evenPaths ( node . right , count , x ) ; } static int countExpPaths ( Node node , int x ) { return evenPaths ( node , 0 , x ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 27 ) ; root . left = newNode ( 9 ) ; root . right = newNode ( 81 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 10 ) ; root . right . left = newNode ( 70 ) ; root . right . right = newNode ( 243 ) ; root . right . right . left = newNode ( 81 ) ; root . right . right . right = newNode ( 909 ) ; int x = find_x ( root . key ) ; System . out . println ( countExpPaths ( root , x ) ) ; } }
Number of pairs such that path between pairs has the two vertices A and B | Java program to find the number of pairs such that the path between every pair contains two given vertices ; Function to perform DFS on the given graph by fixing the a vertex ; To mark a particular vertex as visited ; Variable to store the count of the vertices which can be reached from a ; Performing the DFS by iterating over the visited array ; If the vertex is not visited and removing the vertex b ; Function to return the number of pairs such that path between any two pairs consists of the given two vertices A and B ; Initializing the visited array and assigning it with 0 's ; Initially , the count of vertices is 0 ; Performing DFS by removing the vertex B ; Count the vertices which cannot be reached after removing the vertex B ; Again reinitializing the visited array ; Setting the count of vertices to 0 to perform the DFS again ; Performing the DFS by removing the vertex A ; Count the vertices which cannot be reached after removing the vertex A ; Multiplying both the vertices set ; Driver code ; Loop to store the graph
import java . util . * ; class GFG { static int N = 1000001 ; static int c , n , m , a , b ; static void dfs ( int a , int b , Vector < Integer > v [ ] , int vis [ ] ) { vis [ a ] = 1 ; c ++ ; for ( int i : v [ a ] ) { if ( vis [ i ] == 0 && i != b ) dfs ( i , b , v , vis ) ; } } static void Calculate ( Vector < Integer > v [ ] ) { int [ ] vis = new int [ n + 1 ] ; Arrays . fill ( vis , 0 ) ; c = 0 ; dfs ( a , b , v , vis ) ; int ans1 = n - c - 1 ; Arrays . fill ( vis , 0 ) ; c = 0 ; dfs ( b , a , v , vis ) ; int ans2 = n - c - 1 ; System . out . print ( ans1 * ans2 + "NEW_LINE"); } public static void main ( String [ ] args ) { n = 7 ; m = 7 ; a = 3 ; b = 5 ; int edges [ ] [ ] = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } , { 4 , 5 } , { 5 , 6 } , { 6 , 7 } , { 7 , 5 } } ; Vector < Integer > [ ] v = new Vector [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { v [ i ] = new Vector < Integer > ( ) ; } for ( int i = 0 ; i < m ; i ++ ) { v [ edges [ i ] [ 0 ] ] . add ( edges [ i ] [ 1 ] ) ; v [ edges [ i ] [ 1 ] ] . add ( edges [ i ] [ 0 ] ) ; } Calculate ( v ) ; } }
Travelling Salesman Problem implementation using BackTracking | Java implementation of the approach ; Function to find the minimum weight Hamiltonian Cycle ; If last node is reached and it has a link to the starting node i . e the source then keep the minimum value out of the total cost of traversal and " ans " Finally return to check for more possible values ; BACKTRACKING STEP Loop to traverse the adjacency list of currPos node and increasing the count by 1 and cost by graph [ currPos , i ] value ; Mark as visited ; Mark ith node as unvisited ; Driver code ; n is the number of nodes i . e . V ; Boolean array to check if a node has been visited or not ; Mark 0 th node as visited ; Find the minimum weight Hamiltonian Cycle ; ans is the minimum weight Hamiltonian Cycle
class GFG { static int tsp ( int [ ] [ ] graph , boolean [ ] v , int currPos , int n , int count , int cost , int ans ) { if ( count == n && graph [ currPos ] [ 0 ] > 0 ) { ans = Math . min ( ans , cost + graph [ currPos ] [ 0 ] ) ; return ans ; } for ( int i = 0 ; i < n ; i ++ ) { if ( v [ i ] == false && graph [ currPos ] [ i ] > 0 ) { v [ i ] = true ; ans = tsp ( graph , v , i , n , count + 1 , cost + graph [ currPos ] [ i ] , ans ) ; v [ i ] = false ; } } return ans ; } public static void main ( String [ ] args ) { int n = 4 ; int [ ] [ ] graph = { { 0 , 10 , 15 , 20 } , { 10 , 0 , 35 , 25 } , { 15 , 35 , 0 , 30 } , { 20 , 25 , 30 , 0 } } ; boolean [ ] v = new boolean [ n ] ; v [ 0 ] = true ; int ans = Integer . MAX_VALUE ; ans = tsp ( graph , v , 0 , n , 1 , 0 , ans ) ; System . out . println ( ans ) ; } }
Generate all the binary strings of N bits | Java implementation of the above approach : ; Function to print the output ; Function to generate all binary strings ; First assign "0" at ith position and try for all other permutations for remaining positions ; And then assign "1" at ith position and try for all other permutations for remaining positions ; Driver Code ; Print all binary strings
import java . util . * ; class GFG { static void printTheArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . println ( ) ; } static void generateAllBinaryStrings ( int n , int arr [ ] , int i ) { if ( i == n ) { printTheArray ( arr , n ) ; return ; } arr [ i ] = 0 ; generateAllBinaryStrings ( n , arr , i + 1 ) ; arr [ i ] = 1 ; generateAllBinaryStrings ( n , arr , i + 1 ) ; } public static void main ( String args [ ] ) { int n = 4 ; int [ ] arr = new int [ n ] ; generateAllBinaryStrings ( n , arr , 0 ) ; } }
Print all the combinations of a string in lexicographical order | Java program to find all combinations of a string in lexicographical order ; function to print string ; Method to found all combination of string it is based in tree ; return if level is equal size of string ; if occurrence of char is 0 then skip the iteration of loop ; decrease the char occurrence by 1 ; store the char in result ; print the string till level ; call the function from level + 1 ; backtracking ; declare the map for store each char with occurrence ; initialize the input array with all unique char ; initialize the count array with occurrence the unique char ; temporary char array for store the result ; store the element of input array ; store the element of count array ; size of map ( no of unique char ) ; size of original string ; call function for print string combination ; Driver code
import java . util . HashMap ; class GFG { static void printResult ( char [ ] result , int len ) { for ( int i = 0 ; i <= len ; i ++ ) System . out . print ( result [ i ] ) ; System . out . println ( ) ; } static void stringCombination ( char [ ] result , char [ ] str , int [ ] count , int level , int size , int length ) { if ( level == size ) return ; for ( int i = 0 ; i < length ; i ++ ) { if ( count [ i ] == 0 ) continue ; count [ i ] -- ; result [ level ] = str [ i ] ; printResult ( result , level ) ; stringCombination ( result , str , count , level + 1 , size , length ) ; count [ i ] ++ ; } } static void combination ( String str ) { HashMap < Character , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) mp . put ( str . charAt ( i ) , mp . get ( str . charAt ( i ) ) == null ? 1 : mp . get ( str . charAt ( i ) ) + 1 ) ; char [ ] input = new char [ mp . size ( ) ] ; int [ ] count = new int [ mp . size ( ) ] ; char [ ] result = new char [ str . length ( ) ] ; int i = 0 ; for ( HashMap . Entry < Character , Integer > entry : mp . entrySet ( ) ) { input [ i ] = entry . getKey ( ) ; count [ i ] = entry . getValue ( ) ; i ++ ; } int length = mp . size ( ) ; int size = str . length ( ) ; stringCombination ( result , input , count , 0 , size , length ) ; } public static void main ( String [ ] args ) { String str = " ABC " ; combination ( str ) ; } }
Combinations where every element appears twice and distance between appearances is equal to the value | Java program to find all combinations where every element appears twice and distance between appearances is equal to the value ; Find all combinations that satisfies given constraints ; if all elements are filled , print the solution ; try all possible combinations for element elem ; if position i and ( i + elem + 1 ) are not occupied in the vector ; place elem at position i and ( i + elem + 1 ) ; recurse for next element ; backtrack ( remove elem from position i and ( i + elem + 1 ) ) ; create a vector of double the size of given number with ; all its elements initialized by 1 ; start from element 1 ; Driver method ; given number
import java . util . Vector ; class Test { static void allCombinationsRec ( Vector < Integer > arr , int elem , int n ) { if ( elem > n ) { for ( int i : arr ) System . out . print ( i + " ▁ " ) ; System . out . println ( ) ; return ; } for ( int i = 0 ; i < 2 * n ; i ++ ) { if ( arr . get ( i ) == - 1 && ( i + elem + 1 ) < 2 * n && arr . get ( i + elem + 1 ) == - 1 ) { arr . set ( i , elem ) ; arr . set ( i + elem + 1 , elem ) ; allCombinationsRec ( arr , elem + 1 , n ) ; arr . set ( i , - 1 ) ; arr . set ( i + elem + 1 , - 1 ) ; } } } static void allCombinations ( int n ) { Vector < Integer > arr = new Vector < > ( ) ; for ( int i = 0 ; i < 2 * n ; i ++ ) { arr . add ( - 1 ) ; } int elem = 1 ; allCombinationsRec ( arr , elem , n ) ; } public static void main ( String [ ] args ) { int n = 3 ; allCombinations ( n ) ; } }
Printing all solutions in N | Java Program for above approach ; Program to solve N - Queens Problem ; All_rows_filled is a bit mask having all N bits set ; If rowmask will have all bits set , means queen has been placed successfully in all rows and board is displayed ; We extract a bit mask ( safe ) by rowmask , ldmask and rdmask . all set bits of ' safe ' indicates the safe column index for queen placement of this iteration for row index ( row ) . ; Extracts the right - most set bit ( safe column index ) where queen can be placed for this row ; these bit masks will keep updated in each iteration for next row ; Reset right - most set bit to 0 so , next iteration will continue by placing the queen at another safe column index of this row ; Backtracking , replace ' Q ' by ' ▁ ' ; Program to print board ; Driver Code ; Board size ; Function Call
import java . util . * ; public class NQueenSolution { static List < List < Integer > > result = new ArrayList < List < Integer > > ( ) ; public void solveBoard ( char [ ] [ ] board , int row , int rowmask , int ldmask , int rdmask ) { int n = board . length ; int all_rows_filled = ( 1 << n ) - 1 ; if ( rowmask == all_rows_filled ) { List < Integer > v = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( board [ i ] [ j ] == ' Q ' ) v . add ( j + 1 ) ; } } result . add ( v ) ; return ; } int safe = all_rows_filled & ( ~ ( rowmask ldmask rdmask ) ) ; while ( safe > 0 ) { int p = safe & ( - safe ) ; int col = ( int ) ( Math . log ( p ) / Math . log ( 2 ) ) ; board [ row ] [ col ] = ' Q ' ; solveBoard ( board , row + 1 , rowmask | p , ( ldmask p ) << 1 , ( rdmask p ) >> 1 ) ; safe = safe & ( safe - 1 ) ; board [ row ] [ col ] = ' ▁ ' ; } } public void printBoard ( char [ ] [ ] board ) { for ( int i = 0 ; i < board . length ; i ++ ) { System . out . print ( " ▁ " ) ; for ( int j = 0 ; j < board [ i ] . length ; j ++ ) { System . out . print ( board [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String args [ ] ) { int n = 4 ; char board [ ] [ ] = new char [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { board [ i ] [ j ] = ' ▁ ' ; } } int rowmask = 0 , ldmask = 0 , rdmask = 0 ; int row = 0 ; NQueenSolution solution = new NQueenSolution ( ) ; result . clear ( ) ; solution . solveBoard ( board , row , rowmask , ldmask , rdmask ) ; System . out . println ( result ) ; } }
Fill two instances of all numbers from 1 to n in a specific way | A backtracking based C ++ Program to fill two instances of all numbers from 1 to n in a specific way ; A recursive utility function to fill two instances of numbers from 1 to n in res [ 0. .2 n - 1 ] . ' curr ' is current value of n . ; If current number becomes 0 , then all numbers are filled ; Try placing two instances of ' curr ' at all possible locations till solution is found ; Two ' curr ' should be placed at ' curr + 1' distance ; Plave two instances of ' curr ' ; Recur to check if the above placement leads to a solution ; If solution is not possible , then backtrack ; This function prints the result for input number ' n ' using fillUtil ( ) ; Create an array of size 2 n and initialize all elements in it as 0 ; If solution is possible , then print it . ; Driver Code
import java . io . * ; class GFG { static boolean fillUtil ( int res [ ] , int curr , int n ) { if ( curr == 0 ) return true ; int i ; for ( i = 0 ; i < 2 * n - curr - 1 ; i ++ ) { if ( res [ i ] == 0 && res [ i + curr + 1 ] == 0 ) { res [ i ] = res [ i + curr + 1 ] = curr ; if ( fillUtil ( res , curr - 1 , n ) ) return true ; res [ i ] = res [ i + curr + 1 ] = 0 ; } } return false ; } static void fill ( int n ) { int res [ ] = new int [ 2 * n ] ; int i ; for ( i = 0 ; i < 2 * n ; i ++ ) res [ i ] = 0 ; if ( fillUtil ( res , n , n ) ) { for ( i = 0 ; i < 2 * n ; i ++ ) System . out . print ( res [ i ] + " ▁ " ) ; } else System . out . print ( " Not ▁ Possible " ) ; } public static void main ( String [ ] args ) { fill ( 7 ) ; } }
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 ; 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 } } ; 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 ) ) ; } }
Fast Doubling method to find the Nth Fibonacci number | Java program to find the Nth Fibonacci number using Fast Doubling Method ; Function calculate the N - th fibanacci number using fast doubling method ; Base Condition ; Here a = F ( n ) ; Here b = F ( n + 1 ) ; As F ( 2 n ) = F ( n ) [ 2F ( n + 1 ) F ( n ) ] Here c = F ( 2 n ) ; As F ( 2 n + 1 ) = F ( n ) ^ 2 + F ( n + 1 ) ^ 2 Here d = F ( 2 n + 1 ) ; Check if N is odd or even ; Driver code
class GFG { static void FastDoubling ( int n , int [ ] res ) { int a , b , c , d ; int MOD = 1000000007 ; if ( n == 0 ) { res [ 0 ] = 0 ; res [ 1 ] = 1 ; return ; } FastDoubling ( ( n / 2 ) , res ) ; a = res [ 0 ] ; b = res [ 1 ] ; c = 2 * b - a ; if ( c < 0 ) c += MOD ; c = ( a * c ) % MOD ; d = ( a * a + b * b ) % MOD ; if ( n % 2 == 0 ) { res [ 0 ] = c ; res [ 1 ] = d ; } else { res [ 0 ] = d ; res [ 1 ] = c + d ; } } public static void main ( String [ ] args ) { int N = 6 ; int res [ ] = new int [ 2 ] ; FastDoubling ( N , res ) ; System . out . print ( res [ 0 ] ) ; } }
Frequency of an integer in the given array using Divide and Conquer | Java implrmrntation of the approach ; Function to return the frequency of x in the subarray arr [ low ... high ] ; If the subarray is invalid or the element is not found ; If there 's only a single element  which is equal to x ; Divide the array into two parts and then find the count of occurrences of x in both the parts ; Driver code
class GFG { static int count ( int arr [ ] , int low , int high , int x ) { if ( ( low > high ) || ( low == high && arr [ low ] != x ) ) return 0 ; if ( low == high && arr [ low ] == x ) return 1 ; return count ( arr , low , ( low + high ) / 2 , x ) + count ( arr , 1 + ( low + high ) / 2 , high , x ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 30 , 1 , 42 , 5 , 56 , 3 , 56 , 9 } ; int n = arr . length ; int x = 56 ; System . out . print ( count ( arr , 0 , n - 1 , x ) ) ; } }