codeparrot/xlcost-text-to-code huggingface dataset:
Count of all possible reverse bitonic subarrays | C ++ program for the above approach ; Function that counts all the reverse bitonic subarray in arr [ ] ; To store the count of reverse bitonic subarray ; Iterate the array and select the starting element ; Iterate for selecting the ending element for subarray ; Subarray arr [ i to j ] ; For 1 length , increment the count and continue ; For Decreasing Subarray ; Check if only Decreasing ; For Increasing Subarray ; Print the total count of subarrays ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countReversebitonic ( int arr [ ] , int n ) { int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int temp = arr [ i ] , f = 0 ; if ( j == i ) { c ++ ; continue ; } int k = i + 1 ; while ( temp > arr [ k ] && k <= j ) { temp = arr [ k ] ; k ++ ; } if ( k > j ) { c ++ ; f = 2 ; } while ( temp < arr [ k ] && k <= j && f != 2 ) { temp = arr [ k ] ; k ++ ; } if ( k > j && f != 2 ) { c ++ ; f = 0 ; } } } cout << c << endl ; } int main ( ) { int arr [ ] = { 2 , 3 , 1 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countReversebitonic ( arr , N ) ; }
Longest increasing sequence by the boundary elements of an Array | C ++ Program to print the longest increasing subsequence from the boundary elements of an array ; Function to return the length of Longest Increasing subsequence ; Set pointers at both ends ; Stores the recent value added to the subsequence ; Stores the length of the subsequence ; Check if both elements can be added to the subsequence ; Check if the element on the left can be added to the subsequence only ; Check if the element on the right can be added to the subsequence only ; If none of the values can be added to the subsequence ; Driver Code ; Length of array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSequence ( int n , int arr [ ] ) { int l = 0 , r = n - 1 ; int prev = INT_MIN ; int ans = 0 ; while ( l <= r ) { if ( arr [ l ] > prev && arr [ r ] > prev ) { if ( arr [ l ] < arr [ r ] ) { ans += 1 ; prev = arr [ l ] ; l += 1 ; } else { ans += 1 ; prev = arr [ r ] ; r -= 1 ; } } else if ( arr [ l ] > prev ) { ans += 1 ; prev = arr [ l ] ; l += 1 ; } else if ( arr [ r ] > prev ) { ans += 1 ; prev = arr [ r ] ; r -= 1 ; } else { break ; } } return ans ; } int main ( ) { int arr [ ] = { 3 , 5 , 1 , 4 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << longestSequence ( n , arr ) ; return 0 ; }
Total number of cells covered in a matrix after D days | C ++ implementation to find the Total number of cells covered in a matrix after D days ; Function to return the total infected cells after d days ; Top extension ; Bottom extension ; Left extension ; Right extension ; Calculating the cells in each quadrilateral ; Sum all of them to get total cells in each quadrilateral ; Add the singleblocks along the lines of top , down , left , right ; Return the ans ; Driver code ; Dimensions of cell ; Starting Coordinates ; Number of Days ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int n , int m , int x , int y , int d ) { int top = min ( d , x - 1 ) ; int down = min ( d , n - x ) ; int left = min ( d , y - 1 ) ; int right = min ( d , m - y ) ; int quad1 = top * left ; int quad2 = left * down ; int quad3 = down * right ; int quad4 = right * top ; int totalsq = quad1 + quad2 + quad3 + quad4 ; int singleBlocks = top + down + left + right + 1 ; return totalsq + singleBlocks ; } int main ( ) { int n , m , x , y , d ; n = 10 , m = 10 ; x = 7 , y = 8 ; d = 4 ; d -- ; cout << solve ( n , m , x , y , d ) ; }
Smallest subsequence with sum of absolute difference of consecutive elements maximized | C ++ program to find smallest subsequence with sum of absolute difference of consecutive elements maximized ; Function to print the smallest subsequence and its sum ; Final subsequence ; First element is a default endpoint ; Iterating through the array ; Check for monotonically increasing endpoint ; Check for monotonically decreasing endpoint ; Last element is a default endpoint ; Length of final subsequence ; Print the subsequence ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void getSubsequence ( vector < int > & arr , int n ) { vector < int > req ; req . push_back ( arr [ 0 ] ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] && arr [ i ] > arr [ i - 1 ] ) req . push_back ( arr [ i ] ) ; else if ( arr [ i ] < arr [ i + 1 ] && arr [ i ] < arr [ i - 1 ] ) req . push_back ( arr [ i ] ) ; } req . push_back ( arr [ n - 1 ] ) ; cout << req . size ( ) << endl ; for ( auto x : req ) cout << x << " ▁ " ; } int main ( ) { vector < int > arr = { 1 , 2 , 5 , 3 , 6 , 7 , 4 } ; int n = arr . size ( ) ; getSubsequence ( arr , n ) ; return 0 ; }
Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | C ++ implementation to find the minimum number of changes required such that every path from top left to the bottom right are palindromic paths ; Function to find the minimum number of the changes required for the every path to be palindromic ; count variable for maintaining total changes . ; left and right variables for keeping distance values from cell ( 0 , 0 ) and ( N - 1 , M - 1 ) respectively . ; Iterating over the matrix ; Finding minimum number of changes required . ; Minimum no . of changes will be the the minimum no . of different values and we will assume to make them equals to value with maximum frequency element ; Moving ahead with greater distance ; Drive Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define M  3 NEW_LINE #define N  3 NEW_LINE int minchanges ( int mat [ N ] [ M ] ) { int count = 0 ; int left = 0 , right = N + M - 2 ; while ( left < right ) { unordered_map < int , int > mp ; int totalsize = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( i + j == left ) { mp [ mat [ i ] [ j ] ] ++ ; totalsize ++ ; } else if ( i + j == right ) { mp [ mat [ i ] [ j ] ] ++ ; totalsize ++ ; } } } unordered_map < int , int > :: iterator itr = mp . begin ( ) ; int changes = 0 ; for ( ; itr != mp . end ( ) ; itr ++ ) changes = max ( changes , itr -> second ) ; count += totalsize - changes ; left ++ ; right -- ; } return count ; } int main ( ) { int mat [ ] [ M ] = { { 1 , 4 , 1 } , { 2 , 5 , 3 } , { 1 , 3 , 1 } } ; cout << minchanges ( mat ) ; return 0 ; }
Count of longest possible subarrays with sum not divisible by K | C ++ program for the above problem ; Function to find the count of longest subarrays with sum not divisible by K ; Sum of all elements in an array ; If overall sum is not divisible then return 1 , as only one subarray of size n is possible ; Index of the first number not divisible by K ; Index of the last number not divisible by K ; Subarray doesn 't exist ; Sum of the window ; Calculate the sum of rest of the windows of size len ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CountLongestSubarrays ( int arr [ ] , int n , int k ) { int i , s = 0 ; for ( i = 0 ; i < n ; ++ i ) { s += arr [ i ] ; } if ( s % k ) { return 1 ; } else { int ini = 0 ; while ( ini < n && arr [ ini ] % k == 0 ) { ++ ini ; } int final = n - 1 ; while ( final >= 0 && arr [ final ] % k == 0 ) { -- final ; } int len , sum = 0 , count = 0 ; if ( ini == n ) { return -1 ; } else { len = max ( n - 1 - ini , final ) ; } for ( i = 0 ; i < len ; i ++ ) { sum += arr [ i ] ; } if ( sum % k != 0 ) { count ++ ; } for ( i = len ; i < n ; i ++ ) { sum = sum + arr [ i ] ; sum = sum - arr [ i - len ] ; if ( sum % k != 0 ) { count ++ ; } } return count ; } } int main ( ) { int arr [ ] = { 3 , 2 , 2 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; cout << CountLongestSubarrays ( arr , n , k ) ; return 0 ; }
Count of triplets in a given Array having GCD K | C ++ program to count the number of triplets in the array with GCD equal to K ; frequency array ; mul [ i ] stores the count of multiples of i ; cnt [ i ] stores the count of triplets with gcd = i ; Return nC3 ; Function to count and return the number of triplets in the array with GCD equal to K ; Store frequency of array elements ; Store the multiples of i present in the array ; Count triplets with gcd equal to any multiple of i ; Remove all triplets which have gcd equal to a multiple of i ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAXN = 1e6 + 1 ; int freq [ MAXN ] = { 0 } ; int mul [ MAXN ] = { 0 } ; int cnt [ MAXN ] = { 0 } ; int nC3 ( int n ) { if ( n < 3 ) return 0 ; return ( n * ( n - 1 ) * ( n - 2 ) ) / 6 ; } void count_triplet ( vector < int > arr , int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] ] ++ ; } for ( int i = 1 ; i <= 1000000 ; i ++ ) { for ( int j = i ; j <= 1000000 ; j += i ) { mul [ i ] += freq [ j ] ; } cnt [ i ] = nC3 ( mul [ i ] ) ; } for ( int i = 1000000 ; i >= 1 ; i -- ) { for ( int j = 2 * i ; j <= 1000000 ; j += i ) { cnt [ i ] -= cnt [ j ] ; } } cout << " Number ▁ of ▁ triplets ▁ " << " with ▁ GCD ▁ " << K ; cout << " ▁ are ▁ " << cnt [ K ] ; } int main ( ) { vector < int > arr = { 1 , 7 , 12 , 6 , 15 , 9 } ; int N = 6 , K = 3 ; count_triplet ( arr , N , K ) ; return 0 ; }
Check if a subsequence of length K with odd sum exists | C ++ program to check if a subsequence of length K with odd sum exists in the given array ; Function to check if any required subsequence exists or not ; Store count of odd and even elements in the array ; Calculate the count of odd and even elements ; If no odd elements exists or no even elements exists when K is even ; Subsequence is not possible ; Possible otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isSubseqPossible ( int arr [ ] , int N , int K ) { int i ; int odd = 0 , even = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd ++ ; else even ++ ; } if ( odd == 0 || ( even == 0 && K % 2 == 0 ) ) return false ; return true ; } int main ( ) { int arr [ ] = { 2 , 3 , 5 , 7 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 3 ; cout << ( isSubseqPossible ( arr , N , K ) ? " Yes " : " No " ) ; return 0 ; }
Path traversed using exactly M coins in K jumps | C ++ program to print the path using exactly K jumps and M coins ; Function that print the path using exactly K jumps and M coins ; If no path exists ; It decides which box to be jump ; It decides whether to jump on left side or to jump on right side ; Print the path ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void print_path ( int N , int jump , int coin ) { if ( jump > coin || jump * ( N - 1 ) < coin ) { cout << " - 1" << endl ; } else { int pos = 1 ; while ( jump > 0 ) { int tmp = min ( N - 1 , coin - ( jump - 1 ) ) ; if ( pos + tmp <= N ) { pos += tmp ; } else { pos -= tmp ; } cout << pos << " ▁ " ; coin -= tmp ; jump -= 1 ; } } } int main ( ) { int N = 5 , K = 4 , M = 12 ; print_path ( N , K , M ) ; return 0 ; }
Minimum salary hike for each employee such that no employee feels unfair | C ++ program for the above approach ; Function that print minimum hike ; Insert INF at begin and end of array ; To store hike of each employee ; for Type 1 employee ; for Type 2 employee ; for Type 3 employee ; for Type 4 employee ; Print the min hike for each employee ; Driver Code ; Given array of rating of employees ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define INF  1e9 NEW_LINE void findMinHike ( vector < int > arr , int n ) { arr . insert ( arr . begin ( ) , INF ) ; arr . push_back ( INF ) ; vector < int > hike ( n + 2 , 0 ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] >= arr [ i ] && arr [ i ] <= arr [ i + 1 ] ) { hike [ i ] = 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] < arr [ i ] && arr [ i ] <= arr [ i + 1 ] ) { hike [ i ] = hike [ i - 1 ] + 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] >= arr [ i ] && arr [ i ] > arr [ i + 1 ] ) { hike [ i ] = hike [ i + 1 ] + 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] < arr [ i ] && arr [ i ] > arr [ i + 1 ] ) { hike [ i ] = max ( hike [ i - 1 ] , hike [ i + 1 ] ) + 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { cout << hike [ i ] << " ▁ " ; } } int main ( ) { vector < int > arr = { 5 , 3 , 4 , 2 , 1 , 6 } ; findMinHike ( arr , arr . size ( ) ) ; return 0 ; }
Minimize the cost to make all the adjacent elements distinct in an Array | C ++ program to find the minimum cost required to make all adjacent elements distinct ; Function that prints minimum cost required ; Dp - table ; Base case Not increasing the first element ; Increasing the first element by 1 ; Increasing the first element by 2 ; Condition if current element is not equal to previous non - increased element ; Condition if current element is not equal to previous element after being increased by 1 ; Condition if current element is not equal to previous element after being increased by 2 ; Take the minimum from all cases ; Finding the minimum cost ; Printing the minimum cost required to make all adjacent elements distinct ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumCost ( int arr [ ] , int cost [ ] , int N ) { vector < vector < int > > dp ( N , vector < int > ( 3 ) ) ; dp [ 0 ] [ 0 ] = 0 ; dp [ 0 ] [ 1 ] = cost [ 0 ] ; dp [ 0 ] [ 2 ] = cost [ 0 ] * 2 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { int minimum = 1e6 ; if ( j + arr [ i ] != arr [ i - 1 ] ) minimum = min ( minimum , dp [ i - 1 ] [ 0 ] ) ; if ( j + arr [ i ] != arr [ i - 1 ] + 1 ) minimum = min ( minimum , dp [ i - 1 ] [ 1 ] ) ; if ( j + arr [ i ] != arr [ i - 1 ] + 2 ) minimum = min ( minimum , dp [ i - 1 ] [ 2 ] ) ; dp [ i ] [ j ] = j * cost [ i ] + minimum ; } } int ans = 1e6 ; for ( int i = 0 ; i < 3 ; i ++ ) ans = min ( ans , dp [ N - 1 ] [ i ] ) ; cout << ans << " STRNEWLINE " ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 2 , 3 , 4 } ; int cost [ ] = { 3 , 2 , 5 , 4 , 2 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minimumCost ( arr , cost , N ) ; return 0 ; }
Subarray with largest sum after excluding its maximum element | C ++ implementation to find the maximum sum subarray such by excluding the maximum element from the subarray ; Function to find the maximum sum subarray by excluding the maximum element from the array ; Loop to store all the positive elements in the map ; Loop to iterating over the map and considering as the maximum element of the current including subarray ; Make the current element maximum ; Iterate through array and apply kadane 's algorithm ; Condition if current element is greater than mx then make the element - infinity ; Store the indices in some variable ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maximumSumSubarray ( int arr [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= 0 && mp . find ( arr [ i ] ) == mp . end ( ) ) mp [ arr [ i ] ] = 1 ; } int first = 0 ; int last = 0 ; int ans = 0 ; int INF = 1e6 ; for ( auto i : mp ) { int mx = i . first ; int curr = 0 ; int curr_start ; for ( int j = 0 ; j < n ; j ++ ) { if ( curr == 0 ) curr_start = j ; int val = arr [ j ] > mx ? - INF : arr [ j ] ; curr += val ; if ( curr < 0 ) curr = 0 ; if ( curr > ans ) { ans = curr ; first = curr_start ; last = j ; } } } cout << first + 1 << " ▁ " << last + 1 ; } int main ( ) { int arr [ ] = { 5 , -2 , 10 , -1 , 4 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maximumSumSubarray ( arr , size ) ; return 0 ; }
Extended Knapsack Problem | C ++ code for the extended Knapsack Approach ; To store the dp values ; for each element given ; For each possible weight value ; For each case where the total elements are less than the constraint ; To ensure that we dont go out of the array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 100 ] [ 100 ] [ 100 ] ; int maxProfit ( int profit [ ] , int weight [ ] , int n , int max_W , int max_E ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= max_W ; j ++ ) { for ( int k = 1 ; k <= max_E ; k ++ ) { if ( j >= weight [ i - 1 ] ) { dp [ i ] [ j ] [ k ] = max ( dp [ i - 1 ] [ j ] [ k ] , dp [ i - 1 ] [ j - weight [ i - 1 ] ] [ k - 1 ] + profit [ i - 1 ] ) ; } else { dp [ i ] [ j ] [ k ] = dp [ i - 1 ] [ j ] [ k ] ; } } } } return dp [ n ] [ max_W ] [ max_E ] ; } int main ( ) { memset ( dp , 0 , sizeof ( dp ) ) ; int n = 5 ; int profit [ ] = { 2 , 7 , 1 , 5 , 3 } ; int weight [ ] = { 2 , 5 , 2 , 3 , 4 } ; int max_weight = 8 ; int max_element = 2 ; cout << maxProfit ( profit , weight , n , max_weight , max_element ) << " STRNEWLINE " ; return 0 ; }
Shortest Path with even number of Edges from Source to Destination | C ++ program for the above approach ; Adjacency List : to represent graph ; Distance Array : to store shortest distance to every node ; returns value which will represent even_x ; returns value which will represent odd_x ; converting edge ( a -> b ) to 2 different edges i . e . ( a -> b ) converts to ( 1 ) . even_a -> odd_b ( 2 ) . odd_a -> even_b since , graph is undirected , so we push them in reverse order too hence , 4 push_back operations are there . ; Function calculates shortest distance to all nodes from " source " using Dijkstra Shortest Path Algorithm and returns shortest distance to " destination " ; Priority Queue / min - heap to store and process ( distance , node ) ; pushing source node to priority queue and dist from source to source is set to 0 ; U is the node at top of the priority queue note that pq . top ( ) . first refers to the Distance and pq . top ( ) . second will refer to the Node ; exploring all neighbours of node u ; v is neighbour node of u and c is the cost / weight of edge ( u , v ) ; relaxation : checking if there is a shorter path to v via u ; updating distance of v ; returning shortest distance to " destination " ; Driver function ; n = number of Nodes , m = number of Edges ; if ans is INF : There is no even length path from source to destination else path exists and we print the shortest distance
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAXX = 10000 , INF = 1e9 ; vector < vector < pair < int , int > > > adj ( MAXX * 10 + 3 ) ; vector < int > dist ( MAXX * 10 + 3 , INF ) ; int even ( int x ) { return x * 10 + 2 ; } int odd ( int x ) { return x * 10 + 1 ; } void addEdge ( int a , int b , int cost ) { adj [ even ( a ) ] . push_back ( { odd ( b ) , cost } ) ; adj [ odd ( a ) ] . push_back ( { even ( b ) , cost } ) ; adj [ odd ( b ) ] . push_back ( { even ( a ) , cost } ) ; adj [ even ( b ) ] . push_back ( { odd ( a ) , cost } ) ; } int dijkstra ( int source , int destination ) { priority_queue < pair < int , int > , vector < pair < int , int > > , greater < pair < int , int > > > pq ; pq . push ( { 0 , even ( source ) } ) ; dist [ even ( source ) ] = 0 ; while ( ! pq . empty ( ) ) { int u = pq . top ( ) . second ; pq . pop ( ) ; for ( pair < int , int > p : adj [ u ] ) { int v = p . first ; int c = p . second ; if ( dist [ u ] + c < dist [ v ] ) { dist [ v ] = dist [ u ] + c ; pq . push ( { dist [ v ] , v } ) ; } } } return dist [ even ( destination ) ] ; } int main ( ) { int n = 5 , m = 6 ; addEdge ( 1 , 2 , 1 ) ; addEdge ( 2 , 3 , 2 ) ; addEdge ( 2 , 5 , 15 ) ; addEdge ( 3 , 5 , 1 ) ; addEdge ( 3 , 4 , 4 ) ; addEdge ( 5 , 4 , 3 ) ; int source = 1 ; int destination = n ; int ans = dijkstra ( source , destination ) ; if ( ans == INF ) cout << " - 1" << " STRNEWLINE " ; else cout << ans << " STRNEWLINE " ; return 0 ; }
Minimum insertions to make XOR of an Array equal to half of its sum | C ++ Program to make XOR of of all array elements equal to half of its sum by minimum insertions ; Function to make XOR of the array equal to half of its sum ; Calculate the sum and Xor of all the elements ; If the required condition satisfies already , return the original array ; If Xor is already zero , Insert sum ; Otherwise , insert xr and insert sum + xr ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int make_xor_half ( vector < int > & arr ) { int sum = 0 , xr = 0 ; for ( int a : arr ) { sum += a ; xr ^= a ; } if ( 2 * xr == sum ) return -1 ; if ( xr == 0 ) { arr . push_back ( sum ) ; return 1 ; } arr . push_back ( xr ) ; arr . push_back ( sum + xr ) ; return 2 ; } int main ( ) { int N = 7 ; vector < int > nums = { 3 , 4 , 7 , 1 , 2 , 5 , 6 } ; int count = make_xor_half ( nums ) ; if ( count == -1 ) cout << " - 1" << endl ; else if ( count == 1 ) cout << nums [ N ] << endl ; else cout << nums [ N ] << " ▁ " << nums [ N + 1 ] << endl ; return 0 ; }
Count number of triangles possible for the given sides range | C ++ implementation to count the number of possible triangles for the given sides ranges ; Function to count the number of possible triangles for the given sides ranges ; Iterate for every possible of x ; Range of y is [ b , c ] From this range First we will find the number of x + y greater than d ; For x + y greater than d we can choose all z from [ c , d ] Total permutation will be ; Now we will find the number of x + y in between the [ c , d ] ; [ l , r ] will be the range from total [ c , d ] x + y belongs For any r such that r = x + y We can choose z , in the range [ c , d ] only less than r , Thus total permutation be ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count_triangles ( int a , int b , int c , int d ) { int ans = 0 ; for ( int x = a ; x <= b ; ++ x ) { int num_greater_than_d = max ( d , c + x ) - max ( d , b + x - 1 ) ; ans += num_greater_than_d * ( d - c + 1 ) ; int r = min ( max ( c , c + x ) , d ) - c ; int l = min ( max ( c , b + x - 1 ) , d ) - c ; int x1 = ( r * ( r + 1 ) ) / 2 ; int x2 = ( l * ( l + 1 ) ) / 2 ; ans += x1 - x2 ; } return ans ; } int main ( ) { int a = 2 , b = 3 , c = 4 , d = 5 ; cout << count_triangles ( a , b , c , d ) << endl ; return 0 ; }
Minimum number of swaps required to make the string K periodic | C ++ code to find the minimum number of swaps required to make the string K periodic ; Mark all allowed characters as true ; Initialize the freq array to 0 ; Increase the frequency of each character ; Total number of periods of size K in the string ; Check if the current character is present in allowed and whether the current frequency is greater than all previous frequencies for this position ; update the answer by subtracting the maxfrequency from total positions if there exist extra character at the end of the string apart from the n / k characters then add 1. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minFlip ( string s , int n , int k , char a [ ] , int p ) { bool allowed [ 26 ] = { 0 } ; for ( int i = 0 ; i < p ; i ++ ) { allowed [ a [ i ] - ' a ' ] = true ; } char freq [ k ] [ 26 ] ; for ( int i = 0 ; i < k ; i ++ ) for ( int j = 0 ; j < 26 ; j ++ ) freq [ i ] [ j ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ i % k ] [ s [ i ] - ' a ' ] += 1 ; } int ans = 0 ; int totalpositions = n / k ; for ( int i = 0 ; i < k ; i ++ ) { int maxfrequency = 0 ; for ( int j = 0 ; j < 26 ; j ++ ) { if ( freq [ i ] [ j ] > maxfrequency and allowed [ j ] == true ) maxfrequency = freq [ i ] [ j ] ; } ans += ( totalpositions - maxfrequency + ( ( i % k < n % k ) ? 1 : 0 ) ) ; } cout << ans << endl ; } int main ( ) { string S = " nihsiakyt " ; int n = S . length ( ) ; int K = 3 ; char A [ 5 ] = { ' n ' , ' i ' , ' p ' , ' s ' , ' q ' } ; int p = sizeof ( A ) / sizeof ( A [ 0 ] ) ; minFlip ( S , n , K , A , p ) ; return 0 ; }