codeparrot/xlcost-text-to-code huggingface dataset:
Modify array by merging elements with addition such that it consists of only Primes . | C # program to find the array of primes ; dp array to store the ans for max 20 elements ; To check whether the number is prime or not ; Function to check whether the array can be modify so that there are only primes ; If curr is prime and all elements are visited , return true ; If all elements are not visited , set curr = 0 , to search for new prime sum ; If all elements are visited ; If the current sum is not prime return false ; If this state is already calculated , return the answer directly ; Try all state of mask ; If ith index is not set ; Add the current element and set ith index and recur ; If subset can be formed then return true ; After every possibility of mask , if the subset is not formed , return false by memoizing . ; Driver code
using System ; class GFG { static bool [ ] dp = new bool [ 1 << 20 ] ; static bool isprime ( int n ) { if ( n == 1 ) return false ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { return false ; } } return true ; } static bool solve ( int [ ] arr , int curr , int mask , int n ) { if ( isprime ( curr ) ) { if ( mask == ( 1 << n ) - 1 ) { return true ; } curr = 0 ; } if ( mask == ( 1 << n ) - 1 ) { if ( ! isprime ( curr ) ) { return false ; } } if ( dp [ mask ] ) return dp [ mask ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( mask & ( 1 << i ) ) == 0 ) { if ( solve ( arr , curr + arr [ i ] , mask 1 << i , n ) ) { return true ; } } } return dp [ mask ] = false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 6 , 7 , 13 } ; int n = arr . Length ; if ( solve ( arr , 0 , 0 , n ) ) { Console . Write ( " YES " ) ; } else { Console . Write ( " NO " ) ; } } }
Count all square sub | C # program to count total number of k x k sub matrix whose sum is greater than the given number S ; Function to create prefix sum matrix from the given matrix ; Store first value in table ; Initialize first row of matrix ; Initialize first column of matrix ; Initialize rest table with sum ; Utility Function to count the submatrix whose sum is greater than the S ; Loop to iterate over all the possible positions of the given matrix [ , ] mat ; Condition to check , if K x K is first sub matrix ; Condition to check sub - matrix has no margin at top ; Condition when sub matrix has no margin at left ; Condition when submatrix has margin at top and left ; Increment count , If sub matrix sum is greater than S ; Function to count submatrix of size k x k such that sum if greater than or equal to S ; For loop to initialize prefix sum matrix with zero , initially ; Function to create the prefix sum matrix ; Driver Code ; Print total number of sub matrix
using System ; class GFG { static readonly int dim = 5 ; static void createTable ( int [ , ] mtrx , int k , int p , int [ , ] dp ) { dp [ 0 , 0 ] = mtrx [ 0 , 0 ] ; for ( int j = 1 ; j < dim ; j ++ ) { dp [ 0 , j ] = mtrx [ 0 , j ] + dp [ 0 , j - 1 ] ; } for ( int i = 1 ; i < dim ; i ++ ) { dp [ i , 0 ] = mtrx [ i , 0 ] + dp [ i - 1 , 0 ] ; } for ( int i = 1 ; i < dim ; i ++ ) { for ( int j = 1 ; j < dim ; j ++ ) { dp [ i , j ] = mtrx [ i , j ] + dp [ i - 1 , j ] + dp [ i , j - 1 ] - dp [ i - 1 , j - 1 ] ; } } } static int countSubMatrixUtil ( int [ , ] dp , int k , int p ) { int count = 0 ; int subMatSum = 0 ; for ( int i = k - 1 ; i < dim ; i ++ ) { for ( int j = k - 1 ; j < dim ; j ++ ) { if ( i == ( k - 1 ) || j == ( k - 1 ) ) { if ( i == ( k - 1 ) && j == ( k - 1 ) ) { subMatSum = dp [ i , j ] ; } else if ( i == ( k - 1 ) ) { subMatSum = dp [ i , j ] - dp [ i , j - k ] ; } else { subMatSum = dp [ i , j ] - dp [ i - k , j ] ; } } else { subMatSum = dp [ i , j ] - dp [ i - k , j ] - dp [ i , j - k ] + dp [ i - k , j - k ] ; } if ( subMatSum >= p ) { count ++ ; } } } return count ; } static int countSubMatrix ( int [ , ] mtrx , int k , int p ) { int [ , ] dp = new int [ dim , dim ] ; for ( int i = 0 ; i < dim ; i ++ ) { for ( int j = 0 ; j < dim ; j ++ ) { dp [ i , j ] = 0 ; } } createTable ( mtrx , k , p , dp ) ; return countSubMatrixUtil ( dp , k , p ) ; } public static void Main ( String [ ] args ) { int [ , ] mtrx = { { 1 , 7 , 1 , 1 , 1 } , { 2 , 2 , 2 , 2 , 2 } , { 3 , 9 , 6 , 7 , 3 } , { 4 , 3 , 2 , 4 , 5 } , { 5 , 1 , 5 , 3 , 1 } } ; int k = 3 ; int p = 35 ; Console . Write ( countSubMatrix ( mtrx , k , p ) ) ; } }
Check if a substring can be Palindromic by replacing K characters for Q queries | C # program for the above approach ; Function to find whether String can be made palindromic or not for each queries ; To store the count of ith character of subString str [ 0. . . i ] ; Current character ; Update [ , ] dp on the basis recurrence relation ; For each queries ; To store the count of distinct character ; Find occurrence of i + ' a ' ; Half the distinct Count ; If half the distinct count is less than equals to K then palindromic String can be made ; Driver Code ; Given a String str ; Given Queries ; Function call
using System ; class GFG { static void canMakePaliQueries ( String str , int [ , ] Q ) { int n = str . Length ; int [ , ] dp = new int [ 26 , n ] ; for ( int i = 0 ; i < 26 ; i ++ ) { char currentChar = ( char ) ( i + ' a ' ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( j == 0 ) { dp [ i , j ] = ( str [ j ] == currentChar ) ? 1 : 0 ; } else { dp [ i , j ] = dp [ i , j - 1 ] + ( ( str [ j ] == currentChar ) ? 1 : 0 ) ; } } } for ( int l = 0 ; l < Q . GetLength ( 0 ) ; l ++ ) { int [ ] query = GetRow ( Q , l ) ; int left = query [ 0 ] ; int right = query [ 1 ] ; int k = query [ 2 ] ; int unMatchedCount = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { int occurrence = dp [ i , right ] - dp [ i , left ] + ( str [ left ] == ( i + ' a ' ) ? 1 : 0 ) ; if ( occurrence % 2 == 1 ) unMatchedCount ++ ; } int ans = unMatchedCount / 2 ; if ( ans <= k ) { Console . Write ( " YES STRNEWLINE " ) ; } else { Console . Write ( " NO STRNEWLINE " ) ; } } } public static int [ ] GetRow ( int [ , ] matrix , int row ) { var rowLength = matrix . GetLength ( 1 ) ; var rowVector = new int [ rowLength ] ; for ( var i = 0 ; i < rowLength ; i ++ ) rowVector [ i ] = matrix [ row , i ] ; return rowVector ; } public static void Main ( String [ ] args ) { String str = " GeeksforGeeks " ; int [ , ] Q = { { 1 , 5 , 3 } , { 5 , 7 , 0 } , { 8 , 11 , 3 } , { 3 , 10 , 5 } , { 0 , 9 , 5 } } ; canMakePaliQueries ( str , Q ) ; } }
Maximize product of digit sum of consecutive pairs in a subsequence of length K | C # implementation to find the maximum product of the digit sum of the consecutive pairs of the subsequence of the length K ; Function to find the product of two numbers digit sum in the pair ; Loop to find the digits of the number ; Loop to find the digits of other number ; Function to find the subsequence of the length K ; Base Case ; Condition when we didn 't reach  the length K, but ran out of  elements of the array ; Condition if already calculated ; If length upto this point is odd ; If length is odd , it means we need second element of this current pair , calculate the product of digit sum of current and previous element and recur by moving towards next index ; If length upto this point is even ; Exclude this current element and recur for next elements . ; Return by memoizing it , by selecting the maximum among two choices . ; Driver Code
using System ; class GFG { static int MAX = 100 ; static int [ , , ] dp = new int [ 1000 , MAX , MAX ] ; static int productDigitSum ( int x , int y ) { int sumx = 0 ; while ( x > 0 ) { sumx += ( x % 10 ) ; x /= 10 ; } int sumy = 0 ; while ( y > 0 ) { sumy += ( y % 10 ) ; y /= 10 ; } return ( sumx * sumy ) ; } static int solve ( int [ ] arr , int i , int len , int prev , int n , int k ) { if ( len == k ) return 0 ; if ( i == n ) return Int32 . MinValue ; if ( dp [ i , len , prev ] != 0 ) return dp [ i , len , prev ] ; int inc = 0 , exc = 0 ; if ( ( len & 1 ) != 0 ) { inc = ( productDigitSum ( arr [ prev ] , arr [ i ] ) + solve ( arr , i + 1 , len + 1 , 0 , n , k ) ) ; } else { inc = solve ( arr , i + 1 , len + 1 , i , n , k ) ; } exc = solve ( arr , i + 1 , len , prev , n , k ) ; return dp [ i , len , prev ] = Math . Max ( inc , exc ) ; } public static void Main ( ) { int [ ] arr = { 10 , 5 , 9 , 101 , 24 , 2 , 20 , 14 } ; int n = arr . Length ; int k = 6 ; Console . Write ( solve ( arr , 0 , 0 , 0 , n , k ) ) ; } }
Count minimum factor jumps required to reach the end of an Array | C # code to count minimum factor jumps to reach the end of array ; vector to store factors of each integer ; dp array ; Precomputing all factors of integers from 1 to 100000 ; Function to count the minimum jumps ; If we reach the end of array , no more jumps are required ; If the jump results in out of index , return int . MaxValue ; If the answer has been already computed , return it directly ; Else compute the answer using the recurrence relation ; Iterating over all choices of jumps ; Considering current factor as a jump ; Jump leads to the destination ; Return ans and memorize it ; Driver code ; pre - calculating the factors
using System ; using System . Collections . Generic ; class GFG { static List < int > [ ] factors = new List < int > [ 100005 ] ; static int [ ] dp = new int [ 100005 ] ; static void precompute ( ) { for ( int i = 0 ; i < factors . Length ; i ++ ) factors [ i ] = new List < int > ( ) ; for ( int i = 1 ; i <= 100000 ; i ++ ) { for ( int j = i ; j <= 100000 ; j += i ) { factors [ j ] . Add ( i ) ; } } } static int solve ( int [ ] arr , int k , int n ) { if ( k == n - 1 ) { return 0 ; } if ( k >= n ) { return int . MaxValue ; } if ( dp [ k ] != 0 ) { return dp [ k ] ; } int ans = int . MaxValue ; foreach ( int j in factors [ arr [ k ] ] ) { int res = solve ( arr , k + j , n ) ; if ( res != int . MaxValue ) { ans = Math . Min ( ans , res + 1 ) ; } } return dp [ k ] = ans ; } public static void Main ( String [ ] args ) { precompute ( ) ; int [ ] arr = { 2 , 8 , 16 , 55 , 99 , 100 } ; int n = arr . Length ; Console . Write ( solve ( arr , 0 , n ) ) ; } }
Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix | C # program to find the maximum distance from a 0 - cell to a 1 - cell ; If the matrix consists of only 0 ' s ▁ ▁ or ▁ only ▁ 1' s ; If it 's a 0-cell ; Calculate its distance with every 1 - cell ; Compare and store the minimum ; Compare ans store the maximum ; Driver code
using System ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int maxDistance ( int [ , ] grid ) { List < pair > one = new List < pair > ( ) ; int M = grid . GetLength ( 0 ) ; int N = grid . GetLength ( 1 ) ; int ans = - 1 ; for ( int i = 0 ; i < M ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) { if ( grid [ i , j ] == 1 ) one . Add ( new pair ( i , j ) ) ; } } if ( one . Count == 0 M * N == one . Count ) return - 1 ; for ( int i = 0 ; i < M ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) { if ( grid [ i , j ] == 1 ) continue ; int dist = int . MaxValue ; foreach ( pair p in one ) { int d = Math . Abs ( p . first - i ) + Math . Abs ( p . second - j ) ; dist = Math . Min ( dist , d ) ; if ( dist <= ans ) break ; } ans = Math . Max ( ans , dist ) ; } } return ans ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 0 , 0 , 1 } , { 0 , 0 , 0 } , { 0 , 0 , 0 } } ; Console . Write ( maxDistance ( arr ) + " STRNEWLINE " ) ; } }
Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix | C # program to find the maximum distance from a 0 - cell to a 1 - cell ; Function to find the maximum distance ; Queue to store all 1 - cells ; Grid dimensions ; Directions traversable from a given a particular cell ; If the grid contains only 0 s or only 1 s ; Access every 1 - cell ; Traverse all possible directions from the cells ; Check if the cell is within the boundaries or contains a 1 ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int index = 0 ; class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int maxDistance ( int [ , ] grid ) { Queue < pair > q = new Queue < pair > ( ) ; int M = grid . GetLength ( 0 ) ; int N = grid . GetLength ( 1 ) ; int ans = - 1 ; int [ , ] dirs = { { 0 , 1 } , { 1 , 0 } , { 0 , - 1 } , { - 1 , 0 } } ; for ( int i = 0 ; i < M ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) { if ( grid [ i , j ] == 1 ) q . Enqueue ( new pair ( i , j ) ) ; } } if ( q . Count == 0 M * N == q . Count ) return - 1 ; while ( q . Count > 0 ) { int cnt = q . Count ; while ( cnt -- > 0 ) { pair p = q . Peek ( ) ; q . Dequeue ( ) ; for ( int i = 0 ; i < dirs . GetLength ( 0 ) ; ) { int [ ] dir = GetRow ( dirs , i ++ ) ; int x = p . first + dir [ 0 ] ; int y = p . second + dir [ 1 ] ; if ( x < 0 x >= M y < 0 y >= N grid [ x , y ] > 0 ) continue ; q . Enqueue ( new pair ( x , y ) ) ; grid [ x , y ] = 1 ; } } ++ ans ; } return ans ; } public static int [ ] GetRow ( int [ , ] matrix , int row ) { var rowLength = matrix . GetLength ( 1 ) ; var rowVector = new int [ rowLength ] ; for ( var i = 0 ; i < rowLength ; i ++ ) rowVector [ i ] = matrix [ row , i ] ; return rowVector ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 0 , 0 , 1 } , { 0 , 0 , 0 } , { 0 , 0 , 1 } } ; Console . Write ( maxDistance ( arr ) + " STRNEWLINE " ) ; } }
Count the number of ways to give ranks for N students such that same ranks are possible | C # program to calculate the number of ways to give ranks for N students such that same ranks are possible ; Initializing a table in order to store the bell triangle ; Function to calculate the K - th bell number ; If we have already calculated the bell numbers until the required N ; Base case ; First Bell Number ; If the value of the bell triangle has already been calculated ; Fill the defined dp table ; Function to return the number of ways to give ranks for N students such that same ranks are possible ; Resizing the dp table for the given value of n ; Variables to store the answer and the factorial value ; Iterating till N ; Simultaneously calculate the k ! ; Computing the K - th bell number and multiplying it with K ! ; Driver code
using System ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int [ , ] dp ; static int f ( int n , int k ) { if ( n < k ) return 0 ; if ( n == k ) return 1 ; if ( k == 1 ) return 1 ; if ( dp [ n , k ] != - 1 ) return dp [ n , k ] ; return dp [ n , k ] = ( ( k * f ( n - 1 , k ) ) % mod + ( f ( n - 1 , k - 1 ) ) % mod ) % mod ; } static long operation ( int n ) { dp = new int [ n + 1 , n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < n + 1 ; j ++ ) { dp [ i , j ] = - 1 ; } } long ans = 0 , fac = 1 ; for ( int k = 1 ; k <= n ; k ++ ) { fac *= k ; ans = ( ans + ( fac * f ( n , k ) ) % mod ) % mod ; } return ans ; } public static void Main ( String [ ] args ) { int n = 5 ; Console . Write ( operation ( n ) + " STRNEWLINE " ) ; } }
Count the number of ordered sets not containing consecutive numbers | C # program to count the number of ordered sets not containing consecutive numbers ; Function to calculate the count of ordered set for a given size ; Base cases ; Function returns the count of all ordered sets ; Prestore the factorial value ; Iterate all ordered set sizes and find the count for each one maximum ordered set size will be smaller than N as all elements are distinct and non consecutive ; Multiply ny size ! for all the arrangements because sets are ordered ; Add to total answer ; Driver code
using System ; class GFG { static int CountSets ( int x , int pos ) { if ( x <= 0 ) { if ( pos == 0 ) return 1 ; else return 0 ; } if ( pos == 0 ) return 1 ; int answer = CountSets ( x - 1 , pos ) + CountSets ( x - 2 , pos - 1 ) ; return answer ; } static int CountOrderedSets ( int n ) { int [ ] factorial = new int [ 10000 ] ; factorial [ 0 ] = 1 ; for ( int i = 1 ; i < 10000 ; i ++ ) factorial [ i ] = factorial [ i - 1 ] * i ; int answer = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int sets = CountSets ( n , i ) * factorial [ i ] ; answer = answer + sets ; } return answer ; } public static void Main ( String [ ] args ) { int N = 3 ; Console . Write ( CountOrderedSets ( N ) ) ; } }
Maximum sum such that exactly half of the elements are selected and no two adjacent | C # program to find maximum sum possible such that exactly Math . floor ( N / 2 ) elements are selected and no two selected elements are adjacent to each other ; Function return the maximum sum possible under given condition ; Intitialising the dp table ; Base case ; Condition to select the current element ; Condition to not select the current element if possible ; Driver code
using System ; class GFG { static int MaximumSum ( int [ ] a , int n ) { int [ , ] dp = new int [ n + 1 , n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < n + 1 ; j ++ ) dp [ i , j ] = Int32 . MinValue ; } for ( int i = 0 ; i < n + 1 ; i ++ ) dp [ i , 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { int val = Int32 . MinValue ; if ( ( i - 2 >= 0 && dp [ i - 2 , j - 1 ] != Int32 . MinValue ) i - 2 < 0 ) { val = a [ i - 1 ] + ( i - 2 >= 0 ? dp [ i - 2 , j - 1 ] : 0 ) ; } if ( i - 1 >= j ) { val = Math . Max ( val , dp [ i - 1 , j ] ) ; } dp [ i , j ] = val ; } } return dp [ n , n / 2 ] ; } public static void Main ( ) { int [ ] A = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = A . Length ; Console . Write ( MaximumSum ( A , N ) ) ; } }
Check if N can be converted to the form K power K by the given operation | C # implementation to check whether a given number N can be converted to the form K power K by the given operation ; Function to check if N can be converted to K power K ; Check if n is of the form k ^ k ; Iterate through each digit of n ; Check if it is possible to obtain number of given form ; Reduce the number each time ; Return the result ; Function to check the above method ; Check if conversion if possible ; Driver code ; Pre store K power K form of numbers Loop till 8 , because 8 ^ 8 > 10 ^ 7
using System ; using System . Collections . Generic ; class GFG { static SortedSet < int > kPowKform = new SortedSet < int > ( ) ; static int func ( int n ) { if ( n <= 0 ) return 0 ; if ( kPowKform . Contains ( n ) ) return 1 ; int answer = 0 ; int x = n ; while ( x > 0 ) { int d = x % 10 ; if ( d != 0 ) { if ( func ( n - d * d ) == 1 ) { answer = 1 ; break ; } } x /= 10 ; } return answer ; } static void canBeConverted ( int n ) { if ( func ( n ) == 1 ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( ) { int N = 90 ; for ( int i = 1 ; i <= 8 ; i ++ ) { int val = 1 ; for ( int j = 1 ; j <= i ; j ++ ) val *= i ; kPowKform . Add ( val ) ; } canBeConverted ( N ) ; } }
Count of subarrays of an Array having all unique digits | C # program to find the count of subarrays of an Array having all unique digits ; Function to check whether the subarray has all unique digits ; Storing all digits occurred ; Traversing all the numbers of v ; Storing all digits of v [ i ] ; Checking whether digits of v [ i ] have already occurred ; Inserting digits of v [ i ] in the set ; Function to count the number subarray with all digits unique ; Traverse through all the subarrays ; To store elements of this subarray ; Generate all subarray and store it in vector ; Check whether this subarray has all digits unique ; Increase the count ; Return the count ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool check ( List < int > v ) { HashSet < int > digits = new HashSet < int > ( ) ; for ( int i = 0 ; i < v . Count ; i ++ ) { HashSet < int > d = new HashSet < int > ( ) ; while ( v [ i ] > 0 ) { d . Add ( v [ i ] % 10 ) ; v [ i ] = v [ i ] / 10 ; } foreach ( int it in d ) { if ( digits . Contains ( it ) ) return false ; } foreach ( int it in d ) digits . Add ( it ) ; } return true ; } static int numberOfSubarrays ( int [ ] a , int n ) { int answer = 0 ; for ( int i = 1 ; i < ( 1 << n ) ; i ++ ) { List < int > temp = new List < int > ( ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) > 0 ) temp . Add ( a [ j ] ) ; } if ( check ( temp ) ) answer ++ ; } return answer ; } public static void Main ( String [ ] args ) { int N = 4 ; int [ ] A = { 1 , 12 , 23 , 34 } ; Console . Write ( numberOfSubarrays ( A , N ) ) ; } }
Find the row whose product has maximum count of prime factors | C # implementation to find the row whose product has maximum number of prime factors ; function for SieveOfEratosthenes ; Create a bool array " isPrime [ 0 . . N ] " and initialize all entries it as true . A value in isPrime [ i ] will finally be false if i is not a prime , else true . ; check if isPrime [ p ] is not changed ; Update all multiples of p ; Print all isPrime numbers ; function to display the answer ; function to Count the row number of divisors in particular row multiplication ; Find count of occurrences of each prime factor ; Compute count of all divisors ; Update row number if factors of this row is max ; Clearing map to store prime factors for next row ; Driver code
using System ; using System . Collections . Generic ; class GFG { static readonly int N = 3 ; static readonly int M = 5 ; static int Large = ( int ) 1e6 ; static List < int > prime = new List < int > ( ) ; static void SieveOfEratosthenes ( ) { bool [ ] isPrime = new bool [ Large + 1 ] ; for ( int p = 0 ; p <= Large ; p ++ ) isPrime [ p ] = true ; for ( int p = 2 ; p * p <= Large ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= Large ; i += p ) isPrime [ i ] = false ; } } for ( int p = 2 ; p <= Large ; p ++ ) if ( isPrime [ p ] ) prime . Add ( p ) ; } static void Display ( int [ , ] arr , int row ) { for ( int i = 0 ; i < M ; i ++ ) Console . Write ( arr [ row , i ] + " ▁ " ) ; } static void countDivisorsMult ( int [ , ] arr ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int row_no = 0 ; long max_factor = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { int no = arr [ i , j ] ; for ( int k = 0 ; k < prime . Count ; k ++ ) { while ( no > 1 && no % prime [ k ] == 0 ) { no /= prime [ k ] ; if ( mp . ContainsKey ( prime [ k ] ) ) mp [ prime [ k ] ] = prime [ k ] + 1 ; else mp . Add ( prime [ k ] , 1 ) ; } if ( no == 1 ) break ; } } int res = 1 ; foreach ( KeyValuePair < int , int > it in mp ) { res *= ( it . Value + 1 ) ; } if ( max_factor < res ) { row_no = i ; max_factor = res ; } mp . Clear ( ) ; } Display ( arr , row_no ) ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 1 , 2 , 3 , 10 , 23 } , { 4 , 5 , 6 , 7 , 8 } , { 7 , 8 , 9 , 15 , 45 } } ; SieveOfEratosthenes ( ) ; countDivisorsMult ( arr ) ; } }
Smallest N digit number whose sum of square of digits is a Perfect Square | C # implementation to find Smallest N digit number whose sum of square of digits is a Perfect Square ; function to check if number is a perfect square ; Function to calculate the smallest N digit number ; Place digits greater than equal to prev ; Check if placing this digit leads to a solution then return it ; Else backtrack ; Create a string representing the N digit number ; Driver code ; Initialise N
using System ; class GFG { static int isSquare ( int n ) { int k = ( int ) Math . Sqrt ( n ) ; return k * k == n ? 1 : 0 ; } static int calculate ( int pos , int prev , int sum , int [ ] v ) { if ( pos == v . Length ) return isSquare ( sum ) ; for ( int i = prev ; i <= 9 ; i ++ ) { v [ pos ] = i ; sum += i * i ; if ( calculate ( pos + 1 , i , sum , v ) != 0 ) return 1 ; sum -= i * i ; } return 0 ; } static string minValue ( int n ) { int [ ] v = new int [ n ] ; if ( calculate ( 0 , 1 , 0 , v ) != 0 ) { string answer = " " ; for ( int i = 0 ; i < v . Length ; i ++ ) answer += ( char ) ( v [ i ] + '0' ) ; return answer ; } else return " - 1" ; } public static void Main ( ) { int N = 2 ; Console . Write ( minValue ( N ) ) ; } }