codeparrot/xlcost-text-to-code huggingface dataset:
Minimum bit swaps between given numbers to make their Bitwise OR equal to Bitwise AND | C # implementation of the above approach ; Function for counting number of set bit ; Function to return the count of minimum operations required ; cnt to count the number of bits set in A and in B ; If odd numbers of total set bits ; one_zero = 1 in A and 0 in B at ith bit similarly for zero_one ; When bitpos is set in B , unset in B ; When bitpos is set in A , unset in B ; Number of moves is half of number pairs of each group ; Odd number pairs ; Driver code ; Input ; Function call to compute the result
using System ; using System . Collections . Generic ; class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } static int minOperations ( int A , int B ) { int cnt1 = 0 , cnt2 = 0 ; cnt1 += countSetBits ( A ) ; cnt2 += countSetBits ( B ) ; if ( ( cnt1 + cnt2 ) % 2 != 0 ) return - 1 ; int oneZero = 0 , zeroOne = 0 ; int ans = 0 ; for ( int i = 0 ; i < Math . Max ( cnt1 , cnt2 ) ; i ++ ) { int bitpos = 1 << i ; if ( ( ( bitpos & A ) == 0 ) && ( bitpos & B ) != 0 ) zeroOne ++ ; if ( ( bitpos & A ) != 0 && ( ( bitpos & B ) == 0 ) ) oneZero ++ ; } ans = ( zeroOne / 2 ) + ( oneZero / 2 ) ; if ( zeroOne % 2 != 0 ) ans += 2 ; return ans ; } public static void Main ( ) { int A = 27 , B = 5 ; Console . Write ( minOperations ( A , B ) ) ; } }
Find the count of Smith Brothers Pairs in a given Array | C # program for the above approach ; array to store all prime less than and equal to MAX ; utility function for sieve of sundaram ; Main logic of Sundaram . ; Since 2 is a prime number ; only primes are selected ; Function to check whether a number is a smith number . ; Find sum the digits of prime factors of n ; add its digits of prime factors to pDigitSum . ; one prime factor is still to be summed up ; Now sum the digits of the original number ; return the answer ; Function to check if X and Y are a Smith Brother Pair ; Function to find pairs from array ; Iterate through all pairs ; Increment count if there is a smith brother pair ; Driver code ; Preprocessing ; Input ; Function call
using System ; using System . Collections . Generic ; class GFG { static int MAX = 10000 ; static List < int > primes = new List < int > ( ) ; static void sieveSundaram ( ) { int [ ] marked = new int [ MAX / 2 + 100 ] ; Array . Clear ( marked , 0 , MAX / 2 + 100 ) ; for ( int i = 1 ; i <= ( int ) ( Math . Sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked [ j ] = 1 ; primes . Add ( 2 ) ; for ( int i = 1 ; i <= ( int ) MAX / 2 ; i ++ ) if ( marked [ i ] == 0 ) primes . Add ( 2 * i + 1 ) ; } static bool isSmith ( int n ) { int original_no = n ; int pDigitSum = 0 ; for ( int i = 0 ; primes [ i ] <= n / 2 ; i ++ ) { while ( n % primes [ i ] == 0 ) { int p = primes [ i ] ; n = n / p ; while ( p > 0 ) { pDigitSum += ( p % 10 ) ; p = p / 10 ; } } } if ( n != 1 && n != original_no ) { while ( n > 0 ) { pDigitSum = pDigitSum + n % 10 ; n = n / 10 ; } } int sumDigits = 0 ; while ( original_no > 0 ) { sumDigits = sumDigits + original_no % 10 ; original_no = original_no / 10 ; } return ( pDigitSum == sumDigits ) ; } static bool isSmithBrotherPair ( int X , int Y ) { return isSmith ( X ) && isSmith ( Y ) && Math . Abs ( X - Y ) == 1 ; } static int countSmithBrotherPairs ( int [ ] A , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N ; j ++ ) { if ( isSmithBrotherPair ( A [ i ] , A [ j ] ) ) count ++ ; } return count ; } public static void Main ( ) { sieveSundaram ( ) ; int [ ] A = { 728 , 729 , 28 , 2964 , 2965 } ; int N = A . Length ; Console . Write ( countSmithBrotherPairs ( A , N ) ) ; } }
Count pair sums that are factors of the sum of the array | C # program for the above approach ; Function to find the number of pairs whose sums divides the sum of array ; Initialize the totalSum and count as 0 ; Calculate the total sum of array ; Generate all possible pairs ; If the sum is a factor of totalSum or not ; Increment count by 1 ; Print the total count obtained ; Driver code
using System ; class GFG { public static void countPairs ( int [ ] arr , int N ) { int count = 0 , totalSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalSum += arr [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( totalSum % ( arr [ i ] + arr [ j ] ) == 0 ) { count += 1 ; } } } Console . WriteLine ( count ) ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; countPairs ( arr , N ) ; } }
Print all numbers that are divisors of N and are co | C # program for the above approach ; Function to print all numbers that are divisors of N and are co - prime with the quotient of their division ; Iterate upto square root of N ; If divisors are equal and gcd is 1 , then print only one of them ; Otherwise print both ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int gcd ( int a , int b ) { return b == 0 ? a : gcd ( b , a % b ) ; } static void printUnitaryDivisors ( int n ) { for ( int i = 1 ; i <= ( int ) Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i && gcd ( i , n / i ) == 1 ) { Console . Write ( i + " ▁ " ) ; } else { if ( gcd ( i , n / i ) == 1 ) { Console . Write ( i + " ▁ " + n / i + " ▁ " ) ; } } } } } public static void Main ( ) { int N = 12 ; printUnitaryDivisors ( N ) ; } }
Maximize K to make given array Palindrome when each element is replaced by its remainder with K | c # program for the above approach ; utility function to calculate the GCD of two numbers ; Function to calculate the largest K , replacing all elements of an array A by their modulus with K , makes A a palindromic array ; check if A is palindrome ; A is not palindromic ; K can be infitely large in this case ; variable to store the largest K that makes A palindromic ; return the required answer ; Driver code ; Input ; Function call
using System ; using System . Collections . Generic ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } static int largestK ( int [ ] A , int N ) { int l = 0 , r = N - 1 , flag = 0 ; while ( l < r ) { if ( A [ l ] != A [ r ] ) { flag = 1 ; break ; } l ++ ; r -- ; } if ( flag == 0 ) return - 1 ; int K = Math . Abs ( A [ 0 ] - A [ N - 1 ] ) ; for ( int i = 1 ; i < N / 2 ; i ++ ) K = gcd ( K , Math . Abs ( A [ i ] - A [ N - i - 1 ] ) ) ; return K ; } public static void Main ( ) { int [ ] A = { 1 , 2 , 3 , 2 , 1 } ; int N = A . Length ; Console . Write ( largestK ( A , N ) ) ; } }
Check if N can be represented as sum of distinct powers of 3 | C # program for the above approach ; Function to try all permutations of distinct powers ; Base Case ; If the distinct powers sum is obtained ; Otherwise ; If current element not selected in power [ ] ; If current element selected in power [ ] ; Return 1 if any permutation found ; Function to check the N can be represented as the sum of the distinct powers of 3 ; Stores the all distincts powers of three to [ 0 , 15 ] ; Function Call ; print ; Driver Code
using System ; class GFG { public static bool PermuteAndFind ( int [ ] power , int idx , int SumSoFar , int target ) { if ( idx == power . Length ) { if ( SumSoFar == target ) return true ; return false ; } bool select = PermuteAndFind ( power , idx + 1 , SumSoFar , target ) ; bool notselect = PermuteAndFind ( power , idx + 1 , SumSoFar + power [ idx ] , target ) ; return ( select notselect ) ; } public static void DistinctPowersOf3 ( int N ) { int [ ] power = new int [ 16 ] ; power [ 0 ] = 1 ; for ( int i = 1 ; i < 16 ; i ++ ) power [ i ] = 3 * power [ i - 1 ] ; bool found = PermuteAndFind ( power , 0 , 0 , N ) ; if ( found == true ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } public static void Main ( ) { int N = 91 ; DistinctPowersOf3 ( N ) ; } }
Generate an N | C # program for the above approach ; Function to print the permutation of size N with absolute difference of adjacent elements in range [ 2 , 4 ] ; If N is less than 4 ; Check if N is even ; Traverse through odd integers ; Update the value of i ; Traverse through even integers ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void getPermutation ( int N ) { if ( N <= 3 ) { Console . Write ( - 1 ) ; return ; } int i = N ; if ( N % 2 == 0 ) i -- ; while ( i >= 1 ) { Console . Write ( i + " ▁ " ) ; i -= 2 ; } Console . Write ( 4 + " ▁ " + 2 + " ▁ " ) ; i = 6 ; while ( i <= N ) { Console . Write ( i + " ▁ " ) ; i += 2 ; } } public static void Main ( ) { int N = 9 ; getPermutation ( N ) ; } }
Count of distinct values till C formed by adding or subtracting A , B , or 0 any number of times | C # program for the above approach ; Function to calculate gcd ; Function to find number of possible final values ; Find the gcd of two numbers ; Calculate number of distinct values ; Return values ; Driver code
using System ; class GFG { static int gcd ( int A , int B ) { if ( B == 0 ) return A ; else return gcd ( B , A % B ) ; } static int getDistinctValues ( int A , int B , int C ) { int g = gcd ( A , B ) ; int num_values = C / g ; return num_values ; } static void Main ( ) { int A = 2 ; int B = 3 ; int C = 10 ; Console . Write ( getDistinctValues ( A , B , C ) ) ; } }
Find array whose elements are XOR of adjacent elements in given array | C # program for the above approach ; Function to reconstruct the array arr [ ] with xor of adjacent elements ; Iterate through each element ; Store the xor of current and next element in arr [ i ] ; Function to print the array ; Driver Code ; Inputs ; Length of the array given ; Function call to reconstruct the arr [ ] ; Function call to print arr [ ]
using System ; class GFG { static int [ ] game_with_number ( int [ ] arr , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { arr [ i ] = arr [ i ] ^ arr [ i + 1 ] ; } return arr ; } static void print ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 10 , 11 , 1 , 2 , 3 } ; int n = arr . Length ; int [ ] new_arr = game_with_number ( arr , n ) ; print ( new_arr , n ) ; } }
Maximize score of same | C # program for the above approach ; Function to calculate the score of same - indexed subarrays selected from the arrays a [ ] and b [ ] ; Traverse the current subarray ; Finding the score without reversing the subarray ; Calculating the score of the reversed subarray ; Return the score of subarray ; Function to find the subarray with the maximum score ; Stores the maximum score and the starting and the ending point of subarray with maximum score ; Traverse all the subarrays ; Store the score of the current subarray ; Update the maximum score ; Print the maximum score ; Driver Code
using System ; class GFG { static int currSubArrayScore ( int [ ] a , int [ ] b , int l , int r ) { int straightScore = 0 ; int reverseScore = 0 ; for ( int i = l ; i <= r ; i ++ ) { straightScore += a [ i ] * b [ i ] ; reverseScore += a [ r - ( i - l ) ] * b [ i ] ; } return Math . Max ( straightScore , reverseScore ) ; } static void maxScoreSubArray ( int [ ] a , int [ ] b , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int currScore = currSubArrayScore ( a , b , i , j ) ; if ( currScore > res ) { res = currScore ; } } } Console . Write ( res ) ; } static public void Main ( ) { int [ ] A = { 13 , 4 , 5 } ; int [ ] B = { 10 , 22 , 2 } ; int N = A . Length ; maxScoreSubArray ( A , B , N ) ; } }
Maximize score of same | C # Program for the above approach ; Function to calculate the score of same - indexed subarrays selected from the arrays a [ ] and b [ ] ; Store the required result ; Iterate in the range [ 0 , N - 1 ] ; Consider the case of odd length subarray ; Update the maximum score ; Expanding the subarray in both directions with equal length so that mid point remains same ; Update both the scores ; Consider the case of even length subarray ; Update both the scores ; Print the result ; Driver Code
using System ; public class GFG { static void maxScoreSubArray ( int [ ] a , int [ ] b , int n ) { int res = 0 ; for ( int mid = 0 ; mid < n ; mid ++ ) { int straightScore = a [ mid ] * b [ mid ] , reverseScore = a [ mid ] * a [ mid ] ; int prev = mid - 1 , next = mid + 1 ; res = Math . Max ( res , Math . Max ( straightScore , reverseScore ) ) ; while ( prev >= 0 && next < n ) { straightScore += ( a [ prev ] * b [ prev ] + a [ next ] * b [ next ] ) ; reverseScore += ( a [ prev ] * b [ next ] + a [ next ] * b [ prev ] ) ; res = Math . Max ( res , Math . Max ( straightScore , reverseScore ) ) ; prev -- ; next ++ ; } straightScore = 0 ; reverseScore = 0 ; prev = mid - 1 ; next = mid ; while ( prev >= 0 && next < n ) { straightScore += ( a [ prev ] * b [ prev ] + a [ next ] * b [ next ] ) ; reverseScore += ( a [ prev ] * b [ next ] + a [ next ] * b [ prev ] ) ; res = Math . Max ( res , Math . Max ( straightScore , reverseScore ) ) ; prev -- ; next ++ ; } } Console . WriteLine ( res ) ; } static public void Main ( ) { int [ ] A = { 13 , 4 , 5 } ; int [ ] B = { 10 , 22 , 2 } ; int N = A . Length ; maxScoreSubArray ( A , B , N ) ; } }
Length of the smallest subarray with maximum possible sum | C # program for the above approach ; Function to find the minimum length of the subarray whose sum is maximum ; Stores the starting and the ending index of the resultant subarray ; Traverse the array until a non - zero element is encountered ; If the array contains only of 0 s ; Traverse the array in reverse until a non - zero element is encountered ; Return the resultant size of the subarray ; Driver code
using System ; class GFG { static int minimumSizeSubarray ( int [ ] arr , int N ) { int i = 0 , j = N - 1 ; while ( i < N && arr [ i ] == 0 ) { i ++ ; } if ( i == N ) return 1 ; while ( j >= 0 && arr [ j ] == 0 ) { j -- ; } return ( j - i + 1 ) ; } public static void Main ( ) { int [ ] arr = { 0 , 2 , 0 , 0 , 12 , 0 , 0 , 0 } ; int N = arr . Length ; Console . Write ( minimumSizeSubarray ( arr , N ) ) ; } }
Count ways to represent N as XOR of distinct integers not exceeding N | C # program for the above approach ; Function to count number of ways to represent N as the Bitwise XOR of distinct integers ; Count number of subsets using above - mentioned formula ; Print the resultant count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void countXorPartition ( int N ) { double a = Math . Pow ( 2 , ( int ) ( N - Math . Log ( N + 1 ) / Math . Log ( 2 ) ) ) ; Console . Write ( a ) ; } public static void Main ( ) { int N = 5 ; countXorPartition ( N ) ; } }
Count numbers less than N whose modulo with A is equal to B | C # program for the above approach ; Function to count numbers less than N , whose modulo with A gives B ; If the value of B at least A ; If the value of B is 0 or not ; Stores the resultant count of numbers less than N ; Update the value of ans ; Print the value of ans ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void countValues ( int A , int B , int C ) { if ( B >= A ) { Console . Write ( 0 ) ; return ; } if ( B == 0 ) { Console . Write ( C / A ) ; return ; } int ans = C / A ; if ( ans * A + B <= C ) { ans ++ ; } Console . Write ( ans ) ; } public static void Main ( ) { int A = 6 , B = 3 , N = 15 ; countValues ( A , B , N ) ; } }
Maximum sum of a subsequence whose Bitwise AND is non | C # program for the above approach ; Function to find the maximum sum of a subsequence whose Bitwise AND is non - zero ; Stores the resultant maximum sum of the subsequence ; Iterate over all the bits ; Stores the sum of array elements whose i - th bit is set ; Traverse the array elements ; If the bit is set , then add its value to the sum ; Update the resultant maximum sum ; Return the maximum sum ; Driver code
using System ; class GFG { static int maximumSum ( int [ ] arr , int N ) { int ans = 0 ; for ( int bit = 0 ; bit < 32 ; bit ++ ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & ( 1 << bit ) ) != 0 ) { sum += arr [ i ] ; } } ans = Math . Max ( ans , sum ) ; } return ans ; } static public void Main ( ) { int [ ] arr = { 5 , 4 , 1 , 7 , 11 } ; int N = arr . Length ; Console . Write ( maximumSum ( arr , N ) ) ; } }
Check if an array can be reduced to at most length K by removal of distinct elements | C # program for the above approach ; Function to check if it is possible to reduce the size of the array to K by removing the set of the distinct array elements ; Stores all distinct elements present in the array arr [ ] ; Traverse the given array ; Insert array elements into the set ; Condition for reducing size of the array to at most K ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void maxCount ( int [ ] arr , int N , int K ) { HashSet < int > st = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { st . Add ( arr [ i ] ) ; } if ( N - st . Count <= K ) { Console . Write ( " Yes " ) ; } else Console . Write ( " No " ) ; } static public void Main ( ) { int [ ] arr = { 2 , 2 , 2 , 3 } ; int K = 3 ; int N = arr . Length ; maxCount ( arr , N , K ) ; } }
Check if a pair of integers from two ranges exists such that their Bitwise XOR exceeds both the ranges | C # program for the above approach ; Function to check if there exists any pair ( P , Q ) whose Bitwise XOR is greater than the Bitwise XOR of X and Y ; Stores the Bitwise XOR of X & Y ; Traverse all possible pairs ; If a pair exists ; If a pair is found ; Driver code
using System . Collections . Generic ; using System ; class GFG { static void findWinner ( int X , int Y ) { int playerA = ( X ^ Y ) ; bool flag = false ; for ( int i = 1 ; i <= X ; i ++ ) { for ( int j = 1 ; j <= Y ; j ++ ) { int val = ( i ^ j ) ; if ( val > playerA ) { flag = true ; break ; } } if ( flag ) { break ; } } if ( flag ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } public static void Main ( String [ ] args ) { int A = 2 , B = 4 ; findWinner ( A , B ) ; } }
Check if a pair of integers from two ranges exists such that their Bitwise XOR exceeds both the ranges | C # program for the above approach ; Function to check if there exists any pair ( P , Q ) whose Bitwise XOR is greater than the Bitwise XOR of X and Y ; Check for the invalid condition ; Otherwise , ; Driver code
using System ; class GFG { static void findWinner ( int X , int Y ) { int first = ( X ^ Y ) ; int second = ( X + Y ) ; if ( first == second ) { Console . Write ( " No " ) ; } else { Console . Write ( " Yes " ) ; } } public static void Main ( String [ ] args ) { int A = 2 , B = 4 ; findWinner ( A , B ) ; } }
Smallest number required to be added to M to make it divisible by N | C # program for the above approach ; Function to find the smallest number greater than or equal to N , that is divisible by k ; Function to find the smallest number required to be added to to M to make it divisible by N ; Stores the smallest multiple of N , greater than or equal to M ; Return the result ; Driver Code ; Given Input ; Function Call
using System ; class GFG { public static int findNum ( int N , int K ) { int rem = ( N + K ) % K ; if ( rem == 0 ) return N ; else return N + K - rem ; } public static int findSmallest ( int M , int N ) { int x = findNum ( M , N ) ; return x - M ; } public static void Main ( ) { int M = 100 , N = 28 ; Console . WriteLine ( findSmallest ( M , N ) ) ; } }
Prefix Factorials of a Prefix Sum Array | C # program for the above approach ; Function to find the factorial of prefix sum at every possible index ; Find the prefix sum array ; Stores the factorial of all the element till the sum of array ; Find the factorial array ; Find the factorials of each array element ; Print the resultant array ; Driver code
using System ; class GFG { static void prefixFactorialArray ( int [ ] A , int N ) { for ( int i = 1 ; i < N ; i ++ ) { A [ i ] += A [ i - 1 ] ; } int [ ] fact = new int [ A [ N - 1 ] + 1 ] ; fact [ 0 ] = 1 ; for ( int i = 1 ; i <= A [ N - 1 ] ; i ++ ) { fact [ i ] = i * fact [ i - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { A [ i ] = fact [ A [ i ] ] ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( A [ i ] + " ▁ " ) ; } } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; prefixFactorialArray ( arr , N ) ; } }
Maximum frequency of any array element possible by at most K increments | C # program for the above approach ; Function to find the maximum possible frequency of a most frequent element after at most K increment operations ; Sort the input array ; Stores the sum of sliding window and the maximum possible frequency of any array element ; Traverse the array ; Add the current element to the window ; If it is not possible to make the array elements in the window equal ; Update the value of sum ; Increment the value of start ; Update the maximum possible frequency ; Print the frequency of the most frequent array element after K increments ; Driver Code
using System ; class GFG { static void maxFrequency ( int [ ] arr , int N , int K ) { Array . Sort ( arr ) ; int start = 0 , end = 0 ; int sum = 0 , res = 0 ; for ( end = 0 ; end < N ; end ++ ) { sum += arr [ end ] ; while ( ( end - start + 1 ) * arr [ end ] - sum > K ) { sum -= arr [ start ] ; start ++ ; } res = Math . Max ( res , end - start + 1 ) ; } Console . WriteLine ( res ) ; } public static void Main ( ) { int [ ] arr = { 1 , 4 , 8 , 13 } ; int N = 4 ; int K = 5 ; maxFrequency ( arr , N , K ) ; } }
Count pairs ( i , j ) from an array such that i < j and arr [ j ] | C # program for the above approach ; Function to count the number of pairs ( i , j ) such that i < j and arr [ i ] - arr [ j ] = X * ( j - i ) ; Stores the count of all such pairs that satisfies the condition . ; Stores count of distinct values of arr [ i ] - x * i ; Iterate over the Map ; Increase count of pairs ; Print the count of such pairs ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void countPairs ( int [ ] arr , int n , int x ) { int count = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! mp . ContainsKey ( arr [ i ] - x * i ) ) mp [ arr [ i ] - x * i ] = 0 ; mp [ arr [ i ] - x * i ] = mp [ arr [ i ] - x * i ] + 1 ; } foreach ( KeyValuePair < int , int > v in mp ) { count += ( v . Value * ( v . Value - 1 ) ) / 2 ; } Console . WriteLine ( count ) ; } public static void Main ( string [ ] args ) { int n = 6 , x = 3 ; int [ ] arr = { 5 , 4 , 8 , 11 , 13 , 16 } ; countPairs ( arr , n , x ) ; } }
Check if a triplet of buildings can be selected such that the third building is taller than the first building and smaller than the second building | C # implementation of the above approach ; Function to check if it is possible to select three buildings that satisfy the given condition ; Stores prefix min array ; Iterate over the range [ 1 , N - 1 ] ; Stores the element from the ending in increasing order ; Iterate until j is greater than or equal to 0 ; If current array element is greater than the prefix min upto j ; Iterate while stack is not empty and top element is less than or equal to preMin [ j ] ; Remove the top element ; If stack is not empty and top element of the stack is less than the current element ; Push the arr [ j ] in stack ; If none of the above case satisfy then return " No " ; Driver code ; Input
using System ; using System . Collections . Generic ; public class GFG { static String recreationalSpot ( int [ ] arr , int N ) { if ( N < 3 ) { return " No " ; } int [ ] preMin = new int [ N ] ; preMin [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { preMin [ i ] = Math . Min ( preMin [ i - 1 ] , arr [ i ] ) ; } Stack < int > stack = new Stack < int > ( ) ; for ( int j = N - 1 ; j >= 0 ; j -- ) { if ( arr [ j ] > preMin [ j ] ) { while ( stack . Count != 0 && stack . Peek ( ) <= preMin [ j ] ) { stack . Pop ( ) ; } if ( stack . Count != 0 && stack . Peek ( ) < arr [ j ] ) { return " Yes " ; } stack . Push ( arr [ j ] ) ; } } return " No " ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 7 , 11 , 5 , 13 , 2 } ; int size = arr . Length ; Console . WriteLine ( recreationalSpot ( arr , size ) ) ; } }