codeparrot/xlcost-text-to-code huggingface dataset:
XOR of elements in a given range with updates using Fenwick Tree | C ++ Program to find XOR of elements in given range [ L , R ] . ; Returns XOR of arr [ 0. . index ] . This function assumes that the array is preprocessed and partial XORs of array elements are stored in BITree [ ] . ; Traverse ancestors of BITree [ index ] ; XOR current element of BIT to ans ; Update index to that of the parent node in getXor ( ) view by subtracting LSB ( Least Significant Bit ) ; Updates the Binary Index Tree by replacing all ancestors of index by their respective XOR with val ; Traverse all ancestors and XOR with ' val ' . ; XOR ' val ' to current node of BIT ; Update index to that of the parent node in updateBit ( ) view by adding LSB ( Least Significant Bit ) ; Constructs and returns a Binary Indexed Tree for the given array ; Create and initialize the Binary Indexed Tree ; Store the actual values in BITree [ ] using update ( ) ; Driver Code ; Create the Binary Indexed Tree ; Solve each query in Q ; Update the values of all ancestors of idx
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getXOR ( int BITree [ ] , int index ) { int ans = 0 ; index += 1 ; while ( index > 0 ) { ans ^= BITree [ index ] ; index -= index & ( - index ) ; } return ans ; } void updateBIT ( int BITree [ ] , int n , int index , int val ) { index = index + 1 ; while ( index <= n ) { BITree [ index ] ^= val ; index += index & ( - index ) ; } } int * constructBITree ( int arr [ ] , int n ) { int * BITree = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) BITree [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) updateBIT ( BITree , n , i , arr [ i ] ) ; return BITree ; } int rangeXor ( int BITree [ ] , int l , int r ) { return getXOR ( BITree , r ) ^ getXOR ( BITree , l - 1 ) ; } int main ( ) { int A [ ] = { 2 , 1 , 1 , 3 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; vector < vector < int > > q = { { 1 , 0 , 9 } , { 2 , 3 , 6 } , { 2 , 5 , 5 } , { 2 , 8 , 1 } , { 1 , 0 , 9 } } ; int * BITree = constructBITree ( A , n ) ; for ( int i = 0 ; i < q . size ( ) ; i ++ ) { int id = q [ i ] [ 0 ] ; if ( id == 1 ) { int L = q [ i ] [ 1 ] ; int R = q [ i ] [ 2 ] ; cout << " XOR ▁ of ▁ elements ▁ " << " in ▁ given ▁ range ▁ is ▁ " << rangeXor ( BITree , L , R ) << " STRNEWLINE " ; } else { int idx = q [ i ] [ 1 ] ; int val = q [ i ] [ 2 ] ; A [ idx ] ^= val ; updateBIT ( BITree , n , idx , val ) ; } } return 0 ; }
Count of Double Prime numbers in a given range L to R | C ++ program to find the count of Double Prime numbers in the range L to R ; Array to make Sieve where arr [ i ] = 0 indicates non prime and arr [ i ] = 1 indicates prime ; Array to find double prime ; Function to find the number double prime numbers in range ; Assume all numbers as prime ; Check if the number is prime ; check for multiples of i ; Make all multiples of ith prime as non - prime ; Check if number at ith position is prime then increment count ; Indicates count of numbers from 1 to i that are also prime and hence double prime ; If number is not a double prime ; finding cumulative sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int arr [ 1000001 ] ; int dp [ 1000001 ] ; void count ( ) { int maxN = 1000000 , i , j ; for ( i = 0 ; i < maxN ; i ++ ) arr [ i ] = 1 ; arr [ 0 ] = 0 ; arr [ 1 ] = 0 ; for ( i = 2 ; i * i <= maxN ; i ++ ) { if ( arr [ i ] == 1 ) { for ( j = 2 * i ; j <= maxN ; j += i ) { arr [ j ] = 0 ; } } } int cnt = 0 ; for ( i = 0 ; i <= maxN ; i ++ ) { if ( arr [ i ] == 1 ) cnt ++ ; if ( arr [ cnt ] == 1 ) dp [ i ] = 1 ; else dp [ i ] = 0 ; } for ( i = 1 ; i <= maxN ; i ++ ) dp [ i ] += dp [ i - 1 ] ; } int main ( ) { int L = 4 , R = 12 ; count ( ) ; cout << dp [ R ] - dp [ L - 1 ] ; return 0 ; }
Count of ungrouped characters after dividing a string into K groups of distinct characters | C ++ code to implement the above approach ; create array where index represents alphabets ; fill count of every alphabet to corresponding array index ; count for every element how much is exceeding from no . of groups then sum them ; print answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findUngroupedElement ( string s , int k ) { int n = s . length ( ) ; int b [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) b [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { char p = s . at ( i ) ; b [ p - ' a ' ] += 1 ; } int sum = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( b [ i ] > k ) sum += b [ i ] - k ; } cout << sum << endl ; } int main ( ) { string s = " stayinghomesaveslife " ; int k = 1 ; findUngroupedElement ( s , k ) ; return 0 ; }
Generate an array of given size with equal count and sum of odd and even numbers | C ++ implementation to find the array containing same count of even and odd elements with equal sum of even and odd elements ; Function to find the array such that the array contains the same count of even and odd elements with equal sum of even and odd elements ; Length of array which is not divisible by 4 is unable to form such array ; Loop to find the resulted array containing the same count of even and odd elements ; Find the total sum of even elements ; Find the total sum of odd elements ; Find the difference between the total sum of even and odd elements ; The difference will be added in the last odd element ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSolution ( int N ) { if ( N % 4 != 0 ) cout << -1 << " STRNEWLINE " ; else { int temp = 0 , sum_odd = 0 , sum_even = 0 ; int result [ N ] = { 0 } ; for ( int i = 0 ; i < N ; i += 2 ) { temp += 2 ; result [ i + 1 ] = temp ; sum_even += result [ i + 1 ] ; result [ i ] = temp - 1 ; sum_odd += result [ i ] ; } int diff = sum_even - sum_odd ; result [ N - 2 ] += diff ; for ( int i = 0 ; i < N ; i ++ ) cout << result [ i ] << " ▁ " ; cout << " STRNEWLINE " ; } } int main ( ) { int N = 8 ; findSolution ( N ) ; return 0 ; }
Number of ways to place two queens on a N * N chess | C ++ implementation to find the number of ways to place two queens on the N * N chess board ; Function to find number of valid positions for two queens in the N * N chess board ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE #define ll  long long NEW_LINE using namespace std ; ll possiblePositions ( ll n ) { ll term1 = pow ( n , 4 ) ; ll term2 = pow ( n , 3 ) ; ll term3 = pow ( n , 2 ) ; ll term4 = n / 3 ; ll ans = ( ceil ) ( term1 ) / 2 - ( ceil ) ( 5 * term2 ) / 3 + ( ceil ) ( 3 * term3 ) / 2 - term4 ; return ans ; } int main ( ) { ll n ; n = 3 ; ll ans = possiblePositions ( n ) ; cout << ans << endl ; return 0 ; }
Group all co | C ++ implementation to group mutually coprime numbers into one group with minimum group possible ; Function to group the mutually co - prime numbers into one group ; Loop for the numbers less than the 4 ; Integers 1 , 2 and 3 can be grouped into one group ; Consecutive even and odd numbers ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void mutually_coprime ( int n ) { if ( n <= 3 ) { for ( int j = 1 ; j <= n ; j ++ ) { cout << j << " ▁ " ; } cout << " STRNEWLINE " ; } else { cout << "1 ▁ 2 ▁ 3 STRNEWLINE " ; for ( int j = 4 ; j < n ; j += 2 ) { cout << j << " ▁ " << j + 1 << " STRNEWLINE " ; } if ( n % 2 == 0 ) cout << n << " STRNEWLINE " ; } } int main ( ) { int n = 9 ; mutually_coprime ( n ) ; }
Maximum sum subset having equal number of positive and negative elements | C ++ implementation to find the maximum sum subset having equal number of positive and negative elements in the subset ; Function to find maximum sum subset with equal number of positive and negative elements ; Loop to store the positive and negative elements in two different array ; Sort both the array ; Pointers starting from the highest elements ; Find pairs having sum greater than zero ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxSum ( int * arr , int n ) { vector < int > a ; vector < int > b ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) { a . push_back ( arr [ i ] ) ; } else if ( arr [ i ] < 0 ) { b . push_back ( arr [ i ] ) ; } } sort ( a . begin ( ) , a . end ( ) ) ; sort ( b . begin ( ) , b . end ( ) ) ; int p = a . size ( ) - 1 ; int q = b . size ( ) - 1 ; int s = 0 ; while ( p >= 0 && q >= 0 ) { if ( a [ p ] + b [ q ] > 0 ) { s = s + a [ p ] + b [ q ] ; } else { break ; } p = p - 1 ; q = q - 1 ; } return s ; } int main ( ) { int arr1 [ ] = { 1 , -2 , 3 , 4 , -5 , 8 } ; int n1 = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; cout << findMaxSum ( arr1 , n1 ) << endl ; return 0 ; }
Print the nodes with a prime degree in given Prufer sequence of a Tree | C ++ implementation to print the nodes with prime degree from the given prufer sequence ; Function to create Sieve to check primes ; False here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to print the nodes with prime degree in the tree whose Prufer sequence is given ; Hash - table to mark the degree of every node ; Initially let all the degrees be 1 ; Increase the count of the degree ; Print the nodes with prime degree ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void SieveOfEratosthenes ( bool prime [ ] , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } void PrimeDegreeNodes ( int prufer [ ] , int n ) { int nodes = n + 2 ; bool prime [ nodes + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; SieveOfEratosthenes ( prime , nodes + 1 ) ; int degree [ n + 2 + 1 ] ; for ( int i = 1 ; i <= nodes ; i ++ ) degree [ i ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) degree [ prufer [ i ] ] ++ ; for ( int i = 1 ; i <= nodes ; i ++ ) { if ( prime [ degree [ i ] ] ) { cout << i << " ▁ " ; } } } int main ( ) { int a [ ] = { 4 , 1 , 3 , 4 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; PrimeDegreeNodes ( a , n ) ; return 0 ; }
Find K 'th smallest number such that A + B = A | B | C ++ program for the above approach ; Function to find k 'th smallest number such that A + B = A | B ; res will store final answer ; Skip when j ' th ▁ position ▁ ▁ has ▁ 1 ▁ in ▁ binary ▁ representation ▁ ▁ as ▁ in ▁ res , ▁ j ' th position will be 0. ; j 'th bit is set ; If i ' th ▁ bit ▁ of ▁ k ▁ is ▁ 1 ▁ ▁ and ▁ i ' th bit of j is 0 then set i 'th bit in res. ; Proceed to next bit ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long kthSmallest ( long long a , long long k ) { long long res = 0 ; long long j = 0 ; for ( long long i = 0 ; i < 32 ; i ++ ) { while ( j < 32 && ( a & ( 1 << j ) ) ) { j ++ ; } if ( k & ( 1 << i ) ) { res |= ( 1LL << j ) ; } j ++ ; } return res ; } int main ( ) { long long a = 5 , k = 3 ; cout << kthSmallest ( a , k ) << " STRNEWLINE " ; return 0 ; }
Nodes with prime degree in an undirected Graph | C ++ implementation of the approach ; To store Prime Numbers ; Function to find the prime numbers till 10 ^ 5 ; Traverse all multiple of i and make it false ; Function to print the nodes having prime degree ; To store Adjacency List of a Graph ; Make Adjacency List ; To precompute prime numbers till 10 ^ 5 ; Traverse each vertex ; Find size of Adjacency List ; If length of Adj [ i ] is Prime then print it ; Driver code ; Vertices and Edges ; Edges ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int n = 10005 ; vector < bool > Prime ( n + 1 , true ) ; void SieveOfEratosthenes ( ) { int i , j ; Prime [ 0 ] = Prime [ 1 ] = false ; for ( i = 2 ; i * i <= 10005 ; i ++ ) { if ( Prime [ i ] ) { for ( j = 2 * i ; j < 10005 ; j += i ) { Prime [ j ] = false ; } } } } void primeDegreeNodes ( int N , int M , int edges [ ] [ 2 ] ) { vector < int > Adj [ N + 1 ] ; for ( int i = 0 ; i < M ; i ++ ) { int x = edges [ i ] [ 0 ] ; int y = edges [ i ] [ 1 ] ; Adj [ x ] . push_back ( y ) ; Adj [ y ] . push_back ( x ) ; } SieveOfEratosthenes ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { int x = Adj [ i ] . size ( ) ; if ( Prime [ x ] ) cout << i << ' ▁ ' ; } } int main ( ) { int N = 4 , M = 6 ; int edges [ M ] [ 2 ] = { { 1 , 2 } , { 1 , 3 } , { 1 , 4 } , { 2 , 3 } , { 2 , 4 } , { 3 , 4 } } ; primeDegreeNodes ( N , M , edges ) ; return 0 ; }
Number of ways to color N | C ++ program for the above approach ; Recursive function to count the ways ; Base case ; Initialise answer to 0 ; Color each uncolored block according to the given condition ; If any block is uncolored ; Check if adjacent blocks are colored or not ; Color the block ; recursively iterate for next uncolored block ; Uncolored for the next recursive call ; Return the final count ; Function to count the ways to color block ; Mark which blocks are colored in each recursive step ; Function call to count the ways ; Driver Code ; Number of blocks ; Number of colored blocks ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int mod = 1000000007 ; int countWays ( int colored [ ] , int count , int n ) { if ( count == n ) { return 1 ; } int answer = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { if ( colored [ i ] == 0 ) { if ( colored [ i - 1 ] == 1 colored [ i + 1 ] == 1 ) { colored [ i ] = 1 ; answer = ( answer + countWays ( colored , count + 1 , n ) ) % mod ; colored [ i ] = 0 ; } } } return answer ; } int waysToColor ( int arr [ ] , int n , int k ) { int colored [ n + 2 ] = { 0 } ; for ( int i = 0 ; i < k ; i ++ ) { colored [ arr [ i ] ] = 1 ; } return countWays ( colored , k , n ) ; } int main ( ) { int N = 6 ; int K = 3 ; int arr [ K ] = { 1 , 2 , 6 } ; cout << waysToColor ( arr , N , K ) ; return 0 ; }
Shortest Palindromic Substring | C ++ program to find the shortest palindromic substring ; Function return the shortest palindromic substring ; Finding the smallest character present in the string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char ShortestPalindrome ( string s ) { int n = s . length ( ) ; char ans = s [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { ans = min ( ans , s [ i ] ) ; } return ans ; } int main ( ) { string s = " geeksforgeeks " ; cout << ShortestPalindrome ( s ) ; return 0 ; }
Maximum length Subsequence with alternating sign and maximum Sum | C ++ implementation to find the subsequence with alternating sign having maximum size and maximum sum . ; Function to find the subsequence with alternating sign having maximum size and maximum sum . ; Find whether each element is positive or negative ; Find the required subsequence ; Find the maximum element in the specified range ; print the result ; Driver code ; array declaration ; size of array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSubsequence ( int arr [ ] , int n ) { int sign [ n ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) sign [ i ] = 1 ; else sign [ i ] = -1 ; } int k = 0 ; int result [ n ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { int cur = arr [ i ] ; int j = i ; while ( j < n && sign [ i ] == sign [ j ] ) { cur = max ( cur , arr [ j ] ) ; ++ j ; } result [ k ++ ] = cur ; i = j - 1 ; } for ( int i = 0 ; i < k ; i ++ ) cout << result [ i ] << " ▁ " ; cout << " STRNEWLINE " ; } int main ( ) { int arr [ ] = { -4 , 9 , 4 , 11 , -5 , -17 , 9 , -3 , -5 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findSubsequence ( arr , n ) ; return 0 ; }
Longest Palindrome in a String formed by concatenating its prefix and suffix | C ++ program to find the longest palindrome in a string formed by concatenating its prefix and suffix ; Function to check whether the string is a palindrome ; Reverse the string to compare with the original string ; Check if both are same ; Function to find the longest palindrome in a string formed by concatenating its prefix and suffix ; Length of the string ; Finding the length upto which the suffix and prefix forms a palindrome together ; Check whether the string has prefix and suffix substrings which are palindromes . ; Removing the suffix and prefix substrings which already forms a palindrome and storing them in separate strings ; Check all prefix substrings in the remaining string str ; Check if the prefix substring is a palindrome ; If the prefix substring is a palindrome then check if it is of maximum length so far ; Check all the suffix substrings in the remaining string str ; Check if the suffix substring is a palindrome ; If the suffix substring is a palindrome then check if it is of maximum length so far ; Combining all the thee parts of the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( string r ) { string p = r ; reverse ( p . begin ( ) , p . end ( ) ) ; return ( r == p ) ; } string PrefixSuffixPalindrome ( string str ) { int n = str . size ( ) , len = 0 ; for ( int i = 0 ; i < n / 2 ; i ++ ) { if ( str [ i ] != str [ n - i - 1 ] ) { len = i ; break ; } } string prefix = " " , suffix = " " ; string midPal = " " ; prefix = str . substr ( 0 , len ) ; suffix = str . substr ( n - len ) ; str = str . substr ( len , n - 2 * len ) ; for ( int i = 1 ; i <= str . size ( ) ; i ++ ) { string y = str . substr ( 0 , i ) ; if ( isPalindrome ( y ) ) { if ( midPal . size ( ) < y . size ( ) ) { midPal = y ; } } } for ( int i = 1 ; i <= str . size ( ) ; i ++ ) { string y = str . substr ( str . size ( ) - i ) ; if ( isPalindrome ( y ) ) { if ( midPal . size ( ) < y . size ( ) ) { midPal = y ; } } } string answer = prefix + midPal + suffix ; return answer ; } int main ( ) { string str = " abcdfdcecba " ; cout << PrefixSuffixPalindrome ( str ) << " STRNEWLINE " ; return 0 ; }
Minimize the maximum difference between adjacent elements in an array | C ++ implementation to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Function to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Initialising the minimum difference ; Traversing over subsets in iterative manner ; Number of elements to be taken in the subset ON bits of i represent elements not to be removed ; If the removed set is of size k ; Creating the new array after removing elements ; Maximum difference of adjacent elements of remaining array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumAdjacentDifference ( vector < int > a , int n , int k ) { int minDiff = INT_MAX ; for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int cnt = __builtin_popcount ( i ) ; if ( cnt == n - k ) { vector < int > temp ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) != 0 ) temp . push_back ( a [ j ] ) ; } int maxDiff = INT_MIN ; for ( int j = 0 ; j < temp . size ( ) - 1 ; j ++ ) { maxDiff = max ( maxDiff , temp [ j + 1 ] - temp [ j ] ) ; } minDiff = min ( minDiff , maxDiff ) ; } } return minDiff ; } int main ( ) { int n = 5 ; int k = 2 ; vector < int > a = { 3 , 7 , 8 , 10 , 14 } ; cout << minimumAdjacentDifference ( a , n , k ) ; return 0 ; }
Minimize the maximum difference between adjacent elements in an array | C ++ implementation to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Function to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Initialising the minimum difference ; Iterating over all subarrays of size n - k ; Maximum difference after removing elements ; Minimum Adjacent Difference ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumAdjacentDifference ( vector < int > a , int n , int k ) { int minDiff = INT_MAX ; for ( int i = 0 ; i <= k ; i ++ ) { int maxDiff = INT_MIN ; for ( int j = 0 ; j < n - k - 1 ; j ++ ) { for ( int p = i ; p <= i + j ; p ++ ) { maxDiff = max ( maxDiff , a [ p + 1 ] - a [ p ] ) ; } } minDiff = min ( minDiff , maxDiff ) ; } return minDiff ; } int main ( ) { int n = 5 ; int k = 2 ; vector < int > a = { 3 , 7 , 8 , 10 , 14 } ; cout << minimumAdjacentDifference ( a , n , k ) ; return 0 ; }
Minimize the maximum difference between adjacent elements in an array | C ++ implementation to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Function to find the minimum different in the subarrays of size K in the array ; Create a Double Ended Queue , Qi that will store indexes of array elements , queue will store indexes of useful elements in every window ; Process first k ( or first window ) elements of array ; For every element , the previous smaller elements are useless so remove them from Qi ; Add new element at rear of queue ; Process rest of the elements , i . e . , from arr [ k ] to arr [ n - 1 ] ; The element at the front of the queue is the largest element of previous window ; Remove the elements which are out of this window ; Remove all elements smaller than the currently being added element ( remove useless elements ) ; Add current element at the rear of Qi ; compare the maximum element of last window ; Function to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Create the difference array ; find minimum of all maximum of subarray sizes n - k - 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findKMin ( vector < int > arr , int n , int k ) { deque < int > Qi ( k ) ; int i ; for ( i = 0 ; i < k ; ++ i ) { while ( ( ! Qi . empty ( ) ) && arr [ i ] >= arr [ Qi . back ( ) ] ) Qi . push_back ( i ) ; } int minDiff = INT_MAX ; for ( ; i < n ; ++ i ) { minDiff = min ( minDiff , arr [ Qi . front ( ) ] ) ; while ( ( ! Qi . empty ( ) ) && Qi . front ( ) <= i - k ) Qi . pop_front ( ) ; while ( ( ! Qi . empty ( ) ) && arr [ i ] >= arr [ Qi . back ( ) ] ) Qi . pop_back ( ) ; Qi . push_back ( i ) ; } minDiff = min ( minDiff , arr [ Qi . front ( ) ] ) ; return minDiff ; } int minimumAdjacentDifference ( vector < int > a , int n , int k ) { vector < int > diff ( n - 1 ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { diff [ i ] = a [ i + 1 ] - a [ i ] ; } int answer = findKMin ( diff , n - 1 , n - k - 1 ) ; return answer ; } int main ( ) { int n = 5 ; int k = 2 ; vector < int > a = { 3 , 7 , 8 , 10 , 14 } ; cout << minimumAdjacentDifference ( a , n , k ) ; return 0 ; }
Minimum elements inserted in a sorted array to form an Arithmetic progression | C ++ implementation to find the minimum elements required to be inserted into an array to form an arithmetic progression ; Function to find the greatest common divisor of two numbers ; Function to find the minimum the minimum number of elements required to be inserted into array ; Difference array of consecutive elements of the array ; GCD of the difference array ; Loop to calculate the minimum number of elements required ; Driver Code ; Function calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcdFunc ( int a , int b ) { if ( b == 0 ) return a ; return gcdFunc ( b , a % b ) ; } int findMinimumElements ( int * a , int n ) { int b [ n - 1 ] ; for ( int i = 1 ; i < n ; i ++ ) { b [ i - 1 ] = a [ i ] - a [ i - 1 ] ; } int gcd = b [ 0 ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { gcd = gcdFunc ( gcd , b [ i ] ) ; } int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { ans += ( b [ i ] / gcd ) - 1 ; } return ans ; } int main ( ) { int arr1 [ ] = { 1 , 6 , 8 , 10 , 14 , 16 } ; int n1 = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; cout << findMinimumElements ( arr1 , n1 ) << endl ; }