codeparrot/xlcost-text-to-code huggingface dataset:
Count numbers with difference between number and its digit sum greater than specific value | C # program to count total numbers which have difference with sum of digits greater than specific value ; Utility method to get sum of digits of K ; loop until K is not zero ; method returns count of numbers smaller than N , satisfying difference condition ; binary search while loop ; if difference between number and its sum of digit is smaller than given difference then smallest number will be on left side ; if difference between number and its sum of digit is greater than or equal to given difference then smallest number will be on right side ; return the difference between ' smallest ▁ number ▁ found ' and ' N ' as result ; Driver code
using System ; class Test { static int sumOfDigit ( int K ) { int sod = 0 ; while ( K != 0 ) { sod += K % 10 ; K /= 10 ; } return sod ; } static int totalNumbersWithSpecificDifference ( int N , int diff ) { int low = 1 , high = N ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( mid - sumOfDigit ( mid ) < diff ) low = mid + 1 ; else high = mid - 1 ; } return ( N - high ) ; } public static void Main ( ) { int N = 13 ; int diff = 2 ; Console . Write ( totalNumbersWithSpecificDifference ( N , diff ) ) ; } }
Randomized Binary Search Algorithm | C # program to implement iterative randomized algorithm . ; To generate random number between x and y ie . . [ x , y ] ; A iterative randomized binary search function . It returns location of x in given array arr [ l . . r ] if present , otherwise - 1 ; Here we have defined middle as random index between l and r ie . . [ l , r ] ; Check if x is present at mid ; If x greater , ignore left half ; If x is smaller , ignore right half ; if we reach here , then element was not present ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int getRandom ( int x , int y ) { return ( int ) ( x + new Random ( 10 ) . Next ( 1 ) * 10 % ( y - x + 1 ) ) ; } static int randomizedBinarySearch ( int [ ] arr , int l , int r , int x ) { while ( l <= r ) { int m = getRandom ( l , r ) ; if ( arr [ m ] == x ) return m ; if ( arr [ m ] < x ) l = m + 1 ; else r = m - 1 ; } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 10 , 40 } ; int n = arr . Length ; int x = 10 ; int result = randomizedBinarySearch ( arr , 0 , n - 1 , x ) ; if ( result == - 1 ) Console . Write ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) ; else Console . Write ( " Element ▁ is ▁ present ▁ at ▁ index ▁ { 0 } " , result ) ; } }
Number of buildings facing the sun | C # program to count buildings that can see sunlight . ; Returns count buildings that can see sunlight ; Initialuze result ( Note that first building always sees sunlight ) ; Start traversing element ; If curr_element is maximum or current element is equal , update maximum and increment count ; Driver method
using System ; class GFG { static int countBuildings ( int [ ] arr , int n ) { int count = 1 ; int curr_max = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > curr_max arr [ i ] == curr_max ) { count ++ ; curr_max = arr [ i ] ; } } return count ; } public static void Main ( ) { int [ ] arr = { 7 , 4 , 8 , 2 , 9 } ; Console . Write ( countBuildings ( arr , arr . Length ) ) ; } }
Find index of an extra element present in one sorted array | C # program to find an extra element present in arr1 [ ] ; Returns index of extra element in arr1 [ ] . n is size of arr2 [ ] . Size of arr1 [ ] is n - 1. ; Driver code ; Solve is passed both arrays
using System ; class GfG { static int findExtra ( int [ ] arr1 , int [ ] arr2 , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return i ; return n ; } public static void Main ( ) { int [ ] arr1 = { 2 , 4 , 6 , 8 , 10 , 12 , 13 } ; int [ ] arr2 = { 2 , 4 , 6 , 8 , 10 , 12 } ; int n = arr2 . Length ; Console . Write ( findExtra ( arr1 , arr2 , n ) ) ; } }
Find index of an extra element present in one sorted array | C # program to find an extra element present in arr1 [ ] ; Returns index of extra element in arr1 [ ] . n is size of arr2 [ ] . Size of arr1 [ ] is n - 1. ; Initialize result ; left and right are end points denoting the current range . ; If middle element is same of both arrays , it means that extra element is after mid so we update left to mid + 1 ; If middle element is different of the arrays , it means that the index we are searching for is either mid , or before mid . Hence we update right to mid - 1. ; when right is greater than left our search is complete . ; Driver Code ; Solve is passed both arrays
using System ; class GFG { static int findExtra ( int [ ] arr1 , int [ ] arr2 , int n ) { int index = n ; int left = 0 , right = n - 1 ; while ( left <= right ) { int mid = ( left + right ) / 2 ; if ( arr2 [ mid ] == arr1 [ mid ] ) left = mid + 1 ; else { index = mid ; right = mid - 1 ; } } return index ; } public static void Main ( ) { int [ ] arr1 = { 2 , 4 , 6 , 8 , 10 , 12 , 13 } ; int [ ] arr2 = { 2 , 4 , 6 , 8 , 10 , 12 } ; int n = arr2 . Length ; Console . Write ( findExtra ( arr1 , arr2 , n ) ) ; } }
Make all array elements equal with minimum cost | C # Code to Make all array elements equal with minimum cost ; Utility method to compute cost , when all values of array are made equal to X ; Method to find minimum cost to make all elements equal ; setting limits for ternary search by smallest and largest element ; loop until difference between low and high become less than 3 , because after that mid1 and mid2 will start repeating ; mid1 and mid2 are representative array equal values of search space ; if mid2 point gives more total cost , skip third part ; if mid1 point gives more total cost , skip first part ; computeCost gets optimum cost by sending average of low and high as X ; Driver program to test above function
using System ; class GFG { public static int computeCost ( int [ ] arr , int N , int X ) { int cost = 0 ; for ( int i = 0 ; i < N ; i ++ ) cost += Math . Abs ( arr [ i ] - X ) ; return cost ; } public static int minCostToMakeElementEqual ( int [ ] arr , int N ) { int low , high ; low = high = arr [ 0 ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( low > arr [ i ] ) low = arr [ i ] ; if ( high < arr [ i ] ) high = arr [ i ] ; } while ( ( high - low ) > 2 ) { int mid1 = low + ( high - low ) / 3 ; int mid2 = high - ( high - low ) / 3 ; int cost1 = computeCost ( arr , N , mid1 ) ; int cost2 = computeCost ( arr , N , mid2 ) ; if ( cost1 < cost2 ) high = mid2 ; else low = mid1 ; } return computeCost ( arr , N , ( low + high ) / 2 ) ; } public static void Main ( ) { int [ ] arr = { 1 , 100 , 101 } ; int N = arr . Length ; Console . Write ( minCostToMakeElementEqual ( arr , N ) ) ; } }
Minimum number of jumps required to sort numbers placed on a number line | C # program for the above approach ; Function to find the minimum number of jumps required to sort the array ; Base Case ; Store the required result ; Stores the current position of elements and their respective maximum jump ; Used to check if a position is already taken by another element ; Stores the sorted array a [ ] ; Traverse the array w [ ] & update positions jumps array a [ ] ; Sort the array a [ ] ; Traverse the array a [ ] over the range [ 1 , N - 1 ] ; Store the index of current element and its just smaller element in array w [ ] ; Iterate until current element position is at most its just smaller element position ; Update the position of the current element ; Print the result ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void minJumps ( int [ ] w , int [ ] l , int n ) { if ( n == 1 ) { Console . Write ( 0 ) ; return ; } int ans = 0 ; Dictionary < int , int > pos = new Dictionary < int , int > ( ) ; Dictionary < int , int > jump = new Dictionary < int , int > ( ) ; Dictionary < int , bool > filled = new Dictionary < int , bool > ( ) ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( pos . ContainsKey ( w [ i ] ) ) pos [ w [ i ] ] = i ; else pos . Add ( w [ i ] , i ) ; if ( filled . ContainsKey ( w [ i ] ) ) filled [ i ] = true ; else filled . Add ( i , true ) ; if ( jump . ContainsKey ( w [ i ] ) ) jump [ w [ i ] ] = l [ i ] ; else jump . Add ( w [ i ] , l [ i ] ) ; a [ i ] = w [ i ] ; } Array . Sort ( a ) ; for ( int curr = 1 ; curr < n ; curr ++ ) { int currElementPos = pos [ a [ curr ] ] ; int prevElementPos = pos [ a [ curr - 1 ] ] ; if ( currElementPos > prevElementPos ) continue ; while ( currElementPos <= prevElementPos || filled . ContainsKey ( currElementPos ) && filled [ currElementPos ] ) { currElementPos += jump [ a [ curr ] ] ; ans ++ ; } if ( pos . ContainsKey ( a [ curr ] ) ) pos [ a [ curr ] ] = currElementPos ; else pos . Add ( a [ curr ] , currElementPos ) ; if ( filled . ContainsKey ( currElementPos ) ) filled [ currElementPos ] = true ; else filled . Add ( currElementPos , true ) ; } Console . Write ( ans ) ; } public static void Main ( ) { int [ ] W = { 2 , 1 , 4 , 3 } ; int [ ] L = { 4 , 1 , 2 , 4 } ; int N = W . Length ; minJumps ( W , L , N ) ; } }
Make an array strictly increasing by repeatedly subtracting and adding arr [ i | C # program for the above approach ; Function to check if an array can be made strictly increasing ; Traverse the given array arr [ ] ; Update the value of p , arr [ i ] , and arr [ i - 1 ] ; Traverse the given array ; Check if the array arr [ ] is strictly increasing or not ; Otherwise , array is increasing order , print Yes ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void check ( int [ ] arr , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i - 1 ] >= ( i - 1 ) ) { int p = arr [ i - 1 ] - ( i - 1 ) ; arr [ i ] += p ; arr [ i - 1 ] -= p ; } } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] <= arr [ i - 1 ] ) { Console . Write ( " No " ) ; return ; } } Console . Write ( " Yes " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 5 , 2 , 7 , 6 } ; int N = arr . Length ; check ( arr , N ) ; } }
Minimize maximum array element by splitting array elements into powers of two at most K times | C # program for the above approach ; Function to find the minimum value of the maximum element of the array by splitting at most K array element into perfect powers of 2 ; Sort the array element in the ascending order ; Reverse the array ; If count of 0 is equal to N ; Otherwise , if K is greater than or equal to N ; Otherwise ; Driver code
using System ; class GFG { static void minimumSize ( int [ ] arr , int N , int K ) { Array . Sort ( arr ) ; Array . Reverse ( arr ) ; if ( count ( arr , 0 ) == N ) Console . WriteLine ( 0 ) ; else if ( K >= N ) Console . WriteLine ( 1 ) ; else Console . WriteLine ( arr [ K ] ) ; } static void reverse ( int [ ] a ) { int i , t ; int n = a . Length ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } static int count ( int [ ] a , int n ) { int freq = 0 ; for ( int i = 0 ; i < a . Length ; i ++ ) { if ( a [ i ] == n ) freq ++ ; } return freq ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 4 , 8 , 2 } ; int K = 2 ; int N = arr . Length ; minimumSize ( arr , N , K ) ; } }
Check if removal of a subsequence of non | C # for the above approach ; Function to check if it is possible to sort the array or not ; Stores the index if there are two consecutive 1 's in the array ; Traverse the given array ; Check adjacent same elements having values 1 s ; If there are no two consecutive 1 s , then always remove all the 1 s from array & make it sorted ; If two consecutive 0 ' s ▁ are ▁ ▁ present ▁ after ▁ two ▁ consecutive ▁ ▁ 1' s then array can 't be sorted ; Otherwise , print Yes ; Driver code
using System . IO ; using System ; class GFG { static void isPossibleToSort ( int [ ] arr , int N ) { int idx = - 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == 1 && arr [ i - 1 ] == 1 ) { idx = i ; break ; } } if ( idx == - 1 ) { Console . WriteLine ( " YES " ) ; return ; } for ( int i = idx + 1 ; i < N ; i ++ ) { if ( arr [ i ] == 0 && arr [ i - 1 ] == 0 ) { Console . WriteLine ( " NO " ) ; return ; } } Console . WriteLine ( " YES " ) ; } static void Main ( ) { int [ ] arr = { 1 , 0 , 1 , 0 , 1 , 1 , 0 } ; int N = arr . Length ; isPossibleToSort ( arr , N ) ; } }
Maximize ropes of consecutive length possible by connecting given ropes | C # program for the above approach ; Function to find maximized count of ropes of consecutive length ; Stores the maximum count of ropes of consecutive length ; Sort the ropes by their length ; Traverse the array ; If size of the current rope is less than or equal to current maximum possible size + 1 , update the range to curSize + ropes [ i ] ; If a rope of size ( curSize + 1 ) cannot be obtained ; Driver code ; Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int maxConsecutiveRope ( int [ ] ropes , int N ) { int curSize = 0 ; Array . Sort ( ropes ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ropes [ i ] <= curSize + 1 ) { curSize = curSize + ropes [ i ] ; } else break ; } return curSize ; } public static void Main ( ) { int N = 5 ; int [ ] ropes = { 1 , 2 , 7 , 1 , 1 } ; Console . WriteLine ( maxConsecutiveRope ( ropes , N ) ) ; } }
Sort a string lexicographically by reversing a substring | C # program for the above approach ; Function to find the substring in S required to be reversed ; Stores the size of the string ; Stores the starting point of the substring ; Iterate over the string S while i < N ; Increment the value of i ; Stores the ending index of the substring ; If start <= 0 or i >= N ; If start >= 1 and i <= N ; Return the boolean value ; If start >= 1 ; Return the boolean value ; If i < N ; Return true if S [ start ] is less than or equal to S [ i ] ; Otherwise ; Function to check if it is possible to sort the string or not ; Stores the starting and the ending index of substring ; Stores whether it is possible to sort the substring ; Traverse the range [ 1 , N ] ; If S [ i ] is less than S [ i - 1 ] ; If flag stores true ; If adjust ( S , i , start , end ) return false ; Print - 1 ; Unset the flag ; Otherwise ; Print - 1 ; If start is equal to - 1 ; Update start and end ; Print the value of start ; Driver code
using System ; class GFG { static int i , start , end ; static bool adjust ( string S ) { int N = S . Length ; start = i - 1 ; while ( i < N && S [ i ] < S [ i - 1 ] ) { i ++ ; } end = i - 1 ; if ( start <= 0 && i >= N ) return true ; if ( start >= 1 && i <= N ) { return ( S [ end ] >= S [ start - 1 ] && S [ start ] <= S [ i ] ) ; } if ( start >= 1 ) { return S [ end ] >= S [ start - 1 ] ; } if ( i < N ) { return S [ start ] <= S [ i ] ; } return false ; } static void isPossible ( string S , int N ) { start = - 1 ; end = - 1 ; bool flag = true ; for ( i = 1 ; i < N ; i ++ ) { if ( S [ i ] < S [ i - 1 ] ) { if ( flag ) { if ( adjust ( S ) == false ) { Console . WriteLine ( - 1 ) ; return ; } flag = false ; } else { Console . WriteLine ( - 1 ) ; return ; } } } if ( start == - 1 ) { start = end = 1 ; } Console . WriteLine ( start + " ▁ " + end ) ; } static void Main ( ) { string S = " abcyxuz " ; int N = S . Length ; isPossible ( S , N ) ; } }
Split array into K non | C # program for the above approach ; Function find maximum sum of minimum and maximum of K subsets ; Stores the result ; Sort the array arr [ ] in decreasing order ; Traverse the range [ 0 , K ] ; Sort the array S [ ] in ascending order ; Traverse the array S [ ] ; If S { i ] is 1 ; Stores the index of the minimum element of the i - th subset ; Traverse the array S [ ] ; Update the counter ; Return the resultant sum ; Driver Code
using System ; class GFG { static int maximumSum ( int [ ] arr , int [ ] S , int N , int K ) { int ans = 0 ; Array . Sort ( arr ) ; for ( int i = 0 ; i < N / 2 ; i ++ ) { int temp = arr [ i ] ; arr [ i ] = arr [ N - 1 - i ] ; arr [ N - 1 - i ] = temp ; } for ( int i = 0 ; i < K ; i ++ ) ans += arr [ i ] ; Array . Sort ( S ) ; for ( int i = 0 ; i < K ; i ++ ) { if ( S [ i ] == 1 ) ans += arr [ i ] ; S [ i ] -- ; } int counter = K - 1 ; for ( int i = 0 ; i < K ; i ++ ) { counter = counter + S [ i ] ; if ( S [ i ] != 0 ) ans += arr [ counter ] ; } return ans ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 13 , 7 , 17 } ; int [ ] S = { 1 , 3 } ; int N = arr . Length ; int K = S . Length ; Console . WriteLine ( maximumSum ( arr , S , N , K ) ) ; } }
Check if sequence of removed middle elements from an array is sorted or not | C # program for the above approach ; Function to check if sequence of removed middle elements from an array is sorted or not ; Points to the ends of the array ; Iterate l + 1 < r ; If the element at index L and R is greater than ( L + 1 ) - th and ( R - 1 ) - th elements ; If true , then decrement R by 1 and increment L by 1 ; Otherwise , return false ; If an increasing sequence is formed , then return true ; Driver Code
using System ; class GFG { static bool isSortedArray ( int [ ] arr , int n ) { int l = 0 , r = ( n - 1 ) ; while ( ( l + 1 ) < r ) { if ( arr [ l ] >= Math . Max ( arr [ l + 1 ] , arr [ r - 1 ] ) && arr [ r ] >= Math . Max ( arr [ r - 1 ] , arr [ l + 1 ] ) ) { l ++ ; r -- ; } else { return false ; } } return true ; } public static void Main ( string [ ] args ) { int [ ] arr = { 4 , 3 , 1 , 2 , 5 } ; int N = arr . Length ; if ( isSortedArray ( arr , N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Length of longest strictly increasing subset with each pair of adjacent elements satisfying the condition 2 * A [ i ] â ‰¥ A [ i + 1 ] | C # program for the above approach ; Function to find the length of the longest subset satisfying given conditions ; Sort the array in ascending order ; Stores the starting index and maximum length of the required subset ; Pointer to traverse the array ; Iterate while i < n ; Stores end point of current subset ; Store the length of the current subset ; Continue adding elements to the current subset till the condition satisfies ; Increment length of the current subset ; Increment the pointer j ; If length of the current subset exceeds overall maximum length ; Update maxlen ; Update index ; Increment j ; Update i ; Store the starting index of the required subset in i ; Print the required subset ; Print the array element ; Decrement maxlen ; Increment i ; Driver Code ; Given array ; Store the size of the array
using System ; class GFG { static void maxLenSubset ( int [ ] a , int n ) { Array . Sort ( a ) ; int index = 0 , maxlen = - 1 ; int i = 0 ; while ( i < n ) { int j = i ; int len = 1 ; while ( j < n - 1 ) { if ( 2 * a [ j ] >= a [ j + 1 ] ) { len ++ ; } else break ; j ++ ; } if ( maxlen < len ) { maxlen = len ; index = i ; } j ++ ; i = j ; } i = index ; while ( maxlen > 0 ) { Console . Write ( a [ i ] + " ▁ " ) ; maxlen -- ; i ++ ; } } public static void Main ( string [ ] args ) { int [ ] a = { 3 , 1 , 5 , 11 } ; int n = a . Length ; maxLenSubset ( a , n ) ; } }
Reduce an array to a single element by repeatedly removing larger element from a pair with absolute difference at most K | C # program for the above approach ; Function to check if an array can be reduced to single element by removing maximum element among any chosen pairs ; Sort the array in descending order ; Traverse the array ; If the absolute difference of 2 consecutive array elements is greater than K ; If the array can be reduced to a single element ; Driver Code ; Function Call to check if an array can be reduced to a single element
using System ; class GFG { static void canReduceArray ( int [ ] arr , int N , int K ) { Array . Sort ( arr ) ; int [ ] b = new int [ N ] ; int j = N ; for ( int i = 0 ; i < N ; i ++ ) { b [ j - 1 ] = arr [ i ] ; j = j - 1 ; } for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] - arr [ i + 1 ] > K ) { Console . WriteLine ( " No " ) ; return ; } } Console . WriteLine ( " Yes " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 1 , 3 } ; int N = arr . Length ; int K = 1 ; canReduceArray ( arr , N , K ) ; } }
Lexicographically smallest string possible by merging two sorted strings | C # program for the above approach ; Function to find lexicographically smallest string possible by merging two sorted strings ; Stores length of string s1 ; Stores length of string s2 ; Pointer to beginning of string1 i . e . , s1 ; Pointer to beginning of string2 i . e . , s2 ; Stores the final string ; Traverse the strings ; Append the smaller of the two current characters ; Append the remaining characters of any of the two strings ; Print the final string ; Driver Code ; Function Call
using System ; class GFG { static void mergeStrings ( string s1 , string s2 ) { int len1 = s1 . Length ; int len2 = s2 . Length ; int pntr1 = 0 ; int pntr2 = 0 ; string ans = " " ; while ( pntr1 < len1 && pntr2 < len2 ) { if ( s1 [ pntr1 ] < s2 [ pntr2 ] ) { ans += s1 [ pntr1 ] ; pntr1 ++ ; } else { ans += s2 [ pntr2 ] ; pntr2 ++ ; } } if ( pntr1 < len1 ) { ans += s1 . Substring ( pntr1 , len1 - pntr1 ) ; } if ( pntr2 < len2 ) { ans += s2 . Substring ( pntr2 , len2 - pntr2 ) ; } Console . WriteLine ( ans ) ; } public static void Main ( ) { string S1 = " abdcdtx " ; string S2 = " achilp " ; mergeStrings ( S1 , S2 ) ; } }
Sort a string without altering the position of vowels | C # program for the above approach ; Function to sort the string leaving the vowels unchanged ; Length of string S ; Traverse the string S ; Sort the string temp ; Pointer to traverse the sorted string of consonants ; Traverse the string S ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static void sortStr ( String str ) { char [ ] S = str . ToCharArray ( ) ; int N = S . Length ; List < char > temp = new List < char > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] != ' a ' && S [ i ] != ' e ' && S [ i ] != ' i ' && S [ i ] != ' o ' && S [ i ] != ' u ' ) temp . Add ( S [ i ] ) ; } if ( temp . Count != 0 ) temp . Sort ( ) ; int ptr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] != ' a ' && S [ i ] != ' e ' && S [ i ] != ' i ' && S [ i ] != ' o ' && S [ i ] != ' u ' ) S [ i ] = temp [ ptr ++ ] ; } Console . WriteLine ( new String ( S ) ) ; } public static void Main ( String [ ] args ) { String S = " geeksforgeeks " ; sortStr ( S ) ; } }
Count pairs from an array whose quotient of division of larger number by the smaller number does not exceed K | C # program for the above approach ; Function to count the number having quotient of division of larger element by the smaller element in the pair not exceeding K ; Sort the array in ascending order ; Store the required result ; Traverse the array ; Store the upper bound for the current array element ; Update the number of pairs ; Print the result ; Driver Code ; Given array , arr [ ] ; Store the size of the array
using System ; using System . Collections . Generic ; class GFG { static int upper_bound ( int [ ] arr , int key ) { int l = - 1 , r = arr . Length ; while ( l + 1 < r ) { int m = ( l + r ) >> 1 ; if ( arr [ m ] <= key ) l = m ; else r = m ; } return l + 1 ; } static void countPairs ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int high = upper_bound ( arr , k * arr [ i ] ) ; ans += high - i - 1 ; } Console . WriteLine ( ans ) ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 9 , 5 } ; int n = arr . Length ; int k = 2 ; countPairs ( arr , n , k ) ; } }