codeparrot/xlcost-text-to-code huggingface dataset:
Product of all the Composite Numbers in an array | C # program to find the product of all the composite numbers in an array ; Function that returns the the product of all composite numbers ; Find maximum value in the array ; Use sieve to find all prime numbers less than or equal to max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Set 0 and 1 as primes as they don 't need to be  counted as composite numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Find the product of all composite numbers in the arr [ ] ; Driver code
using System ; using System . Linq ; public class GFG { static int compositeProduct ( int [ ] arr , int n ) { int max_val = arr . Max ( ) ; bool [ ] prime = new bool [ max_val + 1 ] ; for ( int i = 0 ; i < max_val + 1 ; i ++ ) prime [ i ] = true ; prime [ 0 ] = true ; prime [ 1 ] = true ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) { prime [ i ] = false ; } } } int product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! prime [ arr [ i ] ] ) { product *= arr [ i ] ; } } return product ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 5 , 6 , 7 } ; int n = arr . Length ; Console . WriteLine ( compositeProduct ( arr , n ) ) ; } }
Primality test for the sum of digits at odd places of a number | C # program to do Primality test for the sum of digits at odd places of a number ; Function that return sum of the digits at odd places ; Function that returns true if the number is prime else false ; Corner cases ; This condition is checked so that we can skip middle five numbers in the below loop ; Driver code ; Get the sum of the digits at odd places
using System ; public class GFG { static int sum_odd ( int n ) { int sum = 0 , pos = 1 ; while ( n > 0 ) { if ( pos % 2 == 1 ) sum += n % 10 ; n = n / 10 ; pos ++ ; } return sum ; } static bool check_prime ( 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 public void Main ( ) { int n = 223 ; int sum = sum_odd ( n ) ; if ( check_prime ( sum ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Find amount to be added to achieve target ratio in a given mixture | C # program to find amount of water to be added to achieve given target ratio . ; Driver code
using System ; class GFG { public static double findAmount ( double X , double W , double Y ) { return ( X * ( Y - W ) ) / ( 100 - Y ) ; } public static void Main ( ) { double X = 100 , W = 50 , Y = 60 ; Console . WriteLine ( " Water ▁ to ▁ be ▁ added ▁ = ▁ { 0 } " , findAmount ( X , W , Y ) ) ; } }
Check if a number is a Mystery Number | C # implementation of above approach ; Finds reverse of given num x . ; if found print the pair , return
using System ; class GFG { static int reverseNum ( int x ) { string s = x . ToString ( ) ; string str = " " ; for ( int i = s . Length - 1 ; i >= 0 ; i -- ) { str = str + s [ i ] ; } int rev = Int32 . Parse ( str ) ; return rev ; } static bool isMysteryNumber ( int n ) { for ( int i = 1 ; i <= n / 2 ; i ++ ) { int j = reverseNum ( i ) ; if ( i + j == n ) { Console . WriteLine ( i + " ▁ " + j ) ; return true ; } } Console . WriteLine ( " Not ▁ a ▁ Mystery ▁ Number " ) ; return false ; } public static void Main ( ) { int n = 121 ; isMysteryNumber ( n ) ; } }
Replace every element of the array by product of all other elements | C # program to Replace every element by the product of all other elements ; Calculate the product of all the elements ; Replace every element product of all other elements ; Driver Code ; Print the modified array .
using System ; class GFG { static void ReplaceElements ( int [ ] arr , int n ) { int prod = 1 ; for ( int i = 0 ; i < n ; ++ i ) { prod *= arr [ i ] ; } for ( int i = 0 ; i < n ; ++ i ) { arr [ i ] = prod / arr [ i ] ; } } static public void Main ( ) { int [ ] arr = { 2 , 3 , 3 , 5 , 7 } ; int n = arr . Length ; ReplaceElements ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) { Console . Write ( arr [ i ] + " ▁ " ) ; } Console . WriteLine ( " " ) ; } }
Check if there is any pair in a given range with GCD is divisible by k |  ; function to count such possible numbers ; if i is divisible by k ; if count of such numbers is greater than one ; Driver code
using System ; class GFG { public bool Check_is_possible ( int l , int r , int k ) { int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( i % k == 0 ) count ++ ; } return ( count > 1 ) ; } public static void Main ( ) { GFG g = new GFG ( ) ; int l = 4 , r = 12 ; int k = 5 ; if ( g . Check_is_possible ( l , r , k ) ) Console . WriteLine ( " YES STRNEWLINE " ) ; else Console . WriteLine ( " NO STRNEWLINE " ) ; } }
Check if any permutation of N equals any power of K | C # implementation of above approach . ; Function to check if N and K are anagrams ; Function to check if any permutation of N exist such that it is some power of K ; Generate all power of K under 10 ^ 18 ; Check if any power of K is valid ; Driver code ; Function call to print required answer
using System ; using System . Collections . Generic ; class GfG { static bool isValid ( long N , int K ) { HashSet < long > m1 = new HashSet < long > ( ) ; HashSet < int > m2 = new HashSet < int > ( ) ; while ( N > 0 ) { m1 . Add ( N % 10 ) ; N /= 10 ; } while ( K > 0 ) { m2 . Add ( K % 10 ) ; K /= 10 ; } if ( m1 . Equals ( m2 ) ) { return true ; } return false ; } static String anyPermutation ( long N , int K ) { int [ ] powK = new int [ 100 + 1 ] ; int Limit = ( int ) Math . Pow ( 10 , 18 ) ; powK [ 0 ] = K ; int i = 1 ; while ( powK [ i - 1 ] * K < Limit && i < 100 ) { powK [ i ] = powK [ i - 1 ] * K ; i ++ ; } for ( int j = 0 ; j < i ; j ++ ) { if ( ! isValid ( N , powK [ j ] ) ) { return " True " ; } } return " False " ; } public static void Main ( String [ ] args ) { long N = 96889010407 ; int K = 7 ; Console . WriteLine ( anyPermutation ( N , K ) ) ; } }
Sum of first N natural numbers which are divisible by 2 and 7 | C # program to find sum of numbers from 1 to N which are divisible by 2 or 7 ; Function to calculate the sum of numbers divisible by 2 or 7 ; Driver code
using System ; class GFG { public static int sum ( int N ) { int S1 , S2 , S3 ; S1 = ( ( N / 2 ) ) * ( 2 * 2 + ( N / 2 - 1 ) * 2 ) / 2 ; S2 = ( ( N / 7 ) ) * ( 2 * 7 + ( N / 7 - 1 ) * 7 ) / 2 ; S3 = ( ( N / 14 ) ) * ( 2 * 14 + ( N / 14 - 1 ) * 14 ) / 2 ; return S1 + S2 - S3 ; } public static int Main ( ) { int N = 20 ; Console . WriteLine ( sum ( N ) ) ; return 0 ; } }
Ways to color a skewed tree such that parent and child have different colors | C # program to count number of ways to color a N node skewed tree with k colors such that parent and children have different colors ; fast_way is recursive method to calculate power ; Driver code
using System ; class GFG { static int fastPow ( int N , int K ) { if ( K == 0 ) return 1 ; int temp = fastPow ( N , K / 2 ) ; if ( K % 2 == 0 ) return temp * temp ; else return N * temp * temp ; } static int countWays ( int N , int K ) { return K * fastPow ( K - 1 , N - 1 ) ; } public static void Main ( ) { int N = 3 , K = 3 ; Console . WriteLine ( countWays ( N , K ) ) ; } }
Sum of nth terms of Modified Fibonacci series made by every pair of two arrays | C # program to find sum of n - th terms of a Fibonacci like series formed using first two terms of two arrays . ; if sum of first term is required ; if sum of second term is required ; fibonacci series used to find the nth term of every series ; as every b [ i ] term appears m times and every a [ i ] term also appears m times ; Driver Code ; m is the size of the array
using System ; class GFG { static int sumNth ( int [ ] A , int [ ] B , int m , int n ) { int res = 0 ; if ( n == 1 ) { for ( int i = 0 ; i < m ; i ++ ) res = res + A [ i ] ; } else if ( n == 2 ) { for ( int i = 0 ; i < m ; i ++ ) res = res + B [ i ] * m ; } else { int [ ] f = new int [ n ] ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; for ( int i = 2 ; i < n ; i ++ ) f [ i ] = f [ i - 1 ] + f [ i - 2 ] ; for ( int i = 0 ; i < m ; i ++ ) { res = res + ( m * ( B [ i ] * f [ n - 1 ] ) ) + ( m * ( A [ i ] * f [ n - 2 ] ) ) ; } } return res ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 } ; int [ ] B = { 4 , 5 , 6 } ; int n = 3 ; int m = A . Length ; Console . WriteLine ( sumNth ( A , B , m , n ) ) ; } }
Puzzle | Minimum distance for Lizard | C # program to find minimum distance to be travelled by lizard ; side of the cube ; understand from diagram ; understand from diagram ; minimum distance
using System ; class GFG { public static void Main ( ) { int a = 5 ; int AC = a ; int CE = 2 * a ; double shortestDistace = Math . Sqrt ( AC * AC + CE * CE ) ; Console . Write ( shortestDistace ) ; } }
Find Sum of Series 1 ^ 2 | C # Program to find sum of series 1 ^ 2 - 2 ^ 2 + 3 ^ 3 - 4 ^ 4 + ... ; Function to find sum of series ; If n is even ; If n is odd ; return the result ; Driver Code ; Get n ; Find the sum ; Get n ; Find the sum
using System ; class GFG { static int sum_of_series ( int n ) { int result = 0 ; if ( n % 2 == 0 ) { result = - ( n * ( n + 1 ) ) / 2 ; } else { result = ( n * ( n + 1 ) ) / 2 ; } return result ; } public static void Main ( ) { int n = 3 ; Console . WriteLine ( sum_of_series ( n ) ) ; n = 10 ; Console . WriteLine ( sum_of_series ( n ) ) ; } }
Check whether the given numbers are Cousin prime or not | C # Code for Cousin Prime Numbers ; Function to check if the given number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Returns true if n1 and n2 are Cousin primes ; Check if the numbers differ by 4 or not ; Check if both are prime number or not ; Driver program ; Get the 2 numbers ; Check the numbers for cousin prime
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 bool isCousinPrime ( int n1 , int n2 ) { if ( Math . Abs ( n1 - n2 ) != 4 ) { return false ; } else { return ( isPrime ( n1 ) && isPrime ( n2 ) ) ; } } public static void Main ( ) { int n1 = 7 , n2 = 11 ; if ( isCousinPrime ( n1 , n2 ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Chen Prime Number | C # program to check Chen Prime number ; Utility function to check if the given number is semi - prime or not ; Increment count of prime numbers ; If number is greater than 1 , add it to the count variable as it indicates the number remain is prime number ; Return '1' if count is equal to '2' else return '0' ; Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check chen prime number ; Driver code
using System ; class GFG { static bool isSemiPrime ( int num ) { int cnt = 0 ; for ( int i = 2 ; cnt < 2 && i * i <= num ; ++ i ) while ( num % i == 0 ) { num /= i ; ++ cnt ; } if ( num > 1 ) ++ cnt ; return cnt == 2 ? true : false ; } 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 bool isChenPrime ( int n ) { if ( isPrime ( n ) && ( isSemiPrime ( n + 2 ) || isPrime ( n + 2 ) ) ) return true ; else return false ; } public static void Main ( ) { int n = 7 ; if ( isChenPrime ( n ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Thabit number | C # code to check thabit number ; Utility function to check power of two ; function to check if the given number is Thabit Number ; Add 1 to the number ; Divide the number by 3 ; Check if the given number is power of 2 ; Driver Program ; Check if number is thabit number
using System ; class GFG { static bool isPowerOfTwo ( int n ) { return n != 0 && ( ( n & ( n - 1 ) ) == 0 ) ; } static bool isThabitNumber ( int n ) { n = n + 1 ; if ( n % 3 == 0 ) n = n / 3 ; else return false ; if ( isPowerOfTwo ( n ) ) return true ; else return false ; } public static void Main ( ) { int n = 47 ; if ( isThabitNumber ( n ) ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } }
Sum of all the prime numbers in a given range | C # Program to compute sum of prime number in a given range ; Suppose the constraint is N <= 1000 ; Declare an array for dynamic approach ; Method to compute the array ; Declare an extra array as arr ; Iterate the loop till sqrt ( n ) Time Complexity is O ( log ( n ) X sqrt ( n ) ) ; if ith element of arr is 0 i . e . marked as prime ; mark all of it 's multiples till N as non-prime  by setting the locations to 1 ; Update the array ' dp ' with the running sum of prime numbers within the range [ 1 , N ] Time Complexity is O ( n ) ; Here , dp [ i ] is the sum of all the prime numbers within the range [ 1 , i ] ; Driver code ; Compute dp
using System ; public class GFG { static int N = 1000 ; static long [ ] dp = new long [ N + 1 ] ; static void sieve ( ) { int [ ] arr = new int [ N + 1 ] ; arr [ 0 ] = 1 ; arr [ 1 ] = 1 ; for ( int i = 2 ; i <= Math . Sqrt ( N ) ; i ++ ) if ( arr [ i ] == 0 ) for ( int j = i * i ; j <= N ; j += i ) arr [ j ] = 1 ; long runningPrimeSum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( arr [ i ] == 0 ) runningPrimeSum += i ; dp [ i ] = runningPrimeSum ; } } public static void Main ( ) { int l = 4 , r = 13 ; sieve ( ) ; Console . WriteLine ( dp [ r ] - dp [ l - 1 ] ) ; } }
Smallest Integer to be inserted to have equal sums | C # program to find the smallest number to be added to make the sum of left and right subarrays equal ; Function to find the minimum value to be added ; Variable to store entire array sum ; Variables to store sum of subarray1 and subarray 2 ; minimum value to be added ; Traverse through the array ; Sum of both halves ; Calculate minimum number to be added ; Driver code ; Length of array
using System ; class GFG { static int findMinEqualSums ( int [ ] a , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; } int sum1 = 0 , sum2 = 0 ; int min = int . MaxValue ; for ( int i = 0 ; i < N ; i ++ ) { sum1 += a [ i ] ; sum2 = sum - sum1 ; if ( Math . Abs ( sum1 - sum2 ) < min ) { min = Math . Abs ( sum1 - sum2 ) ; } if ( min == 0 ) { break ; } } return min ; } public static void Main ( ) { int [ ] a = { 3 , 2 , 1 , 5 , 7 , 8 } ; int N = a . Length ; Console . WriteLine ( findMinEqualSums ( a , N ) ) ; } }
Sum of the first N Prime numbers | C # implementation of above solution ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Set all multiples of p to non - prime ; find the sum of 1 st N prime numbers ; count of prime numbers ; sum of prime numbers ; if the number is prime add it ; increase the count ; get to next number ; Driver code ; create the sieve ; find the value of 1 st n prime numbers
using System ; public class GFG { static double MAX = 10000 ; static bool [ ] prime = new bool [ ( int ) ( MAX + 1.0 ) ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i <= MAX ; i ++ ) prime [ i ] = true ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } } static int solve ( int n ) { int count = 0 , num = 1 ; long sum = 0 ; while ( count < n ) { if ( prime [ num ] ) { sum += num ; count ++ ; } num ++ ; } return ( int ) sum ; } static public void Main ( ) { SieveOfEratosthenes ( ) ; int n = 4 ; Console . WriteLine ( " Sum ▁ of ▁ 1st ▁ N ▁ prime ▁ numbers ▁ are ▁ : " + solve ( n ) ) ; } }
Implementation of Wilson Primality test | C # implementation to check if a number is prime or not using Wilson Primality Test ; Function to calculate the factorial ; Function to check if the number is prime or not ; Driver code
using System ; class GFG { static long fact ( int p ) { if ( p <= 1 ) return 1 ; return p * fact ( p - 1 ) ; } static long isPrime ( int p ) { if ( p == 4 ) return 0 ; return ( fact ( p >> 1 ) % p ) ; } static void Main ( ) { if ( isPrime ( 127 ) == 0 ) { Console . WriteLine ( 0 ) ; } else { Console . WriteLine ( 1 ) ; } } } }
Find the total Number of Digits in ( N ! ) N | C # program to find the total Number of Digits in ( N ! ) ^ N ; Function to find the total Number of Digits in ( N ! ) ^ N ; Finding X ; Calculating N * X ; Floor ( N * X ) + 1 equivalent to floor ( sum ) + 1 ; Driver code
using System ; class GFG { public double CountDigits ( int n ) { if ( n == 1 ) return 1 ; double sum = 0 ; for ( int i = 2 ; i <= n ; ++ i ) { sum += ( ( double ) Math . Log ( i ) / ( double ) Math . Log ( 10 ) ) ; } sum *= n ; return Math . Ceiling ( sum ) ; } public static void Main ( ) { GFG g = new GFG ( ) ; int N = 5 ; Console . WriteLine ( g . CountDigits ( N ) ) ; } }
Find the value of max ( f ( x ) ) | C # implementation of above approach ; Function to calculate the value ; forming the prefix sum arrays ; Taking the query ; finding the sum in the range l to r in array a ; finding the sum in the range l to r in array b ; Finding the max value of the function ; Finding the min value of the function ; Driver code
using System ; class GFG { static int CONS = 32766 ; static void calc ( int [ ] a , int [ ] b , int [ ] lr , int q , int n ) { int M , m , i , l , r , suma , sumb , cc ; cc = 0 ; for ( i = 0 ; i < n - 1 ; ++ i ) { a [ i + 1 ] += a [ i ] ; b [ i + 1 ] += b [ i ] ; } while ( q != 0 ) { l = lr [ cc ++ ] ; r = lr [ cc ++ ] ; l -= 2 ; r -= 1 ; suma = a [ r ] ; sumb = b [ r ] ; if ( l >= 0 ) { suma -= a [ l ] ; sumb -= b [ l ] ; } M = Math . Max ( CONS * suma + CONS * sumb , - CONS * suma - CONS * sumb ) ; M = Math . Max ( M , Math . Max ( CONS * suma - CONS * sumb , - CONS * suma + CONS * sumb ) ) ; m = Math . Min ( CONS * suma + CONS * sumb , - CONS * suma - CONS * sumb ) ; m = Math . Min ( m , Math . Min ( CONS * suma - CONS * sumb , - CONS * suma + CONS * sumb ) ) ; Console . WriteLine ( ( M - m ) ) ; q -- ; } } public static void Main ( ) { int n = 5 , q = 2 ; int [ ] a = { 0 , 7 , 3 , 4 , 5 } ; int [ ] b = { 0 , 3 , 1 , 2 , 3 } ; int [ ] lr = new int [ q * 2 ] ; lr [ 0 ] = 1 ; lr [ 1 ] = 1 ; lr [ 2 ] = 1 ; lr [ 3 ] = 3 ; calc ( a , b , lr , q , n ) ; } }
Program to find the Nth number of the series 2 , 10 , 24 , 44 , 70. ... . | C # program to find N - th term of the series : ; function to return nth term of the series ; Driver Code ; Taking n as 4 ; Printing the nth term
using System ; class GFG { static long NthTerm ( long n ) { long x = ( 3 * n * n ) % 1000000009 ; return ( x - n + 1000000009 ) % 1000000009 ; } public static void Main ( ) { long N = 4 ; Console . Write ( NthTerm ( N ) ) ; } }
Sum of first N natural numbers by taking powers of 2 as negative number | C # implementation of above approach ; to store power of 2 ; to store presum of the power of 2 's ; function to find power of 2 ; to store power of 2 ; to store pre sum ; Function to find the sum ; first store sum of first n natural numbers . ; find the first greater number than given number then minus double of this from answer ; Driver code ; function call ; function call
using System ; class GFG { static int [ ] power = new int [ 31 ] ; static int [ ] pre = new int [ 31 ] ; static void PowerOfTwo ( ) { int x = 1 ; for ( int i = 0 ; i < 31 ; i ++ ) { power [ i ] = x ; x *= 2 ; } pre [ 0 ] = 1 ; for ( int i = 1 ; i < 31 ; i ++ ) pre [ i ] = pre [ i - 1 ] + power [ i ] ; } static int Sum ( int n ) { int ans = n * ( n + 1 ) / 2 ; for ( int i = 0 ; i < 31 ; i ++ ) { if ( power [ i ] > n ) { ans -= 2 * pre [ i - 1 ] ; break ; } } return ans ; } public static void Main ( ) { PowerOfTwo ( ) ; int n = 4 ; Console . WriteLine ( Sum ( n ) ) ; } }
Check if a number is Quartan Prime or not | C # program to check if a number is Quartan Prime or not ; Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver Code ; Check if number is prime and of the form 16 * n + 1
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 ; } public static void Main ( ) { int n = 17 ; if ( isPrime ( n ) && ( n % 16 == 1 ) ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } }
Print a number strictly less than a given number such that all its digits are distinct . | C # program to find a number less than n such that all its digits are distinct ; Function to find a number less than n such that all its digits are distinct ; looping through numbers less than n ; initializing a hash array ;  ; initializing variables to compare lengths of digits ; counting frequency of the digits ; checking if each digit is present once ; Driver code
using System ; class GFG { static int findNumber ( int n ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { int [ ] count = new int [ 10 ] ; / * int x = i ; int count1 = 0 , count2 = 0 ; while ( x > 0 ) { count [ x % 10 ] ++ ; x /= 10 ; count1 ++ ; } for ( int j = 0 ; j < 10 ; j ++ ) { if ( count [ j ] == 1 ) count2 ++ ; } if ( count1 == count2 ) return i ; } return - 1 ; } static public void Main ( ) { int n = 8490 ; Console . WriteLine ( findNumber ( n ) ) ; } }
Check if two Linked Lists are permutations of each other | C # program to check if linked lists are permutations of each other ; A linked list node ; Function to check if two linked lists * are permutations of each other * first : reference to head of first linked list * second : reference to head of second linked list ; Variables to keep track of sum and multiplication ; Traversing through linked list and calculating sum and multiply ; Traversing through linked list and calculating sum and multiply ; Function to add a node at the beginning of Linked List ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Driver Code ; First constructed linked list is : 12 . 35 . 1 . 10 . 34 . 1 ; Second constructed linked list is : 35 . 1 . 12 . 1 . 10 . 34
using System ; class GFG { public class Node { public int data ; public Node next ; } ; static bool isPermutation ( Node first , Node second ) { int sum1 = 0 , sum2 = 0 , mul1 = 1 , mul2 = 1 ; Node temp1 = first ; while ( temp1 != null ) { sum1 += temp1 . data ; mul1 *= temp1 . data ; temp1 = temp1 . next ; } Node temp2 = second ; while ( temp2 != null ) { sum2 += temp2 . data ; mul2 *= temp2 . data ; temp2 = temp2 . next ; } return ( ( sum1 == sum2 ) && ( mul1 == mul2 ) ) ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } public static void Main ( String [ ] args ) { Node first = null ; first = push ( first , 1 ) ; first = push ( first , 34 ) ; first = push ( first , 10 ) ; first = push ( first , 1 ) ; first = push ( first , 35 ) ; first = push ( first , 12 ) ; Node second = null ; second = push ( second , 35 ) ; second = push ( second , 1 ) ; second = push ( second , 12 ) ; second = push ( second , 1 ) ; second = push ( second , 10 ) ; second = push ( second , 34 ) ; if ( isPermutation ( first , second ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }