codeparrot/xlcost-text-to-code huggingface dataset:
K | C # program to find k - th term of N merged Arithmetic Progressions ; Function to count and return the number of values less than equal to N present in the set ; Check whether j - th bit is set bit or not ; Function to implement Binary Search to find K - th element ; Find middle index of the array ; Search in the left half ; Search in the right half ; If exactly K elements are present ; Driver Code
using System ; class GFG { static readonly int maxm = 1000000000 ; static int count ( int [ ] v , int n ) { int i , odd = 0 , even = 0 ; int j , d , count ; int t = ( int ) 1 << v . Length ; int size = v . Length ; for ( i = 1 ; i < t ; i ++ ) { d = 1 ; count = 0 ; for ( j = 0 ; j < size ; j ++ ) { if ( ( i & ( 1 << j ) ) > 0 ) { d *= v [ j ] ; count ++ ; } } if ( count % 2 == 1 ) odd += n / d ; else even += n / d ; } return ( odd - even ) ; } static int BinarySearch ( int l , int r , int [ ] v , int key ) { int mid ; while ( r - l > 1 ) { mid = ( l + r ) / 2 ; if ( key <= count ( v , mid ) ) { r = mid ; } else { l = mid ; } } if ( key == count ( v , l ) ) return l ; else return r ; } public static void Main ( String [ ] args ) { int K = 10 ; int [ ] v = { 2 , 3 } ; Console . Write ( BinarySearch ( 1 , maxm , v , K ) + " STRNEWLINE " ) ; } }
Find integral points with minimum distance from given set of integers using BFS | C # implementation of above approach ; Function to find points at minimum distance ; Hash to store points that are encountered ; Queue to store initial set of points ; List to store integral points ; Using bfs to visit nearest points from already visited points ; Get first element from queue ; Check if ( x - 1 ) is not encountered so far ; Update hash with this new element ; Insert ( x - 1 ) into queue ; Push ( x - 1 ) as new element ; Decrement counter by 1 ; Check if ( x + 1 ) is not encountered so far ; Update hash with this new element ; Insert ( x + 1 ) into queue ; Push ( x + 1 ) as new element ; Decrement counter by 1 ; Print result array ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void minDistancePoints ( int [ ] A , int K , int n ) { Dictionary < int , Boolean > m = new Dictionary < int , Boolean > ( ) ; Queue < int > q = new Queue < int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { m . Add ( A [ i ] , true ) ; q . Enqueue ( A [ i ] ) ; } List < int > ans = new List < int > ( ) ; while ( K > 0 ) { int x = q . Dequeue ( ) ; if ( ! m . ContainsKey ( x - 1 ) && K > 0 ) { m . Add ( x - 1 , true ) ; q . Enqueue ( x - 1 ) ; ans . Add ( x - 1 ) ; K -- ; } if ( ! m . ContainsKey ( x + 1 ) && K > 0 ) { m . Add ( x + 1 , true ) ; q . Enqueue ( x + 1 ) ; ans . Add ( x + 1 ) ; K -- ; } } foreach ( int i in ans ) Console . Write ( i + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] A = new int [ ] { - 1 , 4 , 6 } ; int K = 3 ; int n = A . Length ; minDistancePoints ( A , K , n ) ; } }
Leftmost Column with atleast one 1 in a row | C # implementation to find the leftmost column with atleast a 1 in a sorted binary matrix ; Function to search for the leftmost column of the matrix with atleast a 1 in sorted binary matrix ; Loop to iterate over all the rows of the matrix ; Binary Search to find the leftmost occurence of the 1 ; Condition if the column contains the 1 at this position of matrix ; If there is a better solution then update the answer ; Condition if the solution doesn 't exist in the matrix ; Driver Code
using System ; class GFG { static int search ( int [ , ] mat , int n , int m ) { int i , a = int . MaxValue ; for ( i = 0 ; i < n ; i ++ ) { int low = 0 ; int high = m - 1 ; int mid ; int ans = int . MaxValue ; while ( low <= high ) { mid = ( low + high ) / 2 ; if ( mat [ i , mid ] == 1 ) { if ( mid == 0 ) { ans = 0 ; break ; } else if ( mat [ i , mid - 1 ] == 0 ) { ans = mid ; break ; } } if ( mat [ i , mid ] == 1 ) high = mid - 1 ; else low = mid + 1 ; } if ( ans < a ) a = ans ; } if ( a == int . MaxValue ) return - 1 ; return a + 1 ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 0 , 0 , 0 } , { 0 , 0 , 1 } , { 0 , 1 , 1 } } ; Console . Write ( search ( mat , 3 , 3 ) ) ; } }
Largest index for each distinct character in given string with frequency K | C # implementation of the approach ; Function to find largest index for each distinct character occuring exactly K times . ; Finding all characters present in S ; Finding all distinct characters in S ; To store result for each character ; Loop through each lower case English character ; If current character is absent in s ; Getting current character ; Finding count of character ch in S ; To store max Index encountred so far ; Printing required result ; If no such character exists , print - 1 ; Driver code
using System ; using System . Collections . Generic ; class GFG { class pair { public char first ; public int second ; public pair ( char first , int second ) { this . first = first ; this . second = second ; } } static void maxSubString ( char [ ] S , int K , int N ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < N ; ++ i ) { freq [ S [ i ] - ' a ' ] = 1 ; } List < pair > answer = new List < pair > ( ) ; for ( int i = 0 ; i < 26 ; ++ i ) { if ( freq [ i ] == 0 ) continue ; char ch = ( char ) ( i + 97 ) ; int count = 0 ; int index = - 1 ; for ( int j = 0 ; j < N ; ++ j ) { if ( S [ j ] == ch ) count ++ ; if ( count == K ) index = j ; } answer . Add ( new pair ( ch , index ) ) ; } int flag = 0 ; for ( int i = 0 ; i < ( int ) answer . Count ; ++ i ) { if ( answer [ i ] . second > - 1 ) { flag = 1 ; Console . Write ( answer [ i ] . first + " ▁ " + answer [ i ] . second + " STRNEWLINE " ) ; } } if ( flag == 0 ) Console . Write ( " - 1" + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { String S = " cbaabaacbcd " ; int K = 2 ; int N = S . Length ; maxSubString ( S . ToCharArray ( ) , K , N ) ; } }
Smallest number greater than n that can be represented as a sum of distinct power of k | C # implementation of the above approach ; Function to find the smallest number greater than or equal to n represented as the sum of distinct powers of k ; List P to store the base k representation of the number ; If the representation is >= 2 , then this power of k has to be added once again and then increase the next power of k and make the current power 0 ; Reduce all the lower power of k to 0 ; Check if the most significant bit also satisfy the above conditions ; Converting back from the k - nary representation to decimal form . ; Driver code
using System ; class GFG { static void greaterK ( int n , int k ) { int [ ] p = new int [ String . Join ( " " , n ) . Length + 2 ] ; int index = 0 ; int x = n ; int j ; while ( x > 0 ) { p [ index ] = ( int ) ( x % k ) ; x /= k ; index ++ ; } int idx = 0 ; for ( int i = 0 ; i < p . Length - 1 ; ++ i ) { if ( p [ i ] >= 2 ) { p [ i ] = 0 ; p [ i + 1 ] ++ ; for ( j = idx ; j < i ; ++ j ) { p [ j ] = 0 ; } idx = i + 1 ; } if ( p [ i ] == k ) { p [ i ] = 0 ; p [ i + 1 ] ++ ; } } j = p . Length - 1 ; if ( p [ j ] >= 2 ) { p [ index ] = 1 ; index ++ ; } int ans = 0 ; for ( int i = p . Length - 1 ; i >= 0 ; -- i ) { ans = ans * k + p [ i ] ; } Console . Write ( ans + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int n = 29 , k = 7 ; greaterK ( n , k ) ; } }
Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2 | C # program to toggle K - th bit of a number N ; Function that returns true if the string can be balanced ; Count to check the difference between the frequencies of ' ( ' and ' ) ' and count_1 is to find the minimum value of freq ( ' ( ' ) - freq ( ' ) ' ) ; Traverse the given string ; Increase the count ; Decrease the count ; Find the minimum value of freq ( ' ( ' ) - freq ( ' ) ' ) ; If the minimum difference is greater than or equal to - 1 and the overall difference is zero ; Driver code
using System ; class GFG { static Boolean canBeBalanced ( String s , int n ) { int count = 0 , count_1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ( ' ) count ++ ; else count -- ; count_1 = Math . Min ( count_1 , count ) ; } if ( count_1 >= - 1 && count == 0 ) return true ; return false ; } public static void Main ( String [ ] args ) { String s = " ( ) ) ( ) ( " ; int n = s . Length ; if ( canBeBalanced ( s , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Absolute difference between the XOR of Non | C # implementation of the approach ; Function to return max_element from an array ; Function to find the absolute difference between the XOR of non - primes and the XOR of primes in the given array ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store the XOR of primes in X1 and the XOR of non primes in X2 ; The number is prime ; The number is non - prime ; Return the absolute difference ; Driver Code ; Find the absolute difference
using System ; class GFG { static int max_element ( int [ ] arr ) { int max = arr [ 0 ] ; foreach ( int ele in arr ) if ( max < ele ) max = ele ; return max ; } static int calculateDifference ( int [ ] arr , int n ) { int max_val = max_element ( arr ) ; bool [ ] prime = new bool [ max_val + 1 ] ; for ( int index = 0 ; index < max_val + 1 ; index ++ ) prime [ index ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int x1 = 1 , x2 = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) x1 ^= arr [ i ] ; else if ( arr [ i ] != 1 ) x2 ^= arr [ i ] ; } return Math . Abs ( x1 - x2 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 5 , 10 , 15 , 7 } ; int n = arr . Length ; Console . WriteLine ( calculateDifference ( arr , n ) ) ; } }
Count duplicates in a given linked list | C # implementation of the approach ; Representation of node ; Function to insert a node at the beginning ; Function to count the number of duplicate nodes in the linked list ; Starting from the next node ; If some duplicate node is found ; Return the count of duplicate nodes ; Driver code
using System ; class GFG { public class Node { public int data ; public Node next ; } ; static Node insert ( Node head , int item ) { Node temp = new Node ( ) ; temp . data = item ; temp . next = head ; head = temp ; return head ; } static int countNode ( Node head ) { int count = 0 ; while ( head . next != null ) { Node ptr = head . next ; while ( ptr != null ) { if ( head . data == ptr . data ) { count ++ ; break ; } ptr = ptr . next ; } head = head . next ; } return count ; } public static void Main ( String [ ] args ) { Node head = null ; head = insert ( head , 5 ) ; head = insert ( head , 7 ) ; head = insert ( head , 5 ) ; head = insert ( head , 1 ) ; head = insert ( head , 7 ) ; Console . WriteLine ( countNode ( head ) ) ; } }
Check if it is possible to form string B from A under the given constraints | C # implementation of the approach ; Function that returns true if it is possible to form B from A satisfying the given conditions ; List to store the frequency of characters in A ; Vector to store the count of characters used from a particular group of characters ; Store the frequency of the characters ; If a character in B is not present in A ; If count of characters used from a particular group of characters exceeds m ; Update low to the starting index of the next group ; If count of characters used from a particular group of characters has not exceeded m ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool isPossible ( string A , string B , int b , int m ) { List < List < int > > S = new List < List < int > > ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) { S . Add ( new List < int > ( ) ) ; } int [ ] box = new int [ A . Length ] ; for ( int i = 0 ; i < A . Length ; i ++ ) { S [ A [ i ] - ' a ' ] . Add ( i ) ; } int low = 0 ; for ( int i = 0 ; i < B . Length ; i ++ ) { List < int > indexes = S [ B [ i ] - ' a ' ] ; int it = lower_bound ( indexes , low ) ; if ( it == indexes . Count ) return false ; int count = indexes [ it ] / b ; box [ count ] = box [ count ] + 1 ; if ( box [ count ] >= m ) { count ++ ; low = ( count ) * b ; } else low = indexes [ it ] + 1 ; } return true ; } static int lower_bound ( List < int > indexes , int k ) { int low = 0 , high = indexes . Count - 1 ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( indexes [ mid ] < k ) low = mid + 1 ; else high = mid ; } return ( indexes [ low ] < k ) ? low + 1 : low ; } static void Main ( ) { string A = " abcbbcdefxyz " ; string B = " acdxyz " ; int b = 5 ; int m = 2 ; if ( isPossible ( A , B , b , m ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Remove all occurrences of any element for maximum array sum | C # program to convert fractional decimal to binary number ; Find total sum and frequencies of elements ; Find minimum value to be subtracted . ; Find maximum sum after removal ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int maxSumArray ( int [ ] arr , int n ) { int sum = 0 ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( m . ContainsKey ( arr [ i ] ) ) { var val = m [ arr [ i ] ] ; m . Remove ( arr [ i ] ) ; m . Add ( arr [ i ] , val + 1 ) ; } else { m . Add ( arr [ i ] , 1 ) ; } } int minimum = int . MaxValue ; foreach ( KeyValuePair < int , int > x in m ) minimum = Math . Min ( minimum , ( x . Value * x . Key ) ) ; return ( sum - minimum ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 3 , 3 , 2 , 2 , 1 , 1 , 1 } ; int n = arr . Length ; Console . WriteLine ( maxSumArray ( arr , n ) ) ; } }
Given an array and two integers l and r , find the kth largest element in the range [ l , r ] | C # implementation of the approach ; Function to calculate the prefix ; Creating one based indexing ; Initializing and creating prefix array ; Creating a prefix array for every possible value in a given range ; Function to return the kth largest element in the index range [ l , r ] ; Binary searching through the 2d array and only checking the range in which the sub array is a part ; Driver code ; Creating the prefix array for the given array ; Queries ; Perform queries
using System ; class GFG { static int MAX = 1001 ; static int [ , ] prefix = new int [ MAX , MAX ] ; static int [ ] ar = new int [ MAX ] ; static void cal_prefix ( int n , int [ ] arr ) { int i , j ; for ( i = 0 ; i < n ; i ++ ) ar [ i + 1 ] = arr [ i ] ; for ( i = 1 ; i <= 1000 ; i ++ ) { for ( j = 0 ; j <= n ; j ++ ) prefix [ i , j ] = 0 ; for ( j = 1 ; j <= n ; j ++ ) { prefix [ i , j ] = prefix [ i , j - 1 ] + ( int ) ( ar [ j ] <= i ? 1 : 0 ) ; } } } static int ksub ( int l , int r , int n , int k ) { int lo , hi , mid ; lo = 1 ; hi = 1000 ; while ( lo + 1 < hi ) { mid = ( lo + hi ) / 2 ; if ( prefix [ mid , r ] - prefix [ mid , l - 1 ] >= k ) hi = mid ; else lo = mid + 1 ; } if ( prefix [ lo , r ] - prefix [ lo , l - 1 ] >= k ) hi = lo ; return hi ; } static void Main ( ) { int [ ] arr = { 1 , 4 , 2 , 3 , 5 , 7 , 6 } ; int n = arr . Length ; cal_prefix ( n , arr ) ; int [ , ] queries = { { 1 , n , 1 } , { 2 , n - 2 , 2 } , { 3 , n - 1 , 3 } } ; int q = queries . Length / queries . Rank - 1 ; for ( int i = 0 ; i < q ; i ++ ) Console . WriteLine ( ksub ( queries [ i , 0 ] , queries [ i , 1 ] , n , queries [ i , 2 ] ) ) ; } }
Find maximum N such that the sum of square of first N natural numbers is not more than X | C # implementation of the approach ; Function to return the sum of the squares of first N natural numbers ; Function to return the maximum N such that the sum of the squares of first N natural numbers is not more than X ; Iterate through the maxvalue of N ; If the condition fails then return the i - 1 i . e sum of squares till i - 1 ; Driver code
using System ; class GFG { static long squareSum ( long N ) { long sum = ( long ) ( N * ( N + 1 ) * ( 2 * N + 1 ) ) / 6 ; return sum ; } static long findMaxN ( long X ) { long N = ( long ) Math . Sqrt ( X ) ; for ( long i = 1 ; i <= N ; i ++ ) { if ( squareSum ( i ) > X ) return i - 1 ; } return - 1 ; } public static void Main ( string [ ] args ) { long X = 25 ; Console . WriteLine ( findMaxN ( X ) ) ; } }
Remove exactly one element from the array such that max | C # implementation of the above approach ; function to calculate max - min ; There should be at - least two elements ; To store first and second minimums ; To store first and second maximums ; Driver code
using System ; class GFG { static int max_min ( int [ ] a , int n ) { if ( n <= 1 ) return Int32 . MaxValue ; int f_min = a [ 0 ] , s_min = Int32 . MaxValue ; int f_max = a [ 0 ] , s_max = Int32 . MinValue ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] <= f_min ) { s_min = f_min ; f_min = a [ i ] ; } else if ( a [ i ] < s_min ) { s_min = a [ i ] ; } if ( a [ i ] >= f_max ) { s_max = f_max ; f_max = a [ i ] ; } else if ( a [ i ] > s_max ) { s_max = a [ i ] ; } } return Math . Min ( ( f_max - s_min ) , ( s_max - f_min ) ) ; } public static void Main ( ) { int [ ] a = { 1 , 3 , 3 , 7 } ; int n = a . Length ; Console . WriteLine ( max_min ( a , n ) ) ; } }
Minimum in an array which is first decreasing then increasing | C # program to find the smallest number in an array of decrease and increasing numbers ; Function to find the smallest number 's index ; Do a binary search ; Find the mid element ; Check for break point ; Return the index ; Driver Code ; Print the smallest number
using System ; class Solution { static int minimal ( int [ ] a , int n ) { int lo = 0 , hi = n - 1 ; while ( lo < hi ) { int mid = ( lo + hi ) >> 1 ; if ( a [ mid ] < a [ mid + 1 ] ) { hi = mid ; } else { lo = mid + 1 ; } } return lo ; } public static void Main ( ) { int [ ] a = { 8 , 5 , 4 , 3 , 4 , 10 } ; int n = a . Length ; int ind = minimal ( a , n ) ; Console . WriteLine ( a [ ind ] ) ; } }
Leftmost and rightmost indices of the maximum and the minimum element of an array | C # implementation of the approach ; If found new minimum ; If arr [ i ] = min then rightmost index for min will change ; If found new maximum ; If arr [ i ] = max then rightmost index for max will change ; Driver code
using System ; class GFG { static void findIndices ( int [ ] arr , int n ) { int leftMin = 0 , rightMin = 0 ; int leftMax = 0 , rightMax = 0 ; int min = arr [ 0 ] , max = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < min ) { leftMin = rightMin = i ; min = arr [ i ] ; } else if ( arr [ i ] == min ) rightMin = i ; if ( arr [ i ] > max ) { leftMax = rightMax = i ; max = arr [ i ] ; } else if ( arr [ i ] == max ) rightMax = i ; } Console . WriteLine ( " Minimum ▁ left ▁ : ▁ " + leftMin ) ; Console . WriteLine ( " Minimum ▁ right ▁ : ▁ " + rightMin ) ; Console . WriteLine ( " Maximum ▁ left ▁ : ▁ " + leftMax ) ; Console . WriteLine ( " Maximum ▁ right ▁ : ▁ " + rightMax ) ; } public static void Main ( ) { int [ ] arr = { 2 , 1 , 1 , 2 , 1 , 5 , 6 , 5 } ; int n = arr . Length ; findIndices ( arr , n ) ; } }
Find smallest and largest element from square matrix diagonals | C # program to find smallest and largest elements of both diagonals ; Function to find smallest and largest element from principal and secondary diagonal ; take length of square matrix ; declare and initialize variables with appropriate value ; Condition for principal diagonal ; take new smallest value ; take new largest value ; Condition for secondary diagonal ; take new smallest value ; take new largest value ; Driver code ; Declare and initialize 5 X5 matrix
using System ; public class GFG { static void diagonalsMinMax ( int [ , ] mat ) { int n = mat . GetLength ( 0 ) ; if ( n == 0 ) return ; int principalMin = mat [ 0 , 0 ] , principalMax = mat [ 0 , 0 ] ; int secondaryMin = mat [ n - 1 , 0 ] , secondaryMax = mat [ n - 1 , 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { if ( i == j ) { if ( mat [ i , j ] < principalMin ) { principalMin = mat [ i , j ] ; } if ( mat [ i , j ] > principalMax ) { principalMax = mat [ i , j ] ; } } if ( ( i + j ) == ( n - 1 ) ) { if ( mat [ i , j ] < secondaryMin ) { secondaryMin = mat [ i , j ] ; } if ( mat [ i , j ] > secondaryMax ) { secondaryMax = mat [ i , j ] ; } } } } Console . WriteLine ( " Principal ▁ Diagonal ▁ Smallest ▁ Element : ▁ " + principalMin ) ; Console . WriteLine ( " Principal ▁ Diagonal ▁ Greatest ▁ Element ▁ : ▁ " + principalMax ) ; Console . WriteLine ( " Secondary ▁ Diagonal ▁ Smallest ▁ Element : ▁ " + secondaryMin ) ; Console . WriteLine ( " Secondary ▁ Diagonal ▁ Greatest ▁ Element : ▁ " + secondaryMax ) ; } static void Main ( ) { int [ , ] matrix = { { 1 , 2 , 3 , 4 , - 10 } , { 5 , 6 , 7 , 8 , 6 } , { 1 , 2 , 11 , 3 , 4 } , { 5 , 6 , 70 , 5 , 8 } , { 4 , 9 , 7 , 1 , - 5 } } ; diagonalsMinMax ( matrix ) ; } }
Indexed Sequential Search | C # program for Indexed Sequential Search ; Storing element ; Storing the index ; Driver code ; Element to search ; Function call
using System ; class GFG { static void indexedSequentialSearch ( int [ ] arr , int n , int k ) { int [ ] elements = new int [ 20 ] ; int [ ] indices = new int [ 20 ] ; int i ; int j = 0 , ind = 0 , start = 0 , end = 0 , set = 0 ; for ( i = 0 ; i < n ; i += 3 ) { elements [ ind ] = arr [ i ] ; indices [ ind ] = i ; ind ++ ; } if ( k < elements [ 0 ] ) { Console . Write ( " Not ▁ found " ) ; return ; } else { for ( i = 1 ; i <= ind ; i ++ ) if ( k <= elements [ i ] ) { start = indices [ i - 1 ] ; set = 1 ; end = indices [ i ] ; break ; } } if ( set == 0 ) { start = indices [ i - 1 ] ; end = n - 1 ; } for ( i = start ; i <= end ; i ++ ) { if ( k == arr [ i ] ) { j = 1 ; break ; } } if ( j == 1 ) Console . WriteLine ( " Found ▁ at ▁ index ▁ " + i ) ; else Console . WriteLine ( " Not ▁ found " ) ; } public static void Main ( ) { int [ ] arr = { 6 , 7 , 8 , 9 , 10 } ; int n = arr . Length ; int k = 8 ; indexedSequentialSearch ( arr , n , k ) ; } }
Count elements such that there are exactly X elements with values greater than or equal to X | C # implementation of the approach ; Sorting the vector ; Count of numbers which are greater than v [ i ] ; Function to implement upper_bound ( ) ; Driver Code
using System ; class GFG { static int getCount ( int [ ] v , int n ) { Array . Sort ( v ) ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int tmp = n - 1 - upperBound ( v , n , v [ i ] - 1 ) ; if ( tmp == v [ i ] ) cnt ++ ; } return cnt ; } static int upperBound ( int [ ] array , int length , int value ) { int low = 0 ; int high = length ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( value >= array [ mid ] ) { low = mid + 1 ; } else { high = mid ; } } return low ; } public static void Main ( String [ ] args ) { int n = 4 ; int [ ] v = { 1 , 2 , 3 , 4 } ; Console . WriteLine ( getCount ( v , n ) ) ; } }
Number of segments where all elements are greater than X | C # program to count the number of segments in which all elements are greater than X ; Function to count number of segments ; Iterate in the array ; check if array element greater then X or not ; if flag is true ; After iteration complete check for the last segment ; Driver Code
using System ; public class GFG { static int countSegments ( int [ ] a , int n , int x ) { bool flag = false ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > x ) { flag = true ; } else { if ( flag ) count += 1 ; flag = false ; } } if ( flag ) count += 1 ; return count ; } static public void Main ( ) { int [ ] a = { 8 , 25 , 10 , 19 , 19 , 18 , 20 , 11 , 18 } ; int n = a . Length ; int x = 13 ; Console . WriteLine ( countSegments ( a , n , x ) ) ; } }