codeparrot/xlcost-text-to-code huggingface dataset:
Rearrange an Array such that Sum of same | C # Program to implement the above approach ; Function to rearrange the array such that no same - indexed subset have sum equal to that in the original array ; Initialize a vector ; Iterate the array ; Sort the vector ; Shift of elements to the index of its next cyclic element ; Print the answer ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void printNewArray ( List < int > a , int n ) { List < Tuple < int , int > > v = new List < Tuple < int , int > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { v . Add ( new Tuple < int , int > ( a [ i ] , i ) ) ; } v . Sort ( ) ; int [ ] ans = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { ans [ v [ ( i + 1 ) % n ] . Item2 ] = v [ i ] . Item1 ; } for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } static void Main ( ) { List < int > a = new List < int > ( new int [ ] { 4 , 1 , 2 , 5 , 3 } ) ; int n = a . Count ; printNewArray ( a , n ) ; } }
Minimize count of array elements to be removed to maximize difference between any pair up to K | C # Program to implement the above approach ; Function to count the number of elements to be removed from the array based on the given condition ; Sort the array ; / Initialize the variable ; Iterate for all possible pairs ; Check the difference between the numbers ; Update the minimum removals ; Return the answer ; Driver Code
using System ; class GFG { static int min_remove ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int ans = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { if ( arr [ j ] - arr [ i ] <= k ) { ans = Math . Min ( ans , n - j + i - 1 ) ; } } } return ans ; } public static void Main ( String [ ] args ) { int k = 3 ; int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; Console . Write ( min_remove ( arr , n , k ) ) ; } }
Reduce a given Binary Array to a single element by removal of Triplets | C # program to implement the above approach ; Function to check if it is possible to reduce the array to a single element ; Stores frequency of 0 's ; Stores frequency of 1 's ; Condition for array to be reduced ; Otherwise ; Driver Code
using System ; class GFG { static void solve ( int [ ] arr , int n ) { int countzeroes = 0 ; int countones = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) countzeroes ++ ; else countones ++ ; } if ( Math . Abs ( countzeroes - countones ) == 1 ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 1 , 0 , 0 , 1 , 1 , 1 } ; int n = arr . Length ; solve ( arr , n ) ; } }
Nearest smaller character to a character K from a Sorted Array | C # program to implement the above approach ; Function to return the nearest smaller character ; Stores the nearest smaller character ; Iterate till starts cross end ; Find the mid element ; Check if K is found ; Check if current character is less than K ; Increment the start ; Otherwise ; Increment end ; Return the character ; Driver Code
using System ; class GFG { static char bs ( char [ ] ar , int n , int ele ) { int start = 0 ; int end = n - 1 ; char ch = ' @ ' ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; if ( ar [ mid ] == ele ) end = mid - 1 ; else if ( ar [ mid ] < ele ) { ch = ar [ mid ] ; start = mid + 1 ; } else end = mid - 1 ; } return ch ; } public static void Main ( String [ ] args ) { char [ ] ar = { ' e ' , ' g ' , ' t ' , ' y ' } ; int n = ar . Length ; char K = ' u ' ; char ch = bs ( ar , n , K ) ; if ( ch == ' @ ' ) Console . Write ( " - 1" ) ; else Console . Write ( ch ) ; } }
Largest possible Subset from an Array such that no element is K times any other element in the Subset | C # implementation of the above approach ; Function to find the maximum size of the required subset ; Size of the array ; Sort the array ; Stores which index is included or excluded ; Stores the indices of array elements ; Count of pairs ; Iterate through all the element ; If element is included ; Check if a [ i ] * k is present in the array or not ; Increase count of pair ; Exclude the pair ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int findMaxLen ( int [ ] a , int k ) { int n = a . Length ; Array . Sort ( a ) ; bool [ ] vis = new bool [ n ] ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mp . Add ( a [ i ] , i ) ; } int c = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( vis [ i ] == false ) { int check = a [ i ] * k ; if ( mp . ContainsKey ( check ) ) { c ++ ; vis [ mp [ check ] ] = true ; } } } return n - c ; } public static void Main ( String [ ] args ) { int K = 3 ; int [ ] arr = { 1 , 4 , 3 , 2 } ; Console . Write ( findMaxLen ( arr , K ) ) ; } }
Minimize length of prefix of string S containing all characters of another string T | C # program for the above approach ; Base Case - if T is empty , it matches 0 length prefix ; Convert strings to lower case for uniformity ; Update dictCount to the letter count of T ; If new character is found , initialize its entry , and increase nUnique ; Increase count of ch ; Iterate from 0 to N ; i - th character ; Skip if ch not in targetStr ; Decrease Count ; If the count of ch reaches 0 , we do not need more ch , and can decrease nUnique ; If nUnique reaches 0 , we have found required prefix ; Otherwise ; Driver code
using System . Collections . Generic ; using System ; class GFG { static int getPrefixLength ( string srcStr , string targetStr ) { if ( targetStr . Length == 0 ) return 0 ; srcStr = srcStr . ToLower ( ) ; targetStr = targetStr . ToLower ( ) ; Dictionary < char , int > dictCount = new Dictionary < char , int > ( ) ; int nUnique = 0 ; foreach ( var ch in targetStr ) { if ( dictCount . ContainsKey ( ch ) != true ) { nUnique += 1 ; dictCount [ ch ] = 0 ; } dictCount [ ch ] += 1 ; } for ( int i = 0 ; i < srcStr . Length ; i ++ ) { char ch = srcStr [ i ] ; if ( dictCount . ContainsKey ( ch ) != true ) continue ; dictCount [ ch ] -= 1 ; if ( dictCount [ ch ] == 0 ) nUnique -= 1 ; if ( nUnique == 0 ) return ( i + 1 ) ; } return - 1 ; } public static void Main ( ) { string S = " MarvoloGaunt " ; string T = " Tom " ; Console . Write ( getPrefixLength ( S , T ) ) ; } }
Minimize length of Substrings containing at least one common Character | C # program to implement the above approach ; Function to check and return if subStrings of length mid has a common character a ; Length of the String ; Initialise the first occurrence of character a ; Check that distance b / w the current and previous occurrence of character a is less than or equal to mid ; If distance exceeds mid ; Function to check for all the alphabets , if subStrings of length mid have a character common ; Check for all 26 alphabets ; Check that char i + a is common in all the subStrings of length mid ; If no characters is common ; Function to calculate and return the minm length of subStrings ; Initialise low and high ; Perform binary search ; Update mid ; Check if one common character is present in the length of the mid ; Returns the minimum length that contain one common character ; Function to check if all characters are distinct ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool check ( char [ ] str , int mid , char a ) { int n = str . Length ; int previous = - 1 , i ; for ( i = 0 ; i < n ; ++ i ) { if ( str [ i ] == a ) { if ( i - previous > mid ) { return false ; } previous = i ; } } if ( i - previous > mid ) return false ; else return true ; } static bool possible ( char [ ] str , int mid ) { for ( int i = 0 ; i < 26 ; ++ i ) { if ( check ( str , mid , ( char ) ( i + ' a ' ) ) ) return true ; } return false ; } static int findMinLength ( char [ ] str ) { int low = 1 , high = str . Length ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( possible ( str , mid ) ) high = mid - 1 ; else low = mid + 1 ; } return high + 1 ; } static bool ifAllDistinct ( String str ) { HashSet < char > s = new HashSet < char > ( ) ; foreach ( char c in str . ToCharArray ( ) ) { s . Add ( c ) ; } return s . Count == str . Length ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; if ( ifAllDistinct ( str ) ) Console . Write ( - 1 + " STRNEWLINE " ) ; else Console . Write ( findMinLength ( str . ToCharArray ( ) ) ) ; } }
Maximize product of absolute index difference with K | C # program to implement the above approach ; Function returns maximum possible value of k ; Pointer i make sure that A [ i ] will result in max k ; Stores maximum possible k ; Possible value of k for current pair ( A [ i ] and A [ j ] ) ; If current value exceeds k ; Update the value of k ; Update pointer i ; Return the maxm possible k ; Driver Code
using System ; class GFG { static int solve ( int [ ] A , int N ) { int i = 0 ; int k = 0 ; for ( int j = 1 ; j < N ; j ++ ) { int tempK = Math . Min ( A [ i ] , A [ j ] ) / ( j - i ) ; if ( tempK > k ) { k = tempK ; } if ( A [ j ] >= A [ i ] / ( j - i ) ) i = j ; } return k ; } public static void Main ( string [ ] args ) { int [ ] A = { 10 , 5 , 12 , 15 , 8 } ; int N = A . Length ; Console . Write ( solve ( A , N ) ) ; } }
Split Array into min number of subsets with difference between each pair greater than 1 | C # program for the above approach ; Function to split the array into minimum number of subsets with difference strictly > 1 ; Sort the array ; Traverse through the sorted array ; Check the pairs of elements with difference 1 ; If we find even a single pair with difference equal to 1 , then 2 partitions else only 1 partition ; Print the count of partitions ; Driver Code ; Given array ; Size of the array ; Function call
using System ; class GFG { static void split ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] == 1 ) { count = 2 ; break ; } } Console . Write ( count ) ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 2 , 4 , 6 } ; int n = arr . Length ; split ( arr , n ) ; } }
Check if a Palindromic String can be formed by concatenating Substrings of two given Strings | C # program to implement the above approach ; Function to check if a palindromic string can be formed from the substring of given strings ; Boolean array to mark presence of characters ; Check if any of the character of str2 is already marked ; If a common character is found ; If no common character is found ; Driver code
using System ; class GFG { public static bool check ( String str1 , String str2 ) { bool [ ] mark = new bool [ 26 ] ; int n = str1 . Length , m = str2 . Length ; for ( int i = 0 ; i < n ; i ++ ) { mark [ str1 [ i ] - ' a ' ] = true ; } for ( int i = 0 ; i < m ; i ++ ) { if ( mark [ str2 [ i ] - ' a ' ] ) return true ; } return false ; } public static void Main ( String [ ] args ) { String str1 = " abca " , str2 = " efad " ; if ( check ( str1 , str2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Generate K co | C # implementation of the above approach ; Function prints the required pairs ; First co - prime pair ; As a pair ( 1 n ) has already been Printed ; If i is a factor of N ; Since ( i , i ) won 't form  a coprime pair ; Driver Code
using System ; class GFG { static void FindPairs ( int n , int k ) { Console . Write ( 1 + " ▁ " + n + " STRNEWLINE " ) ; k -- ; for ( long i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { Console . Write ( 1 + " ▁ " + i + " STRNEWLINE " ) ; k -- ; if ( k == 0 ) break ; if ( i != n / i ) { Console . Write ( 1 + " ▁ " + n / i + " STRNEWLINE " ) ; k -- ; } if ( k == 0 ) break ; } } } public static void Main ( String [ ] args ) { int N = 100 ; int K = 5 ; FindPairs ( N , K ) ; } }
Minimum edges required to make a Directed Graph Strongly Connected | C # program to implement the above approach ; Perform DFS to count the in - degree and out - degree of the graph ; Mark the source as visited ; Traversing adjacent nodes ; Mark out - degree as 1 ; Mark in - degree as 1 ; If not visited ; DFS Traversal on adjacent vertex ; Function to return minimum number of edges required to make the graph strongly connected ; For Adjacency List ; Create the Adjacency List ; Initialize the in - degree array ; Initialize the out - degree array ; Initialize the visited array ; Perform DFS to count in - degrees and out - degreess ; To store the result ; To store total count of in - degree and out - degree ; Find total in - degree and out - degree ; Calculate the minimum edges required ; Return the minimum edges ; Driver Code ; Function call
using System ; using System . Collections . Generic ; class GFG { static void dfs ( int u , List < int > [ ] adj , int [ ] vis , int [ ] inDeg , int [ ] outDeg ) { vis [ u ] = 1 ; foreach ( int v in adj [ u ] ) { outDeg [ u ] = 1 ; inDeg [ v ] = 1 ; if ( vis [ v ] == 0 ) { dfs ( v , adj , vis , inDeg , outDeg ) ; } } } static int findMinimumEdges ( int [ ] source , int N , int M , int [ ] dest ) { List < int > [ ] adj = new List < int > [ N + 1 ] ; for ( int i = 0 ; i < adj . Length ; i ++ ) adj [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < M ; i ++ ) { adj ] . Add ( dest [ i ] ) ; } int [ ] inDeg = new int [ N + 1 ] ; int [ ] outDeg = new int [ N + 1 ] ; int [ ] vis = new int [ N + 1 ] ; dfs ( 1 , adj , vis , inDeg , outDeg ) ; int minEdges = 0 ; int totalIndegree = 0 ; int totalOutdegree = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( inDeg [ i ] == 1 ) totalIndegree ++ ; if ( outDeg [ i ] == 1 ) totalOutdegree ++ ; } minEdges = Math . Max ( N - totalIndegree , N - totalOutdegree ) ; return minEdges ; } public static void Main ( String [ ] args ) { int N = 5 , M = 5 ; int [ ] source = { 1 , 3 , 1 , 3 , 4 } ; int [ ] destination = { 2 , 2 , 3 , 4 , 5 } ; Console . Write ( findMinimumEdges ( source , N , M , destination ) ) ; } }
Count of subarrays which forms a permutation from given Array elements | C # program to implement the above approach ; Function returns the required count ; Store the indices of the elements present in [ ] A . ; Store the maximum and minimum index of the elements from 1 to i . ; Update maxi and mini , to store minimum and maximum index for permutation of elements from 1 to i + 1 ; If difference between maxi and mini is equal to i ; Increase count ; Return final count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int PermuteTheArray ( int [ ] A , int n ) { int [ ] arr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ A [ i ] - 1 ] = i ; } int mini = n , maxi = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mini = Math . Min ( mini , arr [ i ] ) ; maxi = Math . Max ( maxi , arr [ i ] ) ; if ( maxi - mini == i ) count ++ ; } return count ; } public static void Main ( String [ ] args ) { int [ ] A = { 4 , 5 , 1 , 3 , 2 , 6 } ; Console . Write ( PermuteTheArray ( A , 6 ) ) ; } }
Find Second largest element in an array | Set 2 | C # program to implement the above approach ; Function to find the largest element in the array arr [ ] ; Base Condition ; Initialize an empty list ; Divide the array into two equal length subarrays and recursively find the largest among the two ; Store length of compared1 [ ] in the first index ; Store the maximum element ; Return compared1 which contains the maximum element ; Store length of compared2 [ ] in the first index ; Store the maximum element ; Return compared2 [ ] which contains the maximum element ; Function to print the second largest element in the array arr [ ] ; Find the largest element in arr [ ] ; Find the second largest element in arr [ ] ; Print the second largest element ; Driver code
using System ; class GFG { static int [ ] findLargest ( int beg , int end , int [ ] arr , int n ) { if ( beg == end ) { int [ ] compared = new int [ n ] ; compared [ 0 ] = 1 ; compared [ 1 ] = arr [ beg ] ; return compared ; } int [ ] compared1 = findLargest ( beg , ( beg + end ) / 2 , arr , n ) ; int [ ] compared2 = findLargest ( ( beg + end ) / 2 + 1 , end , arr , n ) ; if ( compared1 [ 1 ] > compared2 [ 1 ] ) { int k = compared1 [ 0 ] + 1 ; compared1 [ 0 ] = k ; compared1 [ k ] = compared2 [ 1 ] ; return compared1 ; } else { int k = compared2 [ 0 ] + 1 ; compared2 [ 0 ] = k ; compared2 [ k ] = compared1 [ 1 ] ; return compared2 ; } } static void findSecondLargest ( int end , int [ ] arr ) { int [ ] compared1 = findLargest ( 0 , end - 1 , arr , end ) ; int [ ] compared2 = findLargest ( 2 , compared1 [ 0 ] + 2 , compared1 , compared1 [ 0 ] ) ; Console . WriteLine ( compared2 [ 1 ] ) ; } static public void Main ( ) { int N = 10 ; int [ ] arr = { 20 , 1990 , 12 , 1110 , 1 , 59 , 12 , 15 , 120 , 1110 } ; findSecondLargest ( N , arr ) ; } }
Count of Reverse Bitonic Substrings in a given String | C # program to implement the above approach ; Function to calculate the number of reverse bitonic substrings ; Stores the count ; All possible lengths of substrings ; Starting point of a substring ; Ending point of a substring ; Condition for reverse bitonic substrings of length 1 ; Check for decreasing sequence ; If end of substring is reached ; For increasing sequence ; If end of substring is reached ; Return the number of bitonic substrings ; Driver code
using System ; class GFG { public static int CountsubString ( char [ ] str , int n ) { int c = 0 ; for ( int len = 1 ; len <= n ; len ++ ) { for ( int i = 0 ; i <= n - len ; i ++ ) { int j = i + len - 1 ; char temp = str [ i ] , f = '0' ; if ( j == i ) { c ++ ; continue ; } int k = i + 1 ; while ( temp > str [ k ] && k <= j ) { temp = str [ k ] ; k ++ ; } if ( k > j ) { c ++ ; f = '2' ; } while ( k <= j && temp < str [ k ] && f != '2' ) { temp = str [ k ] ; k ++ ; } if ( k > j && f != 2 ) { c ++ ; f = '0' ; } } } return c ; } public static void Main ( String [ ] args ) { char [ ] str = { ' b ' , ' a ' , ' d ' , ' e ' } ; Console . WriteLine ( CountsubString ( str , str . Length ) - 1 ) ; } }
Maximum subsequence sum from a given array which is a perfect square | C # program to implement the above approach ; If sum is 0 , then answer is true ; If sum is not 0 and [ ] arr is empty , then answer is false ; Fill the subset table in bottom up manner ; Function to find the sum ; Find sum of all values ; return the value ; ; Driver code
using System ; class GFG { static bool isSubsetSum ( int [ ] arr , int n , int sum ) { bool [ , ] subset = new bool [ n + 1 , sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) subset [ i , 0 ] = true ; for ( int i = 1 ; i <= sum ; i ++ ) subset [ 0 , i ] = false ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( j < arr [ i - 1 ] ) subset [ i , j ] = subset [ i - 1 , j ] ; if ( j >= arr [ i - 1 ] ) subset [ i , j ] = subset [ i - 1 , j ] || subset [ i - 1 , j - arr [ i - 1 ] ] ; } } return subset [ n , sum ] ; } static int findSum ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } int val = ( int ) Math . Sqrt ( sum ) ; for ( int i = val ; i >= 0 ; i -- ) { if ( isSubsetSum ( arr , n , i * i ) ) { return i * i ; } } return 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . Length ; Console . WriteLine ( findSum ( arr , n ) ) ; } }
Smallest subarray whose product leaves remainder K when divided by size of the array | C # program to implement the above approach ; Function to find the subarray of minimum length ; Initialize the minimum subarray size to N + 1 ; Generate all possible subarray ; Initialize the product ; Find the product ; Return the minimum size of subarray ; Driver code ; Given array
using System ; class GFG { static int findsubArray ( int [ ] arr , int N , int K ) { int res = N + 1 ; for ( int i = 0 ; i < N ; i ++ ) { int curr_prod = 1 ; for ( int j = i ; j < N ; j ++ ) { curr_prod = curr_prod * arr [ j ] ; if ( curr_prod % N == K && res > ( j - i + 1 ) ) { res = Math . Min ( res , j - i + 1 ) ; break ; } } } return ( res == N + 1 ) ? 0 : res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 2 , 3 } ; int N = arr . Length ; int K = 1 ; int answer = findsubArray ( arr , N , K ) ; if ( answer != 0 ) Console . WriteLine ( answer ) ; else Console . WriteLine ( " - 1" ) ; } }
Count of array elements whose order of deletion precedes order of insertion | C # program to implement the above approach ; Function returns maximum number of required elements ; Insert the elements of array B in the queue and set ; Stores the answer ; If A [ i ] is already processed ; Until we find A [ i ] in the queue ; Remove elements from the queue ; Increment the count ; Remove the current element A [ i ] from the queue and set . ; Return total count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void maximumCount ( int [ ] A , int [ ] B , int n ) { Queue < int > q = new Queue < int > ( ) ; HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { s . Add ( B [ i ] ) ; q . Enqueue ( B [ i ] ) ; } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! s . Contains ( A [ i ] ) ) continue ; while ( q . Count != 0 && q . Peek ( ) != A [ i ] ) { s . Remove ( q . Peek ( ) ) ; q . Dequeue ( ) ; count ++ ; } if ( A [ i ] == q . Peek ( ) ) { q . Dequeue ( ) ; s . Remove ( A [ i ] ) ; } if ( q . Count == 0 ) break ; } Console . Write ( count + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int N = 4 ; int [ ] A = { 1 , 2 , 3 , 4 } ; int [ ] B = { 1 , 2 , 4 , 3 } ; maximumCount ( A , B , N ) ; } }
Check if a Matrix is Bitonic or not | C # program to check if a matrix is Bitonic or not ; Function to check if an array is Bitonic or not ; Check for increasing sequence ; Check for decreasing sequence ; Function to check whether given matrix is bitonic or not ; Check row - wise ; Check column wise ; Generate an array consisting of elements of the current column ; Driver Code
using System ; class GFG { readonly static int N = 3 , M = 3 ; static bool checkBitonic ( int [ ] arr , int n ) { int i , j , f = 0 ; for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) continue ; if ( arr [ i ] == arr [ i - 1 ] ) return false ; else { f = 1 ; break ; } } if ( i == n ) return true ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] < arr [ j - 1 ] ) continue ; if ( arr [ i ] == arr [ i - 1 ] ) return false ; else { if ( f == 1 ) return false ; } } return true ; } static void check ( int [ , ] arr ) { int f = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! checkBitonic ( GetRow ( arr , i ) , M ) ) { Console . WriteLine ( " NO " ) ; return ; } } for ( int i = 0 ; i < N ; i ++ ) { int [ ] temp = new int [ N ] ; for ( int j = 0 ; j < N ; j ++ ) { temp [ j ] = arr [ j , i ] ; } if ( ! checkBitonic ( temp , N ) ) { Console . WriteLine ( " NO " ) ; return ; } } Console . WriteLine ( " YES " ) ; } 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 [ , ] m = { { 1 , 2 , 3 } , { 3 , 4 , 5 } , { 2 , 6 , 4 } } ; check ( m ) ; } }
Maximum possible GCD for a pair of integers with product N | C # program to implement the above approach ; Function to return the maximum GCD ; To find all divisors of N ; If i is a factor ; Store the pair of factors ; Store the maximum GCD ; Return the maximum GCD ; Driver Code
using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int getMaxGcd ( int N ) { int maxGcd = int . MinValue , A , B ; for ( int i = 1 ; i <= Math . Sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { A = i ; B = N / i ; maxGcd = Math . Max ( maxGcd , gcd ( A , B ) ) ; } } return maxGcd ; } public static void Main ( String [ ] s ) { int N = 18 ; Console . WriteLine ( getMaxGcd ( N ) ) ; } }