codeparrot/xlcost-text-to-code huggingface dataset:
Find k pairs with smallest sums in two arrays | C # code to print first k pairs with least with least sum from two arrays . ; Function to find k pairs with least sum such that one elemennt of a pair is from arr1 [ ] and other element is from arr2 [ ] ; Stores current index in arr2 [ ] for every element of arr1 [ ] . Initially all values are considered 0. Here current index is the index before which all elements are considered as part of output . ; Initialize current pair sum as infinite ; To pick next pair , traverse for all elements of arr1 [ ] , for every element , find corresponding current element in arr2 [ ] and pick minimum of all formed pairs . ; Check if current element of arr1 [ ] plus element of array2 to be used gives minimum sum ; Update index that gives minimum ; update minimum sum ; Driver code
using System ; class KSmallestPair { static void kSmallestPair ( int [ ] arr1 , int n1 , int [ ] arr2 , int n2 , int k ) { if ( k > n1 * n2 ) { Console . Write ( " k ▁ pairs ▁ don ' t ▁ exist " ) ; return ; } int [ ] index2 = new int [ n1 ] ; while ( k > 0 ) { int min_sum = int . MaxValue ; int min_index = 0 ; for ( int i1 = 0 ; i1 < n1 ; i1 ++ ) { if ( index2 [ i1 ] < n2 && arr1 [ i1 ] + arr2 [ index2 [ i1 ] ] < min_sum ) { min_index = i1 ; min_sum = arr1 [ i1 ] + arr2 [ index2 [ i1 ] ] ; } } Console . Write ( " ( " + arr1 [ min_index ] + " , ▁ " + arr2 [ index2 [ min_index ] ] + " ) ▁ " ) ; index2 [ min_index ] ++ ; k -- ; } } public static void Main ( String [ ] args ) { int [ ] arr1 = { 1 , 3 , 11 } ; int n1 = arr1 . Length ; int [ ] arr2 = { 2 , 4 , 8 } ; int n2 = arr2 . Length ; int k = 4 ; kSmallestPair ( arr1 , n1 , arr2 , n2 , k ) ; } }
Maximize count of strings of length 3 that can be formed from N 1 s and M 0 s | C # program for the above approach ; Function that counts the number of Strings of length 3 that can be made with given m 0 s and n 1 s ; Print the count of Strings ; Driver Code ; Given count of 1 s and 0 s ; Function Call
using System ; class GFG { static void number_of_Strings ( int N , int M ) { Console . Write ( Math . Min ( N , Math . Min ( M , ( N + M ) / 3 ) ) ) ; } public static void Main ( String [ ] args ) { int N = 4 , M = 19 ; number_of_Strings ( N , M ) ; } }
Maximize count of subsets having product of smallest element and size of the subset at least X | C # program to implement the above approach ; Function to return the maximum count of subsets possible which satisfy the above condition ; Sort the array in descending order ; Stores the count of subsets ; Stores the size of the current subset ; Check for the necessary conditions ; Driver Code
using System ; using System . Linq ; class GFG { static int maxSubset ( int [ ] arr , int N , int X ) { Array . Sort ( arr ) ; Array . Reverse ( arr ) ; int counter = 0 ; int sz = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sz ++ ; if ( arr [ i ] * sz >= X ) { counter ++ ; sz = 0 ; } } return counter ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 11 , 2 , 9 , 5 } ; int N = arr . Length ; int X = 10 ; Console . Write ( maxSubset ( arr , N , X ) ) ; } }
Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array | C # Program to implement the above approach ; Function to find the maximum value possible of the given expression from all permutations of the array ; Stores the minimum value from the array ; Return the answer ; Driver Code
using System ; class GFG { static int maximumModuloValue ( int [ ] A , int n ) { int mn = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { mn = Math . Min ( A [ i ] , mn ) ; } return mn ; } public static void Main ( String [ ] args ) { int [ ] A = { 7 , 10 , 12 } ; int n = A . Length ; Console . WriteLine ( maximumModuloValue ( A , n ) ) ; } }
Final Matrix after incrementing submatrices by K in range given by Q queries | C # program for the above approach ; Query data type ; Function to update the given query ; Update top cell ; Update bottom left cell ; Update bottom right cell ; Update top right cell ; Function that updates the matrix [ , ] mat by adding elements of aux [ , ] ; Compute the prefix sum of all columns ; Compute the prefix sum of all rows ; Get the readonly matrix by adding mat and aux matrix at each cell ; Function that prints matrix [ ] mat ; Traverse each row ; Traverse each columns ; Function that performs each query in the given matrix and print the updated matrix after each operation performed ; Initialize all elements to 0 ; Update auxiliary matrix by traversing each query ; Update Query ; Compute the readonly answer ; Print the updated matrix ; Driver Code ; Given Matrix ; Given Queries ; Function call
using System ; class GFG { static readonly int N = 3 ; static readonly int M = 4 ; class query { public int x1 , x2 , y1 , y2 , K ; public query ( int x1 , int x2 , int y1 , int y2 , int k ) { this . x1 = x1 ; this . x2 = x2 ; this . y1 = y1 ; this . y2 = y2 ; K = k ; } } ; static void updateQuery ( int from_x , int from_y , int to_x , int to_y , int k , int [ , ] aux ) { aux [ from_x , from_y ] += k ; if ( to_x + 1 < N ) aux [ to_x + 1 , from_y ] -= k ; if ( to_x + 1 < N && to_y + 1 < M ) aux [ to_x + 1 , to_y + 1 ] += k ; if ( to_y + 1 < M ) aux [ from_x , to_y + 1 ] -= k ; } static void updateMatrix ( int [ , ] mat , int [ , ] aux ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { aux [ i , j ] += aux [ i , j - 1 ] ; } } for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 1 ; j < N ; j ++ ) { aux [ j , i ] += aux [ j - 1 , i ] ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { mat [ i , j ] += aux [ i , j ] ; } } } static void printMatrix ( int [ , ] mat ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { Console . Write ( mat [ i , j ] + " ▁ " ) ; } Console . Write ( " STRNEWLINE " ) ; } } static void matrixQuery ( int [ , ] mat , int Q , query [ ] q ) { int [ , ] aux = new int [ N , M ] ; for ( int i = 0 ; i < Q ; i ++ ) { updateQuery ( q [ i ] . x1 , q [ i ] . x2 , q [ i ] . y1 , q [ i ] . y2 , q [ i ] . K , aux ) ; } updateMatrix ( mat , aux ) ; printMatrix ( mat ) ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 0 , 1 , 2 } , { 0 , 2 , 4 , 1 } , { 1 , 2 , 1 , 0 } } ; int Q = 1 ; query [ ] q = { new query ( 0 , 0 , 1 , 1 , 2 ) } ; matrixQuery ( mat , Q , q ) ; } }
Find Second largest element in an array | C # program to find second largest element in an array ; Function to print the second largest elements ; There should be atleast two elements ; Sort the array ; Start from second last element as the largest element is at last ; If the element is not equal to largest element ; Driver code
using System ; class GFG { static void print2largest ( int [ ] arr , int arr_size ) { int i ; if ( arr_size < 2 ) { Console . Write ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } Array . Sort ( arr ) ; for ( i = arr_size - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] != arr [ arr_size - 1 ] ) { Console . Write ( " The ▁ second ▁ largest ▁ " + " element ▁ is ▁ { 0 } STRNEWLINE " , arr [ i ] ) ; return ; } } Console . Write ( " There ▁ is ▁ no ▁ second ▁ " + " largest ▁ element STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , 35 , 1 , 10 , 34 , 1 } ; int n = arr . Length ; print2largest ( arr , n ) ; } }
Find sum of all left leaves in a given Binary Tree | C # program to find sum of all left leaves ; A binary tree node ; constructor to create a new Node ; Return the sum of left leaf nodes ; A queue of pairs to do bfs traversal and keep track if the node is a left or right child if bool value is true then it is a left child . ; do bfs traversal ; if temp is a leaf node and left child of its parent ; if it is not leaf then push its children nodes into queue ; bool value is true here because it is left child of its parent ; bool value is false here because it is right child of its parent ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { public class Node { public int key ; public Node left , right ; public Node ( int key_ ) { key = key_ ; left = null ; right = null ; } } ; public class pair { public Node first ; public bool second ; public pair ( Node first , bool second ) { this . first = first ; this . second = second ; } } static int sumOfLeftLeaves ( Node root ) { if ( root == null ) return 0 ; Queue < pair > q = new Queue < pair > ( ) ; q . Enqueue ( new pair ( root , false ) ) ; int sum = 0 ; while ( q . Count != 0 ) { Node temp = q . Peek ( ) . first ; bool is_left_child = q . Peek ( ) . second ; q . Dequeue ( ) ; if ( is_left_child ) sum = sum + temp . key ; if ( temp . left != null && temp . right != null && is_left_child ) sum = sum - temp . key ; if ( temp . left != null ) { q . Enqueue ( new pair ( temp . left , true ) ) ; } if ( temp . right != null ) { q . Enqueue ( new pair ( temp . right , false ) ) ; } } return sum ; } public static void Main ( String [ ] args ) { Node root = new Node ( 20 ) ; root . left = new Node ( 9 ) ; root . right = new Node ( 49 ) ; root . right . left = new Node ( 23 ) ; root . right . right = new Node ( 52 ) ; root . right . right . left = new Node ( 50 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 12 ) ; root . left . right . right = new Node ( 12 ) ; Console . Write ( " Sum ▁ of ▁ left ▁ leaves ▁ is ▁ " + sumOfLeftLeaves ( root ) + " STRNEWLINE " ) ; } }
Count of nested polygons that can be drawn by joining vertices internally | C # program for the above approach ; Function that counts the nested polygons inside another polygons ; Stores the count ; Child polygons can only existss if parent polygon has sides > 5 ; Get next nested polygon ; Return the count ; Driver Code ; Given side of polygon ; Function call
using System ; class GFG { static int countNestedPolygons ( int sides ) { int count = 0 ; while ( sides > 5 ) { sides /= 2 ; count += 1 ; } return count ; } public static void Main ( String [ ] args ) { int N = 12 ; Console . Write ( countNestedPolygons ( N ) ) ; } }
Find Second largest element in an array | C # program to find second largest element in an array ; Function to print the second largest elements ; There should be atleast two elements ; Find the largest element ; Find the second largest element ; Driver code
using System ; class GFG { static void print2largest ( int [ ] arr , int arr_size ) { int i , second ; if ( arr_size < 2 ) { Console . Write ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } int largest = second = int . MinValue ; for ( i = 0 ; i < arr_size ; i ++ ) { largest = Math . Max ( largest , arr [ i ] ) ; } for ( i = 0 ; i < arr_size ; i ++ ) { if ( arr [ i ] != largest ) second = Math . Max ( second , arr [ i ] ) ; } if ( second == int . MinValue ) Console . Write ( " There ▁ is ▁ no ▁ second ▁ " + " largest ▁ element STRNEWLINE " ) ; else Console . Write ( " The ▁ second ▁ largest ▁ " + " element ▁ is ▁ { 0 } STRNEWLINE " , second ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , 35 , 1 , 10 , 34 , 1 } ; int n = arr . Length ; print2largest ( arr , n ) ; } }
Find Second largest element in an array | C # Code for Find Second largest element in an array ; Function to print the second largest elements ; There should be atleast two elements ; If current element is smaller than first then update both first and second ; If arr [ i ] is in between first and second then update second ; Driver Code
using System ; class GFG { public static void print2largest ( int [ ] arr , int arr_size ) { int i , first , second ; if ( arr_size < 2 ) { Console . WriteLine ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } first = second = int . MinValue ; for ( i = 0 ; i < arr_size ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second && arr [ i ] != first ) second = arr [ i ] ; } if ( second == int . MinValue ) Console . Write ( " There ▁ is ▁ no ▁ second ▁ largest " + " ▁ element STRNEWLINE " ) ; else Console . Write ( " The ▁ second ▁ largest ▁ element " + " ▁ is ▁ " + second ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , 35 , 1 , 10 , 34 , 1 } ; int n = arr . Length ; print2largest ( arr , n ) ; } }
Queries to count characters having odd frequency in a range [ L , R ] | C # program to implement the above problem ; Function to print the number of characters having odd frequencies for each query ; A function to construct the [ ] arr and prefix [ ] ; Stores array length ; Stores the unique powers of 2 associated to each character ; Prefix array to store the XOR values from array elements ;  ; Driver Code
using System ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void queryResult ( int [ ] prefix , pair Q ) { int l = Q . first ; int r = Q . second ; if ( l == 0 ) { int xorval = prefix [ r ] ; Console . Write ( countSetBits ( xorval ) + " STRNEWLINE " ) ; } else { int xorval = prefix [ r ] ^ prefix [ l - 1 ] ; Console . Write ( countSetBits ( xorval ) + " STRNEWLINE " ) ; } } static void calculateCount ( String S , pair [ ] Q , int m ) { int n = S . Length ; int [ ] arr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = ( 1 << ( S [ i ] - ' a ' ) ) ; } int [ ] prefix = new int [ n ] ; int x = 0 ; for ( int i = 0 ; i < n ; i ++ ) { x ^= arr [ i ] ; prefix [ i ] = x ; } for ( int i = 0 ; i < m ; i ++ ) { queryResult ( prefix , Q [ i ] ) ; } } static int countSetBits ( long x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } public static void Main ( String [ ] args ) { String S = " geeksforgeeks " ; pair [ ] Q = { new pair ( 2 , 4 ) , new pair ( 0 , 3 ) , new pair ( 0 , 12 ) } ; calculateCount ( S , Q , 3 ) ; } }
Count of Binary Strings possible as per given conditions | C # program to implement the above approach ; Function to generate maximum possible strings that can be generated ; Maximum possible strings ; Driver Code
using System ; class GFG { static long countStrings ( long A , long B , long K ) { long X = ( A + B ) / ( K + 1 ) ; return ( Math . Min ( A , Math . Min ( B , X ) ) * ( K + 1 ) ) ; } public static void Main ( string [ ] args ) { long N = 101 , M = 231 , K = 15 ; Console . Write ( countStrings ( N , M , K ) ) ; } }
Construct a Matrix N x N with first N ^ 2 natural numbers for an input N | C # program for the above approach ; Function to print the desired matrix ; Iterate ove all [ 0 , N ] ; If is even ; If row number is even print the row in forward order ; If row number is odd print the row in reversed order ; Driver code ; Given matrix size ; Function call
using System ; class GFG { static void UniqueMatrix ( int N ) { int element_value = 1 ; int i = 0 ; while ( i < N ) { if ( i % 2 == 0 ) { for ( int f = element_value ; f < element_value + N ; f ++ ) { Console . Write ( f + " ▁ " ) ; } element_value += N ; } else { for ( int k = element_value + N - 1 ; k > element_value - 1 ; k -- ) { Console . Write ( k + " ▁ " ) ; } element_value += N ; } Console . WriteLine ( ) ; i = i + 1 ; } } static public void Main ( ) { int N = 4 ; UniqueMatrix ( N ) ; } }
Maximum and minimum of an array using minimum number of comparisons | C # program of above implementation ; Class Pair is used to return two values from getMinMax ( ) ; If there is only one element then return it as min and max both ; If there are more than one elements , then initialize min and max ; Driver Code
using System ; class GFG { class Pair { public int min ; public int max ; } static Pair getMinMax ( int [ ] arr , int n ) { Pair minmax = new Pair ( ) ; int i ; if ( n == 1 ) { minmax . max = arr [ 0 ] ; minmax . min = arr [ 0 ] ; return minmax ; } if ( arr [ 0 ] > arr [ 1 ] ) { minmax . max = arr [ 0 ] ; minmax . min = arr [ 1 ] ; } else { minmax . max = arr [ 1 ] ; minmax . min = arr [ 0 ] ; } for ( i = 2 ; i < n ; i ++ ) { if ( arr [ i ] > minmax . max ) { minmax . max = arr [ i ] ; } else if ( arr [ i ] < minmax . min ) { minmax . min = arr [ i ] ; } } return minmax ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1000 , 11 , 445 , 1 , 330 , 3000 } ; int arr_size = 6 ; Pair minmax = getMinMax ( arr , arr_size ) ; Console . Write ( " Minimum ▁ element ▁ is ▁ { 0 } " , minmax . min ) ; Console . Write ( " element is { 0 } " , minmax . max ) ; } }
Find sum of all nodes of the given perfect binary tree | C # program to implement the above approach ; function to find sum of all of the nodes of given perfect binary tree ; no of leaf nodes ; list of vector to store nodes of all of the levels ; initialize ; store the nodes of last level i . e . , the leaf nodes ; store nodes of rest of the level by moving in bottom - up manner ; loop to calculate values of parent nodes from the children nodes of lower level ; store the value of parent node as sum of children nodes ; traverse the list of vector and calculate the sum ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { public static int sumNodes ( int l ) { int leafNodeCount = ( int ) Math . Pow ( 2 , l - 1 ) ; List < List < int > > vec = new List < List < int > > ( ) ; for ( int i = 1 ; i <= l ; i ++ ) { vec . Add ( new List < int > ( ) ) ; } for ( int i = 1 ; i <= leafNodeCount ; i ++ ) { vec [ l - 1 ] . Add ( i ) ; } for ( int i = l - 2 ; i >= 0 ; i -- ) { int k = 0 ; while ( k < vec [ i + 1 ] . Count - 1 ) { vec [ i ] . Add ( vec [ i + 1 ] [ k ] + vec [ i + 1 ] [ k + 1 ] ) ; k += 2 ; } } int sum = 0 ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = 0 ; j < vec [ i ] . Count ; j ++ ) { sum += vec [ i ] [ j ] ; } } return sum ; } public static void Main ( string [ ] args ) { int l = 3 ; Console . WriteLine ( sumNodes ( l ) ) ; } }
Maximum and minimum of an array using minimum number of comparisons | C # program of above implementation ; Class Pair is used to return two values from getMinMax ( ) ; If array has even number of elements then initialize the first two elements as minimum and maximum ; set the starting index for loop ; If array has odd number of elements then initialize the first element as minimum and maximum ; set the starting index for loop ; In the while loop , pick elements in pair and compare the pair with max and min so far ; Increment the index by 2 as two elements are processed in loop ; Driver Code
using System ; class GFG { public class Pair { public int min ; public int max ; } static Pair getMinMax ( int [ ] arr , int n ) { Pair minmax = new Pair ( ) ; int i ; if ( n % 2 == 0 ) { if ( arr [ 0 ] > arr [ 1 ] ) { minmax . max = arr [ 0 ] ; minmax . min = arr [ 1 ] ; } else { minmax . min = arr [ 0 ] ; minmax . max = arr [ 1 ] ; } i = 2 ; } else { minmax . min = arr [ 0 ] ; minmax . max = arr [ 0 ] ; i = 1 ; } while ( i < n - 1 ) { if ( arr [ i ] > arr [ i + 1 ] ) { if ( arr [ i ] > minmax . max ) { minmax . max = arr [ i ] ; } if ( arr [ i + 1 ] < minmax . min ) { minmax . min = arr [ i + 1 ] ; } } else { if ( arr [ i + 1 ] > minmax . max ) { minmax . max = arr [ i + 1 ] ; } if ( arr [ i ] < minmax . min ) { minmax . min = arr [ i ] ; } } i += 2 ; } return minmax ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1000 , 11 , 445 , 1 , 330 , 3000 } ; int arr_size = 6 ; Pair minmax = getMinMax ( arr , arr_size ) ; Console . Write ( " Minimum ▁ element ▁ is ▁ { 0 } " , minmax . min ) ; Console . Write ( " element is { 0 } " , minmax . max ) ; } }
MO 's Algorithm (Query Square Root Decomposition) | Set 1 (Introduction) | C # program to compute sum of ranges for different range queries ; Class to represent a query range ; Prints sum of all query ranges . m is number of queries n is the size of the array . ; One by one compute sum of all queries ; Left and right boundaries of current range ; Compute sum of current query range ; Print sum of current query range ; Driver code
using System ; using System . Collections ; public class Query { public int L ; public int R ; public Query ( int L , int R ) { this . L = L ; this . R = R ; } } class GFG { static void printQuerySums ( int [ ] a , int n , ArrayList q , int m ) { for ( int i = 0 ; i < m ; i ++ ) { int L = ( ( Query ) q [ i ] ) . L , R = ( ( Query ) q [ i ] ) . R ; int sum = 0 ; for ( int j = L ; j <= R ; j ++ ) sum += a [ j ] ; Console . Write ( " Sum ▁ of ▁ [ " + L + " , ▁ " + R + " ] ▁ is ▁ " + sum + " STRNEWLINE " ) ; } } public static void Main ( string [ ] argv ) { int [ ] a = { 1 , 1 , 2 , 1 , 3 , 4 , 5 , 2 , 8 } ; int n = a . Length ; ArrayList q = new ArrayList ( ) ; q . Add ( new Query ( 0 , 4 ) ) ; q . Add ( new Query ( 1 , 3 ) ) ; q . Add ( new Query ( 2 , 4 ) ) ; int m = q . Count ; printQuerySums ( a , n , q , m ) ; } }
Count of unique palindromic strings of length X from given string | C # implementation to count different palindromic string of length X from the given string S ; Function to count different palindromic string of length X from the given string S ; Base case ; Create the frequency array ; Count the frequency in the string ; Store frequency of the char ; Check the frequency which is greater than zero ; No . of different char we can put at the position of the i and x - i ; Iterator pointing to the last element of the set ; Decrease the value of the char we put on the position i and n - i ; Different no of char we can put at the position x / 2 ; Return total no of different string ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int findways ( string s , int x ) { if ( x > s . Length ) return 0 ; int n = s . Length ; int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < n ; ++ i ) freq [ s [ i ] - ' a ' ] ++ ; multiset < int > se ; HashSet < int > se = new HashSet < int > ( ) ; for ( int i = 0 ; i < 26 ; ++ i ) if ( freq [ i ] > 0 ) se . Add ( freq [ i ] ) ; int ans = 1 ; for ( int i = 0 ; i < x / 2 ; ++ i ) { int count = 0 ; foreach ( int u in se ) { if ( u >= 2 ) count ++ ; } if ( count == 0 ) return 0 ; else ans = ans * count ; int [ ] arr = new int [ se . Count ] ; se . CopyTo ( arr ) ; int p = arr [ se . Count - 1 ] ; int val = p ; se . Remove ( p ) ; if ( val > 2 ) se . Add ( val - 2 ) ; } if ( x % 2 != 0 ) { int count = 0 ; foreach ( int u in se ) if ( u > 0 ) count ++ ; ans = ans * count ; } return ans ; } static public void Main ( ) { string s = " aaa " ; int x = 2 ; Console . WriteLine ( findways ( s , x ) ) ; } }
Min operations to reduce N to 1 by multiplying by A or dividing by B | C # program for the above approach ; Function to check if it is possible to convert a number N to 1 by a minimum use of the two operations ; For the Case b % a != 0 ; Check if n equal to 1 ; Check if n is not divisible by b ; Initialize a variable ' c ' ; Loop until n is divisible by b ; Count number of divisions ; Loop until n is divisible by c ; Count number of operations ; Check if n is reduced to 1 ; Count steps ; Return the total number of steps ; Driver Code ; Given n , a and b ; Function call
using System ; class GFG { static int findIfPossible ( int n , int a , int b ) { if ( b % a != 0 ) { if ( n == 1 ) return 0 ; else if ( n % b != 0 ) return - 1 ; else return ( int ) n / b ; } int c = b / a ; int x = 0 , y = 0 ; while ( n % b == 0 ) { n = n / b ; x ++ ; } while ( n % c == 0 ) { n = n / c ; y ++ ; } if ( n == 1 ) { int total_steps = x + ( 2 * y ) ; return total_steps ; } else return - 1 ; } public static void Main ( ) { int n = 48 ; int a = 3 , b = 12 ; Console . WriteLine ( findIfPossible ( n , a , b ) ) ; } }