codeparrot/xlcost-text-to-code huggingface dataset:
Maximum sum of a subsequence having difference between their indices equal to the difference between their values | C # program for the above approach ; Function to find the maximum sum of a subsequence having difference between indices equal to difference in their values ; Stores the maximum sum ; Stores the value for each A [ i ] - i ; Traverse the array ; Update the value in map ; Update the answer ; Finally , print the answer ; Driver code ; Given Input ; Function Call
using System . Collections . Generic ; using System ; class GFG { static void maximumSubsequenceSum ( int [ ] A , int N ) { int ans = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( A [ i ] - i ) ) mp [ A [ i ] - i ] += A [ i ] ; else mp [ A [ i ] - i ] = A [ i ] ; ans = Math . Max ( ans , mp [ A [ i ] - i ] ) ; } Console . Write ( ans ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 10 , 7 , 1 , 9 , 10 , 1 } ; int N = A . Length ; maximumSubsequenceSum ( A , N ) ; } }
Find the nearest perfect square for each element of the array | C # program for the above approach ; Function to find the nearest perfect square for every element in the given array ; Traverse the array ; Calculate square root of current element ; Calculate perfect square ; Find the nearest ; Driver Code
using System ; class GFG { static void nearestPerfectSquare ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int sr = ( int ) Math . Sqrt ( arr [ i ] ) ; int a = sr * sr ; int b = ( sr + 1 ) * ( sr + 1 ) ; if ( ( arr [ i ] - a ) < ( b - arr [ i ] ) ) Console . Write ( a + " ▁ " ) ; else Console . Write ( b + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 5 , 2 , 7 , 13 } ; int N = arr . Length ; nearestPerfectSquare ( arr , N ) ; } }
Count of sets possible using integers from a range [ 2 , N ] using given operations that are in Equivalence Relation | C # program for the above approach ; Sieve of Eratosthenes to find primes less than or equal to N ; Function to find number of Sets ; Handle Base Case ; Set which contains less than or equal to N / 2 ; Number greater than N / 2 and are prime increment it by 1 ; If the number is prime Increment answer by 1 ; Driver Code ; Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static bool [ ] prime = new bool [ 100001 ] ; static void SieveOfEratosthenes ( int n ) { for ( int i = 0 ; i < 100001 ; i ++ ) prime [ i ] = true ; 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 NumberofSets ( int N ) { SieveOfEratosthenes ( N ) ; if ( N == 2 ) { Console . Write ( 1 ) ; } else if ( N == 3 ) { Console . Write ( 2 ) ; } else { int ans = 1 ; for ( int i = N / 2 + 1 ; i <= N ; i ++ ) { if ( prime [ i ] ) { ans += 1 ; } } Console . Write ( ans ) ; } } public static void Main ( ) { int N = 9 ; NumberofSets ( N ) ; } }
Absolute difference between floor of Array sum divided by X and floor sum of every Array element when divided by X | C # program for the above approach ; Function to find absolute difference between the two sum values ; Variable to store total sum ; Variable to store sum of A [ i ] / X ; Traverse the array ; Update totalSum ; Update perElementSum ; Floor of total sum divided by X ; Return the absolute difference ; Driver code ; Input ; Size of Array ; Function call to find absolute difference between the two sum values
using System ; class GFG { static int floorDifference ( int [ ] A , int N , int X ) { int totalSum = 0 ; int perElementSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalSum += A [ i ] ; perElementSum += A [ i ] / X ; } int totalFloorSum = totalSum / X ; return Math . Abs ( totalFloorSum - perElementSum ) ; } static void Main ( ) { int [ ] A = { 1 , 2 , 3 , 4 , 5 , 6 } ; int X = 4 ; int N = A . Length ; Console . Write ( floorDifference ( A , N , X ) ) ; } }
Convert a number from base A to base B | C # program for the above approach ; Function to return ASCII value of a character ; Function to convert a number from given basse to decimal number ; Stores the length of the string ; Initialize power of basse ; Initialize result ; Decimal equivalent is str [ len - 1 ] * 1 + str [ len - 2 ] * basse + str [ len - 3 ] * ( basse ^ 2 ) + ... ; A digit in input number must be less than number 's basse ; Update num ; Update power ; Function to return equivalent character of a given value ; Function to convert a given decimal number to a given basse ; Store the result ; Repeatedly divide inputNum by basse and take remainder ; Update res ; Update inputNum ; Reverse the result ; Function to convert a given number from a basse to another basse ; Convert the number from basse A to decimal ; Convert the number from decimal to basse B ; Print the result ; Driver Code ; Given input ; Function Call
using System ; public class GFG { static int val ( char c ) { if ( c >= '0' && c <= '9' ) return ( int ) c - '0' ; else return ( int ) c - ' A ' + 10 ; } static int toDeci ( string str , int basse ) { int len = str . Length ; int power = 1 ; int num = 0 ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str [ i ] ) >= basse ) { Console . Write ( " Invalid ▁ Number " ) ; return - 1 ; } num += val ( str [ i ] ) * power ; power = power * basse ; } return num ; } static char reVal ( int num ) { if ( num >= 0 && num <= 9 ) return ( char ) ( num + '0' ) ; else return ( char ) ( num - 10 + ' A ' ) ; } static string fromDeci ( int basse , int inputNum ) { string res = " " ; while ( inputNum > 0 ) { res += reVal ( inputNum % basse ) ; inputNum /= basse ; } res = reverse ( res ) ; return res ; } static void convertbasse ( string s , int a , int b ) { int num = toDeci ( s , a ) ; string ans = fromDeci ( b , num ) ; Console . Write ( ans ) ; } static string reverse ( string input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return new string ( a ) ; } static public void Main ( ) { string s = "10B " ; int a = 16 , b = 10 ; convertbasse ( s , a , b ) ; } }
Prefix Factorials of a Prefix Sum Array | C # program for the above approach ; Function to find the factorial of a number N ; Base Case ; Find the factorial recursively ; Function to find the prefix factorial array ; Find the prefix sum array ; Find the factorials of each array element ; Print the resultant array ; Driver Code
using System ; class GFG { static int fact ( int N ) { if ( N == 1 N == 0 ) return 1 ; return N * fact ( N - 1 ) ; } static void prefixFactorialArray ( int [ ] arr , int N ) { for ( int i = 1 ; i < N ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = fact ( arr [ i ] ) ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; prefixFactorialArray ( arr , N ) ; } }
Mean of fourth powers of first N natural numbers | C # program for the above approach ; Function to find the average of the fourth power of first N natural numbers ; Store the resultant average calculated using formula ; Return the average ; Driver Code
using System ; class GFG { static double findAverage ( int N ) { double avg = ( ( 6 * N * N * N * N ) + ( 15 * N * N * N ) + ( 10 * N * N ) - 1 ) / 30.0 ; return avg ; } public static void Main ( ) { int N = 3 ; Console . WriteLine ( findAverage ( N ) ) ; } }
Modify array by removing ( arr [ i ] + arr [ i + 1 ] ) th element exactly K times | C # program for the above approach ; Function to modify array by removing every K - th element from the array ; Check if current element is the k - th element ; Stores the elements after removing every kth element ; Append the current element if it is not k - th element ; Return the new array after removing every k - th element ; Function to print the array ; Traverse the array l [ ] ; Function to print the array after performing the given operations exactly k times ; Store first N natural numbers ; Iterate over the range [ 0 , k - 1 ] ; Store sums of the two consecutive terms ; Remove every p - th element from the array ; Increment x by 1 for the next iteration ; Print the resultant array ; Driver Code ; Given arrays ; Function Call
using System ; using System . Collections . Generic ; class GFG { static List < int > removeEveryKth ( List < int > l , int k ) { for ( int i = 0 ; i < l . Count ; i ++ ) { if ( i % k == 0 ) l [ i ] = 0 ; } List < int > arr = new List < int > ( ) ; arr . Add ( 0 ) ; for ( int i = 1 ; i < l . Count ; i ++ ) { if ( l [ i ] != 0 ) arr . Add ( l [ i ] ) ; } return arr ; } static void printArray ( List < int > l ) { for ( int i = 1 ; i < l . Count ; i ++ ) Console . Write ( l [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } static void printSequence ( int n , int k ) { List < int > l = new List < int > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) l . Add ( i ) ; int x = 1 ; for ( int i = 0 ; i < k ; i ++ ) { int p = l [ x ] + l [ x + 1 ] ; l = removeEveryKth ( l , p ) ; x += 1 ; } printArray ( l ) ; } public static void Main ( ) { int N = 8 ; int K = 2 ; printSequence ( N , K ) ; } }
Minimum number of bits of array elements required to be flipped to make all array elements equal | C # program for the above approach ; Function to count minimum number of bits required to be flipped to make all array elements equal ; Stores the count of unset bits ; Stores the count of set bits ; Traverse the array ; Traverse the bit of arr [ i ] ; If current bit is set ; Increment fre1 [ j ] ; Otherwise ; Increment fre0 [ j ] ; Right shift x by 1 ; Stores the count of total moves ; Traverse the range [ 0 , 32 ] ; Update the value of ans ; Return the minimum number of flips required ; Driver Code
using System ; class GFG { static int makeEqual ( int [ ] arr , int n ) { int [ ] fre0 = new int [ 33 ] ; int [ ] fre1 = new int [ 33 ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = arr [ i ] ; for ( int j = 0 ; j < 33 ; j ++ ) { if ( ( x & 1 ) != 0 ) { fre1 [ j ] += 1 ; } else { fre0 [ j ] += 1 ; } x = x >> 1 ; } } int ans = 0 ; for ( int i = 0 ; i < 33 ; i ++ ) { ans += Math . Min ( fre0 [ i ] , fre1 [ i ] ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 3 , 5 } ; int N = arr . Length ; Console . WriteLine ( makeEqual ( arr , N ) ) ; } }
Sum of array elements possible by appending arr [ i ] / K to the end of the array K times for array elements divisible by K | C # program for the above approach ; Function to calculate sum of array elements after adding arr [ i ] / K to the end of the array if arr [ i ] is divisible by K ; Stores the sum of the array ; Traverse the array arr [ ] ; Traverse the vector ; If v [ i ] is divisible by K ; Iterate over the range [ 0 , K ] ; Update v ; Otherwise ; Traverse the vector v ; Return the sum of the updated array ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int sum ( int [ ] arr , int N , int K ) { int sum = 0 ; List < int > v = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { v . Add ( arr [ i ] ) ; } for ( int i = 0 ; i < v . Count ; i ++ ) { if ( v [ i ] % K == 0 ) { int x = v [ i ] / K ; for ( int j = 0 ; j < K ; j ++ ) { v . Add ( x ) ; } } else break ; } for ( int i = 0 ; i < v . Count ; i ++ ) sum = sum + v [ i ] ; return sum ; } public static void Main ( string [ ] args ) { int [ ] arr = { 4 , 6 , 8 , 2 } ; int K = 2 ; int N = arr . Length ; Console . WriteLine ( sum ( arr , N , K ) ) ; } }
Check if sum of arr [ i ] / j for all possible pairs ( i , j ) in an array is 0 or not | C # program for the above approach ; Function to check if sum of all values of ( arr [ i ] / j ) for all 0 < i <= j < ( N - 1 ) is 0 or not ; Stores the required sum ; Traverse the array ; If the sum is equal to 0 ; Otherwise ; Driver Code
using System ; class GFG { static void check ( int [ ] arr , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; if ( sum == 0 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , - 1 , 3 , - 2 , - 1 } ; int N = arr . Length ; check ( arr , N ) ; } }
Program to calculate expected increase in price P after N consecutive days | C # program for the above approach ; Function to find the increased value of P after N days ; Expected value of the number P after N days ; Print the expected value ; Driver code
using System ; class GFG { static void expectedValue ( int P , int a , int b , int N ) { double expValue = P + ( N * 0.5 * ( a + b ) ) ; Console . Write ( expValue ) ; } static void Main ( ) { int P = 3000 , a = 20 , b = 10 , N = 30 ; expectedValue ( P , a , b , N ) ; } }
Find the index in a circular array from which prefix sum is always non | C # program for the above approach ; Function to find the starting index of the given circular array s . t . prefix sum array is non negative ; Stores the sum of the array ; Stores the starting index ; Stores the minimum prefix sum of A [ 0. . i ] ; Traverse the array arr [ ] ; Update the value of sum ; If sum is less than min ; Update the min as the value of prefix sum ; Update in ; Otherwise , no such index is possible ; Driver Code
using System ; class GFG { static int startingPoint ( int [ ] A , int N ) { int sum = 0 ; int ind = 0 ; int min = Int32 . MaxValue ; for ( int i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; if ( sum < min ) { min = sum ; ind = i + 1 ; } } if ( sum < 0 ) { return - 1 ; } return ind % N ; } public static void Main ( ) { int [ ] arr = { 3 , - 6 , 7 , - 4 , - 4 , 6 , - 1 } ; int N = arr . Length ; Console . Write ( startingPoint ( arr , N ) ) ; } }
Modify array by replacing elements with the nearest power of its previous or next element | C # program for the above approach ; Function to calculate the power of y which is nearest to x ; Base Case ; Stores the logarithmic value of x with base y ; Function to replace each array element by the nearest power of its previous or next element ; Stores the previous and next element ; Traverse the array ; Calculate nearest power for previous and next elements ; Replacing the array values ; Print the updated array ; Driver Code ; Given array
using System ; class GFG { static int nearestPow ( int x , int y ) { if ( y == 1 ) return 1 ; int k = ( int ) ( Math . Log ( x , y ) ) ; if ( Math . Abs ( Math . Pow ( y , k ) - x ) < Math . Abs ( Math . Pow ( y , ( k + 1 ) ) - x ) ) return ( int ) ( Math . Pow ( y , k ) ) ; return ( int ) ( Math . Pow ( y , ( k + 1 ) ) ) ; } static void replacebyNearestPower ( int [ ] arr ) { int prev = arr [ arr . Length - 1 ] ; int lastNext = arr [ 0 ] ; int next = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { int temp = arr [ i ] ; if ( i == arr . Length - 1 ) next = lastNext ; else next = arr [ ( i + 1 ) % arr . Length ] ; int prevPow = nearestPow ( arr [ i ] , prev ) ; int nextPow = nearestPow ( arr [ i ] , next ) ; if ( Math . Abs ( arr [ i ] - prevPow ) < Math . Abs ( arr [ i ] - nextPow ) ) arr [ i ] = prevPow ; else arr [ i ] = nextPow ; prev = temp ; } for ( int i = 0 ; i < arr . Length ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 1 , 2 } ; replacebyNearestPower ( arr ) ; } }
Check if sum of array can be made equal to X by removing either the first or last digits of every array element | C # program for the above approach ; Utility Function to check if the sum of the array elements can be made equal to X by removing either the first or last digits of every array element ; Base Case ; Convert arr [ i ] to string ; Remove last digit ; Remove first digit ; Recursive function call ; Function to check if sum of given array can be made equal to X or not ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static bool makeSumX ( int [ ] arr , int X , int S , int i ) { if ( i == arr . Length ) { return S == X ; } string a = Convert . ToString ( arr [ i ] ) ; int l = Int32 . Parse ( a . Substring ( 0 , a . Length - 1 ) ) ; int r = Int32 . Parse ( a . Substring ( 1 ) ) ; bool x = makeSumX ( arr , X , S + l , i + 1 ) ; bool y = makeSumX ( arr , X , S + r , i + 1 ) ; return ( x y ) ; } static void Check ( int [ ] arr , int X ) { if ( makeSumX ( arr , X , 0 , 0 ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } public static void Main ( ) { int [ ] arr = { 545 , 433 , 654 , 23 } ; int X = 134 ; Check ( arr , X ) ; } }
Count subarrays having product equal to the power of a given Prime Number | C # program for the above approach ; Function to check if y is a power of m or not ; Calculate log y base m and store it in a variable with integer datatype ; Calculate log y base m and store it in a variable with double datatype ; If res1 and res2 are equal , return True . Otherwise , return false ; Function to count the number of subarrays having product of elements equal to a power of m , where m is a prime number ; Stores the count of subarrays required ; Stores current sequence of consecutive array elements which are a multiple of m ; Traverse the array ; If arr [ i ] is a power of M ; Increment cnt ; Update ans ; Update cnt ; Return the count of subarrays ; Driver code ; Input
using System ; class GFG { static bool isPower ( int m , int y ) { int res1 = ( int ) Math . Log ( y ) / ( int ) Math . Log ( m ) ; double res2 = ( int ) Math . Log ( y ) / ( int ) Math . Log ( m ) ; return ( res1 == res2 ) ; } static int numSub ( int [ ] arr , int n , int m ) { int ans = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPower ( m , arr [ i ] ) ) { cnt ++ ; ans += ( cnt * ( cnt - 1 ) ) / 2 ; } else { cnt = 0 ; } } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 1 , 3 } ; int m = 3 ; int n = arr . Length ; Console . Write ( numSub ( arr , n , m ) ) ; } }
Count pairs whose Bitwise AND exceeds Bitwise XOR from a given array | C # program to implement the above approach ; Function to count pairs that satisfy the above condition ; Stores the count of pairs ; Stores the count of array elements having same positions of MSB ; Traverse the array ; Stores the index of MSB of array elements ; Calculate number of pairs ; Driver Code ; Given Input ; Function call to count pairs satisfying the given condition
using System ; class GFG { static int cntPairs ( int [ ] arr , int N ) { int res = 0 ; int [ ] bit = new int [ 32 ] ; for ( int i = 0 ; i < N ; i ++ ) { int pos = ( int ) ( Math . Log ( arr [ i ] ) / Math . Log ( 2 ) ) ; bit [ pos ] ++ ; } for ( int i = 0 ; i < 32 ; i ++ ) { res += ( bit [ i ] * ( bit [ i ] - 1 ) ) / 2 ; } return res ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; Console . Write ( cntPairs ( arr , N ) ) ; } }
Minimum MEX from all subarrays of length K | C # program for the above approach ; Function to return minimum MEX from all K - length subarrays ; Stores element from [ 1 , N + 1 ] which are not present in subarray ; Store number 1 to N + 1 in set s ; Find the MEX of K - length subarray starting from index 0 ; Find the MEX of all subarrays of length K by erasing arr [ i ] and inserting arr [ i - K ] ; Store first element of set ; Updating the mex ; Print minimum MEX of all K length subarray ; Driver code
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static void minimumMEX ( int [ ] arr , int N , int K ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 1 ; i <= N + 1 ; i ++ ) s . Add ( i ) ; for ( int i = 0 ; i < K ; i ++ ) s . Remove ( arr [ i ] ) ; int mex = s . First ( ) ; for ( int i = K ; i < N ; i ++ ) { s . Remove ( arr [ i ] ) ; s . Add ( arr [ i - K ] ) ; int firstElem = s . First ( ) ; mex = Math . Min ( mex , firstElem ) ; } Console . Write ( mex + " ▁ " ) ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int K = 3 ; int N = arr . Length ; minimumMEX ( arr , N , K ) ; } }
Count smaller elements present in the array for each array element | C # program for the above approach ; Function to count for each array element , the number of elements that are smaller than that element ; Stores the frequencies of array elements ; Traverse the array ; Update frequency of arr [ i ] ; Compute prefix sum of the array hash [ ] ; Traverse the array arr [ ] ; If current element is 0 ; Print the resultant count ; Driver Code
using System ; class GFG { static void smallerNumbers ( int [ ] arr , int N ) { int [ ] hash = new int [ 100000 ] ; for ( int i = 0 ; i < N ; i ++ ) hash [ arr [ i ] ] ++ ; for ( int i = 1 ; i < 100000 ; i ++ ) { hash [ i ] += hash [ i - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { Console . WriteLine ( "0" ) ; continue ; } Console . Write ( hash [ arr [ i ] - 1 ] + " ▁ " ) ; } } public static void Main ( string [ ] args ) { int [ ] arr = { 3 , 4 , 1 , 1 , 2 } ; int N = arr . Length ; smallerNumbers ( arr , N ) ; } }
Modulo Operations in Programming With Negative Results | C # program for the above approach ; Function to calculate and return the remainder of a % n ; ( a / n ) implicitly gives the truncated result ; Driver Code ; Modulo of two positive numbers ; Modulo of a negative number by a positive number ; Modulo of a positive number by a negative number ; Modulo of two negative numbers
using System ; public class GFG { static int truncMod ( int a , int n ) { int q = a / n ; return a - n * q ; } static public void Main ( ) { int a , b ; a = 9 ; b = 4 ; Console . WriteLine ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + truncMod ( a , b ) ) ; a = - 9 ; b = 4 ; Console . WriteLine ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + truncMod ( a , b ) ) ; a = 9 ; b = - 4 ; Console . WriteLine ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + truncMod ( a , b ) ) ; a = - 9 ; b = - 4 ; Console . WriteLine ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + truncMod ( a , b ) ) ; } }
Program for average of an array without running into overflow | C # program for the above approach ; Function to calculate average of an array using standard method ; Stores the sum of array ; Find the sum of the array ; Return the average ; Function to calculate average of an array using efficient method ; Store the average of the array ; Traverse the array arr [ ] ; Update avg ; Return avg ; Driver Code ; Input ; Function call
using System ; class GFG { static double average ( int [ ] arr , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; return ( double ) ( sum / N ) ; } static double mean ( int [ ] arr , int N ) { double avg = 0.0 ; for ( int i = 0 ; i < N ; i ++ ) { avg += ( ( double ) ( ( arr [ i ] - avg ) / ( i + 1 ) ) ) ; } return avg ; } static public void Main ( ) { int [ ] arr = { Int32 . MaxValue , 1 , 2 } ; int N = arr . Length ; Console . WriteLine ( " Average ▁ by ▁ Standard ▁ method : ▁ " + ( average ( arr , N ) ) . ToString ( " F10" ) ) ; Console . WriteLine ( " Average ▁ by ▁ Efficient ▁ method : ▁ " + ( mean ( arr , N ) ) . ToString ( " F10" ) ) ; } }
Count number of pairs ( i , j ) from an array such that arr [ i ] * j = arr [ j ] * i | C # program for the above approach ; Function to count pairs from an array satisfying given conditions ; Stores the total count of pairs ; Stores count of a [ i ] / i ; Traverse the array ; Updating count ; Update frequency in the Map ; Print count of pairs ; Driver Code ; Given array ; Size of the array ; Function call to count pairs satisfying given conditions
using System ; using System . Collections . Generic ; class GFG { static void countPairs ( int [ ] arr , int N ) { int count = 0 ; Dictionary < double , int > mp = new Dictionary < double , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { double val = 1.0 * arr [ i ] ; double idx = 1.0 * ( i + 1 ) ; if ( mp . ContainsKey ( val / idx ) ) count += mp [ val / idx ] ; if ( mp . ContainsKey ( val / idx ) ) mp [ val / idx ] ++ ; else mp [ val / idx ] = 1 ; } Console . WriteLine ( count ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 6 , 5 } ; int N = arr . Length ; countPairs ( arr , N ) ; } }