codeparrot/xlcost-text-to-code huggingface dataset:
Count levels in a Binary Tree consisting of node values having set bits at different positions | C ++ program for the above approach ; Structure of a node in the binary tree ; Function to find total unique levels ; Stores count of levels , where the set bits of all the nodes are at different positions ; Store nodes at each level of the tree using BFS ; Performing level order traversal ; Stores count of nodes at current level ; Stores prefix XOR of all the nodes at current level ; Stores prefix OR of all the nodes at current level ; Check if set bit of all the nodes at current level is at different positions or not ; Traverse nodes at current level ; Stores front element of the que ; Update prefix_OR ; Update prefix_XOR ; If left subtree not NULL ; If right subtree not NULL ; If bitwise AND is zero ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct TreeNode { int val = 0 ; TreeNode * left , * right ; TreeNode ( int x ) { val = x ; left = NULL ; right = NULL ; } } ; void uniqueLevels ( TreeNode * root ) { int uniqueLevels = 0 ; queue < TreeNode * > que ; que . push ( root ) ; while ( que . size ( ) > 0 ) { int length = que . size ( ) ; int prefix_XOR = 0 ; int prefix_OR = 0 ; bool flag = true ; for ( int i = 0 ; i < length ; i ++ ) { TreeNode * temp = que . front ( ) ; que . pop ( ) ; prefix_OR |= temp -> val ; prefix_XOR ^= temp -> val ; if ( prefix_XOR != prefix_OR ) flag = false ; if ( temp -> left ) que . push ( temp -> left ) ; if ( temp -> right ) que . push ( temp -> right ) ; } if ( flag ) uniqueLevels += 1 ; } cout << uniqueLevels ; } int main ( ) { TreeNode * root = new TreeNode ( 5 ) ; root -> left = new TreeNode ( 6 ) ; root -> right = new TreeNode ( 9 ) ; root -> left -> left = new TreeNode ( 1 ) ; root -> left -> right = new TreeNode ( 4 ) ; root -> right -> right = new TreeNode ( 7 ) ; uniqueLevels ( root ) ; return 0 ; }
Sum of all elements between k1 ' th ▁ and ▁ k2' th smallest elements | C ++ implementation of above approach ; Driver code ; decreasing value by 1 because we want min heapifying k times and it starts from 0 so we have to decrease it 1 time ; Step 1 : Do extract minimum k1 times ( This step takes O ( K1 Log n ) time ) ; Step 2 : Do extract minimum k2 k1 1 times and sum all extracted elements . ( This step takes O ( ( K2 k1 ) * Log n ) time )
#include <bits/stdc++.h> NEW_LINE using namespace std ; int n = 7 ; void minheapify ( int a [ ] , int index ) { int small = index ; int l = 2 * index + 1 ; int r = 2 * index + 2 ; if ( l < n && a [ l ] < a [ small ] ) small = l ; if ( r < n && a [ r ] < a [ small ] ) small = r ; if ( small != index ) { swap ( a [ small ] , a [ index ] ) ; minheapify ( a , small ) ; } } int main ( ) { int i = 0 ; int k1 = 3 ; int k2 = 6 ; int a [ ] = { 20 , 8 , 22 , 4 , 12 , 10 , 14 } ; int ans = 0 ; for ( i = ( n / 2 ) - 1 ; i >= 0 ; i -- ) { minheapify ( a , i ) ; } k1 -- ; k2 -- ; for ( i = 0 ; i <= k1 ; i ++ ) { a [ 0 ] = a [ n - 1 ] ; n -- ; minheapify ( a , 0 ) ; } for ( i = k1 + 1 ; i < k2 ; i ++ ) { ans += a [ 0 ] ; a [ 0 ] = a [ n - 1 ] ; n -- ; minheapify ( a , 0 ) ; } cout << ans ; return 0 ; }
Construct a List using the given Q XOR queries | C ++ program for the above approach ; Function to return required list after performing all the queries ; Store cumulative Bitwise XOR ; Initialize final list to return ; Perform each query ; The initial value of 0 ; Sort the list ; Return final list ; Driver Code ; Given Queries ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  5 NEW_LINE #define M  2 NEW_LINE list < int > ConstructList ( int Q [ N ] [ M ] ) { int xr = 0 ; list < int > ans ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( Q [ i ] [ 0 ] == 0 ) ans . push_back ( Q [ i ] [ 1 ] ^ xr ) ; else xr ^= Q [ i ] [ 1 ] ; } ans . push_back ( xr ) ; ans . sort ( ) ; return ans ; } int main ( ) { int Q [ N ] [ M ] = { { 0 , 6 } , { 0 , 3 } , { 0 , 2 } , { 1 , 4 } , { 1 , 5 } } ; list < int > ans = ConstructList ( Q ) ; for ( auto it = ans . begin ( ) ; it != ans . end ( ) ; ++ it ) cout << ' ▁ ' << * it ; }
Bitwise operations on Subarrays of size K | C ++ program for maximum values of each bitwise OR operation on element of subarray of size K ; Function to convert bit array to decimal number ; Function to find maximum values of each bitwise OR operation on element of subarray of size K ; Maintain an integer array bit [ ] of size 32 all initialized to 0 ; Create a sliding window of size k ; Function call ; Perform operation for removed element ; Perform operation for added_element ; Taking maximum value ; Return the result ; Driver Code ; Given array arr [ ] ; Given subarray size K ; Function Call
#include <iostream> NEW_LINE using namespace std ; int build_num ( int bit [ ] ) { int ans = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) if ( bit [ i ] ) ans += ( 1 << i ) ; return ans ; } int maximumOR ( int arr [ ] , int n , int k ) { int bit [ 32 ] = { 0 } ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < 32 ; j ++ ) { if ( arr [ i ] & ( 1 << j ) ) bit [ j ] ++ ; } } int max_or = build_num ( bit ) ; for ( int i = k ; i < n ; i ++ ) { for ( int j = 0 ; j < 32 ; j ++ ) { if ( arr [ i - k ] & ( 1 << j ) ) bit [ j ] -- ; } for ( int j = 0 ; j < 32 ; j ++ ) { if ( arr [ i ] & ( 1 << j ) ) bit [ j ] ++ ; } max_or = max ( build_num ( bit ) , max_or ) ; } return max_or ; } int main ( ) { int arr [ ] = { 2 , 5 , 3 , 6 , 11 , 13 } ; int k = 3 ; int n = sizeof arr / sizeof arr [ 0 ] ; cout << maximumOR ( arr , n , k ) ; return 0 ; }
Maximum XOR path of a Binary Tree | C ++ program to compute the Max - Xor value of path from the root to leaf of a Binary tree ; Binary tree node ; Function to create a new node ; Function calculate the value of max - xor ; Updating the xor value with the xor of the path from root to the node ; Check if node is leaf node ; Check if the left node exist in the tree ; Check if the right node exist in the tree ; Function to find the required count ; Recursively traverse the tree and compute the max_xor ; Return the result ; Driver code ; Create the binary tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; struct Node * newNode ( int data ) { struct Node * newNode = new Node ; newNode -> data = data ; newNode -> left = newNode -> right = NULL ; return ( newNode ) ; } void Solve ( Node * root , int xr , int & max_xor ) { xr = xr ^ root -> data ; if ( root -> left == NULL && root -> right == NULL ) { max_xor = max ( max_xor , xr ) ; return ; } if ( root -> left != NULL ) { Solve ( root -> left , xr , max_xor ) ; } if ( root -> right != NULL ) { Solve ( root -> right , xr , max_xor ) ; } return ; } int findMaxXor ( Node * root ) { int xr = 0 , max_xor = 0 ; Solve ( root , xr , max_xor ) ; return max_xor ; } int main ( void ) { struct Node * root = newNode ( 2 ) ; root -> left = newNode ( 1 ) ; root -> right = newNode ( 4 ) ; root -> left -> left = newNode ( 10 ) ; root -> left -> right = newNode ( 8 ) ; root -> right -> left = newNode ( 5 ) ; root -> right -> right = newNode ( 10 ) ; cout << findMaxXor ( root ) ; return 0 ; }
XOR of every element of an Array with a given number K | C ++ program to find XOR of every element of an array with a given number K ; Function to construct new array ; Traverse the array and compute XOR with K ; Print new array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void constructXORArray ( int A [ ] , int n , int K ) { int B [ n ] ; for ( int i = 0 ; i < n ; i ++ ) B [ i ] = A [ i ] ^ K ; for ( int i = 0 ; i < n ; i ++ ) cout << B [ i ] << " ▁ " ; cout << endl ; } int main ( ) { int A [ ] = { 2 , 4 , 1 , 3 , 5 } ; int K = 5 ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; constructXORArray ( A , n , K ) ; int B [ ] = { 4 , 75 , 45 , 42 } ; K = 2 ; n = sizeof ( B ) / sizeof ( B [ 0 ] ) ; constructXORArray ( B , n , K ) ; return 0 ; }
Bitwise AND of all the elements of array | C ++ program to find bitwise AND of all the elements in the array ; Function to calculate bitwise AND ; Initialise ans variable is arr [ 0 ] ; Traverse the array compute AND ; Return ans ; Driver function ; Function Call to find AND
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find_and ( int arr [ ] , int len ) { int ans = arr [ 0 ] ; for ( int i = 0 ; i < len ; i ++ ) { ans = ( ans & arr [ i ] ) ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 3 , 5 , 9 , 11 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << find_and ( arr , n ) ; return 0 ; }
Construct XOR tree by Given leaf nodes of Perfect Binary Tree | C ++ implementation of the above approach ; Maximum size for xor tree ; Allocating space to xor tree ; A recursive function that constructs xor tree for vector array [ start ... . . end ] . x is index of current node in XOR tree ; If there is one element in vector array , store it in current node of XOR tree ; cout << xor_tree [ x ] << " ▁ x " ; ; for left subtree ; for right subtree ; for getting the middle index from corner indexes . ; Build the left and the right subtrees by xor operation ; merge the left and right subtrees by XOR operation ; Function to construct XOR tree from the given vector array . This function calls construct_Xor_Tree_Util ( ) to fill the allocated memory of xor_tree vector array ; Driver Code ; leaf nodes of Perfect Binary Tree ; Build the xor tree ; Height of xor tree ; Maximum size of xor tree ; Root node is at index 0 considering 0 - based indexing in XOR Tree ; print value at root node
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxsize = 100005 ; vector < int > xor_tree ( maxsize ) ; void construct_Xor_Tree_Util ( vector < int > current , int start , int end , int x ) { if ( start == end ) { xor_tree [ x ] = current [ start ] ; return ; } int left = x * 2 + 1 ; int right = x * 2 + 2 ; int mid = start + ( end - start ) / 2 ; construct_Xor_Tree_Util ( current , start , mid , left ) ; construct_Xor_Tree_Util ( current , mid + 1 , end , right ) ; xor_tree [ x ] = ( xor_tree [ left ] ^ xor_tree [ right ] ) ; } void construct_Xor_Tree ( vector < int > arr , int n ) { construct_Xor_Tree_Util ( arr , 0 , n - 1 , 0 ) ; } int main ( ) { vector < int > leaf_nodes = { 40 , 32 , 12 , 1 , 4 , 3 , 2 , 7 } ; int n = leaf_nodes . size ( ) ; construct_Xor_Tree ( leaf_nodes , n ) ; int x = ( int ) ( ceil ( log2 ( n ) ) ) ; int max_size = 2 * ( int ) pow ( 2 , x ) - 1 ; cout << " Nodes ▁ of ▁ the ▁ XOR ▁ Tree : STRNEWLINE " ; for ( int i = 0 ; i < max_size ; i ++ ) { cout << xor_tree [ i ] << " ▁ " ; } int root = 0 ; cout << " Root : " }
Maximize distance between any two consecutive 1 ' s ▁ after ▁ flipping ▁ M ▁ 0' s | C ++ program to Maximize distance between any two consecutive 1 ' s ▁ after ▁ flipping ▁ M ▁ 0' s ; Function to return the count ; Flipping zeros at distance " d " ; Function to implement binary search ; Check for valid distance i . e mid ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int arr [ ] , int n , int m , int d ) { int i = 0 ; while ( i < n && m > 0 ) { m -- ; i += d ; } return m == 0 ? true : false ; } int maximumDistance ( int arr [ ] , int n , int m ) { int low = 1 , high = n - 1 ; int ans = 0 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; bool flag = check ( arr , n , m , mid ) ; if ( flag ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } int main ( ) { int n = 5 , m = 3 ; int arr [ n ] = { 0 } ; cout << maximumDistance ( arr , n , m ) ; return 0 ; }
Maximum XOR value of maximum and second maximum element among all possible subarrays | C ++ implementation of the approach ; Function to return the maximum possible xor ; To store the final answer ; Borward traversal ; Backward traversal ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumXor ( int arr [ ] , int n ) { stack < int > sForward , sBackward ; int ans = -1 ; for ( int i = 0 ; i < n ; i ++ ) { while ( ! sForward . empty ( ) && arr [ i ] < arr [ sForward . top ( ) ] ) { ans = max ( ans , arr [ i ] ^ arr [ sForward . top ( ) ] ) ; sForward . pop ( ) ; } sForward . push ( i ) ; while ( ! sBackward . empty ( ) && arr [ n - i - 1 ] < arr [ sBackward . top ( ) ] ) { ans = max ( ans , arr [ n - i - 1 ] ^ arr [ sBackward . top ( ) ] ) ; sBackward . pop ( ) ; } sBackward . push ( n - i - 1 ) ; } return ans ; } int main ( ) { int arr [ ] = { 8 , 1 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maximumXor ( arr , n ) ; return 0 ; }
Minimum count of Full Binary Trees such that the count of leaves is N | C ++ implementation of the approach ; Function to return the minimum count of trees required ; To store the count of set bits in n ; Driver code
#include <iostream> NEW_LINE using namespace std ; int minTrees ( int n ) { int count = 0 ; while ( n ) { n &= ( n - 1 ) ; count ++ ; } return count ; } int main ( ) { int n = 7 ; cout << minTrees ( n ) ; return 0 ; }
Count number of steps to cover a distance if steps can be taken in powers of 2 | C ++ program to count the minimum number of steps ; Function to count the minimum number of steps ; __builtin_popcount ( ) is a C ++ function to count the number of set bits in a number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMinSteps ( int K ) { return __builtin_popcount ( k ) ; } int main ( ) { int n = 343 ; cout << getMinSteps ( n ) << ' ' ; return 0 ; }
Game Theory in Balanced Ternary Numeral System | ( Moving 3 k steps at a time ) | C ++ implementation of the approach ; Function that returns true if the game cannot be won ; Driver code ; Common length
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDefeat ( string s1 , string s2 , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( ( s1 [ i ] == '0' && s2 [ i ] == '1' ) || ( s1 [ i ] == '1' && s2 [ i ] == '0' ) ) continue ; else if ( ( s1 [ i ] == '0' && s2 [ i ] == ' Z ' ) || ( s1 [ i ] == ' Z ' && s2 [ i ] == '0' ) ) continue ; else { return true ; } } return false ; } int main ( ) { string s1 = { "01001101ZZ " } ; string s2 = { "10Z1001000" } ; int n = 10 ; if ( isDefeat ( s1 , s2 , n ) ) cout << " Defeat " ; else cout << " Victory " ; return 0 ; }
Check if matrix A can be converted to B by changing parity of corner elements of any submatrix | C ++ implementation of the above approach ; Boolean function that returns true or false ; Traverse for all elements ; If both are not equal ; Change the parity of all corner elements ; Check if A is equal to B ; Not equal ; Driver Code ; First binary matrix ; Second binary matrix
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  3 NEW_LINE #define M  3 NEW_LINE bool check ( int a [ N ] [ M ] , int b [ N ] [ M ] ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { if ( a [ i ] [ j ] != b [ i ] [ j ] ) { a [ i ] [ j ] ^= 1 ; a [ 0 ] [ 0 ] ^= 1 ; a [ 0 ] [ j ] ^= 1 ; a [ i ] [ 0 ] ^= 1 ; } } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( a [ i ] [ j ] != b [ i ] [ j ] ) return false ; } } return true ; } int main ( ) { int a [ N ] [ N ] = { { 0 , 1 , 0 } , { 0 , 1 , 0 } , { 1 , 0 , 0 } } ; int b [ N ] [ N ] = { { 1 , 0 , 0 } , { 1 , 0 , 0 } , { 1 , 0 , 0 } } ; if ( check ( a , b ) ) cout << " Yes " ; else cout << " No " ; }
Position of the K | C ++ program to implement the above approach ; Function that returns the Kth set bit ; Traverse in the binary ; Check if the last bit is set or not ; Check if count is equal to k then return the index ; Increase the index as we move right ; Right shift the number by 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int FindIndexKthBit ( int n , int k ) { int cnt = 0 ; int ind = 0 ; while ( n ) { if ( n & 1 ) cnt ++ ; if ( cnt == k ) return ind ; ind ++ ; n = n >> 1 ; } return -1 ; } int main ( ) { int n = 15 , k = 3 ; int ans = FindIndexKthBit ( n , k ) ; if ( ans != -1 ) cout << ans ; else cout << " No ▁ k - th ▁ set ▁ bit " ; return 0 ; }
Check if the binary representation of a number has equal number of 0 s and 1 s in blocks | C ++ implementation of the above approach ; Function to check ; Converting integer to its equivalent binary number ; If adjacent character are same then increase counter ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void hasEqualBlockFrequency ( int N ) { string S = bitset < 3 > ( N ) . to_string ( ) ; set < int > p ; int c = 1 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( S [ i ] == S [ i + 1 ] ) c += 1 ; else { p . insert ( c ) ; c = 1 ; } p . insert ( c ) ; } if ( p . size ( ) == 1 ) cout << " Yes " << endl ; else cout << " No " << endl ; } int main ( ) { int N = 5 ; hasEqualBlockFrequency ( N ) ; return 0 ; }
Distance between two nodes of binary tree with node values from 1 to N | C ++ program to find minimum distance between two nodes in binary tree ; Function to get minimum path distance ; count bit length of n1 and n2 ; find bit difference and maxBit ; calculate result by formula ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minDistance ( int n1 , int n2 ) { int bitCount1 = floor ( log2 ( n1 ) ) + 1 ; int bitCount2 = floor ( log2 ( n2 ) ) + 1 ; int bitDiff = abs ( bitCount1 - bitCount2 ) ; int maxBitCount = max ( bitCount1 , bitCount2 ) ; if ( bitCount1 > bitCount2 ) { n2 = n2 * pow ( 2 , bitDiff ) ; } else { n1 = n1 * pow ( 2 , bitDiff ) ; } int xorValue = n1 ^ n2 ; int bitCountXorValue ; if ( xorValue == 0 ) bitCountXorValue = 1 ; else { bitCountXorValue = floor ( log2 ( xorValue ) ) + 1 ; } int disSimilarBitPosition = maxBitCount - bitCountXorValue ; int result = bitCount1 + bitCount2 - 2 * disSimilarBitPosition ; return result ; } int main ( ) { int n1 = 12 , n2 = 5 ; cout << minDistance ( n1 , n2 ) ; return 0 ; }
Remove one bit from a binary number to get maximum value | C ++ program to find next maximum binary number with one bit removed ; Function to find the maximum binary number ; Traverse the binary number ; Try finding a 0 and skip it ; Driver code ; Get the binary number ; Find the maximum binary number
#include <bits/stdc++.h> NEW_LINE using namespace std ; int printMaxAfterRemoval ( string s ) { bool flag = false ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '0' && flag == false ) { flag = true ; continue ; } else cout << s [ i ] ; } } int main ( ) { string s = "1001" ; printMaxAfterRemoval ( s ) ; }
Find position of left most dis | C ++ program to find the leftmost position of first dis - similar bit ; Function to find first dis - similar bit ; return zero for equal number ; count bit length of n1 and n2 ; find bit difference and maxBit ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int bitPos ( int n1 , int n2 ) { if ( n1 == n2 ) return 0 ; int bitCount1 = floor ( log2 ( n1 ) ) + 1 ; int bitCount2 = floor ( log2 ( n2 ) ) + 1 ; int bitDiff = abs ( bitCount1 - bitCount2 ) ; int maxBitCount = max ( bitCount1 , bitCount2 ) ; if ( bitCount1 > bitCount2 ) { n2 = n2 * pow ( 2 , bitDiff ) ; } else { n1 = n1 * pow ( 2 , bitDiff ) ; } int xorValue = n1 ^ n2 ; int bitCountXorValue = floor ( log2 ( xorValue ) ) + 1 ; int disSimilarBitPosition = maxBitCount - bitCountXorValue + 1 ; return disSimilarBitPosition ; } int main ( ) { int n1 = 53 , n2 = 55 ; cout << bitPos ( n1 , n2 ) ; return 0 ; }
Number of pairs with Bitwise OR as Odd number | C ++ program to count pairs with odd OR ; Function to count pairs with odd OR ; Count total even numbers in array ; Even pair count ; Total pairs ; Return Odd pair count ; Driver main
#include <iostream> NEW_LINE using namespace std ; int countOddPair ( int A [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( ! ( A [ i ] & 1 ) ) count ++ ; int evenPairCount = count * ( count - 1 ) / 2 ; int totPairs = N * ( N - 1 ) / 2 ; return totPairs - evenPairCount ; } int main ( ) { int A [ ] = { 5 , 6 , 2 , 8 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << countOddPair ( A , N ) << endl ; return 0 ; }
Replace every array element by Bitwise Xor of previous and next element | C ++ program to update every array element with sum of previous and next numbers in array ; Nothing to do when array size is 1 ; store current value of arr [ 0 ] and update it ; Update rest of the array elements ; Store current value of next interaction ; Update current value using previous value ; Update previous value ; Update last array element separately ; Driver program ; Print the modified array
#include <iostream> NEW_LINE using namespace std ; void ReplaceElements ( int arr [ ] , int n ) { if ( n <= 1 ) return ; int prev = arr [ 0 ] ; arr [ 0 ] = arr [ 0 ] ^ arr [ 1 ] ; for ( int i = 1 ; i < n - 1 ; i ++ ) { int curr = arr [ i ] ; arr [ i ] = prev ^ arr [ i + 1 ] ; prev = curr ; } arr [ n - 1 ] = prev ^ arr [ n - 1 ] ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; ReplaceElements ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; return 0 ; }
Lucky alive person in a circle | Set | C ++ implementation of the above approach ; to calculate the number of bits in the binary equivalent of n ; Finding complement by inverting the bits one by one from last ; XOR of n2 with ( 1 << i ) to flip the last ( i + 1 ) th bit of binary equivalent of n2 ; n2 will be the one 's complement of n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int luckiest_person ( int n ) { int len = log2 ( n ) + 1 ; int n2 = n ; for ( int i = 0 ; i < len ; i ++ ) { n2 = n2 ^ ( 1 << i ) ; } return abs ( n - n2 ) ; } int main ( ) { int N = 3 ; int lucky_p = luckiest_person ( N ) ; cout << lucky_p ; return 0 ; }
Lucky alive person in a circle | Set | Here is a O ( 1 ) solution for this problemit will work for 32 bit integers ] ; function to find the highest power of 2 which is less than 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 ; to calculate the highest number which is power of 2 and less than n ; return the correct answer as per the approach in above article ; Driver Code
#include <bits/stdc++.h> 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 luckiest_person ( int n ) { int nearestPower = setBitNumber ( n ) ; return 2 * ( n - nearestPower ) + 1 ; } int main ( ) { int N = 8 ; int lucky_p = luckiest_person ( N ) ; cout << lucky_p ; return 0 ; }