codeparrot/xlcost-text-to-code huggingface dataset:
Count of odd length contiguous Palindromic sequences in a Matrix | C # 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
using System ; class GFG { static 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 ( ) { int m = 3 , n = 3 ; int [ , ] matrix = { { 2 , 1 , 2 } , { 1 , 1 , 1 } , { 2 , 1 , 2 } } ; Console . WriteLine ( countPalindromes ( n , m , matrix ) ) ; } }
Reduce a number to 1 by performing given operations | Set 2 | C # 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
using System ; 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 ; Console . Write ( minSteps ( n ) ) ; } }
Minimum integer that can be obtained by swapping adjacent digits of different parity | C # 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
using System ; using System . Collections . Generic ; class GFG { static int minimumNo ( int n ) { int ans = 0 ; Stack < int > stack1 = new Stack < int > ( ) ; Stack < int > stack2 = new Stack < int > ( ) ; while ( n != 0 ) { int r = n % 10 ; if ( r % 2 == 0 ) { stack1 . Push ( r ) ; } else { stack2 . Push ( r ) ; } n = n / 10 ; } while ( stack1 . Count != 0 && stack2 . Count != 0 ) { if ( stack1 . Peek ( ) < stack2 . Peek ( ) ) { ans = ans * 10 + stack1 . Peek ( ) ; stack1 . Pop ( ) ; } else { ans = ans * 10 + stack2 . Peek ( ) ; stack2 . Pop ( ) ; } } while ( stack1 . Count != 0 ) { ans = ans * 10 + stack1 . Peek ( ) ; stack1 . Pop ( ) ; } while ( stack2 . Count != 0 ) { ans = ans * 10 + stack2 . Peek ( ) ; stack2 . Pop ( ) ; } return ans ; } public static void Main ( String [ ] args ) { int n1 = 64432 ; Console . Write ( minimumNo ( n1 ) + " STRNEWLINE " ) ; int n2 = 3137 ; Console . Write ( minimumNo ( n2 ) + " STRNEWLINE " ) ; } }
Find two numbers with the given LCM and minimum possible difference | C # 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
using System ; 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 ; } } Console . Write ( 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 | C # 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
using System ; class GFG { 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 ( ) { int [ ] A = { 13 , 1 , 2 , 5 } ; int [ ] D = { 5 , 10 , 1 , 12 } ; int N = A . Length ; Console . WriteLine ( maxCommonElement ( A , D , N ) ) ; } }
Number of trailing zeros in N * ( N | C # 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
using System ; 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 ; Console . WriteLine ( findTrailingZeros ( n ) ) ; } }
Divide N into K unique parts such that gcd of those parts is maximum | C # 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
using System ; 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 ( ) { int N = 18 , K = 3 ; Console . WriteLine ( maxGCD ( N , K ) ) ; } }
Radius of the inscribed circle within three tangent circles | C # 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
using System ; 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 > 0.00000001 ) { 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 ; Console . Write ( " { 0 : F6 } " , binary_search ( ) ) ; } }
Radius of the inscribed circle within three tangent circles | C # 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
using System ; 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 ) ) ) ; Console . Write ( " { 0 : F6 } " , r4 ) ; } }
Make all elements zero by decreasing any two elements by one at a time | C # 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
using System ; class GFG { static bool checkZeroArray ( int [ ] arr , int n ) { int sum = 0 , maximum = int . MinValue ; 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 ( ) { int [ ] arr = { 1 , 2 , 1 , 2 , 2 } ; int n = arr . Length ; if ( checkZeroArray ( arr , n ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Subarray permutation that satisfies the given condition | C # 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
using System ; using System . Collections . Generic ; class GFG { static bool subArray ( int [ ] arr , int n , int m ) { int i ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( i = 0 ; i < n ; i ++ ) { mp . Add ( arr [ i ] , i + 1 ) ; } int sumcur = 0 ; int p = int . MaxValue ; List < int > ans = new List < int > ( ) ; for ( i = 1 ; i <= m ; i ++ ) { sumcur += mp [ i ] ; p = Math . Min ( p , mp [ 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 ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Sum of all N digit palindrome numbers | C # 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
using System ; class GFG { static bool isPalindrome ( string s ) { int left = 0 , right = s . Length - 1 ; while ( left <= right ) { if ( s [ left ] != s [ 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 = i . ToString ( ) ; ; if ( isPalindrome ( s ) ) { sum += i ; } } return sum ; } public static void Main ( ) { int n = 1 ; long ans = getSum ( n ) ; Console . Write ( ans ) ; } }
Sum of all N digit palindrome numbers | C # program for the above approach ; Function to calculate sum of n digit number ; Corner case ; Using above approach ; Driver code
using System ; 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 ) ; Console . Write ( ans ) ; } }
Longest sub | C # 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
using System ; 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 ( ) { int [ ] arr = { 1 , 2 , 2 } ; int n = arr . Length ; Console . WriteLine ( findLength ( arr , n ) ) ; } }
Count number of pairs ( i , j ) such that arr [ i ] * arr [ j ] = arr [ i ] + arr [ j ] | C # 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
using System ; 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 ; Console . WriteLine ( countPairs ( arr , n ) ) ; } }
Count number of pairs ( i , j ) such that arr [ i ] * arr [ j ] = arr [ i ] + arr [ j ] | C # 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
using System ; 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 ; Console . WriteLine ( countPairs ( arr , n ) ) ; } }
Number of subsequences with positive product | C # 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
using System ; 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 *= ( int ) Math . Pow ( 2 , neg_count - 1 ) ; result -= 1 ; return result ; } public static void Main ( ) { int [ ] arr = { 2 , - 3 , - 1 , 4 } ; int n = arr . Length ; Console . Write ( cntSubSeq ( arr , n ) ) ; } }
Smallest number dividing minimum number of elements in the array | Set 2 | C # 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
using System ; 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 ( ) { int [ ] arr = { 2 , 12 , 6 } ; int n = arr . Length ; Console . WriteLine ( findMin ( arr , n ) ) ; } }
Smallest number dividing minimum number of elements in the Array | C # 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
using System ; 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 ; Console . WriteLine ( findMin ( arr , n ) ) ; } }
Remove two consecutive integers from 1 to N to make sum equal to S | C # 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
using System ; 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 ) Console . WriteLine ( " Yes : ▁ " + integerI + " , ▁ " + ( integerI + 1 ) ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( ) { int N = 4 , S = 3 ; check ( N , S ) ; N = 5 ; S = 3 ; check ( N , S ) ; } }
Remove all the prime numbers from the given array | C # 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
using System ; class GFG { static int sz = ( int ) 1e5 ; static bool [ ] isPrime = new bool [ 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 ++ ) { Console . Write ( 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 | C # 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
using System ; 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 ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 } ; int n = arr . Length ; Console . WriteLine ( minOR ( arr , n ) ) ; } }
Check if there exists a prime number which gives Y after being repeatedly subtracted from X | C # implementation of the approach ; Function that returns true if any prime number satisfies the given conditions ; No such prime exists ; Driver code
using System ; class GFG { static bool 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 ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Find number of square of area Z which can be built in a matrix having blocked regions | C # 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
using System ; using System . Collections . Generic ; class GFG { static int subgrids ( int N , int Z , int [ ] row , int [ ] col , int r , int d ) { List < int > conrow = new List < int > ( ) ; List < int > concol = new List < int > ( ) ; 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 . Count ; int col_size = concol . Count ; int answer = 0 ; for ( int i = 0 ; i < row_size ; i ++ ) { for ( int j = 0 ; j < col_size ; j ++ ) { int total = ( concol [ j ] / K ) * ( conrow [ 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 ; Console . Write ( subgrids ( N , Z , row , col , r , d ) ) ; } }