codeparrot/xlcost-text-to-code huggingface dataset:
Longest substring of only 4 's from the first N characters of the infinite string | C ++ implementation of the approach ; Function to return the length of longest contiguous string containing only 4 aTMs from the first N characters of the string ; Initialize result ; Initialize prefix sum array of characters and product variable ; Preprocessing of prefix sum array ; Initialize variable to store the string length where N belongs to ; Finding the string length where N belongs to ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAXN  30 NEW_LINE int countMaxLength ( int N ) { int res ; int pre [ MAXN ] , p = 1 ; pre [ 0 ] = 0 ; for ( int i = 1 ; i < MAXN ; i ++ ) { p *= 2 ; pre [ i ] = pre [ i - 1 ] + i * p ; } int ind ; for ( int i = 1 ; i < MAXN ; i ++ ) { if ( pre [ i ] >= N ) { ind = i ; break ; } } int x = N - pre [ ind - 1 ] ; int y = 2 * ind - 1 ; if ( x >= y ) res = min ( x , y ) ; else res = max ( x , 2 * ( ind - 2 ) + 1 ) ; return res ; } int main ( ) { int N = 25 ; cout << countMaxLength ( N ) ; return 0 ; }
Difference between Recursion and Iteration | C ++ program to find factorial of given number ; -- -- - Recursion -- -- - method to find factorial of given number ; recursion call ; -- -- - Iteration -- -- - Method to find the factorial of a given number ; using iteration ; Driver method
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorialUsingRecursion ( int n ) { if ( n == 0 ) return 1 ; return n * factorialUsingRecursion ( n - 1 ) ; } int factorialUsingIteration ( int n ) { int res = 1 , i ; for ( i = 2 ; i <= n ; i ++ ) res *= i ; return res ; } int main ( ) { int num = 5 ; cout << " Factorial ▁ of ▁ " << num << " ▁ using ▁ Recursion ▁ is : ▁ " << factorialUsingRecursion ( 5 ) << endl ; cout << " Factorial ▁ of ▁ " << num << " ▁ using ▁ Iteration ▁ is : ▁ " << factorialUsingIteration ( 5 ) ; return 0 ; }
In | An Not in - place C ++ program to reverse an array ; Function to reverse arr [ ] from start to end ; Create a copy array and store reversed elements ; Now copy reversed elements back to arr [ ] ; Utility function to print an array ; Driver function to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; void revereseArray ( int arr [ ] , int n ) { int rev [ n ] ; for ( int i = 0 ; i < n ; i ++ ) rev [ n - i - 1 ] = arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = rev [ i ] ; } void printArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) cout << arr [ i ] << " ▁ " ; cout << endl ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printArray ( arr , n ) ; revereseArray ( arr , n ) ; cout << " Reversed ▁ array ▁ is " << endl ; printArray ( arr , n ) ; return 0 ; }
In | An in - place C ++ program to reverse an array ; Function to reverse arr [ ] from start to end ; Utility function to print an array ; Driver function to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; void revereseArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n / 2 ; i ++ ) swap ( arr [ i ] , arr [ n - i - 1 ] ) ; } void printArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) cout << arr [ i ] << " ▁ " ; cout << endl ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printArray ( arr , n ) ; revereseArray ( arr , n ) ; cout << " Reversed ▁ array ▁ is " << endl ; printArray ( arr , n ) ; return 0 ; }
Find Binary string by converting all 01 or 10 to 11 after M iterations | C ++ program for the above approach . ; Function to find the modified binary string after M iterations ; Set the value of M to the minimum of N or M . ; Declaration of current string state ; Loop over M iterations ; Set the current state as null before each iteration ; Check if this zero has exactly one 1 as neighbour ; Flip the zero ; If there is no change , then no need for further iterations . ; Set the current state as the new previous state ; Driver Code ; Given String ; Number of Iterations ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findString ( string str , int M ) { int N = str . length ( ) ; M = min ( M , N ) ; string s1 = " " ; while ( M != 0 ) { s1 = " " ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == '0' ) { if ( ( str [ i - 1 ] == '1' && str [ i + 1 ] != '1' ) || ( str [ i - 1 ] != '1' && str [ i + 1 ] == '1' ) ) s1 += '1' ; else s1 += '0' ; } else s1 += '1' ; } if ( str == s1 ) break ; str = s1 ; M -- ; } cout << s1 ; } int main ( ) { string str = "0110100" ; int M = 3 ; findString ( str , M ) ; return 0 ; }
Count of subsequences with a sum in range [ L , R ] and difference between max and min element at least X | C ++ program for the above approach ; Function to find the number of subsequences of the given array with a sum in range [ L , R ] and the difference between the maximum and minimum element is at least X ; Initialize answer as 0 ; Creating mask from [ 0 , 2 ^ n - 1 ] ; Stores the count and sum of selected elements respectively ; Variables to store the value of Minimum and maximum element ; Traverse the array ; If the jth bit of the ith mask is on ; Add the selected element ; Update maxVal and minVal value ; Check if the given conditions are true , increment ans by 1. ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberofSubsequences ( int a [ ] , int L , int R , int X , int n ) { int ans = 0 ; for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int cnt = 0 , sum = 0 ; int minVal = INT_MAX , maxVal = INT_MIN ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) ) { cnt += 1 ; sum += a [ j ] ; maxVal = max ( maxVal , a [ j ] ) ; minVal = min ( minVal , a [ j ] ) ; } } if ( cnt >= 2 && sum >= L && sum <= R && ( maxVal - minVal >= X ) ) { ans += 1 ; } } return ans ; } int main ( ) { int a [ ] = { 10 , 20 , 30 , 25 } ; int L = 40 , R = 50 , X = 10 ; int N = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << numberofSubsequences ( a , L , R , X , N ) << endl ; return 0 ; }
Minimum count of numbers needed from 1 to N that yields the sum as K | C ++ program for the above approach ; Function to find minimum number of elements required to obtain sum K ; Stores the maximum sum that can be obtained ; If K is greater than the Maximum sum ; If K is less than or equal to to N ; Stores the sum ; Stores the count of numbers needed ; Iterate until N is greater than or equal to 1 and sum is less than K ; Increment count by 1 ; Increment sum by N ; Update the sum ; Finally , return the count ; Driver Code ; Given Input ; Function Call
#include <iostream> NEW_LINE using namespace std ; int Minimum ( int N , int K ) { int sum = N * ( N + 1 ) / 2 ; if ( K > sum ) return -1 ; if ( K <= N ) return 1 ; sum = 0 ; int count = 0 ; while ( N >= 1 && sum < K ) { count += 1 ; sum += N ; N -= 1 ; } return count ; } int main ( ) { int N = 5 , K = 10 ; cout << ( Minimum ( N , K ) ) ; return 0 ; }
Maximize X such that sum of numbers in range [ 1 , X ] is at most K | C ++ program for the above approach ; Function to count the elements with sum of the first that many natural numbers less than or equal to K ; If K equals to 0 ; Stores the result ; Iterate until low is less than or equal to high ; Stores the sum of first mid natural numbers ; If sum is less than or equal to K ; Update res and low ; Otherwise , ; Update ; Return res ; Driver Code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Count ( int N , int K ) { if ( K == 0 ) return 0 ; int res = 0 ; int low = 1 , high = N ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int sum = ( mid * mid + mid ) / 2 ; if ( sum <= K ) { res = max ( res , mid ) ; low = mid + 1 ; } else { high = mid - 1 ; } } return res ; } int main ( ) { int N = 6 , K = 14 ; cout << Count ( N , K ) ; return 0 ; }
Largest number having both positive and negative values present in the array | C ++ program for the above approach ; Function to find the largest number k such that both k and - k are present in the array ; Stores the array elements ; Initialize a variable res as 0 to store maximum element while traversing the array ; Iterate through array arr ; Add the current element into the st ; Check if the negative of this element is also present in the st or not ; Return the resultant element ; Drive Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int largestNum ( int arr [ ] , int n ) { set < int > st ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { st . insert ( arr [ i ] ) ; if ( st . find ( -1 * arr [ i ] ) != st . end ( ) ) { res = max ( res , abs ( arr [ i ] ) ) ; } } return res ; } int main ( ) { int arr [ ] = { 3 , 2 , -2 , 5 , -3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << largestNum ( arr , n ) ; }
Check if an element is present in an array using at most floor ( N / 2 ) + 2 comparisons | C ++ program for the above approach ; Function to check whether X is present in the array A [ ] ; Initialise a pointer ; Store the number of comparisons ; Variable to store product ; Check is N is odd ; Update i and T ; Traverse the array ; Check if i < N ; Update T ; Check if T is equal to 0 ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findElement ( int A [ ] , int N , int X ) { int i = 0 ; int Comparisons = 0 ; int T = 1 ; string Found = " No " ; Comparisons ++ ; if ( N % 2 == 1 ) { i = 1 ; T *= ( A [ 0 ] - X ) ; } for ( ; i < N ; i += 2 ) { Comparisons += 1 ; T *= ( A [ i ] - X ) ; T *= ( A [ i + 1 ] - X ) ; } Comparisons += 1 ; if ( T == 0 ) { cout << " Yes ▁ " << Comparisons ; } else { cout << " No " ; } } int main ( ) { int A [ ] = { -3 , 5 , 11 , 3 , 100 , 2 , 88 , 22 , 7 , 900 , 23 , 4 , 1 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int X = 1 ; findElement ( A , N , X ) ; return 0 ; }
Search an element in a sorted array formed by reversing subarrays from a random index | C ++ program for the above approach ; Function to search an element in a sorted array formed by reversing subarrays from a random index ; Set the boundaries for binary search ; Apply binary search ; Initialize the middle element ; If element found ; Random point is on right side of mid ; From l to mid arr is reverse sorted ; Random point is on the left side of mid ; From mid to h arr is reverse sorted ; Return Not Found ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find ( vector < int > arr , int N , int key ) { int l = 0 ; int h = N - 1 ; while ( l <= h ) { int mid = ( l + h ) / 2 ; if ( arr [ mid ] == key ) return mid ; if ( arr [ l ] >= arr [ mid ] ) { if ( arr [ l ] >= key && key >= arr [ mid ] ) h = mid - 1 ; else l = mid + 1 ; } else { if ( arr [ mid ] >= key && key >= arr [ h ] ) l = mid + 1 ; else h = mid - 1 ; } } return -1 ; } int main ( ) { vector < int > arr = { 10 , 8 , 6 , 5 , 2 , 1 , 13 , 12 } ; int N = arr . size ( ) ; int key = 8 ; int ans = find ( arr , N , key ) ; cout << ans ; }
Maximum elements that can be removed from front of two arrays such that their sum is at most K | C ++ program for the above approach ; Function to find the maximum number of items that can be removed from both the arrays ; Stores the maximum item count ; Stores the prefix sum of the cost of items ; Insert the item cost 0 at the front of the arrays ; Build the prefix sum for the array A [ ] ; Update the value of A [ i ] ; Build the prefix sum for the array B [ ] ; Update the value of B [ i ] ; Iterate through each item of the array A [ ] ; If A [ i ] exceeds K ; Store the remaining amount after taking top i elements from the array A ; Store the number of items possible to take from the array B [ ] ; Store low and high bounds for binary search ; Binary search to find number of item that can be taken from stack B in rem amount ; Calculate the mid value ; Update the value of j and lo ; Update the value of the hi ; Store the maximum of total item count ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxItems ( int n , int m , int a [ ] , int b [ ] , int K ) { int count = 0 ; int A [ n + 1 ] ; int B [ m + 1 ] ; A [ 0 ] = 0 ; B [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { A [ i ] = a [ i - 1 ] + A [ i - 1 ] ; } for ( int i = 1 ; i <= m ; i ++ ) { B [ i ] = b [ i - 1 ] + B [ i - 1 ] ; } for ( int i = 0 ; i <= n ; i ++ ) { if ( A [ i ] > K ) break ; int rem = K - A [ i ] ; int j = 0 ; int lo = 0 , hi = m ; while ( lo <= hi ) { int mid = ( lo + hi ) / 2 ; if ( B [ mid ] <= rem ) { j = mid ; lo = mid + 1 ; } else { hi = mid - 1 ; } } count = max ( j + i , count ) ; } cout << count ; } int main ( ) { int n = 4 , m = 5 , K = 7 ; int A [ n ] = { 2 , 4 , 7 , 3 } ; int B [ m ] = { 1 , 9 , 3 , 4 , 5 } ; maxItems ( n , m , A , B , K ) ; return 0 ; }
Numbers of pairs from an array whose average is also present in the array | CPP program for the above approach ; Function to count the total count of pairs having sum S ; Store the total count of all elements in map mp ; Initialize value to 0 , if key not found ; Stores the total count of total pairs ; Iterate through each element and increment the count ; If the value ( S - arr [ i ] ) exists in the map hm ; Update the twice count ; Return the half of twice_count ; Function to count of pairs having whose average exists in the array ; Stores the total count of pairs ; Use set to remove duplicates ; Insert all the element in the set S ; For every sum find the getCountPairs ; Return the total count of pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getCountPairs ( int arr [ ] , int N , int S ) { map < int , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { mp [ arr [ i ] ] ++ ; } int twice_count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . find ( S - arr [ i ] ) != mp . end ( ) ) { twice_count += mp [ S - arr [ i ] ] ; } if ( S - arr [ i ] == arr [ i ] ) twice_count -- ; } return twice_count / 2 ; } int countPairs ( int arr [ ] , int N ) { int count = 0 ; set < int > S ; for ( int i = 0 ; i < N ; i ++ ) S . insert ( arr [ i ] ) ; for ( int ele : S ) { int sum = 2 * ele ; count += getCountPairs ( arr , N , sum ) ; } return count ; } int main ( ) { int N = 6 ; int arr [ ] = { 4 , 2 , 5 , 1 , 3 , 5 } ; cout << ( countPairs ( arr , N ) ) ; }
Number which is co | C ++ program for the above approach ; Function to check whether the given number N is prime or not ; Base Case ; If N has more than one factor , then return false ; Otherwise , return true ; Function to find X which is co - prime with the integers from the range [ L , R ] ; Store the resultant number ; Check for prime integers greater than R ; If the current number is prime , then update coPrime and break out of loop ; Print the resultant number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int N ) { if ( N == 1 ) return false ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) return false ; } return true ; } int findCoPrime ( int L , int R ) { int coPrime ; for ( int i = R + 1 ; ; i ++ ) { if ( isPrime ( i ) ) { coPrime = i ; break ; } } return coPrime ; } int main ( ) { int L = 16 , R = 17 ; cout << findCoPrime ( L , R ) ; return 0 ; }
Maximum value of arr [ i ] + arr [ j ] + i â €“ j for any pair of an array | C ++ program to for the above approach ; Function to find the maximum value of arr [ i ] + arr [ j ] + i - j over all pairs ; Stores the required result ; Traverse over all the pairs ( i , j ) ; Calculate the value of the expression and update the overall maximum value ; Print the result ; Driven Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maximumValue ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { ans = max ( ans , arr [ i ] + arr [ j ] + i - j ) ; } } cout << ans ; } int main ( ) { int arr [ ] = { 1 , 9 , 3 , 6 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maximumValue ( arr , N ) ; return 0 ; }
Maximum value of arr [ i ] + arr [ j ] + i â €“ j for any pair of an array | C ++ program for the above approach ; Function to find the maximum value of ( arr [ i ] + arr [ j ] + i - j ) possible for a pair in the array ; Stores the maximum value of ( arr [ i ] + i ) ; Stores the required result ; Traverse the array arr [ ] ; Calculate for current pair and update maximum value ; Update maxValue if ( arr [ i ] + I ) is greater than maxValue ; Print the result ; Driven Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maximumValue ( int arr [ ] , int n ) { int maxvalue = arr [ 0 ] ; int result = 0 ; for ( int i = 1 ; i < n ; i ++ ) { result = max ( result , maxvalue + arr [ i ] - i ) ; maxvalue = max ( maxvalue , arr [ i ] + i ) ; } cout << result ; } int main ( ) { int arr [ ] = { 1 , 9 , 3 , 6 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maximumValue ( arr , N ) ; return 0 ; }
Minimum sum of absolute differences of pairs in a triplet from three arrays | C ++ program for the above approach ; Function to find the value closest to K in the array A [ ] ; Initialize close value as the end element ; Find lower bound of the array ; If lower_bound is found ; If lower_bound is not the first array element ; If * ( it - 1 ) is closer to k ; Return closest value of k ; Function to find the minimum sum of abs ( arr [ i ] - brr [ j ] ) and abs ( brr [ j ] - crr [ k ] ) ; Sort the vectors arr and crr ; Initialize minimum as INT_MAX ; Traverse the array brr [ ] ; Stores the element closest to val from the array arr [ ] ; Stores the element closest to val from the array crr [ ] ; If sum of differences is minimum ; Update the minimum ; Print the minimum absolute difference possible ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int closestValue ( vector < int > A , int k ) { int close = A . back ( ) ; auto it = lower_bound ( A . begin ( ) , A . end ( ) , k ) ; if ( it != A . end ( ) ) { close = * it ; if ( it != A . begin ( ) ) { if ( ( k - * ( it - 1 ) ) < ( close - k ) ) { close = * ( it - 1 ) ; } } } return close ; } void minPossible ( vector < int > arr , vector < int > brr , vector < int > crr ) { sort ( arr . begin ( ) , arr . end ( ) ) ; sort ( crr . begin ( ) , crr . end ( ) ) ; int minimum = INT_MAX ; for ( int val : brr ) { int arr_close = closestValue ( arr , val ) ; int crr_close = closestValue ( crr , val ) ; if ( abs ( val - arr_close ) + abs ( val - crr_close ) < minimum ) minimum = abs ( val - arr_close ) + abs ( val - crr_close ) ; } cout << minimum ; } int main ( ) { vector < int > a = { 1 , 8 , 5 } ; vector < int > b = { 2 , 9 } ; vector < int > c = { 5 , 4 } ; minPossible ( a , b , c ) ; return 0 ; }
Maximum time required for all patients to get infected | C ++ program for the above approach ; Direction arrays ; Function to find the maximum time required for all patients to get infected ; Stores the number of rows ; Stores the number of columns ; Stores whether particular index ( i , j ) is visited or not ; Stores index and time of infection of infected persons ; Stores uninfected patients count ; Stores time at which last person got infected ; Traverse the matrix ; If the current patient is already infected ; Push the index of current patient and mark it as visited ; If current patient is uninfected increment uninfected count ; Iterate until queue becomes empty ; Stores the front element of queue ; Pop out the front element ; Check for all four adjacent indices ; Find the index of the adjacent cell ; If the current adjacent cell is invalid or it contains an infected patient ; Continue to the next neighbouring cell ; Push the infected neighbour into queue ; If an uninfected patient is present ; Return the final result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < pair < int , int > > direction = { { 1 , 0 } , { 0 , -1 } , { -1 , 0 } , { 0 , 1 } } ; int maximumTime ( vector < vector < int > > arr ) { int n = arr . size ( ) ; int m = arr [ 0 ] . size ( ) ; vector < vector < bool > > visited ( n , vector < bool > ( m , 0 ) ) ; queue < pair < pair < int , int > , int > > q ; int uninfected_count = 0 ; int time = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( arr [ i ] [ j ] == 2 ) { q . push ( { { i , j } , 0 } ) ; visited [ i ] [ j ] = 1 ; } if ( arr [ i ] [ j ] == 1 ) { uninfected_count ++ ; } } } while ( ! q . empty ( ) ) { pair < pair < int , int > , int > current = q . front ( ) ; time = current . second ; q . pop ( ) ; for ( auto it : direction ) { int i = current . first . first + it . first ; int j = current . first . second + it . second ; if ( i < 0 j < 0 i > = n j > = m arr [ i ] [ j ] != 1 visited [ i ] [ j ] != 0 ) { continue ; } q . push ( { { i , j } , time + 1 } ) ; visited [ i ] [ j ] = 1 ; uninfected_count -- ; } } if ( uninfected_count != 0 ) return -1 ; return time ; } int main ( ) { vector < vector < int > > arr = { { 2 , 1 , 0 , 2 , 1 } , { 1 , 0 , 1 , 2 , 1 } , { 1 , 0 , 0 , 2 , 1 } } ; cout << maximumTime ( arr ) ; return 0 ; }
Minimize swaps required to make the first and last elements the largest and smallest elements in the array respectively | C ++ program for the above approach ; Function to find the minimum number of swaps required to make the first and the last elements the largest and smallest element in the array ; Stores the count of swaps ; Stores the maximum element ; Stores the minimum element ; If the array contains a single distinct element ; Stores the indices of the maximum and minimum elements ; If the first index of the maximum element is found ; If current index has the minimum element ; Update the count of operations to place largest element at the first ; Update the count of operations to place largest element at the last ; If smallest element is present before the largest element initially ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimum_swaps ( int arr [ ] , int n ) { int count = 0 ; int max_el = * max_element ( arr , arr + n ) ; int min_el = * min_element ( arr , arr + n ) ; if ( min_el == max_el ) return 0 ; int index_max = -1 ; int index_min = -1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == max_el && index_max == -1 ) { index_max = i ; } if ( arr [ i ] == min_el ) { index_min = i ; } } count += index_max ; count += ( n - 1 - index_min ) ; if ( index_min < index_max ) count -= 1 ; return count ; } int main ( ) { int arr [ ] = { 2 , 4 , 1 , 6 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minimum_swaps ( arr , N ) ; return 0 ; }
Maximum difference between node and its ancestor in a Directed Acyclic Graph ( DAG ) | C ++ program for the above approach ; Function to perform DFS Traversal on the given graph ; Update the value of ans ; Update the currentMin and currentMax ; Traverse the adjacency list of the node src ; Recursively call for the child node ; Function to calculate maximum absolute difference between a node and its ancestor ; Stores the adjacency list of graph ; Create Adjacency list ; Add a directed edge ; Perform DFS Traversal ; Print the maximum absolute difference ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void DFS ( int src , vector < int > Adj [ ] , int & ans , int arr [ ] , int currentMin , int currentMax ) { ans = max ( ans , max ( abs ( currentMax - arr [ src - 1 ] ) , abs ( currentMin - arr [ src - 1 ] ) ) ) ; currentMin = min ( currentMin , arr [ src - 1 ] ) ; currentMax = min ( currentMax , arr [ src - 1 ] ) ; for ( auto & child : Adj [ src ] ) { DFS ( child , Adj , ans , arr , currentMin , currentMax ) ; } } void getMaximumDifference ( int Edges [ ] [ 2 ] , int arr [ ] , int N , int M ) { vector < int > Adj [ N + 1 ] ; for ( int i = 0 ; i < M ; i ++ ) { int u = Edges [ i ] [ 0 ] ; int v = Edges [ i ] [ 1 ] ; Adj [ u ] . push_back ( v ) ; } int ans = 0 ; DFS ( 1 , Adj , ans , arr , arr [ 0 ] , arr [ 0 ] ) ; cout << ans ; } int main ( ) { int N = 5 , M = 4 ; int Edges [ ] [ 2 ] = { { 1 , 2 } , { 2 , 3 } , { 4 , 5 } , { 1 , 3 } } ; int arr [ ] = { 13 , 8 , 3 , 15 , 18 } ; getMaximumDifference ( Edges , arr , N , M ) ; return 0 ; }