codeparrot/xlcost-text-to-code huggingface dataset:
XOR of all possible pairwise sum from two given Arrays | C ++ Program to implement the above approach ; Function to calculate the XOR of the sum of every pair ; Stores the maximum bit ; Look for all the k - th bit ; Stores the modulo of elements B [ ] with ( 2 ^ ( k + 1 ) ) ; Calculate modulo of array B [ ] with ( 2 ^ ( k + 1 ) ) ; Sort the array C [ ] ; Stores the total number whose k - th bit is set ; Calculate and store the modulo of array A [ ] with ( 2 ^ ( k + 1 ) ) ; Lower bound to count the number of elements having k - th bit in the range ( 2 ^ k - x , 2 * 2 ^ ( k ) - x ) ; Add total number i . e ( r - l ) whose k - th bit is one ; Lower bound to count the number of elements having k - th bit in range ( 3 * 2 ^ k - x , 4 * 2 ^ ( k ) - x ) ; If count is even , Xor of k - th bit becomes zero , no need to add to the answer . If count is odd , only then , add to the final answer ; Return answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int XorSum ( int A [ ] , int B [ ] , int N ) { const int maxBit = 29 ; int ans = 0 ; for ( int k = 0 ; k < maxBit ; k ++ ) { int C [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { C [ i ] = B [ i ] % ( 1 << ( k + 1 ) ) ; } sort ( C , C + N ) ; long long count = 0 ; long long l , r ; for ( int i = 0 ; i < N ; i ++ ) { int x = A [ i ] % ( 1 << ( k + 1 ) ) ; l = lower_bound ( C , C + N , ( 1 << k ) - x ) - C ; r = lower_bound ( C , C + N , ( 1 << k ) * 2 - x ) - C ; count += ( r - l ) ; l = lower_bound ( C , C + N , ( 1 << k ) * 3 - x ) - C ; r = lower_bound ( C , C + N , ( 1 << k ) * 4 - x ) - C ; count += ( r - l ) ; } if ( count & 1 ) ans += ( 1 << k ) ; } return ans ; } int main ( ) { int A [ ] = { 4 , 6 , 0 , 0 , 3 , 3 } ; int B [ ] = { 0 , 5 , 6 , 5 , 0 , 3 } ; int N = sizeof A / sizeof A [ 0 ] ; cout << XorSum ( A , B , N ) << endl ; return 0 ; }
Minimize count of Subsets with difference between maximum and minimum element not exceeding K | C ++ Program to implement above approach ; Function to find the minimum count of subsets of required type ; Stores the result ; Store the maximum and minimum element of the current subset ; Update current maximum ; If difference exceeds K ; Update count ; Update maximum and minimum to the current subset ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCount ( int arr [ ] , int N , int K ) { sort ( arr , arr + N ) ; int result = 1 ; int cur_max = arr [ 0 ] ; int cur_min = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { cur_max = arr [ i ] ; if ( cur_max - cur_min > K ) { result ++ ; cur_max = arr [ i ] ; cur_min = arr [ i ] ; } } return result ; } int main ( ) { int arr [ ] = { 1 , 10 , 8 , 3 , 9 } ; int K = 3 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findCount ( arr , N , K ) ; return 0 ; }
Minimize the Sum of all the subarrays made up of the products of same | C ++ Program to implement the above approach ; Function to rearrange the second array such that the sum of its product of same indexed elements from both the arrays is minimized ; Stores ( i - 1 ) * ( n - i ) * a [ i ] for every i - th element ; Updating the value of pro according to the function ; Sort the array in reverse order ; Sort the products ; Updating the ans ; Return the ans ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE #define ll  long long NEW_LINE using namespace std ; const int mod = ( int ) 1e9 + 7 ; bool comp ( ll a , ll b ) { if ( a > b ) return true ; else return false ; } ll findMinValue ( vector < ll > & a , vector < ll > & b ) { int n = a . size ( ) ; vector < ll > pro ( n ) ; for ( int i = 0 ; i < n ; ++ i ) { pro [ i ] = ( ( ll ) ( i + 1 ) * ( ll ) ( n - i ) ) ; pro [ i ] *= ( 1LL * a [ i ] ) ; ; } sort ( b . begin ( ) , b . end ( ) , comp ) ; sort ( pro . begin ( ) , pro . end ( ) ) ; ll ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) { ans += ( pro [ i ] % mod * b [ i ] ) % mod ; ans %= mod ; } return ans ; } int main ( ) { vector < ll > a = { 1 , 2 , 3 } ; vector < ll > b = { 2 , 3 , 2 } ; cout << findMinValue ( a , b ) << endl ; }
Maximum number of unique Triplets such that each element is selected only once | C ++ program for the above approach ; Function that finds maximum number of triplets with different elements ; Map M will store the frequency of each element in the array ; Priority queue of pairs { frequency , value } ; ans will store possible triplets ; Extract top 3 elements ; Make a triplet ; Decrease frequency and push back into priority queue if non - zero frequency ; Print the triplets ; Print the triplets ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findTriplets ( int ar [ ] , int n ) { unordered_map < int , int > mp ; for ( int x = 0 ; x < n ; x ++ ) mp [ ar [ x ] ] ++ ; priority_queue < pair < int , int > > pq ; for ( auto & pa : mp ) pq . push ( { pa . second , pa . first } ) ; vector < array < int , 3 > > ans ; while ( pq . size ( ) >= 3 ) { pair < int , int > ar [ 3 ] ; for ( int x = 0 ; x < 3 ; x ++ ) { ar [ x ] = pq . top ( ) ; pq . pop ( ) ; } ans . push_back ( { ar [ 0 ] . second , ar [ 1 ] . second , ar [ 2 ] . second } ) ; for ( int x = 0 ; x < 3 ; x ++ ) { ar [ x ] . first -- ; if ( ar [ x ] . first ) pq . push ( ar [ x ] ) ; } } cout << " Maximum ▁ number ▁ of ▁ " << " possible ▁ triples : ▁ " ; cout << ans . size ( ) << endl ; for ( auto & pa : ans ) { for ( int v : pa ) cout << v << " ▁ " ; cout << endl ; } } int main ( ) { int arr [ ] = { 2 , 2 , 3 , 3 , 4 , 4 , 4 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findTriplets ( arr , n ) ; return 0 ; }
Sort Array such that smallest is at 0 th index and next smallest it at last index and so on | C ++ program for the above approach ; Function to perform the rearrangement ; Initialize variables ; Loop until i crosses j ; This check is to find the minimum values in the ascending order ; Condition to alternatively iterate variable i and j ; Perform swap operation ; Increment i ; Assign the value of min ; Perform swap ; Decrement i ; Assign the value of min ; Print the array ; Driver Code ; Given Array arr [ ] ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void rearrange ( int a [ ] , int N ) { int i = 0 , j = N - 1 ; int min = 0 , k , x = 0 , temp ; while ( i < j ) { for ( k = i ; k <= j ; k ++ ) { if ( a [ k ] < a [ min ] ) min = k ; } if ( x % 2 == 0 ) { temp = a [ i ] ; a [ i ] = a [ min ] ; a [ min ] = temp ; i ++ ; min = i ; } else { temp = a [ j ] ; a [ j ] = a [ min ] ; a [ min ] = temp ; j -- ; min = j ; } x ++ ; } for ( i = 0 ; i < N ; i ++ ) cout << a [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 1 , 3 , 3 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; rearrange ( arr , N ) ; return 0 ; }
Maximum number of elements greater than X after equally distributing subset of array | C ++ implementation to find the maximum number of elements greater than X by equally distributing ; Function to find the maximum number of elements greater than X by equally distributing ; Sorting the array ; Loop to iterate over the elements of the array ; If no more elements can become larger than x ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void redistribute ( int arr [ ] , int n , int x ) { sort ( arr , arr + n , greater < int > ( ) ) ; int i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum / ( i + 1 ) < x ) { cout << i << endl ; break ; } } if ( i == n ) cout << n << endl ; } int main ( ) { int arr [ ] = { 5 , 1 , 2 , 1 } ; int x = 3 ; redistribute ( arr , 4 , x ) ; return 0 ; }
Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal | C ++ program for the above approach ; Function to print the remaining element ; Priority queue can be used to construct max - heap ; Insert all element of arr [ ] into priority queue ; Perform operation until heap size becomes 0 or 1 ; Remove largest element ; Remove 2 nd largest element ; If extracted element are not equal ; Find X - Y and push it to heap ; If heap size is 1 , then print the remaining element ; Else print " - 1" ; Driver Code ; Given array arr [ ] ; Size of array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int final_element ( int arr [ ] , int n ) { priority_queue < int > heap ; for ( int i = 0 ; i < n ; i ++ ) heap . push ( arr [ i ] ) ; while ( heap . size ( ) > 1 ) { int X = heap . top ( ) ; heap . pop ( ) ; int Y = heap . top ( ) ; heap . pop ( ) ; if ( X != Y ) { int diff = abs ( X - Y ) ; heap . push ( diff ) ; } } if ( heap . size ( ) == 1 ) { cout << heap . top ( ) ; } else { cout << " - 1" ; } } int main ( ) { int arr [ ] = { 3 , 5 , 2 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; final_element ( arr , n ) ; return 0 ; }
Sort a string according to the frequency of characters | C ++ implementation to Sort strings according to the frequency of characters in ascending order ; Returns count of character in the string ; Check for vowel ; Function to sort the string according to the frequency ; Vector to store the frequency of characters with respective character ; Inserting frequency with respective character in the vector pair ; Sort the vector , this will sort the pair according to the number of characters ; Print the sorted vector content ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countFrequency ( string str , char ch ) { int count = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( str [ i ] == ch ) ++ count ; return count ; } void sortArr ( string str ) { int n = str . length ( ) ; vector < pair < int , char > > vp ; for ( int i = 0 ; i < n ; i ++ ) { vp . push_back ( make_pair ( countFrequency ( str , str [ i ] ) , str [ i ] ) ) ; } sort ( vp . begin ( ) , vp . end ( ) ) ; for ( int i = 0 ; i < vp . size ( ) ; i ++ ) cout << vp [ i ] . second ; } int main ( ) { string str = " geeksforgeeks " ; sortArr ( str ) ; return 0 ; }
Maximum sum after rearranging the array for K queries | C ++ program to find the maximum sum after rearranging the array for K queries ; Function to find maximum sum after rearranging array elements ; Auxiliary array to find the count of each selected elements ; Finding count of every element to be selected ; Making it to 0 - indexing ; Prefix sum array concept is used to obtain the count array ; Iterating over the count array to get the final array ; Variable to store the maximum sum ; Sorting both the arrays ; Loop to find the maximum sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSumArrangement ( int A [ ] , int R [ ] [ 2 ] , int N , int M ) { int count [ N ] ; memset ( count , 0 , sizeof count ) ; for ( int i = 0 ; i < M ; ++ i ) { int l = R [ i ] [ 0 ] , r = R [ i ] [ 1 ] + 1 ; l -- ; r -- ; count [ l ] ++ ; if ( r < N ) count [ r ] -- ; } for ( int i = 1 ; i < N ; ++ i ) { count [ i ] += count [ i - 1 ] ; } for ( int i = 0 ; i < N ; ++ i ) { cout << count [ i ] ; } int ans = 0 ; sort ( count , count + N ) ; sort ( A , A + N ) ; for ( int i = 0 ; i < N ; ++ i ) { cout << endl << A [ i ] ; } for ( int i = N - 1 ; i >= 0 ; -- i ) { ans += A [ i ] * count [ i ] ; } return ans ; } int main ( ) { int A [ ] = { 2 , 6 , 10 , 1 , 5 , 6 } ; int R [ ] [ 2 ] = { { 1 , 3 } , { 4 , 6 } , { 3 , 4 } } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int M = sizeof ( R ) / sizeof ( R [ 0 ] ) ; cout << maxSumArrangement ( A , R , N , M ) ; return 0 ; }
Sum of M maximum distinct digit sum from 1 to N that are factors of K | C ++ implementation to find the sum of maximum distinct digit sum of at most M numbers from 1 to N that are factors of K ; Function to find the factors of K in N ; Initialise a vector ; Find out the factors of K less than N ; Find the digit sum of each factor ; Sum of digits for each element in vector ; Find the largest M distinct digit sum from the digitSum vector ; Find the sum of last M numbers . ; Find the at most M numbers from N natural numbers whose digit sum is distinct and those M numbers are factors of K . ; Find out the factors of K less than N ; Sum of digits for each element in vector ; Sorting the digitSum vector ; Removing the duplicate elements ; " HashSet " is stores only unique elements ; Finding the sum and returning it ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > findFactors ( int n , int k ) { vector < int > factors ; for ( int i = 1 ; i <= sqrt ( k ) ; i ++ ) { if ( k % i == 0 ) { if ( k / i == i && i <= n ) factors . push_back ( i ) ; else { if ( i <= n ) factors . push_back ( i ) ; if ( k / i <= n ) factors . push_back ( k / i ) ; } } } return factors ; } vector < int > findDigitSum ( vector < int > a ) { for ( int i = 0 ; i < a . size ( ) ; i ++ ) { int c = 0 ; while ( a [ i ] > 0 ) { c += a [ i ] % 10 ; a [ i ] = a [ i ] / 10 ; } a [ i ] = c ; } return a ; } int findMMaxDistinctDigitSum ( vector < int > distinctDigitSum , int m ) { int sum = 0 ; for ( int i = distinctDigitSum . size ( ) - 1 ; i >= 0 && m > 0 ; i -- , m -- ) sum += distinctDigitSum [ i ] ; return sum ; } int findDistinctMnumbers ( int n , int k , int m ) { vector < int > factors = findFactors ( n , k ) ; vector < int > digitSum = findDigitSum ( factors ) ; sort ( digitSum . begin ( ) , digitSum . end ( ) ) ; vector < int > :: iterator ip ; ip = unique ( digitSum . begin ( ) , digitSum . end ( ) ) ; digitSum . resize ( distance ( digitSum . begin ( ) , ip ) ) ; return findMMaxDistinctDigitSum ( digitSum , m ) ; } int main ( ) { int n = 100 , k = 80 , m = 4 ; cout << findDistinctMnumbers ( n , k , m ) << endl ; return 0 ; }
Sorting objects using In | C ++ implementation of the approach ; Partition function which will partition the array and into two parts ; Compare hash values of objects ; Classic quicksort algorithm ; Function to sort and print the objects ; Create a hash table ; As the sorting order is blue objects , red objects and then yellow objects ; Quick sort function ; Printing the sorted array ; Driver code ; Let 's represent objects as strings
#include <bits/stdc++.h> NEW_LINE using namespace std ; int partition ( vector < string > & objects , int l , int r , unordered_map < string , int > & hash ) { int j = l - 1 ; int last_element = hash [ objects [ r ] ] ; for ( int i = l ; i < r ; i ++ ) { if ( hash [ objects [ i ] ] <= last_element ) { j ++ ; swap ( objects [ i ] , objects [ j ] ) ; } } j ++ ; swap ( objects [ j ] , objects [ r ] ) ; return j ; } void quicksort ( vector < string > & objects , int l , int r , unordered_map < string , int > & hash ) { if ( l < r ) { int mid = partition ( objects , l , r , hash ) ; quicksort ( objects , l , mid - 1 , hash ) ; quicksort ( objects , mid + 1 , r , hash ) ; } } void sortObj ( vector < string > & objects ) { unordered_map < string , int > hash ; hash [ " blue " ] = 1 ; hash [ " red " ] = 2 ; hash [ " yellow " ] = 3 ; quicksort ( objects , 0 , int ( objects . size ( ) - 1 ) , hash ) ; for ( int i = 0 ; i < objects . size ( ) ; i ++ ) cout << objects [ i ] << " ▁ " ; } int main ( ) { vector < string > objects { " red " , " blue " , " red " , " yellow " , " blue " } ; sortObj ( objects ) ; return 0 ; }
Sort the numbers according to their product of digits | C ++ implementation of the approach ; Function to return the product of the digits of n ; Function to sort the array according to the product of the digits of elements ; Vector to store the digit product with respective elements ; Inserting digit product with elements in the vector pair ; Sort the vector , this will sort the pair according to the product of the digits ; Print the sorted vector content ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int productOfDigit ( int n ) { int product = 1 ; while ( n > 0 ) { product *= n % 10 ; n = n / 10 ; } return product ; } void sortArr ( int arr [ ] , int n ) { vector < pair < int , int > > vp ; for ( int i = 0 ; i < n ; i ++ ) { vp . push_back ( make_pair ( productOfDigit ( arr [ i ] ) , arr [ i ] ) ) ; } sort ( vp . begin ( ) , vp . end ( ) ) ; for ( int i = 0 ; i < vp . size ( ) ; i ++ ) cout << vp [ i ] . second << " ▁ " ; } int main ( ) { int arr [ ] = { 12 , 10 , 102 , 31 , 15 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortArr ( arr , n ) ; return 0 ; }
Minimum steps required to reduce all the elements of the array to zero | C ++ implementation of the approach ; Function to return the minimum steps required to reduce all the elements to 0 ; Maximum element from the array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSteps ( int arr [ ] , int n ) { int maxVal = * max_element ( arr , arr + n ) ; return maxVal ; } int main ( ) { int arr [ ] = { 1 , 2 , 4 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << minSteps ( arr , n ) ; return 0 ; }
Maximum water that can be stored between two buildings | C ++ implementation of the above approach ; Return the maximum water that can be stored ; Check all possible pairs of buildings ; Maximum so far ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxWater ( int height [ ] , int n ) { int maximum = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int current = ( min ( height [ i ] , height [ j ] ) * ( j - i - 1 ) ) ; maximum = max ( maximum , current ) ; } } return maximum ; } int main ( ) { int height [ ] = { 2 , 1 , 3 , 4 , 6 , 5 } ; int n = sizeof ( height ) / sizeof ( height [ 0 ] ) ; cout << maxWater ( height , n ) ; return 0 ; }
Maximum water that can be stored between two buildings | C ++ implementation of the above approach ; Return the maximum water that can be stored ; Make pairs with indices ; Sort array based on heights ; To store the min and max index so far from the right ; Current building paired with the building greater in height and on the extreme left ; Current building paired with the building greater in height and on the extreme right ; Maximum so far ; Update the maximum and minimum so far ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool compareTo ( pair < int , int > p1 , pair < int , int > p2 ) { return p1 . second < p2 . second ; } int maxWater ( int height [ ] , int n ) { pair < int , int > pairs [ n ] ; for ( int i = 0 ; i < n ; i ++ ) pairs [ i ] = make_pair ( i , height [ i ] ) ; sort ( pairs , pairs + n , compareTo ) ; int minIndSoFar = pairs [ n - 1 ] . first ; int maxIndSoFar = pairs [ n - 1 ] . first ; int maxi = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int left = 0 ; if ( minIndSoFar < pairs [ i ] . first ) { left = ( pairs [ i ] . second * ( pairs [ i ] . first - minIndSoFar - 1 ) ) ; } int right = 0 ; if ( maxIndSoFar > pairs [ i ] . first ) { right = ( pairs [ i ] . second * ( maxIndSoFar - pairs [ i ] . first - 1 ) ) ; } maxi = max ( left , max ( right , maxi ) ) ; minIndSoFar = min ( minIndSoFar , pairs [ i ] . first ) ; maxIndSoFar = max ( maxIndSoFar , pairs [ i ] . first ) ; } return maxi ; } int main ( ) { int height [ ] = { 2 , 1 , 3 , 4 , 6 , 5 } ; int n = sizeof ( height ) / sizeof ( height [ 0 ] ) ; cout << maxWater ( height , n ) ; }
Check if the given array contains all the divisors of some integer | C ++ implementation of the approach ; Function that returns true if arr [ ] contains all the divisors of some integer ; Maximum element from the array ; Vector to store divisors of the maximum element i . e . X ; Store all the divisors of X ; If the lengths of a [ ] and b are different return false ; Sort a [ ] and b ; If divisors are not equal return false ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkDivisors ( int a [ ] , int n ) { int X = * max_element ( a , a + n ) ; vector < int > b ; for ( int i = 1 ; i * i <= X ; i ++ ) { if ( X % i == 0 ) { b . push_back ( i ) ; if ( X / i != i ) b . push_back ( X / i ) ; } } if ( b . size ( ) != n ) return false ; sort ( a , a + n ) ; sort ( b . begin ( ) , b . end ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( b [ i ] != a [ i ] ) return false ; } return true ; } int main ( ) { int arr [ ] = { 8 , 1 , 2 , 12 , 48 , 6 , 4 , 24 , 16 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( checkDivisors ( arr , N ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Kth largest node among all directly connected nodes to the given node in an undirected graph | C ++ implementation of the approach ; Function to print Kth node for each node ; Vector to store nodes directly connected to ith node along with their values ; Add edges to the vector along with the values of the node ; Sort neighbors of every node and find the Kth node ; Get the kth node ; If total nodes are < k ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findKthNode ( int u [ ] , int v [ ] , int n , int val [ ] , int V , int k ) { vector < pair < int , int > > g [ V ] ; for ( int i = 0 ; i < n ; i ++ ) { g [ u [ i ] ] . push_back ( make_pair ( val [ v [ i ] ] , v [ i ] ) ) ; g [ v [ i ] ] . push_back ( make_pair ( val [ u [ i ] ] , u [ i ] ) ) ; } for ( int i = 0 ; i < V ; i ++ ) { if ( g [ i ] . size ( ) > 0 ) sort ( g [ i ] . begin ( ) , g [ i ] . end ( ) ) ; if ( k <= g [ i ] . size ( ) ) printf ( " % d STRNEWLINE " , g [ i ] [ g [ i ] . size ( ) - k ] . second ) ; else printf ( " - 1 STRNEWLINE " ) ; } return ; } int main ( ) { int V = 3 ; int val [ ] = { 2 , 4 , 3 } ; int u [ ] = { 0 , 0 , 1 } ; int v [ ] = { 2 , 1 , 2 } ; int n = sizeof ( u ) / sizeof ( int ) ; int k = 2 ; findKthNode ( u , v , n , val , V , k ) ; return 0 ; }
Minimum length of square to contain at least half of the given Coordinates | C ++ implementation of the above approach ; Function to Calculate Absolute Value ; Function to Calculate the Minimum value of M ; To store the minimum M for each point in array ; Sort the array ; Index at which atleast required point are inside square of length 2 * M ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int mod ( int x ) { if ( x >= 0 ) return x ; return - x ; } void findSquare ( int n ) { int points [ n ] [ 2 ] = { { 1 , 2 } , { -3 , 4 } , { 1 , 78 } , { -3 , -7 } } ; int a [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int x , y ; x = points [ i ] [ 0 ] ; y = points [ i ] [ 1 ] ; a [ i ] = max ( mod ( x ) , mod ( y ) ) ; } sort ( a , a + n ) ; int index = floor ( n / 2 ) - 1 ; cout << " Minimum ▁ M ▁ required ▁ is : ▁ " << a [ index ] << endl ; } int main ( ) { int N ; N = 4 ; findSquare ( N ) ; return 0 ; }