codeparrot/xlcost-text-to-code huggingface dataset:
Find a number such that maximum in array is minimum possible after XOR | C ++ program that find the minimum possible maximum ; Recursive function that find the minimum value after exclusive - OR ; Condition if ref size is zero or bit is negative then return 0 ; Condition if current bit is off then push current value in curr_off vector ; Condition if current bit is on then push current value in curr_on vector ; Condition if curr_off is empty then call recursive function on curr_on vector ; Condition if curr_on is empty then call recursive function on curr_off vector ; Return the minimum of curr_off and curr_on and add power of 2 of current bit ; Function that print the minimum value after exclusive - OR ; Pushing values in vector ; Printing answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int RecursiveFunction ( vector < int > ref , int bit ) { if ( ref . size ( ) == 0 bit < 0 ) return 0 ; vector < int > curr_on , curr_off ; for ( int i = 0 ; i < ref . size ( ) ; i ++ ) { if ( ( ( ref [ i ] >> bit ) & 1 ) == 0 ) curr_off . push_back ( ref [ i ] ) ; else curr_on . push_back ( ref [ i ] ) ; } if ( curr_off . size ( ) == 0 ) return RecursiveFunction ( curr_on , bit - 1 ) ; if ( curr_on . size ( ) == 0 ) return RecursiveFunction ( curr_off , bit - 1 ) ; return min ( RecursiveFunction ( curr_off , bit - 1 ) , RecursiveFunction ( curr_on , bit - 1 ) ) + ( 1 << bit ) ; } void PrintMinimum ( int a [ ] , int n ) { vector < int > v ; for ( int i = 0 ; i < n ; i ++ ) v . push_back ( a [ i ] ) ; cout << RecursiveFunction ( v , 30 ) << " STRNEWLINE " ; } int main ( ) { int arr [ ] = { 3 , 2 , 1 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; PrintMinimum ( arr , size ) ; return 0 ; }
Count of elements which are equal to the XOR of the next two elements | C ++ implementation of the approach ; Function to return the count of elements which are equal to the XOR of the next two elements ; To store the required count ; For every element of the array such that it has at least two elements appearing after it in the array ; If current element is equal to the XOR of the next two elements in the array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cntElements ( int arr [ ] , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { if ( arr [ i ] == ( arr [ i + 1 ] ^ arr [ i + 2 ] ) ) { cnt ++ ; } } return cnt ; } int main ( ) { int arr [ ] = { 4 , 2 , 1 , 3 , 7 , 8 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << cntElements ( arr , n ) ; return 0 ; }
Number of triplets in array having subarray xor equal | A simple C ++ program to find Number of triplets in array having subarray xor equal ; Function to return the count ; Initialise result ; Pick 1 st element of the triplet ; Pick 2 nd element of the triplet ; Pick 3 rd element of the triplet ; Taking xor in the first subarray ; Taking xor in the second subarray ; If both xor is equal ; Driver Code ; Function Calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; int xor_triplet ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = j ; k < n ; k ++ ) { int xor1 = 0 , xor2 = 0 ; for ( int x = i ; x < j ; x ++ ) { xor1 ^= arr [ x ] ; } for ( int x = j ; x <= k ; x ++ ) { xor2 ^= arr [ x ] ; } if ( xor1 == xor2 ) { ans ++ ; } } } } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << xor_triplet ( arr , n ) ; return 0 ; }
Number of triplets in array having subarray xor equal | C ++ trie based program to find the Number of triplets in array having subarray xor equal ; maximum number of bits in an integer <= 1e9 ; Structure of a Trie Node ; [ 0 ] index is bit 0 and [ 1 ] index is bit 1 ; Sum of indexes inserted at at a node ; Number of indexes inserted at a node ; Constructor to initialize a newly created node ; Function to insert curr_xor into the trie ; Iterate from the 31 st bit to the 0 th bit of curr_xor number ; Check if the current bit is set or not ; If this node isn 't already  present in the trie structure  insert it into the trie. ; Increase the sum of indexes by the current index value ; Increase the number of indexes by 1 ; Function to check if curr_xor is present in trie or not ; Iterate from the 31 st bit to the 0 th bit of curr_xor number ; Check if the current bit is set or not ; If this node isn 't already  present in the trie structure  that means no sub array till  current index has 0 xor so  return 0 ; Calculate the number of index inserted at final node ; Calculate the sum of index inserted at final node ; Function to return the count of valid triplets ; To store cumulative xor ; The root of the trie ; Insert the curr_xor in the trie ; Update the cumulative xor ; Check if the cumulative xor is present in the trie or not if present then add ( sz * index ) - sum ; Driver Code ; Given array
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define lg  31 NEW_LINE struct TrieNode { TrieNode * children [ 2 ] ; int sum_of_indexes ; int number_of_indexes ; TrieNode ( ) { this -> children [ 0 ] = nullptr ; this -> children [ 1 ] = nullptr ; this -> sum_of_indexes = 0 ; this -> number_of_indexes = 0 ; } } ; void insert ( TrieNode * node , int num , int index ) { for ( int bits = lg ; bits >= 0 ; bits -- ) { int curr_bit = ( num >> bits ) & 1 ; if ( node -> children [ curr_bit ] == nullptr ) { node -> children [ curr_bit ] = new TrieNode ( ) ; } node = node -> children [ curr_bit ] ; } node -> sum_of_indexes += index ; node -> number_of_indexes ++ ; } int query ( TrieNode * node , int num , int index ) { for ( int bits = lg ; bits >= 0 ; bits -- ) { int curr_bit = ( num >> bits ) & 1 ; if ( node -> children [ curr_bit ] == nullptr ) { return 0 ; } node = node -> children [ curr_bit ] ; } int sz = node -> number_of_indexes ; int sum = node -> sum_of_indexes ; int ans = ( sz * index ) - ( sum ) ; return ans ; } int no_of_triplets ( int arr [ ] , int n ) { int curr_xor = 0 ; int number_of_triplets = 0 ; TrieNode * root = new TrieNode ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int x = arr [ i ] ; insert ( root , curr_xor , i ) ; curr_xor ^= x ; number_of_triplets += query ( root , curr_xor , i ) ; } return number_of_triplets ; } int main ( ) { int arr [ ] = { 5 , 2 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << no_of_triplets ( arr , n ) ; return 0 ; }
Find the number of pair of Ideal nodes in a given tree | C ++ implementation of the approach ; Adjacency list ; bit : bit array i and j are starting and ending index INCLUSIVE ; bit : bit array n : size of bit array i is the index to be updated diff is ( new_val - old_val ) ; DFS function to find ideal pairs ; Function for initialisation ; Function to add an edge ; Function to find number of ideal pairs ; Find root of the tree ; Driver code ; Add edges ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100005 NEW_LINE int n , k ; vector < int > al [ N ] ; long long Ideal_pair ; long long bit [ N ] ; bool root_node [ N ] ; long long bit_q ( int i , int j ) { long long sum = 0ll ; while ( j > 0 ) { sum += bit [ j ] ; j -= ( j & ( j * -1 ) ) ; } i -- ; while ( i > 0 ) { sum -= bit [ i ] ; i -= ( i & ( i * -1 ) ) ; } return sum ; } void bit_up ( int i , long long diff ) { while ( i <= n ) { bit [ i ] += diff ; i += i & - i ; } } void dfs ( int node ) { Ideal_pair += bit_q ( max ( 1 , node - k ) , min ( n , node + k ) ) ; bit_up ( node , 1 ) ; for ( int i = 0 ; i < al [ node ] . size ( ) ; i ++ ) dfs ( al [ node ] [ i ] ) ; bit_up ( node , -1 ) ; } void initialise ( ) { Ideal_pair = 0 ; for ( int i = 0 ; i <= n ; i ++ ) { root_node [ i ] = true ; bit [ i ] = 0LL ; } } void Add_Edge ( int x , int y ) { al [ x ] . push_back ( y ) ; root_node [ y ] = false ; } long long Idealpairs ( ) { int r = -1 ; for ( int i = 1 ; i <= n ; i ++ ) if ( root_node [ i ] ) { r = i ; break ; } dfs ( r ) ; return Ideal_pair ; } int main ( ) { n = 6 , k = 3 ; initialise ( ) ; Add_Edge ( 1 , 2 ) ; Add_Edge ( 1 , 3 ) ; Add_Edge ( 3 , 4 ) ; Add_Edge ( 3 , 5 ) ; Add_Edge ( 3 , 6 ) ; cout << Idealpairs ( ) ; return 0 ; }
Print bitwise AND set of a number N | CPP program to print all bitwise subsets of N ( Efficient approach ) ; function to find bitwise subsets Efficient approach ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSubsets ( int n ) { for ( int i = n ; i > 0 ; i = ( i - 1 ) & n ) cout << i << " ▁ " ; cout << 0 ; } int main ( ) { int n = 9 ; printSubsets ( n ) ; return 0 ; }
Check if a number is divisible by 17 using bitwise operators | CPP program to check if a number is divisible by 17 or not using bitwise operator . ; function to check recursively if the number is divisible by 17 or not ; if n = 0 or n = 17 then yes ; if n is less then 17 , not divisible by 17 ; reducing the number by floor ( n / 16 ) - n % 16 ; driver code to check the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDivisibleby17 ( int n ) { if ( n == 0 n == 17 ) return true ; if ( n < 17 ) return false ; return isDivisibleby17 ( ( int ) ( n >> 4 ) - ( int ) ( n & 15 ) ) ; } int main ( ) { int n = 35 ; if ( isDivisibleby17 ( n ) ) cout << n << " ▁ is ▁ divisible ▁ by ▁ 17" ; else cout << n << " ▁ is ▁ not ▁ divisible ▁ by ▁ 17" ; return 0 ; }
Largest number with binary representation is m 1 's and m | CPP program to find largest number smaller than equal to n with m set bits then m - 1 0 bits . ; Returns largest number with m set bits then m - 1 0 bits . ; Start with 2 bits . ; initial answer is 1 which meets the given condition ; check for all numbers ; compute the number ; if less then N ; increment m to get the next number ; driver code to check the above condition
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long answer ( long long n ) { long m = 2 ; long long ans = 1 ; long long r = 1 ; while ( r < n ) { r = ( int ) ( pow ( 2 , m ) - 1 ) * ( pow ( 2 , m - 1 ) ) ; if ( r < n ) ans = r ; m ++ ; } return ans ; } int main ( ) { long long n = 7 ; cout << answer ( n ) ; return 0 ; }
Find most significant set bit of a number | Simple CPP program to find MSB number for given n . ; Driver code
#include <iostream> NEW_LINE using namespace std ; int setBitNumber ( int n ) { if ( n == 0 ) return 0 ; int msb = 0 ; n = n / 2 ; while ( n != 0 ) { n = n / 2 ; msb ++ ; } return ( 1 << msb ) ; } int main ( ) { int n = 0 ; cout << setBitNumber ( n ) ; return 0 ; }
Find most significant set bit of a number | CPP program to find MSB number for given n . ; Suppose n is 273 ( binary is 100010001 ) . It does following 100010001 | 010001000 = 110011001 ; This makes sure 4 bits ( From MSB and including MSB ) are set . It does following 110011001 | 001100110 = 111111111 ; Increment n by 1 so that there is only one set bit which is just before original MSB . n now becomes 1000000000 ; Return original MSB after shifting . n now becomes 100000000 ; Driver code
#include <iostream> NEW_LINE using namespace std ; int setBitNumber ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; n = n + 1 ; return ( n >> 1 ) ; } int main ( ) { int n = 273 ; cout << setBitNumber ( n ) ; return 0 ; }
Count trailing zero bits using lookup table | Simple C ++ code for counting trailing zeros in binary representation of a number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countTrailingZero ( int x ) { int count = 0 ; while ( ( x & 1 ) == 0 ) { x = x >> 1 ; count ++ ; } return count ; } int main ( ) { cout << countTrailingZero ( 11 ) << endl ; return 0 ; }
Count trailing zero bits using lookup table | C ++ code for counting trailing zeros in binary representation of a number ; Map a bit value mod 37 to its position ; Only difference between ( x and - x ) is the value of signed magnitude ( leftmostbit ) negative numbers signed bit is 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countTrailingZero ( int x ) { static const int lookup [ ] = { 32 , 0 , 1 , 26 , 2 , 23 , 27 , 0 , 3 , 16 , 24 , 30 , 28 , 11 , 0 , 13 , 4 , 7 , 17 , 0 , 25 , 22 , 31 , 15 , 29 , 10 , 12 , 6 , 0 , 21 , 14 , 9 , 5 , 20 , 8 , 19 , 18 } ; return lookup [ ( - x & x ) % 37 ] ; } int main ( ) { cout << countTrailingZero ( 48 ) << endl ; return 0 ; }
Calculate 7 n / 8 without using division and multiplication operators | C ++ program to evaluate ceil ( 7 n / 8 ) without using * and / ; Note the inner bracket here . This is needed because precedence of ' - ' operator is higher than ' < < ' ; Driver code
#include <iostream> NEW_LINE using namespace std ; int multiplyBySevenByEight ( int n ) { return ( n - ( n >> 3 ) ) ; } int main ( ) { int n = 9 ; cout << multiplyBySevenByEight ( n ) ; return 0 ; }
Longest set of Palindrome Numbers from the range [ L , R ] with at most K difference between its maximum and minimum | C ++ program for the above approach ; Function to search the rightmost index of given number ; Store the rightmost index ; Calculate the mid ; If given number <= num ; Assign ans = mid ; Update low ; Update high ; return ans ; Function to check if the given number is palindrome or not ; Generate reverse of the given number ; If n is a palindrome ; Function to find the maximum size of group of palindrome numbers having difference between maximum and minimum element at most K ; Stores the all the palindromic numbers in the range [ L , R ] ; Traverse over the range [ L , R ] ; If i is a palindrome ; Append the number in the list ; Stores count of maximum palindromic numbers ; Iterate each element in the list ; Calculate rightmost index in the list < current element + K ; Check if there is rightmost index from the current index ; Return the count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; static int search ( vector < int > list , int num ) { int low = 0 , high = list . size ( ) - 1 ; int ans = -1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( list [ mid ] <= num ) { ans = mid ; low = mid + 1 ; } else high = mid - 1 ; } return ans ; } bool isPalindrome ( int n ) { int rev = 0 ; int temp = n ; while ( n > 0 ) { rev = rev * 10 + n % 10 ; n /= 10 ; } return rev == temp ; } int countNumbers ( int L , int R , int K ) { vector < int > list ; for ( int i = L ; i <= R ; i ++ ) { if ( isPalindrome ( i ) ) { list . push_back ( i ) ; } } int count = 0 ; for ( int i = 0 ; i < list . size ( ) ; i ++ ) { int right_index = search ( list , list [ i ] + K - 1 ) ; if ( right_index != -1 ) count = max ( count , right_index - i + 1 ) ; } return count ; } int main ( ) { int L = 98 , R = 112 ; int K = 13 ; cout << countNumbers ( L , R , K ) ; }
Maximize Sum possible by subtracting same value from all elements of a Subarray of the given Array | C ++ program to implement the above approach ; Function to generate previous smaller element for each array element ; The first element has no previous smaller ; Stack to keep track of elements that have occurred previously ; Push the first index ; Pop all the elements until the previous element is smaller than current element ; Store the previous smaller element ; Push the index of the current element ; Return the array ; Function to generate next smaller element for each array element ; Stack to keep track of elements that have occurring next ; Iterate in reverse order for calculating next smaller ; Pop all the elements until the next element is smaller than current element ; Store the next smaller element ; Push the index of the current element ; Return the array ; Function to find the maximum sum by subtracting same value from all elements of a Subarray ; Stores previous smaller element ; Stores next smaller element ; Calculate contribution of each element ; Return answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > findPrevious ( vector < int > a , int n ) { vector < int > ps ( n ) ; ps [ 0 ] = -1 ; stack < int > Stack ; Stack . push ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { while ( Stack . size ( ) > 0 && a [ Stack . top ( ) ] >= a [ i ] ) Stack . pop ( ) ; ps [ i ] = Stack . size ( ) > 0 ? Stack . top ( ) : -1 ; Stack . push ( i ) ; } return ps ; } vector < int > findNext ( vector < int > a , int n ) { vector < int > ns ( n ) ; ns [ n - 1 ] = n ; stack < int > Stack ; Stack . push ( n - 1 ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { while ( Stack . size ( ) > 0 && a [ Stack . top ( ) ] >= a [ i ] ) Stack . pop ( ) ; ns [ i ] = Stack . size ( ) > 0 ? Stack . top ( ) : n ; Stack . push ( i ) ; } return ns ; } int findMaximumSum ( vector < int > a , int n ) { vector < int > prev_smaller = findPrevious ( a , n ) ; vector < int > next_smaller = findNext ( a , n ) ; int max_value = 0 ; for ( int i = 0 ; i < n ; i ++ ) { max_value = max ( max_value , a [ i ] * ( next_smaller [ i ] - prev_smaller [ i ] - 1 ) ) ; } return max_value ; } int main ( ) { int n = 3 ; vector < int > a { 80 , 48 , 82 } ; cout << findMaximumSum ( a , n ) ; return 0 ; }
Check if the given string is shuffled substring of another string | This function returns true if contents of arr1 [ ] and arr2 [ ] are same , otherwise false . ; This function search for all permutations of pat [ ] in txt [ ] ; countP [ ] : Store count of all characters of pattern countTW [ ] : Store count of current window of text ; Traverse through remaining characters of pattern ; Compare counts of current window of text with counts of pattern [ ] ; Add current character to current window ; Remove the first character of previous window ; Check for the last window in text ; Driver program to test above function
#include <iostream> NEW_LINE #include <cstring> NEW_LINE #define MAX  256 NEW_LINE using namespace std ; bool compare ( char arr1 [ ] , char arr2 [ ] ) { for ( int i = 0 ; i < MAX ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } bool search ( char * pat , char * txt ) { int M = strlen ( pat ) , N = strlen ( txt ) ; char countP [ MAX ] = { 0 } , countTW [ MAX ] = { 0 } ; for ( int i = 0 ; i < M ; i ++ ) { ( countP [ pat [ i ] ] ) ++ ; ( countTW [ txt [ i ] ] ) ++ ; } for ( int i = M ; i < N ; i ++ ) { if ( compare ( countP , countTW ) ) return true ; ( countTW [ txt [ i ] ] ) ++ ; countTW [ txt [ i - M ] ] -- ; } if ( compare ( countP , countTW ) ) return true ; return false ; } int main ( ) { char txt [ ] = " BACDGABCDA " ; char pat [ ] = " ABCD " ; if ( search ( pat , txt ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Maximize the median of the given array after adding K elements to the same array | C ++ implementation of the approach ; Function to return the maximized median ; Sort the array ; If size is even ; If size is odd ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float getMaxMedian ( int arr [ ] , int n , int k ) { int size = n + k ; sort ( arr , arr + n ) ; if ( size % 2 == 0 ) { float median = ( float ) ( arr [ ( size / 2 ) - 1 ] + arr [ size / 2 ] ) / 2 ; return median ; } float median = arr [ size / 2 ] ; return median ; } int main ( ) { int arr [ ] = { 3 , 2 , 3 , 4 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 ; cout << getMaxMedian ( arr , n , k ) ; return 0 ; }
Sort 3 Integers without using if condition or using only max ( ) function | C ++ program to print three numbers in sorted order using max function ; Find maximum element ; Find minimum element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSorted ( int a , int b , int c ) { int get_max = max ( a , max ( b , c ) ) ; int get_min = - max ( - a , max ( - b , - c ) ) ; int get_mid = ( a + b + c ) - ( get_max + get_min ) ; cout << get_min << " ▁ " << get_mid << " ▁ " << get_max ; } int main ( ) { int a = 4 , b = 1 , c = 9 ; printSorted ( a , b , c ) ; return 0 ; }
Binary Insertion Sort | iterative implementation ; Function to sort an array a [ ] of size ' n ' ; find location where selected should be inseretd ; Move all elements after location to create space ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int binarySearch ( int a [ ] , int item , int low , int high ) { while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( item == a [ mid ] ) return mid + 1 ; else if ( item > a [ mid ] ) low = mid + 1 ; else high = mid - 1 ; } return low ; } void insertionSort ( int a [ ] , int n ) { int i , loc , j , k , selected ; for ( i = 1 ; i < n ; ++ i ) { j = i - 1 ; selected = a [ i ] ; loc = binarySearch ( a , selected , 0 , j ) ; while ( j >= loc ) { a [ j + 1 ] = a [ j ] ; j -- ; } a [ j + 1 ] = selected ; } } int main ( ) { int a [ ] = { 37 , 23 , 0 , 17 , 12 , 72 , 31 , 46 , 100 , 88 , 54 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) , i ; insertionSort ( a , n ) ; cout << " Sorted ▁ array : ▁ STRNEWLINE " ; for ( i = 0 ; i < n ; i ++ ) cout << " ▁ " << a [ i ] ; return 0 ; }
Insertion Sort | C ++ program for insertion sort ; Function to sort an array using insertion sort ; Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position ; A utility function to print an array of size n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void insertionSort ( int arr [ ] , int n ) { int i , key , j ; for ( i = 1 ; i < n ; i ++ ) { key = arr [ i ] ; j = i - 1 ; while ( j >= 0 && arr [ j ] > key ) { arr [ j + 1 ] = arr [ j ] ; j = j - 1 ; } arr [ j + 1 ] = key ; } } void printArray ( int arr [ ] , int n ) { int i ; for ( i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; cout << endl ; } int main ( ) { int arr [ ] = { 12 , 11 , 13 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; insertionSort ( arr , n ) ; printArray ( arr , n ) ; return 0 ; }
Count of distinct permutation of a String obtained by swapping only unequal characters | C ++ program for the above approach ; Function to calculate total number of valid permutations ; Creating count which is equal to the Total number of characters present and ans that will store the number of unique permutations ; Storing frequency of each character present in the string ; Adding count of characters by excluding characters equal to current char ; Reduce the frequency of the current character and count by 1 , so that it cannot interfere with the calculations of the same elements present to the right of it . ; Return ans + 1 ( Because the given string is also a unique permutation ) ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int validPermutations ( string str ) { unordered_map < char , int > m ; int count = str . length ( ) , ans = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { m [ str [ i ] ] ++ ; } for ( int i = 0 ; i < str . length ( ) ; i ++ ) { ans += count - m [ str [ i ] ] ; m [ str [ i ] ] -- ; count -- ; } return ans + 1 ; } int main ( ) { string str = " sstt " ; cout << validPermutations ( str ) ; return 0 ; }
Counts paths from a point to reach Origin | C ++ program to count total number of paths from a point to origin ; DP based function to count number of paths ; Fill entries in bottommost row and leftmost columns ; Fill DP in bottom up manner ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPaths ( int n , int m ) { int dp [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = 1 ; for ( int i = 0 ; i <= m ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= m ; j ++ ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] ; return dp [ n ] [ m ] ; } int main ( ) { int n = 3 , m = 2 ; cout << " ▁ Number ▁ of ▁ Paths ▁ " << countPaths ( n , m ) ; return 0 ; }