codeparrot/xlcost-text-to-code huggingface dataset:
Minimize sum of adjacent difference with removal of one element from array | C # implementation of the approach ; Function to find the element ; Value variable for storing the total value ; Declaring maximum value as zero ; If array contains on element ; Storing the maximum value in temp variable ; Adding the adjacent difference modulus values of removed element . Removing adjacent difference modulus value after removing element ; Returning total value - maximum value ; Driver code
using System ; class GFG { static int findMinRemoval ( int [ ] arr , int n ) { int temp , value = 0 ; int maximum = 0 ; if ( n == 1 ) return 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i != 0 && i != n - 1 ) { value = value + Math . Abs ( arr [ i ] - arr [ i + 1 ] ) ; temp = Math . Abs ( arr [ i ] - arr [ i + 1 ] ) + Math . Abs ( arr [ i ] - arr [ i - 1 ] ) - Math . Abs ( arr [ i - 1 ] - arr [ i + 1 ] ) ; } else if ( i == 0 ) { value = value + Math . Abs ( arr [ i ] - arr [ i + 1 ] ) ; temp = Math . Abs ( arr [ i ] - arr [ i + 1 ] ) ; } else temp = Math . Abs ( arr [ i ] - arr [ i - 1 ] ) ; maximum = Math . Max ( maximum , temp ) ; } return ( value - maximum ) ; } public static void Main ( ) { int [ ] arr = { 1 , 5 , 3 , 2 , 10 } ; int n = arr . Length ; Console . WriteLine ( findMinRemoval ( arr , n ) ) ; } }
Time until distance gets equal to X between two objects moving in opposite direction | C # implementation of the approach ; Function to return the time for which the two policemen can communicate ; time = distance / speed ; Driver code
using System ; class GFG { static double getTime ( int u , int v , int x ) { double speed = u + v ; double time = x / speed ; return time ; } public static void Main ( ) { int u = 3 , v = 3 , x = 3 ; Console . WriteLine ( getTime ( u , v , x ) ) ; } }
Given number of matches played , find number of teams in tournament | C # implementation of the approach ; Function to return the number of teams ; To store both roots of the equation ; sqrt ( b ^ 2 - 4 ac ) ; First root ( - b + sqrt ( b ^ 2 - 4 ac ) ) / 2 a ; Second root ( - b - sqrt ( b ^ 2 - 4 ac ) ) / 2 a ; Return the positive root ; Driver code
using System ; class GFG { static int number_of_teams ( int M ) { int N1 , N2 , sqr ; sqr = ( int ) Math . Sqrt ( 1 + ( 8 * M ) ) ; N1 = ( 1 + sqr ) / 2 ; N2 = ( 1 - sqr ) / 2 ; if ( N1 > 0 ) return N1 ; return N2 ; } public static void Main ( ) { int M = 45 ; Console . WriteLine ( number_of_teams ( M ) ) ; } }
Sum of numbers from 1 to N which are in Lucas Sequence | C # program to find sum of numbers from 1 to N which are in Lucas Sequence ; Function to return the required sum ; Generate lucas number and keep on adding them ; Driver code
using System ; class GFG { static int LucasSum ( int N ) { int sum = 0 ; int a = 2 , b = 1 , c ; sum += a ; while ( b <= N ) { sum += b ; c = a + b ; a = b ; b = c ; } return sum ; } public static void Main ( String [ ] args ) { int N = 20 ; Console . WriteLine ( LucasSum ( N ) ) ; } }
Count of all even numbers in the range [ L , R ] whose sum of digits is divisible by 3 | C # implementation of the above approach ; Function to return the count of required numbers ; Count of numbers in range which are divisible by 6 ; Driver code
using System ; class GFG { static int countNumbers ( int l , int r ) { return ( ( r / 6 ) - ( l - 1 ) / 6 ) ; } public static void Main ( String [ ] args ) { int l = 1000 , r = 6000 ; Console . WriteLine ( countNumbers ( l , r ) ) ; } }
Sum of minimum element of all sub | C # implementation of the above approach ; Function to find the sum of minimum of all subsequence ; Driver code
using System ; class GFG { static int findMinSum ( int [ ] arr , int n ) { int occ = n - 1 , sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] * ( int ) Math . Pow ( 2 , occ ) ; occ -- ; } return sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 5 } ; int n = arr . Length ; Console . WriteLine ( findMinSum ( arr , n ) ) ; } }
Number of trailing zeroes in base B representation of N ! | C # program to find the number of trailing zeroes in base B representation of N ! ; To find the power of a prime p in factorial N ; calculating floor ( n / r ) and adding to the count ; increasing the power of p from 1 to 2 to 3 and so on ; returns all the prime factors of k ; vector to store all the prime factors along with their number of occurrence in factorization of B ; Returns largest power of B that divides N ! ; calculating minimum power of all the prime factors of B ; Driver code
using System ; using System . Collections . Generic ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int findPowerOfP ( int N , int p ) { int count = 0 ; int r = p ; while ( r <= N ) { count += ( N / r ) ; r = r * p ; } return count ; } static List < pair > primeFactorsofB ( int B ) { List < pair > ans = new List < pair > ( ) ; for ( int i = 2 ; B != 1 ; i ++ ) { if ( B % i == 0 ) { int count = 0 ; while ( B % i == 0 ) { B = B / i ; count ++ ; } ans . Add ( new pair ( i , count ) ) ; } } return ans ; } static int largestPowerOfB ( int N , int B ) { List < pair > vec = new List < pair > ( ) ; vec = primeFactorsofB ( B ) ; int ans = int . MaxValue ; for ( int i = 0 ; i < vec . Count ; i ++ ) ans = Math . Min ( ans , findPowerOfP ( N , vec [ i ] . first ) / vec [ i ] . second ) ; return ans ; } public static void Main ( String [ ] args ) { Console . WriteLine ( largestPowerOfB ( 5 , 2 ) ) ; Console . WriteLine ( largestPowerOfB ( 6 , 9 ) ) ; } }
Count numbers in range 1 to N which are divisible by X but not by Y | C # implementation of the above approach ; Function to count total numbers divisible by x but not y in range 1 to N ; Check if Number is divisible by x but not Y if yes , Increment count ; Driver Code
using System ; class GFG { static int countNumbers ( int X , int Y , int N ) { int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( ( i % X == 0 ) && ( i % Y != 0 ) ) count ++ ; } return count ; } public static void Main ( ) { int X = 2 , Y = 3 , N = 10 ; Console . WriteLine ( countNumbers ( X , Y , N ) ) ; } }
Position of a person diametrically opposite on a circle | C # implementation of the approach ; Function to return the required position ; Driver code
using System ; class GFG { static int getPosition ( int n , int m ) { if ( m > ( n / 2 ) ) return ( m - ( n / 2 ) ) ; return ( m + ( n / 2 ) ) ; } static public void Main ( ) { int n = 8 , m = 5 ; Console . WriteLine ( getPosition ( n , m ) ) ; } }
Minimum operations required to modify the array such that parity of adjacent elements is different | C # implementation of the approach ; Function to return the parity of a number ; Function to return the minimum number of operations required ; Operation needs to be performed ; Parity of previous element ; Parity of next element ; Update parity of current element to be other than the parities of the previous and the next number ; Driver Code
using System ; class GFG { static int parity ( int a ) { return a % 3 ; } static int solve ( int [ ] array , int size ) { int operations = 0 ; for ( int i = 0 ; i < size - 1 ; i ++ ) { if ( parity ( array [ i ] ) == parity ( array [ i + 1 ] ) ) { operations ++ ; if ( i + 2 < size ) { int pari1 = parity ( array [ i ] ) ; int pari2 = parity ( array [ i + 2 ] ) ; if ( pari1 == pari2 ) { if ( pari1 == 0 ) array [ i + 1 ] = 1 ; else if ( pari1 == 1 ) array [ i + 1 ] = 0 ; else array [ i + 1 ] = 1 ; } else { if ( ( pari1 == 0 && pari2 == 1 ) || ( pari1 == 1 && pari2 == 0 ) ) array [ i + 1 ] = 2 ; if ( ( pari1 == 1 && pari2 == 2 ) || ( pari1 == 2 && pari2 == 1 ) ) array [ i + 1 ] = 0 ; if ( ( pari1 == 2 && pari2 == 0 ) || ( pari1 == 0 && pari2 == 2 ) ) array [ i + 1 ] = 1 ; } } } } return operations ; } public static void Main ( ) { int [ ] array = { 2 , 1 , 3 , 0 } ; int size = array . Length ; Console . WriteLine ( solve ( array , size ) ) ; } }
Number of submatrices with OR value 1 | C # program to count number of submatrices with OR value 1 ; Function to find required prefix - count for each row from right to left ; Function to find the count of submatrices with OR value 1 ; Array to store prefix count of zeros from right to left for bool array ; Variable to store the count of submatrices with OR value 0 ; Loop to evaluate each column of the prefix matrix uniquely . For each index of a column we will try to determine the number of sub - matrices starting from that index and has all 1 s ; First part of pair will be the value of inserted element . Second part will be the count of the number of elements . Pushed before with a greater value ; Variable to store the number of submatrices with all 0 s ; Return the final answer ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int n = 3 ; class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findPrefixCount ( int [ , ] p_arr , bool [ , ] arr ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = n - 1 ; j >= 0 ; j -- ) { if ( arr [ i , j ] ) continue ; if ( j != n - 1 ) p_arr [ i , j ] += p_arr [ i , j + 1 ] ; p_arr [ i , j ] += ( arr [ i , j ] == false ? 1 : 0 ) ; } } static int matrixOrValueOne ( bool [ , ] arr ) { int [ , ] p_arr = new int [ n , n ] ; findPrefixCount ( p_arr , arr ) ; int count_zero_submatrices = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int i = n - 1 ; Stack < pair > q = new Stack < pair > ( ) ; int to_sum = 0 ; while ( i >= 0 ) { int c = 0 ; while ( q . Count != 0 && q . Peek ( ) . first > p_arr [ i , j ] ) { to_sum -= ( q . Peek ( ) . second + 1 ) * ( q . Peek ( ) . first - p_arr [ i , j ] ) ; c += q . Peek ( ) . second + 1 ; q . Pop ( ) ; } to_sum += p_arr [ i , j ] ; count_zero_submatrices += to_sum ; q . Push ( new pair ( p_arr [ i , j ] , c ) ) ; i -- ; } } return ( n * ( n + 1 ) * n * ( n + 1 ) ) / 4 - count_zero_submatrices ; } public static void Main ( String [ ] args ) { bool [ , ] arr = { { false , false , false } , { false , true , false } , { false , false , false } } ; Console . WriteLine ( matrixOrValueOne ( arr ) ) ; } }
XOR of XORs of all sub | C # program to find the XOR of XOR 's of all submatrices ; Function to find to required XOR value ; Nested loop to find the number of sub - matrix each index belongs to ; Number of ways to choose from top - left elements ; Number of ways to choose from bottom - right elements ; Driver Code
using System ; class GFG { static int submatrixXor ( int [ , ] arr ) { int n = 3 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { int top_left = ( i + 1 ) * ( j + 1 ) ; int bottom_right = ( n - i ) * ( n - j ) ; if ( ( top_left % 2 == 1 ) && ( bottom_right % 2 == 1 ) ) ans = ( ans ^ arr [ i , j ] ) ; } } return ans ; } public static void Main ( ) { int [ , ] arr = { { 6 , 7 , 13 } , { 8 , 3 , 4 } , { 9 , 7 , 6 } } ; Console . Write ( submatrixXor ( arr ) ) ; } }
Find Nth positive number whose digital root is X | C # program to find the N - th number whose digital root is X ; Function to find the digital root of a number ; Function to find the Nth number with digital root as X ; Counter variable to keep the count of valid numbers ; Find digital root ; Check if is required answer or not ; Print the answer if you have found it and breakout of the loop ; Driver Code
using System ; class GFG { static int findDigitalRoot ( int num ) { int sum = int . MaxValue , tempNum = num ; while ( sum >= 10 ) { sum = 0 ; while ( tempNum > 0 ) { sum += tempNum % 10 ; tempNum /= 10 ; } tempNum = sum ; } return sum ; } static void findAnswer ( int X , int N ) { int counter = 0 ; for ( int i = 1 ; counter < N ; ++ i ) { int digitalRoot = findDigitalRoot ( i ) ; if ( digitalRoot == X ) { ++ counter ; } if ( counter == N ) { Console . Write ( i ) ; break ; } } } public static void Main ( String [ ] args ) { int X = 1 , N = 3 ; findAnswer ( X , N ) ; } }
Find Nth positive number whose digital root is X | C # program to find the N - th number with digital root as X ; Function to find the N - th number with digital root as X ; Driver Code
using System ; class GFG { static int findAnswer ( int X , int N ) { return ( N - 1 ) * 9 + X ; } public static void Main ( ) { int X = 7 , N = 43 ; Console . WriteLine ( findAnswer ( X , N ) ) ; } }
Sum of the natural numbers ( up to N ) whose modulo with K yield R | C # implementation of the approach ; Function to return the sum ; If current number gives R as the remainder on dividing by K ; Update the sum ; Return the sum ; Driver code
using System ; class GFG { static long count ( int N , int K , int R ) { long sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i % K == R ) sum += i ; } return sum ; } public static void Main ( ) { int N = 20 , K = 4 , R = 3 ; Console . Write ( count ( N , K , R ) ) ; } }
Longest sub | C # implementation of the approach ; Function to return the length of the longest required sub - sequence ; Find the maximum element from the array ; Insert all lucas numbers below max to the set a and b are first two elements of the Lucas sequence ; If current element is a Lucas number , increment count ; Return the count ; Driver code
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int LucasSequence ( int [ ] arr , int n ) { int max = arr . Max ( ) ; int counter = 0 ; HashSet < int > s = new HashSet < int > ( ) ; int a = 2 , b = 1 ; s . Add ( a ) ; s . Add ( b ) ; while ( b < max ) { int c = a + b ; a = b ; b = c ; s . Add ( b ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( s . Contains ( arr [ i ] ) ) counter ++ ; } return counter ; } static public void Main ( ) { int [ ] arr = { 7 , 11 , 22 , 4 , 2 , 1 , 8 , 9 } ; int n = arr . Length ; Console . WriteLine ( LucasSequence ( arr , n ) ) ; } }
Find the number of solutions to the given equation | C # implementation of the approach ; Function to return the count of valid values of X ; Iterate through all possible sum of digits of X ; Get current value of X for sum of digits i ; Find sum of digits of cr ; If cr is a valid choice for X ; Return the count ; Driver code
using System ; class GFG { static int getCount ( int a , int b , int c ) { int count = 0 ; for ( int i = 1 ; i <= 81 ; i ++ ) { int cr = b * ( int ) Math . Pow ( i , a ) + c ; int tmp = cr ; int sm = 0 ; while ( tmp != 0 ) { sm += tmp % 10 ; tmp /= 10 ; } if ( sm == i && cr < 1e9 ) count ++ ; } return count ; } public static void Main ( ) { int a = 3 , b = 2 , c = 8 ; Console . Write ( getCount ( a , b , c ) ) ; } }
Check if an array of 1 s and 2 s can be divided into 2 parts with equal sum | C # implementation of the above approach : ; Function to check if it is possible to split the array in two parts with equal sum ; Calculate sum of elements and count of 1 's ; If total sum is odd , return False ; If sum of each part is even , return True ; If sum of each part is even but there is atleast one 1 ; Driver Code
using System ; class GFG { static bool isSpiltPossible ( int n , int [ ] a ) { int sum = 0 , c1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; if ( a [ i ] == 1 ) { c1 ++ ; } } if ( sum % 2 != 0 ) return false ; if ( ( sum / 2 ) % 2 == 0 ) return true ; if ( c1 > 0 ) return true ; else return false ; } public static void Main ( ) { int n = 3 ; int [ ] a = { 1 , 1 , 2 } ; if ( isSpiltPossible ( n , a ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Sum of all Submatrices of a Given Matrix | C # program to find the sum of all possible submatrices of a given Matrix ; Function to find the sum of all possible submatrices of a given Matrix ; Varialbe to store the required sum ; Nested loop to find the number of submatrices , each number belongs to ; Number of ways to choose from top - left elements ; Number of ways to choose from bottom - right elements ; Driver Code
using System ; class GFG { static int n = 3 ; static int matrixSum ( int [ , ] arr ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { int top_left = ( i + 1 ) * ( j + 1 ) ; int bottom_right = ( n - i ) * ( n - j ) ; sum += ( top_left * bottom_right * arr [ i , j ] ) ; } } return sum ; } public static void Main ( ) { int [ , ] arr = { { 1 , 1 , 1 } , { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; Console . WriteLine ( matrixSum ( arr ) ) ; } }
Maximum Bitwise AND pair from given range | C # implementation of the approach ; Function to return the maximum bitwise AND possible among all the possible pairs ; Maximum among all ( i , j ) pairs ; Driver code
using System ; class GFG { static int maxAND ( int L , int R ) { int maximum = L & R ; for ( int i = L ; i < R ; i ++ ) for ( int j = i + 1 ; j <= R ; j ++ ) maximum = Math . Max ( maximum , ( i & j ) ) ; return maximum ; } public static void Main ( String [ ] args ) { int L = 1 , R = 632 ; Console . WriteLine ( maxAND ( L , R ) ) ; } }
Split the array into odd number of segments of odd lengths | C # to check whether given array is breakable or not ; Function to check ; Check the result by processing the first & last element and size ; Driver code
using System ; class GFG { static int checkArray ( int [ ] arr , int n ) { return ( ( arr [ 0 ] % 2 ) > 0 && ( arr [ n - 1 ] % 2 ) > 0 && ( n % 2 ) > 0 ) ? 1 : 0 ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; Console . WriteLine ( checkArray ( arr , n ) ) ; } }
Minimum removals to make array sum odd | C # implementation of the approach ; Function to find minimum removals ; Count odd numbers ; If the counter is odd return 0 otherwise return 1 ; Driver Code
using System ; class GfG { static int findCount ( int [ ] arr , int n ) { int countOdd = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 == 1 ) countOdd ++ ; if ( countOdd % 2 == 0 ) return 1 ; else return 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 5 , 1 } ; int n = arr . Length ; Console . WriteLine ( findCount ( arr , n ) ) ; } }
Check whether the number can be made perfect square after adding 1 | C # implementation of the approach ; Function that returns true if x is a perfect square ; Find floating point value of square root of x ; If square root is an integer ; Function that returns true if n is a sunny number ; If ( n + 1 ) is a perfect square ; Driver code
using System ; class GFG { static bool isPerfectSquare ( double x ) { double sr = Math . Sqrt ( x ) ; return ( ( sr - Math . Floor ( sr ) ) == 0 ) ; } static bool isSunnyNum ( int n ) { if ( isPerfectSquare ( n + 1 ) ) return true ; return false ; } public static void Main ( ) { int n = 3 ; if ( isSunnyNum ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Minimum operations to make counts of remainders same in an array | C # implementation of the above approach ; Function to return the minimum number of operations required ; To store modulos values ; If it 's size greater than k  it needed to be decreased ; If it 's size is less than k  it needed to be increased ; Driver code
using System ; using System . Collections . Generic ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int minOperations ( int n , int [ ] a , int m ) { int k = n / m ; List < int > [ ] val = new List < int > [ m ] ; for ( int i = 0 ; i < val . Length ; i ++ ) val [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { val [ a [ i ] % m ] . Add ( i ) ; } long ans = 0 ; List < pair > extra = new List < pair > ( ) ; for ( int i = 0 ; i < 2 * m ; ++ i ) { int cur = i % m ; while ( ( val [ cur ] . Count ) > k ) { int elem = val [ cur ] [ val [ cur ] . Count - 1 ] ; val [ cur ] . RemoveAt ( val [ cur ] . Count - 1 ) ; extra . Add ( new pair ( elem , i ) ) ; } while ( val [ cur ] . Count < k && extra . Count != 0 ) { int elem = extra [ extra . Count - 1 ] . first ; int mmod = extra [ extra . Count - 1 ] . second ; extra . RemoveAt ( extra . Count - 1 ) ; val [ cur ] . Add ( elem ) ; ans += i - mmod ; } } return ( int ) ans ; } public static void Main ( String [ ] args ) { int m = 3 ; int [ ] a = { 3 , 2 , 0 , 6 , 10 , 12 } ; int n = a . Length ; Console . Write ( minOperations ( n , a , m ) ) ; } }
Count primes that can be expressed as sum of two consecutive primes and 1 | C # implementation of the approach ; To check if a number is prime or not ; To store possible numbers ; Function to return all prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to count all possible prime numbers that can be expressed as the sum of two consecutive primes and one ; All possible prime numbers below N ; Driver code
using System ; using System . Collections ; class GfG { static int N = 100005 ; static bool [ ] isprime = new bool [ N ] ; static bool [ ] can = new bool [ N ] ; static ArrayList SieveOfEratosthenes ( ) { for ( int a = 0 ; a < N ; a ++ ) { isprime [ a ] = true ; } for ( int p = 2 ; p * p < N ; p ++ ) { if ( isprime [ p ] == true ) { for ( int i = p * p ; i < N ; i += p ) isprime [ i ] = false ; } } ArrayList primes = new ArrayList ( ) ; for ( int i = 2 ; i < N ; i ++ ) if ( isprime [ i ] ) primes . Add ( i ) ; return primes ; } static int Prime_Numbers ( int n ) { ArrayList primes = SieveOfEratosthenes ( ) ; for ( int i = 0 ; i < primes . Count - 1 ; i ++ ) if ( ( int ) primes [ i ] + ( int ) primes [ i + 1 ] + 1 < N ) can [ ( int ) primes [ i ] + ( int ) primes [ i + 1 ] + 1 ] = true ; int ans = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( can [ i ] && isprime [ i ] == true ) { ans ++ ; } } return ans ; } static void Main ( ) { int n = 50 ; Console . WriteLine ( Prime_Numbers ( n ) ) ; } }
Sum of bitwise AND of all subarrays | C # program to find sum of bitwise AND of all subarrays ; Function to find the sum of bitwise AND of all subarrays ; variable to store the final sum ; multiplier ; variable to check if counting is on ; variable to store the length of the subarrays ; loop to find the contiguous segments ; updating the multiplier ; returning the sum ; Driver Code
using System ; class GFG { static int findAndSum ( int [ ] arr , int n ) { int sum = 0 ; int mul = 1 ; for ( int i = 0 ; i < 30 ; i ++ ) { bool count_on = false ; int l = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & ( 1 << i ) ) > 0 ) if ( count_on ) l ++ ; else { count_on = true ; l ++ ; } else if ( count_on ) { sum += ( ( mul * l * ( l + 1 ) ) / 2 ) ; count_on = false ; l = 0 ; } } if ( count_on ) { sum += ( ( mul * l * ( l + 1 ) ) / 2 ) ; count_on = false ; l = 0 ; } mul *= 2 ; } return sum ; } public static void Main ( ) { int [ ] arr = { 7 , 1 , 1 , 5 } ; int n = arr . Length ; Console . Write ( findAndSum ( arr , n ) ) ; } }
Source to destination in 2 | C # implementation of the approach ; Function that returns true if it is possible to move from source to the destination with the given moves ; Driver code
using System ; class GFG { static bool isPossible ( int Sx , int Sy , int Dx , int Dy , int x , int y ) { if ( Math . Abs ( Sx - Dx ) % x == 0 && Math . Abs ( Sy - Dy ) % y == 0 && ( Math . Abs ( Sx - Dx ) / x ) % 2 == ( Math . Abs ( Sy - Dy ) / y ) % 2 ) return true ; return false ; } static void Main ( ) { int Sx = 0 , Sy = 0 , Dx = 0 , Dy = 0 ; int x = 3 , y = 4 ; if ( isPossible ( Sx , Sy , Dx , Dy , x , y ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Count of pairs ( x , y ) in an array such that x < y | C # implementation of the approach ; Function to return the number of pairs ( x , y ) such that x < y ; To store the number of valid pairs ; If a valid pair is found ; Return the count of valid pairs ; Driver code
using System ; class GFG { static int getPairs ( int [ ] a ) { int count = 0 ; for ( int i = 0 ; i < a . Length ; i ++ ) { for ( int j = 0 ; j < a . Length ; j ++ ) { if ( a [ i ] < a [ j ] ) count ++ ; } } return count ; } public static void Main ( ) { int [ ] a = { 2 , 4 , 3 , 1 } ; Console . WriteLine ( getPairs ( a ) ) ; } }