codeparrot/xlcost-text-to-code huggingface dataset:
Count of pass required to visit same index again by moving arr [ i ] to index arr [ i ] | C # program for the above approach ; Function to find the number of moves required to visit the same index again for every array element ; Make given array 0 index based ; Stores the number of moves ; Store index value ; Update the value of cnt ; Make a pass ; Print the value of cnt ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void numberOfPasses ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; ++ i ) { -- arr [ i ] ; } for ( int i = 0 ; i < N ; ++ i ) { int cnt = 0 ; int k = i ; do { ++ cnt ; k = arr [ k ] ; } while ( k != i ) ; Console . Write ( cnt + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 4 , 6 , 2 , 1 , 5 , 3 } ; int N = arr . Length ; numberOfPasses ( arr , N ) ; } }
Find subfactorial of a number | / C # program for the above approach ; Function to find the subfactorial of the number ; Initialize variables ; Iterating over range N ; Fact variable store factorial of the i ; If count is even ; Increase the value of count by 1 ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static double subfactorial ( int N ) { double res = 0 , fact = 1 ; int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { fact = fact * i ; if ( count % 2 == 0 ) res = res - ( 1 / fact ) ; else res = res + ( 1 / fact ) ; count ++ ; } return fact * ( 1 + res ) ; } public static void Main ( ) { int N = 4 ; Console . Write ( subfactorial ( N ) ) ; } }
Minimize operations to delete all elements of permutation A by removing a subsequence having order as array B | C # program for the above approach ; Function to find the minimum number of operations to delete all elements of permutation A in order described by B ; Stores the count of operations ; Stores the index of current integer in B to be deleted ; Loop to iterate over all values of B ; Stores the current index in A ; Iterate over all values A ; If current integer of B and A equal , increment the index of the current integer of B ; As the permutation A has been traversed completely , increment the count of operations by 1 ; Return Answer ; Driver Code
using System ; class GFG { static int minOperations ( int [ ] A , int [ ] B , int N ) { int cnt = 0 ; int i = 0 ; while ( i < N ) { int j = 0 ; while ( j < N && i < N ) { if ( B [ i ] == A [ j ] ) { i ++ ; } j ++ ; } cnt ++ ; } return cnt ; } public static void Main ( string [ ] args ) { int [ ] A = { 2 , 4 , 6 , 1 , 5 , 3 } ; int [ ] B = { 6 , 5 , 4 , 2 , 3 , 1 } ; int N = A . Length ; Console . WriteLine ( minOperations ( A , B , N ) ) ; } }
Count of pair of integers ( x , y ) such that difference between square of x and y is a perfect square | C # program for the above approach ; Function to find number of pairs ( x , y ) such that x ^ 2 - y is a square number ; Stores the count of total pairs ; Iterate q value 1 to sqrt ( N ) ; Maximum possible value of p is min ( 2 * N - q , N / q ) ; P must be greater than or equal to q ; Total number of pairs are ; Adding all valid pairs to res ; Return total no of pairs ( x , y ) ; Driver Code
using System ; class GFG { static int countPairs ( int N ) { int res = 0 ; for ( int q = 1 ; q * q <= N ; q ++ ) { int maxP = Math . Min ( 2 * N - q , N / q ) ; if ( maxP < q ) continue ; int cnt = maxP - q + 1 ; res += ( cnt / 2 + ( cnt & 1 ) ) ; } return res ; } public static void Main ( ) { int N = 3 ; Console . WriteLine ( countPairs ( N ) ) ; } }
Maximum size of subset such that product of all subset elements is a factor of N | C # program for the above approach ; Function to find the maximum size of the subset such that the product of subset elements is a factor of N ; Base Case ; Stores maximum size of valid subset ; Traverse the given array ; If N % arr [ i ] = 0 , include arr [ i ] in a subset and recursively call for the remaining array integers ; Return Answer ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int maximizeSubset ( int N , int [ ] arr , int M , int x ) { if ( x == M ) { return 0 ; } int ans = 0 ; for ( int i = x ; i < M ; i ++ ) { if ( N % arr [ i ] == 0 ) { ans = Math . Max ( ans , maximizeSubset ( N / arr [ i ] , arr , M , x + 1 ) + 1 ) ; } } return ans ; } public static void Main ( ) { int N = 64 ; int [ ] arr = { 1 , 2 , 4 , 8 , 16 , 32 } ; int M = arr . Length ; Console . Write ( maximizeSubset ( N , arr , M , 0 ) ) ; } }
Count of distinct sums formed by N numbers taken form range [ L , R ] | C # program for the above approach ; Function to find total number of different sums of N numbers in the range [ L , R ] ; To store minimum possible sum with N numbers with all as L ; To store maximum possible sum with N numbers with all as R ; All other numbers in between maxSum and minSum can also be formed so numbers in this range is the final answer ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countDistinctSums ( int N , int L , int R ) { int minSum = L * N ; int maxSum = R * N ; return maxSum - minSum + 1 ; } public static void Main ( ) { int N = 2 , L = 1 , R = 3 ; Console . Write ( countDistinctSums ( N , L , R ) ) ; } }
Make the Array sum 0 by using either ceil or floor on each element | C # program for the above approach ; Function to modify the array element such that the sum is close to 0 ; Stores the modified elements ; Stores the sum of array element ; Stores minimum size of the array ; Traverse the array and find the sum ; If sum is positive ; Find the minimum number of elements that must be changed ; Iterate until M elements are modified or the array end ; Update the current array elements to its floor ; Print the resultant array ; Driver Code
using System ; public class GFG { static void setSumtoZero ( double [ ] arr , int N ) { int [ ] A = new int [ N ] ; int sum = 0 ; int m = int . MinValue ; for ( int i = 0 ; i < N ; i ++ ) { sum += ( int ) Math . Ceiling ( arr [ i ] ) ; A [ i ] = ( int ) Math . Ceiling ( arr [ i ] ) ; } if ( sum > 0 ) { m = Math . Min ( sum , N ) ; for ( int i = 0 ; i < N && m > 0 ; i ++ ) { A [ i ] = ( int ) Math . Floor ( arr [ i ] ) ; if ( A [ i ] != Math . Floor ( arr [ i ] ) ) m -- ; } } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( A [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { double [ ] arr = { - 2 , - 2 , 4.5 } ; int N = arr . Length ; setSumtoZero ( arr , N ) ; } }
Finding the Nth term in a sequence formed by removing digit K from natural numbers | C # implementation for the above approach ; Denotes the digit place ; Method to convert any number to binary equivalent ; denotes the current digits place ; If current digit is >= K increment its value by 1 ; Else add the digit as it is ; Move to the next digit ; Driver code
using System ; class GFG { static long convertToBase9 ( long n ) { long ans = 0 ; long a = 1 ; while ( n > 0 ) { ans += ( a * ( n % 9 ) ) ; a *= 10 ; n /= 9 ; } return ans ; } static long getNthnumber ( long base9 , long K ) { long ans = 0 ; long a = 1 ; while ( base9 > 0 ) { int cur = ( int ) ( base9 % 10 ) ; if ( cur >= K ) { ans += a * ( cur + 1 ) ; } else { ans += a * cur ; } base9 /= 10 ; a *= 10 ; } return ans ; } public static void Main ( String [ ] args ) { long N = 10 , K = 1 ; long base9 = convertToBase9 ( N ) ; Console . Write ( getNthnumber ( base9 , K ) ) ; } }
Check if given array can be rearranged such that mean is equal to median | C # program for the above approach ; Function to return true or false if size of array is odd ; To prevent overflow ; If element is greater than mid , then it can only be present in right subarray ; If element is smaller than mid , then it can only be present in left subarray ; Else the element is present at the middle then return 1 ; when element is not present in array then return 0 ; Function to return true or false if size of array is even ; Calculating Candidate Median ; If Candidate Median if greater than Mean then decrement j ; If Candidate Median if less than Mean then increment i ; If Candidate Median if equal to Mean then return 1 ; when No candidate found for mean ; Function to return true if Mean can be equal to any candidate median otherwise return false ; Calculating Mean ; If N is Odd ; If N is even ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool binarySearch ( float [ ] arr , int size , float key ) { int low = 0 , high = size - 1 , mid ; while ( low <= high ) { mid = low + ( high - low ) / 2 ; if ( key > arr [ mid ] ) low = mid + 1 ; else if ( key < arr [ mid ] ) high = mid - 1 ; else return true ; } return false ; } static bool twoPointers ( float [ ] arr , int N , float mean ) { int i = 0 , j = N - 1 ; while ( i < j ) { float temp = ( arr [ i ] + arr [ j ] ) / 2 ; if ( temp > mean ) j -- ; else if ( temp < mean ) i ++ ; else return true ; } return false ; } static bool checkArray ( float [ ] arr , int N ) { float sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; float mean = sum / N ; if ( ( N & 1 ) != 0 ) return binarySearch ( arr , N , mean ) ; else return twoPointers ( arr , N , mean ) ; } public static void Main ( ) { float [ ] arr = { 1.0f , 3.0f , 6.0f , 9.0f , 12.0f , 32.0f } ; int N = arr . Length ; if ( checkArray ( arr , N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Count of distinct integers belonging to first N terms of at least one of given GPs | C # program for the above approach ; Function to find the count of distinct integers that belong to the first N terms of at least one of them is GP ; Stores the integers that occur in GPs in a set data - structure ; Stores the current integer of the first GP ; Iterate first N terms of first GP ; Insert the ith term of GP in S ; Stores the current integer of the second GP ; Iterate first N terms of second GP ; Return Answer ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static int UniqueGeometricTerms ( int N , int a1 , int r1 , int a2 , int r2 ) { HashSet < int > S = new HashSet < int > ( ) ; int p1 = a1 ; for ( int i = 0 ; i < N ; i ++ ) { S . Add ( p1 ) ; p1 = ( p1 * r1 ) ; } int p2 = a2 ; for ( int i = 0 ; i < N ; i ++ ) { S . Add ( p2 ) ; p2 = ( p2 * r2 ) ; } return S . Count ; } public static void Main ( string [ ] args ) { int N = 5 ; int a1 = 3 , r1 = 2 , a2 = 2 , r2 = 3 ; Console . Write ( UniqueGeometricTerms ( N , a1 , r1 , a2 , r2 ) ) ; } }
Minimize operations to make all array elements | C # program for the above approach ; Function to find minimum the number of operations required to make all the array elements to - 1 ; Stores the array elements with their corresponding indices ; Push the array element and it 's index ; Sort the elements according to it 's first value ; Stores the minimum number of operations required ; Traverse until vp is not empty ; Stores the first value of vp ; Stores the second value of vp ; Update the minCnt ; Pop the back element from the vp until the first value is same as val and difference between indices is less than K ; Return the minCnt ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { class pair : IComparable < pair > { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } public int CompareTo ( pair p ) { return this . first - p . first ; } } static int minOperations ( int [ ] arr , int N , int K ) { List < pair > vp = new List < pair > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { vp . Add ( new pair ( arr [ i ] , i ) ) ; } vp . Sort ( ) ; int minCnt = 0 ; while ( vp . Count != 0 ) { int val , ind ; val = vp [ vp . Count - 1 ] . first ; ind = vp [ vp . Count - 1 ] . second ; minCnt ++ ; while ( vp . Count != 0 && vp [ vp . Count - 1 ] . first == val && ind - vp [ vp . Count - 1 ] . second + 1 <= K ) vp . RemoveAt ( vp . Count - 1 ) ; } return minCnt ; } public static void Main ( String [ ] args ) { int [ ] arr = { 18 , 11 , 18 , 11 , 18 } ; int K = 3 ; int N = arr . Length ; Console . Write ( minOperations ( arr , N , K ) ) ; } }
Find array sum after incrementing by K adjacent elements of every positive element M times | C # program for the above approach ; Function to find the nearest non - zero element in the left direction ; Stores the index of the first element greater than 0 from the right side ; Traverse the array in the range [ 1 , N ] ; Check arr [ i ] is greater than 0 ; Update the value of L ; Traverse the array from the left side ; Check arr [ i ] is greater than 0 ; Update the value of L ; Update the value of steps [ i ] ; Function to find the nearest non - zero element in the right direction ; Stores the index of the first element greater than 0 from the left side ; Traverse the array from the left side ; Check arr [ i ] is greater than 0 ; Update the value of R ; Traverse the array from the right side ; Check arr [ i ] is greater than 0 ; Update the value of R ; Update the value of steps [ i ] ; Function to find the sum of the array after the given operation M times ; Stores the distance of the nearest non zero element . ; Stores sum of the initial array [ ] arr ; Traverse the array from the left side ; Update the value of sum ; Print the total sum of the array ; Driver Code
using System ; public class GFG { static void nearestLeft ( int [ ] arr , int N , int [ ] steps ) { int L = - N ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] > 0 ) { L = - ( N - i ) ; break ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) { L = i ; } steps [ i ] = i - L ; } } static void nearestRight ( int [ ] arr , int N , int [ ] steps ) { int R = 2 * N ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) { R = N + i ; break ; } } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] > 0 ) { R = i ; } steps [ i ] = Math . Min ( steps [ i ] , R - i ) ; } } static int accumulate ( int [ ] arr , int start , int end ) { int sum = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) sum += arr [ i ] ; return sum ; } static int findSum ( int [ ] arr , int N , int M , int K ) { int [ ] steps = new int [ N ] ; int sum = accumulate ( arr , 0 , N ) ; if ( sum == 0 ) { return 0 ; } nearestLeft ( arr , N , steps ) ; nearestRight ( arr , N , steps ) ; for ( int i = 0 ; i < N ; i ++ ) sum += 2 * K * Math . Max ( 0 , M - steps [ i ] ) ; return sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 1 , 0 , 1 , 0 , 0 } ; int N = arr . Length ; int M = 2 ; int K = 1 ; Console . Write ( findSum ( arr , N , M , K ) ) ; } }
Count of pairs in given range having their ratio equal to ratio of product of their digits | C # program for the above approach ; Function to find the product of digits of the given number ; Function to find the count of pairs ( a , b ) such that a : b = ( product of digits of a ) : ( product of digits of b ) ; Stores the count of the valid pairs ; Loop to iterate over all unordered pairs ( a , b ) ; Stores the product of digits of a ; Stores the product of digits of b ; If x != 0 and y != 0 and a : b is equivalent to x : y ; Increment valid pair count ; Return Answer ; Driver code ; Function Call
using System ; public class GFG { public static int getProduct ( int n ) { int product = 1 ; while ( n != 0 ) { product = product * ( n % 10 ) ; n = n / 10 ; } return product ; } public static int countPairs ( int L , int R ) { int cntPair = 0 ; for ( int a = L ; a <= R ; a ++ ) { for ( int b = a + 1 ; b <= R ; b ++ ) { int x = getProduct ( a ) ; int y = getProduct ( b ) ; if ( x != 0 && y != 0 && ( a * y ) == ( b * x ) ) { cntPair ++ ; } } } return cntPair ; } public static void Main ( string [ ] args ) { int L = 1 ; int R = 100 ; Console . WriteLine ( countPairs ( L , R ) ) ; } }
Find numbers in range [ L , R ] that are coprime with given Array elements | C # program for the above approach ; Function to find all the elements in the range [ L , R ] which are co prime with all array elements ; Store all the divisors of array element in S ; Find the divisors ; Stores all possible required number satisfying the given criteria ; Insert all element [ L , R ] ; Traverse the set ; Remove the multiples of ele ; Print the resultant numbers ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static void elementsCoprimeWithArr ( int [ ] A , int N , int L , int R ) { HashSet < int > S = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int curr_ele = A [ i ] ; for ( int j = 1 ; j <= Math . Sqrt ( curr_ele ) + 1 ; j ++ ) { if ( curr_ele % j == 0 ) { S . Add ( j ) ; S . Add ( curr_ele / j ) ; } } } HashSet < int > store = new HashSet < int > ( ) ; for ( int i = L ; i <= R ; i ++ ) store . Add ( i ) ; S . Remove ( 1 ) ; foreach ( int it in S ) { int ele = it ; int index = 1 ; while ( index * ele <= R ) { store . Remove ( index * ele ) ; index ++ ; } } foreach ( int i in store ) { Console . Write ( i + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 5 } ; int L = 1 , R = 10 ; int N = arr . Length ; elementsCoprimeWithArr ( arr , N , L , R ) ; } }
Maximize matrix sum by flipping the sign of any adjacent pairs | C # program for the above approach ; Function to find the maximum sum of matrix element after flipping the signs of adjacent matrix elements ; Initialize row and column ; Stores the sum of absolute matrix element ; Find the minimum absolute value in the matrix ; Store count of negatives ; Find the smallest absolute value in the matrix ; Increment the count of negative numbers ; Find the absolute sum ; If the count of negatives is even then print the sum ; Subtract minimum absolute element ; Driver Code
using System ; public class GFG { static int maxSum ( int [ , ] matrix ) { int r = matrix . GetLength ( 0 ) ; int c = matrix . GetLength ( 1 ) ; int sum = 0 ; int mini = int . MaxValue ; int count = 0 ; for ( int i = 0 ; i < r ; i ++ ) { for ( int j = 0 ; j < c ; j ++ ) { int k = matrix [ i , j ] ; mini = Math . Min ( mini , Math . Abs ( k ) ) ; if ( k < 0 ) count ++ ; sum += Math . Abs ( k ) ; } } if ( count % 2 == 0 ) { return sum ; } else { return ( sum - 2 * mini ) ; } } public static void Main ( string [ ] args ) { int [ , ] matrix = { { 2 , - 2 } , { - 2 , 2 } } ; Console . Write ( maxSum ( matrix ) ) ; } }
Find the last positive element remaining after repeated subtractions of smallest positive element from all Array elements | C # program for the above approach ; Function to calculate last positive element of the array ; Return the first element if N = 1 ; Stores the greatest and the second greatest element ; Traverse the array A [ ] ; If current element is greater than the greatest element ; If current element is greater than second greatest element ; Return the final answer ; Driver Code
using System ; class GFG { static int lastPositiveElement ( int [ ] arr ) { int N = arr . Length ; if ( N == 1 ) return arr [ 0 ] ; int greatest = - 1 , secondGreatest = - 1 ; for ( int x = 0 ; x < N ; x ++ ) { if ( arr [ x ] >= greatest ) { secondGreatest = greatest ; greatest = arr [ x ] ; } else if ( arr [ x ] >= secondGreatest ) { secondGreatest = arr [ x ] ; } } return greatest - secondGreatest ; } public static void Main ( ) { int [ ] arr = { 3 , 5 , 4 , 7 } ; Console . Write ( lastPositiveElement ( arr ) ) ; } }
Reduce given array by replacing subarrays with values less than K with their sum | C # program for the above approach ; Function to replace all the subarray having values < K with their sum ; Stores the sum of subarray having elements less than K ; Stores the updated array ; Traverse the array ; Update the sum ; Otherwise , update the vector ; Print the result ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void updateArray ( List < int > arr , int K ) { int sum = 0 ; List < int > res = new List < int > ( ) ; for ( int i = 0 ; i < arr . Count ; i ++ ) { if ( arr [ i ] < K ) { sum += arr [ i ] ; } else { if ( sum != 0 ) { res . Add ( sum ) ; } sum = 0 ; res . Add ( arr [ i ] ) ; } } if ( sum != 0 ) res . Add ( sum ) ; foreach ( int it in res ) Console . Write ( it + " ▁ " ) ; } public static void Main ( ) { List < int > arr = new List < int > { 200 , 6 , 36 , 612 , 121 , 66 , 63 , 39 , 668 , 108 } ; int K = 100 ; updateArray ( arr , K ) ; } }
Minimum size of set having either element in range [ 0 , X ] or an odd power of 2 with sum N | C # program for the above approach ; Function to find the highest odd power of 2 in the range [ 0 , N ] ; If P is even , subtract 1 ; Function to find the minimum operations to make N ; If N is odd and X = 0 , then no valid set exist ; Stores the minimum possible size of the valid set ; Loop to subtract highest odd power of 2 while X < N , step 2 ; If N > 0 , then increment the value of answer by 1 ; Return the resultant size of set ; Driver Code
using System ; class GFG { static int highestPowerof2 ( int n ) { int p = ( int ) Math . Floor ( Math . Log ( n ) / Math . Log ( 2.0 ) ) ; if ( p % 2 == 0 ) p -= 1 ; int result = ( int ) ( Math . Pow ( 2 , p ) ) ; return result ; } static int minStep ( int N , int X ) { if ( N % 2 != 0 && X == 0 ) return - 1 ; int size = 0 ; while ( X < N ) { N -= highestPowerof2 ( N ) ; size += 1 ; } if ( N != 0 ) size += 1 ; return size ; } public static void Main ( String [ ] args ) { int N = 11 ; int X = 2 ; Console . Write ( minStep ( N , X ) ) ; } }
Check if all the digits of the given number are same | C # Program to implement the above approach ; Function to check if all the digits in the number N is the same or not ; Find the last digit ; Find the current last digit ; Update the value of N ; If there exists any distinct digit , then return No ; Otherwise , return Yes ; Driver Code
using System ; class GFG { static string checkSameDigits ( int N ) { int digit = N % 10 ; while ( N != 0 ) { int current_digit = N % 10 ; N = N / 10 ; if ( current_digit != digit ) { return " No " ; } } return " Yes " ; } public static void Main ( ) { int N = 222 ; Console . Write ( checkSameDigits ( N ) ) ; } }
Find number formed by K times alternatively reducing X and adding Y to 0 | C # program for the above approach ; Function to find the value obtained after alternatively reducing X and adding Y to 0 total K number of times ; Stores the final result after adding only Y to 0 ; Stores the final number after reducing only X from 0 ; Return the result obtained ; Driver Code
using System ; class GFG { static int positionAfterKJumps ( int X , int Y , int K ) { int addY = Y * ( K / 2 ) ; int reduceX = - 1 * X * ( K / 2 + K % 2 ) ; return addY + reduceX ; } public static void Main ( String [ ] args ) { int X = 2 , Y = 5 , K = 3 ; Console . WriteLine ( positionAfterKJumps ( X , Y , K ) ) ; } }
Maximize the largest number K such that bitwise and of K till N is 0 | C # program for the above approach ; Function to find maximum value of k which makes bitwise AND zero . ; Take k = N initially ; Start traversing from N - 1 till 0 ; Whenever we get AND as 0 we stop and return ; Driver Code
using System ; class GFG { static int findMaxK ( int N ) { int K = N ; for ( int i = N - 1 ; i >= 0 ; i -- ) { K &= i ; if ( K == 0 ) { return i ; } } return 0 ; } public static void Main ( String [ ] args ) { int N = 5 ; Console . Write ( findMaxK ( N ) ) ; } }
Count of unordered pair of indices such that ratio of elements at these indices is same as ratio of indices | C # program for the above approach ; Function of find the count of unordered pairs ( i , j ) in the array such that arr [ j ] / arr [ i ] = j / i . ; Stores the count of valid pairs ; Iterating over all possible pairs ; Check if the pair is valid ; Return answer ; Driver Code ; Function Call
using System ; public class GFG { static int countPairs ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( ( arr [ j ] % arr [ i ] == 0 ) && ( j + 1 ) % ( i + 1 ) == 0 && ( arr [ j ] / arr [ i ] == ( j + 1 ) / ( i + 1 ) ) ) { count ++ ; } } } return count ; } public static void Main ( string [ ] args ) { int [ ] arr = { 5 , - 2 , 4 , 20 , 25 , - 6 } ; int n = arr . Length ; Console . WriteLine ( countPairs ( arr , n ) ) ; } }