codeparrot/xlcost-text-to-code huggingface dataset:
Count elements which divide all numbers in range L | CPP program to Count elements which divides all numbers in range L - R ; function to count element Time complexity O ( n ^ 2 ) worst case ; answer for query ; 0 based index ; iterate for all elements ; check if the element divides all numbers in range ; no of elements ; if all elements are divisible by a [ i ] ; answer for every query ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int answerQuery ( int a [ ] , int n , int l , int r ) { int count = 0 ; l = l - 1 ; for ( int i = l ; i < r ; i ++ ) { int element = a [ i ] ; int divisors = 0 ; for ( int j = l ; j < r ; j ++ ) { if ( a [ j ] % a [ i ] == 0 ) divisors ++ ; else break ; } if ( divisors == ( r - l ) ) count ++ ; } return count ; } int main ( ) { int a [ ] = { 1 , 2 , 3 , 5 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int l = 1 , r = 4 ; cout << answerQuery ( a , n , l , r ) << endl ; l = 2 , r = 4 ; cout << answerQuery ( a , n , l , r ) << endl ; return 0 ; }
Minimum number of Binary strings to represent a Number | C ++ program to find the minimum number of binary strings to represent a number ; Function to find the minimum number of binary strings to represent a number ; Storing digits in correct order ; Find the maximum digit in the array ; Traverse for all the binary strings ; If digit at jth position is greater than 0 then substitute 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minBinary ( int n ) { int digit [ 10 ] , len = 0 ; while ( n > 0 ) { digit [ len ++ ] = n % 10 ; n /= 10 ; } reverse ( digit , digit + len ) ; int ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) { ans = max ( ans , digit [ i ] ) ; } cout << " Minimum ▁ Number ▁ of ▁ binary ▁ strings ▁ needed : ▁ " << ans << endl ; for ( int i = 1 ; i <= ans ; i ++ ) { int num = 0 ; for ( int j = 0 ; j < len ; j ++ ) { if ( digit [ j ] > 0 ) { num = num * 10 + 1 ; digit [ j ] -- ; } else { num *= 10 ; } } cout << num << " ▁ " ; } } int main ( ) { int n = 564 ; minBinary ( n ) ; return 0 ; }
Number whose sum of XOR with given array range is maximum | CPP program to find smallest integer X such that sum of its XOR with range is maximum . ; Function to make prefix array which counts 1 's of each bit up to that number ; Making a prefix array which sums number of 1 's up to that position ; If j - th bit of a number is set then add one to previously counted 1 's ; Function to find X ; Initially taking maximum value all bits 1 ; Iterating over each bit ; get 1 ' s ▁ at ▁ ith ▁ bit ▁ between ▁ the ▁ ▁ range ▁ L - R ▁ by ▁ subtracting ▁ 1' s till Rth number - 1 's till L-1th number ; If 1 ' s ▁ are ▁ more ▁ than ▁ or ▁ equal ▁ to ▁ 0' s then unset the ith bit from answer ; Set ith bit to 0 by doing Xor with 1 ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  2147483647 NEW_LINE int one [ 100001 ] [ 32 ] ; void make_prefix ( int A [ ] , int n ) { for ( int j = 0 ; j < 32 ; j ++ ) one [ 0 ] [ j ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int a = A [ i - 1 ] ; for ( int j = 0 ; j < 32 ; j ++ ) { int x = pow ( 2 , j ) ; if ( a & x ) one [ i ] [ j ] = 1 + one [ i - 1 ] [ j ] ; else one [ i ] [ j ] = one [ i - 1 ] [ j ] ; } } } int Solve ( int L , int R ) { int l = L , r = R ; int tot_bits = r - l + 1 ; int X = MAX ; for ( int i = 0 ; i < 31 ; i ++ ) { int x = one [ r ] [ i ] - one [ l - 1 ] [ i ] ; if ( x >= tot_bits - x ) { int ith_bit = pow ( 2 , i ) ; X = X ^ ith_bit ; } } return X ; } int main ( ) { int n = 5 , q = 3 ; int A [ ] = { 210 , 11 , 48 , 22 , 133 } ; int L [ ] = { 1 , 4 , 2 } , R [ ] = { 3 , 14 , 4 } ; make_prefix ( A , n ) ; for ( int j = 0 ; j < q ; j ++ ) cout << Solve ( L [ j ] , R [ j ] ) << endl ; return 0 ; }
Array range queries over range queries | CPP program to perform range queries over range queries . ; Function to execute type 1 query ; incrementing the array by 1 for type 1 queries ; Function to execute type 2 query ; If the query is of type 1 function call to type 1 query ; If the query is of type 2 recursive call to type 2 query ; Driver code ; Input size of array amd number of queries ; Build query matrix ; Perform queries ; printing the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; void type1 ( int arr [ ] , int start , int limit ) { for ( int i = start ; i <= limit ; i ++ ) arr [ i ] ++ ; } void type2 ( int arr [ ] , int query [ ] [ 3 ] , int start , int limit ) { for ( int i = start ; i <= limit ; i ++ ) { if ( query [ i ] [ 0 ] == 1 ) type1 ( arr , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; else if ( query [ i ] [ 0 ] == 2 ) type2 ( arr , query , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; } } int main ( ) { int n = 5 , m = 5 ; int arr [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) arr [ i ] = 0 ; int temp [ 15 ] = { 1 , 1 , 2 , 1 , 4 , 5 , 2 , 1 , 2 , 2 , 1 , 3 , 2 , 3 , 4 } ; int query [ 5 ] [ 3 ] ; int j = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { query [ i ] [ 0 ] = temp [ j ++ ] ; query [ i ] [ 1 ] = temp [ j ++ ] ; query [ i ] [ 2 ] = temp [ j ++ ] ; } for ( int i = 1 ; i <= m ; i ++ ) if ( query [ i ] [ 0 ] == 1 ) type1 ( arr , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; else if ( query [ i ] [ 0 ] == 2 ) type2 ( arr , query , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; for ( int i = 1 ; i <= n ; i ++ ) cout << arr [ i ] << " ▁ " ; return 0 ; }
Array range queries over range queries | CPP program to perform range queries over range queries . ; Function to create the record array ; Driver Code ; Build query matrix ; If query is of type 2 then function call to record_sum ; If query is of type 1 then simply add 1 to the record array ; for type 1 queries adding the contains of record array to the main array record array ; printing the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void record_sum ( int record [ ] , int l , int r , int n , int adder ) { for ( int i = l ; i <= r ; i ++ ) record [ i ] += adder ; } int main ( ) { int n = 5 , m = 5 ; int arr [ n ] ; memset ( arr , 0 , sizeof arr ) ; int query [ 5 ] [ 3 ] = { { 1 , 1 , 2 } , { 1 , 4 , 5 } , { 2 , 1 , 2 } , { 2 , 1 , 3 } , { 2 , 3 , 4 } } ; int record [ m ] ; memset ( record , 0 , sizeof record ) ; for ( int i = m - 1 ; i >= 0 ; i -- ) { if ( query [ i ] [ 0 ] == 2 ) record_sum ( record , query [ i ] [ 1 ] - 1 , query [ i ] [ 2 ] - 1 , m , record [ i ] + 1 ) ; else record_sum ( record , i , i , m , 1 ) ; } for ( int i = 0 ; i < m ; i ++ ) { if ( query [ i ] [ 0 ] == 1 ) record_sum ( arr , query [ i ] [ 1 ] - 1 , query [ i ] [ 2 ] - 1 , n , record [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << ' ▁ ' ; return 0 ; }
Array range queries over range queries | CPP program to perform range queries over range queries . ; For prefix sum array ; This function is used to apply square root decomposition in the record array ; traversing first block in range ; traversing completely overlapped blocks in range ; traversing last block in range ; Function to print the resultant array ; Driver code ; If query is of type 2 then function call to record_func ; If query is of type 1 then simply add 1 to the record array ; Merging the value of the block in the record array ; If query is of type 1 then the array elements are over - written by the record array ; The prefix sum of the array ; Printing the resultant array
#include <bits/stdc++.h> NEW_LINE #define max  10000 NEW_LINE using namespace std ; void update ( int arr [ ] , int l ) { arr [ l ] += arr [ l - 1 ] ; } void record_func ( int block_size , int block [ ] , int record [ ] , int l , int r , int value ) { while ( l < r && l % block_size != 0 && l != 0 ) { record [ l ] += value ; l ++ ; } while ( l + block_size <= r + 1 ) { block [ l / block_size ] += value ; l += block_size ; } while ( l <= r ) { record [ l ] += value ; l ++ ; } } void print ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int n = 5 , m = 5 ; int arr [ n ] , record [ m ] ; int block_size = sqrt ( m ) ; int block [ max ] ; int command [ 5 ] [ 3 ] = { { 1 , 1 , 2 } , { 1 , 4 , 5 } , { 2 , 1 , 2 } , { 2 , 1 , 3 } , { 2 , 3 , 4 } } ; memset ( arr , 0 , sizeof arr ) ; memset ( record , 0 , sizeof record ) ; memset ( block , 0 , sizeof block ) ; for ( int i = m - 1 ; i >= 0 ; i -- ) { if ( command [ i ] [ 0 ] == 2 ) { int x = i / ( block_size ) ; record_func ( block_size , block , record , command [ i ] [ 1 ] - 1 , command [ i ] [ 2 ] - 1 , ( block [ x ] + record [ i ] + 1 ) ) ; } else record [ i ] ++ ; } for ( int i = 0 ; i < m ; i ++ ) { int check = ( i / block_size ) ; record [ i ] += block [ check ] ; } for ( int i = 0 ; i < m ; i ++ ) { if ( command [ i ] [ 0 ] == 1 ) { arr [ command [ i ] [ 1 ] - 1 ] += record [ i ] ; if ( ( command [ i ] [ 2 ] - 1 ) < n - 1 ) arr [ ( command [ i ] [ 2 ] ) ] -= record [ i ] ; } } for ( int i = 1 ; i < n ; i ++ ) update ( arr , i ) ; print ( arr , n ) ; return 0 ; }
Array range queries over range queries | C ++ program to perform range queries over range queries . ; Updates a node in Binary Index Tree ( BITree ) at given index in BITree . The given value ' val ' is added to BITree [ i ] and all of its ancestors in tree . ; index in BITree [ ] is 1 more than the index in arr [ ] ; Traverse all ancestors and add ' val ' ; Add ' val ' to current node of BI Tree ; Update index to that of parent in update View ; Constructs and returns a Binary Indexed Tree for given array of size n . ; Create and initialize BITree [ ] as 0 ; Returns sum of arr [ 0. . index ] . This function assumes that the array is preprocessed and partial sums of array elements are stored in BITree [ ] ; index in BITree [ ] is 1 more than the index in arr [ ] ; Traverse ancestors of BITree [ index ] ; Add element of BITree to sum ; Move index to parent node in getSum View ; Function to update the BITree ; Driver code ; BITree for query of type 2 ; BITree for query of type 1 ; Input the queries in a 2D matrix ; If query is of type 2 then function call to update with BITree ; If query is of type 1 then function call to update with BITree2
#include <bits/stdc++.h> NEW_LINE using namespace std ; void updateBIT ( int BITree [ ] , int n , int index , int val ) { index = index + 1 ; while ( index <= n ) { BITree [ index ] = ( val + BITree [ index ] ) ; index = ( index + ( index & ( - index ) ) ) ; } return ; } int * constructBITree ( int n ) { int * BITree = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) BITree [ i ] = 0 ; return BITree ; } int getSum ( int BITree [ ] , int index ) { int sum = 0 ; index = index + 1 ; while ( index > 0 ) { sum = ( sum + BITree [ index ] ) ; index -= index & ( - index ) ; } return sum ; } void update ( int BITree [ ] , int l , int r , int n , int val ) { updateBIT ( BITree , n , l , val ) ; updateBIT ( BITree , n , r + 1 , - val ) ; return ; } int main ( ) { int n = 5 , m = 5 ; int temp [ 15 ] = { 1 , 1 , 2 , 1 , 4 , 5 , 2 , 1 , 2 , 2 , 1 , 3 , 2 , 3 , 4 } ; int q [ 5 ] [ 3 ] ; int j = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { q [ i ] [ 0 ] = temp [ j ++ ] ; q [ i ] [ 1 ] = temp [ j ++ ] ; q [ i ] [ 2 ] = temp [ j ++ ] ; } int * BITree = constructBITree ( m ) ; int * BITree2 = constructBITree ( n ) ; for ( int i = 1 ; i <= m ; i ++ ) cin >> q [ i ] [ 0 ] >> q [ i ] [ 1 ] >> q [ i ] [ 2 ] ; for ( int i = m ; i >= 1 ; i -- ) if ( q [ i ] [ 0 ] == 2 ) update ( BITree , q [ i ] [ 1 ] - 1 , q [ i ] [ 2 ] - 1 , m , 1 ) ; for ( int i = m ; i >= 1 ; i -- ) { if ( q [ i ] [ 0 ] == 2 ) { long int val = getSum ( BITree , i - 1 ) ; update ( BITree , q [ i ] [ 1 ] - 1 , q [ i ] [ 2 ] - 1 , m , val ) ; } } for ( int i = m ; i >= 1 ; i -- ) { if ( q [ i ] [ 0 ] == 1 ) { long int val = getSum ( BITree , i - 1 ) ; update ( BITree2 , q [ i ] [ 1 ] - 1 , q [ i ] [ 2 ] - 1 , n , ( val + 1 ) ) ; } } for ( int i = 1 ; i <= n ; i ++ ) cout << ( getSum ( BITree2 , i - 1 ) ) << " ▁ " ; return 0 ; }
Array range queries for searching an element | Program to determine if the element exists for different range queries ; Structure to represent a query range ; Find the root of the group containing the element at index x ; merge the two groups containing elements at indices x and y into one group ; make n subsets with every element as its root ; consecutive elements equal in value are merged into one single group ; Driver code ; check if the current element in consideration is equal to x or not if it is equal , then x exists in the range ; Print if x exists or not
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Query { int L , R , X ; } ; const int maxn = 100 ; int root [ maxn ] ; int find ( int x ) { return x == root [ x ] ? x : root [ x ] = find ( root [ x ] ) ; } int uni ( int x , int y ) { int p = find ( x ) , q = find ( y ) ; if ( p != q ) { root [ p ] = root [ q ] ; } } void initialize ( int a [ ] , int n , Query q [ ] , int m ) { for ( int i = 0 ; i < n ; i ++ ) root [ i ] = i ; for ( int i = 1 ; i < n ; i ++ ) if ( a [ i ] == a [ i - 1 ] ) uni ( i , i - 1 ) ; } int main ( ) { int a [ ] = { 1 , 1 , 5 , 4 , 5 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; Query q [ ] = { { 0 , 2 , 2 } , { 1 , 4 , 1 } , { 2 , 4 , 5 } } ; int m = sizeof ( q ) / sizeof ( q [ 0 ] ) ; initialize ( a , n , q , m ) ; for ( int i = 0 ; i < m ; i ++ ) { int flag = 0 ; int l = q [ i ] . L , r = q [ i ] . R , x = q [ i ] . X ; int p = r ; while ( p >= l ) { if ( a [ p ] == x ) { flag = 1 ; break ; } p = find ( p ) - 1 ; } if ( flag != 0 ) cout << x << " ▁ exists ▁ between ▁ [ " << l << " , ▁ " << r << " ] ▁ " << endl ; else cout << x << " ▁ does ▁ not ▁ exist ▁ between ▁ [ " << l << " , ▁ " << r << " ] ▁ " << endl ; } }
Count of pairs from 1 to a and 1 to b whose sum is divisible by N | C ++ implementation of above approach ; Function to find the distinct pairs from 1 - a & 1 - b such that their sum is divisible by n . ; Iterate over 1 to a to find distinct pairs ; For each integer from 1 to a b / n integers exists such that pair sum is divisible by n ; If ( i % n + b % n ) >= n one more pair is possible ; Return answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCountOfPairs ( int a , int b , int n ) { int ans = 0 ; for ( int i = 1 ; i <= a ; i ++ ) { ans += b / n ; ans += ( i % n + b % n ) >= n ? 1 : 0 ; } return ans ; } int main ( ) { int a = 5 , b = 13 , n = 3 ; cout << findCountOfPairs ( a , b , n ) ; return 0 ; }
Count of pairs from 1 to a and 1 to b whose sum is divisible by N | C ++ implementation of above approach ; Function to find the distinct pairs from 1 - a & 1 - b such that their sum is divisible by n . ; if first element is bigger then swap ; count is store the number of pair . ; we use temp for breaking a loop . ; count when a is greater . ; Count when a is smaller but b is greater ; Count when a and b both are smaller ; breaking condition ; For storing The pair in count . ; return the number of pairs . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCountOfPairs ( int a , int b , int n ) { if ( a > b ) { swap ( a , b ) ; } int temp = 1 , count = 0 ; for ( int i = n ; temp > 0 ; i += n ) { if ( a >= i ) { temp = i - 1 ; } else if ( b >= i ) { temp = a ; } else if ( i > b ) { temp = a - ( i - b ) + 1 ; } if ( temp > 0 ) { count += temp ; } } return count ; } int main ( ) { int a = 5 , b = 13 , n = 3 ; cout << findCountOfPairs ( a , b , n ) ; return 0 ; }
Array range queries for elements with frequency same as value | C ++ Program to answer Q queries to find number of times an element x appears x times in a Query subarray ; Returns the count of number x with frequency x in the subarray from start to end ; map for frequency of elements ; store frequency of each element in arr [ start ; end ] ; Count elements with same frequency as value ; Driver code ; 2D array of queries with 2 columns ; calculating number of queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solveQuery ( int start , int end , int arr [ ] ) { unordered_map < int , int > frequency ; for ( int i = start ; i <= end ; i ++ ) frequency [ arr [ i ] ] ++ ; int count = 0 ; for ( auto x : frequency ) if ( x . first == x . second ) count ++ ; return count ; } int main ( ) { int A [ ] = { 1 , 2 , 2 , 3 , 3 , 3 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int queries [ ] [ 3 ] = { { 0 , 1 } , { 1 , 1 } , { 0 , 2 } , { 1 , 3 } , { 3 , 5 } , { 0 , 5 } } ; int q = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; for ( int i = 0 ; i < q ; i ++ ) { int start = queries [ i ] [ 0 ] ; int end = queries [ i ] [ 1 ] ; cout << " Answer ▁ for ▁ Query ▁ " << ( i + 1 ) << " ▁ = ▁ " << solveQuery ( start , end , A ) << endl ; } return 0 ; }
Buy minimum items without change and given coins |  ; See if we can buy less than 10 items Using 10 Rs coins and one r Rs coin ; We can always buy 10 items ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minItems ( int k , int r ) { for ( int i = 1 ; i < 10 ; i ++ ) if ( ( i * k - r ) % 10 == 0 || ( i * k ) % 10 == 0 ) return i ; return 10 ; } int main ( ) { int k = 15 ; int r = 2 ; cout << minItems ( k , r ) ; return 0 ; }
Number of indexes with equal elements in given range | CPP program to count the number of indexes in range L R such that Ai = Ai + 1 ; function that answers every query in O ( r - l ) ; traverse from l to r and count the required indexes ; Driver Code ; 1 - st query ; 2 nd query
#include <bits/stdc++.h> NEW_LINE using namespace std ; int answer_query ( int a [ ] , int n , int l , int r ) { int count = 0 ; for ( int i = l ; i < r ; i ++ ) if ( a [ i ] == a [ i + 1 ] ) count += 1 ; return count ; } int main ( ) { int a [ ] = { 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 4 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int L , R ; L = 1 ; R = 8 ; cout << answer_query ( a , n , L , R ) << endl ; L = 0 ; R = 4 ; cout << answer_query ( a , n , L , R ) << endl ; return 0 ; }
Diagonal Sum of a Binary Tree | C ++ Program to calculate the sum of diagonal nodes . ; Node Structure ; to map the node with level - index ; Function to create new node ; recursvise function to calculate sum of elements where level - index is same . ; if there is no child then return ; add the element in the group of node whose level - index is equal ; left child call ; right child call ; Function call ; for different values of level - index add te sum of those node to answer ; Driver code ; build binary tree ; Function Call ; print the daigonal sums
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; map < int , int > grid ; struct Node * newNode ( int data ) { struct Node * Node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; Node -> data = data ; Node -> left = Node -> right = NULL ; return Node ; } void addConsideringGrid ( Node * root , int level , int index ) { if ( root == NULL ) return ; grid [ level - index ] += ( root -> data ) ; addConsideringGrid ( root -> left , level + 1 , index - 1 ) ; addConsideringGrid ( root -> right , level + 1 , index + 1 ) ; } vector < int > diagonalSum ( Node * root ) { grid . clear ( ) ; addConsideringGrid ( root , 0 , 0 ) ; vector < int > ans ; for ( auto x : grid ) { ans . push_back ( x . second ) ; } return ans ; } int main ( ) { struct Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 9 ) ; root -> left -> right = newNode ( 6 ) ; root -> right -> left = newNode ( 4 ) ; root -> right -> right = newNode ( 5 ) ; root -> right -> left -> right = newNode ( 7 ) ; root -> right -> left -> left = newNode ( 12 ) ; root -> left -> right -> left = newNode ( 11 ) ; root -> left -> left -> right = newNode ( 10 ) ; vector < int > v = diagonalSum ( root ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) cout << v [ i ] << " ▁ " ; return 0 ; }
Number of indexes with equal elements in given range | CPP program to count the number of indexes in range L R such that Ai = Ai + 1 ; array to store count of index from 0 to i that obey condition ; precomputing prefixans [ ] array ; traverse to compute the prefixans [ ] array ; function that answers every query in O ( 1 ) ; Driver Code ; pre - computation ; 1 - st query ; 2 nd query
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 1000 ; int prefixans [ N ] ; int countIndex ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == a [ i + 1 ] ) prefixans [ i ] = 1 ; if ( i != 0 ) prefixans [ i ] += prefixans [ i - 1 ] ; } } int answer_query ( int l , int r ) { if ( l == 0 ) return prefixans [ r - 1 ] ; else return prefixans [ r - 1 ] - prefixans [ l - 1 ] ; } int main ( ) { int a [ ] = { 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 4 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; countIndex ( a , n ) ; int L , R ; L = 1 ; R = 8 ; cout << answer_query ( L , R ) << endl ; L = 0 ; R = 4 ; cout << answer_query ( L , R ) << endl ; return 0 ; }
Count subarrays with Prime sum | C ++ program to count subarrays with Prime sum ; Function to count subarrays with Prime sum ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Initialize result ; Traverse through the array ; return answer ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int primeSubarrays ( int A [ ] , int n ) { int max_val = int ( pow ( 10 , 7 ) ) ; vector < bool > prime ( max_val + 1 , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int cnt = 0 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { int val = A [ i ] ; for ( int j = i + 1 ; j < n ; ++ j ) { val += A [ j ] ; if ( prime [ val ] ) ++ cnt ; } } return cnt ; } int main ( ) { int A [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << primeSubarrays ( A , n ) ; return 0 ; }
Total numbers with no repeated digits in a range | C ++ implementation of brute force solution . ; Function to check if the given number has repeated digit or not ; Traversing through each digit ; if the digit is present more than once in the number ; return 0 if the number has repeated digit ; return 1 if the number has no repeated digit ; Function to find total number in the given range which has no repeated digit ; Traversing through the range ; Add 1 to the answer if i has no repeated digit else 0 ; Driver Code ; Calling the calculate
#include <bits/stdc++.h> NEW_LINE using namespace std ; int repeated_digit ( int n ) { unordered_set < int > s ; while ( n != 0 ) { int d = n % 10 ; if ( s . find ( d ) != s . end ( ) ) { return 0 ; } s . insert ( d ) ; n = n / 10 ; } return 1 ; } int calculate ( int L , int R ) { int answer = 0 ; for ( int i = L ; i < R + 1 ; ++ i ) { answer = answer + repeated_digit ( i ) ; } return answer ; } int main ( ) { int L = 1 , R = 100 ; cout << calculate ( L , R ) ; return 0 ; }
Minimum swaps required to make a binary string alternating | CPP implementation of the approach ; returns the minimum number of swaps of a binary string passed as the argument to make it alternating ; counts number of zeroes at odd and even positions ; counts number of ones at odd and even positions ; alternating string starts with 0 ; alternating string starts with 1 ; calculates the minimum number of swaps ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countMinSwaps ( string st ) { int min_swaps = 0 ; int odd_0 = 0 , even_0 = 0 ; int odd_1 = 0 , even_1 = 0 ; int n = st . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { if ( st [ i ] == '1' ) even_1 ++ ; else even_0 ++ ; } else { if ( st [ i ] == '1' ) odd_1 ++ ; else odd_0 ++ ; } } int cnt_swaps_1 = min ( even_0 , odd_1 ) ; int cnt_swaps_2 = min ( even_1 , odd_0 ) ; return min ( cnt_swaps_1 , cnt_swaps_2 ) ; } int main ( ) { string st = "000111" ; cout << countMinSwaps ( st ) << endl ; return 0 ; }