codeparrot/xlcost-text-to-code huggingface dataset:
Maximize X such that sum of numbers in range [ 1 , X ] is at most K | C # program for the above approach ; Function to count the elements with sum of the first that many natural numbers less than or equal to K ; If K equals to 0 ; Stores sum of first i natural numbers ; Stores the result ; Iterate over the range [ 1 , N ] ; Increment sum by i ; Is sum is less than or equal to K ; Otherwise , ; Return res ; Driver Code ; Input ; Function call
using System ; class GFG { static int Count ( int N , int K ) { if ( K == 0 ) return 0 ; int sum = 0 ; int res = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { sum += i ; if ( sum <= K ) res ++ ; else break ; } return res ; } public static void Main ( ) { int N = 6 , K = 14 ; Console . WriteLine ( Count ( N , K ) ) ; } }
Remove last occurrence of a word from a given sentence string | C # program for the above approach ; Function to remove last occurrence of W from S ; If M is greater than N ; Iterate while i is greater than or equal to 0 ; Stores if occurrence of W has been found or not ; Iterate over the range [ 0 , M ] ; If S [ j + 1 ] is not equal to W [ j ] ; Mark flag true and break ; If occurrence has been found ; Delete the substring over the range [ i , i + M ] ; Resize the string S ; Return S ; Driver Code ; Input ; Function call
using System ; using System . Collections . Generic ; class GFG { static string removeLastOccurrence ( string S , string W , int N , int M ) { char [ ] ch = S . ToCharArray ( ) ; if ( M > N ) return S ; for ( int i = N - M ; i >= 0 ; i -- ) { int flag = 0 ; for ( int j = 0 ; j < M ; j ++ ) { if ( ch [ j + i ] != W [ j ] ) { flag = 1 ; break ; } } if ( flag == 0 ) { for ( int j = i ; j < N - M ; j ++ ) ch [ j ] = ch [ j + M ] ; Array . Resize ( ref ch , N - M ) ; break ; } } S = string . Concat ( ch ) ; return S ; } public static void Main ( ) { string S = " This ▁ is ▁ GeeksForGeeks " ; string W = " Geeks " ; int N = S . Length ; int M = W . Length ; Console . Write ( removeLastOccurrence ( S , W , N , M ) ) ; } }
Rearrange the Array to maximize the elements which is smaller than both its adjacent elements | C # program for the above approach ; Function to rearrange array such that count of element that are smaller than their adjacent elements is maximum ; Stores the rearranged array ; Stores the maximum count of elements ; Sort the given array ; Place the smallest ( N - 1 ) / 2 elements at odd indices ; Placing the rest of the elements at remaining indices ; If no element of the array has been placed here ; Print the resultant array ; Driver Code ; Input ; Function call
using System ; class GFG { public static void maximumIndices ( int [ ] arr , int N ) { int [ ] temp = new int [ N ] ; int maxIndices = ( N - 1 ) / 2 ; Array . Sort ( arr ) ; for ( int i = 0 ; i < maxIndices ; i ++ ) { temp [ 2 * i + 1 ] = arr [ i ] ; } int j = 0 ; for ( int i = maxIndices ; i < N ; ) { if ( temp [ j ] == 0 ) { temp [ j ] = arr [ i ] ; i ++ ; } j ++ ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( temp [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = 4 ; maximumIndices ( arr , N ) ; } }
Find the repeating element in an Array of size N consisting of first M natural numbers | C ++ program for the above approach ; Function to calculate the repeating character in a given permutation ; variables to store maximum element and sum of the array respectively . ; calculate sum of array ; calculate maximum element in the array ; calculating sum of permutation ; calculate required answer ; Driver code ; Input ; Function call
using System ; public class GFG { public static int repeatingElement ( int [ ] arr , int N ) { int M = 0 , sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; M = Math . Max ( M , arr [ i ] ) ; } int sum1 = M * ( M + 1 ) / 2 ; int ans = ( sum - sum1 ) / ( N - M ) ; return ans ; } public static void Main ( ) { int [ ] arr = { 2 , 6 , 4 , 3 , 1 , 5 , 2 } ; int N = 7 ; Console . WriteLine ( repeatingElement ( arr , N ) ) ; } }
Maximize value at Kth index to create N size array with adjacent difference 1 and sum less than M | C # program for the above approach ; Function to calculate maximum value that can be placed at the Kth index in a distribution in which difference of adjacent elements is less than 1 and total sum of distribution is M . ; variable to store final answer ; variables for binary search ; Binary search ; variable for binary search ; variable to store total sum of array ; number of indices on the left excluding the Kth index ; number of indices on the left excluding the Kth index ; add mid to final sum ; distribution on left side is possible ; sum of distribution on the left side ; sum of distribution on the left side with ( L - mid ) 1 s ; distribution on right side is possible ; sum of distribution on the right side ; sum of distribution on the left side with ( R - mid ) 1 s ; Distribution is valid ; return answer ; Driver code ; Input ; Function call
using System ; class GFG { public static int calculateMax ( int N , int M , int K ) { int ans = - 1 ; int low = 0 , high = M ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int val = 0 ; int L = K - 1 ; int R = N - K ; val += mid ; if ( mid >= L ) { val += ( L ) * ( 2 * mid - L - 1 ) / 2 ; } else { val += mid * ( mid - 1 ) / 2 + ( L - mid ) ; } if ( mid >= R ) { val += ( R ) * ( 2 * mid - R - 1 ) / 2 ; } else { val += mid * ( mid - 1 ) / 2 + ( R - mid ) ; } if ( val <= M ) { ans = Math . Max ( ans , mid ) ; low = mid + 1 ; } else high = mid - 1 ; } return ans ; } static void Main ( ) { int N = 7 , M = 100 , K = 6 ; Console . Write ( calculateMax ( N , M , K ) ) ; } }
Minimum time required to color all edges of a Tree | C # program for the above approach ; Stores the required answer ; Stores the graph ; Function to add edges ; Function to calculate the minimum time required to color all the edges of a tree ; Starting from time = 0 , for all the child edges ; If the edge is not visited yet . ; Time of coloring of the current edge ; If the parent edge has been colored at the same time ; Update the maximum time ; Recursively call the function to its child node ; Driver code ; Function call ; Finally , print the answer
using System ; using System . Collections . Generic ; class GFG { static int ans = 0 ; static List < List < int > > edges = new List < List < int > > ( ) ; static void Add_edge ( int u , int v ) { edges [ u ] . Add ( v ) ; edges [ v ] . Add ( u ) ; } static void minTimeToColor ( int node , int parent , int arrival_time ) { int current_time = 0 ; for ( int x = 0 ; x < edges [ node ] . Count ; x ++ ) { if ( edges [ node ] [ x ] != parent ) { ++ current_time ; if ( current_time == arrival_time ) ++ current_time ; ans = Math . Max ( ans , current_time ) ; minTimeToColor ( edges [ node ] [ x ] , node , current_time ) ; } } } static void Main ( ) { for ( int i = 0 ; i < 100000 ; i ++ ) { edges . Add ( new List < int > ( ) ) ; } int [ , ] A = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } } ; for ( int i = 0 ; i < 3 ; i ++ ) { Add_edge ( A [ i , 0 ] , A [ i , 1 ] ) ; } minTimeToColor ( 1 , - 1 , 0 ) ; Console . WriteLine ( ans ) ; } }
Largest number having both positive and negative values present in the array | C # program for the above approach ; Function to find the largest number k such that both k and - k are present in the array ; Stores the resultant value of K ; Sort the array arr [ ] ; Initialize two variables to use two pointers technique ; Iterate until the value of l is less than r ; Find the value of the sum ; If the sum is 0 , then the resultant element is found ; If the sum is negative ; Otherwise , decrement r ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int largestNum ( List < int > arr ) { int res = 0 ; arr . Sort ( ) ; int l = 0 , r = arr . Count - 1 ; while ( l < r ) { int sum = arr [ l ] + arr [ r ] ; if ( sum == 0 ) { res = Math . Max ( res , Math . Max ( arr [ l ] , arr [ r ] ) ) ; return res ; } else if ( sum < 0 ) { l ++ ; } else { r -- ; } } return res ; } public static void Main ( ) { List < int > arr = new List < int > ( ) { 3 , 2 , - 2 , 5 , - 3 } ; Console . Write ( largestNum ( arr ) ) ; } }
Find elements larger than half of the elements in an array | Set 2 | C # program for the above approach ; Function to find the element that are larger than half of elements of the array ; Find the value of mid ; Stores the maximum element ; Stores the frequency of each array element ; Traverse the array in the reverse order ; Decrement the value of count [ i ] and mid ; Print the current element ; Check if the value of mid is equal to 0 ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void findLarger ( int [ ] arr , int n ) { int mid = ( n + 1 ) / 2 ; int mx = Int32 . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > mx ) mx = arr [ i ] ; } int [ ] count = new int [ mx + 1 ] ; Array . Clear ( count , 0 , mx + 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { count [ arr [ i ] ] ++ ; } for ( int i = mx ; i >= 0 ; i -- ) { while ( count [ i ] > 0 ) { count [ i ] -- ; mid -- ; Console . Write ( i + " ▁ " ) ; if ( mid == 0 ) break ; } if ( mid == 0 ) break ; } } public static void Main ( ) { int [ ] arr = { 10 , 4 , 2 , 8 , 9 } ; int N = arr . Length ; findLarger ( arr , N ) ; } }
Probability of obtaining pairs from two arrays such that element from the first array is smaller than that of the second array | C # program for the above approach ; Function to find probability such that x < y and X belongs to arr1 [ ] and Y belongs to arr2 [ ] ; Stores the length of arr1 ; Stores the length of arr2 ; Stores the result ; Traverse the arr1 [ ] ; Stores the count of elements in arr2 that are greater than arr [ i ] ; Traverse the arr2 [ ] ; If arr2 [ j ] is greater than arr1 [ i ] ; Increment res by y ; Update the value of res ; Return resultant probability ; Driver Code
using System ; class GFG { static double probability ( int [ ] arr1 , int [ ] arr2 ) { int N = arr1 . Length ; int M = arr2 . Length ; double res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int y = 0 ; for ( int j = 0 ; j < M ; j ++ ) { if ( arr2 [ j ] > arr1 [ i ] ) y ++ ; } res += y ; } res = ( double ) res / ( double ) ( N * M ) ; return res ; } static void Main ( ) { int [ ] arr1 = { 5 , 2 , 6 , 1 } ; int [ ] arr2 = { 1 , 6 , 10 , 1 } ; Console . WriteLine ( probability ( arr1 , arr2 ) ) ; } }
Probability of obtaining pairs from two arrays such that element from the first array is smaller than that of the second array | C # program for the above approach ; Function to find probability such that x < y and X belongs to arr1 [ ] & Y belongs to arr2 [ ] ; Stores the length of arr1 ; Stores the length of arr2 ; Stores the result ; Sort the arr2 [ ] in the ascending order ; Traverse the arr1 [ ] ; Stores the count of elements in arr2 that are greater than arr [ i ] ; Increment res by y ; Update the resultant probability ; Return the result ; Function to return the count of elements from the array which are greater than k ; Stores the index of the leftmost element from the array which is at least k ; Finds number of elements greater than k ; If mid element is at least K , then update the value of leftGreater and r ; Update leftGreater ; Update r ; If mid element is at most K , then update the value of l ; Return the count of elements greater than k ; Driver code
using System ; class GFG { static double probability ( int [ ] arr1 , int [ ] arr2 ) { int N = arr1 . Length ; int M = arr2 . Length ; double res = 0 ; Array . Sort ( arr2 ) ; for ( int i = 0 ; i < N ; i ++ ) { int y = countGreater ( arr2 , arr1 [ i ] ) ; res += y ; } res = ( double ) res / ( double ) ( N * M ) ; return res ; } static int countGreater ( int [ ] arr , int k ) { int n = arr . Length ; int l = 0 ; int r = n - 1 ; int leftGreater = n ; while ( l <= r ) { int m = l + ( r - l ) / 2 ; if ( arr [ m ] > k ) { leftGreater = m ; r = m - 1 ; } else l = m + 1 ; } return ( n - leftGreater ) ; } public static void Main ( ) { int [ ] arr1 = { 5 , 2 , 6 , 1 } ; int [ ] arr2 = { 1 , 6 , 10 , 1 } ; Console . Write ( probability ( arr1 , arr2 ) ) ; } }
Minimize cost to cover floor using tiles of dimensions 1 * 1 and 1 * 2 | C # program for the above approach ; Function to find the minimum cost of flooring with the given tiles ; Store the size of the 2d array ; Stores the minimum cost of flooring ; Traverse the 2d array row - wise ; If the current character is ' * ' , then skip it ; Choose the 1 * 1 tile if j is m - 1 ; If consecutive ' . ' are present , the greedily choose tile with the minimum cost ; Otherwise choose the 1 * 1 tile ; Print the minimum cost ; Driver Code
using System ; public class GFG { static void minCost ( char [ , ] arr , int A , int B ) { int n = arr . GetLength ( 0 ) ; int m = arr . GetLength ( 1 ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( arr [ i , j ] == ' * ' ) continue ; if ( j == m - 1 ) ans += A ; else { if ( arr [ i , j + 1 ] == ' . ' ) { ans += Math . Min ( 2 * A , B ) ; j ++ ; } else ans += A ; } } } Console . WriteLine ( ans ) ; } static public void Main ( ) { char [ , ] arr = { { ' . ' , ' . ' , ' * ' } , { ' . ' , ' * ' , ' * ' } } ; int A = 2 , B = 10 ; minCost ( arr , A , B ) ; } }
Count inversions in a permutation of first N natural numbers | C # program for the above approach ; Function to count number of inversions in a permutation of first N natural numbers ; Store array elements in sorted order ; Store the count of inversions ; Traverse the array ; Store the index of first occurrence of arr [ i ] in vector V ; Add count of smaller elements than current element ; Erase current element from vector and go to next index ; Print the result ; Driver code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void countInversions ( int [ ] arr , int n ) { List < int > v = new List < int > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { v . Add ( i ) ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int itr = v . IndexOf ( arr [ i ] ) ; ans += itr ; v . RemoveAt ( itr ) ; } Console . WriteLine ( ans ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 3 , 1 , 5 , 4 } ; int n = arr . Length ; countInversions ( arr , n ) ; } }
Find all possible pairs with given Bitwise OR and Bitwise XOR values | C # code for the above approach ; Function to find pairs with XOR equal to A and OR equal to B ; Iterate from 1 to B ; Check if ( i OR y ) is B ; Driver code
using System ; class GFG { static void findPairs ( int A , int B ) { for ( int i = 1 ; i <= B ; i ++ ) { int y = A ^ i ; if ( y > 0 && ( i y ) == B ) { Console . WriteLine ( i + " ▁ " + y ) ; } } } static void Main ( ) { int A = 8 , B = 10 ; findPairs ( A , B ) ; } }
Count distinct elements from a range of a sorted sequence from a given frequency array | C # program for the above approach ; Function to find the first index with value is at least element ; Update the value of left ; Binary search for the element ; Find the middle element ; Check if the value lies between the elements at index mid - 1 and mid ; Check in the right subarray ; Update the value of left ; Check in left subarray ; Update the value of right ; Function to count the number of distinct elements over the range [ L , R ] in the sorted sequence ; Stores the count of distinct elements ; Create the prefix sum array ; Update the value of count ; Update the value of pref [ i ] ; Calculating the first index of L and R using binary search ; Print the resultant count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int binarysearch ( int [ ] array , int right , int element ) { int left = 1 ; while ( left <= right ) { int mid = ( left + right / 2 ) ; if ( array [ mid ] == element ) { return mid ; } if ( mid - 1 > 0 && array [ mid ] > element && array [ mid - 1 ] < element ) { return mid ; } else if ( array [ mid ] < element ) { left = mid + 1 ; } else { right = mid - 1 ; } } return 1 ; } static void countDistinct ( List < int > arr , int L , int R ) { int count = 0 ; int [ ] pref = new int [ arr . Count + 1 ] ; for ( int i = 1 ; i <= arr . Count ; ++ i ) { count += arr [ i - 1 ] ; pref [ i ] = count ; } int left = binarysearch ( pref , arr . Count + 1 , L ) ; int right = binarysearch ( pref , arr . Count + 1 , R ) ; Console . Write ( right - left + 1 ) ; } public static void Main ( ) { List < int > arr = new List < int > ( ) { 3 , 6 , 7 , 1 , 8 } ; int L = 3 ; int R = 7 ; countDistinct ( arr , L , R ) ; } }
Count substrings having frequency of a character exceeding that of another character in a string | C # program for the above approach ; Function to find the number of substrings having the frequency of ' a ' greater than frequency of ' c ' ; Stores the size of the string ; Stores the resultant count of substrings ; Traverse the given string ; Store the difference between frequency of ' a ' and ' c ' ; Traverse all substrings beginning at index i ; If the frequency of ' a ' is greater than ' c ' ; Print the answer ; Drive Code
using System ; public class GFG { public static void countSubstrings ( string s ) { int n = s . Length ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = 0 ; for ( int j = i ; j < n ; j ++ ) { if ( s [ j ] == ' a ' ) cnt ++ ; else if ( s [ j ] == ' c ' ) cnt -- ; if ( cnt > 0 ) { ans ++ ; } } } Console . WriteLine ( ans ) ; } public static void Main ( string [ ] args ) { string S = " abccaab " ; countSubstrings ( S ) ; } }
Find Unique ID and Domain Name of a Website from a string | C # program for the above approach ; Function to check if a character is alphabet or not ; Function to check if a character is a numeric or not ; Function to find ID and Domain name from a given String ; Stores ID and the domain names ; Stores the words of String S ; Stores the temporary word ; Traverse the String S ; If the current character is space ; Push the curr in words ; Update the curr ; Otherwise ; If curr is not empty ; If length of ss is 10 ; Traverse the String ss ; If j is in the range [ 5 , 9 ) ; If current character is not numeric ; Mark flag 1 ; Otherwise ; If current character is not alphabet ; Mark flag 1 ; If flag is false ; Assign ss to ID ; If subString formed by the first 3 character is " www " and last 3 character is " moc " ; Update the domain name ; Print ID and Domain ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static bool ischar ( char x ) { if ( ( x >= ' A ' && x <= ' Z ' ) || ( x >= ' a ' && x <= ' z ' ) ) { return true ; } return false ; } static bool isnum ( char x ) { if ( x >= '0' && x <= '9' ) return true ; return false ; } static void findIdandDoMain ( String S , int N ) { String ID = " " , Domain = " " ; List < String > words = new List < String > ( ) ; String curr = " " ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ▁ ' ) { words . Add ( curr ) ; curr = " " ; } else { if ( S [ i ] == ' . ' ) { if ( i + 1 == N || ( i + 1 < N && S [ i + 1 ] == ' ▁ ' ) ) continue ; } curr += S [ i ] ; } } if ( curr . Length > 0 ) words . Add ( curr ) ; foreach ( String ss in words ) { if ( ss . Length == 10 ) { bool flag = false ; for ( int j = 0 ; j <= 9 ; j ++ ) { if ( j >= 5 && j < 9 ) { if ( isnum ( ss [ j ] ) == false ) flag = true ; } else { if ( ischar ( ss [ j ] ) == false ) flag = true ; } } if ( ! flag ) { ID = ss ; } } if ( ss . Length > 2 && ss . Substring ( 0 , 3 ) . Equals ( " www " ) && ss . Substring ( ss . Length - 3 ) . Equals ( " com " ) ) { Domain = ss . Substring ( 4 , ss . Length - 4 ) ; } } Console . Write ( " ID ▁ = ▁ " + ID + " STRNEWLINE " ) ; Console . Write ( " Domain ▁ = ▁ " + Domain ) ; } public static void Main ( String [ ] args ) { String S = " We ▁ thank ▁ ABCDE1234F ▁ for ▁ visiting ▁ " + " us ▁ and ▁ buying ▁ products ▁ item ▁ AMZrr @ ! k . ▁ " + " For ▁ more ▁ offers , ▁ visit ▁ us ▁ at ▁ www . amazon . com " ; int N = S . Length ; findIdandDoMain ( S , N ) ; } }
Capacity To Ship Packages Within D Days | C # program for the above approach ; Function to check if the weights can be delivered in D days or not ; Stores the count of days required to ship all the weights if the maximum capacity is mx ; Traverse all the weights ; If total weight is more than the maximum capacity ; If days are more than D , then return false ; Return true for the days < D ; Function to find the least weight capacity of a boat to ship all the weights within D days ; Stores the total weights to be shipped ; Find the sum of weights ; Stores the maximum weight in the array that has to be shipped ; Store the ending value for the search space ; Store the required result ; Perform binary search ; Store the middle value ; If mid can be shipped , then update the result and end value of the search space ; Search for minimum value in the right part ; Print the result ; Driver Code
using System ; class GFG { static bool isValid ( int [ ] weight , int n , int D , int mx ) { int st = 1 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += weight [ i ] ; if ( sum > mx ) { st ++ ; sum = weight [ i ] ; } if ( st > D ) return false ; } return true ; } static void shipWithinDays ( int [ ] weight , int D , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += weight [ i ] ; int s = weight [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { s = Math . Max ( s , weight [ i ] ) ; } int e = sum ; int res = - 1 ; while ( s <= e ) { int mid = s + ( e - s ) / 2 ; if ( isValid ( weight , n , D , mid ) ) { res = mid ; e = mid - 1 ; } else s = mid + 1 ; } Console . WriteLine ( res ) ; } public static void Main ( ) { int [ ] weight = { 9 , 8 , 10 } ; int D = 3 ; int N = weight . Length ; shipWithinDays ( weight , D , N ) ; } }
Find a point whose sum of distances from all given points on a line is K | C # program for the above approach ; Function to find the sum of distances of all points from a given point ; Stores sum of distances ; Traverse the array ; Return the sum ; Function to find such a point having sum of distances of all other points from this point equal to K ; If N is odd keep left as arr [ n / 2 ] else keep left as arr [ n / 2 - 1 ] + 1 ; ; Keep right as arr [ N - 1 ] ; Perform binary search in the right half ; Calculate the mid index of the range ; If temp is equal to K ; Print the value of mid ; If the value of K < temp ; Update right to mid - 1 ; If the value of K > temp ; Update left to mid + 1 ; Update the value of left ; Update the value of right ; Perform binary search on the left half ; Calculate the mid index of the range ; If temp is equal to K ; Print mid ; if K > temp ; Update right to mid - 1 ; If K < temp ; Update left to mid + 1 ; If no such point found ; Driver Code
using System ; class GFG { public static int findSum ( int [ ] arr , int N , int pt ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += Math . Abs ( arr [ i ] - pt ) ; } return sum ; } public static void findPoint ( int [ ] arr , int N , int K ) { int left ; if ( N % 2 != 0 ) { left = arr [ N / 2 ] ; } else { left = arr [ N / 2 - 1 ] + 1 ; } int right = arr [ N - 1 ] ; while ( left <= right ) { int mid = ( left + right ) / 2 ; int temp = findSum ( arr , N , mid ) ; if ( temp == K ) { Console . WriteLine ( mid ) ; return ; } else if ( K < temp ) { right = mid - 1 ; } else { left = mid + 1 ; } } left = arr [ 0 ] ; right = arr [ N / 2 ] - 1 ; while ( left <= right ) { int mid = ( left + right ) / 2 ; int temp = findSum ( arr , N , mid ) ; if ( temp == K ) { Console . WriteLine ( mid ) ; return ; } else if ( K > temp ) { right = mid - 1 ; } else { left = mid + 1 ; } } Console . WriteLine ( " - 1" ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 3 , 6 , 7 , 11 } ; int K = 18 ; int N = arr . Length ; findPoint ( arr , N , K ) ; } }