codeparrot/xlcost-text-to-code huggingface dataset:
Print all permutations of a number N greater than itself | Java implementation to print all the permutation greater than the integer N ; Function to print all the permutation which are greater than N itself ; Iterate and count the number of digits in N ; vector to print the permutations of N ; Store digits of N in the vector num ; Iterate over every permutation of N which is greater than N ; Print the current permutation of N ; Function to print all the permutation which are greater than N itself ; Driver Code
import java . util . * ; class GFG { static void printPermutation ( int N ) { int temp = N , count = 0 ; while ( temp > 0 ) { count ++ ; temp /= 10 ; } int [ ] num = new int [ count ] ; while ( N > 0 ) { num [ count -- - 1 ] = N % 10 ; N = N / 10 ; } while ( next_permutation ( num ) ) { for ( int i = 0 ; i < num . length ; i ++ ) System . out . print ( num [ i ] ) ; System . out . print ( "NEW_LINE"); } } static boolean next_permutation ( int [ ] p ) { for ( int a = p . length - 2 ; a >= 0 ; -- a ) if ( p [ a ] < p [ a + 1 ] ) for ( int b = p . length - 1 ; ; -- b ) if ( p [ b ] > p [ a ] ) { int t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } return false ; } public static void main ( String [ ] args ) { int N = 324 ; printPermutation ( N ) ; } }
Count of ways to represent N as sum of a prime number and twice of a square | Java implementation to count the number of ways a number can be written as sum of prime number and twice a square ; Function to mark all the prime numbers using sieve ; Initially all the numbers are marked as prime ; Loop to mark the prime numbers upto the Square root of N ; Loop to store the prime numbers in an array ; Function to find the number ways to represent a number as the sum of prime number and square of a number ; Loop to iterate over all the possible prime numbers ; Increment the count if the given number is a valid number ; Driver Code ; Function Call
import java . util . * ; class GFG { static int n = 500000 - 2 ; static Vector < Integer > v = new Vector < > ( ) ; static void sieveoferanthones ( ) { boolean [ ] prime = new boolean [ n + 1 ] ; Arrays . fill ( prime , true ) ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( prime [ i ] ) for ( int j = i * i ; j <= n ; j += i ) { prime [ j ] = false ; } } for ( int i = 2 ; i < n ; i ++ ) { if ( prime [ i ] ) v . add ( i ) ; } } static void numberOfWays ( int n ) { int count = 0 ; for ( int j = 1 ; 2 * ( Math . pow ( j , 2 ) ) < n ; j ++ ) { for ( int i = 1 ; v . get ( i ) + 2 <= n ; i ++ ) { if ( n == v . get ( i ) + ( 2 * ( Math . pow ( j , 2 ) ) ) ) count ++ ; } } System . out . print ( count + "NEW_LINE"); } public static void main ( String [ ] args ) { sieveoferanthones ( ) ; int n = 9 ; numberOfWays ( n ) ; } }
Program to convert Degree to Radian | Java program to convert degree to radian ; Function for conversion ; Driver code
import java . io . * ; import java . util . * ; class GFG { static double Convert ( double degree ) { double pi = 3.14159265359 ; return ( degree * ( pi / 180 ) ) ; } public static void main ( String [ ] args ) { double degree = 30 ; double radian = Convert ( degree ) ; System . out . printf ( " % .6f " , radian ) ; } }
Program to convert a Binary Number to Hexa | Java code to convert BCD to its HexaDecimal number ( base 16 ) . Including Header Files ; Function to convert BCD to HexaDecimal ; Iterating through the bits backwards ; Computing the HexaDecimal Number formed so far and storing it in a vector . ; Reinitializing all variables for next group . ; Printing the Hexadecimal number formed so far . ; Driver Code ; Function Call
import java . util . * ; class GFG { static void bcdToHexaDecimal ( char [ ] s ) { int len = s . length , check = 0 ; int num = 0 , sum = 0 , mul = 1 ; Vector < Character > ans = new Vector < Character > ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) { sum += ( s [ i ] - '0' ) * mul ; mul *= 2 ; check ++ ; if ( check == 4 i == 0 ) { if ( sum <= 9 ) ans . add ( ( char ) ( sum + '0' ) ) ; else ans . add ( ( char ) ( sum + 55 ) ) ; check = 0 ; sum = 0 ; mul = 1 ; } } len = ans . size ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) System . out . print ( ans . get ( i ) ) ; } public static void main ( String [ ] args ) { String s = "100000101111" ; bcdToHexaDecimal ( s . toCharArray ( ) ) ; } }
Find the sum of the first N Dodecagonal Numbers | Java program to find the sum of the first N dodecagonal numbers ; Function to find the N - th dodecagonal number ; Formula to calculate N - th dodecagonal number ; Function to find the sum of the first N dodecagonal numbers ; Variable to get the sum ; Iterating through the first N numbers ; Compute the sum ; Driver Code ; Display first Nth centered_decagonal number
class GFG { static int Dodecagonal_num ( int n ) { return ( 5 * n * n - 4 * n ) ; } static int sum_Dodecagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += Dodecagonal_num ( i ) ; } return summ ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( sum_Dodecagonal_num ( n ) ) ; } }
Program to check if N is a Myriagon Number | Java program for the above approach ; Function to check if N is a myriagon number ; Condition to check if the number is a myriagon number ; Driver Code ; Given Number ; Function call
import java . io . * ; class GFG { static boolean isMyriagon ( int N ) { double n = ( 9996 + Math . sqrt ( 79984 * N + 99920016 ) ) / 19996 ; return ( n - ( int ) n ) == 0 ; } public static void main ( String [ ] args ) { int N = 10000 ; if ( isMyriagon ( N ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Program to check if N is a Heptagonal Number | Java program for the above approach ; Function to check if N is a heptagonal number ; Condition to check if the number is a heptagonal number ; Driver code ; Given Number ; Function call
import java . io . * ; import java . util . * ; class GFG { public static boolean isheptagonal ( int N ) { double n = ( 3 + Math . sqrt ( 40 * N + 9 ) ) / 10 ; return ( n - ( int ) n ) == 0 ; } public static void main ( String [ ] args ) { int N = 7 ; if ( isheptagonal ( N ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Program to check if N is a Enneadecagonal Number | Java program for the above approach ; Function to check if number N is a enneadecagonal number ; Condition to check if N is a enneadecagonal number ; Driver Code ; Given Number ; Function call
class GFG { static boolean isenneadecagonal ( int N ) { float n = ( float ) ( 15 + Math . sqrt ( 136 * N + 225 ) ) / 34 ; return ( n - ( int ) n ) == 0 ; } public static void main ( String [ ] args ) { int N = 19 ; if ( isenneadecagonal ( N ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Program to check if N is a Centered Tridecagonal Number | Java program for the above approach ; Function to check if the number N is a centered tridecagonal number ; Condition to check if the N is a centered tridecagonal number ; Driver Code ; Given Number ; Function call
class GFG { static boolean isCenteredtridecagonal ( int N ) { float n = ( float ) ( ( 13 + Math . sqrt ( 104 * N + 65 ) ) / 26 ) ; return ( n - ( int ) n ) == 0 ; } public static void main ( String [ ] args ) { int N = 14 ; if ( isCenteredtridecagonal ( N ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
Orbit counting theorem or Burnside 's Lemma | Java program for implementing the Orbit counting theorem or Burnside 's Lemma ; Function to find result using Orbit counting theorem or Burnside 's Lemma ; According to Burnside 's Lemma  calculate distinct ways for each  rotation ; Find GCD ; Divide By N ; Print the distinct ways ; Driver Code ; N stones and M colors ; Function call
class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void countDistinctWays ( int N , int M ) { int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int K = gcd ( i , N ) ; ans += Math . pow ( M , K ) ; } ans /= N ; System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int N = 4 , M = 3 ; countDistinctWays ( N , M ) ; } }
Find the Nth natural number which is not divisible by A | Java code to Find the Nth number which is not divisible by A from the series ; Find the quotient and the remainder when k is divided by n - 1 ; If the remainder is not 0 multiply n by q and subtract 1 if remainder is 0 then multiply n by q and add the remainder ; Print the answer ; Driver code
class GFG { static void findNum ( int n , int k ) { int q = k / ( n - 1 ) ; int r = k % ( n - 1 ) ; int a = 0 ; if ( r != 0 ) a = ( n * q ) + r ; else a = ( n * q ) - 1 ; System . out . println ( a ) ; } public static void main ( String [ ] args ) { int A = 4 ; int N = 6 ; findNum ( A , N ) ; } }
Count the nodes in the given Tree whose weight is a Perfect Number | Java implementation to Count the nodes in the given tree whose weight is a Perfect Number ; Function that returns true if n is perfect ; Variable to store sum of divisors ; Find all divisors and add them ; Check if sum of divisors is equal to n , then n is a perfect number ; Function to perform dfs ; If weight of the current node is a perfect number ; Driver code ; Weights of the node ; Edges of the tree
import java . util . * ; class GFG { static int ans = 0 ; static Vector < Integer > [ ] graph = new Vector [ 100 ] ; static int [ ] weight = new int [ 100 ] ; static boolean isPerfect ( int n ) { int sum = 1 ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i != n ) sum = sum + i + n / i ; else sum = sum + i ; } } if ( sum == n && n != 1 ) return true ; return false ; } static void dfs ( int node , int parent ) { if ( isPerfect ( weight [ node ] ) ) ans += 1 ; for ( int to : graph [ node ] ) { if ( to == parent ) continue ; dfs ( to , node ) ; } } public static void main ( String [ ] args ) { for ( int i = 0 ; i < graph . length ; i ++ ) graph [ i ] = new Vector < Integer > ( ) ; weight [ 1 ] = 5 ; weight [ 2 ] = 10 ; weight [ 3 ] = 11 ; weight [ 4 ] = 8 ; weight [ 5 ] = 6 ; graph [ 1 ] . add ( 2 ) ; graph [ 2 ] . add ( 3 ) ; graph [ 2 ] . add ( 4 ) ; graph [ 1 ] . add ( 5 ) ; dfs ( 1 , 1 ) ; System . out . print ( ans ) ; } }
Minimum cost to merge numbers from 1 to N | Java program for the above approach ; Function returns the minimum cost ; Min Heap representation ; Add all elements to heap ; First minimum ; Second minimum ; Multiply them ; Add to the cost ; Push the product into the heap again ; Return the optimal cost ; Driver Code
import java . util . * ; class GFG { static int GetMinCost ( int N ) { PriorityQueue < Integer > pq ; pq = new PriorityQueue < > ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { pq . add ( i ) ; } int cost = 0 ; while ( pq . size ( ) > 1 ) { int mini = pq . remove ( ) ; int secondmini = pq . remove ( ) ; int current = mini * secondmini ; cost += current ; pq . add ( current ) ; } return cost ; } public static void main ( String args [ ] ) { int N = 5 ; System . out . println ( GetMinCost ( N ) ) ; } }
Permutation of first N positive integers such that prime numbers are at prime indices | Set 2 | Java program to count permutations from 1 to N such that prime numbers occur at prime indices ;  ; Computing count of prime numbers using sieve ; Computing permutations for primes ; Computing permutations for non - primes ; Driver program
import java . util . * ; class GFG { static int MOD = ( int ) ( 1e9 + 7 ) ; static int numPrimeArrangements ( int n ) { boolean [ ] prime = new boolean [ n + 1 ] ; Arrays . fill ( prime , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( prime [ i ] ) for ( int factor = 2 ; factor * i <= n ; factor ++ ) prime [ factor * i ] = false ; } int primeIndices = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( prime [ i ] ) primeIndices ++ ; int mod = ( int ) ( 1e9 + 7 ) , res = 1 ; for ( int i = 1 ; i <= primeIndices ; i ++ ) res = ( int ) ( ( 1L * res * i ) % mod ) ; for ( int i = 1 ; i <= ( n - primeIndices ) ; i ++ ) res = ( int ) ( ( 1L * res * i ) % mod ) ; return res ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . print ( numPrimeArrangements ( N ) ) ; } }
Probability of getting K heads in N coin tosses | Java program to find probability of getting K heads in N coin tosses ; function to calculate factorial ; apply the formula ; Driver function ; call count_heads with n and r
class GFG { static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static double count_heads ( int n , int r ) { double output ; output = fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; output = output / ( Math . pow ( 2 , n ) ) ; return output ; } public static void main ( String [ ] args ) { int n = 4 , r = 3 ; System . out . print ( count_heads ( n , r ) ) ; } }
Largest component size in a graph formed by connecting non |  ; Mark this node as visited ; Apply dfs and add nodes belonging to this component ; Create graph and store in adjacency list form ; Iterate over all pair of nodes ; If not co - prime ; Build undirected graph ; Visited array for dfs ; Utility gcd ; Driver Code
import java . util . * ; class GFG { static int dfs ( int u , Vector < Integer > [ ] adj , int vis [ ] ) { vis [ u ] = 1 ; int componentSize = 1 ; for ( int it : adj [ u ] ) { if ( vis [ it ] == 0 ) { componentSize += dfs ( it , adj , vis ) ; } } return componentSize ; } static int maximumComponentSize ( int a [ ] , int n ) { @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] adj = new Vector [ n ] ; for ( int i = 0 ; i < adj . length ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( __gcd ( a [ i ] , a [ j ] ) > 1 ) adj [ i ] . add ( j ) ; adj [ j ] . add ( i ) ; } } int answer = 0 ; int [ ] vis = new int [ n ] ; for ( int k = 0 ; k < n ; k ++ ) { vis [ k ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { if ( vis [ i ] == 0 ) { answer = Math . max ( answer , dfs ( i , adj , vis ) ) ; } } return answer ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int n = 8 ; int A [ ] = { 2 , 3 , 6 , 7 , 4 , 12 , 21 , 39 } ; System . out . print ( maximumComponentSize ( A , n ) ) ; } }
Largest component size in a graph formed by connecting non | Java program to implement the above approach ; smallest prime factor ; Sieve of Eratosthenes ; is spf [ i ] = 0 , then it 's prime ; smallest prime factor of all multiples is i ; Prime factorise n , and store prime factors in set s ; for implementing DSU ; root of component of node i ; finding root as well as applying path compression ; merging two components ; find roots of both components ; if already belonging to the same compenent ; Union by rank , the rank in this case is sizeContainer of the component . Smaller sizeContainer will be merged into larger , so the larger 's root will be  final root ; Function to find the maximum sized container ; intitalise the parents , and component sizeContainer ; intitally all component sizeContainers are 1 ans each node it parent of itself ; store prime factors of a [ i ] in s ; if this prime is seen as a factor for the first time ; if not then merge with that component in which this prime was previously seen ; maximum of sizeContainer of all components ; Driver Code
import java . util . * ; class GFG { static int [ ] spf = new int [ 100005 ] ; static void sieve ( ) { for ( int i = 2 ; i < 100005 ; i ++ ) { if ( spf [ i ] == 0 ) { spf [ i ] = i ; for ( int j = 2 * i ; j < 100005 ; j += i ) { if ( spf [ j ] == 0 ) spf [ j ] = i ; } } } } static void factorize ( int n , HashSet < Integer > s ) { while ( n > 1 ) { int z = spf [ n ] ; s . add ( z ) ; while ( n % z == 0 ) n /= z ; } } static int [ ] id = new int [ 100005 ] ; static int [ ] par = new int [ 100005 ] ; static int [ ] sizeContainer = new int [ 100005 ] ; static int root ( int i ) { if ( par [ i ] == i ) return i ; else return par [ i ] = root ( par [ i ] ) ; } static void merge ( int a , int b ) { int p = root ( a ) ; int q = root ( b ) ; if ( p == q ) return ; if ( sizeContainer [ p ] > sizeContainer [ q ] ) { p = p + q ; q = p - q ; p = p - q ; } par [ p ] = q ; sizeContainer [ q ] += sizeContainer [ p ] ; } static int maximumComponentsizeContainer ( int a [ ] , int n ) { for ( int i = 0 ; i < 100005 ; i ++ ) { par [ i ] = i ; sizeContainer [ i ] = 1 ; } sieve ( ) ; for ( int i = 0 ; i < n ; i ++ ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; factorize ( a [ i ] , s ) ; for ( int it : s ) { if ( id [ it ] == 0 ) id [ it ] = i + 1 ; else merge ( i + 1 , id [ it ] ) ; } } int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) answer = Math . max ( answer , sizeContainer [ i ] ) ; return answer ; } public static void main ( String [ ] args ) { int n = 8 ; int A [ ] = { 2 , 3 , 6 , 7 , 4 , 12 , 21 , 39 } ; System . out . print ( maximumComponentsizeContainer ( A , n ) ) ; } }
Count of nodes in a Binary Tree whose child is its prime factors | Java program for Counting nodes whose immediate children are its factors ; To store all prime numbers ; Create a boolean array " prime [ 0 . . N ] " and initialize all its entries as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiples of p and are less than p ^ 2 are already marked . ; A Tree node ; Utility function to create a new node ; Function to check if immediate children of a node are its factors or not ; Function to get the count of full Nodes in a binary tree ; If tree is empty ; Initialize count of full nodes having children as their factors ; If only right child exist ; If only left child exist ; Both left and right child exist ; Check for left child ; Check for right child ; Driver Code ; Create Binary Tree as shown ; To save all prime numbers ; Print Count of all nodes having children as their factors
import java . util . * ; class GFG { static int N = 1000000 ; static Vector < Integer > prime = new Vector < Integer > ( ) ; static void SieveOfEratosthenes ( ) { boolean [ ] check = new boolean [ N + 1 ] ; Arrays . fill ( check , true ) ; for ( int p = 2 ; p * p <= N ; p ++ ) { if ( check [ p ] == true ) { prime . add ( p ) ; for ( int i = p * p ; i <= N ; i += p ) check [ i ] = false ; } } } 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 boolean IsChilrenPrimeFactor ( Node parent , Node a ) { if ( prime . get ( a . key ) > 0 && ( parent . key % a . key == 0 ) ) return true ; else return false ; } static int GetCount ( Node node ) { if ( node == null ) return 0 ; Queue < Node > q = new LinkedList < > ( ) ; int count = 0 ; q . add ( node ) ; while ( ! q . isEmpty ( ) ) { Node temp = q . peek ( ) ; q . remove ( ) ; if ( temp . left == null && temp . right != null ) { if ( IsChilrenPrimeFactor ( temp , temp . right ) ) count ++ ; } else { if ( temp . right == null && temp . left != null ) { if ( IsChilrenPrimeFactor ( temp , temp . left ) ) count ++ ; } else { if ( temp . left != null && temp . right != null ) { if ( IsChilrenPrimeFactor ( temp , temp . right ) && IsChilrenPrimeFactor ( temp , temp . left ) ) count ++ ; } } } if ( temp . left != null ) q . add ( temp . left ) ; if ( temp . right != null ) q . add ( temp . right ) ; } return count ; } public static void main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 5 ) ; root . right . left = newNode ( 18 ) ; root . right . right = newNode ( 12 ) ; root . right . left . left = newNode ( 2 ) ; root . right . left . right = newNode ( 3 ) ; root . right . right . left = newNode ( 3 ) ; root . right . right . right = newNode ( 14 ) ; root . right . right . right . left = newNode ( 7 ) ; SieveOfEratosthenes ( ) ; System . out . print ( GetCount ( root ) + "NEW_LINE"); } }
Check if original Array is retained after performing XOR with M exactly K times |  ; Function to check if original Array can be retained by performing XOR with M exactly K times ; Check if O is present or not ; If K is odd and 0 is not present then the answer will always be No . ; Else it will be Yes ; Driver Code
import java . util . * ; class GFG { static String check ( int [ ] Arr , int n , int M , int K ) { int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( Arr [ i ] == 0 ) flag = 1 ; } if ( K % 2 != 0 && flag == 0 ) return " No " ; else return " Yes " ; } public static void main ( String args [ ] ) { int [ ] Arr = { 1 , 1 , 2 , 4 , 7 , 8 } ; int M = 5 ; int K = 6 ; int n = Arr . length ; System . out . println ( check ( Arr , n , M , K ) ) ; } }
Longest sub | Java program to find longest subarray of prime numbers ; To store the prime numbers ; Function that find prime numbers till limit ; Find primes using Sieve of Eratosthenes ; Function that finds all prime numbers in given range using Segmented Sieve ; Find the limit ; To store the prime numbers ; Comput all primes less than or equals to sqrt ( high ) using Simple Sieve ; Count the elements in the range [ low , high ] ; Declaring boolean for the range [ low , high ] ; Traverse the prime numbers till limit ; Find the minimum number in [ low . . high ] that is a multiple of prime [ i ] ; Mark the multiples of prime [ i ] in [ low , high ] as true ; Element which are not marked in range are Prime ; Function that finds longest subarray of prime numbers ; If element is Non - prime then updated current_max to 0 ; If element is prime , then update current_max and max_so_far ; Return the count of longest subarray ; Driver code ; Find minimum and maximum element ; Find prime in the range [ min_el , max_el ] ; Function call
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static Set < Integer > allPrimes = new HashSet < > ( ) ; static void simpleSieve ( int limit , ArrayList < Integer > prime ) { boolean [ ] mark = new boolean [ limit + 1 ] ; for ( int i = 2 ; i <= limit ; ++ i ) { if ( mark [ i ] == false ) { prime . add ( i ) ; for ( int j = i ; j <= limit ; j += i ) { mark [ j ] = true ; } } } } static void primesInRange ( int low , int high ) { int limit = ( int ) Math . floor ( Math . sqrt ( high ) ) + 1 ; ArrayList < Integer > prime = new ArrayList < > ( ) ; simpleSieve ( limit , prime ) ; int n = high - low + 1 ; boolean [ ] mark = new boolean [ n + 1 ] ; for ( int i = 0 ; i < prime . size ( ) ; i ++ ) { int loLim = ( int ) Math . floor ( ( double ) low / ( int ) prime . get ( i ) ) ; loLim *= ( int ) prime . get ( i ) ; if ( loLim < low ) { loLim += ( int ) prime . get ( i ) ; } if ( loLim == ( int ) prime . get ( i ) ) { loLim += ( int ) prime . get ( i ) ; } for ( int j = loLim ; j <= high ; j += ( int ) prime . get ( i ) ) mark [ j - low ] = true ; } for ( int i = low ; i <= high ; i ++ ) { if ( ! mark [ i - low ] ) { allPrimes . add ( i ) ; } } } static int maxPrimeSubarray ( int [ ] arr , int n ) { int current_max = 0 ; int max_so_far = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! allPrimes . contains ( arr [ i ] ) ) current_max = 0 ; else { current_max ++ ; max_so_far = Math . max ( current_max , max_so_far ) ; } } return max_so_far ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 3 , 29 , 11 , 7 , 8 , 9 } ; int n = arr . length ; int max_el = Integer . MIN_VALUE ; int min_el = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < min_el ) { min_el = arr [ i ] ; } if ( arr [ i ] > max_el ) { max_el = arr [ i ] ; } } primesInRange ( min_el , max_el ) ; System . out . print ( maxPrimeSubarray ( arr , n ) ) ; } }