codeparrot/xlcost-text-to-code huggingface dataset:
Count triplets with sum smaller than a given value | A Simple C # program to count triplets with sum smaller than a given value ; Initialize result ; Fix the first element as A [ i ] ; Fix the second element as A [ j ] ; Now look for the third number ; Driver method to test the above function
using System ; class Test { static int [ ] arr = new int [ ] { 5 , 1 , 3 , 4 , 7 } ; static int countTriplets ( int n , int sum ) { int ans = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { for ( int j = i + 1 ; j < n - 1 ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) if ( arr [ i ] + arr [ j ] + arr [ k ] < sum ) ans ++ ; } } return ans ; } public static void Main ( ) { int sum = 12 ; Console . Write ( countTriplets ( arr . Length , sum ) ) ; } }
Count triplets with sum smaller than a given value | A Simple C # program to count triplets with sum smaller than a given value ; Sort input array ; Initialize result ; Every iteration of loop counts triplet with first element as arr [ i ] . ; Initialize other two elements as corner elements of subarray arr [ j + 1. . k ] ; Use Meet in the Middle concept ; If sum of current triplet is more or equal , move right corner to look for smaller values ; Else move left corner ; This is important . For current i and j , there can be total k - j third elements . ; Driver Code
using System ; class GFG { static int [ ] arr = new int [ ] { 5 , 1 , 3 , 4 , 7 } ; static int countTriplets ( int n , int sum ) { Array . Sort ( arr ) ; int ans = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { int j = i + 1 , k = n - 1 ; while ( j < k ) { if ( arr [ i ] + arr [ j ] + arr [ k ] >= sum ) k -- ; else { ans += ( k - j ) ; j ++ ; } } } return ans ; } public static void Main ( ) { int sum = 12 ; Console . Write ( countTriplets ( arr . Length , sum ) ) ; } }
Number of unique triplets whose XOR is zero | C # program to count the number of unique triplets whose XOR is 0 ; function to count the number of unique triplets whose xor is 0 ; To store values that are present ; stores the count of unique triplets ; traverse for all i , j pairs such that j > i ; xor of a [ i ] and a [ j ] ; if xr of two numbers is present , then increase the count ; returns answer ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int countTriplets ( int [ ] a , int n ) { List < int > s = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) s . Add ( a [ i ] ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int xr = a [ i ] ^ a [ j ] ; if ( s . Exists ( item => item == xr ) && xr != a [ i ] && xr != a [ j ] ) count ++ ; } } return count / 3 ; } static void Main ( ) { int [ ] a = new int [ ] { 1 , 3 , 5 , 10 , 14 , 15 } ; int n = a . Length ; Console . Write ( countTriplets ( a , n ) ) ; } }
Find the Missing Number | C # program to find missing Number ; Function to ind missing number ; program to test above function
using System ; class GFG { static int getMissingNo ( int [ ] a , int n ) { int total = ( n + 1 ) * ( n + 2 ) / 2 ; for ( int i = 0 ; i < n ; i ++ ) total -= a [ i ] ; return total ; } public static void Main ( ) { int [ ] a = { 1 , 2 , 4 , 5 , 6 } ; int miss = getMissingNo ( a , 5 ) ; Console . Write ( miss ) ; } }
Find the Missing Number |  ; a represents the array n : Number of elements in array a ; Driver Code
using System ; class GFG { static int getMissingNo ( int [ ] a , int n ) { int i , total = 1 ; for ( i = 2 ; i <= ( n + 1 ) ; i ++ ) { total += i ; total -= a [ i - 2 ] ; } return total ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 5 } ; Console . Write ( getMissingNo ( arr , ( arr . Length ) ) ) ; } }
Find the Missing Number | C # program to implement the above approach ; Function to find missing number ; Driver code
using System ; class GFG { static int getMissingNo ( int [ ] a , int n ) { int n_elements_sum = ( n * ( n + 1 ) / 2 ) ; int sum = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) sum = sum + a [ i ] ; return ( n_elements_sum - sum ) ; } public static void Main ( ) { int [ ] a = { 1 , 2 , 4 , 5 , 6 } ; int miss = getMissingNo ( a , 5 ) ; Console . Write ( miss ) ; } }
Count number of occurrences ( or frequency ) in a sorted array | C # program to count occurrences of an element ; Returns number of times x occurs in arr [ 0. . n - 1 ] ; driver code
using System ; class GFG { static int countOccurrences ( int [ ] arr , int n , int x ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( x == arr [ i ] ) res ++ ; return res ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 2 , 2 , 2 , 3 , 4 , 7 , 8 , 8 } ; int n = arr . Length ; int x = 2 ; Console . Write ( countOccurrences ( arr , n , x ) ) ; } }
Count number of occurrences ( or frequency ) in a sorted array | C # program to count occurrences of an element ; A recursive binary search function . It returns location of x in given array arr [ l . . r ] is present , otherwise - 1 ; If the element is present at the middle itself ; If element is smaller than mid , then it can only be present in left subarray ; Else the element can only be present in right subarray ; Returns number of times x occurs in arr [ 0. . n - 1 ] ; If element is not present ; Count elements on left side . ; Count elements on right side . ; Driver code
using System ; class GFG { static int binarySearch ( int [ ] arr , int l , int r , int x ) { if ( r < l ) return - 1 ; int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } static int countOccurrences ( int [ ] arr , int n , int x ) { int ind = binarySearch ( arr , 0 , n - 1 , x ) ; if ( ind == - 1 ) return 0 ; int count = 1 ; int left = ind - 1 ; while ( left >= 0 && arr [ left ] == x ) { count ++ ; left -- ; } int right = ind + 1 ; while ( right < n && arr [ right ] == x ) { count ++ ; right ++ ; } return count ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 2 , 2 , 2 , 3 , 4 , 7 , 8 , 8 } ; int n = arr . Length ; int x = 2 ; Console . Write ( countOccurrences ( arr , n , x ) ) ; } }
Find lost element from a duplicated array | C # program to find missing element from same arrays ( except one missing element ) ; Function to find missing element based on binary search approach . arr1 [ ] is of larger size and N is size of it . arr1 [ ] and arr2 [ ] are assumed to be in same order . ; special case , for only element which is missing in second array ; special case , for first element missing ; Initialize current corner points ; loop until lo < hi ; If element at mid indices are equal then go to right subarray ; if lo , hi becomes contiguous , break ; missing element will be at hi index of bigger array ; This function mainly does basic error checking and calls findMissingUtil ; Driver Code
using System ; class GFG { static int findMissingUtil ( int [ ] arr1 , int [ ] arr2 , int N ) { if ( N == 1 ) return arr1 [ 0 ] ; if ( arr1 [ 0 ] != arr2 [ 0 ] ) return arr1 [ 0 ] ; int lo = 0 , hi = N - 1 ; while ( lo < hi ) { int mid = ( lo + hi ) / 2 ; if ( arr1 [ mid ] == arr2 [ mid ] ) lo = mid ; else hi = mid ; if ( lo == hi - 1 ) break ; } return arr1 [ hi ] ; } static void findMissing ( int [ ] arr1 , int [ ] arr2 , int M , int N ) { if ( N == M - 1 ) Console . WriteLine ( " Missing ▁ Element ▁ is ▁ " + findMissingUtil ( arr1 , arr2 , M ) + " STRNEWLINE " ) ; else if ( M == N - 1 ) Console . WriteLine ( " Missing ▁ Element ▁ is ▁ " + findMissingUtil ( arr2 , arr1 , N ) + " STRNEWLINE " ) ; else Console . WriteLine ( " Invalid ▁ Input " ) ; } public static void Main ( ) { int [ ] arr1 = { 1 , 4 , 5 , 7 , 9 } ; int [ ] arr2 = { 4 , 5 , 7 , 9 } ; int M = arr1 . Length ; int N = arr2 . Length ; findMissing ( arr1 , arr2 , M , N ) ; } }
Find lost element from a duplicated array | C # program to find missing element from one array such that it has all elements of other array except one . Elements in two arrays can be in any order . ; This function mainly does XOR of all elements of arr1 [ ] and arr2 [ ] ; Do XOR of all element ; Driver Code
using System ; class GFG { static void findMissing ( int [ ] arr1 , int [ ] arr2 , int M , int N ) { if ( M != N - 1 && N != M - 1 ) { Console . WriteLine ( " Invalid ▁ Input " ) ; return ; } int res = 0 ; for ( int i = 0 ; i < M ; i ++ ) res = res ^ arr1 [ i ] ; for ( int i = 0 ; i < N ; i ++ ) res = res ^ arr2 [ i ] ; Console . WriteLine ( " Missing ▁ element ▁ is ▁ " + res ) ; } public static void Main ( ) { int [ ] arr1 = { 4 , 1 , 5 , 9 , 7 } ; int [ ] arr2 = { 7 , 5 , 9 , 4 } ; int M = arr1 . Length ; int N = arr2 . Length ; findMissing ( arr1 , arr2 , M , N ) ; } }
Find the repeating and the missing | Added 3 new methods | C # program to Find the repeating and missing elements ; Driver program
using System ; class GFG { static void printTwoElements ( int [ ] arr , int size ) { int i ; Console . Write ( " The ▁ repeating ▁ element ▁ is ▁ " ) ; for ( i = 0 ; i < size ; i ++ ) { int abs_val = Math . Abs ( arr [ i ] ) ; if ( arr [ abs_val - 1 ] > 0 ) arr [ abs_val - 1 ] = - arr [ abs_val - 1 ] ; else Console . WriteLine ( abs_val ) ; } Console . Write ( " And ▁ the ▁ missing ▁ element ▁ is ▁ " ) ; for ( i = 0 ; i < size ; i ++ ) { if ( arr [ i ] > 0 ) Console . WriteLine ( i + 1 ) ; } } public static void Main ( ) { int [ ] arr = { 7 , 3 , 4 , 5 , 5 , 6 , 2 } ; int n = arr . Length ; printTwoElements ( arr , n ) ; } }
Find the repeating and the missing | Added 3 new methods | C # program to Find the repeating and missing elements ; The output of this function is stored at * x and * y ; Will hold xor of all elements and numbers from 1 to n ; Will have only single set bit of xor1 ; Get the xor of all array elements ; XOR the previous result with numbers from 1 to n ; Get the rightmost set bit in set_bit_no ; Now divide elements in two sets by comparing rightmost set bit of xor1 with bit at same position in each element . Also , get XORs of two sets . The two XORs are the output elements . The following two for loops serve the purpose ; arr [ i ] belongs to first set ; arr [ i ] belongs to second set ; i belongs to first set ; i belongs to second set ; * x and * y hold the desired output elements ; Driver program
using System ; class GFG { static int x , y ; static void getTwoElements ( int [ ] arr , int n ) { int xor1 ; int set_bit_no ; int i ; x = 0 ; y = 0 ; xor1 = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) xor1 = xor1 ^ arr [ i ] ; for ( i = 1 ; i <= n ; i ++ ) xor1 = xor1 ^ i ; set_bit_no = xor1 & ~ ( xor1 - 1 ) ; for ( i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & set_bit_no ) != 0 ) x = x ^ arr [ i ] ; else y = y ^ arr [ i ] ; } for ( i = 1 ; i <= n ; i ++ ) { if ( ( i & set_bit_no ) != 0 ) x = x ^ i ; else y = y ^ i ; } } public static void Main ( ) { int [ ] arr = { 1 , 3 , 4 , 5 , 1 , 6 , 2 } ; int n = arr . Length ; getTwoElements ( arr , n ) ; Console . Write ( " ▁ The ▁ missing ▁ element ▁ is ▁ " + x + " and ▁ the ▁ " + " repeating ▁ number ▁ is ▁ " + y ) ; } }
Find the repeating and the missing | Added 3 new methods | C # program to find the repeating and missing elements using Maps
using System ; using System . Collections . Generic ; class GFG { public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 3 , 6 , 2 , 1 , 1 } ; Dictionary < int , Boolean > numberMap = new Dictionary < int , Boolean > ( ) ; int max = arr . Length ; foreach ( int i in arr ) { if ( ! numberMap . ContainsKey ( i ) ) { numberMap . Add ( i , true ) ; } else { Console . WriteLine ( " Repeating ▁ = ▁ " + i ) ; } } for ( int i = 1 ; i <= max ; i ++ ) { if ( ! numberMap . ContainsKey ( i ) ) { Console . WriteLine ( " Missing ▁ = ▁ " + i ) ; } } } }
Find four elements that sum to a given value | Set 1 ( n ^ 3 solution ) | C # program for naive solution to print all combination of 4 elements in A [ ] with sum equal to X ; A naive solution to print all combination of 4 elements in A [ ] with sum equal to X ; Fix the first element and find other three ; Fix the second element and find other two ; Fix the third element and find the fourth ; find the fourth ; Driver program to test above functions
using System ; class FindFourElements { void findFourElements ( int [ ] A , int n , int X ) { for ( int i = 0 ; i < n - 3 ; i ++ ) { for ( int j = i + 1 ; j < n - 2 ; j ++ ) { for ( int k = j + 1 ; k < n - 1 ; k ++ ) { for ( int l = k + 1 ; l < n ; l ++ ) { if ( A [ i ] + A [ j ] + A [ k ] + A [ l ] == X ) Console . Write ( A [ i ] + " ▁ " + A [ j ] + " ▁ " + A [ k ] + " ▁ " + A [ l ] ) ; } } } } } public static void Main ( ) { FindFourElements findfour = new FindFourElements ( ) ; int [ ] A = { 10 , 20 , 30 , 40 , 1 , 2 } ; int n = A . Length ; int X = 91 ; findfour . findFourElements ( A , n , X ) ; } }
Find four elements that sum to a given value | Set 2 | A hashing based C # program to find if there are four elements with given sum . ; The function finds four elements with given sum X ; Store sums of all pairs in a hash table ; Traverse through all pairs and search for X - ( current pair sum ) . ; If X - sum is present in hash table , ; Making sure that all elements are distinct array elements and an element is not considered more than once . ; Driver Code ; Function call
using System ; using System . Collections . Generic ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findFourElements ( int [ ] arr , int n , int X ) { Dictionary < int , pair > mp = new Dictionary < int , pair > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( mp . ContainsKey ( arr [ i ] + arr [ j ] ) ) mp [ arr [ i ] + arr [ j ] ] = new pair ( i , j ) ; else mp . Add ( arr [ i ] + arr [ j ] , new pair ( i , j ) ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( mp . ContainsKey ( X - sum ) ) { pair p = mp [ X - sum ] ; if ( p . first != i && p . first != j && p . second != i && p . second != j ) { Console . Write ( arr [ i ] + " , ▁ " + arr [ j ] + " , ▁ " + arr [ p . first ] + " , ▁ " + arr [ p . second ] ) ; return ; } } } } } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 20 , 30 , 40 , 1 , 2 } ; int n = arr . Length ; int X = 91 ; findFourElements ( arr , n , X ) ; } }
Find four elements that sum to a given value | Set 2 | C # program to find four elements with the given sum ; Function to find 4 elements that add up to given sum ; Iterate from 0 to temp . length ; Iterate from 0 to arr . length ; Iterate from i + 1 to arr . length ; Store curr_sum = arr [ i ] + arr [ j ] ; Check if X - curr_sum if present in map ; Store pair having map value X - curr_sum ; Print the output ; Program for two Sum ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static void fourSum ( int X , int [ ] arr , Dictionary < int , Tuple < int , int > > Map , int N ) { int [ ] temp = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) temp [ i ] = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { int curr_sum = arr [ i ] + arr [ j ] ; if ( Map . ContainsKey ( X - curr_sum ) ) { Tuple < int , int > p = Map [ X - curr_sum ] ; if ( p . Item1 != i && p . Item2 != i && p . Item1 != j && p . Item2 != j && temp [ p . Item1 ] == 0 && temp [ p . Item2 ] == 0 && temp [ i ] == 0 && temp [ j ] == 0 ) { Console . Write ( arr [ i ] + " , " + arr [ j ] + " , " + arr [ p . Item1 ] + " , " + arr [ p . Item2 ] ) ; temp [ p . Item2 ] = 1 ; temp [ i ] = 1 ; temp [ j ] = 1 ; break ; } } } } } static Dictionary < int , Tuple < int , int > > twoSum ( int [ ] nums , int N ) { Dictionary < int , Tuple < int , int > > Map = new Dictionary < int , Tuple < int , int > > ( ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { Map [ nums [ i ] + nums [ j ] ] = new Tuple < int , int > ( i , j ) ; } } return Map ; } static void Main ( ) { int [ ] arr = { 10 , 20 , 30 , 40 , 1 , 2 } ; int n = arr . Length ; int X = 91 ; Dictionary < int , Tuple < int , int > > Map = twoSum ( arr , n ) ; fourSum ( X , arr , Map , n ) ; } }
Search an element in an array where difference between adjacent elements is 1 | C # program to search an element in an array where difference between all elements is 1 ; in arr [ 0. . n - 1 ] ; Traverse the given array starting from leftmost element ; If x is found at index i ; Jump the difference between current array element and x ; Driver code
using System ; public class GFG { static int search ( int [ ] arr , int n , int x ) { int i = 0 ; while ( i < n ) { if ( arr [ i ] == x ) return i ; i = i + Math . Abs ( arr [ i ] - x ) ; } Console . WriteLine ( " number ▁ is ▁ not " + " ▁ present ! " ) ; return - 1 ; } public static void Main ( ) { int [ ] arr = { 8 , 7 , 6 , 7 , 6 , 5 , 4 , 3 , 2 , 3 , 4 , 3 } ; int n = arr . Length ; int x = 3 ; Console . WriteLine ( " Element ▁ " + x + " ▁ is ▁ present ▁ at ▁ index ▁ " + search ( arr , n , 3 ) ) ; } }
Third largest element in an array of distinct elements | C # program to find third Largest element in an array of distinct elements ; There should be atleast three elements ; Find first largest element ; Find second largest element ; Find third largest element ; Driver code
using System ; class GFG { static void thirdLargest ( int [ ] arr , int arr_size ) { if ( arr_size < 3 ) { Console . Write ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } int first = arr [ 0 ] ; for ( int i = 1 ; i < arr_size ; i ++ ) if ( arr [ i ] > first ) first = arr [ i ] ; int second = - int . MaxValue ; for ( int i = 0 ; i < arr_size ; i ++ ) if ( arr [ i ] > second && arr [ i ] < first ) second = arr [ i ] ; int third = - int . MaxValue ; for ( int i = 0 ; i < arr_size ; i ++ ) if ( arr [ i ] > third && arr [ i ] < second ) third = arr [ i ] ; Console . Write ( " The ▁ third ▁ Largest ▁ " + " element ▁ is ▁ " + third ) ; } public static void Main ( ) { int [ ] arr = { 12 , 13 , 1 , 10 , 34 , 16 } ; int n = arr . Length ; thirdLargest ( arr , n ) ; } }
Third largest element in an array of distinct elements | C # program to find third Largest element in an array ; There should be atleast three elements ; Initialize first , second and third Largest element ; Traverse array elements to find the third Largest ; If current element is greater than first , then update first , second and third ; If arr [ i ] is in between first and second ; If arr [ i ] is in between second and third ; Driver program to test above function
using System ; class GFG { static void thirdLargest ( int [ ] arr , int arr_size ) { if ( arr_size < 3 ) { Console . Write ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } int first = arr [ 0 ] , second = int . MinValue , third = int . MinValue ; for ( int i = 1 ; i < arr_size ; i ++ ) { if ( arr [ i ] > first ) { third = second ; second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) { third = second ; second = arr [ i ] ; } else if ( arr [ i ] > third ) { third = arr [ i ] ; } } Console . Write ( " The ▁ third ▁ Largest ▁ element ▁ is ▁ " + third ) ; } public static void Main ( ) { int [ ] arr = { 12 , 13 , 1 , 10 , 34 , 16 } ; int n = arr . Length ; thirdLargest ( arr , n ) ; } }
Check if there exist two elements in an array whose sum is equal to the sum of rest of the array | C # program to find whether two elements exist whose sum is equal to sum of rest of the elements . ; Function to check whether two elements exist whose sum is equal to sum of rest of the elements . ; Find sum of whole array ; If sum of array is not even than we can not divide it into two part ; For each element arr [ i ] , see if there is another element with vaalue sum - arr [ i ] ; If element exist than return the pair ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool checkPair ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } if ( sum % 2 != 0 ) { return false ; } sum = sum / 2 ; HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int val = sum - arr [ i ] ; if ( s . Contains ( val ) ) { Console . Write ( " Pair ▁ elements ▁ are ▁ { 0 } ▁ and ▁ { 1 } STRNEWLINE " , arr [ i ] , val ) ; return true ; } s . Add ( arr [ i ] ) ; } return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 11 , 5 , 1 , 4 , 7 } ; int n = arr . Length ; if ( checkPair ( arr , n ) == false ) { Console . Write ( " No ▁ pair ▁ found " ) ; } } }
Search an element in an unsorted array using minimum number of comparisons | C # implementation to search an element in the unsorted array using minimum number of comparisons ; Function to search an element in minimum number of comparisons ; 1 st comparison ; no termination condition and thus no comparison ; this would be executed at - most n times and therefore at - most n comparisons ; replace arr [ n - 1 ] with its actual element as in original ' arr [ ] ' ; if ' x ' is found before the ' ( n - 1 ) th ' index , then it is present in the array final comparison ; else not present in the array ; driver program
using System ; class GFG { static String search ( int [ ] arr , int n , int x ) { if ( arr [ n - 1 ] == x ) return " Found " ; int backup = arr [ n - 1 ] ; arr [ n - 1 ] = x ; for ( int i = 0 ; ; i ++ ) { if ( arr [ i ] == x ) { arr [ n - 1 ] = backup ; if ( i < n - 1 ) return " Found " ; return " Not ▁ Found " ; } } } public static void Main ( ) { int [ ] arr = { 4 , 6 , 1 , 5 , 8 } ; int n = arr . Length ; int x = 1 ; Console . WriteLine ( search ( arr , n , x ) ) ; } }
Count of only repeated element in a sorted array of consecutive elements | C # program to find the only repeated element and number of times it appears ; Assumptions : vector a is sorted , max - difference of two adjacent elements is 1 ; if a [ m ] = m + a [ 0 ] , there is no repeating character in [ s . . m ] ; if a [ m ] < m + a [ 0 ] , there is a repeating character in [ s . . m ] ; Driver code
using System ; using System . Collections . Generic ; public class Point { public int first , second ; public Point ( int first , int second ) { this . first = first ; this . second = second ; } } public class Test { static Point sequence ( List < int > a ) { if ( a . Count == 0 ) return new Point ( 0 , 0 ) ; int s = 0 ; int e = a . Count - 1 ; while ( s < e ) { int m = ( s + e ) / 2 ; if ( a [ m ] >= m + a [ 0 ] ) s = m + 1 ; else e = m ; } return new Point ( a [ s ] , a . Count - ( a [ a . Count - 1 ] - a [ 0 ] ) ) ; } public static void Main ( String [ ] args ) { int [ ] array = new int [ ] { 1 , 2 , 3 , 4 , 4 , 4 , 5 , 6 } ; Point p = sequence ( new List < int > ( array ) ) ; Console . WriteLine ( " Repeated ▁ element ▁ is ▁ " + p . first + " , ▁ it ▁ appears ▁ " + p . second + " ▁ times " ) ; } }
Find element in a sorted array whose frequency is greater than or equal to n / 2. | C # code to find majority element in a sorted array ; Driver code
using System ; public class GFG { public static int findMajority ( int [ ] arr , int n ) { return arr [ n / 2 ] ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 2 , 3 } ; int n = arr . Length ; Console . WriteLine ( findMajority ( arr , n ) ) ; } }
Minimum absolute difference of adjacent elements in a circular array | C # program to find maximum difference between adjacent elements in a circular array . ; Checking normal adjacent elements ; Checking circular link ; driver code
using System ; class GFG { static void minAdjDifference ( int [ ] arr , int n ) { if ( n < 2 ) return ; int res = Math . Abs ( arr [ 1 ] - arr [ 0 ] ) ; for ( int i = 2 ; i < n ; i ++ ) res = Math . Min ( res , Math . Abs ( arr [ i ] - arr [ i - 1 ] ) ) ; res = Math . Min ( res , Math . Abs ( arr [ n - 1 ] - arr [ 0 ] ) ) ; Console . Write ( " Min ▁ Difference ▁ = ▁ " + res ) ; } public static void Main ( ) { int [ ] a = { 10 , 12 , 13 , 15 , 10 } ; int n = a . Length ; minAdjDifference ( a , n ) ; } }