codeparrot/xlcost-text-to-code huggingface dataset:
Length of longest Fibonacci subarray formed by removing only one element | C ++ program to find length of the longest subarray with all fibonacci numbers ; Function to create hash table to check for Fibonacci numbers ; Insert first two fibnonacci numbers ; Summation of last two numbers ; Update the variable each time ; Function to find the longest fibonacci subarray ; Find maximum value in the array ; Creating a set containing Fibonacci numbers ; Left array is used to count number of continuous fibonacci numbers starting from left of current element ; Check if current element is a fibonacci number ; Right array is used to count number of continuous fibonacci numbers starting from right of current element ; Check if current element is a fibonacci number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100000 NEW_LINE void createHash ( set < int > & hash , int maxElement ) { int prev = 0 , curr = 1 ; hash . insert ( prev ) ; hash . insert ( curr ) ; while ( curr <= maxElement ) { int temp = curr + prev ; hash . insert ( temp ) ; prev = curr ; curr = temp ; } } int longestFibSubarray ( int arr [ ] , int n ) { int max_val = * max_element ( arr , arr + n ) ; set < int > hash ; createHash ( hash , max_val ) ; int left [ n ] , right [ n ] ; int fibcount = 0 , res = -1 ; for ( int i = 0 ; i < n ; i ++ ) { left [ i ] = fibcount ; if ( hash . find ( arr [ i ] ) != hash . end ( ) ) { fibcount ++ ; } else fibcount = 0 ; } fibcount = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { right [ i ] = fibcount ; if ( hash . find ( arr [ i ] ) != hash . end ( ) ) { fibcount ++ ; } else fibcount = 0 ; } for ( int i = 0 ; i < n ; i ++ ) res = max ( res , left [ i ] + right [ i ] ) ; return res ; } int main ( ) { int arr [ ] = { 2 , 8 , 5 , 7 , 3 , 5 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << longestFibSubarray ( arr , n ) << endl ; return 0 ; }
Minimum window size containing atleast P primes in every window of given range | C ++ implementation to find the minimum window size in the range such that each window of that size contains atleast P primes ; Function to check that a number is a prime or not in O ( sqrt ( N ) ) ; Loop to check if any number number is divisible by any other number or not ; Function to check whether window size satisfies condition or not ; Loop to check each window of size have atleast P primes ; Checking condition using prefix sum ; Function to find the minimum window size possible for the given range in X and Y ; Prefix array ; Mark those numbers which are primes as 1 ; Convert to prefix sum ; Applying binary search over window size ; Check whether mid satisfies the condition or not ; If satisfies search in first half ; Else search in second half ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int N ) { if ( N < 2 ) return false ; if ( N < 4 ) return true ; if ( ( N & 1 ) == 0 ) return false ; if ( N % 3 == 0 ) return false ; int curr = 5 , s = sqrt ( N ) ; while ( curr <= s ) { if ( N % curr == 0 ) return false ; curr += 2 ; if ( N % curr == 0 ) return false ; curr += 4 ; } return true ; } bool check ( int s , int p , int prefix_sum [ ] , int n ) { bool satisfies = true ; for ( int i = 0 ; i < n ; i ++ ) { if ( i + s - 1 >= n ) break ; if ( prefix_sum [ i + s - 1 ] - ( i - 1 >= 0 ? prefix_sum [ i - 1 ] : 0 ) < p ) satisfies = false ; } return satisfies ; } int minimumWindowSize ( int x , int y , int p ) { int prefix_sum [ y - x + 1 ] = { 0 } ; for ( int i = x ; i <= y ; i ++ ) { if ( isPrime ( i ) ) prefix_sum [ i - x ] = 1 ; } for ( int i = 1 ; i < y - x + 1 ; i ++ ) prefix_sum [ i ] += prefix_sum [ i - 1 ] ; int low = 1 , high = y - x + 1 ; int mid ; while ( high - low > 1 ) { mid = ( low + high ) / 2 ; if ( check ( mid , p , prefix_sum , y - x + 1 ) ) { high = mid ; } else low = mid ; } if ( check ( low , p , prefix_sum , y - x + 1 ) ) return low ; return high ; } int main ( ) { int x = 12 ; int y = 42 ; int p = 3 ; cout << minimumWindowSize ( x , y , p ) ; return 0 ; }
XOR of pairwise sum of every unordered pairs in an array | C ++ implementation to find XOR of pairwise sum of every unordered pairs in an array ; Function to find XOR of pairwise sum of every unordered pairs ; Loop to choose every possible pairs in the array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int xorOfSum ( int a [ ] , int n ) { int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) answer ^= ( a [ i ] + a [ j ] ) ; } return answer ; } int main ( ) { int n = 3 ; int A [ n ] = { 1 , 2 , 3 } ; cout << xorOfSum ( A , n ) ; return 0 ; }
Maximum size of square such that all submatrices of that size have sum less than K | C ++ implementation to find the maximum size square submatrix such that their sum is less than K ; Size of matrix ; Function to preprocess the matrix for computing the sum of every possible matrix of the given size ; Loop to copy the first row of the matrix into the aux matrix ; Computing the sum column - wise ; Computing row wise sum ; Function to find the sum of a submatrix with the given indices ; Overall sum from the top to right corner of matrix ; Removing the sum from the top corer of the matrix ; Remove the overlapping sum ; Add the sum of top corner which is subtracted twice ; Function to find the maximum square size possible with the such that every submatrix have sum less than the given sum ; Loop to choose the size of matrix ; Loop to find the sum of the matrix of every possible submatrix ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  4 NEW_LINE #define M  5 NEW_LINE void preProcess ( int mat [ N ] [ M ] , int aux [ N ] [ M ] ) { for ( int i = 0 ; i < M ; i ++ ) aux [ 0 ] [ i ] = mat [ 0 ] [ i ] ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) aux [ i ] [ j ] = mat [ i ] [ j ] + aux [ i - 1 ] [ j ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 1 ; j < M ; j ++ ) aux [ i ] [ j ] += aux [ i ] [ j - 1 ] ; } int sumQuery ( int aux [ N ] [ M ] , int tli , int tlj , int rbi , int rbj ) { int res = aux [ rbi ] [ rbj ] ; if ( tli > 0 ) res = res - aux [ tli - 1 ] [ rbj ] ; if ( tlj > 0 ) res = res - aux [ rbi ] [ tlj - 1 ] ; if ( tli > 0 && tlj > 0 ) res = res + aux [ tli - 1 ] [ tlj - 1 ] ; return res ; } int maximumSquareSize ( int mat [ N ] [ M ] , int K ) { int aux [ N ] [ M ] ; preProcess ( mat , aux ) ; for ( int i = min ( N , M ) ; i >= 1 ; i -- ) { bool satisfies = true ; for ( int x = 0 ; x < N ; x ++ ) { for ( int y = 0 ; y < M ; y ++ ) { if ( x + i - 1 <= N - 1 && y + i - 1 <= M - 1 ) { if ( sumQuery ( aux , x , y , x + i - 1 , y + i - 1 ) > K ) satisfies = false ; } } } if ( satisfies == true ) return ( i ) ; } return 0 ; } int main ( ) { int K = 30 ; int mat [ N ] [ M ] = { { 1 , 2 , 3 , 4 , 6 } , { 5 , 3 , 8 , 1 , 2 } , { 4 , 6 , 7 , 5 , 5 } , { 2 , 4 , 8 , 9 , 4 } } ; cout << maximumSquareSize ( mat , K ) ; return 0 ; }
Find two Fibonacci numbers whose sum can be represented as N | C ++ program to find two Fibonacci numbers whose sum can be represented as N ; Function to create hash table to check Fibonacci numbers ; Storing the first two numbers in the hash ; Finding Fibonacci numbers up to N and storing them in the hash ; Function to find the Fibonacci pair with the given sum ; creating a set containing all fibonacci numbers ; Traversing all numbers to find first pair ; If both i and ( N - i ) are Fibonacci ; Printing the pair because i + ( N - i ) = N ; If no fibonacci pair is found whose sum is equal to n ; Driven code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void createHash ( set < int > & hash , int maxElement ) { int prev = 0 , curr = 1 ; hash . insert ( prev ) ; hash . insert ( curr ) ; while ( curr < maxElement ) { int temp = curr + prev ; hash . insert ( temp ) ; prev = curr ; curr = temp ; } } void findFibonacciPair ( int n ) { set < int > hash ; createHash ( hash , n ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hash . find ( i ) != hash . end ( ) && hash . find ( n - i ) != hash . end ( ) ) { cout << i << " , ▁ " << ( n - i ) << endl ; return ; } } cout << " - 1 STRNEWLINE " ; } int main ( ) { int N = 90 ; findFibonacciPair ( N ) ; return 0 ; }
Count of multiples in an Array before every element | C ++ program to count of multiples in an Array before every element ; Function to find all factors of N and keep their count in map ; Traverse from 1 to sqrt ( N ) if i divides N , increment i and N / i in map ; Function to count of multiples in an Array before every element ; To store factors all of all numbers ; Traverse for all possible i 's ; Printing value of a [ i ] in map ; Now updating the factors of a [ i ] in the map ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void add_factors ( int n , unordered_map < int , int > & mp ) { for ( int i = 1 ; i <= int ( sqrt ( n ) ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) mp [ i ] ++ ; else { mp [ i ] ++ ; mp [ n / i ] ++ ; } } } } void count_divisors ( int a [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { cout << mp [ a [ i ] ] << " ▁ " ; add_factors ( a [ i ] , mp ) ; } } int main ( ) { int arr [ ] = { 8 , 1 , 28 , 4 , 2 , 6 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; count_divisors ( arr , n ) ; return 0 ; }
Reduce the array such that each element appears at most 2 times | C ++ program to reduce the array such that each element appears at most 2 times ; Function to remove duplicates ; Initialise 2 nd pointer ; Iterate over the array ; Updating the 2 nd pointer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void removeDuplicates ( int arr [ ] , int n ) { int st = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n - 2 && arr [ i ] == arr [ i + 1 ] && arr [ i ] == arr [ i + 2 ] ) continue ; else { arr [ st ] = arr [ i ] ; st ++ ; } } cout << " { " ; for ( int i = 0 ; i < st ; i ++ ) { cout << arr [ i ] ; if ( i != st - 1 ) cout << " , ▁ " ; } cout << " } " ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 3 , 3 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; removeDuplicates ( arr , n ) ; return 0 ; }
Check if an Array is a permutation of numbers from 1 to N | C ++ Program to decide if an array represents a permutation or not ; Function to check if an array represents a permutation or not ; Set to check the count of non - repeating elements ; Insert all elements in the set ; Calculating the max element ; Check if set size is equal to n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool permutation ( int arr [ ] , int n ) { set < int > hash ; int maxEle = 0 ; for ( int i = 0 ; i < n ; i ++ ) { hash . insert ( arr [ i ] ) ; maxEle = max ( maxEle , arr [ i ] ) ; } if ( maxEle != n ) return false ; if ( hash . size ( ) == n ) return true ; return false ; } int main ( ) { int arr [ ] = { 1 , 2 , 5 , 3 , 2 } ; int n = sizeof ( arr ) / sizeof ( int ) ; if ( permutation ( arr , n ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
D 'Esopo | C ++ implementation for D 'Esopo-Pape algorithm ; Number of vertices in graph ; Adjacency list of graph ; Queue to store unoperated vertices ; Distance from source vertex distance = [ float ( ' inf ' ) ] * v ; Status of vertex ; let 0 be the source vertex ; Pop from front of the queue ; Scan adjacent vertices of u ; e < - [ weight , vertex ] ; if e . second is entering first time in the queue ; Append at back of queue ; Append at front of queue ; Driver Code ; Adjacency matrix of graph
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define inf  INT_MAX NEW_LINE vector < int > desopo ( vector < vector < int > > & graph ) { int v = graph . size ( ) ; map < int , vector < pair < int , int > > > adj ; for ( int i = 0 ; i < v ; i ++ ) { for ( int j = i + 1 ; j < v ; j ++ ) { if ( graph [ i ] [ j ] != 0 ) { adj [ i ] . push_back ( { graph [ i ] [ j ] , j } ) ; adj [ j ] . push_back ( { graph [ i ] [ j ] , i } ) ; } } } deque < int > q ; vector < int > distance ( v , inf ) ; vector < bool > is_in_queue ( v , false ) ; int source = 0 ; distance = 0 ; q . push_back ( source ) ; is_in_queue = true ; while ( ! q . empty ( ) ) { int u = q . front ( ) ; q . pop_front ( ) ; is_in_queue [ u ] = false ; for ( auto e : adj [ u ] ) { if ( distance [ e . second ] > distance [ u ] + e . first ) { distance [ e . second ] = distance [ u ] + e . first ; if ( ! is_in_queue [ e . second ] ) { if ( distance [ e . second ] == inf ) q . push_back ( e . second ) ; else q . push_front ( e . second ) ; is_in_queue [ e . second ] = true ; } } } } return distance ; } int main ( int argc , char const * argv [ ] ) { vector < vector < int > > graph = { { 0 , 4 , 0 , 0 , 8 } , { 0 , 0 , 8 , 0 , 11 } , { 0 , 8 , 0 , 2 , 0 } , { 0 , 0 , 2 , 0 , 1 } , { 8 , 11 , 0 , 1 , 0 } } ; for ( auto i : desopo ( graph ) ) { cout << i << " ▁ " ; } return 0 ; }
Count of Subsets of a given Set with element X present in it | C ++ code to implement the above approach ; N stores total number of subsets ; Generate each subset one by one ; Check every bit of i ; if j 'th bit of i is set,  check arr[j] with X ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CountSubSet ( int arr [ ] , int n , int X ) { int N = pow ( 2 , n ) ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i & ( 1 << j ) ) if ( arr [ j ] == X ) count += 1 ; } } return count ; } int main ( ) { int arr [ ] = { 4 , 5 , 6 , 7 } ; int X = 5 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << CountSubSet ( arr , n , X ) ; return 0 ; }
Count of Subsets of a given Set with element X present in it | C ++ implementation of above approach ; Function to calculate ( 2 ^ ( n - 1 ) ) ; Initially initialize answer to 1 ; If e is odd , multiply b with answer ; Function to count subsets in which X element is present ; Check if X is present in given subset or not ; If X is present in set then calculate 2 ^ ( n - 1 ) as count ; if X is not present in a given set ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculatePower ( int b , int e ) { int ans = 1 ; while ( e > 0 ) { if ( e % 2 == 1 ) ans = ans * b ; e = e / 2 ; b = b * b ; } return ans ; } int CountSubSet ( int arr [ ] , int n , int X ) { int count = 0 , checkX = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == X ) { checkX = 1 ; break ; } } if ( checkX == 1 ) count = calculatePower ( 2 , n - 1 ) ; else count = 0 ; return count ; } int main ( ) { int arr [ ] = { 4 , 5 , 6 , 7 } ; int X = 5 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << CountSubSet ( arr , n , X ) ; return 0 ; }
Value to be subtracted from array elements to make sum of all elements equals K | C ++ implementation of the approach ; Function to return the amount of wood collected if the cut is made at height m ; Function that returns Height at which cut should be made ; Sort the heights of the trees ; The minimum and the maximum cut that can be made ; Binary search to find the answer ; The amount of wood collected when cut is made at the mid ; If the current collected wood is equal to the required amount ; If it is more than the required amount then the cut needs to be made at a height higher than the current height ; Else made the cut at a lower height ; Driver code
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int woodCollected ( int height [ ] , int n , int m ) { int sum = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( height [ i ] - m <= 0 ) break ; sum += ( height [ i ] - m ) ; } return sum ; } int collectKWood ( int height [ ] , int n , int k ) { sort ( height , height + n ) ; int low = 0 , high = height [ n - 1 ] ; while ( low <= high ) { int mid = low + ( ( high - low ) / 2 ) ; int collected = woodCollected ( height , n , mid ) ; if ( collected == k ) return mid ; if ( collected > k ) low = mid + 1 ; else high = mid - 1 ; } return -1 ; } int main ( ) { int height [ ] = { 1 , 2 , 1 , 2 } ; int n = sizeof ( height ) / sizeof ( height [ 0 ] ) ; int k = 2 ; cout << collectKWood ( height , n , k ) ; return 0 ; }
Check if given string contains all the digits | C ++ implementation of the approach ; Function that returns true if ch is a digit ; Function that returns true if str contains all the digits from 0 to 9 ; To mark the present digits ; For every character of the string ; If the current character is a digit ; Mark the current digit as present ; For every digit from 0 to 9 ; If the current digit is not present in str ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 10 ; bool isDigit ( char ch ) { if ( ch >= '0' && ch <= '9' ) return true ; return false ; } bool allDigits ( string str , int len ) { bool present [ MAX ] = { false } ; for ( int i = 0 ; i < len ; i ++ ) { if ( isDigit ( str [ i ] ) ) { int digit = str [ i ] - '0' ; present [ digit ] = true ; } } for ( int i = 0 ; i < MAX ; i ++ ) { if ( ! present [ i ] ) return false ; } return true ; } int main ( ) { string str = " Geeks12345for69708" ; int len = str . length ( ) ; if ( allDigits ( str , len ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check if a symmetric plus is possible from the elements of the given array | C ++ implementation of the approach ; Function that return true if a symmetric is possible with the elements of the array ; Map to store the frequency of the array elements ; Traverse through array elements and count frequencies ; For every unique element ; Element has already been found ; The frequency of the element something other than 0 and 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPlusPossible ( int arr [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ arr [ i ] ] ++ ; bool foundModOne = false ; for ( auto x : mp ) { int element = x . first ; int frequency = x . second ; if ( frequency % 4 == 0 ) continue ; if ( frequency % 4 == 1 ) { if ( foundModOne ) return false ; foundModOne = true ; } else return false ; } } int main ( ) { int arr [ ] = { 1 , 1 , 1 , 1 , 2 , 2 , 2 , 3 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( isPlusPossible ( arr , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Queries for elements greater than K in the given index range using Segment Tree | C ++ implementation of the approach ; Merge procedure to merge two vectors into a single vector ; Final vector to return after merging ; Loop continues until it reaches the end of one of the vectors ; Here , simply add the remaining elements to the vector v ; Procedure to build the segment tree ; Reached the leaf node of the segment tree ; Recursively call the buildTree on both the nodes of the tree ; Storing the final vector after merging the two of its sorted child vector ; Query procedure to get the answer for each query l and r are query range ; out of bound or no overlap ; Complete overlap Query range completely lies in the segment tree node range ; binary search to find index of k ; Partially overlap Query range partially lies in the segment tree node range ; Function to perform the queries ; Driver code ; 1 - based indexing ; Number of queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > merge ( vector < int > & v1 , vector < int > & v2 ) { int i = 0 , j = 0 ; vector < int > v ; while ( i < v1 . size ( ) && j < v2 . size ( ) ) { if ( v1 [ i ] <= v2 [ j ] ) { v . push_back ( v1 [ i ] ) ; i ++ ; } else { v . push_back ( v2 [ j ] ) ; j ++ ; } } for ( int k = i ; k < v1 . size ( ) ; k ++ ) v . push_back ( v1 [ k ] ) ; for ( int k = j ; k < v2 . size ( ) ; k ++ ) v . push_back ( v2 [ k ] ) ; return v ; } void buildTree ( vector < int > * tree , int * arr , int index , int s , int e ) { if ( s == e ) { tree [ index ] . push_back ( arr [ s ] ) ; return ; } int mid = ( s + e ) / 2 ; buildTree ( tree , arr , 2 * index , s , mid ) ; buildTree ( tree , arr , 2 * index + 1 , mid + 1 , e ) ; tree [ index ] = merge ( tree [ 2 * index ] , tree [ 2 * index + 1 ] ) ; } int query ( vector < int > * tree , int index , int s , int e , int l , int r , int k ) { if ( r < s l > e ) return 0 ; if ( s >= l && e <= r ) { return ( tree [ index ] . size ( ) - ( lower_bound ( tree [ index ] . begin ( ) , tree [ index ] . end ( ) , k ) - tree [ index ] . begin ( ) ) ) ; } int mid = ( s + e ) / 2 ; return ( query ( tree , 2 * index , s , mid , l , r , k ) + query ( tree , 2 * index + 1 , mid + 1 , e , l , r , k ) ) ; } void performQueries ( int L [ ] , int R [ ] , int K [ ] , int n , int q , vector < int > tree [ ] ) { for ( int i = 0 ; i < q ; i ++ ) { cout << query ( tree , 1 , 0 , n - 1 , L [ i ] - 1 , R [ i ] - 1 , K [ i ] ) << endl ; } } int main ( ) { int arr [ ] = { 7 , 3 , 9 , 13 , 5 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; vector < int > tree [ 4 * n + 1 ] ; buildTree ( tree , arr , 1 , 0 , n - 1 ) ; int L [ ] = { 1 , 2 } ; int R [ ] = { 4 , 6 } ; int K [ ] = { 6 , 8 } ; int q = sizeof ( L ) / sizeof ( L [ 0 ] ) ; performQueries ( L , R , K , n , q , tree ) ; return 0 ; }
Queries for elements greater than K in the given index range using Segment Tree | C ++ implementation of the approach ; combine function to make parent node ; building the tree ; leaf node ; merging the nodes while backtracking . ; performing query ; Out of Bounds ; completely overlaps ; partially overlaps ; Driver Code ; 1 - based indexing
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > arr ( 1000000 ) , tree ( 4 * arr . size ( ) ) ; int combine ( int a , int b ) { if ( a != 0 && b != 0 ) { return a ; } if ( a >= b ) { return a ; } return b ; } void buildTree ( int ind , int low , int high , int x ) { if ( low == high ) { if ( arr [ low ] > x ) { tree [ ind ] = arr [ low ] ; } else { tree [ ind ] = 0 ; } return ; } int mid = ( low + high ) / 2 ; buildTree ( 2 * ind + 1 , low , mid , x ) ; buildTree ( 2 * ind + 2 , mid + 1 , high , x ) ; tree [ ind ] = combine ( tree [ 2 * ind + 1 ] , tree [ 2 * ind + 2 ] ) ; } int query ( int ind , int low , int high , int l , int r ) { int mid = ( low + high ) / 2 ; if ( low > r high < l ) { return 0 ; } if ( l <= low && r >= high ) { return tree [ ind ] ; } return combine ( query ( 2 * ind + 1 , low , mid , l , r ) , query ( 2 * ind + 2 , mid + 1 , high , l , r ) ) ; } int main ( ) { arr = { 7 , 3 , 9 , 13 , 5 , 4 } ; int n = 6 ; int k = 6 ; int l = 1 , r = 4 ; buildTree ( 0 , 0 , n - 1 , k ) ; cout << query ( 0 , 0 , n - 1 , l - 1 , r - 1 ) ; return 0 ; }
Calculate the Sum of GCD over all subarrays | C ++ program to find Sum of GCD over all subarrays . ; Utility function to calculate sum of gcd of all sub - arrays . ; Fixing the starting index of a subarray ; Fixing the ending index of a subarray ; Finding the GCD of this subarray ; Adding this GCD in our sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findGCDSum ( int n , int a [ ] ) { int GCDSum = 0 ; int tempGCD = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { tempGCD = 0 ; for ( int k = i ; k <= j ; k ++ ) { tempGCD = __gcd ( tempGCD , a [ k ] ) ; } GCDSum += tempGCD ; } } return GCDSum ; } int main ( ) { int n = 5 ; int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; int totalSum = findGCDSum ( n , a ) ; cout << totalSum << " STRNEWLINE " ; }
Check if the array has an element which is equal to XOR of remaining elements | C ++ implementation of the approach ; Function that returns true if the array contains an element which is equal to the XOR of the remaining elements ; To store the XOR of all the array elements ; For every element of the array ; Take the XOR after excluding the current element ; If the XOR of the remaining elements is equal to the current element ; If no such element is found ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool containsElement ( int arr [ ] , int n ) { int xorArr = 0 ; for ( int i = 0 ; i < n ; ++ i ) xorArr ^= arr [ i ] ; for ( int i = 0 ; i < n ; ++ i ) { int x = xorArr ^ arr [ i ] ; if ( arr [ i ] == x ) return true ; } return false ; } int main ( ) { int arr [ ] = { 8 , 2 , 4 , 15 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( containsElement ( arr , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }