codeparrot/xlcost-text-to-code huggingface dataset:
Count pairs with Even Product from two given arrays | C # program to implement the above approach ; Function to count pairs ( arr [ i ] , brr [ j ] ) whose product is an even number ; Stores count of odd numbers in arr [ ] ; Stores count of odd numbers in brr [ ] ; Traverse the array , arr [ ] ; If arr [ i ] is an odd number ; Update cntOddArr ; Traverse the array , brr [ ] ; If brr [ i ] is an odd number ; Update cntOddArr ; Return pairs whose product is an even number ; Driver Code
using System ; class GFG { static int cntPairsInTwoArray ( int [ ] arr , int [ ] brr , int N , int M ) { int cntOddArr = 0 ; int cntOddBrr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { cntOddArr += 1 ; } } for ( int i = 0 ; i < M ; i ++ ) { if ( brr [ i ] % 2 == 1 ) { cntOddBrr += 1 ; } } return ( N * M ) - ( cntOddArr * cntOddBrr ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 } ; int N = arr . Length ; int [ ] brr = { 1 , 2 } ; int M = brr . Length ; Console . Write ( cntPairsInTwoArray ( arr , brr , N , M ) ) ; } }
Maximize frequency of an element by at most one increment or decrement of all array elements | C # program to implement the above approach ; Function to maximize the frequency of an array element by incrementing or decrementing array elements at most once ; Stores the largest array element ; Stores the smallest array element ; freq [ i ] : Stores frequency of ( i + Min ) in the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores maximum frequency of an array element by incrementing or decrementing array elements ; Iterate all the numbers over the range [ Min , Max ] ; Stores sum of three consecutive numbers ; Update maxSum ; Print maxSum ; Driver Code ; Function call
using System ; class GFG { static void max_freq ( int [ ] arr , int N ) { Array . Sort ( arr ) ; int Max = arr [ N - 1 ] ; int Min = arr [ 0 ] ; int [ ] freq = new int [ Max - Min + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] - Min ] ++ ; } int maxSum = 0 ; for ( int i = 0 ; i < ( Max - Min - 1 ) ; i ++ ) { int val = freq [ i ] + freq [ i + 1 ] + freq [ i + 2 ] ; maxSum = Math . Max ( maxSum , val ) ; } Console . WriteLine ( maxSum ) ; } public static void Main ( ) { int [ ] arr = { 3 , 1 , 4 , 1 , 5 , 9 , 2 } ; int N = arr . Length ; max_freq ( arr , N ) ; } }
Longest increasing subsequence consisting of elements from indices divisible by previously selected indices | C # program for the above approach ; Function to find length of longest subsequence generated that satisfies the specified conditions ; Stores the length of longest subsequences of all lengths ; Iterate through the given array ; Iterate through the multiples i ; Update dp [ j ] as maximum of dp [ j ] and dp [ i ] + 1 ; Return the maximum element in dp [ ] as the length of longest subsequence ; Driver Code ; Function Call
using System ; using System . Linq ; class GFG { static int findMaxLength ( int N , int [ ] arr ) { int [ ] dp = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) { dp [ i ] = 1 ; } for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 2 * i ; j <= N ; j += i ) { if ( arr [ i - 1 ] < arr [ j - 1 ] ) { dp [ j ] = Math . Max ( dp [ j ] , dp [ i ] + 1 ) ; } } } return dp . Max ( ) ; ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int N = arr . Length ; Console . WriteLine ( findMaxLength ( N , arr ) ) ; } }
Sort a stream of integers | C # program for the above approach ; Function to sort a stream of integers ; Stores the position of array elements ; Traverse through the array ; If any element is found to be greater than the current element ; If an element > num is not present ; Otherwise , place the number at its right position ; Function to print the sorted stream of integers ; Stores the sorted stream of integers ; Traverse the array ; Function Call ; Print the array after every insertion ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void Sort ( List < int > ans , int num ) { int pos = - 1 ; for ( int i = 0 ; i < ans . Count ; i ++ ) { if ( ans [ i ] >= num ) { pos = i ; break ; } } if ( pos == - 1 ) ans . Add ( num ) ; else ans . Insert ( pos , num ) ; } static void sortStream ( int [ ] arr , int N ) { List < int > ans = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Sort ( ans , arr [ i ] ) ; for ( int j = 0 ; j < ans . Count ; j ++ ) { Console . Write ( ans [ j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 1 , 7 , 6 , 2 } ; int N = arr . Length ; sortStream ( arr , N ) ; } }
Count ways to split array into two equal sum subarrays by changing sign of any one array element | C # program for the above approach ; Function to count ways of splitting the array in two subarrays of equal sum by changing sign of any 1 element ; Stores the count of elements in prefix and suffix of array ; Stores the total sum of array ; Traverse the array ; Increase the frequency of current element in suffix ; Stores prefix sum upto an index ; Stores sum of suffix from an index ; Stores the count of ways to split the array in 2 subarrays having equal sum ; Traverse the array ; Modify prefix sum ; Add arr [ i ] to prefix Map ; Calculate suffix sum by subtracting prefix sum from total sum of elements ; Remove arr [ i ] from suffix Map ; Store the difference between the subarrays ; Check if diff is even or not ; Count number of ways to split array at index i such that subarray sums are same ; Update the count ; Return the count ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int countSubArraySignChange ( int [ ] arr , int N ) { Dictionary < int , int > prefixCount = new Dictionary < int , int > ( ) ; Dictionary < int , int > suffixCount = new Dictionary < int , int > ( ) ; int total = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { total += arr [ i ] ; if ( suffixCount . ContainsKey ( arr [ i ] ) ) { suffixCount [ arr [ i ] ] = suffixCount [ arr [ i ] ] + 1 ; } else { suffixCount . Add ( arr [ i ] , 1 ) ; } } int prefixSum = 0 ; int suffixSum = 0 ; int count = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { prefixSum += arr [ i ] ; if ( prefixCount . ContainsKey ( arr [ i ] ) ) { prefixCount [ arr [ i ] ] = prefixCount [ arr [ i ] ] + 1 ; } else { prefixCount . Add ( arr [ i ] , 1 ) ; } suffixSum = total - prefixSum ; if ( suffixCount . ContainsKey ( arr [ i ] ) ) { suffixCount [ arr [ i ] ] = suffixCount [ arr [ i ] ] - 1 ; } int diff = prefixSum - suffixSum ; if ( diff % 2 == 0 ) { int x = ( prefixCount . ContainsKey ( diff / 2 ) ? prefixCount : 0 ) + ( suffixCount . ContainsKey ( - diff / 2 ) ? suffixCount [ - diff / 2 ] : 0 ) ; count = count + x ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 2 , - 3 , 3 } ; int N = arr . Length ; Console . Write ( countSubArraySignChange ( arr , N ) ) ; } }
Partition string into two substrings having maximum number of common non | C # program to implement the above approach ; Function to count maximum common non - repeating characters that can be obtained by partitioning the String into two non - empty subStrings ; Stores count of non - repeating characters present in both the subStrings ; Stores distinct characters in left subString ; Stores distinct characters in right subString ; Traverse left subString ; Insert S [ i ] into ls ; Traverse right subString ; Insert S [ i ] into rs ; Traverse distinct characters of left subString ; If current character is present in right subString ; Update cnt ; Return count ; Function to partition the String into two non - empty subStrings in all possible ways ; Stores maximum common distinct characters present in both the subString partitions ; Traverse the String ; Update ans ; Print count of maximum common non - repeating characters ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countCommonChar ( int ind , String S ) { int cnt = 0 ; HashSet < char > ls = new HashSet < char > ( ) ; HashSet < char > rs = new HashSet < char > ( ) ; for ( int i = 0 ; i < ind ; ++ i ) { ls . Add ( S [ i ] ) ; } for ( int i = ind ; i < S . Length ; ++ i ) { rs . Add ( S [ i ] ) ; } foreach ( char v in ls ) { if ( rs . Contains ( v ) ) { ++ cnt ; } } return cnt ; } static void partitionStringWithMaxCom ( String S ) { int ans = 0 ; for ( int i = 1 ; i < S . Length ; ++ i ) { ans = Math . Max ( ans , countCommonChar ( i , S ) ) ; } Console . Write ( ans + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { String str = " aabbca " ; partitionStringWithMaxCom ( str ) ; } }
Longest Substring of 1 's after removing one character | C # program to implement the above approach ; Function to calculate the length of the longest substring of '1' s that can be obtained by deleting one character ; Add '0' at the end ; Iterator to traverse the string ; Stores maximum length of required substring ; Stores length of substring of '1' preceding the current character ; Stores length of substring of '1' succeeding the current character ; Counts number of '0' s ; Traverse the string S ; If current character is '1' ; Increase curr_one by one ; Otherwise ; Increment numberofZeros by one ; Count length of substring obtained y concatenating preceding and succeeding substrings of '1' ; Store maximum size in res ; Assign curr_one to prev_one ; Reset curr_one ; If string contains only one '0' ; Return the answer ; Driver Code
using System ; class GFG { static int longestSubarray ( String s ) { s += '0' ; int i ; int res = 0 ; int prev_one = 0 ; int curr_one = 0 ; int numberOfZeros = 0 ; for ( i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '1' ) { curr_one += 1 ; } else { numberOfZeros += 1 ; prev_one += curr_one ; res = Math . Max ( res , prev_one ) ; prev_one = curr_one ; curr_one = 0 ; } } if ( numberOfZeros == 1 ) { res -= 1 ; } return res ; } public static void Main ( String [ ] args ) { String S = "1101" ; Console . WriteLine ( longestSubarray ( S ) ) ; } }
Longest Substring of 1 's after removing one character | C # program to implement the above approach ; Function to calculate the length of the longest subString of '1' s that can be obtained by deleting one character ; Initializing i and j as left and right boundaries of sliding window ; If current character is '0' ; Decrement k by one ; If k is less than zero and character at ith index is '0' ; Return result ; Driver Code
using System ; class GFG { static int longestSubarray ( string s ) { int i = 0 , j = 0 , k = 1 ; for ( j = 0 ; j < s . Length ; ++ j ) { if ( s [ j ] == '0' ) k -= 1 ; if ( k < 0 && s [ i ++ ] == '0' ) k ++ ; } return j - i - 1 ; } public static void Main ( string [ ] args ) { string S = "011101101" ; Console . Write ( longestSubarray ( S ) ) ; } }
Lexicographically smallest string possible by inserting given character | C # program to implement the above approach ; Function to obtain lexicographically smallest String possible by inserting character c in the String s ; Traverse the String ; If the current character is greater than the given character ; Insert the character before the greater character ; Return the String ; Append the character at the end ; Return the String ; Driver Code
using System ; class GFG { static String SmallestString ( String s , char c ) { for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] > c ) { String temp = s ; s = s . Substring ( 0 , i ) ; s += c ; s += temp . Substring ( i , temp . Length - 1 ) ; return s ; } } s += c ; return s ; } public static void Main ( String [ ] args ) { String S = " acd " ; char C = ' b ' ; Console . WriteLine ( SmallestString ( S , C ) ) ; } }
Largest Non | C # program to implement the above approach ; Function to find the largest unique element of the array ; Store frequency of each distinct element of the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores largest non - repeating element present in the array ; Stores index of the largest non - repeating array element ; Traverse the array ; If frequency of arr [ i ] is equal to 1 and arr [ i ] exceeds LNRElem ; Update ind ; Update LNRElem ; If no array element is found with frequency equal to 1 ; Print largest non - repeating element ; Drivers Code
using System ; using System . Collections . Generic ; class GFG { static void LarUnEl ( int [ ] arr , int N ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( map . ContainsKey ( arr [ i ] ) == true ) map [ arr [ i ] ] += 1 ; else map [ arr [ i ] ] = 1 ; } int LNRElem = Int32 . MinValue ; int ind = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( map [ arr [ i ] ] == 1 && arr [ i ] > LNRElem ) { ind = i ; LNRElem = arr [ i ] ; } } if ( ind == - 1 ) { Console . WriteLine ( ind ) ; return ; } Console . WriteLine ( arr [ ind ] ) ; } public static void Main ( ) { int [ ] arr = { 3 , 1 , 8 , 8 , 4 } ; int N = arr . Length ; LarUnEl ( arr , N ) ; } }
Minimum shifts of substrings of 1 s required to group all 1 s together in a given Binary string | C # program for the above approach ; Function to count indices substrings of 1 s need to be shifted such that all 1 s in the string are grouped together ; Stores first occurrence of '1' ; Stores last occurrence of '1' ; Count of 0 s between firstOne and lastOne ; Traverse the string to find the first and last occurrences of '1' ; Count number of 0 s present between firstOne and lastOne ; Print minimum operations ; Driver code ; Given string
using System ; using System . Collections . Generic ; class GFG { static void countShifts ( string str ) { int firstOne = - 1 ; int lastOne = - 1 ; int count = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == '1' ) { if ( firstOne == - 1 ) firstOne = i ; lastOne = i ; } } if ( ( firstOne == - 1 ) || ( firstOne == lastOne ) ) { Console . Write ( 0 ) ; return ; } for ( int i = firstOne ; i <= lastOne ; i ++ ) { if ( str [ i ] == '0' ) { count ++ ; } } Console . WriteLine ( count ) ; } static void Main ( ) { string str = "00110111011" ; countShifts ( str ) ; } }
Check if a non | C # program for the above approach ; Utility Function to check whether a subsequence same as the given subarray exists or not ; Check if first element of the subarray is also present before ; Check if last element of the subarray is also present later ; If above two conditions are not satisfied , then no such subsequence exists ; Function to check and print if a subsequence which is same as the given subarray is present or not ; Driver code ; Function Call
using System ; class GFG { static bool checkSubsequenceUtil ( int [ ] arr , int L , int R , int N ) { for ( int i = 0 ; i < L ; i ++ ) if ( arr [ i ] == arr [ L ] ) return true ; for ( int i = R + 1 ; i < N ; i ++ ) if ( arr [ i ] == arr [ R ] ) return true ; return false ; } static void checkSubsequence ( int [ ] arr , int L , int R , int N ) { if ( checkSubsequenceUtil ( arr , L , R , N ) ) { Console . Write ( " YES STRNEWLINE " ) ; } else { Console . Write ( " NO STRNEWLINE " ) ; } } public static void Main ( ) { int [ ] arr = { 1 , 7 , 12 , 1 , 7 , 5 , 10 , 11 , 42 } ; int N = arr . Length ; int L = 3 , R = 6 ; checkSubsequence ( arr , L , R , N ) ; } }
Search an element in a Doubly Linked List | C # program to implement the above approach ; Structure of a node of the doubly linked list ; Stores data value of a node ; Stores pointer to next node ; Stores pointer to previous node ; Function to insert a node at the beginning of the Doubly Linked List ; Allocate memory for new node ; Insert the data ; Since node is added at the beginning , prev is always null ; Link the old list to the new node ; If pointer to head is not null ; Change the prev of head node to new node ; Move the head to point to the new node ; Function to find the position of an integer in doubly linked list ; Stores head Node ; Stores position of the integer in the doubly linked list ; Traverse the doubly linked list ; Update pos ; Update temp ; If the integer not present in the doubly linked list ; If the integer present in the doubly linked list ; Driver Code ; Create the doubly linked list 18 < -> 15 < -> 8 < -> 9 < -> 14
using System ; class GFG { public class Node { public int data ; public Node next ; public Node prev ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . prev = null ; new_node . next = head_ref ; if ( head_ref != null ) { head_ref . prev = new_node ; } head_ref = new_node ; return head_ref ; } static int search ( Node head_ref , int x ) { Node temp = head_ref ; int pos = 0 ; while ( temp . data != x && temp . next != null ) { pos ++ ; temp = temp . next ; } if ( temp . data != x ) return - 1 ; return ( pos + 1 ) ; } public static void Main ( String [ ] args ) { Node head = null ; int X = 8 ; head = push ( head , 14 ) ; head = push ( head , 9 ) ; head = push ( head , 8 ) ; head = push ( head , 15 ) ; head = push ( head , 18 ) ; Console . Write ( search ( head , X ) ) ; } }
Longest subarray in which all elements are smaller than K | C # program for the above approach ; Function to find the length of the longest subarray with all elements smaller than K ; Stores the length of maximum consecutive sequence ; Stores maximum length of subarray ; Iterate through the array ; Check if array element smaller than K ; Store the maximum of length and count ; Reset the counter ; Print the maximum length ; Driver code ; Given array arr [ ] ; Size of the array ; Given K ; Function Call
using System ; class GFG { static void largestSubarray ( int [ ] arr , int N , int K ) { int count = 0 ; int len = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < K ) { count += 1 ; } else { len = Math . Max ( len , count ) ; count = 0 ; } } if ( count != 0 ) { len = Math . Max ( len , count ) ; } Console . WriteLine ( len ) ; } static void Main ( ) { int [ ] arr = { 1 , 8 , 3 , 5 , 2 , 2 , 1 , 13 } ; int N = arr . Length ; int K = 6 ; largestSubarray ( arr , N , K ) ; } }
Smallest pair of indices with product of subarray co | C # program for the above approach ; Function to calculate GCD of two integers ; Recursively calculate GCD ; Function to find the lexicographically smallest pair of indices whose product is co - prime with the product of the subarrays on its left and right ; Stores the suffix product of array elements ; Set 0 / 1 if pair satisfies the given condition or not ; Initialize array right_prod [ ] ; Update the suffix product ; Stores product of all elements ; Stores the product of subarray in between the pair of indices ; Iterate through every pair of indices ( i , j ) ; Store product of A [ i , j ] ; Check if product is co - prime to product of either the left or right subarrays ; If no such pair is found , then print - 1 ; Driver Code ; Function Call
using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void findPair ( int [ ] A , int N ) { int [ ] right_prod = new int [ N ] ; int flag = 0 ; right_prod [ N - 1 ] = A [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) right_prod [ i ] = right_prod [ i + 1 ] * A [ i ] ; int total_prod = right_prod [ 0 ] ; int product ; for ( int i = 1 ; i < N - 1 ; i ++ ) { product = 1 ; for ( int j = i ; j < N - 1 ; j ++ ) { product *= A [ j ] ; if ( gcd ( product , right_prod [ j + 1 ] ) == 1 || gcd ( product , total_prod / right_prod [ i ] ) == 1 ) { flag = 1 ; Console . WriteLine ( " ( " + ( i - 1 ) + " , ▁ " + ( j + 1 ) + " ) " ) ; break ; } } if ( flag == 1 ) break ; } if ( flag == 0 ) Console . Write ( - 1 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 1 , 3 , 7 } ; int N = arr . Length ; findPair ( arr , N ) ; } }
Count binary strings of length same as given string after removal of substrings "01" and "00" that consists of at least one '1' | C # program for the above approach ; Function to count the strings consisting of at least 1 set bit ; Initialize count ; Iterate through string ; The answer is 2 ^ N - 1 ; Driver Code ; Given string ; Function call
using System ; class GFG { static void countString ( string S ) { int count = 0 ; foreach ( char it in S ) { if ( it == '0' && count > 0 ) { count -- ; } else { count ++ ; } } Console . Write ( ( 1 << count ) - 1 ) ; } public static void Main ( string [ ] args ) { string S = "1001" ; countString ( S ) ; } }
Append digits to the end of duplicate strings to make all strings in an array unique | C # program for the above approach ; Function to replace duplicate strings by alphanumeric strings to make all strings in the array unique ; Store the frequency of strings ; Iterate over the array ; For the first occurrence , update the frequency count ; Otherwise ; Append frequency count to end of the string ; Print the modified array ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void replaceDuplicates ( string [ ] names ) { Dictionary < string , int > hash = new Dictionary < string , int > ( ) ; for ( int i = 0 ; i < names . Length ; i ++ ) { if ( ! hash . ContainsKey ( names [ i ] ) ) hash [ names [ i ] ] = 1 ; else { int count = hash [ names [ i ] ] ; hash [ names [ i ] ] += 1 ; names [ i ] += count . ToString ( ) ; } } for ( int i = 0 ; i < names . Length ; i ++ ) { Console . Write ( names [ i ] + ' ▁ ' ) ; } } public static void Main ( ) { string [ ] str = { " aa " , " bb " , " cc " , " bb " , " aa " , " aa " , " aa " } ; replaceDuplicates ( str ) ; } }
Minimum cost of flipping characters required to convert Binary String to 0 s only | C # program to implement the above approach ; Function to find the minimum cost to convert given string to 0 s only ; Length of string ; Stores the index of leftmost '1' in str ; Update the index of leftmost '1' in str ; Stores the index of rightmost '1' in str ; Update the index of rightmost '1' in str ; If str does not contain any '1' s ; No changes required ; Stores minimum cost ; Iterating through str form left_1 to right_1 ; Stores length of consecutive 0 s ; Calculate length of consecutive 0 s ; If a substring of 0 s exists ; Update minimum cost ; Printing the minimum cost ; Driver code
using System ; class GFG { public static void convert_to_allzeroes ( string str , int a , int b ) { int len = str . Length ; int left_1 , i = 0 ; while ( i < len && str [ i ] == '0' ) i ++ ; left_1 = i ; int right_1 ; i = len - 1 ; while ( i >= 0 && str [ i ] == '0' ) i -- ; right_1 = i ; if ( left_1 == len && right_1 == - 1 ) { Console . Write ( 0 ) ; return ; } int cost = a , zeroes ; for ( i = left_1 ; i <= right_1 ; i ++ ) { zeroes = 0 ; while ( i < len && str [ i ] == '0' ) { zeroes ++ ; i ++ ; } if ( zeroes != 0 ) cost += Math . Min ( zeroes * b , a ) ; } Console . Write ( cost ) ; } public static void Main ( ) { string str = "01101110" ; int A = 5 , B = 1 ; convert_to_allzeroes ( str , A , B ) ; } }
Minimum distance to visit given K points on X | C # program to implement the above approach ; Function to find the minimum distance travelled to visit K point ; Stores minimum distance travelled to visit K point ; Stores distance travelled to visit points ; Traverse the array arr [ ] ; If arr [ i ] and arr [ i + K - 1 ] are positive ; Update dist ; Update dist ; Update res ; Driver Code ; Initial the array
using System ; class GFG { static int MinDistK ( int [ ] arr , int N , int K ) { int res = Int32 . MaxValue ; int dist = 0 ; for ( int i = 0 ; i <= ( N - K ) ; i ++ ) { if ( arr [ i ] >= 0 && arr [ i + K - 1 ] >= 0 ) { dist = Math . Max ( arr [ i ] , arr [ i + K - 1 ] ) ; } else { dist = Math . Abs ( arr [ i ] ) + Math . Abs ( arr [ i + K - 1 ] ) + Math . Min ( Math . Abs ( arr [ i ] ) , Math . Abs ( arr [ i + K - 1 ] ) ) ; } res = Math . Min ( res , dist ) ; } return res ; } public static void Main ( ) { int K = 3 ; int [ ] arr = { - 30 , - 10 , 10 , 20 , 50 } ; int N = arr . Length ; Console . WriteLine ( MinDistK ( arr , N , K ) ) ; } }