codeparrot/xlcost-text-to-code huggingface dataset:
Count of odd length contiguous Palindromic sequences in a Matrix | Java code to Count the odd length contiguous Palindromic sequences in the matrix ; Function to count the number of contiguous palindromic sequences in the matrix ; Add the total number of elements in the matrix to the count ; Length of possible sequence to be checked for palindrome horizontally and vertically ; Iterate through each element of the matrix and count the number of palindromic sequences in each row and column ; Find the possible length of sequences that can be a palindrome ; From i , check if the sequence formed by elements to its left and right is palindrome or not ; if the sequence [ i , j - k ] to [ i , j + k ] is a palindrome , increment the count by 1 ; From i , check if the sequence formed by elements to its above and below is palindrome or not ; if the sequence [ i - k , j ] to [ i + k , j ] is a palindrome , increment the count by 1 ; Return the total count of the palindromic sequences ; Driver code
class GFG { static final int MAX = 10 ; static int countPalindromes ( int n , int m , int matrix [ ] [ ] ) { int count = n * m ; int length_of_sequence_row ; int length_of_sequence_column ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { length_of_sequence_row = Math . min ( j , m - 1 - j ) ; length_of_sequence_column = Math . min ( i , n - i - 1 ) ; for ( int k = 1 ; k <= length_of_sequence_row ; k ++ ) { if ( matrix [ i ] [ j - k ] == matrix [ i ] [ j + k ] ) { count ++ ; } else { break ; } } for ( int k = 1 ; k <= length_of_sequence_column ; k ++ ) { if ( matrix [ i - k ] [ j ] == matrix [ i + k ] [ j ] ) { count ++ ; } else { break ; } } } } return count ; } public static void main ( String [ ] args ) { int m = 3 , n = 3 ; int matrix [ ] [ ] = { { 2 , 1 , 2 } , { 1 , 1 , 1 } , { 2 , 1 , 2 } } ; System . out . print ( countPalindromes ( n , m , matrix ) + "NEW_LINE"); } } `
Reduce a number to 1 by performing given operations | Set 2 | Java implementation of the approach ; Function to return the number of set bits in n ; Function to return the minimum steps required to reach 1 ; If n is even then divide it by 2 ; If n is 3 or the number of set bits in ( n - 1 ) is less than the number of set bits in ( n + 1 ) ; Increment the number of steps ; Return the minimum number of steps ; Driver code
class GFG { static int set_bits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n % 2 ; n /= 2 ; } return count ; } static int minSteps ( int n ) { int ans = 0 ; while ( n != 1 ) { if ( n % 2 == 0 ) n /= 2 ; else if ( n == 3 || set_bits ( n - 1 ) < set_bits ( n + 1 ) ) n -- ; else n ++ ; ans ++ ; } return ans ; } public static void main ( String [ ] args ) { int n = 15 ; System . out . print ( minSteps ( n ) ) ; } }
Minimum integer that can be obtained by swapping adjacent digits of different parity | Java implementation of the above approach . ; Function to return the minimum number ; Store the elements which are divisible by two in stack1 ; Store the elements which are not divisible by two in stack2 . ; Concatenate the answer with smaller value of the topmost elements of both the stacks and then pop that element ; Concatenate the answer with remaining values of stack1 . ; Concatenate the answer with remaining values of stack2 . ; Driver code ; Function calling
import java . util . * ; class GFG { static int minimumNo ( int n ) { int ans = 0 ; Stack < Integer > stack1 = new Stack < Integer > ( ) ; Stack < Integer > stack2 = new Stack < Integer > ( ) ; while ( n != 0 ) { int r = n % 10 ; if ( r % 2 == 0 ) { stack1 . add ( r ) ; } else { stack2 . add ( r ) ; } n = n / 10 ; } while ( ! stack1 . isEmpty ( ) && ! stack2 . isEmpty ( ) ) { if ( stack1 . peek ( ) < stack2 . peek ( ) ) { ans = ans * 10 + stack1 . peek ( ) ; stack1 . pop ( ) ; } else { ans = ans * 10 + stack2 . peek ( ) ; stack2 . pop ( ) ; } } while ( ! stack1 . isEmpty ( ) ) { ans = ans * 10 + stack1 . peek ( ) ; stack1 . pop ( ) ; } while ( ! stack2 . isEmpty ( ) ) { ans = ans * 10 + stack2 . peek ( ) ; stack2 . pop ( ) ; } return ans ; } public static void main ( String [ ] args ) { int n1 = 64432 ; System . out . print ( minimumNo ( n1 ) + "NEW_LINE"); int n2 = 3137 ; System . out . print ( minimumNo ( n2 ) + "NEW_LINE"); } }
Find two numbers with the given LCM and minimum possible difference | Java implementation of the approach ; Function to return the LCM of a and b ; Function to find and print the two numbers ; To find the factors ; To check if i is a factor of x and the minimum possible number satisfying the given conditions ; Driver code
class GFG { static int lcm ( int a , int b ) { return ( a / __gcd ( a , b ) * b ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static void findNums ( int x ) { int ans = - 1 ; for ( int i = 1 ; i <= Math . sqrt ( x ) ; i ++ ) { if ( x % i == 0 && lcm ( i , x / i ) == x ) { ans = i ; } } System . out . print ( ans + " ▁ " + ( x / ans ) ) ; } public static void main ( String [ ] args ) { int x = 12 ; findNums ( x ) ; } }
Find an integer that is common in the maximum number of given arithmetic progressions | Java implementation of the approach ; Function to return element common in maximum number of APs ; Initialize the count variable ; Increment count for every element of an AP ; Find the index with maximum count ; Return the maximum common element ; Driver code
class GFG { final static int MAXN = 1000000 ; static int max_element ( int [ ] A , int n ) { int max = A [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) if ( A [ i ] > max ) max = A [ i ] ; return max ; } static int maxCommonElement ( int A [ ] , int D [ ] , int N ) { int cnt [ ] = new int [ MAXN ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = A [ i ] ; j < MAXN ; j += D [ i ] ) cnt [ j ] ++ ; } int ans = 0 ; int com = 0 ; for ( int i = 0 ; i < MAXN ; i ++ ) { if ( cnt [ i ] > ans ) { ans = cnt [ i ] ; com = i ; } } return com ; } public static void main ( String args [ ] ) { int A [ ] = { 13 , 1 , 2 , 5 } , D [ ] = { 5 , 10 , 1 , 12 } ; int N = A . length ; System . out . println ( maxCommonElement ( A , D , N ) ) ; } }
Number of trailing zeros in N * ( N | Java implementation of the approach ; Function to return the count of trailing 0 s in the given function ; If n is odd ; If n is even ; Find the trailing zeros in n / 2 factorial ; Return the required answer ; Driver code
import java . util . * ; class GFG { static int findTrailingZeros ( int n ) { if ( ( n & 1 ) == 1 ) return 0 ; else { int ans = 0 ; n /= 2 ; while ( n != 0 ) { ans += n / 5 ; n /= 5 ; } return ans ; } } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( findTrailingZeros ( n ) ) ; } }
Divide N into K unique parts such that gcd of those parts is maximum | Java implementation of the approach ; Function to calculate maximum GCD ; Minimum possible sum for K unique positive integers ; It is not possible to divide N into K unique parts ; All the factors greater than sqrt ( N ) are complementary of the factors less than sqrt ( N ) ; If i is a factor of N ; Driver code
import java . io . * ; import java . lang . Math ; class GFG { static int maxGCD ( int N , int K ) { int minSum = ( K * ( K + 1 ) ) / 2 ; if ( N < minSum ) return - 1 ; int i = ( int ) Math . sqrt ( N ) ; int res = 1 ; while ( i >= 1 ) { if ( N % i == 0 ) { if ( i >= minSum ) res = Math . max ( res , N / i ) ; if ( N / i >= minSum ) res = Math . max ( res , i ) ; } i -- ; } return res ; } public static void main ( String [ ] args ) { int N = 18 , K = 3 ; System . out . println ( maxGCD ( N , K ) ) ; } }
Radius of the inscribed circle within three tangent circles | Java implementation of the approach ; Radius of the 3 given circles declared as double . ; Calculation of area of a triangle by Heron 's formula ; Applying binary search to find the radius r4 of the required circle ; Area of main triangle ; Loop runs until l and h becomes approximately equal ; Area of smaller triangles ; If sum of smaller triangles is less than main triangle ; If sum of smaller triangles is greater than or equal to main triangle ; Driver code ; Taking r1 , r2 , r3 as input ; Call to function binary search
import java . util . * ; class GFG { static double r1 , r2 , r3 ; static double area ( double a , double b , double c ) { double p = ( a + b + c ) / 2 ; return Math . sqrt ( p ) * Math . sqrt ( p - a ) * Math . sqrt ( p - b ) * Math . sqrt ( p - c ) ; } static double binary_search ( ) { double s = area ( r1 + r2 , r2 + r3 , r3 + r1 ) ; double l = 0 , h = s / ( r1 + r2 + r3 ) ; while ( h - l >= 1.e-7 ) { double mid = ( l + h ) / 2 ; double s1 = area ( mid + r1 , mid + r2 , r1 + r2 ) ; double s2 = area ( mid + r1 , mid + r3 , r1 + r3 ) ; double s3 = area ( mid + r2 , mid + r3 , r2 + r3 ) ; if ( s1 + s2 + s3 < s ) { l = mid ; } else { h = mid ; } } return ( l + h ) / 2 ; } public static void main ( String [ ] args ) { r1 = 1.0 ; r2 = 2.0 ; r3 = 3.0 ; System . out . printf ( " % .6f " , binary_search ( ) ) ; } }
Radius of the inscribed circle within three tangent circles | Java implementation of the approach ; Driver code ; Radius of the 3 given circles declared as double . ; Taking r1 , r2 , r3 as input ; Calculation of r4 using formula given above
class GFG { public static void main ( String [ ] args ) { double r1 , r2 , r3 ; r1 = 1 ; r2 = 2 ; r3 = 3 ; double r4 = ( r1 * r2 * r3 ) / ( r1 * r2 + r2 * r3 + r1 * r3 + 2.0 * Math . sqrt ( r1 * r2 * r3 * ( r1 + r2 + r3 ) ) ) ; System . out . printf ( " % .6f " , r4 ) ; } }
Make all elements zero by decreasing any two elements by one at a time | Java implementation of the above approach ; Function that returns true if all the array elements can be made 0 with the given operation ; Find the maximum element and the sum ; Check the required condition ; Driver code
public class GFG { static boolean checkZeroArray ( int [ ] arr , int n ) { int sum = 0 , maximum = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + arr [ i ] ; maximum = Math . max ( maximum , arr [ i ] ) ; } if ( sum % 2 == 0 && maximum <= sum / 2 ) return true ; return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 1 , 2 , 2 } ; int n = arr . length ; if ( checkZeroArray ( arr , n ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Subarray permutation that satisfies the given condition | Java implementation of the approach ; Function that returns true if the required subarray exists in the given array ; Map to store the positions of each integer in the original permutation ; To store the address of each entry in arr [ n ] but with 1 - based indexing ; To track minimum position sumcur for sum of aint positions tiint this position ; Summing up addresses ; Tracking minimum address encountered tiint now ; The sum of the addresses if it forms the required subarray ; If current sum of address is equal to val ; Driver code
import java . util . * ; class GFG { static boolean subArray ( int [ ] arr , int n , int m ) { int i ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( i = 0 ; i < n ; i ++ ) { mp . put ( arr [ i ] , i + 1 ) ; } int sumcur = 0 ; int p = Integer . MAX_VALUE ; Vector < Integer > ans = new Vector < Integer > ( ) ; for ( i = 1 ; i <= m ; i ++ ) { sumcur += mp . get ( i ) ; p = Math . min ( p , mp . get ( i ) ) ; int val = p * i - i + ( i * ( i + 1 ) ) / 2 ; if ( i == m ) { if ( val == sumcur ) { return true ; } else return false ; } } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 1 , 3 , 2 , 6 } ; int n = arr . length ; int m = 3 ; if ( subArray ( arr , n , m ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Sum of all N digit palindrome numbers | Java program for the above approach ; Function to check palindrome ; Function to calculate the sum of n - digit palindrome ; Run a loop to check all possible palindrome ; If palindrome append sum ; Driver code
import java . util . * ; class GFG { static boolean isPalindrome ( String s ) { int left = 0 , right = s . length ( ) - 1 ; while ( left <= right ) { if ( s . charAt ( left ) != s . charAt ( right ) ) { return false ; } left ++ ; right -- ; } return true ; } static long getSum ( int n ) { int start = ( int ) Math . pow ( 10 , n - 1 ) ; int end = ( int ) ( Math . pow ( 10 , n ) - 1 ) ; long sum = 0 ; for ( int i = start ; i <= end ; i ++ ) { String s = String . valueOf ( i ) ; if ( isPalindrome ( s ) ) { sum += i ; } } return sum ; } public static void main ( String [ ] args ) { int n = 1 ; long ans = getSum ( n ) ; System . out . print ( ans ) ; } }
Sum of all N digit palindrome numbers | Java program for the above approach ; Function to calculate sum of n digit number ; Corner case ; Using above approach ; Driver code
class GFG { static double getSum ( int n ) { double sum = 0 ; if ( n == 1 ) { sum = 45.0 ; } else { sum = ( 99.0 / 2.0 ) * Math . pow ( 10 , n - 1 ) * Math . pow ( 10 , ( n - 1 ) / 2 ) ; } return sum ; } public static void main ( String [ ] args ) { int n = 3 ; double ans = getSum ( n ) ; System . out . print ( ans ) ; } }
Longest sub | Java implementation of the approach ; Function to return the length of the largest subarray with maximum possible GCD ; To store the maximum number present in the array ; Finding the maximum element ; To store the final answer ; Two pointer ; Running a loop from j = i ; Condition for incrementing ' j ' ; Updating the answer ; Driver code
class GFG { static int findLength ( int [ ] arr , int n ) { int x = 0 ; for ( int i = 0 ; i < n ; i ++ ) x = Math . max ( x , arr [ i ] ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != x ) continue ; int j = i ; while ( arr [ j ] == x ) { j ++ ; if ( j >= n ) break ; } ans = Math . max ( ans , j - i ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 } ; int n = arr . length ; System . out . println ( findLength ( arr , n ) ) ; } }
Count number of pairs ( i , j ) such that arr [ i ] * arr [ j ] = arr [ i ] + arr [ j ] | Java program to count pairs ( i , j ) such that arr [ i ] * arr [ j ] = arr [ i ] + arr [ j ] ; Function to return the count of pairs ( i , j ) such that arr [ i ] * arr [ j ] = arr [ i ] + arr [ j ] ; Increment count if condition satisfy ; Return count of pairs ; Driver code ; Get and print count of pairs
class GFG { static long countPairs ( int arr [ ] , int n ) { long count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] * arr [ j ] == arr [ i ] + arr [ j ] ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 0 , 3 , 2 , 0 } ; int n = arr . length ; System . out . println ( countPairs ( arr , n ) ) ; } }
Count number of pairs ( i , j ) such that arr [ i ] * arr [ j ] = arr [ i ] + arr [ j ] | Java program to count pairs ( i , j ) such that arr [ i ] * arr [ j ] = arr [ i ] + arr [ j ] ; Function to return the count of pairs ( i , j ) such that arr [ i ] * arr [ j ] = arr [ i ] + arr [ j ] ; Count number of 0 ' s ▁ and ▁ 2' s in the array ; Total pairs due to occurrence of 0 's ; Total pairs due to occurrence of 2 's ; Return count of all pairs ; Driver code ; Get and print count of pairs
class GFG { static long countPairs ( int arr [ ] , int n ) { int countZero = 0 ; int countTwo = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) countZero ++ ; else if ( arr [ i ] == 2 ) countTwo ++ ; } long pair0 = ( countZero * ( countZero - 1 ) ) / 2 ; long pair2 = ( countTwo * ( countTwo - 1 ) ) / 2 ; return pair0 + pair2 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 0 , 3 , 2 , 0 } ; int n = arr . length ; System . out . println ( countPairs ( arr , n ) ) ; } }
Number of subsequences with positive product | Java implementation of the approach ; Function to return the count of all the subsequences with positive product ; To store the count of positive elements in the array ; To store the count of negative elements in the array ; If the current element is positive ; If the current element is negative ; For all the positive elements of the array ; For all the negative elements of the array ; For the empty subsequence ; Driver code
import java . util . * ; class GFG { static int cntSubSeq ( int arr [ ] , int n ) { int pos_count = 0 ; int neg_count = 0 ; int result ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) pos_count ++ ; if ( arr [ i ] < 0 ) neg_count ++ ; } result = ( int ) Math . pow ( 2 , pos_count ) ; if ( neg_count > 0 ) result *= Math . pow ( 2 , neg_count - 1 ) ; result -= 1 ; return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , - 3 , - 1 , 4 } ; int n = arr . length ; System . out . print ( cntSubSeq ( arr , n ) ) ; } }
Smallest number dividing minimum number of elements in the array | Set 2 | Java implementation of the approach ; Function to return the smallest number that divides minimum number of elements in the given array ; m stores the maximum in the array ; Frequency array ; Sieve ; Incrementing j ; If no multiples of j are in the array ; Driver code
class GFG { static int findMin ( int arr [ ] , int n ) { int m = 0 ; for ( int i = 0 ; i < n ; i ++ ) m = Math . max ( m , arr [ i ] ) ; int freq [ ] = new int [ m + 2 ] ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; for ( int i = 1 ; i <= m + 1 ; i ++ ) { int j = i ; int cnt = 0 ; while ( j <= m ) { cnt += freq [ j ] ; j += i ; } if ( cnt == 0 ) return i ; } return m + 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 12 , 6 } ; int n = arr . length ; System . out . println ( findMin ( arr , n ) ) ; } }
Smallest number dividing minimum number of elements in the Array | Java implementation of the approach ; Function to return the smallest number that divides minimum number of elements ; m stores the maximum in the array ; Frequency table ; Loop to factorize ; sqrt factorization of the numbers ; Finding the smallest number with zero multiples ; Driver code
class GFG { static int findMin ( int arr [ ] , int n ) { int m = 0 ; for ( int i = 0 ; i < n ; i ++ ) m = Math . max ( m , arr [ i ] ) ; int cnt [ ] = new int [ m + 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 1 ; j * j <= arr [ i ] ; j ++ ) { if ( arr [ i ] % j == 0 ) { if ( j * j == arr [ i ] ) cnt [ j ] ++ ; else { cnt [ j ] ++ ; cnt [ arr [ i ] / j ] ++ ; } } } } for ( int i = 1 ; i <= m + 1 ; i ++ ) if ( cnt [ i ] == 0 ) { return i ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 12 , 6 } ; int n = arr . length ; System . out . println ( findMin ( arr , n ) ) ; } }
Remove two consecutive integers from 1 to N to make sum equal to S | Java program remove two consecutive integers from 1 to N to make sum equal to S ; Function to find the numbers to be removed ; typecast appropriately so that answer is float ; return the obtained result ; Convert i to integer ; If i is an integer is 0 then answer is Yes ; Driver code
class GFG { static float findNumber ( int N , int S ) { float i = ( ( ( float ) ( N ) * ( float ) ( N + 1 ) ) / 4 ) - ( ( float ) ( S + 1 ) / 2 ) ; return i ; } static void check ( int N , int S ) { float i = findNumber ( N , S ) ; int integerI = ( int ) i ; if ( i - integerI == 0 ) System . out . println ( " Yes : ▁ " + integerI + " , ▁ " + ( integerI + 1 ) ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int N = 4 , S = 3 ; check ( N , S ) ; N = 5 ; S = 3 ; check ( N , S ) ; } }
Remove all the prime numbers from the given array | Java implementation of the approach ; Function for Sieve of Eratosthenes ; Function to print the elements of the array ; Function to remove all the prime numbers ; Generate primes ; Traverse the array ; If the current element is prime ; Shift all the elements on the right of it to the left ; Decrease the loop counter by 1 to check the shifted element ; Decrease the length ; Print the updated array ; Driver code
class GFG { static int sz = ( int ) 1e5 ; static boolean [ ] isPrime = new boolean [ sz + 1 ] ; static void sieve ( ) { for ( int i = 0 ; i < sz + 1 ; i ++ ) isPrime [ i ] = true ; isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= sz ; i ++ ) { if ( isPrime [ i ] ) { for ( int j = i * i ; j < sz ; j += i ) { isPrime [ j ] = false ; } } } } static void printArray ( int arr [ ] , int len ) { for ( int i = 0 ; i < len ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } static void removePrimes ( int arr [ ] , int len ) { sieve ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( isPrime [ arr [ i ] ] ) { for ( int j = i ; j < len - 1 ; j ++ ) { arr [ j ] = arr [ j + 1 ] ; } i -- ; len -- ; } } printArray ( arr , len ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 5 , 3 , 8 , 7 , 10 , 11 , 14 , 15 } ; int len = arr . length ; removePrimes ( arr , len ) ; } }
Remove one element to get minimum OR value | Java implementation of the approach ; Function to return the minimized OR after removing an element from the array ; Base case ; Prefix and suffix OR array ; Computing prefix / suffix OR arrays ; To store the final answer ; Finding the final answer ; Returning the final answer ; Driver code
class GFG { static int minOR ( int [ ] arr , int n ) { if ( n == 1 ) return 0 ; int [ ] pre = new int [ n ] ; int [ ] suf = new int [ n ] ; pre [ 0 ] = arr [ 0 ] ; suf [ n - 1 ] = arr [ n - 1 ] ; for ( int i = 1 ; i < n ; i ++ ) pre [ i ] = ( pre [ i - 1 ] arr [ i ] ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) suf [ i ] = ( suf [ i + 1 ] arr [ i ] ) ; int ans = Math . min ( pre [ n - 2 ] , suf [ 1 ] ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) ans = Math . min ( ans , ( pre [ i - 1 ] suf [ i + 1 ] ) ) ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int n = arr . length ; System . out . print ( minOR ( arr , n ) ) ; } }
Check if there exists a prime number which gives Y after being repeatedly subtracted from X | Java implementation of the approach ; Function that returns true if any prime number satisfies the given conditions ; No such prime exists ; Driver code
import java . util . * ; class GFG { static boolean isPossible ( int x , int y ) { if ( ( x - y ) == 1 ) return false ; return true ; } public static void main ( String [ ] args ) { int x = 100 , y = 98 ; if ( isPossible ( x , y ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Find number of square of area Z which can be built in a matrix having blocked regions | Java implementation of the approach ; Function to calculate the number of square areas of size K * K ; Row array and column array to store the lengths of differences between consecutive rows / columns ; Fill the conrow vector ; Fill the concol vector ; To store the required answer ; Every pair of row size and column size would result in an unblocked region ; Driver code
import java . util . * ; class GFG { static int subgrids ( int N , int Z , int row [ ] , int col [ ] , int r , int d ) { Vector < Integer > conrow = new Vector < Integer > ( ) ; Vector < Integer > concol = new Vector < Integer > ( ) ; int K = ( int ) Math . sqrt ( Z ) ; conrow . add ( row [ 0 ] - 0 - 1 ) ; conrow . add ( N + 1 - row [ r - 1 ] - 1 ) ; for ( int i = 1 ; i < r ; i ++ ) { conrow . add ( row [ i ] - row [ i - 1 ] - 1 ) ; } concol . add ( col [ 0 ] - 0 - 1 ) ; concol . add ( N + 1 - col [ d - 1 ] - 1 ) ; for ( int i = 1 ; i < d ; i ++ ) { concol . add ( col [ i ] - col [ i - 1 ] - 1 ) ; } int row_size = conrow . size ( ) ; int col_size = concol . size ( ) ; int answer = 0 ; for ( int i = 0 ; i < row_size ; i ++ ) { for ( int j = 0 ; j < col_size ; j ++ ) { int total = ( concol . get ( j ) / K ) * ( conrow . get ( i ) / K ) ; answer += ( total ) ; } } return answer ; } public static void main ( String [ ] args ) { int N = 8 , Z = 4 ; int row [ ] = { 4 , 6 } ; int col [ ] = { 3 , 8 } ; int r = row . length ; int d = col . length ; System . out . print ( subgrids ( N , Z , row , col , r , d ) ) ; } }