codeparrot/xlcost-text-to-code huggingface dataset:
Check if bits of a number has count of consecutive set bits in increasing order | C ++ program to find if bit - pattern of a number has increasing value of continuous - 1 or not . ; Returns true if n has increasing count of continuous - 1 else false ; store the bit - pattern of n into bit bitset - bp ; set prev_count = 0 and curr_count = 0. ; increment current count of continuous - 1 ; traverse all continuous - 0 ; check prev_count and curr_count on encounter of first zero after continuous - 1 s ; check for last sequence of continuous - 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool findContinuous1 ( int n ) { const int bits = 8 * sizeof ( int ) ; string bp = bitset < bits > ( n ) . to_string ( ) ; int prev_count = 0 , curr_count = 0 ; int i = 0 ; while ( i < bits ) { if ( bp [ i ] == '1' ) { curr_count ++ ; i ++ ; } else if ( bp [ i - 1 ] == '0' ) { i ++ ; curr_count = 0 ; continue ; } else { if ( curr_count < prev_count ) return 0 ; i ++ ; prev_count = curr_count ; curr_count = 0 ; } } if ( prev_count > curr_count && ( curr_count != 0 ) ) return 0 ; return 1 ; } int main ( ) { int n = 179 ; if ( findContinuous1 ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check if bits of a number has count of consecutive set bits in increasing order | C ++ program to check if counts of consecutive 1 s are increasing order . ; Returns true if n has counts of consecutive 1 's are increasing order. ; Initialize previous count ; We traverse bits from right to left and check if counts are decreasing order . ; Ignore 0 s until we reach a set bit . ; Count current set bits ; Compare current with previous and update previous . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool areSetBitsIncreasing ( int n ) { int prev_count = INT_MAX ; while ( n > 0 ) { while ( n > 0 && n % 2 == 0 ) n = n / 2 ; int curr_count = 1 ; while ( n > 0 && n % 2 == 1 ) { n = n / 2 ; curr_count ++ ; } if ( curr_count >= prev_count ) return false ; prev_count = curr_count ; } return true ; } int main ( ) { int n = 10 ; if ( areSetBitsIncreasing ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check if a number has bits in alternate pattern | Set 1 | C ++ program to find if a number has alternate bit pattern ; Returns true if n has alternate bit pattern else false ; Store last bit ; Traverse through remaining bits ; If current bit is same as previous ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool findPattern ( int n ) { int prev = n % 2 ; n = n / 2 ; while ( n > 0 ) { int curr = n % 2 ; if ( curr == prev ) return false ; prev = curr ; n = n / 2 ; } return true ; } int main ( ) { int n = 10 ; if ( findPattern ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
XOR counts of 0 s and 1 s in binary representation | C ++ program to find XOR of counts 0 s and 1 s in binary representation of n . ; Returns XOR of counts 0 s and 1 s in binary representation of n . ; calculating count of zeros and ones ; Driver Program
#include <iostream> NEW_LINE using namespace std ; int countXOR ( int n ) { int count0 = 0 , count1 = 0 ; while ( n ) { ( n % 2 == 0 ) ? count0 ++ : count1 ++ ; n /= 2 ; } return ( count0 ^ count1 ) ; } int main ( ) { int n = 31 ; cout << countXOR ( n ) ; return 0 ; }
Count all pairs with given XOR | C ++ program to Count all pair with given XOR value x ; Returns count of pairs in arr [ 0. . n - 1 ] with XOR value equals to x . ; create empty map that stores counts of individual elements of array . ; If there exist an element in map m with XOR equals to x ^ arr [ i ] , that means there exist an element such that the XOR of element with arr [ i ] is equal to x , then increment count . ; Increment count of current element ; return total count of pairs with XOR equal to x ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int xorPairCount ( int arr [ ] , int n , int x ) { unordered_map < int , int > m ; for ( int i = 0 ; i < n ; i ++ ) { int curr_xor = x ^ arr [ i ] ; if ( m . find ( curr_xor ) != m . end ( ) ) result += m [ curr_xor ] ; m [ arr [ i ] ] ++ ; } return result ; } int main ( ) { int arr [ ] = { 2 , 5 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int x = 0 ; cout << " Count ▁ of ▁ pairs ▁ with ▁ given ▁ XOR ▁ = ▁ " << xorPairCount ( arr , n , x ) ; return 0 ; }
Bitwise and ( or & ) of a range | An efficient C ++ program to find bit - wise & of all numbers from x to y . ; Find position of MSB in n . For example if n = 17 , then position of MSB is 4. If n = 7 , value of MSB is 3 ; Function to find Bit - wise & of all numbers from x to y . ; ll res = 0 ; Initialize result ; Find positions of MSB in x and y ; If positions are not same , return ; Add 2 ^ msb_p1 to result ; subtract 2 ^ msb_p1 from x and y . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long int ll ; int msbPos ( ll n ) { int msb_p = -1 ; while ( n ) { n = n >> 1 ; msb_p ++ ; } return msb_p ; } ll andOperator ( ll x , ll y ) { while ( x && y ) { int msb_p1 = msbPos ( x ) ; int msb_p2 = msbPos ( y ) ; if ( msb_p1 != msb_p2 ) break ; ll msb_val = ( 1 << msb_p1 ) ; res = res + msb_val ; x = x - msb_val ; y = y - msb_val ; } return res ; } int main ( ) { ll x = 10 , y = 15 ; cout << andOperator ( x , y ) ; return 0 ; }
Multiply a number with 10 without using multiplication operator | C ++ program to multiply a number with 10 using bitwise operators ; Function to find multiplication of n with 10 without using multiplication operator ; Driver program to run the case
#include <bits/stdc++.h> NEW_LINE using namespace std ; int multiplyTen ( int n ) { return ( n << 1 ) + ( n << 3 ) ; } int main ( ) { int n = 50 ; cout << multiplyTen ( n ) ; return 0 ; }
Equal Sum and XOR | C ++ program to print count of values such that n + i = n ^ i ; function to count number of values less than equal to n that satisfy the given condition ; Traverse all numbers from 0 to n and increment result only when given condition is satisfied . ; Driver program
#include <iostream> NEW_LINE using namespace std ; int countValues ( int n ) { int countV = 0 ; for ( int i = 0 ; i <= n ; i ++ ) if ( ( n + i ) == ( n ^ i ) ) countV ++ ; return countV ; } int main ( ) { int n = 12 ; cout << countValues ( n ) ; return 0 ; }
Equal Sum and XOR | c ++ program to print count of values such that n + i = n ^ i ; function to count number of values less than equal to n that satisfy the given condition ; unset_bits keeps track of count of un - set bits in binary representation of n ; Return 2 ^ unset_bits ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countValues ( int n ) { int unset_bits = 0 ; while ( n ) { if ( ( n & 1 ) == 0 ) unset_bits ++ ; n = n >> 1 ; } return 1 << unset_bits ; } int main ( ) { int n = 12 ; cout << countValues ( n ) ; return 0 ; }
Find profession in a special family | C ++ program to find profession of a person at given level and position . ; Returns ' e ' if profession of node at given level and position is engineer . Else doctor . The function assumes that given position and level have valid values . ; Base case ; Recursively find parent 's profession. If parent  is a Doctor, this node will be a Doctor if it is  at odd position and an engineer if at even position ; If parent is an engineer , then current node will be an engineer if at add position and doctor if even position . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char findProffesion ( int level , int pos ) { if ( level == 1 ) return ' e ' ; if ( findProffesion ( level - 1 , ( pos + 1 ) / 2 ) == ' d ' ) return ( pos % 2 ) ? ' d ' : ' e ' ; return ( pos % 2 ) ? ' e ' : ' d ' ; } int main ( void ) { int level = 4 , pos = 2 ; ( findProffesion ( level , pos ) == ' e ' ) ? cout << " Engineer " : cout << " Doctor " ; return 0 ; }
Print first n numbers with exactly two set bits | C ++ program to print first n numbers with exactly two set bits ; Prints first n numbers with two set bits ; Initialize higher of two sets bits ; Keep reducing n for every number with two set bits . ; Consider all lower set bits for current higher set bit ; Print current number ; If we have found n numbers ; Consider next lower bit for current higher bit . ; Increment higher set bit ; Driver code
#include <iostream> NEW_LINE using namespace std ; void printTwoSetBitNums ( int n ) { int x = 1 ; while ( n > 0 ) { int y = 0 ; while ( y < x ) { cout << ( 1 << x ) + ( 1 << y ) << " ▁ " ; n -- ; if ( n == 0 ) return ; y ++ ; } x ++ ; } } int main ( ) { printTwoSetBitNums ( 4 ) ; return 0 ; }
Generate 0 and 1 with 25 % and 75 % probability | Program to print 1 with 75 % probability and 0 with 25 % probability ; Random Function to that returns 0 or 1 with equal probability ; rand ( ) function will generate odd or even number with equal probability . If rand ( ) generates odd number , the function will return 1 else it will return 0. ; Random Function to that returns 1 with 75 % probability and 0 with 25 % probability using Bitwise OR ; Driver code to test above functions ; Initialize random number generator
#include <iostream> NEW_LINE using namespace std ; int rand50 ( ) { return rand ( ) & 1 ; } bool rand75 ( ) { return rand50 ( ) | rand50 ( ) ; } int main ( ) { srand ( time ( NULL ) ) ; for ( int i = 0 ; i < 50 ; i ++ ) cout << rand75 ( ) ; return 0 ; }
Find even occurring elements in an array of limited range | C ++ Program to find the even occurring elements in given array ; Function to find the even occurring elements in given array ; do for each element of array ; left - shift 1 by value of current element ; Toggle the bit everytime element gets repeated ; Traverse array again and use _xor to find even occurring elements ; left - shift 1 by value of current element ; Each 0 in _xor represents an even occurrence ; print the even occurring numbers ; set bit as 1 to avoid printing duplicates ; Driver code
#include <iostream> NEW_LINE using namespace std ; void printRepeatingEven ( int arr [ ] , int n ) { long long _xor = 0L ; long long pos ; for ( int i = 0 ; i < n ; ++ i ) { pos = 1 << arr [ i ] ; _xor ^= pos ; } for ( int i = 0 ; i < n ; ++ i ) { pos = 1 << arr [ i ] ; if ( ! ( pos & _xor ) ) { cout << arr [ i ] << " ▁ " ; _xor ^= pos ; } } } int main ( ) { int arr [ ] = { 9 , 12 , 23 , 10 , 12 , 12 , 15 , 23 , 14 , 12 , 15 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printRepeatingEven ( arr , n ) ; return 0 ; }
Cyclic Redundancy Check and Modulo | Returns XOR of ' a ' and ' b ' ( both of same length ) ; Initialize result ; Traverse all bits , if bits are same , then XOR is 0 , else 1 ; Performs Modulo - 2 division ; Number of bits to be XORed at a time . ; Slicing the divident to appropriate length for particular step ; Replace the divident by the result of XOR and pull 1 bit down ; If leftmost bit is '0' . If the leftmost bit of the dividend ( or the part used in each step ) is 0 , the step cannot use the regular divisor ; we need to use an all - 0 s divisor . ; Increment pick to move further ; For the last n bits , we have to carry it out normally as increased value of pick will cause Index Out of Bounds . ; Function used at the sender side to encode data by appending remainder of modular division at the end of data . ; Appends n - 1 zeroes at end of data ; Append remainder in the original data ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string xor1 ( string a , string b ) { string result = " " ; int n = b . length ( ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] == b [ i ] ) result += "0" ; else result += "1" ; } return result ; } string mod2div ( string divident , string divisor ) { int pick = divisor . length ( ) ; string tmp = divident . substr ( 0 , pick ) ; int n = divident . length ( ) ; while ( pick < n ) { if ( tmp [ 0 ] == '1' ) tmp = xor1 ( divisor , tmp ) + divident [ pick ] ; else tmp = xor1 ( std :: string ( pick , '0' ) , tmp ) + divident [ pick ] ; pick += 1 ; } if ( tmp [ 0 ] == '1' ) tmp = xor1 ( divisor , tmp ) ; else tmp = xor1 ( std :: string ( pick , '0' ) , tmp ) ; return tmp ; } void encodeData ( string data , string key ) { int l_key = key . length ( ) ; string appended_data = ( data + std :: string ( l_key - 1 , '0' ) ) ; string remainder = mod2div ( appended_data , key ) ; string codeword = data + remainder ; cout << " Remainder ▁ : ▁ " << remainder << " STRNEWLINE " ; cout << " Encoded ▁ Data ▁ ( Data ▁ + ▁ Remainder ) ▁ : " << codeword << " STRNEWLINE " ; } int main ( ) { string data = "100100" ; string key = "1101" ; encodeData ( data , key ) ; return 0 ; }
Check if a number is Bleak | A simple C ++ program to check Bleak Number ; Function to get no of set bits in binary representation of passed binary no . ; Returns true if n is Bleak ; Check for all numbers ' x ' smaller than n . If x + countSetBits ( x ) becomes n , then n can 't be Bleak ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSetBits ( int x ) { unsigned int count = 0 ; while ( x ) { x &= ( x - 1 ) ; count ++ ; } return count ; } bool isBleak ( int n ) { for ( int x = 1 ; x < n ; x ++ ) if ( x + countSetBits ( x ) == n ) return false ; return true ; } int main ( ) { isBleak ( 3 ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; isBleak ( 4 ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; return 0 ; }
Check if a number is Bleak | C ++ program to demonstrate __builtin_popcount ( )
#include <iostream> NEW_LINE using namespace std ; int main ( ) { cout << __builtin_popcount ( 4 ) << endl ; cout << __builtin_popcount ( 15 ) ; return 0 ; }
Find the maximum subset XOR of a given set | C ++ program to find maximum XOR subset ; Number of bits to represent int ; Function to return maximum XOR subset in set [ ] ; Initialize index of chosen elements ; Traverse through all bits of integer starting from the most significant bit ( MSB ) ; Initialize index of maximum element and the maximum element ; If i 'th bit of set[j]  is set and set[j] is  greater than max so far. ; If there was no element with i 'th  bit set, move to  smaller i ; Put maximum element with i ' th ▁ bit ▁ set ▁ ▁ at ▁ index ▁ ' index ' ; Update maxInd and increment index ; Do XOR of set [ maxIndex ] with all numbers having i 'th bit as set. ; XOR set [ maxInd ] those numbers which have the i 'th bit set ; Increment index of chosen elements ; Final result is XOR of all elements ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define INT_BITS  32 NEW_LINE int maxSubarrayXOR ( int set [ ] , int n ) { int index = 0 ; for ( int i = INT_BITS - 1 ; i >= 0 ; i -- ) { int maxInd = index ; int maxEle = INT_MIN ; for ( int j = index ; j < n ; j ++ ) { if ( ( set [ j ] & ( 1 << i ) ) != 0 && set [ j ] > maxEle ) maxEle = set [ j ] , maxInd = j ; } if ( maxEle == INT_MIN ) continue ; swap ( set [ index ] , set [ maxInd ] ) ; maxInd = index ; for ( int j = 0 ; j < n ; j ++ ) { if ( j != maxInd && ( set [ j ] & ( 1 << i ) ) != 0 ) set [ j ] = set [ j ] ^ set [ maxInd ] ; } index ++ ; } int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res ^= set [ i ] ; return res ; } int main ( ) { int set [ ] = { 9 , 8 , 5 } ; int n = sizeof ( set ) / sizeof ( set [ 0 ] ) ; cout << " Max ▁ subset ▁ XOR ▁ is ▁ " ; cout << maxSubarrayXOR ( set , n ) ; return 0 ; }
Given a set , find XOR of the XOR 's of all subsets. | C ++ program to find XOR of XOR 's of all subsets ; Returns XOR of all XOR 's of given subset ; XOR is 1 only when n is 1 , else 0 ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findXOR ( int Set [ ] , int n ) { if ( n == 1 ) return Set [ 0 ] ; else return 0 ; } int main ( ) { int Set [ ] = { 1 , 2 , 3 } ; int n = sizeof ( Set ) / sizeof ( Set [ 0 ] ) ; cout << " XOR ▁ of ▁ XOR ' s ▁ of ▁ all ▁ subsets ▁ is ▁ " << findXOR ( Set , n ) ; return 0 ; }
Find XOR of two number without using XOR operator | C ++ program to find XOR without using ^ ; Returns XOR of x and y ; Assuming 32 - bit Integer ; Find current bits in x and y ; If both are 1 then 0 else xor is same as OR ; Update result ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; int myXOR ( int x , int y ) { for ( int i = 31 ; i >= 0 ; i -- ) { bool b1 = x & ( 1 << i ) ; bool b2 = y & ( 1 << i ) ; bool xoredBit = ( b1 & b2 ) ? 0 : ( b1 b2 ) ; res <<= 1 ; res |= xoredBit ; } return res ; } int main ( ) { int x = 3 , y = 5 ; cout << " XOR ▁ is ▁ " << myXOR ( x , y ) ; return 0 ; }
Find XOR of two number without using XOR operator | C ++ program to find XOR without using ^ ; Returns XOR of x and y ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; int myXOR ( int x , int y ) { return ( x y ) & ( ~ x ~ y ) ; } int main ( ) { int x = 3 , y = 5 ; cout << " XOR ▁ is ▁ " << myXOR ( x , y ) ; return 0 ; }
Operators in C | Set 2 ( Relational and Logical Operators ) | C ++ program to demonstrate working of logical operators ; greater than example ; greater than equal to ; less than example ; lesser than equal to ; equal to ; not equal to
#include <iostream> NEW_LINE using namespace std ; int main ( ) { int a = 10 , b = 4 ; if ( a > b ) cout << " a ▁ is ▁ greater ▁ than ▁ b STRNEWLINE " ; else cout << " a ▁ is ▁ less ▁ than ▁ or ▁ equal ▁ to ▁ b STRNEWLINE " ; if ( a >= b ) cout << " a ▁ is ▁ greater ▁ than ▁ or ▁ equal ▁ to ▁ b STRNEWLINE " ; else cout << " a ▁ is ▁ lesser ▁ than ▁ b STRNEWLINE " ; if ( a < b ) cout << " a ▁ is ▁ less ▁ than ▁ b STRNEWLINE " ; else cout << " a ▁ is ▁ greater ▁ than ▁ or ▁ equal ▁ to ▁ b STRNEWLINE " ; if ( a <= b ) cout << " a ▁ is ▁ lesser ▁ than ▁ or ▁ equal ▁ to ▁ b STRNEWLINE " ; else cout << " a ▁ is ▁ greater ▁ than ▁ b STRNEWLINE " ; if ( a == b ) cout << " a ▁ is ▁ equal ▁ to ▁ b STRNEWLINE " ; else cout << " a ▁ and ▁ b ▁ are ▁ not ▁ equal STRNEWLINE " ; if ( a != b ) cout << " a ▁ is ▁ not ▁ equal ▁ to ▁ b STRNEWLINE " ; else cout << " a ▁ is ▁ equal ▁ b STRNEWLINE " ; return 0 ; }
Operators in C | Set 2 ( Relational and Logical Operators ) | C ++ program to demonstrate working of logical operators ; logical AND example ; logical OR example ; logical NOT example
#include <iostream> NEW_LINE using namespace std ; int main ( ) { int a = 10 , b = 4 , c = 10 , d = 20 ; if ( a > b && c == d ) cout << " a ▁ is ▁ greater ▁ than ▁ b ▁ AND ▁ c ▁ is ▁ equal ▁ to ▁ d STRNEWLINE " ; else cout << " AND ▁ condition ▁ not ▁ satisfied STRNEWLINE " ; if ( a > b c == d ) cout << " a ▁ is ▁ greater ▁ than ▁ b ▁ OR ▁ c ▁ is ▁ equal ▁ to ▁ d STRNEWLINE " ; else cout << " Neither ▁ a ▁ is ▁ greater ▁ than ▁ b ▁ nor ▁ c ▁ is ▁ equal ▁ " " ▁ to ▁ d STRNEWLINE " ; if ( ! a ) cout << " a ▁ is ▁ zero STRNEWLINE " ; else cout << " a ▁ is ▁ not ▁ zero " ; return 0 ; }
Operators in C | Set 2 ( Relational and Logical Operators ) |
#include <stdbool.h> NEW_LINE #include <stdio.h> NEW_LINE int main ( ) { int a = 10 , b = 4 ; bool res = ( ( a != b ) cout << " GeeksQuiz " ) ; return 0 ; }
How to swap two bits in a given integer ? | C ++ program to swap bits in an integer ; This function swaps bit at positions p1 and p2 in an integer n ; Move p1 'th to rightmost side  ; Move p2 'th to rightmost side  ; XOR the two bits ; Put the xor bit back to their original positions ; XOR ' x ' with the original number so that the two sets are swapped ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int swapBits ( unsigned int n , unsigned int p1 , unsigned int p2 ) { unsigned int bit1 = ( n >> p1 ) & 1 ; unsigned int bit2 = ( n >> p2 ) & 1 ; unsigned int x = ( bit1 ^ bit2 ) ; x = ( x << p1 ) | ( x << p2 ) ; unsigned int result = n ^ x ; } int main ( ) { int res = swapBits ( 28 , 0 , 3 ) ; cout << " Result ▁ = ▁ " << res << " ▁ " ; return 0 ; }
Bitwise Operators in C / C ++ |  ; Function to return the only odd occurring element ; Driver Method
#include <iostream> NEW_LINE using namespace std ; int findOdd ( int arr [ ] , int n ) { int res = 0 , i ; for ( i = 0 ; i < n ; i ++ ) res ^= arr [ i ] ; return res ; } int main ( void ) { int arr [ ] = { 12 , 12 , 14 , 90 , 14 , 14 , 14 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " The ▁ odd ▁ occurring ▁ element ▁ is ▁ " << findOdd ( arr , n ) ; return 0 ; }
Bitwise Operators in C / C ++ |
#include <iostream> NEW_LINE using namespace std ; int main ( ) { int x = 2 , y = 5 ; ( x & y ) ? cout << " True ▁ " : cout << " False ▁ " ; ( x && y ) ? cout << " True ▁ " : cout << " False ▁ " ; return 0 ; }
Maximum length sub | C ++ implementation of the approach ; Function to return the maximum length of the required sub - array ; For the first consecutive pair of elements ; While a consecutive pair can be selected ; If current pair forms a valid sub - array ; 2 is the length of the current sub - array ; To extend the sub - array both ways ; While elements at indices l and r are part of a valid sub - array ; Update the maximum length so far ; Select the next consecutive pair ; Return the maximum length ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLength ( int arr [ ] , int n ) { int maxLen = 0 ; int i = 0 ; int j = i + 1 ; while ( j < n ) { if ( arr [ i ] != arr [ j ] ) { maxLen = max ( maxLen , 2 ) ; int l = i - 1 ; int r = j + 1 ; while ( l >= 0 && r < n && arr [ l ] == arr [ i ] && arr [ r ] == arr [ j ] ) { l -- ; r ++ ; } maxLen = max ( maxLen , 2 * ( r - j ) ) ; } i ++ ; j = i + 1 ; } return maxLen ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 , 0 , 0 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxLength ( arr , n ) ; return 0 ; }
Freivaldâ €™ s Algorithm to check if a matrix is product of two | CPP code to implement FreivaldaTMs Algorithm ; Function to check if ABx = Cx ; Generate a random vector ; Now comput B * r for evaluating expression A * ( B * r ) - ( C * r ) ; Now comput C * r for evaluating expression A * ( B * r ) - ( C * r ) ; Now comput A * ( B * r ) for evaluating expression A * ( B * r ) - ( C * r ) ; Finally check if value of expression A * ( B * r ) - ( C * r ) is 0 or not ; Runs k iterations Freivald . The value of k determines accuracy . Higher value means higher accuracy . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  2 NEW_LINE int freivald ( int a [ ] [ N ] , int b [ ] [ N ] , int c [ ] [ N ] ) { bool r [ N ] ; for ( int i = 0 ; i < N ; i ++ ) r [ i ] = random ( ) % 2 ; int br [ N ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) br [ i ] = br [ i ] + b [ i ] [ j ] * r [ j ] ; int cr [ N ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) cr [ i ] = cr [ i ] + c [ i ] [ j ] * r [ j ] ; int axbr [ N ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) axbr [ i ] = axbr [ i ] + a [ i ] [ j ] * br [ j ] ; for ( int i = 0 ; i < N ; i ++ ) if ( axbr [ i ] - cr [ i ] != 0 ) false ; return true ; } bool isProduct ( int a [ ] [ N ] , int b [ ] [ N ] , int c [ ] [ N ] , int k ) { for ( int i = 0 ; i < k ; i ++ ) if ( freivald ( a , b , c ) == false ) return false ; return true ; } int main ( ) { int a [ N ] [ N ] = { { 1 , 1 } , { 1 , 1 } } ; int b [ N ] [ N ] = { { 1 , 1 } , { 1 , 1 } } ; int c [ N ] [ N ] = { { 2 , 2 } , { 2 , 2 } } ; int k = 2 ; if ( isProduct ( a , b , c , k ) ) printf ( " Yes " ) ; else printf ( " No " ) ; return 0 ; }
Expectation or expected value of an array | CPP code to calculate expected value of an array ; Function to calculate expectation ; variable prb is for probability of each element which is same for each element ; calculating expectation overall ; returning expectation as sum ; Driver program ; Function for calculating expectation ; Display expectation of given array
#include <bits/stdc++.h> NEW_LINE using namespace std ; float calc_Expectation ( float a [ ] , float n ) { float prb = ( 1 / n ) ; float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] * prb ; return sum ; } int main ( ) { float expect , n = 6.0 ; float a [ 6 ] = { 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 } ; expect = calc_Expectation ( a , n ) ; cout << " Expectation ▁ of ▁ array ▁ E ( X ) ▁ is ▁ : ▁ " << expect << " STRNEWLINE " ; return 0 ; }