codeparrot/xlcost-text-to-code huggingface dataset:
Product of values of all possible non | C # implementation of the approach ; Function to find product of all elements in all subsets ; Driver Code
using System ; class GFG { static int product ( int [ ] a , int n ) { int ans = 1 ; int val = ( int ) Math . Pow ( 2 , n - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { ans *= ( int ) Math . Pow ( a [ i ] , val ) ; } return ans ; } public static void Main ( ) { int n = 2 ; int [ ] a = { 3 , 7 } ; Console . WriteLine ( product ( a , n ) ) ; } }
Program to find Nth odd Fibonacci Number | C # program for Nth odd fibonacci number ; Function to find nth odd fibonacci number ; Driver Code
using System ; class GFG { static int oddFib ( int n ) { n = ( 3 * n + 1 ) / 2 ; int a = - 1 , b = 1 , c = 0 , i ; for ( i = 1 ; i <= n ; i ++ ) { c = a + b ; a = b ; b = c ; } return c ; } public static void Main ( String [ ] args ) { int n = 4 ; Console . WriteLine ( oddFib ( n ) ) ; } }
Find a pair ( n , r ) in an integer array such that value of nPr is maximum | C # implementation of the approach ; Function to print the pair ( n , r ) such that nPr is maximum possible ; There should be atleast 2 elements ; Findex the largest 2 elements ; Driver code
using System ; class GFG { static void findPair ( int [ ] arr , int n ) { if ( n < 2 ) { Console . Write ( " - 1" ) ; return ; } int i , first , second ; first = second = - 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) { second = arr [ i ] ; } } Console . WriteLine ( " n ▁ = ▁ " + first + " ▁ and ▁ r ▁ = ▁ " + second ) ; } public static void Main ( ) { int [ ] arr = { 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 } ; int n = arr . Length ; findPair ( arr , n ) ; } }
Sum of values of all possible non | C # implementation of the approach ; Function to return the required sum ; Find the sum of the array elements ; Every element appears 2 ^ ( n - 1 ) times ; Driver code
using System ; class GFG { static int sum ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } sum = sum * ( int ) Math . Pow ( 2 , n - 1 ) ; return sum ; } public static void Main ( ) { int [ ] arr = { 2 , 1 , 5 , 6 } ; int n = arr . Length ; Console . WriteLine ( sum ( arr , n ) ) ; } }
Printing the Triangle Pattern using last term N | C # code for printing the Triangle Pattern using last term N ; Function to demonstrate printing pattern ; number of spaces ; character to be printed ; outer loop to handle number of rows n in this case ; inner loop to handle number spaces values changing acc . to requirement ; decrementing k after each loop ; inner loop to handle number of columns values changing acc . to outer loop ; printing stars ; ending line after each row ; Function to find the max height or the number of lines in the triangle pattern ; Driver Code
using System ; class GFG { static void triangle ( int n ) { int k = 2 * n - 2 ; int ch = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < k ; j ++ ) Console . Write ( " ▁ " ) ; k = k - 1 ; for ( int j = 0 ; j <= i ; j ++ ) { Console . Write ( ch ++ + " ▁ " ) ; } Console . WriteLine ( ) ; } } static int maxHeight ( int n ) { return ( ( ( int ) Math . Sqrt ( 1 + 8.0 * n ) ) - 1 ) / 2 ; } public static void Main ( String [ ] args ) { int N = 9 ; triangle ( maxHeight ( N ) ) ; } }
Number of ways in which N can be represented as the sum of two positive integers | C # implementation of the approach ; Function to return the number of distinct ways to represent n as the sum of two integers ; Driver code
using System ; class GFG { static int ways ( int n ) { return n / 2 ; } public static void Main ( ) { int n = 2 ; Console . WriteLine ( ways ( n ) ) ; } }
Number of ways to erase exactly one element in the Binary Array to make XOR zero | C # program to find the number of ways to erase exactly one element from this array to make XOR zero ; Function to find the number of ways ; Calculate the number of 1 ' s ▁ and ▁ 0' s ; Considering the 4 cases ; Driver code
using System ; class GFG { static int no_of_ways ( int [ ] a , int n ) { int count_0 = 0 , count_1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) count_0 ++ ; else count_1 ++ ; } if ( count_1 % 2 == 0 ) return count_0 ; else return count_1 ; } public static void Main ( ) { int n = 4 ; int [ ] a1 = { 1 , 1 , 0 , 0 } ; Console . WriteLine ( no_of_ways ( a1 , n ) ) ; n = 5 ; int [ ] a2 = { 1 , 1 , 1 , 0 , 0 } ; Console . WriteLine ( no_of_ways ( a2 , n ) ) ; n = 5 ; int [ ] a3 = { 1 , 1 , 0 , 0 , 0 } ; Console . WriteLine ( no_of_ways ( a3 , n ) ) ; n = 6 ; int [ ] a4 = { 1 , 1 , 1 , 0 , 0 , 0 } ; Console . WriteLine ( no_of_ways ( a4 , n ) ) ; } }
Minimum absolute difference between N and any power of 2 | C # implementation of the approach ; Function to return the highest power of 2 less than or equal to n ; Function to return the smallest power of 2 greater than or equal to n ; Function that returns the minimum absolute difference between n and any power of 2 ; Driver code
using System ; class GFG { static int prevPowerof2 ( int n ) { int p = ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) ; return ( int ) Math . Pow ( 2 , p ) ; } static int nextPowerOf2 ( int n ) { int p = 1 ; if ( ( n == 0 ) && ! ( ( n & ( n - 1 ) ) == 0 ) ) return n ; while ( p < n ) p <<= 1 ; return p ; } static int minDiff ( int n ) { int low = prevPowerof2 ( n ) ; int high = nextPowerOf2 ( n ) ; return Math . Min ( n - low , high - n ) ; } public static void Main ( String [ ] args ) { int n = 6 ; Console . WriteLine ( minDiff ( n ) ) ; } }
Maximum possible number with the given operation | C # implementation of the approach ; Function to return the maximum possible integer that can be obtained from the given integer after performing the given operations ; For every digit ; Digits greater than or equal to 5 need not to be changed as changing them will lead to a smaller number ; The resulting integer cannot have leading zero ; Driver code
using System ; class GFG { static String maxInt ( char [ ] str ) { for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] < '5' ) { str [ i ] = ( char ) ( ( '9' - str [ i ] ) + '0' ) ; } } if ( str [ 0 ] == '0' ) str [ 0 ] = '9' ; String str2 = new String ( str ) ; return str2 ; } public static void Main ( String [ ] args ) { String str = "42" ; Console . WriteLine ( maxInt ( str . ToCharArray ( ) ) ) ; } }
Find the ratio of number of elements in two Arrays from their individual and combined average | C # program to Find the Ratio of number of Elements in two Arrays from their individual and combined Average ; function to find the ratio of number of array elements ; calculating GCD of them ; make neumarator and denominator coprime ; Driver Code
using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void FindRatio ( int a , int b , int c ) { int up = Math . Abs ( b - c ) ; int down = Math . Abs ( c - a ) ; int g = gcd ( up , down ) ; up /= g ; down /= g ; Console . WriteLine ( up + " : " + down ) ; } public static void Main ( String [ ] args ) { int a = 4 , b = 10 , c = 6 ; FindRatio ( a , b , c ) ; } }
Maximum distance between two 1 's in Binary representation of N | C # program to find the Maximum distance between two 1 's in Binary representation of N ; Compute the binary representation ; if N is a power of 2 then return - 1 ; else find the distance between the first position of 1 and last position of 1 ; Driver code
using System ; class GFG { static int longest_gap ( int N ) { int distance = 0 , count = 0 , first_1 = - 1 , last_1 = - 1 ; while ( N != 0 ) { count ++ ; int r = N & 1 ; if ( r == 1 ) { first_1 = first_1 == - 1 ? count : first_1 ; last_1 = count ; } N = N / 2 ; } if ( last_1 <= first_1 ) { return - 1 ; } else { distance = ( last_1 - first_1 - 1 ) ; return distance ; } } public static void Main ( String [ ] args ) { int N = 131 ; Console . WriteLine ( longest_gap ( N ) ) ; N = 8 ; Console . WriteLine ( longest_gap ( N ) ) ; N = 17 ; Console . WriteLine ( longest_gap ( N ) ) ; N = 33 ; Console . WriteLine ( longest_gap ( N ) ) ; } }
Check if it is possible to move from ( 0 , 0 ) to ( X , Y ) in exactly K steps | C # implementation of the approach ; Function that returns true if it is possible to move from ( 0 , 0 ) to ( x , y ) in exactly k moves ; Minimum moves required ; If possible ; Driver code
using System ; class GFG { static bool isPossible ( int x , int y , int k ) { int minMoves = Math . Abs ( x ) + Math . Abs ( y ) ; if ( k >= minMoves && ( k - minMoves ) % 2 == 0 ) return true ; return false ; } public static void Main ( ) { int x = 5 , y = 8 , k = 20 ; if ( isPossible ( x , y , k ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Check whether N is a Factorion or not | C # implementation of the above approach ; Function that returns true if n is a Factorion ; fact [ i ] will store i ! ; A copy of the given integer ; To store the sum of factorials of the digits of n ; Get the last digit ; Add the factorial of the current digit to the sum ; Remove the last digit ; Driver code
using System ; class GFG { static int MAX = 10 ; static bool isFactorion ( int n ) { int [ ] fact = new int [ MAX ] ; fact [ 0 ] = 1 ; for ( int i = 1 ; i < MAX ; i ++ ) fact [ i ] = i * fact [ i - 1 ] ; int org = n ; int sum = 0 ; while ( n > 0 ) { int d = n % 10 ; sum += fact [ d ] ; n /= 10 ; } if ( sum == org ) return true ; return false ; } public static void Main ( String [ ] args ) { int n = 40585 ; if ( isFactorion ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find the possible permutation of the bits of N | C # implementation of the above approach ; Function that returns true if it is possible to arrange the bits of n in alternate fashion ; To store the count of 1 s in the binary representation of n ; If the number set bits and the number of unset bits is equal ; Driver code
using System ; class GFG { static int TOTAL_BITS = 32 ; static int CountBits ( int value ) { int count = 0 ; while ( value != 0 ) { count ++ ; value &= value - 1 ; } return count ; } static bool isPossible ( int n ) { int cnt = CountBits ( n ) ; if ( cnt == TOTAL_BITS / 2 ) return true ; return false ; } public static void Main ( String [ ] arr ) { int n = 524280 ; if ( isPossible ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Check if two Integer are anagrams of each other | C # implementation of the approach ; Function to update the frequency array such that freq [ i ] stores the frequency of digit i in n ; While there are digits left to process ; Update the frequency of the current digit ; Remove the last digit ; Function that returns true if a and b are anagarams of each other ; To store the frequencies of the digits in a and b ; Update the frequency of the digits in a ; Update the frequency of the digits in b ; Match the frequencies of the common digits ; If frequency differs for any digit then the numbers are not anagrams of each other ; Driver code
using System ; class GFG { static int TEN = 10 ; static void updateFreq ( int n , int [ ] freq ) { while ( n > 0 ) { int digit = n % TEN ; freq [ digit ] ++ ; n /= TEN ; } } static bool areAnagrams ( int a , int b ) { int [ ] freqA = new int [ TEN ] ; int [ ] freqB = new int [ TEN ] ; ; updateFreq ( a , freqA ) ; updateFreq ( b , freqB ) ; for ( int i = 0 ; i < TEN ; i ++ ) { if ( freqA [ i ] != freqB [ i ] ) return false ; } return true ; } public static void Main ( ) { int a = 204 , b = 240 ; if ( areAnagrams ( a , b ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find the permutation of first N natural numbers such that sum of i % Pi is maximum possible | C # implementation of the approach ; Function to find the permutation of the first N natural numbers such that the sum of ( i % Pi ) is maximum possible and return the maximum sum ; Driver code ; Function call
using System ; class GFG { static int Max_Sum ( int n ) { return ( n * ( n - 1 ) ) / 2 ; } public static void Main ( String [ ] args ) { int n = 8 ; Console . WriteLine ( Max_Sum ( n ) ) ; } }
Check if the number formed by the last digits of N numbers is divisible by 10 or not | C # implementation of the approach ; Function that returns true if the number formed by the last digits of all the elements is divisible by 10 ; Last digit of the last element ; Number formed will be divisible by 10 ; Driver code
using System ; class GFG { static bool isDivisible ( int [ ] arr , int n ) { int lastDigit = arr [ n - 1 ] % 10 ; if ( lastDigit == 0 ) return true ; return false ; } static public void Main ( String [ ] arg ) { int [ ] arr = { 12 , 65 , 46 , 37 , 99 } ; int n = arr . Length ; if ( isDivisible ( arr , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Count of Multiples of A , B or C less than or equal to N | C # implementation for above approach ; Function to return the gcd of a and b ; Function to return the count of integers from the range [ 1 , num ] which are divisible by either a , b or c ; Calculate the number of terms divisible by a , b and c then remove the terms which are divisible by both ( a , b ) or ( b , c ) or ( c , a ) and then add the numbers which are divisible by a , b and c ; Driver code
using System ; class GFG { static long gcd ( long a , long b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static long divTermCount ( long a , long b , long c , long num ) { return ( ( num / a ) + ( num / b ) + ( num / c ) - ( num / ( ( a * b ) / gcd ( a , b ) ) ) - ( num / ( ( c * b ) / gcd ( c , b ) ) ) - ( num / ( ( a * c ) / gcd ( a , c ) ) ) + ( num / ( ( a * b * c ) / gcd ( gcd ( a , b ) , c ) ) ) ) ; } static public void Main ( String [ ] arr ) { long a = 7 , b = 3 , c = 5 , n = 100 ; Console . WriteLine ( divTermCount ( a , b , c , n ) ) ; } }
Array containing power of 2 whose XOR and Sum of elements equals X | C # implementation of the approach ; Function to return the required array ; Store the power of 2 ; while n is greater than 0 ; if there is 1 in binary representation ; Divide n by 2 Multiply p2 by 2 ; Driver code ; Get the answer ; Printing the array
using System ; using System . Collections . Generic ; class GFG { static List < long > getArray ( int n ) { List < long > ans = new List < long > ( ) ; long p2 = 1 ; while ( n > 0 ) { if ( n % 2 == 1 ) ans . Add ( p2 ) ; n >>= 1 ; p2 *= 2 ; } return ans ; } public static void Main ( String [ ] args ) { int n = 15 ; List < long > ans = getArray ( n ) ; foreach ( long i in ans ) Console . Write ( i + " ▁ " ) ; } }
Find out the correct position of the ball after shuffling | C # implementation of the above approach ; Function to generate the index of the glass containing the ball ; Change the index ; Change the index ; Print the index ; Driver Code ; Storing all the shuffle operation
using System ; class GFG { static int M = 3 ; static int N = 2 ; static void getIndex ( int n , int [ , ] shuffle ) { for ( int i = 0 ; i < 3 ; i ++ ) { if ( shuffle [ i , 0 ] == n ) n = shuffle [ i , 1 ] ; else if ( shuffle [ i , 1 ] == n ) n = shuffle [ i , 0 ] ; } Console . WriteLine ( n ) ; } public static void Main ( String [ ] args ) { int n = 3 ; int [ , ] shuffle = { { 3 , 1 } , { 2 , 1 } , { 1 , 2 } } ; getIndex ( n , shuffle ) ; } }
Count the number of subsequences of length k having equal LCM and HCF | C # implementation for above approach ; Returns factorial of n ; Returns nCr for the given values of r and n ; Map to store the frequencies of each elements ; Loop to store the frequencies of elements in the map ; Using nCR formula to calculate the number of subsequences of a given length ; Driver Code ; Function calling
using System ; using System . Collections . Generic ; public class GFG { static long fact ( int n ) { long res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static long nCr ( int n , int r ) { return fact ( n ) / ( 1 * fact ( r ) * fact ( n - r ) ) ; } static long number_of_subsequences ( int [ ] arr , int k , int n ) { long s = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { var val = mp [ arr [ i ] ] ; mp . Remove ( arr [ i ] ) ; mp . Add ( arr [ i ] , val + 1 ) ; } else { mp . Add ( arr [ i ] , 1 ) ; } } foreach ( KeyValuePair < int , int > j in mp ) { s = s + 1 * nCr ( j . Value , k ) ; } return s ; } static public void Main ( String [ ] arg ) { int [ ] arr = { 1 , 1 , 1 , 1 , 2 , 2 , 2 } ; int k = 2 ; int n = arr . Length ; Console . Write ( number_of_subsequences ( arr , k , n ) ) ; } }
Find the sum of all possible pairs in an array of N elements | C # implementation of the approach ; Function to return the sum of the elements of all possible pairs from the array ; To store the required sum ; For every element of the array ; It appears ( 2 * n ) times ; Driver code
using System ; class GFG { static int sumPairs ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ( arr [ i ] * ( 2 * n ) ) ; } return sum ; } static public void Main ( String [ ] arg ) { int [ ] arr = { 1 , 2 , 3 } ; int n = arr . Length ; Console . WriteLine ( sumPairs ( arr , n ) ) ; } }
Minimum sum obtained from groups of four elements from the given array | C # implementation of the approach ; Function to return the minimum required sum ; To store the required sum ; Sort the array in descending order ; The indices which give 0 or 1 as the remainder when divided by 4 will be the maximum two elements of the group ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int minSum ( int [ ] arr , int n ) { int sum = 0 ; Array . Sort ( arr ) ; Array . Reverse ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 4 < 2 ) sum = sum + arr [ i ] ; } return sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 10 , 2 , 2 , 2 , 1 } ; int n = arr . Length ; Console . WriteLine ( minSum ( arr , n ) ) ; } }
Count of triples ( A , B , C ) where A * C is greater than B * B | C # implementation of above approach ; function to return the count of the valid triplets ; Driver Code ; function calling
using System ; class GFG { static long countTriplets ( int A , int B , int C ) { long ans = 0 ; for ( int i = 1 ; i <= A ; i ++ ) { for ( int j = 1 ; j <= B ; j ++ ) { for ( int k = 1 ; k <= C ; k ++ ) { if ( i * k > j * j ) ans ++ ; } } } return ans ; } public static void Main ( String [ ] args ) { int A = 3 , B = 2 , C = 2 ; Console . WriteLine ( countTriplets ( A , B , C ) ) ; } }
Count of triples ( A , B , C ) where A * C is greater than B * B | C # implementation of the approach ; Counts the number of triplets for a given value of b ; Count all triples in which a = i ; Smallest value j such that i * j > B2 ; Count all ( i , B2 , x ) such that x >= j ; count all ( x , B2 , y ) such that x >= j this counts all such triples in which a >= j ; As all triples with a >= j have been counted reduce A to j - 1. ; Counts the number of triples that satisfy the given constraints ; GetCount of triples in which b = i ; Driver Code ; Function calling
using System ; using System . Collections . Generic ; class GFG { static long getCount ( int A , int B2 , int C ) { long count = 0 ; for ( int i = 1 ; i <= A ; i ++ ) { long j = ( B2 / i ) + 1 ; if ( C >= j ) count = ( count + C - j + 1 ) ; if ( A >= j && C >= i ) count = ( count + ( C - i + 1 ) * ( A - j + 1 ) ) ; if ( A >= j ) A = ( int ) ( j - 1 ) ; } return count ; } static long countTriplets ( int A , int B , int C ) { long ans = 0 ; for ( int i = 1 ; i <= B ; i ++ ) { ans = ( ans + getCount ( A , i * i , C ) ) ; } return ans ; } public static void Main ( String [ ] args ) { int A , B , C ; A = 3 ; B = 2 ; C = 2 ; Console . WriteLine ( countTriplets ( A , B , C ) ) ; } }
Summation of floor of harmonic progression | C # implementation of the approach ; Function to return the summation of the given harmonic series ; To store the summation ; Floor of sqrt ( n ) ; Summation of floor ( n / i ) ; From the formula ; Driver code
using System ; class GFG { static double getSum ( int n ) { double sum = 0 ; int k = ( int ) Math . Sqrt ( n ) ; for ( int i = 1 ; i <= k ; i ++ ) { sum += Math . Floor ( ( double ) n / i ) ; } sum *= 2 ; sum -= Math . Pow ( k , 2 ) ; return sum ; } public static void Main ( String [ ] args ) { int n = 5 ; Console . WriteLine ( getSum ( n ) ) ; } }
Count of distinct remainders when N is divided by all the numbers from the range [ 1 , N ] | C # implementation of the above approach ; Function to return the count of distinct remainders that can be obtained when n is divided by every element from the range [ 1 , n ] ; If n is even ; If n is odd ; Driver code
using System ; class GFG { static int distinctRemainders ( int n ) { if ( n % 2 == 0 ) return ( n / 2 ) ; return ( 1 + ( n / 2 ) ) ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( distinctRemainders ( n ) ) ; } }
Count total unset bits in all the numbers from 1 to N | C # implementation of the approach ; Function to return the count of unset bits in the binary representation of all the numbers from 1 to n ; To store the count of unset bits ; For every integer from the range [ 1 , n ] ; A copy of the current integer ; Count of unset bits in the current integer ; If current bit is unset ; Driver code
using System ; class GFG { static int countUnsetBits ( int n ) { int cnt = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int temp = i ; while ( temp > 0 ) { if ( temp % 2 == 0 ) cnt = cnt + 1 ; temp = temp / 2 ; } } return cnt ; } public static void Main ( ) { int n = 5 ; Console . Write ( countUnsetBits ( n ) ) ; } }
Find if a degree sequence can form a simple graph | Havel | C # implementation of the approach ; Function that returns true if a simple graph exists ; Keep performing the operations until one of the stopping condition is met ; Sort the list in non - decreasing order ; Check if all the elements are equal to 0 ; Store the first element in a variable and delete it from the list ; Check if enough elements are present in the list ; Subtract first element from next v elements ; Check if negative element is encountered after subtraction ; Driver Code
using System ; using System . Collections ; class GFG { static bool graphExists ( ArrayList a , int n ) { while ( true ) { a . Sort ( ) ; a . Reverse ( ) ; if ( ( int ) a [ 0 ] == 0 ) return true ; int v = ( int ) a [ 0 ] ; a . Remove ( a [ 0 ] ) ; if ( v > a . Count ) return false ; for ( int i = 0 ; i < v ; i ++ ) { a [ i ] = ( int ) a [ i ] - 1 ; if ( ( int ) a [ i ] < 0 ) return false ; } } } public static void Main ( string [ ] args ) { ArrayList a = new ArrayList ( ) { 3 , 3 , 3 , 3 } ; int n = a . Count ; if ( graphExists ( a , n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " NO " ) ; } } }
Number of sub arrays with negative product | C # implementation of the approach ; Function to return the count of subarrays with negative product ; Replace current element with 1 if it is positive else replace it with - 1 instead ; Take product with previous element to form the prefix product ; Count positive and negative elements in the prefix product array ; Return the required count of subarrays ; Driver code
using System ; class GFG { static int negProdSubArr ( int [ ] arr , int n ) { int positive = 1 , negative = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) arr [ i ] = 1 ; else arr [ i ] = - 1 ; if ( i > 0 ) arr [ i ] *= arr [ i - 1 ] ; if ( arr [ i ] == 1 ) positive ++ ; else negative ++ ; } return ( positive * negative ) ; } static public void Main ( ) { int [ ] arr = { 5 , - 4 , - 3 , 2 , - 5 } ; int n = arr . Length ; Console . Write ( negProdSubArr ( arr , n ) ) ; } }
Repeated sum of first N natural numbers | C # implementation of the approach ; Function to return the sum of the first n natural numbers ; Function to return the repeated sum ; Perform the operation exactly k times ; Update n with the sum of first n natural numbers ; Driver code
using System ; class GFG { static int sum ( int n ) { int sum = ( n * ( n + 1 ) ) / 2 ; return sum ; } static int repeatedSum ( int n , int k ) { for ( int i = 0 ; i < k ; i ++ ) { n = sum ( n ) ; } return n ; } public static void Main ( String [ ] args ) { int n = 2 , k = 2 ; Console . WriteLine ( repeatedSum ( n , k ) ) ; } }