codeparrot/xlcost-text-to-code huggingface dataset:
Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree | C ++ program of the above approach ; Stores the nodes to be deleted ; Structure of a Tree node ; Function to create a new node ; Function to find the min value of node for each level ; Count is used to diffentiate each level of the tree ; Function to check whether the nodes in the level below it are smaller by performing post order traversal ; Traverse the left subtree ; Traverse right subtree ; Check from minimum values computed at each level ; Function to print count of nodes from all lower levels having values less than the the nodes in the current level ; Stores the number of levels ; Stores the required count of nodes for each level ; Driver Code ; 4 / \ 3 5 / \ / \ 10 2 3 1
#include <bits/stdc++.h> NEW_LINE using namespace std ; unordered_map < int , bool > mp ; struct Node { int key ; struct Node * left , * right ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return ( temp ) ; } void calculateMin ( Node * root , vector < int > & levelMin ) { queue < Node * > qt ; qt . push ( root ) ; int count = 1 ; int min_v = INT_MAX ; while ( ! qt . empty ( ) ) { Node * temp = qt . front ( ) ; min_v = min ( min_v , temp -> key ) ; qt . pop ( ) ; if ( temp -> left ) { qt . push ( temp -> left ) ; } if ( temp -> right ) { qt . push ( temp -> right ) ; } count -- ; if ( count == 0 ) { levelMin . push_back ( min_v ) ; min_v = INT_MAX ; count = qt . size ( ) ; } } } void findNodes ( Node * root , vector < int > & levelMin , vector < int > & levelResult , int level ) { if ( root == NULL ) return ; findNodes ( root -> left , levelMin , levelResult , level + 1 ) ; findNodes ( root -> right , levelMin , levelResult , level + 1 ) ; for ( int i = 0 ; i < level ; i ++ ) { if ( root -> key <= levelMin [ i ] ) { levelResult [ i ] += 1 ; } } } void printNodes ( Node * root ) { vector < int > levelMin ; calculateMin ( root , levelMin ) ; int numLevels = levelMin . size ( ) ; vector < int > levelResult ( numLevels , 0 ) ; findNodes ( root , levelMin , levelResult , 0 ) ; for ( int i = 0 ; i < numLevels ; i ++ ) { cout << levelResult [ i ] << " ▁ " ; } } int main ( ) { Node * root = newNode ( 4 ) ; root -> left = newNode ( 3 ) ; root -> right = newNode ( 5 ) ; root -> right -> left = newNode ( 3 ) ; root -> right -> right = newNode ( 1 ) ; root -> left -> left = newNode ( 10 ) ; root -> left -> right = newNode ( 2 ) ; printNodes ( root ) ; }
Design a data structure that supports insert , delete , search and getRandom in constant time | C ++ program to design a DS that supports following operations in Theta ( n ) timea ) Insertb ) Deletec ) Searchd ) getRandom ; class to represent the required data structure ; A resizable array ; A hash where keys are array elements and values are indexes in arr [ ] ; A Theta ( 1 ) function to add an element to MyDS data structure ; If element is already present , then nothing to do ; Else put element at the end of arr [ ] ; and hashmap also ; function to remove a number to DS in O ( 1 ) ; element not found then return ; remove element from map ; swap with last element in arr ; then remove element at back ; Update hash table for new index of last element ; Returns a random element from myStructure ; Find a random index from 0 to size - 1 ; Return element at randomly picked index ; Returns index of element if element is present , otherwise null ; Driver main
#include <bits/stdc++.h> NEW_LINE using namespace std ; class myStructure { vector < int > arr ; map < int , int > Map ; public : void add ( int x ) { if ( Map . find ( x ) != Map . end ( ) ) return ; int index = arr . size ( ) ; arr . push_back ( x ) ; Map . insert ( std :: pair < int , int > ( x , index ) ) ; } void remove ( int x ) { if ( Map . find ( x ) == Map . end ( ) ) return ; int index = Map . at ( x ) ; Map . erase ( x ) ; int last = arr . size ( ) - 1 ; swap ( arr [ index ] , arr [ last ] ) ; arr . pop_back ( ) ; Map . at ( arr [ index ] ) = index ; } int getRandom ( ) { srand ( time ( NULL ) ) ; int random_index = rand ( ) % arr . size ( ) ; return arr . at ( random_index ) ; } int search ( int x ) { if ( Map . find ( x ) != Map . end ( ) ) return Map . at ( x ) ; return -1 ; } } ; int main ( ) { myStructure ds ; ds . add ( 10 ) ; ds . add ( 20 ) ; ds . add ( 30 ) ; ds . add ( 40 ) ; cout << ds . search ( 30 ) << endl ; ds . remove ( 20 ) ; ds . add ( 50 ) ; cout << ds . search ( 50 ) << endl ; cout << ds . getRandom ( ) << endl ; }
Count subarrays which contains both the maximum and minimum array element | C ++ program for the above approach ; Function to count subarray containing both maximum and minimum array elements ; If the length of the array is less than 2 ; Find the index of maximum element ; Find the index of minimum element ; If i > j , then swap the value of i and j ; Return the answer ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubArray ( int arr [ ] , int n ) { if ( n < 2 ) return n ; int i = max_element ( arr , arr + n ) - arr ; int j = min_element ( arr , arr + n ) - arr ; if ( i > j ) swap ( i , j ) ; return ( i + 1 ) * ( n - j ) ; } int main ( ) { int arr [ ] = { 4 , 1 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countSubArray ( arr , n ) ; return 0 ; }
Count pairs of leaf nodes in a Binary Tree which are at most K distance apart | C ++ 14 implementation of the above approach ; Structure of a Node ; Constructor of the class ; Stores the count of required pairs ; Function to perform dfs to find pair of leaf nodes at most K distance apart ; Return empty array if node is NULL ; If node is a leaf node and return res ; Traverse to the left ; Traverse to the right ; Update the distance between left and right leaf node ; Count all pair of leaf nodes which are at most K distance apart ; Return res to parent node ; Driver Code ; 1 / \ 2 3 / 4 ; Given distance K ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; Node ( int item ) { data = item ; left = right = NULL ; } } ; int result = 0 ; vector < int > dfs ( Node * root , int distance ) { if ( root == NULL ) { vector < int > res ( distance + 1 , 0 ) ; return res ; } if ( root -> left == NULL && root -> right == NULL ) { vector < int > res ( distance + 1 , 0 ) ; res [ 1 ] ++ ; return res ; } vector < int > left = dfs ( root -> left , distance ) ; vector < int > right = dfs ( root -> right , distance ) ; vector < int > res ( distance + 1 , 0 ) ; for ( int i = res . size ( ) - 2 ; i >= 1 ; i -- ) res [ i + 1 ] = left [ i ] + right [ i ] ; for ( int l = 1 ; l < left . size ( ) ; l ++ ) { for ( int r = 0 ; r < right . size ( ) ; r ++ ) { if ( l + r <= distance ) { result += left [ l ] * right [ r ] ; } } } return res ; } int main ( ) { Node * root = new Node ( 1 ) ; root -> left = new Node ( 2 ) ; root -> right = new Node ( 3 ) ; root -> left -> left = new Node ( 4 ) ; int K = 3 ; dfs ( root , K ) ; cout << result ; }
Count number of triangles possible with length of sides not exceeding N | C ++ implementation of the above approach ; Function to count total number of right angled triangle ; Consider a set to store the three sides ; Find possible third side ; Condition for a right angled triangle ; Check if the third side is an integer ; Push the three sides ; Insert the three sides in the set to find unique triangles ; return the size of set ; Driver code ; Given N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int right_angled ( int n ) { set < pair < int , pair < int , int > > > s ; for ( int x = 1 ; x <= n ; x ++ ) { for ( int y = 1 ; y <= n ; y ++ ) { if ( x * x + y * y <= n * n ) { int z = sqrt ( x * x + y * y ) ; if ( z * z != ( x * x + y * y ) ) continue ; vector < int > v ; v . push_back ( x ) ; v . push_back ( y ) ; v . push_back ( sqrt ( x * x + y * y ) ) ; sort ( v . begin ( ) , v . end ( ) ) ; s . insert ( { v [ 0 ] , { v [ 1 ] , v [ 2 ] } } ) ; } else break ; } } return s . size ( ) ; } int main ( ) { int n = 5 ; cout << right_angled ( n ) ; return 0 ; }
Maximum absolute difference between any two level sum in a N | C ++ program for the above approach ; Function to find the maximum absolute difference of level sum ; Create the adjacency list ; Initialize value of maximum and minimum level sum ; Do Level order traversal keeping track of nodes at every level ; Get the size of queue when the level order traversal for one level finishes ; Iterate for all the nodes in the queue currently ; Dequeue an node from queue ; Enqueue the children of dequeued node ; Update the maximum level sum value ; Update the minimum level sum value ; Return the result ; Driver Code ; Number of nodes and edges ; Edges of the N - ary tree ; Given cost ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxAbsDiffLevelSum ( int N , int M , vector < int > cost , int Edges [ ] [ 2 ] ) { vector < int > adj [ N ] ; for ( int i = 0 ; i < M ; i ++ ) { int u = Edges [ i ] [ 0 ] ; int v = Edges [ i ] [ 1 ] ; adj [ u ] . push_back ( v ) ; } int maxSum = cost [ 0 ] , minSum = cost [ 0 ] ; queue < int > q ; q . push ( 0 ) ; while ( ! q . empty ( ) ) { int count = q . size ( ) ; int sum = 0 ; while ( count -- ) { int temp = q . front ( ) ; q . pop ( ) ; sum = sum + cost [ temp ] ; for ( int i = 0 ; i < adj [ temp ] . size ( ) ; i ++ ) { q . push ( adj [ temp ] [ i ] ) ; } } maxSum = max ( sum , maxSum ) ; minSum = min ( sum , minSum ) ; } cout << abs ( maxSum - minSum ) ; } int main ( ) { int N = 10 , M = 9 ; int Edges [ ] [ 2 ] = { { 0 , 1 } , { 0 , 2 } , { 0 , 3 } , { 1 , 4 } , { 1 , 5 } , { 3 , 6 } , { 6 , 7 } , { 6 , 8 } , { 6 , 9 } } ; vector < int > cost = { 1 , 2 , -1 , 3 , 4 , 5 , 8 , 6 , 12 , 7 } ; maxAbsDiffLevelSum ( N , M , cost , Edges ) ; return 0 ; }
Split array into subarrays at minimum cost by minimizing count of repeating elements in each subarray | C ++ program for the above approach ; Function to find the minimum cost of splitting the array into subarrays ; Size of the array ; Get the maximum element ; dp [ ] will store the minimum cost upto index i ; Initialize the result array ; Initialise the first element ; Create the frequency array ; Update the frequency ; Counting the cost of the duplicate element ; Minimum cost of operation from 0 to j ; Total cost of the array ; Driver Code ; Given cost K ; Function Call
#include <bits/stdc++.h> NEW_LINE #define ll  long long NEW_LINE using namespace std ; int findMinCost ( vector < int > & a , int k ) { int n = ( int ) a . size ( ) ; int max_ele = * max_element ( a . begin ( ) , a . end ( ) ) ; ll dp [ n + 1 ] ; for ( int i = 1 ; i <= n ; ++ i ) dp [ i ] = INT_MAX ; dp [ 0 ] = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int freq [ max_ele + 1 ] ; memset ( freq , 0 , sizeof freq ) ; for ( int j = i ; j < n ; ++ j ) { freq [ a [ j ] ] ++ ; int cost = 0 ; for ( int x = 0 ; x <= max_ele ; ++ x ) { cost += ( freq [ x ] == 1 ) ? 0 : freq [ x ] ; } dp [ j + 1 ] = min ( dp [ i ] + cost + k , dp [ j + 1 ] ) ; } } return dp [ n ] ; } int main ( ) { vector < int > arr = { 1 , 2 , 1 , 1 , 1 } ; int K = 2 ; cout << findMinCost ( arr , K ) ; return 0 ; }
Find subarray with given sum | Set 2 ( Handles Negative Numbers ) | C ++ program to print subarray with sum as given sum ; Function to print subarray with sum as given sum ; create an empty map ; if curr_sum is equal to target sum we found a subarray starting from index 0 and ending at index i ; If curr_sum - sum already exists in map we have found a subarray with target sum ; if value is not present then add to hashmap ; If we reach here , then no subarray exists ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void subArraySum ( int arr [ ] , int n , int sum ) { unordered_map < int , int > map ; int curr_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { curr_sum = curr_sum + arr [ i ] ; if ( curr_sum == sum ) { cout << " Sum ▁ found ▁ between ▁ indexes ▁ " << 0 << " ▁ to ▁ " << i << endl ; return ; } if ( map . find ( curr_sum - sum ) != map . end ( ) ) { cout << " Sum ▁ found ▁ between ▁ indexes ▁ " << map [ curr_sum - sum ] + 1 << " ▁ to ▁ " << i << endl ; return ; } map [ curr_sum ] = i ; } cout << " No ▁ subarray ▁ with ▁ given ▁ sum ▁ exists " ; } int main ( ) { int arr [ ] = { 10 , 2 , -2 , -20 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int sum = -10 ; subArraySum ( arr , n , sum ) ; return 0 ; }
Write Code to Determine if Two Trees are Identical | C ++ program to see if two trees are identical ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Given two trees , return true if they are structurally identical ; 1. both empty ; 2. both non - empty -> compare them ; 3. one empty , one not -> false ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : int data ; node * left ; node * right ; } ; node * newNode ( int data ) { node * Node = new node ( ) ; Node -> data = data ; Node -> left = NULL ; Node -> right = NULL ; return ( Node ) ; } int identicalTrees ( node * a , node * b ) { if ( a == NULL && b == NULL ) return 1 ; if ( a != NULL && b != NULL ) { return ( a -> data == b -> data && identicalTrees ( a -> left , b -> left ) && identicalTrees ( a -> right , b -> right ) ) ; } return 0 ; } int main ( ) { node * root1 = newNode ( 1 ) ; node * root2 = newNode ( 1 ) ; root1 -> left = newNode ( 2 ) ; root1 -> right = newNode ( 3 ) ; root1 -> left -> left = newNode ( 4 ) ; root1 -> left -> right = newNode ( 5 ) ; root2 -> left = newNode ( 2 ) ; root2 -> right = newNode ( 3 ) ; root2 -> left -> left = newNode ( 4 ) ; root2 -> left -> right = newNode ( 5 ) ; if ( identicalTrees ( root1 , root2 ) ) cout << " Both ▁ tree ▁ are ▁ identical . " ; else cout << " Trees ▁ are ▁ not ▁ identical . " ; return 0 ; }
Queries to find Kth greatest character in a range [ L , R ] from a string with updates | C ++ Program to implement the above approach ; Maximum Size of a String ; Fenwick Tree to store the frequencies of 26 alphabets ; Size of the String . ; Function to update Fenwick Tree for Character c at index val ; Add val to current node Fenwick Tree ; Move index to parent node in update View ; Function to get sum of frequencies of character c till index ; Stores the sum ; Add current element of Fenwick tree to sum ; Move index to parent node in getSum View ; Function to create the Fenwick tree ; Function to print the kth largest character in the range of l to r ; Stores the count of characters ; Stores the required character ; Calculate frequency of C in the given range ; Increase count ; If count exceeds K ; Required character found ; Function to update character at pos by character s ; 0 based index system ; Driver Code ; Makes the string 1 - based indexed ; Number of queries ; Construct the Fenwick Tree
#include " bits / stdc + + . h " NEW_LINE using namespace std ; const int maxn = 100005 ; int BITree [ 26 ] [ maxn ] ; int N ; void update_BITree ( int index , char C , int val ) { while ( index <= N ) { BITree [ C - ' a ' ] [ index ] += val ; index += ( index & - index ) ; } } int sum_BITree ( int index , char C ) { int s = 0 ; while ( index ) { s += BITree [ C - ' a ' ] [ index ] ; index -= ( index & - index ) ; } return s ; } void buildTree ( string str ) { for ( int i = 1 ; i <= N ; i ++ ) { update_BITree ( i , str [ i ] , 1 ) ; } cout << endl ; } char printCharacter ( string str , int l , int r , int k ) { int count = 0 ; char ans ; for ( char C = ' z ' ; C >= ' a ' ; C -- ) { int times = sum_BITree ( r , C ) - sum_BITree ( l - 1 , C ) ; count += times ; if ( count >= k ) { ans = C ; break ; } } return ans ; } void updateTree ( string str , int pos , char s ) { int index = pos ; update_BITree ( index , str [ index ] , -1 ) ; str [ index ] = s ; update_BITree ( index , s , 1 ) ; } int main ( ) { string str = " abcddef " ; N = str . size ( ) ; str = ' # ' + str ; int Q = 3 ; buildTree ( str ) ; cout << printCharacter ( str , 1 , 2 , 2 ) << endl ; updateTree ( str , 4 , ' g ' ) ; cout << printCharacter ( str , 1 , 5 , 4 ) << endl ; return 0 ; }
Generate a string from an array of alphanumeric strings based on given conditions | C ++ program for the above approach ; Function to generate required string ; To store the result ; Split name and number ; Stores the maximum number less than or equal to the length of name ; Check for number by parsing it to integer if it is greater than max number so far ; Check if no such number is found then we append X to the result . ; Otherwise ; Append max index of the name ; Return the final string ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string generatePassword ( vector < string > arr , char T ) { string result ; for ( auto s : arr ) { int index ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] == ' : ' ) { index = i ; break ; } } string name = s . substr ( 0 , index ) ; string number = s . substr ( index + 1 , s . size ( ) - index - 1 ) ; int n = name . length ( ) ; int max = 0 ; for ( int i = 0 ; i < number . length ( ) ; i ++ ) { int temp = number [ i ] - '0' ; if ( temp > max && temp <= n ) max = temp ; } if ( max == 0 ) result . push_back ( T ) ; else result . push_back ( name [ max - 1 ] ) ; } return result ; } int main ( ) { vector < string > arr = { " Geeks : 89167" , " gfg : 68795" } ; char T = ' X ' ; cout << ( generatePassword ( arr , T ) ) ; }
Minimum number of edges required to be removed from an Undirected Graph to make it acyclic | C ++ Program to implement the above approach ; Stores the adjacency list ; Stores if a vertex is visited or not ; Function to perform DFS Traversal to count the number and size of all connected components ; Mark the current node as visited ; Traverse the adjacency list of the current node ; For every unvisited node ; Recursive DFS Call ; Function to add undirected edge in the graph ; Function to calculate minimum number of edges to be removed ; Create Adjacency list ; Iterate over all the nodes ; Print the final count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > vec [ 100001 ] ; bool vis [ 100001 ] ; int cc = 1 ; void dfs ( int node ) { vis [ node ] = true ; for ( auto x : vec [ node ] ) { if ( ! vis [ x ] ) { cc ++ ; dfs ( x ) ; } } } void addEdge ( int u , int v ) { vec [ u ] . push_back ( v ) ; vec [ v ] . push_back ( u ) ; } void minEdgeRemoved ( int N , int M , int Edges [ ] [ 2 ] ) { for ( int i = 0 ; i < M ; i ++ ) { addEdge ( Edges [ i ] [ 0 ] , Edges [ i ] [ 1 ] ) ; } memset ( vis , false , sizeof ( vis ) ) ; int k = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( ! vis [ i ] ) { cc = 1 ; dfs ( i ) ; k ++ ; } } cout << M - N + k << endl ; } int main ( ) { int N = 3 , M = 2 ; int Edges [ ] [ 2 ] = { { 1 , 2 } , { 2 , 3 } } ; minEdgeRemoved ( N , M , Edges ) ; }
Group Shifted String | C / C ++ program to print groups of shifted strings together . ; Total lowercase letter ; Method to a difference string for a given string . If string is " adf " then difference string will be " cb " ( first difference 3 then difference 2 ) ; Representing the difference as char ; This string will be 1 less length than str ; Method for grouping shifted string ; map for storing indices of string which are in same group ; iterating through map to print group ; Driver method to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int ALPHA = 26 ; string getDiffString ( string str ) { string shift = " " ; for ( int i = 1 ; i < str . length ( ) ; i ++ ) { int dif = str [ i ] - str [ i - 1 ] ; if ( dif < 0 ) dif += ALPHA ; shift += ( dif + ' a ' ) ; } return shift ; } void groupShiftedString ( string str [ ] , int n ) { map < string , vector < int > > groupMap ; for ( int i = 0 ; i < n ; i ++ ) { string diffStr = getDiffString ( str [ i ] ) ; groupMap [ diffStr ] . push_back ( i ) ; } for ( auto it = groupMap . begin ( ) ; it != groupMap . end ( ) ; it ++ ) { vector < int > v = it -> second ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) cout << str [ v [ i ] ] << " ▁ " ; cout << endl ; } } int main ( ) { string str [ ] = { " acd " , " dfg " , " wyz " , " yab " , " mop " , " bdfh " , " a " , " x " , " moqs " } ; int n = sizeof ( str ) / sizeof ( str [ 0 ] ) ; groupShiftedString ( str , n ) ; return 0 ; }
Check if a Binary Tree consists of a pair of leaf nodes with sum K | C ++ Program to implement the above approach ; Stores if a pair exists or not ; Struct binary tree node ; Creates a new node ; Function to check if a pair of leaf nodes exists with sum K ; checks if root is NULL ; Checks if the current node is a leaf node ; Checks for a valid pair of leaf nodes ; Insert value of current node into the set ; Traverse left and right subtree ; Driver Code ; Construct binary tree ; Stores the leaf nodes
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool pairFound = false ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * temp = new Node ( ) ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } void pairSum ( Node * root , int target , unordered_set < int > & S ) { if ( ! root ) return ; if ( ! root -> left and ! root -> right ) { if ( S . count ( target - root -> data ) ) { cout << target - root -> data << " ▁ " << root -> data ; pairFound = true ; return ; } else S . insert ( root -> data ) ; } pairSum ( root -> left , target , S ) ; pairSum ( root -> right , target , S ) ; } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> right = newNode ( 3 ) ; root -> right -> left = newNode ( 6 ) ; root -> right -> right = newNode ( 7 ) ; root -> right -> right -> right = newNode ( 8 ) ; unordered_set < int > S ; int K = 13 ; pairSum ( root , K , S ) ; if ( pairFound == false ) cout << " - 1" ; return 0 ; }
Maximum non | C ++ Program to implement the above approach ; Function to print the maximum rooks and their positions ; Initialize row and col array ; Marking the location of already placed rooks ; Print number of non - attacking rooks that can be placed ; To store the placed rook location ; Print lexographically smallest order ; Driver Code ; Size of board ; Number of rooks already placed ; Position of rooks ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countRooks ( int n , int k , int pos [ 2 ] [ 2 ] ) { int row [ n ] = { 0 } ; int col [ n ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { row [ i ] = 0 ; col [ i ] = 0 ; } for ( int i = 0 ; i < k ; i ++ ) { row [ pos [ i ] [ 0 ] - 1 ] = 1 ; col [ pos [ i ] [ 1 ] - 1 ] = 1 ; } int res = n - k ; cout << res << " ▁ " << endl ; int ri = 0 , ci = 0 ; while ( res -- > 0 ) { while ( row [ ri ] == 1 ) { ri ++ ; } while ( col [ ci ] == 1 ) { ci ++ ; } cout << ( ri + 1 ) << " ▁ " << ( ci + 1 ) << " ▁ " << endl ; ri ++ ; ci ++ ; } } int main ( ) { int N = 4 ; int K = 2 ; int pos [ 2 ] [ 2 ] = { { 1 , 4 } , { 2 , 2 } } ; countRooks ( N , K , pos ) ; }
Minimum insertions to form a palindrome with permutations allowed | CPP program to find minimum number of insertions to make a string palindrome ; Function will return number of characters to be added ; To store string length ; To store number of characters occurring odd number of times ; To store count of each character ; To store occurrence of each character ; To count characters with odd occurrence ; As one character can be odd return res - 1 but if string is already palindrome return 0 ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minInsertion ( string str ) { int n = str . length ( ) ; int res = 0 ; int count [ 26 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) count [ str [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) if ( count [ i ] % 2 == 1 ) res ++ ; return ( res == 0 ) ? 0 : res - 1 ; } int main ( ) { string str = " geeksforgeeks " ; cout << minInsertion ( str ) ; return 0 ; }