codeparrot/xlcost-text-to-code huggingface dataset:
Check if a given number is one less than twice its reverse | C # program of the above approach ; Iterative function to reverse digits of num ; Loop to extract all digits of the number ; Function to check if N satisfies given equation ; Driver Code
using System ; class GFG { static int rev ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } static bool check ( int n ) { return 2 * rev ( n ) == n + 1 ; } public static void Main ( String [ ] args ) { int n = 73 ; if ( check ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Check if a given number is one less than twice its reverse | C # program to implement the above approach ; Function to check y is a power of x ; logarithm function to calculate value ; Compare to the result1 or result2 both are equal ; Function to check if N satisfies the equation 2 * reverse ( n ) = n + 1 ; Driver Code
using System ; class GFG { static bool isPower ( int x , int y ) { double res1 = Math . Log ( y ) / Math . Log ( x ) ; double res2 = Math . Log ( y ) / Math . Log ( x ) ; return ( res1 == res2 ) ; } static bool check ( int n ) { int x = ( n + 7 ) / 8 ; if ( ( n + 7 ) % 8 == 0 && isPower ( 10 , x ) ) return true ; else return false ; } static public void Main ( ) { int n = 73 ; if ( check ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Nth term of a recurrence relation generated by two given arrays | C # program for the above approach ; Function to calculate Nth term of general recurrence relations ; Stores the generated sequence ; Current term is sum of previous k terms ; Print the nth term ; Driver Code ; Given Array F [ ] and C [ ] ; Given N and K ; Function call
using System ; class GFG { static double mod = 1e9 + 7 ; static void NthTerm ( int [ ] F , int [ ] C , int K , int n ) { int [ ] ans = new int [ n + 1 ] ; for ( int i = 0 ; i < K ; i ++ ) ans [ i ] = F [ i ] ; for ( int i = K ; i <= n ; i ++ ) { for ( int j = i - K ; j < i ; j ++ ) { ans [ i ] += ans [ j ] ; ans [ i ] %= ( int ) mod ; } } Console . WriteLine ( ans [ n ] ) ; } public static void Main ( String [ ] args ) { int [ ] F = { 0 , 1 } ; int [ ] C = { 1 , 1 } ; int K = 2 ; int N = 10 ; NthTerm ( F , C , K , N ) ; } }
Mode of frequencies of given array elements | C # program of the above approach ; Function to find the mode of the frequency of the array ; Stores the frequencies of array elements ; Traverse through array elements and count frequencies ; Stores the frequencies 's of  frequencies of array elements ; Stores the minimum value ; Find the Mode in 2 nd map ; Search for this Mode ; When mode is find then return to main function . ; If mode is not found ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countFreq ( int [ ] arr , int n ) { Dictionary < int , int > mp1 = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp1 . ContainsKey ( arr [ i ] ) ) { mp1 [ arr [ i ] ] = mp1 [ arr [ i ] ] + 1 ; } else { mp1 . Add ( arr [ i ] , 1 ) ; } } Dictionary < int , int > mp2 = new Dictionary < int , int > ( ) ; foreach ( KeyValuePair < int , int > it in mp1 ) { if ( mp2 . ContainsKey ( it . Value ) ) { mp2 [ it . Value ] = mp2 [ it . Value ] + 1 ; } else { mp2 . Add ( it . Value , 1 ) ; } } int M = int . MinValue ; foreach ( KeyValuePair < int , int > it in mp2 ) { M = Math . Max ( M , it . Value ) ; } foreach ( KeyValuePair < int , int > it in mp2 ) { if ( M == it . Value ) { return it . Key ; } } return 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 10 , 3 , 10 , 8 , 3 , 6 , 4 } ; int n = arr . Length ; Console . Write ( countFreq ( arr , n ) ) ; } }
Maximize count of nodes disconnected from all other nodes in a Graph | C # implementation of the above approach ; Function which returns the maximum number of isolated nodes ; Used nodes ; Remaining edges ; Count nodes used ; If given edges are non - zero ; Driver Code ; Given N and E ; Function call
using System ; class GFG { static int maxDisconnected ( int N , int E ) { int curr = 1 ; int rem = E ; while ( rem > 0 ) { rem = rem - Math . Min ( curr , rem ) ; curr ++ ; } if ( curr > 1 ) { return N - curr ; } else { return N ; } } public static void Main ( String [ ] args ) { int N = 5 , E = 1 ; Console . Write ( maxDisconnected ( N , E ) ) ; } }
Check if number is palindrome or not in base B | C # program to implement the above approach ; Function to check if N in base B is palindrome or not ; Stores the reverse of N ; Stores the value of N ; Extract all the digits of N ; Generate its reverse ; Driver code
using System ; class GFG { static bool checkPalindromeB ( int N , int B ) { int rev = 0 ; int N1 = N ; while ( N1 > 0 ) { rev = rev * B + N1 % B ; N1 = N1 / B ; } return N == rev ; } public static void Main ( String [ ] args ) { int N = 5 , B = 2 ; if ( checkPalindromeB ( N , B ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Product of all numbers up to N that are co | C # program for the above approach ; Function to return gcd of a and b ; Base Case ; Recursive GCD ; Function to find the product of all the numbers till N that are relatively prime to N ; Stores the resultant product ; Iterate over [ 2 , N ] ; If gcd is 1 , then find the product with result ; Return the readonly product ; Driver Code
using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int findProduct ( int N ) { int result = 1 ; for ( int i = 2 ; i < N ; i ++ ) { if ( gcd ( i , N ) == 1 ) { result *= i ; } } return result ; } public static void Main ( String [ ] args ) { int N = 5 ; Console . Write ( findProduct ( N ) ) ; } }
Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum | C # implementation of the above approach ; Function to count maximum number of array elements equal ; Driver Code ; Function call
using System ; class GFG { static int countEqual ( int n ) { return ( n + 1 ) / 2 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . Length ; Console . WriteLine ( countEqual ( n ) ) ; } }
Minimize count of unequal elements at corresponding indices between given arrays | C # program for the above approach ; Function that count of the mismatched pairs in bot the array ; Create a parent array and initialize it ; Preprocessing of the given pairs of indices ; 1 - based indexing ; If both indices doesn 't belong  to same component ; Insert the indices in same component ; Dictionary to get the indices of array A ; If current element is not present in array B then count this as mismatched ; Get the index of the element in array B ; Check if both indices belong to same connected component if not increment the count ; Return answer ; Function that gives the connected component of each index ; Function that creates the connected components ; Find parent of a and b recursively ; Update the parent of a ; Driver Code ; Given arrays [ ] A , [ ] B ; List of indices ; Function Call
using System ; using System . Collections . Generic ; class GFG { public static int countPairs ( int [ ] A , int [ ] B , int N , int M , int [ , ] List ) { int count = 0 ; int [ ] par = new int [ N + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) par [ i ] = i ; for ( int i = 0 ; i < M ; i ++ ) { int index1 = find ( par , List [ i , 0 ] - 1 ) ; int index2 = find ( par , List [ i , 1 ] - 1 ) ; if ( index1 != index2 ) { union ( par , index1 , index2 ) ; } } Dictionary < int , int > map = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( map . ContainsKey ( A [ i ] ) ) map [ A [ i ] ] = i ; else map . Add ( A [ i ] , i ) ; } for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] != B [ i ] ) { if ( ! map . ContainsKey ( B [ i ] ) ) { count ++ ; continue ; } int j = map [ B [ i ] ] ; if ( find ( par , i ) != find ( par , j ) ) count ++ ; } } return count ; } public static int find ( int [ ] par , int x ) { if ( par [ x ] == x ) return x ; else return par [ x ] = find ( par , par [ x ] ) ; } public static void union ( int [ ] par , int a , int b ) { a = find ( par , a ) ; b = find ( par , b ) ; if ( a == b ) return ; par [ a ] = b ; } public static void Main ( String [ ] args ) { int N = 5 ; int M = 4 ; int [ ] A = { 1 , 5 , 9 , 2 , 3 } ; int [ ] B = { 2 , 4 , 5 , 1 , 3 } ; int [ , ] List = { { 1 , 4 } , { 2 , 3 } , { 3 , 5 } , { 2 , 5 } } ; Console . WriteLine ( countPairs ( A , B , N , M , List ) ) ; } }
Maximum value of expression ( arr [ i ] + arr [ j ] * arr [ k ] ) formed from a valid Triplet | C # program for the above approach ; Function that generate all valid triplets and calculate the value of the valid triplets ; Generate all triplets ; Check whether the triplet is valid or not ; Update the value ; Print the maximum value ; Driver Code ; Given array [ ] arr ; Function Call
using System ; class GFG { static void max_valid_triplet ( int [ ] A , int n ) { int ans = - 1 ; 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 ( A [ i ] < A [ j ] && A [ j ] < A [ k ] ) { int value = A [ i ] + A [ j ] * A [ k ] ; if ( value > ans ) { ans = value ; } } } } } Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { int [ ] arr = new int [ ] { 7 , 9 , 3 , 8 , 11 , 10 } ; int n = arr . Length ; max_valid_triplet ( arr , n ) ; } }
Product of proper divisors of a number for Q queries | C # implementation of the above approach ; Function to precompute the product of proper divisors of a number at it 's corresponding index ; Returning the pre - computed values ; Driver code
using System ; class GFG { static readonly int mod = 1000000007 ; static long [ ] ans = new long [ 100002 ] ; static void preCompute ( ) { for ( int i = 2 ; i <= 100000 / 2 ; i ++ ) { for ( int j = 2 * i ; j <= 100000 ; j += i ) { ans [ j ] = ( ans [ j ] * i ) % mod ; } } } static long productOfProperDivi ( int num ) { return ans [ num ] ; } public static void Main ( String [ ] args ) { for ( int i = 0 ; i < 100002 ; i ++ ) ans [ i ] = 1 ; preCompute ( ) ; int queries = 5 ; int [ ] a = { 4 , 6 , 8 , 16 , 36 } ; for ( int i = 0 ; i < queries ; i ++ ) { Console . Write ( productOfProperDivi ( a [ i ] ) + " , ▁ " ) ; } } }
Highest power of 2 that divides the LCM of first N Natural numbers . | C # implementation of the approach ; Function to find LCM of first N natural numbers ; Initialize result ; Ans contains LCM of 1 , 2 , 3 , . . i after i 'th iteration ; Function to find the highest power of 2 which divides LCM of first n natural numbers ; Find lcm of first N natural numbers ; To store the highest required power of 2 ; Counting number of consecutive zeros from the end in the given binary String ; Driver code
using System ; class GFG { static int findlcm ( int n ) { int ans = 1 ; for ( int i = 1 ; i <= n ; i ++ ) ans = ( ( ( i * ans ) ) / ( __gcd ( i , ans ) ) ) ; return ans ; } static int highestPower ( int n ) { int lcm = findlcm ( n ) ; int ans = 0 ; for ( int i = 1 ; ; i ++ ) { int x = ( int ) Math . Pow ( 2 , i ) ; if ( lcm % x == 0 ) { ans = i ; } if ( x > n ) break ; } return ans ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int n = 15 ; Console . Write ( highestPower ( n ) ) ; } }
Highest power of 2 that divides the LCM of first N Natural numbers . | C # implementation of the approach ; Function to find the highest power of 2 which divides LCM of first n natural numbers ; Driver code
using System ; class GFG { static int highestPower ( int n ) { return ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) ; } public static void Main ( String [ ] args ) { int n = 15 ; Console . WriteLine ( highestPower ( n ) ) ; } }
Check if number can be made prime by deleting a single digit | C # implementation to check if a number becomes prime by deleting any digit ; Function to check if N is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to delete character at index i from given String str ; Deletes character at position 4 ; Function to check if a number becomes prime by deleting any digit ; Converting the number to String ; length of String ; number should not be of single digit ; Loop to find all numbers after deleting a single digit ; Deleting ith character from the String ; converting String to int ; Driver Code
using System ; class GFG { static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static String deleteIth ( String str , int i ) { str = str . Substring ( 0 , i ) + str . Substring ( i + 1 ) ; return str ; } static bool isPrimePossible ( int N ) { String s = String . Join ( " " , N ) ; int l = s . Length ; if ( l < 2 ) return false ; for ( int i = 0 ; i < l ; i ++ ) { String str = deleteIth ( s , i ) ; int num = Int32 . Parse ( str ) ; if ( isPrime ( num ) ) return true ; } return false ; } public static void Main ( String [ ] args ) { int N = 610 ; if ( isPrimePossible ( N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Check if a number has an odd count of odd divisors and even count of even divisors | C # implementation of the above approach ; Function to find the count of even and odd factors of N ; Loop runs till square root ; Condition to check if the even factors of the number N is is even and count of odd factors is odd ; Driver Code
using System ; class GFG { static void checkFactors ( long N ) { long ev_count = 0 , od_count = 0 ; for ( long i = 1 ; i <= Math . Sqrt ( N ) + 1 ; i ++ ) { if ( N % i == 0 ) { if ( i == N / i ) { if ( i % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; } else { if ( i % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; if ( ( N / i ) % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; } } } if ( ev_count % 2 == 0 && od_count % 2 == 1 ) Console . Write ( " Yes " + " STRNEWLINE " ) ; else Console . Write ( " No " + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { long N = 36 ; checkFactors ( N ) ; } }
Count of distinct permutations of length N having no similar adjacent characters | C # program to implement the above approach ; Function to print the number of permutations possible ; Driver Code
using System ; class GFG { static int countofPermutations ( int N ) { return ( int ) ( 3 * Math . Pow ( 2 , N - 1 ) ) ; } public static void Main ( String [ ] args ) { int N = 5 ; Console . Write ( countofPermutations ( N ) ) ; } }
Find two distinct numbers such that their LCM lies in given range | C # program for the above approach ; Function to find two distinct numbers X and Y s . t . their LCM lies between L and R and X , Y are minimum possible ; Check if 2 * L lies in range L , R ; Print the answer ; Driver Code ; Given value of ranges ; Function call
using System ; class GFG { static void answer ( int L , int R ) { if ( 2 * L <= R ) Console . WriteLine ( L + " , ▁ " + ( 2 * L ) ) ; else Console . WriteLine ( " - 1" ) ; } public static void Main ( ) { int L = 3 , R = 8 ; answer ( L , R ) ; } }
Maximum count of pairwise co | C # program for the above approach ; Function to find the gcd of two numbers ; Function to of pairwise co - prime and common divisors of two numbers ; Initialize answer with 1 , to include 1 in the count ; Count of primes of gcd ( N , M ) ; Finding prime factors of gcd ; Increment count if it is divisible by i ; Return the total count ; Function Call for each pair to calculate the count of pairwise co - prime divisors ; Driver Code ; Given array of pairs ; Function Call
using System ; class GFG { static int gcd ( int x , int y ) { if ( x % y == 0 ) return y ; else return gcd ( y , x % y ) ; } static int countPairwiseCoprime ( int N , int M ) { int answer = 1 ; int g = gcd ( N , M ) ; int temp = g ; for ( int i = 2 ; i * i <= g ; i ++ ) { if ( temp % i == 0 ) { answer ++ ; while ( temp % i == 0 ) temp /= i ; } } if ( temp != 1 ) answer ++ ; return answer ; } static void countCoprimePair ( int [ , ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( countPairwiseCoprime ( arr [ i , 0 ] , arr [ i , 1 ] ) + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ , ] arr = { { 12 , 18 } , { 420 , 660 } } ; int N = arr . GetLength ( 0 ) ; countCoprimePair ( arr , N ) ; } }
Product of absolute difference of every pair in given Array | C # program for the above approach ; Function to return the product of abs diff of all pairs ( x , y ) ; To store product ; Iterate all possible pairs ; Find the product ; Return product ; Driver Code ; Given array arr [ ] ; Function call
using System ; class GFG { static int getProduct ( int [ ] a , int n ) { int p = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { p *= Math . Abs ( a [ i ] - a [ j ] ) ; } } return p ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; Console . Write ( getProduct ( arr , N ) ) ; } }
Check whether a number can be represented as difference of two consecutive cubes | C # program for the above approach ; Function to print the two consecutive numbers whose difference is N ; Iterate in the range [ 0 , 10 ^ 5 ] ; Function to check if N is a perfect cube ; Find floating point value of square root of x . ; If square root is an integer ; Function to check whether a number can be represented as difference of two consecutive cubes ; Check if 12 * N - 3 is a perfect square or not ; Driver Code ; Given Number N
using System ; class GFG { static void print ( int N ) { for ( int i = 0 ; i < 100000 ; i ++ ) { if ( Math . Pow ( i + 1 , 3 ) - Math . Pow ( i , 3 ) == N ) { int j = i + 1 ; Console . WriteLine ( i + " ▁ " + j ) ; return ; } } } static bool isPerfectSquare ( double x ) { double sr = Math . Sqrt ( x ) ; return ( ( sr - Math . Floor ( sr ) ) == 0 ) ; } static bool diffCube ( int N ) { return isPerfectSquare ( 12 * N - 3 ) ; } public static void Main ( String [ ] args ) { int N = 19 ; if ( diffCube ( N ) ) { Console . WriteLine ( " Yes " ) ; print ( N ) ; } else { Console . WriteLine ( " No " ) ; } } }
Sum of bit differences for numbers from 0 to N | Set 2 | C # program to find the sum of bit differences between consecutive numbers from 0 to N using recursion ; Recursive function to find sum of different bits between consecutive numbers from 0 to N ; Base case ; Calculate the Nth term ; Driver Code ; Given number ; Function call
using System ; class GFG { static int totalCountDifference ( int n ) { if ( n == 1 ) return 1 ; return n + totalCountDifference ( n / 2 ) ; } public static void Main ( ) { int N = 5 ; Console . WriteLine ( totalCountDifference ( N ) ) ; } }
Sum of elements of a Geometric Progression ( GP ) in a given range | C # program to find the sum of elements of an GP in the given range ; Function to find sum in the given range ; Find the value of k ; Find the common difference ; Find the sum ; Driver Code
using System ; class GFG { static int findSum ( int [ ] arr , int n , int left , int right ) { int k = right - left + 1 ; int d = arr [ 1 ] / arr [ 0 ] ; int ans = arr [ left - 1 ] ; if ( d == 1 ) ans = ans * d * k ; else ans = ans * ( ( int ) Math . Pow ( d , k ) - 1 / ( d - 1 ) ) ; return ans ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 } ; int queries = 3 ; int [ , ] q = { { 2 , 4 } , { 2 , 6 } , { 5 , 8 } } ; int n = arr . Length ; for ( int i = 0 ; i < queries ; i ++ ) Console . Write ( findSum ( arr , n , q [ i , 0 ] , q [ i , 1 ] ) + " STRNEWLINE " ) ; } }
Find the first and last M digits from K | C # program to implement the above approach ; Function to find a ^ b modulo M ; Function to find the first and last M digits from N ^ K ; Calculate Last M digits ; Calculate First M digits ; Extract the number after decimal ; Find 10 ^ y ; Move the Decimal Point M - 1 digits forward ; Print the result ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static long modPower ( long a , long b , long M ) { long res = 1 ; while ( b > 0 ) { if ( b % 2 == 1 ) res = res * a % M ; a = a * a % M ; b >>= 1 ; } return res ; } static void findFirstAndLastM ( long N , long K , long M ) { long lastM = modPower ( N , K , ( 1L ) * ( long ) Math . Pow ( 10 , M ) ) ; long firstM ; double y = ( double ) K * Math . Log10 ( N * 1.0 ) ; y = y - ( long ) y ; double temp = Math . Pow ( 10.0 , y ) ; firstM = ( long ) ( temp * ( 1L ) * Math . Pow ( 10 , ( M - 1 ) ) ) ; Console . Write ( firstM + " ▁ " + lastM + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { long N = 12 , K = 12 , M = 4 ; findFirstAndLastM ( N , K , M ) ; } }
Minimize increment / decrement of Array elements to make each modulo K equal | C # program for the above approach ; Function to find the minimum operations required to make the modulo of each element of the array equal to each other ; Variable to store minimum operation required ; To store operation required to make all modulo equal ; Iterating through all possible modulo value ; Iterating through all different modulo obtained so far ; Calculating oprn required to make all modulos equal to x ; Checking the operations that will cost less ; Check operation that will cost less ; Update the minimum number of operations ; Returning the answer ; Function to store different modulos ; Set to store all different modulo ; Map to store count of all different modulo obtained ; Storing all different modulo count ; Insert into the set ; Increment count ; Function call to return value of min oprn required ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int Find_min ( HashSet < int > diff_mod , Dictionary < int , int > count_mod , int k ) { int min_oprn = int . MaxValue ; int oprn = 0 ; for ( int x = 0 ; x < k ; x ++ ) { oprn = 0 ; foreach ( int w in diff_mod ) { if ( w != x ) { if ( w == 0 ) { oprn += Math . Min ( x , k - x ) * count_mod [ w ] ; } else { oprn += Math . Min ( Math . Abs ( x - w ) , k + x - w ) * count_mod [ w ] ; } } } if ( oprn < min_oprn ) min_oprn = oprn ; } return min_oprn ; } static int Cal_min ( int [ ] arr , int n , int k ) { HashSet < int > diff_mod = new HashSet < int > ( ) ; Dictionary < int , int > count_mod = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { diff_mod . Add ( arr [ i ] % k ) ; if ( count_mod . ContainsKey ( ( arr [ i ] % k ) ) ) count_mod [ arr [ i ] % k ] = count_mod [ ( arr [ i ] % k ) ] + 1 ; else count_mod . Add ( arr [ i ] % k , 1 ) ; } return Find_min ( diff_mod , count_mod , k ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 35 , 48 , 23 , 52 } ; int n = arr . Length ; int k = 3 ; Console . Write ( Cal_min ( arr , n , k ) ) ; } }
Minimize Steps required to obtain Sorted Order of an Array | C # program to implement the above approach ; Function to find GCD of two numbers ; Function to calculate the LCM of array elements ; Initialize result ; Function to find minimum steps required to obtain sorted sequence ; Inititalize dat [ ] array for Direct Address Table . ; Calculating steps required for each element to reach its sorted position ; Calculate LCM of the array ; 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 findlcm ( int [ ] arr , int n ) { int ans = 1 ; for ( int i = 1 ; i <= n ; i ++ ) ans = ( ( ( arr [ i ] * ans ) ) / ( gcd ( arr [ i ] , ans ) ) ) ; return ans ; } static void minimumSteps ( int [ ] arr , int n ) { int i ; int [ ] dat = new int [ n + 1 ] ; for ( i = 1 ; i <= n ; i ++ ) dat [ arr [ i - 1 ] ] = i ; int [ ] b = new int [ n + 1 ] ; int j = 0 , c ; for ( i = 1 ; i <= n ; i ++ ) { c = 1 ; j = dat [ i ] ; while ( j != i ) { c ++ ; j = dat [ j ] ; } b [ i ] = c ; } Console . WriteLine ( findlcm ( b , n ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 1 , 4 , 3 , 2 , 7 , 6 } ; int N = arr . Length ; minimumSteps ( arr , N ) ; } }