codeparrot/xlcost-text-to-code huggingface dataset:
Minimum cost path from source node to destination node via K intermediate nodes | C ++ program for the above approach ; Function to find the minimum cost path from the source vertex to destination vertex via K intermediate vertices ; Initialize the adjacency list ; Generate the adjacency list ; Initialize the minimum priority queue ; Stores the minimum cost to travel between vertices via K intermediate nodes ; Push the starting vertex , cost to reach and the number of remaining vertices ; Pop the top element of the stack ; If destination is reached ; Return the cost ; If all stops are exhausted ; Find the neighbour with minimum cost ; Pruning ; Update cost ; Update priority queue ; If no path exists ; Driver Code ; Function Call to find the path from src to dist via k nodes having least sum of weights
#include <bits/stdc++.h> NEW_LINE using namespace std ; int leastWeightedSumPath ( int n , vector < vector < int > > & edges , int src , int dst , int K ) { unordered_map < int , vector < pair < int , int > > > graph ; for ( vector < int > & edge : edges ) { graph [ edge [ 0 ] ] . push_back ( make_pair ( edge [ 1 ] , edge [ 2 ] ) ) ; } priority_queue < vector < int > , vector < vector < int > > , greater < vector < int > > > pq ; vector < vector < int > > costs ( n , vector < int > ( K + 2 , INT_MAX ) ) ; costs [ src ] [ K + 1 ] = 0 ; pq . push ( { 0 , src , K + 1 } ) ; while ( ! pq . empty ( ) ) { auto top = pq . top ( ) ; pq . pop ( ) ; if ( top [ 1 ] == dst ) return top [ 0 ] ; if ( top [ 2 ] == 0 ) continue ; for ( auto neighbor : graph [ top [ 1 ] ] ) { if ( costs [ neighbor . first ] [ top [ 2 ] - 1 ] < neighbor . second + top [ 0 ] ) { continue ; } costs [ neighbor . first ] [ top [ 2 ] - 1 ] = neighbor . second + top [ 0 ] ; pq . push ( { neighbor . second + top [ 0 ] , neighbor . first , top [ 2 ] - 1 } ) ; } } return -1 ; } int main ( ) { int n = 3 , src = 0 , dst = 2 , k = 1 ; vector < vector < int > > edges = { { 0 , 1 , 100 } , { 1 , 2 , 100 } , { 0 , 2 , 500 } } ; cout << leastWeightedSumPath ( n , edges , src , dst , k ) ; return 0 ; }
Check if a given string is a comment or not | C ++ program for the above approach ; Function to check if the given string is a comment or not ; If two continuous slashes precedes the comment ; Driver Code ; Given string ; Function call to check whether then given string is a comment or not
#include <bits/stdc++.h> NEW_LINE using namespace std ; void isComment ( string line ) { if ( line [ 0 ] == ' / ' && line [ 1 ] == ' / ' && line [ 2 ] != ' / ' ) { cout << " It ▁ is ▁ a ▁ single - line ▁ comment " ; return ; } if ( line [ line . size ( ) - 2 ] == ' * ' && line [ line . size ( ) - 1 ] == ' / ' && line [ 0 ] == ' / ' && line [ 1 ] == ' * ' ) { cout << " It ▁ is ▁ a ▁ multi - line ▁ comment " ; return ; } cout << " It ▁ is ▁ not ▁ a ▁ comment " ; } int main ( ) { string line = " GeeksForGeeks ▁ GeeksForGeeks " ; isComment ( line ) ; return 0 ; }
Print digits for each array element that does not divide any digit of that element | C ++ program for the above approach ; Function to find digits for each array element that doesn 't divide any digit of the that element ; Traverse the array arr [ ] ; Iterate over the range [ 2 , 9 ] ; Stores if there exists any digit in arr [ i ] which is divisible by j ; If any digit of the number is divisible by j ; If the digit j doesn 't  divide any digit of arr[i] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void indivisibleDigits ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int num = 0 ; cout << arr [ i ] << " : ▁ " ; for ( int j = 2 ; j < 10 ; j ++ ) { int temp = arr [ i ] ; bool flag = true ; while ( temp > 0 ) { if ( ( temp % 10 ) != 0 && ( temp % 10 ) % j == 0 ) { flag = false ; break ; } temp /= 10 ; } if ( flag ) { cout << j << ' ▁ ' ; } } cout << endl ; } } int main ( ) { int arr [ ] = { 4162 , 1152 , 99842 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; indivisibleDigits ( arr , N ) ; return 0 ; }
Length of longest non | C ++ program to implement the above approach ; Function to find the longest non - decreasing subsequence with difference between adjacent elements exactly equal to 1 ; Base case ; Sort the array in ascending order ; Stores the maximum length ; Traverse the array ; If difference between current pair of adjacent elements is 1 or 0 ; Extend the current sequence Update len and max_len ; Otherwise , start a new subsequence ; Print the maximum length ; Driver Code ; Given array ; Size of the array ; Function call to find the longest subsequence
#include <bits/stdc++.h> NEW_LINE using namespace std ; void longestSequence ( int arr [ ] , int N ) { if ( N == 0 ) { cout << 0 ; return ; } sort ( arr , arr + N ) ; int maxLen = 1 ; int len = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] arr [ i ] == arr [ i - 1 ] + 1 ) { len ++ ; maxLen = max ( maxLen , len ) ; } else { len = 1 ; } } cout << maxLen ; } int main ( ) { int arr [ ] = { 8 , 5 , 4 , 8 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; longestSequence ( arr , N ) ; return 0 ; }
Queries to find the minimum array sum possible by removing elements from either end | C ++ program for the above approach ; Function to find the minimum sum for each query after removing elements from either ends ; Traverse the query array ; Traverse the array from the front ; If element equals val , then break out of loop ; Traverse the array from rear ; If element equals val , break ; Print the minimum of the two as the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minSum ( int arr [ ] , int N , int Q [ ] , int M ) { for ( int i = 0 ; i < M ; i ++ ) { int val = Q [ i ] ; int front = 0 , rear = 0 ; for ( int j = 0 ; j < N ; j ++ ) { front += arr [ j ] ; if ( arr [ j ] == val ) { break ; } } for ( int j = N - 1 ; j >= 0 ; j -- ) { rear += arr [ j ] ; if ( arr [ j ] == val ) { break ; } } cout << min ( front , rear ) << " ▁ " ; } } int main ( ) { int arr [ ] = { 2 , 3 , 6 , 7 , 4 , 5 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int Q [ ] = { 7 , 6 } ; int M = sizeof ( Q ) / sizeof ( Q [ 0 ] ) ; minSum ( arr , N , Q , M ) ; return 0 ; }
Frequency of lexicographically Kth smallest character in the a string | C ++ program for the above approach ; Function to find the frequency of the lexicographically Kth smallest character ; Convert the string to array of characters ; Sort the array in ascending order ; Store the Kth character ; Store the frequency of the K - th character ; Count the frequency of the K - th character ; Print the count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void KthCharacter ( string S , int N , int K ) { char strarray [ N + 1 ] ; strcpy ( strarray , S . c_str ( ) ) ; sort ( strarray , strarray + N ) ; char ch = strarray [ K - 1 ] ; int count = 0 ; for ( auto c : strarray ) { if ( c == ch ) count ++ ; } cout << count ; } int main ( ) { string S = " geeksforgeeks " ; int N = S . length ( ) ; int K = 3 ; KthCharacter ( S , N , K ) ; return 0 ; }
Longest substring with no pair of adjacent characters are adjacent English alphabets | C ++ program for the above approach ; Function to find the longest substring satisfying the given condition ; Stores all temporary substrings ; Stores the longest substring ; Stores the length of the substring T ; Stores the first character of string S ; Traverse the string ; If the absolute difference is 1 ; Update the length of substring T ; Update the longest substring ; Otherwise , stores the current character ; Again checking for longest substring and update accordingly ; Print the longest substring ; Driver Code ; Given string ; Function call to find the longest substring satisfying given condition
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSubstring ( string S ) { string T = " " ; string ans = " " ; int l = 0 ; T += S [ 0 ] ; for ( int i = 1 ; i < S . length ( ) ; i ++ ) { if ( abs ( S [ i ] - S [ i - 1 ] ) == 1 ) { l = T . length ( ) ; if ( l > ans . length ( ) ) { ans = T ; } T = " " ; T += S [ i ] ; } else { T += S [ i ] ; } } l = ( int ) T . length ( ) ; if ( l > ( int ) ans . length ( ) ) { ans = T ; } cout << ans << endl ; } int main ( ) { string S = " aabdml " ; findSubstring ( S ) ; return 0 ; }
Minimize length of an array by removing similar subarrays from both ends | C ++ program for the above approach ; Function to minimize length of the array by removing similar subarrays from both ends of the array ; Initialize two pointers ; Stores the current integer ; Check if the elements at both ends are same or not ; Move the front pointer ; Move the rear pointer ; Print the minimized length of the array ; Driver Code ; Input ; Function call to find the minimized length of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinLength ( int arr [ ] , int N ) { int front = 0 , back = N - 1 ; while ( front < back ) { int x = arr [ front ] ; if ( arr [ front ] != arr [ back ] ) break ; while ( arr [ front ] == x && front <= back ) front ++ ; while ( arr [ back ] == x && front <= back ) back -- ; } cout << back - front + 1 << endl ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 3 , 3 , 1 , 2 , 2 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMinLength ( arr , N ) ; return 0 ; }
Digits whose alphabetic representations are jumbled in a given string | C ++ program for the above approach ; Function to convert the jumbled string into digits ; Strings of digits 0 - 9 ; Initialize vector ; Initialize answer ; Size of the string ; Traverse the string ; Update the elements of the vector ; Print the digits into their original format ; Return answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string finddigits ( string s ) { string num [ ] = { " zero " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " } ; vector < int > arr ( 10 ) ; string ans = " " ; int n = s . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' z ' ) arr [ 0 ] ++ ; if ( s [ i ] == ' w ' ) arr [ 2 ] ++ ; if ( s [ i ] == ' g ' ) arr [ 8 ] ++ ; if ( s [ i ] == ' x ' ) arr [ 6 ] ++ ; if ( s [ i ] == ' v ' ) arr [ 5 ] ++ ; if ( s [ i ] == ' o ' ) arr [ 1 ] ++ ; if ( s [ i ] == ' s ' ) arr [ 7 ] ++ ; if ( s [ i ] == ' f ' ) arr [ 4 ] ++ ; if ( s [ i ] == ' h ' ) arr [ 3 ] ++ ; if ( s [ i ] == ' i ' ) arr [ 9 ] ++ ; } arr [ 7 ] -= arr [ 6 ] ; arr [ 5 ] -= arr [ 7 ] ; arr [ 4 ] -= arr [ 5 ] ; arr [ 1 ] -= ( arr [ 2 ] + arr [ 4 ] + arr [ 0 ] ) ; arr [ 3 ] -= arr [ 8 ] ; arr [ 9 ] -= ( arr [ 5 ] + arr [ 6 ] + arr [ 8 ] ) ; for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < arr [ i ] ; j ++ ) { ans += ( char ) ( i + '0' ) ; } } return ans ; } int main ( ) { string s = " owoftnuoer " ; cout << finddigits ( s ) << endl ; }
Check if two binary strings can be made equal by swapping pairs of unequal characters | C ++ Program to implement of above approach ; Function to check if a string s1 can be converted into s2 ; Count of '0' in strings in s1 and s2 ; Iterate both the strings and count the number of occurrences of ; Count is not equal ; Iterating over both the arrays and count the number of occurrences of '0' ; If the count of occurrences of '0' in S2 exceeds that in S1 ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void check ( string s1 , string s2 ) { int s1_0 = 0 , s2_0 = 0 ; for ( int i = 0 ; i < s1 . size ( ) ; i ++ ) { if ( s1 [ i ] == '0' ) { s1_0 ++ ; } if ( s2 [ i ] == '0' ) { s2_0 ++ ; } } if ( s1_0 != s2_0 ) { cout << " NO " << endl ; return ; } else { int Count1 = 0 , Count2 = 0 ; for ( int i = 0 ; i < s1 . size ( ) ; i ++ ) { if ( s1 [ i ] == '0' ) { Count1 ++ ; } if ( s2 [ i ] == '0' ) { Count2 ++ ; } if ( Count1 < Count2 ) { cout << " NO " << endl ; return ; } } cout << " YES " << endl ; } } int main ( ) { string s1 = "100111" ; string s2 = "111010" ; check ( s1 , s2 ) ; s1 = "110100" ; s2 = "010101" ; check ( s1 , s2 ) ; return 0 ; }
Check if two binary strings can be made equal by swapping 1 s occurring before 0 s | C ++ program for the above approach ; Function to check if it is possible to make two binary strings equal by given operations ; Stores count of 1 ' s ▁ and ▁ 0' s of the string str1 ; Stores count of 1 ' s ▁ and ▁ 0' s of the string str2 ; Stores current count of 1 's  presenty in the string str1 ; Count the number of 1 ' s ▁ and ▁ 0' s present in the strings str1 and str2 ; If the number of 1 ' s ▁ and ▁ 0' s are not same of the strings str1 and str2 then print not possible ; Traversing through the strings str1 and str2 ; If the str1 character not equals to str2 character ; Swaps 0 with 1 of the string str1 ; Breaks the loop as the count of 1 's is zero. Hence, no swaps possible ; Swaps 1 with 0 in the string str1 ; Print not possible ; Driver Code ; Given Strings ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void isBinaryStringsEqual ( string str1 , string str2 ) { int str1Zeros = 0 , str1Ones = 0 ; int str2Zeros = 0 , str2Ones = 0 ; int flag = 0 ; int curStr1Ones = 0 ; for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) { if ( str1 [ i ] == '1' ) { str1Ones ++ ; } else if ( str1 [ i ] == '0' ) { str1Zeros ++ ; } if ( str2 [ i ] == '1' ) { str2Ones ++ ; } else if ( str2 [ i ] == '0' ) { str2Zeros ++ ; } } if ( str1Zeros != str2Zeros && str1Ones != str2Ones ) { cout << " Not ▁ Possible " ; } else { for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) { if ( str1 [ i ] != str2 [ i ] ) { if ( str1 [ i ] == '0' && curStr1Ones > 0 ) { str1 [ i ] = '1' ; curStr1Ones -- ; } if ( str1 [ i ] == '0' && curStr1Ones == 0 ) { flag ++ ; break ; } if ( str1 [ i ] == '1' && str2 [ i ] == '0' ) { str1 [ i ] = '0' ; curStr1Ones ++ ; } } } if ( flag == 0 ) { cout << " Possible " ; } else { cout << " Not ▁ Possible " ; } } } int main ( ) { string str1 = "0110" ; string str2 = "0011" ; isBinaryStringsEqual ( str1 , str2 ) ; return 0 ; }
Count array elements whose all distinct digits appear in K | C ++ program to implement the above approach ; Function to the count of array elements whose distinct digits are a subset of the digits of K ; Stores distinct digits of K ; Iterate over all the digits of K ; Insert current digit into set ; Update K ; Stores the count of array elements whose distinct digits are a subset of the digits of K ; Traverse the array , arr [ ] ; Stores current element ; Check if all the digits of arr [ i ] are present in K or not ; Iterate over all the digits of arr [ i ] ; Stores current digit ; If digit not present in the set ; Update flag ; Update no ; If all the digits of arr [ i ] present in set ; Update count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; static int noOfValidKbers ( int K , vector < int > arr ) { map < int , int > set ; while ( K != 0 ) { set [ K % 10 ] = 1 ; K = K / 10 ; } int count = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { int no = arr [ i ] ; bool flag = true ; while ( no != 0 ) { int digit = no % 10 ; if ( set . find ( digit ) == set . end ( ) ) { flag = false ; break ; } no = no / 10 ; } if ( flag == true ) { count ++ ; } } return count ; } int main ( ) { int K = 12 ; vector < int > arr = { 1 , 12 , 1222 , 13 , 2 } ; cout << ( noOfValidKbers ( K , arr ) ) ; }
Queries to find the minimum index in a range [ L , R ] having at least value X with updates | C ++ program for the above approach ; Stores nodes value of the Tree ; Function to build segment tree ; Base Case ; Find the value of mid ; Update for left subtree ; Update for right subtree ; Update the value at the current index ; Function for finding the index of the first element at least x ; If current range does not lie in query range ; If current range is inside of query range ; Maximum value in this range is less than x ; Finding index of first value in this range ; Update the value of the minimum index ; Find mid of the current range ; Left subtree ; If it does not lie in left subtree ; Function for updating segment tree ; Update the value , we reached leaf node ; Find the mid ; If pos lies in the left subtree ; pos lies in the right subtree ; Update the maximum value in the range ; Function to print the answer for the given queries ; Build segment tree ; Find index of first value atleast 2 in range [ 0 , n - 1 ] ; Update value at index 2 to 5 ; Find index of first value atleast 4 in range [ 0 , n - 1 ] ; Find index of first value atleast 0 in range [ 0 , n - 1 ] ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE #define maxN  100 NEW_LINE using namespace std ; int Tree [ 4 * maxN ] ; void build ( int arr [ ] , int index , int s , int e ) { if ( s == e ) Tree [ index ] = arr [ s ] ; else { int m = ( s + e ) / 2 ; build ( arr , 2 * index , s , m ) ; build ( arr , 2 * index + 1 , m + 1 , e ) ; Tree [ index ] = max ( Tree [ 2 * index ] , Tree [ 2 * index + 1 ] ) ; } } int atleast_x ( int index , int s , int e , int ql , int qr , int x ) { if ( ql > e qr < s ) return -1 ; if ( s <= ql && e <= qr ) { if ( Tree [ index ] < x ) return -1 ; while ( s != e ) { int m = ( s + e ) / 2 ; if ( Tree [ 2 * index ] >= x ) { e = m ; index = 2 * index ; } else { s = m + 1 ; index = 2 * index + 1 ; } } return s ; } int m = ( s + e ) / 2 ; int val = atleast_x ( 2 * index , s , m , ql , qr , x ) ; if ( val != -1 ) return val ; return atleast_x ( 2 * index + 1 , m + 1 , e , ql , qr , x ) ; } void update ( int index , int s , int e , int new_val , int pos ) { if ( s == e ) Tree [ index ] = new_val ; else { int m = ( s + e ) / 2 ; if ( pos <= m ) { update ( 2 * index , s , m , new_val , pos ) ; } else { update ( 2 * index + 1 , m + 1 , e , new_val , pos ) ; } Tree [ index ] = max ( Tree [ 2 * index ] , Tree [ 2 * index + 1 ] ) ; } } void printAnswer ( int * arr , int n ) { build ( arr , 1 , 0 , n - 1 ) ; cout << atleast_x ( 1 , 0 , n - 1 , 0 , n - 1 , 2 ) << " STRNEWLINE " ; arr [ 2 ] = 5 ; update ( 1 , 0 , n - 1 , 5 , 2 ) ; cout << atleast_x ( 1 , 0 , n - 1 , 0 , n - 1 , 4 ) << " STRNEWLINE " ; cout << atleast_x ( 1 , 0 , n - 1 , 0 , n - 1 , 0 ) << " STRNEWLINE " ; } int main ( ) { int arr [ ] = { 1 , 3 , 2 , 4 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printAnswer ( arr , N ) ; return 0 ; }
Smallest Subtree with all the Deepest Nodes | C ++ program to implement the above approach ; Structure of a Node ; Function to return depth of the Tree from root ; If current node is a leaf node ; Function to find the root of the smallest subtree consisting of all deepest nodes ; Stores height of left subtree ; Stores height of right subtree ; If height of left subtree exceeds that of the right subtree ; Traverse left subtree ; If height of right subtree exceeds that of the left subtree ; Otherwise ; Return current node ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct TreeNode { int val ; TreeNode * left ; TreeNode * right ; TreeNode ( int data ) { this -> val = data ; left = NULL ; right = NULL ; } } ; int find_ht ( TreeNode * root ) { if ( ! root ) return 0 ; if ( root -> left == NULL && root -> right == NULL ) return 1 ; return max ( find_ht ( root -> left ) , find_ht ( root -> right ) ) + 1 ; } void find_node ( TreeNode * root , TreeNode * & req_node ) { if ( ! root ) return ; int left_ht = find_ht ( root -> left ) ; int right_ht = find_ht ( root -> right ) ; if ( left_ht > right_ht ) { find_node ( root -> left , req_node ) ; } else if ( right_ht > left_ht ) { find_node ( root -> right , req_node ) ; } else { req_node = root ; return ; } } int main ( ) { struct TreeNode * root = new TreeNode ( 1 ) ; root -> left = new TreeNode ( 2 ) ; root -> right = new TreeNode ( 3 ) ; TreeNode * req_node = NULL ; find_node ( root , req_node ) ; cout << req_node -> val ; return 0 ; }
Maximum even numbers present in any subarray of size K | C ++ program to implement the above approach ; Function to find the maximum count of even numbers from all the subarrays of size K ; Stores the maximum count of even numbers from all the subarrays of size K ; Generate all subarrays of size K ; Store count of even numbers in current subarray of size K ; Traverse the current subarray ; If current element is an even number ; Update the answer ; Return answer ; Driver Code ; Size of the input array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxEvenIntegers ( int arr [ ] , int N , int M ) { int ans = 0 ; for ( int i = 0 ; i <= N - M ; i ++ ) { int cnt = 0 ; for ( int j = 0 ; j < M ; j ++ ) { if ( arr [ i + j ] % 2 == 0 ) cnt ++ ; } ans = max ( ans , cnt ) ; } return ans ; } int main ( ) { int arr [ ] = { 2 , 3 , 5 , 4 , 7 , 6 } ; int K = 3 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxEvenIntegers ( arr , N , K ) << endl ; return 0 ; }
Count maximum concatenation of pairs from given array that are divisible by 3 | C ++ program to implement the above approach ; Function to count pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Stores count of array elements whose remainder is 0 by taking modulo by 3 ; Stores count of array elements whose remainder is 1 by taking modulo by 3 ; Stores count of array elements whose remainder is 2 by taking modulo by 3 ; Traverse the array ; Stores sum of digits of arr [ i ] ; Update digitSum ; If remainder of digitSum by by taking modulo 3 is 0 ; Update rem0 ; If remainder of digitSum by by taking modulo 3 is 1 ; Update rem1 ; Update rem2 ; Driver code ; To display the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDiv ( int arr [ ] , int n ) { int rem0 = 0 ; int rem1 = 0 ; int rem2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int digitSum = 0 ; digitSum += arr [ i ] ; if ( digitSum % 3 == 0 ) { rem0 += 1 ; } else if ( digitSum % 3 == 1 ) { rem1 += 1 ; } else { rem2 += 1 ; } } return ( rem0 / 2 + min ( rem1 , rem2 ) ) ; } int main ( ) { int arr [ ] = { 5 , 3 , 2 , 8 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << ( countDiv ( arr , n ) ) ; }
Maximum Sum Subsequence | C ++ program to implement the above approach ; Function to print the maximum non - emepty subsequence sum ; Stores the maximum non - emepty subsequence sum in an array ; Stores the largest element in the array ; Traverse the array ; If a [ i ] is greater than 0 ; Update sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MaxNonEmpSubSeq ( int a [ ] , int n ) { int sum = 0 ; int max = * max_element ( a , a + n ) ; if ( max <= 0 ) { return max ; } for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > 0 ) { sum += a [ i ] ; } } return sum ; } int main ( ) { int arr [ ] = { -2 , 11 , -4 , 2 , -3 , -10 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << MaxNonEmpSubSeq ( arr , N ) ; return 0 ; }
Count numbers up to N having Kth bit set | C ++ program for above approach ; Function to return the count of number of 1 's at ith bit in a range [1, n - 1] ; Store count till nearest power of 2 less than N ; If K - th bit is set in N ; Add to result the nearest power of 2 less than N ; Return result ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long getcount ( long long n , int k ) { long long res = ( n >> ( k + 1 ) ) << k ; if ( ( n >> k ) & 1 ) res += n & ( ( 1ll << k ) - 1 ) ; return res ; } int main ( ) { long long int N = 14 ; int K = 2 ; cout << getcount ( N + 1 , K ) << endl ; return 0 ; }
Check if a string can be split into two substrings such that one substring is a substring of the other | C ++ program to implement the above approach ; Function to check if a string can be divided into two substrings such that one substring is substring of the other ; Store the last character of S ; Traverse the characters at indices [ 0 , N - 2 ] ; Check if the current character is equal to the last character ; If true , set f = 1 ; Break out of the loop ; Driver Code ; Given string , S ; Store the size of S ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void splitString ( string S , int N ) { char c = S [ N - 1 ] ; int f = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( S [ i ] == c ) { f = 1 ; break ; } } if ( f ) cout << " Yes " ; else cout << " No " ; } int main ( ) { string S = " abcdab " ; int N = S . size ( ) ; splitString ( S , N ) ; return 0 ; }