codeparrot/xlcost-text-to-code huggingface dataset:
Minimize replacements to make every element in an array exceed every element in another given array | C # program for the above approach ; Function to find the minimize replacements to make every element in the array A [ ] strictly greater than every element in B [ ] or vice - versa ; Store the final result ; Create two arrays and initialize with 0 s ; Traverse the array a [ ] ; Increment prefix_a [ a [ i ] ] by 1 ; Traverse the array b [ ] ; Increment prefix_b [ b [ i ] ] by 1 ; Calculate prefix sum of the array a [ ] ; Calculate prefix sum of the array b [ ] ; Iterate over the range [ 0 , 9 ] ; Make every element in array a [ ] strictly greater than digit i and make every element in the array b [ ] less than digit i ; Make every element in array b [ ] strictly greater than digit i and make every element in array a [ ] less than digit i ; Print the answer ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void MinTime ( int [ ] a , int [ ] b , int n , int m ) { int ans = 2147483647 ; int [ ] prefix_a = new int [ 10 ] ; int [ ] prefix_b = new int [ 10 ] ; Array . Clear ( prefix_a , 0 , prefix_a . Length ) ; Array . Clear ( prefix_b , 0 , prefix_b . Length ) ; for ( int i = 0 ; i < n ; i ++ ) { prefix_a [ a [ i ] ] ++ ; } for ( int i = 0 ; i < m ; i ++ ) { prefix_b [ b [ i ] ] ++ ; } for ( int i = 1 ; i <= 9 ; i ++ ) { prefix_a [ i ] += prefix_a [ i - 1 ] ; } for ( int i = 1 ; i <= 9 ; i ++ ) { prefix_b [ i ] += prefix_b [ i - 1 ] ; } for ( int i = 0 ; i <= 9 ; i ++ ) { ans = Math . Min ( ans , prefix_a [ i ] + m - prefix_b [ i ] ) ; ans = Math . Min ( ans , n - prefix_a [ i ] + prefix_b [ i ] ) ; } Console . WriteLine ( ans ) ; } public static void Main ( ) { int [ ] A = { 0 , 0 , 1 , 3 , 3 } ; int [ ] B = { 2 , 0 , 3 } ; int N = A . Length ; int M = B . Length ; MinTime ( A , B , N , M ) ; } }
Count remaining array elements after reversing binary representation of each array element | C # program for the above approach ; Function to reverse the binary representation of a number ; Traverse bits of N from the right ; Bitwise left shift ' rev ' by 1 ; If current bit is '1' ; Bitwise right shift N by 1 ; Required number ; Function to count elements from the original array that are also present in the array formed by reversing the binary representation of each element ; Stores the reversed num ; Iterate from [ 0 , N ] ; Stores the presence of integer ; Stores count of elements present in original array ; Traverse the array ; If current number is present ; Print the answer ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int findReverse ( int N ) { int rev = 0 ; while ( N > 0 ) { rev <<= 1 ; if ( ( N & 1 ) == 1 ) rev ^= 1 ; N >>= 1 ; } return rev ; } static void countElements ( int [ ] arr , int N ) { List < int > ans = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { ans . Add ( findReverse ( arr [ i ] ) ) ; } Dictionary < int , int > cnt = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { cnt [ arr [ i ] ] = 1 ; } int count = 0 ; foreach ( int i in ans ) { if ( cnt . ContainsKey ( i ) ) count ++ ; } Console . WriteLine ( count ) ; } public static void Main ( ) { int [ ] arr = { 1 , 30 , 3 , 8 , 12 } ; int N = arr . Length ; countElements ( arr , N ) ; } }
Fizz Buzz Implementation | Set 2 | C # program for the above approach ; Function to generate FizzBuzz sequence ; Stores count of multiples of 3 and 5 respectively ; Iterate from 1 to N ; Increment count3 by 1 ; Increment count5 by 1 ; Initialize a bool variable to check if none of the condition matches ; Check if the value of count3 is equal to 3 ; Reset count3 to 0 , and set flag as True ; Check if the value of count5 is equal to 5 ; Reset count5 to 0 , and set flag as True ; If none of the condition matches ; Driver Code
using System ; class GFG { static void fizzBuzz ( int N ) { int count3 = 0 ; int count5 = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { count3 ++ ; count5 ++ ; bool flag = false ; if ( count3 == 3 ) { Console . Write ( " Fizz " ) ; count3 = 0 ; flag = true ; } if ( count5 == 5 ) { Console . Write ( " Buzz " ) ; count5 = 0 ; flag = true ; } if ( ! flag ) { Console . Write ( i ) ; } Console . Write ( " ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 15 ; fizzBuzz ( N ) ; } }
Check if diagonal elements of a Matrix are Prime or not | C # program for the above approach ; Stores if a number is prime or not ; Function to generate and store primes using Sieve Of Eratosthenes ; Set all numbers as prime ; If p is a prime ; Set all its multiples as non - prime ; Function to check if all diagonal elements are prime or not ; Stores if all diagonal elements are prime or not ; Precompute primes ; Traverse the range [ 0 , N - 1 ] ; Check if numbers on the cross diagonal and main diagonal are primes or not ; If true , then print " Yes " ; Otherwise , print " No " ; Driver Code ; Function Call
using System ; class GFG { static bool [ ] prime = new bool [ 1000005 ] ; static void SieveOfEratosthenes ( int N ) { Array . Fill ( prime , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= N ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= N ; i += p ) prime [ i ] = false ; } } } static void checkElementsOnDiagonal ( int [ , ] M , int N ) { int flag = 1 ; SieveOfEratosthenes ( 1000000 ) ; for ( int i = 0 ; i < N ; i ++ ) { bool flg = ( bool ) ( prime [ M [ i , i ] ] && prime [ M [ i , N - 1 - i ] ] ) ; int val = ( flg ) ? 1 : 0 ; flag &= val ; } if ( flag != 0 ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( string [ ] args ) { int [ , ] M = { { 1 , 2 , 3 , 13 } , { 5 , 3 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 7 } } ; int N = M . GetLength ( 0 ) ; checkElementsOnDiagonal ( M , N ) ; } }
Generate a circular permutation with number of mismatching bits between pairs of adjacent elements exactly 1 | C # program for the above approach ; Function to find the permutation of integers from a given range such that number of mismatching bits between pairs of adjacent elements is 1 ; Initialize an arrayList to store the resultant permutation ; Store the index of rotation ; Iterate over the range [ 0 , N - 1 ] ; Traverse all the array elements up to ( 2 ^ k ) - th index in reverse ; If current element is S ; Check if S is zero ; Rotate the array by index value to the left ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { public static List < int > circularPermutation ( int n , int start ) { List < int > res = new List < int > ( ) { 0 } ; List < int > ret = new List < int > ( ) ; int index = - 1 ; for ( int k = 0 , add = 1 << k ; k < n ; k ++ , add = 1 << k ) { for ( int i = res . Count - 1 ; i >= 0 ; i -- ) { if ( res [ i ] + add == start ) index = res . Count ; res . Add ( res [ i ] + add ) ; } } if ( start == 0 ) return res ; while ( ret . Count < res . Count ) { ret . Add ( res [ index ] ) ; index = ( index + 1 ) % res . Count ; } return ret ; } static public void Main ( ) { int N = 2 , S = 3 ; List < int > print = circularPermutation ( N , S ) ; Console . Write ( " [ " ) ; for ( int i = 0 ; i < print . Count - 1 ; i ++ ) { Console . Write ( print [ i ] + " , ▁ " ) ; } Console . Write ( print [ print . Count - 1 ] + " ] " ) ; } }
Count pairs from an array having equal sum and quotient | C # program for the above approach ; Function to count all pairs ( i , j ) such that a [ i ] + [ j ] = a [ i ] / a [ j ] ; Stores total count of pairs ; Generate all possible pairs ; If a valid pair is found ; Increment count ; Return the final count ; Driver code
using System ; class GFG { static int countPairs ( int [ ] a , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( a [ j ] != 0 && a [ i ] % a [ j ] == 0 ) { if ( ( a [ i ] + a [ j ] ) == ( a [ i ] / a [ j ] ) ) count ++ ; } } } return count ; } static void Main ( ) { int [ ] arr = { - 4 , - 3 , 0 , 2 , 1 } ; int N = arr . Length ; Console . WriteLine ( countPairs ( arr , N ) ) ; } }
Count pairs from an array having equal sum and quotient | C # program for the above approach ; Function to find number of pairs with equal sum and quotient from a given array ; Store the count of pairs ; Stores frequencies ; Traverse the array ; If y is neither 1 or 0 ; Evaluate x ; Increment count by frequency of x ; Update map ; Print the final count ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int countPairs ( int [ ] a , int n ) { int count = 0 ; Dictionary < double , int > mp = new Dictionary < double , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int y = a [ i ] ; if ( y != 0 && y != 1 ) { double x = ( ( y * 1.0 ) / ( 1 - y ) ) * y ; if ( ! mp . ContainsKey ( x ) ) mp [ x ] = 0 ; count += mp [ x ] ; } if ( ! mp . ContainsKey ( y ) ) mp [ y ] = 0 ; mp [ y ] ++ ; } return count ; } public static void Main ( ) { int [ ] arr = { - 4 , - 3 , 0 , 2 , 1 } ; int N = arr . Length ; Console . Write ( countPairs ( arr , N ) ) ; } }
Queries to count numbers from a range which does not contain digit K in their decimal or octal representation | C # program for the above approach ; Function to check if the given digit ' K ' is present in the decimal and octal representations of num or not ; Stores if the digit exists or not ; Iterate till nums is non - zero ; Find the remainder ; If the remainder is K ; Function to count the numbers in the range [ 1 , N ] such that it doesn ' t ▁ ▁ contain ▁ the ▁ digit ▁ ' K ' in its decimal  and octal representation ; Stores count of numbers in the range [ 0 , i ] that contains the digit ' K ' in its octal or decimal representation ; Traverse the range [ 0 , 1e6 + 5 ] ; Check if i contains the digit ' K ' in its decimal or octal representation ; Update pref [ i ] ; Print the answer of queries ; Driver code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static bool contains ( int num , int K , int Base ) { bool isThere = false ; while ( num > 0 ) { int remainder = num % Base ; if ( remainder == K ) { isThere = true ; } num /= Base ; } return isThere ; } static void count ( int n , int k , List < List < int > > v ) { int [ ] pref = new int [ 1000005 ] ; for ( int i = 1 ; i < 1e6 + 5 ; i ++ ) { bool present = contains ( i , k , 10 ) || contains ( i , k , 8 ) ; if ( present ) { pref [ i ] += pref [ i - 1 ] + 1 ; } } Console . Write ( ( v [ 0 ] [ 1 ] - v [ 0 ] [ 0 ] + 1 - ( pref [ v [ 0 ] [ 1 ] ] - pref [ v [ 0 ] [ 0 ] - 1 ] ) ) + " ▁ " ) ; Console . Write ( ( v [ 1 ] [ 1 ] - v [ 1 ] [ 0 ] - ( pref [ v [ 1 ] [ 1 ] ] - pref [ v [ 1 ] [ 0 ] - 1 ] ) ) + " ▁ " ) ; } static void Main ( ) { int K = 7 ; List < List < int > > Q = new List < List < int > > ( ) ; Q . Add ( new List < int > ( ) ) ; Q [ 0 ] . Add ( 2 ) ; Q [ 0 ] . Add ( 5 ) ; Q . Add ( new List < int > ( ) ) ; Q [ 1 ] . Add ( 1 ) ; Q [ 1 ] . Add ( 15 ) ; int N = Q . Count ; count ( N , K , Q ) ; } }
Count tiles of dimensions 2 * 1 that can be placed in an M * N rectangular board that satisfies the given conditions | C # Program to implement the above approach ; Function to tiles of size 2 x 1 find the number of tiles that can be placed as per the given conditions ; Number of tiles required ; Driver Code
using System ; public class GFG { static int numberOfTiles ( int n , int m ) { if ( n % 2 == 1 ) { return - 1 ; } return ( m * n ) / 2 ; } static public void Main ( ) { int n = 2 , m = 4 ; Console . WriteLine ( numberOfTiles ( n , m ) ) ; } }
Check if an array can be converted to another given array by swapping pairs of unequal elements | C # program for the above approach ; Function to check if arr1 [ ] can be converted to arr2 [ ] by swapping pair ( i , j ) such that i < j and arr [ i ] is 1 and arr [ j ] is 0 ; Stores the differences of prefix sum of arr1 and arr2 ; Stores the count of 1 and zero of arr1 ; Stores the count of 1 and zero of arr2 ; Iterate in the range [ 0 , N - 1 ] ; If arr1 [ i ] is 1 , then increment arr1_one by one ; Otherwise increment arr1_zero by one ; If arr2 [ i ] is 1 , then increment arr2_one by one ; Otherwise increment arr2_zero by one ; Check if number of 1 s and 0 s of arr1 is equal to number of 1 s and 0 s of arr2 respectievly ; Iterate over the range [ 0 , N - 1 ] ; Increment count by differences arr1 [ i ] and arr2 [ i ] ; Check if number of 1 's in  arr2 are more than arr1 and  then print "No" ; Finally , print " Yes " ; Driver Code ; Given input arrays ; Size of the array ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void canMakeEqual ( int [ ] arr1 , int [ ] arr2 , int N ) { int count = 0 ; int arr1_one = 0 , arr1_zero = 0 ; int arr2_one = 0 , arr2_zero = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr1 [ i ] == 1 ) { arr1_one ++ ; } else if ( arr1 [ i ] == 0 ) { arr1_zero ++ ; } if ( arr2 [ i ] == 1 ) { arr2_one ++ ; } else if ( arr2 [ i ] == 0 ) { arr2_zero ++ ; } } if ( arr1_one != arr2_one arr1_zero != arr2_zero ) { Console . WriteLine ( " No " ) ; return ; } for ( int i = 0 ; i < N ; i ++ ) { count = count + ( arr1 [ i ] - arr2 [ i ] ) ; if ( count < 0 ) { Console . WriteLine ( " No " ) ; return ; } } Console . WriteLine ( " Yes " ) ; } public static void Main ( ) { int [ ] arr1 = { 0 , 1 , 1 , 0 } ; int [ ] arr2 = { 0 , 0 , 1 , 1 } ; int N = arr1 . Length ; canMakeEqual ( arr1 , arr2 , N ) ; } }
Check for each subarray whether it consists of all natural numbers up to its length or not | C # program to implement the above approach ; Function to check if a subarray of size i exists that contain all the numbers in the range [ 1 , i ] ; Store the position of each element of arr [ ] ; Traverse the array ; Insert the position of arr [ i ] ; Store position of each element from the range [ 1 , N ] ; Iterate over the range [ 1 , N ] ; Insert the index of i into st ; Find the smallest element of st ; Find the largest element of st ; If distance between the largest and smallest element of arr [ ] till i - th index is equal to i ; Driver code
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static void checksubarrayExist1_N ( int [ ] arr , int N ) { Dictionary < int , int > pos = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { pos [ arr [ i ] ] = i ; } HashSet < int > st = new HashSet < int > ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { st . Add ( pos [ i ] ) ; int Min = st . Min ( ) ; int Max = st . Max ( ) ; if ( Max - Min + 1 == i ) { Console . Write ( " True ▁ " ) ; } else { Console . Write ( " False ▁ " ) ; } } } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 4 , 3 , 2 } ; int N = arr . Length ; checksubarrayExist1_N ( arr , N ) ; } }
Check if a pair of integers A and B can coincide by shifting them by distances arr [ ( A % N + N ) % N ] and arr [ ( B % N + N ) % N ] | C # program for the above approach ; Function to check if two integers coincide at a point ; Store the final position of integers A and B ; Iterate over the range [ 0 , n ] ; Store the final position of the integer i ; If temp is present in the Map ; Print Yes and return ; Mark its final position as visited ; If every integer stored in the Map is unique ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void checkSamePosition ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int temp = ( ( i + arr [ i ] ) % n + n ) % n ; if ( mp . ContainsKey ( temp ) ) { Console . Write ( " Yes " ) ; return ; } mp [ temp ] = 1 ; } Console . Write ( " No " ) ; } static void Main ( ) { int [ ] arr = { 5 , 4 , 3 } ; int N = arr . Length ; checkSamePosition ( arr , N ) ; } }
Sum of Fibonacci Numbers | Set 2 | C # program for the above approach ; Function to find the sum of first N + 1 fibonacci numbers ; Apply the formula ; Print the result ; Driver Code
using System ; class GFG { public static void sumFib ( int N ) { long num = ( long ) Math . Round ( Math . Pow ( ( Math . Sqrt ( 5 ) + 1 ) / 2.0 , N + 2 ) / Math . Sqrt ( 5 ) ) ; Console . WriteLine ( num - 1 ) ; } static public void Main ( ) { int N = 3 ; sumFib ( N ) ; } }
Sum of Fibonacci Numbers | Set 2 | C # program for the above approach ; Function to find the sum of first N + 1 fibonacci numbers ; Apply the formula ; Print the result ; Driver Code ; Function Call
using System ; public class GFG { public static void sumFib ( int N ) { double num = ( 1 - Math . Sqrt ( 5 ) ) / 2 ; double val = Math . Round ( Math . Abs ( 1 / ( Math . Pow ( num , N + 2 ) + Math . Pow ( num , N + 1 ) + Math . Pow ( num , N ) + Math . Pow ( num , N - 1 ) ) ) - 1 ) ; Console . WriteLine ( val ) ; } public static void Main ( String [ ] args ) { int N = 3 ; sumFib ( N ) ; } }
Make all array elements equal to K by repeatedly incrementing subsequences | C # program for the above approach ; Function to find the minimum number of operations required to make all elements equal to k ; Initialize a hashmap ; Store frequency of array elements ; Store the minimum number of operations required ; Iterate until all array elements becomes equal to K ; Iterate through range [ 1 , k - 1 ] since only one element can be increased from each group ; Check if the current number has frequency > 0 , i . e . , it is a part of a group ; If true , decrease the frequency of current group of element by 1 ; Increase the frequency of the next group of elements by 1 ; If the next element is not the part of any group , then skip it ; Increment count of operations ; Print the count of operations ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; public class GFG { static void minOperations ( int [ ] arr , int n , int k ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } } int ans = 0 ; while ( mp . ContainsKey ( k ) == false mp [ k ] < n ) { for ( int i = 1 ; i <= k - 1 ; i ++ ) { if ( mp . ContainsKey ( i ) && mp [ i ] > 0 ) { mp [ i ] = mp [ i ] - 1 ; if ( mp . ContainsKey ( i + 1 ) ) mp [ i + 1 ] = mp [ i + 1 ] + 1 ; else mp . Add ( i + 1 , 1 ) ; if ( mp . ContainsKey ( i + 1 ) && mp [ i + 1 ] == 1 ) { i ++ ; } } } ans ++ ; } Console . Write ( ans ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 3 , 4 } ; int K = 5 ; int N = arr . Length ; minOperations ( arr , N , K ) ; } }
Minimum number of pigs required to find the poisonous bucket | C # program for the above approach ; Function to find the minimum number of pigs required to find the poisonous bucket ; Print the result ; Driver Code
using System ; class GFG { static void poorPigs ( int buckets , int minutesToDie , int minutesToTest ) { Console . WriteLine ( ( int ) Math . Ceiling ( Math . Log ( buckets ) / Math . Log ( ( minutesToTest / minutesToDie ) + 1 ) ) ) ; } static public void Main ( ) { int N = 1000 , M = 15 , P = 60 ; poorPigs ( N , M , P ) ; } }
Maximum product of the remaining pair after repeatedly replacing pairs of adjacent array elements with their sum | C # program for the above approach ; Function to find the maximum product possible after repeatedly replacing pairs of adjacent array elements with their sum ; Store the maximum product ; Store the prefix sum ; Store the total sum of array ; Traverse the array to find the total sum ; Iterate in the range [ 0 , N - 2 ] ; Add arr [ i ] to prefix_sum ; Store the value of prefix_sum ; Store the value of ( total sum - prefix sum ) ; Update the maximum product ; Print the answer ; Driver code
using System ; class GFG { static void maxProduct ( int [ ] arr , int N ) { int max_product = Int32 . MinValue ; int prefix_sum = 0 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } for ( int i = 0 ; i < N - 1 ; i ++ ) { prefix_sum += arr [ i ] ; int X = prefix_sum ; int Y = sum - prefix_sum ; max_product = Math . Max ( max_product , X * Y ) ; } Console . WriteLine ( max_product ) ; } static void Main ( ) { int [ ] arr = { 2 , 3 , 5 , 6 , 7 } ; int N = arr . Length ; maxProduct ( arr , N ) ; } }
Find array elements with rightmost set bit at the position of the rightmost set bit in K | C # program for the above approach ; Function to find the mask for finding rightmost set bit in K ; Function to find all array elements with rightmost set bit same as that in K ; Stores mask of K ; Store position of rightmost set bit ; Traverse the array ; Check if rightmost set bit of current array element is same as position of rightmost set bit in K ; Driver Code ; Input ; Function call to find the elements having same rightmost set bit as of K
using System ; public class GFG { static int findMask ( int K ) { int mask = 1 ; while ( ( K & mask ) == 0 ) { mask = mask << 1 ; } return mask ; } public static void sameRightSetBitPos ( int [ ] arr , int N , int K ) { int mask = findMask ( K ) ; int pos = ( K & mask ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & mask ) == pos ) Console . Write ( arr [ i ] + " ▁ " ) ; } } static public void Main ( ) { int [ ] arr = { 3 , 4 , 6 , 7 , 9 , 12 , 15 } ; int N = arr . Length ; int K = 7 ; sameRightSetBitPos ( arr , N , K ) ; } }
Maximum sum of K | C # Program to implement the above approach ; Function to print the sum of subarray of length K having maximum distinct prime factors ; If K exceeds N ; Stores the count of distinct primes ; True , if index ' i ' is a prime ; Initialize the count of factors to 0 and set all indices as prime ; If i is prime ; Number is prime ; Count of factors of a prime number is 1 ; Increment CountDistinct as the factors of i ; Mark its multiple non - prime ; Compute sum of first K - length subarray ; Compute sums of remaining windows by removing first element of the previous window and adding last element of the current window ; Print the maximum sum ; Driver Code ; Given array ; Given size of subarray ; Size of the array
using System ; public class GFG { static int MAX = 100001 ; static void maxSum ( int [ ] arr , int N , int K ) { if ( N < K ) { Console . WriteLine ( " Invalid " ) ; return ; } int [ ] CountDistinct = new int [ MAX + 1 ] ; bool [ ] prime = new bool [ MAX + 1 ] ; for ( int i = 0 ; i <= MAX ; i ++ ) { CountDistinct [ i ] = 0 ; prime [ i ] = true ; } for ( int i = 2 ; i <= MAX ; i ++ ) { if ( prime [ i ] == true ) { CountDistinct [ i ] = 1 ; for ( int j = i * 2 ; j <= MAX ; j += i ) { CountDistinct [ j ] ++ ; prime [ j ] = false ; } } } int Maxarr_sum = 0 , DistPrimeSum = 0 ; for ( int i = 0 ; i < K ; i ++ ) { Maxarr_sum += arr [ i ] ; DistPrimeSum += CountDistinct [ arr [ i ] ] ; } int curr_sum = DistPrimeSum ; int curr_arrSum = Maxarr_sum ; for ( int i = K ; i < N ; i ++ ) { curr_sum += CountDistinct [ arr [ i ] ] - CountDistinct [ arr [ i - K ] ] ; curr_arrSum += arr [ i ] - arr [ i - K ] ; if ( curr_sum > DistPrimeSum ) { DistPrimeSum = curr_sum ; Maxarr_sum = curr_arrSum ; } else if ( curr_sum == DistPrimeSum ) { Maxarr_sum = Math . Max ( curr_arrSum , Maxarr_sum ) ; } } Console . WriteLine ( Maxarr_sum ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 2 , 10 , 3 } ; int K = 3 ; int N = arr . Length ; maxSum ( arr , N , K ) ; } }
Count Pronic numbers from a given range | C # program for the above approach ; Function to check if x is a Pronic Number or not ; Check for Pronic Number by multiplying consecutive numbers ; Function to count pronic numbers in the range [ A , B ] ; Initialise count ; Iterate from A to B ; If i is pronic ; Increment count ; Print count ; Driver Code ; Function call to count pronic numbers in the range [ A , B ]
using System ; public class GFG { static bool checkPronic ( int x ) { for ( int i = 0 ; i <= ( int ) ( Math . Sqrt ( x ) ) ; i ++ ) { if ( ( x == i * ( i + 1 ) ) != false ) { return true ; } } return false ; } static void countPronic ( int A , int B ) { int count = 0 ; for ( int i = A ; i <= B ; i ++ ) { if ( checkPronic ( i ) != false ) { count ++ ; } } Console . Write ( count ) ; } public static void Main ( String [ ] args ) { int A = 3 , B = 20 ; countPronic ( A , B ) ; } }