codeparrot/xlcost-text-to-code huggingface dataset:
Check for Palindrome after every character replacement Query | C ++ program to find if string becomes palindrome after every query . ; Function to check if string is Palindrome or Not ; Takes two inputs for Q queries . For every query , it prints Yes if string becomes palindrome and No if not . ; Process all queries one by one ; query 1 : i1 = 3 , i2 = 0 , ch = ' e ' query 2 : i1 = 0 , i2 = 2 , ch = ' s ' replace character at index i1 & i2 with new ' ch ' ; check string is palindrome or not ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool IsPalindrome ( string & str ) { int n = strlen ( str ) ; for ( int i = 0 ; i < n / 2 ; i ++ ) if ( str [ i ] != str [ n - 1 - i ] ) return false ; return true ; } void Query ( string & str , int Q ) { int i1 , i2 ; char ch ; for ( int q = 1 ; q <= Q ; q ++ ) { cin >> i1 >> i2 >> ch ; str [ i1 ] = str [ i2 ] = ch ; ( isPalindrome ( str ) == true ) ? cout << " YES " << endl : cout << " NO " << endl ; } } int main ( ) { char str [ ] = " geeks " ; int Q = 2 ; Query ( str , Q ) ; return 0 ; }
Generate a string which differs by only a single character from all given strings | C ++ Program to implement the above approach ; Function to check if a given string differs by a single character from all the strings in an array ; Traverse over the strings ; Stores the count of characters differing from the strings ; If differs by more than one character ; Function to find the string which only differ at one position from the all given strings of the array ; Size of the array ; Length of a string ; Replace i - th character by all possible characters ; Check if it differs by a single character from all other strings ; If desired string is obtained ; Print the answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE #define ll  long long NEW_LINE using namespace std ; bool check ( string ans , vector < string > & s , int n , int m ) { for ( int i = 1 ; i < n ; ++ i ) { int count = 0 ; for ( int j = 0 ; j < m ; ++ j ) { if ( ans [ j ] != s [ i ] [ j ] ) count ++ ; } if ( count > 1 ) return false ; } return true ; } string findString ( vector < string > & s ) { int n = s . size ( ) ; int m = s [ 0 ] . size ( ) ; string ans = s [ 0 ] ; int flag = 0 ; for ( int i = 0 ; i < m ; ++ i ) { for ( int j = 0 ; j < 26 ; ++ j ) { string x = ans ; x [ i ] = ( j + ' a ' ) ; if ( check ( x , s , n , m ) ) { ans = x ; flag = 1 ; break ; } } if ( flag == 1 ) break ; } if ( flag == 0 ) return " - 1" ; else return ans ; } int main ( ) { vector < string > s = { " geeks " , " teeds " } ; cout << findString ( s ) << endl ; }
Minimum increments of Non | C ++ program to implement the above approach ; Function to return to the minimum number of operations required to make the array non - decreasing ; Stores the count of operations ; If arr [ i ] > arr [ i + 1 ] , add arr [ i ] - arr [ i + 1 ] to the answer Otherwise , add 0 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMinOps ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { ans += max ( arr [ i ] - arr [ i + 1 ] , 0 ) ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 3 , 1 , 2 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << ( getMinOps ( arr , n ) ) ; }
Maximum difference between frequency of two elements such that element having greater frequency is also greater | C ++ program to find maximum difference between frequency of any two element such that element with greater frequency is also greater in value . ; Return the maximum difference between frequencies of any two elements such that element with greater frequency is also greater in value . ; Finding the frequency of each element . ; finding difference such that element having greater frequency is also greater in value . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxdiff ( int arr [ ] , int n ) { unordered_map < int , int > freq ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( freq [ arr [ i ] ] > freq [ arr [ j ] ] && arr [ i ] > arr [ j ] ) ans = max ( ans , freq [ arr [ i ] ] - freq [ arr [ j ] ] ) ; else if ( freq [ arr [ i ] ] < freq [ arr [ j ] ] && arr [ i ] < arr [ j ] ) ans = max ( ans , freq [ arr [ j ] ] - freq [ arr [ i ] ] ) ; } } return ans ; } int main ( ) { int arr [ ] = { 3 , 1 , 3 , 2 , 3 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxdiff ( arr , n ) << endl ; return 0 ; }
Maximum difference between frequency of two elements such that element having greater frequency is also greater | Efficient C ++ program to find maximum difference between frequency of any two elements such that element with greater frequency is also greater in value . ; Return the maximum difference between frequencies of any two elements such that element with greater frequency is also greater in value . ; Finding the frequency of each element . ; Sorting the distinct element ; Iterate through all sorted distinct elements . For each distinct element , maintaining the element with minimum frequency than that element and also finding the maximum frequency difference ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxdiff ( int arr [ ] , int n ) { unordered_map < int , int > freq ; int dist [ n ] ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . find ( arr [ i ] ) == freq . end ( ) ) dist [ j ++ ] = arr [ i ] ; freq [ arr [ i ] ] ++ ; } sort ( dist , dist + j ) ; int min_freq = n + 1 ; int ans = 0 ; for ( int i = 0 ; i < j ; i ++ ) { int cur_freq = freq [ dist [ i ] ] ; ans = max ( ans , cur_freq - min_freq ) ; min_freq = min ( min_freq , cur_freq ) ; } return ans ; } int main ( ) { int arr [ ] = { 3 , 1 , 3 , 2 , 3 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxdiff ( arr , n ) << endl ; return 0 ; }
Maximum Sum possible by selecting X elements from a Matrix based on given conditions | C ++ 14 program to implement the above approach ; Function to calculate the maximum possible sum by selecting X elements from the Matrix ; Generate prefix sum of the matrix ; Initialize [ , ] dp ; Maximum possible sum by selecting 0 elements from the first i rows ; If a single row is present ; If elements from the current row is not selected ; Iterate over all possible selections from current row ; Return maximum possible sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int n , m , X ; int maxSum ( vector < vector < int > > grid ) { vector < vector < int > > prefsum ( n , vector < int > ( m ) ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int x = 0 ; x < m ; x ++ ) { if ( x == 0 ) prefsum [ i ] [ x ] = grid [ i ] [ x ] ; else prefsum [ i ] [ x ] = prefsum [ i ] [ x - 1 ] + grid [ i ] [ x ] ; } } vector < vector < int > > dp ( n , vector < int > ( X + 1 , INT_MIN ) ) ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( int i = 1 ; i <= min ( m , X ) ; ++ i ) { dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + grid [ 0 ] [ i - 1 ] ; } for ( int i = 1 ; i < n ; ++ i ) { for ( int j = 1 ; j <= X ; ++ j ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; for ( int x = 1 ; x <= min ( j , m ) ; x ++ ) { dp [ i ] [ j ] = max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - x ] + prefsum [ i ] [ x - 1 ] ) ; } } } return dp [ n - 1 ] [ X ] ; } int main ( ) { n = 4 ; m = 4 ; X = 6 ; vector < vector < int > > grid = { { 3 , 2 , 6 , 1 } , { 1 , 9 , 2 , 4 } , { 4 , 1 , 3 , 9 } , { 3 , 8 , 2 , 1 } } ; int ans = maxSum ( grid ) ; cout << ( ans ) ; return 0 ; }
Difference between highest and least frequencies in an array | CPP code to find the difference between highest and least frequencies ; sort the array ; checking consecutive elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findDiff ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int count = 0 , max_count = 0 , min_count = n ; for ( int i = 0 ; i < ( n - 1 ) ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { count += 1 ; continue ; } else { max_count = max ( max_count , count ) ; min_count = min ( min_count , count ) ; count = 0 ; } } return ( max_count - min_count ) ; } int main ( ) { int arr [ ] = { 7 , 8 , 4 , 5 , 4 , 1 , 1 , 7 , 7 , 2 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findDiff ( arr , n ) << " STRNEWLINE " ; return 0 ; }
Minimum Decrements on Subarrays required to reduce all Array elements to zero | C ++ Program to implement the above approach ; Function to count the minimum number of subarrays that are required to be decremented by 1 ; Base Case ; Initialize ans to first element ; For A [ i ] > A [ i - 1 ] , operation ( A [ i ] - A [ i - 1 ] ) is required ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int min_operations ( vector < int > & A ) { if ( A . size ( ) == 0 ) return 0 ; int ans = A [ 0 ] ; for ( int i = 1 ; i < A . size ( ) ; i ++ ) { ans += max ( A [ i ] - A [ i - 1 ] , 0 ) ; } return ans ; } int main ( ) { vector < int > A { 1 , 2 , 3 , 2 , 1 } ; cout << min_operations ( A ) << " STRNEWLINE " ; return 0 ; }
Difference between highest and least frequencies in an array | CPP code to find the difference between highest and least frequencies ; Put all elements in a hash map ; Find counts of maximum and minimum frequent elements ; Driver
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findDiff ( int arr [ ] , int n ) { unordered_map < int , int > hm ; for ( int i = 0 ; i < n ; i ++ ) hm [ arr [ i ] ] ++ ; int max_count = 0 , min_count = n ; for ( auto x : hm ) { max_count = max ( max_count , x . second ) ; min_count = min ( min_count , x . second ) ; } return ( max_count - min_count ) ; } int main ( ) { int arr [ ] = { 7 , 8 , 4 , 5 , 4 , 1 , 1 , 7 , 7 , 2 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findDiff ( arr , n ) << " STRNEWLINE " ; return 0 ; }
Iterative function to check if two trees are identical | Iterative C ++ program to check if two ; A Binary Tree Node ; Iterative method to find height of Binary Tree ; Return true if both trees are empty ; Return false if one is empty and other is not ; Create an empty queues for simultaneous traversals ; Enqueue Roots of trees in respective queues ; Get front nodes and compare them ; Remove front nodes from queues ; Enqueue left children of both nodes ; If one left child is empty and other is not ; Right child code ( Similar to left child code ) ; Utility function to create a new tree node ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; bool areIdentical ( Node * root1 , Node * root2 ) { if ( root1 == NULL && root2 == NULL ) return true ; if ( root1 == NULL ) return false ; if ( root2 == NULL ) return false ; queue < Node * > q1 , q2 ; q1 . push ( root1 ) ; q2 . push ( root2 ) ; while ( ! q1 . empty ( ) && ! q2 . empty ( ) ) { Node * n1 = q1 . front ( ) ; Node * n2 = q2 . front ( ) ; if ( n1 -> data != n2 -> data ) return false ; q1 . pop ( ) , q2 . pop ( ) ; if ( n1 -> left && n2 -> left ) { q1 . push ( n1 -> left ) ; q2 . push ( n2 -> left ) ; } else if ( n1 -> left n2 -> left ) return false ; if ( n1 -> right && n2 -> right ) { q1 . push ( n1 -> right ) ; q2 . push ( n2 -> right ) ; } else if ( n1 -> right n2 -> right ) return false ; } return true ; } Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int main ( ) { Node * root1 = newNode ( 1 ) ; root1 -> left = newNode ( 2 ) ; root1 -> right = newNode ( 3 ) ; root1 -> left -> left = newNode ( 4 ) ; root1 -> left -> right = newNode ( 5 ) ; Node * root2 = newNode ( 1 ) ; root2 -> left = newNode ( 2 ) ; root2 -> right = newNode ( 3 ) ; root2 -> left -> left = newNode ( 4 ) ; root2 -> left -> right = newNode ( 5 ) ; areIdentical ( root1 , root2 ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Queries to check if vertices X and Y are in the same Connected Component of an Undirected Graph | C ++ Program to implement the above approach ; Maximum number of nodes or vertices that can be present in the graph ; Store the parent of each vertex ; Stores the size of each set ; Function to initialize the parent of each vertices ; Function to find the representative of the set which contain element v ; Path compression technique to optimize the time complexity ; Function to merge two different set into a single set by finding the representative of each set and merge the smallest set with the larger one ; Finding the set representative of each element ; Check if they have different set repersentative ; Compare the set sizes ; Assign parent of smaller set to the larger one ; Add the size of smaller set to the larger one ; Function to check the vertices are on the same set or not ; Check if they have same set representative or not ; Driver Code ; Connected vertices and taking them into single set ; Number of queries ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX_NODES  100005 NEW_LINE int parent [ MAX_NODES ] ; int size_set [ MAX_NODES ] ; void make_set ( int v ) { parent [ v ] = v ; size_set [ v ] = 1 ; } int find_set ( int v ) { if ( v == parent [ v ] ) return v ; return parent [ v ] = find_set ( parent [ v ] ) ; } void union_set ( int a , int b ) { a = find_set ( a ) ; b = find_set ( b ) ; if ( a != b ) { if ( size_set [ a ] < size_set [ b ] ) swap ( a , b ) ; parent [ b ] = a ; size_set [ a ] += size_set [ b ] ; } } string check ( int a , int b ) { a = find_set ( a ) ; b = find_set ( b ) ; return ( a == b ) ? " Yes " : " No " ; } int main ( ) { int n = 5 , m = 3 ; make_set ( 1 ) ; make_set ( 2 ) ; make_set ( 3 ) ; make_set ( 4 ) ; make_set ( 5 ) ; union_set ( 1 , 3 ) ; union_set ( 3 , 4 ) ; union_set ( 3 , 5 ) ; int q = 3 ; cout << check ( 1 , 5 ) << endl ; cout << check ( 3 , 2 ) << endl ; cout << check ( 5 , 2 ) << endl ; return 0 ; }
Count of N | C ++ Program to implement the above approach ; Function to calculate the total count of N - digit numbers such that the sum of digits at even positions and odd positions are divisible by A and B respectively ; For single digit numbers ; Largest possible number ; Count of possible odd digits ; Count of possible even digits ; Calculate total count of sequences of length even_count with sum divisible by A where first digit can be zero ; Calculate total count of sequences of length odd_count with sum divisible by B where cannot be zero ; Return their product as answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long count ( int N , int A , int B ) { if ( N == 1 ) { return 9 / B + 1 ; } int max_sum = 9 * N ; int odd_count = N / 2 + N % 2 ; int even_count = N - odd_count ; long dp [ even_count ] [ max_sum + 1 ] = { 0 } ; for ( int i = 0 ; i <= 9 ; i ++ ) dp [ 0 ] [ i % A ] ++ ; for ( int i = 1 ; i < even_count ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { for ( int k = 0 ; k <= max_sum ; k ++ ) { if ( dp [ i - 1 ] [ k ] > 0 ) dp [ i ] [ ( j + k ) % A ] += dp [ i - 1 ] [ k ] ; } } } long dp1 [ odd_count ] [ max_sum + 1 ] = { 0 } ; for ( int i = 1 ; i <= 9 ; i ++ ) dp1 [ 0 ] [ i % B ] ++ ; for ( int i = 1 ; i < odd_count ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { for ( int k = 0 ; k <= max_sum ; k ++ ) { if ( dp1 [ i - 1 ] [ k ] > 0 ) dp1 [ i ] [ ( j + k ) % B ] += dp1 [ i - 1 ] [ k ] ; } } } return dp [ even_count - 1 ] [ 0 ] * dp1 [ odd_count - 1 ] [ 0 ] ; } int main ( ) { int N = 2 , A = 2 , B = 5 ; cout << count ( N , A , B ) ; }
Maximum possible difference of two subsets of an array | CPP find maximum difference of subset sum ; function for maximum subset diff ; if frequency of any element is two make both equal to zero ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxDiff ( int arr [ ] , int n ) { int SubsetSum_1 = 0 , SubsetSum_2 = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { bool isSingleOccurance = true ; for ( int j = i + 1 ; j <= n - 1 ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { isSingleOccurance = false ; arr [ i ] = arr [ j ] = 0 ; break ; } } if ( isSingleOccurance ) { if ( arr [ i ] > 0 ) SubsetSum_1 += arr [ i ] ; else SubsetSum_2 += arr [ i ] ; } } return abs ( SubsetSum_1 - SubsetSum_2 ) ; } int main ( ) { int arr [ ] = { 4 , 2 , -3 , 3 , -2 , -2 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Maximum ▁ Difference ▁ = ▁ " << maxDiff ( arr , n ) ; return 0 ; }
Count of all possible Paths in a Tree such that Node X does not appear before Node Y | C ++ Program to implement the above approach ; Maximum number of nodes ; Vector to store the tree ; Function to perform DFS Traversal ; Mark the node as visited ; Initialize the subtree size of each node as 1 ; If the node is same as A ; Mark check_subtree [ node ] as true ; Otherwise ; Iterate over the adjacent nodes ; If the adjacent node is not visited ; Update the size of the subtree of current node ; Check if the subtree of current node contains node A ; Return size of subtree of node ; Function to add edges to the tree ; Function to calculate the number of possible paths ; Stores the size of subtree of each node ; Stores which nodes are visited ; Stores if the subtree of a node contains node A ; DFS Call ; Stores the difference between total number of nodes and subtree size of an immediate child of Y lies between the path from A to B ; Iterate over the adjacent nodes B ; If the node is in the path from A to B ; Calculate the difference ; Return the final answer ; Driver Code ; Insert Edges
#include <bits/stdc++.h> NEW_LINE #define int  long long int NEW_LINE using namespace std ; const int NN = 3e5 ; vector < int > G [ NN + 1 ] ; int dfs ( int node , int A , int * subtree_size , int * visited , int * check_subtree ) { visited [ node ] = true ; subtree_size [ node ] = 1 ; if ( node == A ) { check_subtree [ node ] = true ; } else check_subtree [ node ] = false ; for ( int v : G [ node ] ) { if ( ! visited [ v ] ) { subtree_size [ node ] += dfs ( v , A , subtree_size , visited , check_subtree ) ; check_subtree [ node ] = check_subtree [ node ] | check_subtree [ v ] ; } } return subtree_size [ node ] ; } void addedge ( int node1 , int node2 ) { G [ node1 ] . push_back ( node2 ) ; G [ node2 ] . push_back ( node1 ) ; } int numberOfPairs ( int N , int B , int A ) { int subtree_size [ N + 1 ] ; int visited [ N + 1 ] ; memset ( visited , 0 , sizeof ( visited ) ) ; int check_subtree [ N + 1 ] ; dfs ( B , A , subtree_size , visited , check_subtree ) ; int difference ; for ( int v : G [ B ] ) { if ( check_subtree [ v ] ) { difference = N - subtree_size [ v ] ; break ; } } return ( N * ( N - 1 ) ) - difference * ( subtree_size [ A ] ) ; } int32_t main ( ) { int N = 9 ; int X = 5 , Y = 3 ; addedge ( 0 , 2 ) ; addedge ( 1 , 2 ) ; addedge ( 2 , 3 ) ; addedge ( 3 , 4 ) ; addedge ( 4 , 6 ) ; addedge ( 4 , 5 ) ; addedge ( 5 , 7 ) ; addedge ( 5 , 8 ) ; cout << numberOfPairs ( N , Y , X ) ; return 0 ; }
Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them | C ++ Program to implement the above approach ; Function to find required number M ; Initialising m ; Finding the index of the most significant bit of N ; Calculating required number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int equalXORandOR ( int n ) { int m = 0 ; int MSB = ( int ) log2 ( n ) ; for ( int i = 0 ; i <= MSB ; i ++ ) { if ( ! ( n & ( 1 << i ) ) ) { m += ( 1 << i ) ; } } return m ; } int main ( ) { int n = 14 ; cout << equalXORandOR ( n ) ; return 0 ; }
Maximum possible difference of two subsets of an array | CPP find maximum difference of subset sum ; function for maximum subset diff ; sort the array ; calculate the result ; check for last element ; return result ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxDiff ( int arr [ ] , int n ) { int result = 0 ; sort ( arr , arr + n ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) result += abs ( arr [ i ] ) ; else i ++ ; } if ( arr [ n - 2 ] != arr [ n - 1 ] ) result += abs ( arr [ n - 1 ] ) ; return result ; } int main ( ) { int arr [ ] = { 4 , 2 , -3 , 3 , -2 , -2 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Maximum ▁ Difference ▁ = ▁ " << maxDiff ( arr , n ) ; return 0 ; }
Count of Palindromic Strings possible by swapping of a pair of Characters | C ++ Program to implement the above approach ; Function to return the count of possible palindromic strings ; Stores the frequencies of each character ; Stores the length of the string ; Increase the number of swaps , the current character make with its previous occurrences ; Increase frequency ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long findNewString ( string s ) { long long ans = 0 ; int freq [ 26 ] ; int n = s . length ( ) ; memset ( freq , 0 , sizeof freq ) ; for ( int i = 0 ; i < ( int ) s . length ( ) ; ++ i ) { ans += freq [ s [ i ] - ' a ' ] ; freq [ s [ i ] - ' a ' ] ++ ; } return ans ; } int main ( ) { string s = " aaabaaa " ; cout << findNewString ( s ) << ' ' ; return 0 ; }
Maximum possible difference of two subsets of an array | CPP find maximum difference of subset sum ; function for maximum subset diff ; construct hash for positive elements ; calculate subset sum for positive elements ; construct hash for negative elements ; calculate subset sum for negative elements ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxDiff ( int arr [ ] , int n ) { unordered_map < int , int > hashPositive ; unordered_map < int , int > hashNegative ; int SubsetSum_1 = 0 , SubsetSum_2 = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) if ( arr [ i ] > 0 ) hashPositive [ arr [ i ] ] ++ ; for ( int i = 0 ; i <= n - 1 ; i ++ ) if ( arr [ i ] > 0 && hashPositive [ arr [ i ] ] == 1 ) SubsetSum_1 += arr [ i ] ; for ( int i = 0 ; i <= n - 1 ; i ++ ) if ( arr [ i ] < 0 ) hashNegative [ abs ( arr [ i ] ) ] ++ ; for ( int i = 0 ; i <= n - 1 ; i ++ ) if ( arr [ i ] < 0 && hashNegative [ abs ( arr [ i ] ) ] == 1 ) SubsetSum_2 += arr [ i ] ; return abs ( SubsetSum_1 - SubsetSum_2 ) ; } int main ( ) { int arr [ ] = { 4 , 2 , -3 , 3 , -2 , -2 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Maximum ▁ Difference ▁ = ▁ " << maxDiff ( arr , n ) ; return 0 ; }
Change the given string according to the given conditions | C ++ program for the above approach ; Function to proofread the spells ; Loop to iterate over the characters of the string ; Push the current character c in the stack ; Check for Rule 1 ; Check for Rule 2 ; To store the resultant string ; Loop to iterate over the characters of stack ; Return the resultant string ; Driver Code ; Given string str ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string proofreadSpell ( string & str ) { vector < char > result ; for ( char c : str ) { result . push_back ( c ) ; int n = result . size ( ) ; if ( n >= 3 ) { if ( result [ n - 1 ] == result [ n - 2 ] && result [ n - 1 ] == result [ n - 3 ] ) { result . pop_back ( ) ; } } n = result . size ( ) ; if ( n >= 4 ) { if ( result [ n - 1 ] == result [ n - 2 ] && result [ n - 3 ] == result [ n - 4 ] ) { result . pop_back ( ) ; } } } string resultStr = " " ; for ( char c : result ) { resultStr += c ; } return resultStr ; } int main ( ) { string str = " hello " ; cout << proofreadSpell ( str ) ; }