codeparrot/xlcost-text-to-code huggingface dataset:
Check if a number can be written as sum of three consecutive integers | C # Code to check if a number can be written as sum of three consecutive integers . ; function to check if a number can be written as sum of three consecutive integers . ; if n is multiple of 3 ; else print " - 1" . ; Driver program to test above function
using System ; class GFG { static void checksum ( int n ) { if ( n % 3 == 0 ) Console . WriteLine ( n / 3 - 1 + " ▁ " + n / 3 + " ▁ " + ( n / 3 + 1 ) ) ; else Console . WriteLine ( " - 1" ) ; } public static void Main ( ) { int n = 6 ; checksum ( n ) ; } }
Sum of all divisors from 1 to n | C # program to find sum of all divisor of number up to ' n ' ; Utility function to find sum of all divisor of number up to ' n ' ; Driver code
using System ; class GFG { static int divisorSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; ++ i ) sum += ( n / i ) * i ; return sum ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( divisorSum ( n ) ) ; n = 5 ; Console . WriteLine ( divisorSum ( n ) ) ; } }
Sum of all divisors from 1 to n |  ; int t1 = i * ( num / i - i + 1 ) ; adding i every time it appears with numbers greater than or equal to itself int t2 = ( ( ( num / i ) * ( num / i + 1 ) ) / 2 ) - ( ( i * ( i + 1 ) ) / 2 ) ; adding numbers that appear with i and are greater than i ; Driver code
using System ; class GFG { public static int sum_all_divisors ( int num ) { int sum = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( num ) ; i ++ ) { sum += t1 + t2 ; } return sum ; } public static void Main ( String [ ] args ) { int n = 1 ; int sum = sum_all_divisors ( n ) ; Console . Write ( sum ) ; } }
N | Java program for finding N - th polite number ; Function to find N - th polite number ; Driver code
using System ; class GFG { static double polite ( double n ) { n += 1 ; double base1 = 2 ; return n + ( Math . Log ( ( n + ( Math . Log ( n ) / Math . Log ( base1 ) ) ) ) ) / Math . Log ( base1 ) ; } public static void Main ( String [ ] args ) { double n = 7 ; Console . Write ( ( int ) polite ( n ) ) ; } }
Find the number of stair steps | C # program to find the number of steps ; Modified Binary search function to solve the equation ; if mid is solution to equation ; if our solution to equation lies between mid and mid - 1 ; if solution to equation is greater than mid ; if solution to equation is less than mid ; Driver function ; call binary search method to solve for limits 1 to T ; Because our pattern starts from 2 , 3 , 4 , 5. . . so , we subtract 1 from ans
using System ; public class GfG { public static int solve ( int low , int high , int T ) { while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( ( mid * ( mid + 1 ) ) == T ) return mid ; if ( mid > 0 && ( mid * ( mid + 1 ) ) > T && ( mid * ( mid - 1 ) ) <= T ) return mid - 1 ; if ( ( mid * ( mid + 1 ) ) > T ) high = mid - 1 ; else low = mid + 1 ; } return - 1 ; } public static void Main ( ) { int T = 15 ; int ans = solve ( 1 , T , 2 * T ) ; if ( ans != - 1 ) ans -- ; Console . WriteLine ( " Number ▁ of ▁ stair ▁ steps ▁ = ▁ " + ans ) ; } }
Check for integer overflow on multiplication | C # program to check for integer overflow on multiplication ; Function to check whether there is overflow in a * b or not . It returns true if there is overflow . ; Check if either of them is zero ; Driver function
using System ; public class GfG { static bool isOverflow ( long a , long b ) { if ( a == 0 b == 0 ) return false ; long result = a * b ; if ( a == result / b ) return false ; else return true ; } public static void Main ( ) { long a = 10000000000 ; long b = - 10000000000 ; if ( isOverflow ( a , b ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Sum of first n odd numbers in O ( 1 ) Complexity | C # program to find sum of first n odd numbers ; Returns the sum of first n odd numbers ; driver function
using System ; class GFG { public static int oddSum ( int n ) { int sum = 0 , curr = 1 ; for ( int i = 0 ; i < n ; i ++ ) { sum += curr ; curr += 2 ; } return sum ; } public static void Main ( ) { int n = 20 ; Console . WriteLine ( " ▁ Sum ▁ of ▁ first ▁ " + n + " ▁ Odd ▁ Numbers ▁ is : ▁ " + oddSum ( n ) ) ; } }
Sum of first n odd numbers in O ( 1 ) Complexity | C # program to find sum of first n odd numbers ; Returns the sum of first n odd numbers ; driver function
using System ; class GFG { public static int oddSum ( int n ) { return ( n * n ) ; } public static void Main ( ) { int n = 20 ; Console . WriteLine ( " ▁ Sum ▁ of ▁ first ▁ " + n + " ▁ Odd ▁ Numbers ▁ is : ▁ " + oddSum ( n ) ) ; } }
K | C # program to count all those numbers in given range whose count of prime factors is k ; Returns the sum of first n odd numbers ; Count prime factors of all numbers till B . ; Print all numbers with k prime factors ; Driver code
using System ; class GFG { static void printKPFNums ( int A , int B , int K ) { bool [ ] prime = new bool [ B + 1 ] ; for ( int i = 0 ; i < B + 1 ; i ++ ) prime [ i ] = true ; int [ ] p_factors = new int [ B + 1 ] ; for ( int i = 0 ; i < B + 1 ; i ++ ) p_factors [ i ] = 0 ; for ( int p = 2 ; p <= B ; p ++ ) if ( p_factors [ p ] == 0 ) for ( int i = p ; i <= B ; i += p ) p_factors [ i ] ++ ; for ( int i = A ; i <= B ; i ++ ) if ( p_factors [ i ] == K ) Console . Write ( i + " ▁ " ) ; } public static void Main ( ) { int A = 14 , B = 18 , K = 2 ; printKPFNums ( A , B , K ) ; } }
Queries for maximum difference between prime numbers in given ranges | C # program to find maximum differences between two prime numbers in given ranges ; Precompute Sieve , Prefix array , Suffix array ; Sieve of Eratosthenes ; Precomputing Prefix array . ; Precompute Suffix array . ; Function to solve each query ; Driven Program
using System ; public class GFG { static readonly int MAX = 100005 ; static void precompute ( int [ ] prefix , int [ ] suffix ) { bool [ ] prime = new bool [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { prime [ i ] = true ; } for ( int i = 2 ; i * i < MAX ; i ++ ) { if ( prime [ i ] ) { for ( int j = i * i ; j < MAX ; j += i ) { prime [ j ] = false ; } } } prefix [ 1 ] = 1 ; suffix [ MAX - 1 ] = ( int ) 1e9 + 7 ; for ( int i = 2 ; i < MAX ; i ++ ) { if ( prime [ i ] ) { prefix [ i ] = i ; } else { prefix [ i ] = prefix [ i - 1 ] ; } } for ( int i = MAX - 2 ; i > 1 ; i -- ) { if ( prime [ i ] ) { suffix [ i ] = i ; } else { suffix [ i ] = suffix [ i + 1 ] ; } } } static int query ( int [ ] prefix , int [ ] suffix , int L , int R ) { if ( prefix [ R ] < L suffix [ L ] > R ) { return 0 ; } else { return prefix [ R ] - suffix [ L ] ; } } public static void Main ( ) { int q = 3 ; int [ ] L = { 2 , 2 , 24 } ; int [ ] R = { 5 , 2 , 28 } ; int [ ] prefix = new int [ MAX ] ; int [ ] suffix = new int [ MAX ] ; precompute ( prefix , suffix ) ; for ( int i = 0 ; i < q ; i ++ ) { Console . WriteLine ( query ( prefix , suffix , L [ i ] , R [ i ] ) ) ; } } }
Sum of the Series 1 + x / 1 + x ^ 2 / 2 + x ^ 3 / 3 + . . + x ^ n / n | C # program to find sum of series 1 + 1 / x + x ^ 2 / 2 + x ^ 3 / 3 + ... . + x ^ n / n ; Function to print the sum of the series ; Driver code
using System ; class GFG { static float sum ( int x , int n ) { double i , total = 1.0 ; for ( i = 1 ; i <= n ; i ++ ) total = total + ( Math . Pow ( x , i ) / i ) ; return ( float ) total ; } public static void Main ( ) { int x = 2 ; int n = 5 ; Console . WriteLine ( sum ( x , n ) ) ; } }
Find if a number is part of AP whose first element and difference are given | C # program to check if x exist or not in the given AP . ; returns yes if exist else no . ; If difference is 0 , then x must be same as a . ; Else difference between x and a must be divisible by d . ; Driver code .
using System ; class GFG { static bool isMember ( int a , int d , int x ) { if ( d == 0 ) return ( x == a ) ; return ( ( x - a ) % d == 0 && ( x - a ) / d >= 0 ) ; } public static void Main ( ) { int a = 1 , x = 7 , d = 3 ; if ( isMember ( a , d , x ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Number of GP ( Geometric Progression ) subsequences of size 3 | C # program to count GP subsequences of size 3. ; To calculate nCr DP approach ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Returns count of G . P . subsequences with length 3 and common ratio r ; Hashing to maintain left and right array elements to the main count ; Stores the answer ; Traverse through the elements ; IF RATIO IS ONE ; Traverse the count in hash ; Calculating nC3 , where ' n ' is the number of times each number is repeated in the input ; Traverse through all elements and find out the number of elements as k1 * k2 ; Keep the count of left and right elements left is a [ i ] / r and right a [ i ] * r ; If the current element is divisible by k , count elements in left hash . ; Decrease the count in right hash ; Number of right elements ; left count of a [ i ] ; Returns answer ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int binomialCoeff ( int n , int k ) { int [ ] C = new int [ k + 1 ] ; C [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . Min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } static long subsequences ( int [ ] a , int n , int r ) { Dictionary < int , int > left = new Dictionary < int , int > ( ) ; Dictionary < int , int > right = new Dictionary < int , int > ( ) ; long ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( right . ContainsKey ( a [ i ] ) ) { right [ a [ i ] ] ++ ; } else { right . Add ( a [ i ] , 1 ) ; } if ( r == 1 ) { foreach ( KeyValuePair < int , int > i in right ) { ans += binomialCoeff ( i . Value , 3 ) ; } return ans ; } for ( int i = 0 ; i < n ; i ++ ) { long c1 = 0 , c2 = 0 ; if ( a [ i ] % r == 0 ) if ( left . ContainsKey ( a [ i ] / r ) ) c1 = left [ a [ i ] / r ] ; if ( right . ContainsKey ( a [ i ] ) ) { right [ a [ i ] ] -- ; } else { right . Add ( a [ i ] , - 1 ) ; } if ( right . ContainsKey ( a [ i ] * r ) ) c2 = right [ a [ i ] * r ] ; ans += c1 * c2 ; if ( left . ContainsKey ( a [ i ] ) ) { left [ a [ i ] ] ++ ; } else { left . Add ( a [ i ] , 1 ) ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 6 , 2 , 3 , 6 , 9 , 18 , 3 , 9 } ; int n = a . GetLength ( 0 ) ; int r = 3 ; Console . Write ( subsequences ( a , n , r ) ) ; } }
Check whether a number can be represented by sum of two squares | A brute force approach based implementation to find if a number can be written as sum of two squares ; function to check if there exist two numbers sum of whose squares is n ; Driver Code
using System ; class GFG { static bool sumSquare ( int n ) { for ( long i = 1 ; i * i <= n ; i ++ ) for ( long j = 1 ; j * j <= n ; j ++ ) if ( i * i + j * j == n ) { Console . Write ( i + " ^ 2 ▁ + ▁ " + j + " ^ 2" ) ; return true ; } return false ; } public static void Main ( String [ ] args ) { int n = 25 ; if ( sumSquare ( n ) ) Console . Write ( " STRNEWLINE Yes " ) ; else Console . Write ( " STRNEWLINE No " ) ; } }
Queries to count minimum flips required to fill a binary submatrix with 0 s only | C # program for the above approach ; Function to compute the matrix prefixCnt [ M ] [ N ] from mat [ M ] [ N ] such that prefixCnt [ i ] [ j ] stores the count of 0 's from (0, 0) to (i, j) ; Initialize prefixCnt [ i ] [ j ] with 1 if mat [ i , j ] is 0 ; Otherwise , assign with 0 ; Calculate prefix sum for each row ; Calculate prefix sum for each column ; Function to compute count of 0 's in submatrix from (pi, pj) to (qi, qj) from prefixCnt[M][N] ; Initialize that count of 0 's  in the sub-matrix within  indices (0, 0) to (qi, qj) ; Subtract count of 0 's within  indices (0, 0) and (pi-1, qj) ; Subtract count of 0 's within  indices (0, 0) and (qi, pj-1) ; Add prefixCnt [ pi - 1 ] [ pj - 1 ] because its value has been added ; once but subtracted twice ; Function to count the 0 s in the each given submatrix ; Stores the prefix sum of each row and column ; Compute matrix prefixCnt [ , ] ; Function Call for each query ; Driver Code ; Given matrix ; Function Call
using System ; class GFG { static int M = 6 ; static int N = 7 ; static void preCompute ( int [ , ] mat , int [ , ] prefixCnt ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( mat [ i , j ] == 0 ) { prefixCnt [ i , j ] = 1 ; } else { prefixCnt [ i , j ] = 0 ; } } } for ( int i = 0 ; i < M ; i ++ ) for ( int j = 1 ; j < N ; j ++ ) prefixCnt [ i , j ] += prefixCnt [ i , j - 1 ] ; for ( int i = 1 ; i < M ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) prefixCnt [ i , j ] += prefixCnt [ i - 1 , j ] ; } static int countQuery ( int [ , ] prefixCnt , int pi , int pj , int qi , int qj ) { int cnt = prefixCnt [ qi , qj ] ; if ( pi > 0 ) cnt -= prefixCnt [ pi - 1 , qj ] ; if ( pj > 0 ) cnt -= prefixCnt [ qi , pj - 1 ] ; if ( pi > 0 && pj > 0 ) cnt += prefixCnt [ pi - 1 , pj - 1 ] ; return cnt ; } static void count0s ( int [ , ] mat , int [ , ] Q , int sizeQ ) { int [ , ] prefixCnt = new int [ M , N ] ; preCompute ( mat , prefixCnt ) ; for ( int i = 0 ; i < sizeQ ; i ++ ) { Console . Write ( countQuery ( prefixCnt , Q [ i , 0 ] , Q [ i , 1 ] , Q [ i , 2 ] , Q [ i , 3 ] ) + " ▁ " ) ; } } public static void Main ( string [ ] args ) { int [ , ] mat = { { 0 , 1 , 0 , 1 , 1 , 1 , 0 } , { 1 , 0 , 1 , 1 , 1 , 0 , 1 } , { 1 , 1 , 0 , 0 , 1 , 1 , 0 } , { 1 , 1 , 1 , 1 , 1 , 0 , 1 } , { 0 , 0 , 1 , 0 , 1 , 1 , 1 } , { 1 , 1 , 0 , 1 , 1 , 0 , 1 } } ; int [ , ] Q = { { 0 , 1 , 3 , 2 } , { 2 , 2 , 4 , 5 } , { 4 , 3 , 5 , 6 } } ; int sizeQ = Q . GetLength ( 0 ) ; count0s ( mat , Q , sizeQ ) ; } }
Smallest root of the equation x ^ 2 + s ( x ) * x | C # program to find smallest value of root of an equation under given constraints . ; function to check if the sum of digits is equal to the summation assumed ; calculate the sum of digit ; function to find the largest root possible . ; iterate for all possible sum of digits . ; check if discriminent is a perfect square . ; check if discriminent is a perfect square and if it as perefect root of the equation ; function returns answer ; driver program
using System ; public class GFG { static bool check ( long a , long b ) { long c = 0 ; while ( a != 0 ) { c = c + a % 10 ; a = a / 10 ; } return ( c == b ) ; } static long root ( long n ) { bool found = false ; long mx = ( long ) 1E18 ; for ( long i = 0 ; i <= 90 ; i ++ ) { long s = i * i + 4 * n ; long sq = ( long ) Math . Sqrt ( s ) ; if ( sq * sq == s && check ( ( sq - i ) / 2 , i ) ) { found = true ; mx = Math . Min ( mx , ( sq - i ) / 2 ) ; } } if ( found ) return mx ; else return - 1 ; } public static void Main ( ) { long n = 110 ; Console . Write ( root ( n ) ) ; } }
Sum of digits of a given number to a given power | C # program to find sum of digits of a given number to a given power ; Function to calculate sum ; Driver Code
using System ; public class base_power { static int calculate ( int n , int power ) { int sum = 0 ; int bp = ( int ) Math . Pow ( n , power ) ; while ( bp != 0 ) { int d = bp % 10 ; sum += d ; bp /= 10 ; } return sum ; } public static void Main ( ) { int n = 5 ; int power = 4 ; Console . WriteLine ( calculate ( n , power ) ) ; } }
Co | C # program to represent a number as sum of a co - prime pair such that difference between them is minimum ; Utility function ; function to check if pair is co - prime or not ; function to find and print co - prime pair ; Driver code
using System ; class GFG { static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static bool coprime ( int a , int b ) { return ( __gcd ( a , b ) == 1 ) ; } static void pairSum ( int n ) { int mid = n / 2 ; for ( int i = mid ; i >= 1 ; i -- ) { if ( coprime ( i , n - i ) == true ) { Console . Write ( i + " ▁ " + ( n - i ) ) ; break ; } } } public static void Main ( ) { int n = 11 ; pairSum ( n ) ; } }
Program for quotient and remainder of big number | C # program to find quotient and remainder when a number is divided by large number represented as String . ; Function to calculate the modulus ; Store the modulus of big number ; Do step by step division ; Update modulo by concatenating current digit . ; Update quotient ; Update mod for next iteration . ; Flag used to remove starting zeros ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void modBigNumber ( string num , long m ) { List < int > vec = new List < int > ( ) ; long mod = 0 ; for ( int i = 0 ; i < num . Length ; i ++ ) { int digit = num [ i ] - '0' ; mod = mod * 10 + digit ; int quo = ( int ) ( mod / m ) ; vec . Add ( quo ) ; mod = mod % m ; } Console . Write ( " Remainder ▁ : ▁ " + mod + " STRNEWLINE " ) ; Console . Write ( " Quotient ▁ : ▁ " ) ; bool zeroflag = false ; for ( int i = 0 ; i < vec . Count ; i ++ ) { if ( vec [ i ] == 0 && zeroflag == false ) { continue ; } zeroflag = true ; Console . Write ( vec [ i ] ) ; } return ; } public static void Main ( ) { string num = "14598499948265358486" ; long m = 487 ; modBigNumber ( num , m ) ; } }
Queries to find whether a number has exactly four distinct factors or not | C # program to check whether number has exactly four distinct factors or not ; Initialize global variable according to given condition so that it can be accessible to all function ; Function to calculate all number having four distinct distinct factors ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Initialize prime [ ] array which will contains all the primes from 1 - N ; Iterate over all the prime numbers ; Mark cube root of prime numbers ; Mark product of prime numbers ; Driver Code
using System ; using System . Collections ; class GFG { static int N = ( int ) 1E6 ; static bool [ ] fourDiv = new bool [ N + 1 ] ; static void fourDistinctFactors ( ) { bool [ ] primeAll = new bool [ N + 1 ] ; for ( int p = 2 ; p * p <= N ; p ++ ) { if ( primeAll [ p ] == false ) { for ( int i = p * 2 ; i <= N ; i += p ) primeAll [ i ] = true ; } } ArrayList prime = new ArrayList ( ) ; for ( int p = 2 ; p <= N ; p ++ ) if ( ! primeAll [ p ] ) prime . Add ( p ) ; for ( int i = 0 ; i < prime . Count ; ++ i ) { int p = ( int ) prime [ i ] ; if ( 1L * p * p * p <= N ) fourDiv [ p * p * p ] = true ; for ( int j = i + 1 ; j < prime . Count ; ++ j ) { int q = ( int ) prime [ j ] ; if ( 1L * p * q > N ) break ; fourDiv [ p * q ] = true ; } } } public static void Main ( ) { fourDistinctFactors ( ) ; int num = 10 ; if ( fourDiv [ num ] ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; num = 12 ; if ( fourDiv [ num ] ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Leonardo Number | A simple recursive program to find n - th leonardo number . ; Driver code
using System ; class GFG { static int leonardo ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + 1 ; return dp [ n ] ; } public static void Main ( ) { Console . WriteLine ( leonardo ( 3 ) ) ; } }
Cholesky Decomposition : Matrix Decomposition | C # program to decompose a matrix using Cholesky Decomposition ; Decomposing a matrix into Lower Triangular ; summation for diagonals ; Evaluating L ( i , j ) using L ( j , j ) ; Displaying Lower Triangular and its Transpose ; Lower Triangular ; Transpose of Lower Triangular ; Driver Code
using System ; class GFG { static void Cholesky_Decomposition ( int [ , ] matrix , int n ) { int [ , ] lower = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { int sum = 0 ; if ( j == i ) { for ( int k = 0 ; k < j ; k ++ ) sum += ( int ) Math . Pow ( lower [ j , k ] , 2 ) ; lower [ j , j ] = ( int ) Math . Sqrt ( matrix [ j , j ] - sum ) ; } else { for ( int k = 0 ; k < j ; k ++ ) sum += ( lower [ i , k ] * lower [ j , k ] ) ; lower [ i , j ] = ( matrix [ i , j ] - sum ) / lower [ j , j ] ; } } } Console . WriteLine ( " ▁ Lower ▁ Triangular TABSYMBOL ▁ Transpose " ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) Console . Write ( lower [ i , j ] + " TABSYMBOL " ) ; Console . Write ( " " ) ; for ( int j = 0 ; j < n ; j ++ ) Console . Write ( lower [ j , i ] + " TABSYMBOL " ) ; Console . WriteLine ( ) ; } } static int Main ( ) { int n = 3 ; int [ , ] matrix = { { 4 , 12 , - 16 } , { 12 , 37 , - 43 } , { - 16 , - 43 , 98 } } ; Cholesky_Decomposition ( matrix , n ) ; return 0 ; } }
Program for sum of arithmetic series | C # efficient solution to find sum of arithmetic series . ; Driver code
using System ; class GFG { static float sumOfAP ( float a , float d , float n ) { float sum = ( n / 2 ) * ( 2 * a + ( n - 1 ) * d ) ; return sum ; } static public void Main ( ) { float n = 20 ; float a = 2.5f , d = 1.5f ; Console . WriteLine ( sumOfAP ( a , d , n ) ) ; } }
Program for cube sum of first n natural numbers | Efficient C # program to find sum of cubes of first n natural numbers that avoids overflow if result is going to be withing limits . ; Returns the sum of series ; Driver code
using System ; class GFG { public static int sumOfSeries ( int n ) { int x ; if ( n % 2 == 0 ) x = ( n / 2 ) * ( n + 1 ) ; else x = ( ( n + 1 ) / 2 ) * n ; return x * x ; } static public void Main ( ) { int n = 5 ; Console . WriteLine ( sumOfSeries ( n ) ) ; } }
Maximum value of | arr [ i ] | C # program to find maximum value of | arr [ i ] - arr [ j ] | + | i - j | ; Return maximum value of | arr [ i ] - arr [ j ] | + | i - j | ; Iterating two for loop , one for i and another for j . ; Evaluating | arr [ i ] - arr [ j ] | + | i - j | and compare with previous maximum . ; Driver code
using System ; class GFG { static int findValue ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) ans = Math . Max ( ans , Math . Abs ( arr [ i ] - arr [ j ] ) + Math . Abs ( i - j ) ) ; return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 1 } ; int n = arr . Length ; Console . Write ( findValue ( arr , n ) ) ; } }
Maximum value of | arr [ i ] | Efficient Java program to find maximum value of | arr [ i ] - arr [ j ] | + | i - j | ; Return maximum | arr [ i ] - arr [ j ] | + | i - j | ; Calculating first_array and second_array ; Finding maximum and minimum value in first_array ; Storing the difference between maximum and minimum value in first_array ; Finding maximum and minimum value in second_array ; Storing the difference between maximum and minimum value in second_array ; Driver Code
using System ; class GFG { static int findValue ( int [ ] arr , int n ) { int [ ] a = new int [ n ] ; int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = ( arr [ i ] + i ) ; b [ i ] = ( arr [ i ] - i ) ; } int x = a [ 0 ] , y = a [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > x ) x = a [ i ] ; if ( a [ i ] < y ) y = a [ i ] ; } int ans1 = ( x - y ) ; x = b [ 0 ] ; y = b [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( b [ i ] > x ) x = b [ i ] ; if ( b [ i ] < y ) y = b [ i ] ; } int ans2 = ( x - y ) ; return Math . Max ( ans1 , ans2 ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 1 } ; int n = arr . Length ; Console . WriteLine ( findValue ( arr , n ) ) ; } }
Number of subarrays having product less than K | C # program to count subarrays having product less than k . ; Counter for single element ; Multiple subarray ; If this multiple is less than k , then increment ; Driver Code
using System ; public class GFG { static int countsubarray ( int [ ] array , int n , int k ) { int count = 0 ; int i , j , mul ; for ( i = 0 ; i < n ; i ++ ) { if ( array [ i ] < k ) count ++ ; mul = array [ i ] ; for ( j = i + 1 ; j < n ; j ++ ) { mul = mul * array [ j ] ; if ( mul < k ) count ++ ; else break ; } } return count ; } static public void Main ( ) { int [ ] array = { 1 , 2 , 3 , 4 } ; int k = 10 ; int size = array . Length ; int count = countsubarray ( array , size , k ) ; Console . WriteLine ( count ) ; } }