codeparrot/xlcost-text-to-code huggingface dataset:
Count 1 s in binary matrix having remaining indices of its row and column filled with 0 s | C # program for the above approach ; Function to count required 1 s from the given matrix ; Stores the dimensions of the [ , ] mat ; Calculate sum of rows ; Calculate sum of columns ; Stores required count of 1 s ; If current cell is 1 and sum of row and column is 1 ; Increment count of 1 s ; Return the readonly count ; Driver Code ; Given matrix ; Function call
using System ; class GFG { static int numSpecial ( int [ , ] mat ) { int m = mat . GetLength ( 0 ) ; int n = mat . GetLength ( 1 ) ; int [ ] rows = new int [ m ] ; int [ ] cols = new int [ n ] ; int i , j ; for ( i = 0 ; i < m ; i ++ ) { rows [ i ] = 0 ; for ( j = 0 ; j < n ; j ++ ) rows [ i ] += mat [ i , j ] ; } for ( i = 0 ; i < n ; i ++ ) { cols [ i ] = 0 ; for ( j = 0 ; j < m ; j ++ ) cols [ i ] += mat [ j , i ] ; } int cnt = 0 ; for ( i = 0 ; i < m ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( mat [ i , j ] == 1 && rows [ i ] == 1 && cols [ j ] == 1 ) cnt ++ ; } } return cnt ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 0 , 0 } , { 0 , 0 , 1 } , { 0 , 0 , 0 } } ; Console . Write ( numSpecial ( mat ) + " STRNEWLINE " ) ; } }
Find all matrix elements which are minimum in their row and maximum in their column | C # program for the above approach ; Functionto find all the matrix elements which are minimum in its row and maximum in its column ; Initialize unordered set ; Traverse the matrix ; Update the minimum element of current row ; Insert the minimum element of the row ; Update the maximum element of current column ; Checking if it is already present in the unordered_set or not ; Driver code ; Function call ; If no such matrix element is found
using System ; using System . Collections . Generic ; class GFG { public static List < int > minmaxNumbers ( int [ , ] matrix , List < int > res ) { HashSet < int > set = new HashSet < int > ( ) ; for ( int i = 0 ; i < matrix . GetLength ( 0 ) ; i ++ ) { int minr = int . MaxValue ; for ( int j = 0 ; j < matrix . GetLength ( 1 ) ; j ++ ) { minr = Math . Min ( minr , matrix [ i , j ] ) ; } set . Add ( minr ) ; } for ( int j = 0 ; j < matrix . GetLength ( 0 ) ; j ++ ) { int maxc = int . MinValue ; for ( int i = 0 ; i < matrix . GetLength ( 1 ) ; i ++ ) { maxc = Math . Max ( maxc , matrix [ i , j ] ) ; } if ( set . Contains ( maxc ) ) { res . Add ( maxc ) ; } } return res ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 10 , 4 } , { 9 , 3 , 8 } , { 15 , 16 , 17 } } ; List < int > ans = new List < int > ( ) ; ans = minmaxNumbers ( mat , ans ) ; if ( ans . Count == 0 ) Console . WriteLine ( " - 1" ) ; for ( int i = 0 ; i < ans . Count ; i ++ ) Console . WriteLine ( ans [ i ] ) ; } }
Node whose removal minimizes the maximum size forest from an N | C # program to implement the above approach ; Function to create the graph ; Function to traverse the graph and find the minimum of maximum size forest after removing a node ; Traversing every child subtree except the parent node ; Traverse all subtrees ; Update the maximum size of forests ; Update the minimum of maximum size of forests obtained ; Condition to find the minimum of maximum size forest ; Update and store the corresponding node ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int mini = 105 , ans , n ; static List < int > [ ] g = new List < int > [ 100 ] ; static int [ ] size = new int [ 100 ] ; static void create_graph ( ) { g [ 1 ] . Add ( 2 ) ; g [ 2 ] . Add ( 1 ) ; g [ 1 ] . Add ( 3 ) ; g [ 3 ] . Add ( 1 ) ; g [ 1 ] . Add ( 4 ) ; g [ 4 ] . Add ( 1 ) ; g [ 2 ] . Add ( 5 ) ; g [ 5 ] . Add ( 2 ) ; g [ 2 ] . Add ( 6 ) ; g [ 6 ] . Add ( 2 ) ; } static void dfs ( int node , int parent ) { size [ node ] = 1 ; int mx = 0 ; foreach ( int y in g [ node ] ) { if ( y == parent ) continue ; dfs ( y , node ) ; size [ node ] += size [ y ] ; mx = Math . Max ( mx , size [ y ] ) ; } mx = Math . Max ( mx , n - size [ node ] ) ; if ( mx < mini ) { mini = mx ; ans = node ; } } public static void Main ( String [ ] args ) { n = 6 ; for ( int i = 0 ; i < g . Length ; i ++ ) g [ i ] = new List < int > ( ) ; create_graph ( ) ; dfs ( 1 , - 1 ) ; Console . Write ( ans + " STRNEWLINE " ) ; } }
Minimum non | C # program for the above approach ; Function to find minimum flips required to remove all 0 s from a given binary string ; Length of given string ; Stores the indices of previous 0 s ; Stores the minimum operations ; Traverse string to find minimum operations to obtain required string ; Current character ; If current character is '0' ; Update temp1 with current index , if both temp variables are empty ; Update temp2 with current index , if temp1 contains prev index but temp2 is empty ; If temp1 is not empty ; Reset temp1 to - 1 ; Increase ans ; If temp2 is not empty but temp1 is empty ; Reset temp2 to - 1 ; Increase ans ; If both temp variables are not empty ; Otherwise ; Return the answer ; Driver Code
using System ; class GFG { static int minOperation ( String s ) { int n = s . Length ; int temp1 = - 1 , temp2 = - 1 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int curr = s [ i ] ; if ( curr == '0' ) { if ( temp1 == - 1 && temp2 == - 1 ) { temp1 = i ; } else if ( temp1 != - 1 && temp2 == - 1 && i - temp1 == 1 ) { temp2 = i ; } else if ( temp1 != - 1 ) { temp1 = - 1 ; ans ++ ; } else if ( temp1 == - 1 && temp2 != - 1 && i - temp2 != 1 ) { temp2 = - 1 ; ans ++ ; } } } if ( temp1 != - 1 && temp2 != - 1 ) { ans += 2 ; } else if ( temp1 != - 1 temp2 != - 1 ) { ans += 1 ; } return ans ; } public static void Main ( String [ ] args ) { String s = "110010" ; Console . WriteLine ( minOperation ( s ) ) ; } }
Smallest prefix to be deleted such that remaining array can be rearranged to form a sorted array | C # program for above approach ; Function to find the minimum length of prefix required to be deleted ; Stores index to be returned ; Iterate until previous element is <= current index ; Decrementing index ; Return index ; Driver code ; Given [ ] arr ; Function call
using System ; class GFG { static int findMinLength ( int [ ] arr ) { int index = ( int ) arr . Length - 1 ; while ( index > 0 && arr [ index ] >= arr [ index - 1 ] ) { index -- ; } return index ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 8 , 5 , 0 , - 1 , - 1 , 0 , 1 , 2 , 3 , 4 } ; int n = arr . Length ; Console . WriteLine ( findMinLength ( arr ) ) ; } }
Count array elements exceeding sum of preceding K elements | C # program for the above approach ; Function to count array elements exceeding sum of preceding K elements ; Iterate over the array ; Update prefix sum ; Check if arr [ K ] > arr [ 0 ] + . . + arr [ K - 1 ] ; Increment count ; Check if arr [ i ] > arr [ i - K - 1 ] + . . + arr [ i - 1 ] ; Increment count ; Driver Code ; Given array
using System ; class GFG { static int countPrecedingK ( int [ ] a , int n , int K ) { int [ ] prefix = new int [ n ] ; prefix [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + a [ i ] ; } int ctr = 0 ; if ( prefix [ K - 1 ] < a [ K ] ) ctr ++ ; for ( int i = K + 1 ; i < n ; i ++ ) { if ( prefix [ i - 1 ] - prefix [ i - K - 1 ] < a [ i ] ) ctr ++ ; } return ctr ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 8 , 10 , - 2 , 7 , 5 , 5 , 9 , 15 } ; int N = arr . Length ; int K = 2 ; Console . Write ( countPrecedingK ( arr , N , K ) ) ; } }
Count minimum moves required to convert A to B | C # program for the above approach ; Function to find minimum number of moves to obtained B from A ; Stores the minimum number of moves ; Absolute difference ; K is in range [ 0 , 10 ] ; Print the required moves ; Driver Code
using System ; class GFG { static void convertBfromA ( int a , int b ) { int moves = 0 ; int x = Math . Abs ( a - b ) ; for ( int i = 10 ; i > 0 ; i -- ) { moves += x / i ; x = x % i ; } Console . Write ( moves + " ▁ " ) ; } public static void Main ( ) { int A = 188 , B = 4 ; convertBfromA ( A , B ) ; } }
Count N digits numbers with sum divisible by K | C # program to implement the above approach ; Function to count the N digit numbers whose sum is divisible by K ; Base case ; If already computed subproblem occurred ; Store the count of N digit numbers whoe sum is divisible by K ; Check if the number does not contain any leading 0. ; Recurrence relation ; Driver code ; Stores the values of overlapping subproblems
using System ; class GFG { static int M = 1000 ; static int countNum ( int N , int sum , int K , int st , int [ , , ] dp ) { if ( N == 0 && sum == 0 ) { return 1 ; } if ( N < 0 ) { return 0 ; } if ( dp [ N , sum , st ] != - 1 ) { return dp [ N , sum , st ] ; } int res = 0 ; int start = ( st == 1 ? 0 : 1 ) ; for ( int i = start ; i <= 9 ; i ++ ) { res += countNum ( N - 1 , ( sum + i ) % K , K , ( ( st i ) > 0 ) ? 1 : 0 , dp ) ; } return dp [ N , sum , st ] = res ; } static public void Main ( ) { int N = 2 , K = 7 ; int [ , , ] dp = new int [ M , M , 2 ] ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) for ( int k = 0 ; k < 2 ; k ++ ) dp [ i , j , k ] = - 1 ; Console . WriteLine ( countNum ( N , 0 , K , 0 , dp ) ) ; } }
Minimize Nth term of an Arithmetic progression ( AP ) | C # program to implement the above approach ; Function to find the smallest Nth term of an AP possible ; Stores the smallest Nth term ; Check if common difference of AP is an integer ; Store the common difference ; Store the First Term of that AP ; Store the Nth term of that AP ; Check if all elements of an AP are positive ; Return the least Nth term obtained ; Driver Code
using System ; class GFG { static int smallestNth ( int A , int B , int N ) { int res = Int32 . MaxValue ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = N ; j > i ; j -- ) { if ( ( B - A ) % ( j - i ) == 0 ) { int D = ( B - A ) / ( j - i ) ; int FirstTerm = A - ( i - 1 ) * D ; int NthTerm = FirstTerm + ( N - 1 ) * D ; if ( FirstTerm > 0 ) res = Math . Min ( res , NthTerm ) ; } } } return res ; } public static void Main ( ) { int N = 3 ; int A = 1 ; int B = 6 ; Console . Write ( smallestNth ( A , B , N ) ) ; } }
Subarray of size K with prime sum | C # program to implement the above approach ; Generate all prime numbers in the range [ 1 , 1000000 ] ; Set all numbers as prime initially ; Mark 0 and 1 as non - prime ; If current element is prime ; Mark all its multiples as non - prime ; Function to print the subarray whose sum of elements is prime ; Store the current subarray of size K ; Calculate the sum of first K elements ; Check if currSum is prime ; Store the start and last index of subarray of size K ; Iterate over remaining array ; Check if currSum ; Driver Code
using System ; class GFG { static void sieve ( bool [ ] prime ) { for ( int i = 0 ; i < 1000000 ; i ++ ) { prime [ i ] = true ; } prime [ 0 ] = prime [ 1 ] = false ; for ( int i = 2 ; i * i <= 1000000 ; i ++ ) { if ( prime [ i ] ) { for ( int j = i * i ; j < 1000000 ; j += i ) { prime [ j ] = false ; } } } } static void subPrimeSum ( int N , int K , int [ ] arr , bool [ ] prime ) { int currSum = 0 ; for ( int i = 0 ; i < K ; i ++ ) { currSum += arr [ i ] ; } if ( prime [ currSum ] ) { for ( int i = 0 ; i < K ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } return ; } int st = 1 , en = K ; while ( en < N ) { currSum += arr [ en ] - arr [ st - 1 ] ; if ( prime [ currSum ] ) { for ( int i = st ; i <= en ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } return ; } en ++ ; st ++ ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 20 , 7 , 5 , 4 , 3 , 11 , 99 , 87 , 23 , 45 } ; int K = 4 ; int N = arr . Length ; bool [ ] prime = new bool [ 1000000 ] ; sieve ( prime ) ; subPrimeSum ( N , K , arr , prime ) ; } }
Minimum characters required to be removed to sort binary string in ascending order | C # program for the above approach ; Function to find the minimum count of characters to be removed to make the string sorted in ascending order ; Length of given string ; Stores the first occurrence of '1' ; Stores the last occurrence of '0' ; Traverse the string to find the first occurrence of '1' ; Traverse the string to find the last occurrence of '0' ; Return 0 if the str have only one type of character ; Initialize count1 and count0 to count '1' s before lastIdx0 and '0' s after firstIdx1 ; Traverse the string to count0 ; Traverse the string to count1 ; Return the minimum of count0 and count1 ; Driver Code ; Given string str ; Function call
using System . Collections . Generic ; using System ; class GFG { static int minDeletion ( string str ) { int n = str . Length ; int firstIdx1 = - 1 ; int lastIdx0 = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '1' ) { firstIdx1 = i ; break ; } } for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == '0' ) { lastIdx0 = i ; break ; } } if ( firstIdx1 == - 1 lastIdx0 == - 1 ) return 0 ; int count1 = 0 , count0 = 0 ; for ( int i = 0 ; i < lastIdx0 ; i ++ ) { if ( str [ i ] == '1' ) { count1 ++ ; } } for ( int i = firstIdx1 + 1 ; i < n ; i ++ ) { if ( str [ i ] == '1' ) { count0 ++ ; } } return Math . Min ( count0 , count1 ) ; } public static void Main ( ) { string str = "1000101" ; Console . WriteLine ( minDeletion ( str ) ) ; } }
Maximum score assigned to a subsequence of numerically consecutive and distinct array elements | C # program for the above approach ; Function to find the maximum score with unique element in the subset ; Base Case ; Check if the previously picked element differs by 1 from the current element ; Calculate score by including the current element ; Calculate score by excluding the current element ; Return maximum of the two possibilities ; Driver Code ; Given arrays ; Function Call
using System ; class GFG { public static int maximumSum ( int [ ] a , int [ ] b , int n , int index , int lastpicked ) { if ( index == n ) return 0 ; int option1 = 0 , option2 = 0 ; if ( lastpicked == - 1 a [ lastpicked ] != a [ index ] ) option1 = b [ index ] + maximumSum ( a , b , n , index + 1 , index ) ; option2 = maximumSum ( a , b , n , index + 1 , lastpicked ) ; return Math . Max ( option1 , option2 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 3 , 3 , 1 } ; int [ ] brr = { - 1 , 2 , 10 , 20 , - 10 , - 9 } ; int N = arr . Length ; Console . WriteLine ( maximumSum ( arr , brr , N , 0 , - 1 ) ) ; } }
Replace every element in a circular array by sum of next K elements | C # program for the above approach ; Function to print the required resultant array ; Reverse the array ; Traverse the range ; Store prefix sum ; Find the prefix sum ; Store the answer ; Calculate the answers ; Count of remaining elements ; Add the sum of all elements y times ; Add the remaining elements ; Update ans [ i ] ; If array is reversed print ans [ ] in reverse ; Driver Code ; Given array [ ] arr ; Given K ; Function
using System ; class GFG { static void sumOfKElements ( int [ ] arr , int n , int k ) { bool rev = false ; if ( k < 0 ) { rev = true ; k *= - 1 ; int l = 0 , r = n - 1 ; while ( l < r ) { int tmp = arr [ l ] ; arr [ l ] = arr [ r ] ; arr [ r ] = tmp ; l ++ ; r -- ; } } int [ ] dp = new int [ n ] ; dp [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] += dp [ i - 1 ] + arr [ i ] ; } int [ ] ans = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( i + k < n ) ans [ i ] = dp [ i + k ] - dp [ i ] ; else { int x = k - ( n - 1 - i ) ; int y = x / n ; int rem = x % n ; ans [ i ] = dp [ n - 1 ] - dp [ i ] + y * dp [ n - 1 ] + ( rem - 1 >= 0 ? dp [ rem - 1 ] : 0 ) ; } } if ( rev ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } else { for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 2 , - 5 , 11 } ; int N = arr . Length ; int K = 3 ; sumOfKElements ( arr , N , K ) ; } }
Count subarrays for every array element in which they are the minimum | C # program for the above approach ; Function to find the boundary of every element within which it is minimum ; Perform Binary Search ; Find mid m ; Update l ; Update r ; Inserting the index ; Function to required count subarrays ; Stores the indices of element ; Initialize the output array ; Left boundary , till the element is smallest ; Right boundary , till the element is smallest ; Calculate the number of subarrays based on its boundary ; Adding cnt to the ans ; Driver code ; Given array arr [ ] ; Function call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int binaryInsert ( ArrayList boundary , int i ) { int l = 0 ; int r = boundary . Count - 1 ; while ( l <= r ) { int m = ( l + r ) / 2 ; if ( ( int ) boundary [ m ] < i ) l = m + 1 ; else r = m - 1 ; } boundary . Insert ( l , i ) ; return l ; } static int [ ] countingSubarray ( int [ ] arr , int n ) { Dictionary < int , int > index = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) index [ arr [ i ] ] = i ; ArrayList boundary = new ArrayList ( ) ; boundary . Add ( - 1 ) ; boundary . Add ( n ) ; Array . Sort ( arr ) ; int [ ] ans = new int [ n ] ; foreach ( int num in arr ) { int i = binaryInsert ( boundary , index [ num ] ) ; int l = ( int ) boundary [ i ] - ( int ) boundary [ i - 1 ] - 1 ; int r = ( int ) boundary [ i + 1 ] - ( int ) boundary [ i ] - 1 ; int cnt = l + r + l * r + 1 ; ans [ index [ num ] ] += cnt ; } return ans ; } public static void Main ( string [ ] args ) { int N = 5 ; int [ ] arr = { 3 , 2 , 4 , 1 , 5 } ; int [ ] a = countingSubarray ( arr , N ) ; Console . Write ( " [ " ) ; int n = a . Length - 1 ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] + " , ▁ " ) ; Console . Write ( a [ n ] + " ] " ) ; } }
Check if digit cube limit of an integer arrives at fixed point or a limit cycle | C # program for the above approach ; Define the limit ; Function to get the sum of cube of digits of a number ; Convert to String to get sum of the cubes of its digits ; return sum ; Function to check if the number arrives at a fixed point or a cycle ; Stores the values obtained ; Insert N to set s ; Get the next number using F ( N ) ; Check if the next number is repeated or not ; Function to check if digit cube limit of an integer arrives at fixed point or in a limit cycle ; N is a non negative integer ; Function Call ; If the value received is greater than limit ; If the value received is an Armstrong number ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static readonly int limit = 1000000000 ; static int F ( int N ) { String str = String . Join ( " " , N ) ; int sum = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { int val = ( int ) ( str [ i ] - '0' ) ; sum += val * val * val ; } return sum ; } static int findDestination ( int N ) { HashSet < int > s = new HashSet < int > ( ) ; int prev = N , next = 0 ; s . Add ( N ) ; while ( N <= limit ) { next = F ( N ) ; if ( s . Contains ( next ) ) { return next ; } prev = next ; s . Add ( prev ) ; N = next ; } return next ; } static void digitCubeLimit ( int N ) { if ( N < 0 ) Console . Write ( " N ▁ cannot ▁ be ▁ negative STRNEWLINE " ) ; else { int ans = findDestination ( N ) ; if ( ans > limit ) Console . Write ( " Limit ▁ exceeded STRNEWLINE " ) ; else if ( ans == F ( ans ) ) { Console . Write ( N ) ; Console . Write ( " ▁ reaches ▁ to ▁ a " + " ▁ fixed ▁ point : ▁ " ) ; Console . Write ( ans ) ; } else { Console . Write ( N ) ; Console . Write ( " ▁ reaches ▁ to ▁ a " + " ▁ limit ▁ cycle : ▁ " ) ; Console . Write ( ans ) ; } } } public static void Main ( String [ ] args ) { int N = 3 ; digitCubeLimit ( N ) ; } }
Count quadruples ( i , j , k , l ) in an array such that i < j < k < l and arr [ i ] = arr [ k ] and arr [ j ] = arr [ l ] | C # program for the above approach ; Function to count total number of required tuples ; Initialize unordered map ; Find the pairs ( j , l ) such that arr [ j ] = arr [ l ] and j < l ; Elements are equal ; Update the count ; Add the frequency of arr [ l ] to val ; Update the frequency of element arr [ j ] ; Return the answer ; Driver code ; Given array [ ] arr ; Function call
using System ; using System . Collections . Generic ; class GFG { static int countTuples ( int [ ] arr , int N ) { int ans = 0 , val = 0 ; Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int j = 0 ; j < N - 2 ; j ++ ) { val = 0 ; for ( int l = j + 1 ; l < N ; l ++ ) { if ( arr [ j ] == arr [ l ] ) { ans += val ; } if ( freq . ContainsKey ( arr [ l ] ) ) val += freq [ arr [ l ] ] ; } if ( freq . ContainsKey ( arr [ j ] ) ) { freq [ arr [ j ] ] = freq [ arr [ j ] ] + 1 ; } else { freq . Add ( arr [ j ] , 1 ) ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 1 , 2 , 2 , 2 } ; int N = arr . Length ; Console . Write ( countTuples ( arr , N ) ) ; } }
Minimize subarray increments / decrements required to reduce all array elements to 0 | C # program of the above approach ; Function to count the minimum number of operations required ; Traverse the array ; If array element is negative ; Update minimum negative ; Update maximum positive ; Return minOp ; Driver Code
using System ; class GFG { static int minOperation ( int [ ] arr ) { int minOp = int . MinValue ; int minNeg = 0 , maxPos = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( arr [ i ] < 0 ) { if ( arr [ i ] < minNeg ) minNeg = arr [ i ] ; } else { if ( arr [ i ] > maxPos ) maxPos = arr [ i ] ; } } return Math . Abs ( minNeg ) + maxPos ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 4 , 1 } ; Console . WriteLine ( minOperation ( arr ) ) ; } }
Pair of integers having least GCD among all given pairs having GCD exceeding K | C # program for the above approach ; Function to calculate the GCD of two numbers ; Function to print the pair having a gcd value just greater than the given integer ; Initialize variables ; Iterate until low less than equal to high ; Calculate mid ; Reducing the search space ; Print the answer ; Driver Code ; Given array and K ; 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 GcdPair ( int [ , ] arr , int k ) { int lo = 0 , hi = arr . Length - 1 , mid ; int [ ] ans = { - 1 , 0 } ; while ( lo <= hi ) { mid = lo + ( hi - lo ) / 2 ; if ( GCD ( arr [ mid , 0 ] , arr [ mid , 1 ] ) > k ) { ans = GetRow ( arr , mid ) ; hi = mid - 1 ; } else lo = mid + 1 ; } if ( ans [ 0 ] == - 1 ) Console . Write ( " - 1" ) ; else Console . Write ( " ( ▁ " + ans [ 0 ] + " , ▁ " + ans [ 1 ] + " ▁ ) " ) ; return ; } public static int [ ] GetRow ( int [ , ] matrix , int row ) { var rowLength = matrix . GetLength ( 1 ) ; var rowVector = new int [ rowLength ] ; for ( var i = 0 ; i < rowLength ; i ++ ) rowVector [ i ] = matrix [ row , i ] ; return rowVector ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 3 , 6 } , { 15 , 30 } , { 25 , 75 } , { 30 , 120 } } ; int K = 16 ; GcdPair ( arr , K ) ; } }
Check if array can be sorted by swapping pairs having GCD equal to the smallest element in the array | C # implementation of the above approach ; Function to check if it is possible to sort array or not ; Store the smallest element ; Copy the original array ; Iterate over the given array ; Update smallest element ; Copy elements of [ ] arr to array [ ] B ; Sort original array ; Iterate over the given array ; If the i - th element is not in its sorted place ; Not possible to swap ; Driver Code ; Given array ; Function Call
using System ; class GFG { static void isPossible ( int [ ] arr , int N ) { int mn = int . MaxValue ; int [ ] B = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { mn = Math . Min ( mn , arr [ i ] ) ; B [ i ] = arr [ i ] ; } Array . Sort ( arr ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != B [ i ] ) { if ( B [ i ] % mn != 0 ) { Console . Write ( " No " ) ; return ; } } } Console . Write ( " Yes " ) ; return ; } public static void Main ( String [ ] args ) { int N = 6 ; int [ ] arr = { 4 , 3 , 6 , 6 , 2 , 9 } ; isPossible ( arr , N ) ; } }