codeparrot/xlcost-text-to-code huggingface dataset:
Count of N | C # program for the above approach ; Function to find the count of odd and even integers having N bits and K set bits ; Find the count of even integers ; Find the count of odd integers ; Print the total count ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int factorial ( int n ) { int ans = 1 ; while ( n >= 1 ) { ans *= n ; n -- ; } return ans ; } static void Binary_Num ( int n , int k ) { int num_even , num_odd ; if ( n - k - 1 >= 0 && k - 1 >= 0 ) { num_even = factorial ( n - 2 ) / ( factorial ( k - 1 ) * factorial ( n - k - 1 ) ) ; } else { num_even = 0 ; } if ( k - 2 >= 0 ) { num_odd = factorial ( n - 2 ) / ( factorial ( k - 2 ) * factorial ( n - k ) ) ; } else { num_odd = 0 ; } Console . Write ( num_even + " ▁ " + num_odd ) ; } public static void Main ( ) { int N = 9 , K = 6 ; Binary_Num ( N , K ) ; } }
Sum of all subsets of a given size ( = K ) | C # program for the above approach ; Function to find the sum of all sub - sets of size K ; Frequency of each array element in summation equation . ; calculate factorial of n - 1 ; calculate factorial of k - 1 ; calculate factorial of n - k ; Calculate sum of array . ; Sum of all subsets of size k . ; Driver Code
using System ; class GFG { static void findSumOfAllSubsets ( int [ ] arr , int n , int k ) { int factorial_N = 1 , factorial_d = 1 , factorial_D = 1 ; for ( int i = 1 ; i <= n - 1 ; i ++ ) factorial_N *= i ; for ( int i = 1 ; i <= k - 1 ; i ++ ) factorial_d *= i ; for ( int i = 1 ; i <= n - k ; i ++ ) factorial_D *= i ; int freq = factorial_N / ( factorial_d * factorial_D ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; sum = sum * freq ; Console . WriteLine ( " Sum ▁ of ▁ all ▁ subsets ▁ of ▁ size ▁ = ▁ " + k + " ▁ is ▁ = > ▁ " + sum ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 4 , 5 } ; int n = 4 , k = 2 ; findSumOfAllSubsets ( arr , n , k ) ; } }
Maximize XOR by selecting 3 numbers in range [ 0 , A ] , [ 0 , B ] , and [ 0 , C ] respectively | C # Program to implement the above approach ; Function to calculate maximum triplet XOR form 3 ranges ; Initialize a variable to store the answer ; create minimum number that have a set bit at ith position ; Check for each number and try to greedily choose the bit if possible If A >= cur then A also have a set bit at ith position ; Increment the answer ; Subtract this value from A ; Check for B ; Increment the answer ; Subtract this value from B ; Check for C ; Increment the answer ; Subtract this value from C ; If any of the above conditions is not satisfied then there is no way to turn that bit ON . ; Driver Code
using System ; class GFG { static int maximumTripletXOR ( int A , int B , int C ) { int ans = 0 ; for ( int i = 30 ; i >= 0 ; i -- ) { int cur = 1 << i ; if ( A >= cur ) { ans += cur ; A -= cur ; } else if ( B >= cur ) { ans += cur ; B -= cur ; } else if ( C >= cur ) { ans += cur ; C -= cur ; } } return ans ; } public static void Main ( String [ ] args ) { int A = 6 ; int B = 2 ; int C = 10 ; Console . Write ( maximumTripletXOR ( A , B , C ) ) ; } }
Count of integers in given range having their last K digits are equal | C # Program of the above approach ; Function to return the count of integers from 1 to X having the last K digits as equal ; Stores the total count of integers ; Loop to iterate over all possible values of z ; Terminate the loop when z > X ; Add count of integers with last K digits equal to z ; Return count ; Function to return the count of integers from L to R having the last K digits as equal ; Driver Code ; Function Call
using System ; class GFG { static int intCount ( int X , int K ) { int ans = 0 ; for ( int z = 0 ; z < Math . Pow ( 10 , K ) ; z += ( ( int ) Math . Pow ( 10 , K ) - 1 ) / 9 ) { if ( z > X ) break ; ans += ( ( X - z ) / ( int ) Math . Pow ( 10 , K ) + 1 ) ; } return ans ; } static int intCountInRange ( int L , int R , int K ) { return ( intCount ( R , K ) - intCount ( L - 1 , K ) ) ; } public static void Main ( String [ ] args ) { int L = 49 ; int R = 101 ; int K = 2 ; Console . Write ( intCountInRange ( L , R , K ) ) ; } }
Count subsequence of length 4 having product of the first three elements equal to the fourth element | C # program for the above approach ; Function to find the total number of subsequences satisfying the given criteria ; Stores the count of quadruplets ; Stores the frequency of product of the triplet ; Traverse the given array arr [ ] ; Consider arr [ i ] as fourth element of subsequences ; Generate all possible pairs of the array [ 0 , i - 1 ] ; Increment the frequency of the triplet ; Return the total count obtained ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countQuadruples ( int [ ] A , int N ) { int ans = 0 ; Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( freq . ContainsKey ( A [ i ] ) ) ans += freq [ A [ i ] ] ; else freq . Add ( A [ i ] , 0 ) ; for ( int j = 0 ; j < i ; j ++ ) { for ( int k = 0 ; k < j ; k ++ ) { if ( freq . ContainsKey ( A [ i ] * A [ j ] * A [ k ] ) ) freq [ A [ i ] * A [ j ] * A [ k ] ] ++ ; else freq . Add ( A [ i ] * A [ j ] * A [ k ] , 1 ) ; } } } return ans ; } public static void Main ( ) { int [ ] arr = { 10 , 2 , 2 , 7 , 40 , 160 } ; int N = arr . Length ; Console . Write ( countQuadruples ( arr , N ) ) ; } }
Maximum times X and Y can be reduced to near 0 using numbers A or B | C # program to implement the above approach ; Helper function to check if we can perform Mid number of moves ; Remove atleast Mid * B from both X and Y ; If any value is negative return false . ; Calculate remaining values ; If val >= Mid then it is possible to perform this much moves ; Else return false ; Initialize a variable to store the answer ; Fix the left and right range ; Binary Search over the answer ; Check for the middle value as the answer ; It is possible to perform this much moves ; Maximise the answer ; Return answer ; Driver Code ; Generalise that A >= B
using System ; class GFG { static int MAXN = 10000000 ; static Boolean can ( int Mid , int X , int Y , int A , int B ) { int p1 = X - Mid * B ; int p2 = Y - Mid * B ; if ( p1 < 0 p2 < 0 ) { return false ; } int k = A - B ; if ( k == 0 ) { return true ; } int val = p1 / k + p2 / k ; if ( val >= Mid ) { return true ; } return false ; } static int maxPossibleMoves ( int X , int Y , int A , int B ) { int ans = 0 ; int L = 1 , R = MAXN ; while ( L <= R ) { int Mid = ( L + R ) / 2 ; if ( can ( Mid , X , Y , A , B ) ) { L = Mid + 1 ; ans = Math . Max ( ans , Mid ) ; } else { R = Mid - 1 ; } } return ans ; } public static void Main ( String [ ] args ) { int X = 10 , Y = 12 , A = 2 , B = 5 ; if ( A < B ) { int temp = A ; A = B ; B = temp ; } Console . Write ( maxPossibleMoves ( X , Y , A , B ) ) ; } }
Find two proper factors of N such that their sum is coprime with N | C # Program for the above approach ; Function to find two proper factors of N such that their sum is coprime with N ; Find factors in sorted order ; Find largest value of d2 such that d1 and d2 are co - prime ; Check if d1 and d2 are proper factors of N ; Print answer ; No such factors exist ; Driver code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void printFactors ( int n ) { for ( int i = 2 ; i <= ( int ) Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { int d1 = i , d2 = n ; while ( d2 % d1 == 0 ) { d2 = d2 / d1 ; } if ( d1 > 1 && d2 > 1 ) { Console . Write ( d1 + " , ▁ " + d2 ) ; return ; } } } Console . Write ( - 1 ) ; } public static void Main ( ) { int N = 10 ; printFactors ( N ) ; } }
Count of triplets in an Array with odd sum | C # program for the above approach ; Function to count the number of unordered triplets such that their sum is an odd integer ; Count the number of odd and even integers in the array ; Number of ways to create triplets using one odd and two even integers ; Number of ways to create triplets using three odd integers ; Return answer ; Driver code ; Function Call ; Print Answer
using System ; class GFG { static int countTriplets ( int [ ] arr , int n ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) odd ++ ; else even ++ ; } int c1 = odd * ( even * ( even - 1 ) ) / 2 ; int c2 = ( odd * ( odd - 1 ) * ( odd - 2 ) ) / 6 ; return c1 + c2 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 5 , 6 , 4 , 5 , 10 , 1 , 7 } ; int n = arr . Length ; int ans = countTriplets ( arr , n ) ; Console . Write ( ans ) ; } }
Longest remaining array of distinct elements possible after repeated removal of maximum and minimum elements of triplets | C # Program to implement the above approach ; Function to return length of longest remaining array of pairwise distinct array possible by removing triplets ; Stores the frequency of array elements ; Traverse the array ; Iterate through the map ; If frequency of current element is even ; Stores the required count of unique elements remaining ; If count is odd ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int maxUniqueElements ( int [ ] Arr , int N ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( Arr [ i ] ) ) { mp [ Arr [ i ] ] += 1 ; } else { mp [ Arr [ i ] ] = 1 ; } } int cnt = 0 ; foreach ( KeyValuePair < int , int > entry in mp ) { if ( ( entry . Value ) % 2 == 0 ) { cnt ++ ; } } int ans = mp . Count ; if ( cnt % 2 == 1 ) { ans -- ; } return ans ; } public static void Main ( string [ ] args ) { int N = 5 ; int [ ] A = { 1 , 2 , 1 , 3 , 7 } ; Console . Write ( maxUniqueElements ( A , N ) ) ; } }
Count cells in a grid from which maximum number of cells can be reached by K vertical or horizontal jumps | C # program for the above approach ; Function to count the number of cells in the grid such that maximum cell is reachable with a jump of K ; Maximum reachable rows from the current row ; Stores the count of cell that are reachable from the current row ; Count of reachable rows ; Update the maximum value ; Add it to the count ; Maximum reachable columns from the current column ; Stores the count of cell that are reachable from the current column ; Count of rechable columns ; Update the maximum value ; Add it to the count ; Return the total count of cells ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countCells ( int n , int m , int s ) { int mx1 = - 1 ; int cont1 = 0 ; for ( int i = 0 ; i < s && i < n ; ++ i ) { int aux = ( n - ( i + 1 ) ) / s + 1 ; if ( aux > mx1 ) { mx1 = cont1 = aux ; } else if ( aux == mx1 ) cont1 += aux ; } int mx2 = - 1 ; int cont2 = 0 ; for ( int i = 0 ; i < s && i < m ; ++ i ) { int aux = ( m - ( i + 1 ) ) / s + 1 ; if ( aux > mx2 ) mx2 = cont2 = aux ; else if ( aux == mx2 ) cont2 += aux ; } return ( cont1 * cont2 ) ; } public static void Main ( ) { int N = 5 , M = 5 , K = 2 ; Console . Write ( countCells ( N , M , K ) ) ; } }
Count of pairs from first N natural numbers with remainder at least K | C # program for the above approach ; Function to count the number of pairs ( a , b ) such that a % b is at least K ; Base Case ; Stores resultant count of pairs ; Iterate over the range [ K + 1 , N ] ; Find the cycled elements ; Find the remaining elements ; Return the resultant possible count of pairs ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countTotalPairs ( int N , int K ) { if ( K == 0 ) { return N * N ; } int ans = 0 ; for ( int b = K + 1 ; b <= N ; b ++ ) { ans += ( N / b ) * ( b - K ) ; ans += Math . Max ( N % b - K + 1 , 0 ) ; } return ans ; } public static void Main ( ) { int N = 5 , K = 2 ; Console . Write ( countTotalPairs ( N , K ) ) ; } }
Minimize sum of absolute difference between all pairs of array elements by decrementing and incrementing pairs by 1 | C # program for the above approach ; Function to find the minimum value of the sum of absolute difference between all pairs of arrays ; Stores the sum of array elements ; Find the sum of array element ; Store the value of sum % N ; Return the resultant value ; Driver Code
using System ; class GFG { public static int minSumDifference ( int [ ] ar , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ar [ i ] ; int rem = sum % n ; return rem * ( n - rem ) ; } public static void Main ( ) { int [ ] arr = { 3 , 6 , 8 , 5 , 2 , 1 , 11 , 7 , 10 , 4 } ; int N = arr . Length ; Console . Write ( minSumDifference ( arr , N ) ) ; } }
Count N | C # program for the above approach ; Function to find the value of x ^ y ; Stores the value of x ^ y ; Iterate until y is positive ; If y is odd ; Divide y by 2 ; Return the value of x ^ y ; Function to find the number of N - digit integers satisfying the given criteria ; Count of even positions ; Count of odd positions ; Return the resultant count ; Driver Code
using System ; class GFG { static int m = 1000000007 ; static int power ( int x , int y ) { int res = 1 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % m ; y = y >> 1 ; x = ( x * x ) % m ; } return res ; } static int countNDigitNumber ( int N ) { int ne = N / 2 + N % 2 ; int no = ( int ) Math . Floor ( ( double ) N / 2 ) ; return power ( 4 , ne ) * power ( 5 , no ) ; } public static void Main ( ) { int N = 5 ; Console . Write ( countNDigitNumber ( N ) % m ) ; } }
Minimum jumps to traverse all integers in range [ 1 , N ] such that integer i can jump i steps | C # program for the above approach ; Utility function to find minimum steps ; Initialize count and result ; Traverse over the range [ 1 , N ] ; Update res ; Increment count ; Return res ; Driver Code ; Input ; Function call
using System ; class GFG { static int minSteps ( int N ) { int count = 1 , res = 0 ; for ( int i = 1 ; i <= N ; i += count ) { res = Math . Max ( res , count ) ; count ++ ; } return res ; } public static void Main ( ) { int N = 6 ; Console . Write ( minSteps ( N ) ) ; } }
Minimum jumps to traverse all integers in range [ 1 , N ] such that integer i can jump i steps | Java implementation of the above approach ; Utility function to find minimum steps ; Driver code ; Input integer ; Function call
using System ; class GFG { static int minSteps ( int N ) { int res = ( ( int ) Math . Sqrt ( 1 + 8 * N ) - 1 ) / 2 ; return res ; } public static void Main ( String [ ] args ) { int N = 6 ; Console . Write ( minSteps ( N ) + " STRNEWLINE " ) ; } }
The dice problem | C # program for the above approach ; Function to find number written on the opposite face of the dice ; Stores number on opposite face of dice ; Print the answer ; Driver Code ; Given value of N ; Function call to find number written on the opposite face of the dice
using System ; using System . Collections . Generic ; class GFG { static void oppositeFaceOfDice ( int N ) { int ans = 7 - N ; Console . Write ( ans ) ; } public static void Main ( ) { int N = 2 ; oppositeFaceOfDice ( N ) ; } }
Count numbers from a given range that can be visited moving any number of steps from the range [ L , R ] | C # program for the above approach ; Function to count points from the range [ X , Y ] that can be reached by moving by L or R steps ; Initialize difference array ; Initialize Count ; Marking starting point ; Iterating from X to Y ; Accumulate difference array ; If diff_arr [ i ] is greater than 1 ; Updating difference array ; Visited point found ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static int countReachablePoints ( int X , int Y , int L , int R ) { int [ ] diff_arr = new int [ 100000 ] ; int count = 0 ; diff_arr [ X ] = 1 ; diff_arr [ X + 1 ] = - 1 ; for ( int i = X ; i <= Y ; i ++ ) { diff_arr [ i ] += diff_arr [ i - 1 ] ; if ( diff_arr [ i ] >= 1 ) { diff_arr [ i + L ] += 1 ; diff_arr [ i + R + 1 ] -= 1 ; count ++ ; } } return count ; } public static void Main ( ) { int X = 3 , Y = 12 , L = 2 , R = 3 ; Console . Write ( countReachablePoints ( X , Y , L , R ) ) ; } }
Multiply a single digit number in place to a number represented as a Linked List | C # program for the above approach ; Node of a Linked List ; Function to create a new node with given data ; Initialize new node ; Set the data field of the node ; Return the new node ; Function to reverse the linked list ; Traverse until curr != null ; Return the head of the reversed linked list ; Utility function to multiply a single digit to a linked list ; Store the head of list ; Stores the address of previous node ; Initially set carry as 0 and product as 1 ; Multiply M with each digit ; Add carry to product if carry exist ; Update carry for next calculation ; Update the value of each nodes ; Move head and temp pointers to next nodes ; If some carry is still there , add a new node to list ; Return head of the resultant list ; Function to multiply a single digit to a linked list ; Reverse linked list ; Multiply M from left to right of reversed list ; Reverse the modified list and return its head ; Function to print the linked list ; Driver Code ; Given Input list : 1.2 . 7.3 ; Function Call ; Print resultant list
using System ; public class GFG { class Node { public int data ; public Node next ; } ; static Node newNode ( int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } static Node reverse ( Node head ) { Node prev = null ; Node current = head ; Node next ; while ( current != null ) { next = current . next ; current . next = prev ; prev = current ; current = next ; } return prev ; } static Node multiplyHelp ( Node head , int M ) { Node res = head ; Node prev = null ; int carry = 0 , product = 1 ; while ( head != null ) { product = head . data * M ; product += carry ; carry = product / 10 ; head . data = product % 10 ; prev = head ; head = head . next ; } if ( carry > 0 ) prev . next = newNode ( carry ) ; return res ; } static Node multiply ( Node head , int M ) { head = reverse ( head ) ; head = multiplyHelp ( head , M ) ; return reverse ( head ) ; } static void printList ( Node node ) { while ( node != null ) { Console . Write ( node . data ) ; node = node . next ; } } public static void Main ( String [ ] args ) { Node head = newNode ( 1 ) ; head . next = newNode ( 2 ) ; head . next . next = newNode ( 7 ) ; head . next . next . next = newNode ( 3 ) ; int M = 3 ; head = multiply ( head , M ) ; printList ( head ) ; } }
Minimum length of the subarray required to be replaced to make frequency of array elements equal to N / M | C # program for the above approach ; Function to find the minimum length of the subarray to be changed . ; Stores the frequencies of array elements ; Stores the number of array elements that are present more than N / M times ; Iterate over the range ; Increment the frequency ; If the frequency of all array elements are already N / M ; Stores the resultant length of the subarray ; The left and right pointers ; Iterate over the range ; If the current element is ; If the value of c is 0 , then find the possible answer ; Iterate over the range ; If the element at left is making it extra ; Update the left pointer ; Update the right pointer ; Return the resultant length ; Driver Code
using System ; class GFG { public static int minimumSubarray ( int [ ] arr , int n , int m ) { int [ ] mapu = new int [ m + 1 ] ; Array . Fill ( mapu , 0 ) ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mapu [ arr [ i ] ] ++ ; if ( mapu [ arr [ i ] ] == ( n / m ) + 1 ) c ++ ; } if ( c == 0 ) return 0 ; int ans = n ; int l = 0 , r = 0 ; while ( r < n ) { if ( -- mapu [ arr [ r ] ] == ( n / m ) ) c -- ; if ( c == 0 ) { while ( l <= r && c == 0 ) { ans = Math . Min ( ans , r - l + 1 ) ; if ( ++ mapu [ arr [ l ] ] > ( n / m ) ) c ++ ; l ++ ; } } r ++ ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 1 , 1 , 2 } ; int M = 2 ; int N = arr . Length ; Console . Write ( minimumSubarray ( arr , N , M ) ) ; } }
Check if the sum of K least and most frequent array elements are equal or not | C # program for the above approach ; Function to compare the sum of K most and least occurrences ; Stores frequency of array element ; Stores the frequencies as indexes and putelements with the frequency in a vector ; Find the frequency ; Insert in the vector ; Stores the count of elements ; Traverse the frequency array ; Find the kleastfreqsum ; If the count is K , break ; Reinitialize the count to zero ; Traverse the frequency ; Find the kmostfreqsum ; If the count is K , break ; Comparing the sum ; Otherwise , return No ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static String checkSum ( int [ ] arr , int n , int k ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( m . ContainsKey ( arr [ i ] ) ) { m [ arr [ i ] ] = m [ arr [ i ] ] + 1 ; } else { m . Add ( arr [ i ] , 1 ) ; } List < int > [ ] freq = new List < int > [ n + 1 ] ; for ( int i = 0 ; i < freq . Length ; i ++ ) freq [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int f = m [ arr [ i ] ] ; if ( f != - 1 ) { freq [ f ] . Add ( arr [ i ] ) ; if ( m . ContainsKey ( arr [ i ] ) ) { m [ arr [ i ] ] = - 1 ; } else { m . Add ( arr [ i ] , - 1 ) ; } } } int count = 0 ; int kleastfreqsum = 0 ; for ( int i = 0 ; i <= n ; i ++ ) { foreach ( int x in freq [ i ] ) { kleastfreqsum += x ; count ++ ; if ( count == k ) break ; } if ( count == k ) break ; } count = 0 ; int kmostfreqsum = 0 ; for ( int i = n ; i >= 0 ; i -- ) { foreach ( int x in freq [ i ] ) { kmostfreqsum += x ; count ++ ; if ( count == k ) break ; } if ( count == k ) break ; } if ( kleastfreqsum == kmostfreqsum ) return " Yes " ; return " No " ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 , 2 , 3 , 3 , 4 } ; int N = arr . Length ; int K = 2 ; Console . Write ( checkSum ( arr , N , K ) ) ; } }
Print all numbers from a given range that are made up of consecutive digits | C # program for the above approach ; Function to find the consecutive digit numbers in the given range ; Initialize num as empty String ; Stores the resultant number ; Iterate over the range [ 1 , 9 ] ; Check if the current number is within range ; Iterate on the digits starting from i ; Checking the consecutive digits numbers starting from i and ending at j is within range or not ; Sort the numbers in the increasing order ; Print the required numbers ; Driver code
using System ; using System . Collections . Generic ; class Program { static void solve ( int start , int end ) { string num = " " ; List < int > ans = new List < int > ( ) ; for ( int i = 1 ; i <= 9 ; i ++ ) { num = i . ToString ( ) ; int value = i ; if ( value >= start && value <= end ) { ans . Add ( value ) ; } for ( int j = i + 1 ; j <= 9 ; j ++ ) { num += j . ToString ( ) ; value = Int32 . Parse ( num ) ; if ( value >= start && value <= end ) { ans . Add ( value ) ; } } } ans . Sort ( ) ; foreach ( int it in ans ) Console . Write ( it + " ▁ " ) ; } static void Main ( ) { int L = 12 , R = 87 ; solve ( L , R ) ; } }
Minimum distance a person has to move in order to take a picture of every racer | C # program for above approach ; Function to return the minimum distance the person has to move int order to get the pictures ; To store the minimum ending point ; To store the maximum starting point ; Find the values of minSeg and maxSeg ; Impossible ; The person doesn 't need to move ; Get closer to the left point ; Get closer to the right point ; Driver Code
using System ; class GFG { static int minDistance ( int [ ] start , int [ ] end , int n , int d ) { int left = Int32 . MinValue ; int right = Int32 . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { left = Math . Max ( left , start [ i ] ) ; right = Math . Min ( right , end [ i ] ) ; } if ( left > right ) return - 1 ; if ( d >= left && d <= right ) return 0 ; if ( d < left ) return ( left - d ) ; if ( d > right ) return ( d - right ) ; return - 1 ; } public static void Main ( String [ ] args ) { int [ ] start = { 0 , 2 , 4 } ; int [ ] end = { 7 , 14 , 6 } ; int n = start . Length ; int d = 3 ; Console . Write ( minDistance ( start , end , n , d ) ) ; } }