codeparrot/xlcost-text-to-code huggingface dataset:
Maximize median after doing K addition operation on the Array | C ++ program for the above approach ; Function to check operation can be perform or not ; Number of operation to perform s . t . mid is median ; If mid is median of the array ; Function to find max median of the array ; Lowest possible median ; Highest possible median ; Checking for mid is possible for the median of array after doing at most k operation ; Return the max possible ans ; Driver Code ; Given array ; Given number of operation ; Size of array ; Sort the array ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool possible ( int arr [ ] , int N , int mid , int K ) { int add = 0 ; for ( int i = N / 2 - ( N + 1 ) % 2 ; i < N ; ++ i ) { if ( mid - arr [ i ] > 0 ) { add += ( mid - arr [ i ] ) ; if ( add > K ) return false ; } } if ( add <= K ) return true ; else return false ; } int findMaxMedian ( int arr [ ] , int N , int K ) { int low = 1 ; int mx = 0 ; for ( int i = 0 ; i < N ; ++ i ) { mx = max ( mx , arr [ i ] ) ; } long long int high = K + mx ; while ( low <= high ) { int mid = ( high + low ) / 2 ; if ( possible ( arr , N , mid , K ) ) { low = mid + 1 ; } else { high = mid - 1 ; } } if ( N % 2 == 0 ) { if ( low - 1 < arr [ N / 2 ] ) { return ( arr [ N / 2 ] + low - 1 ) / 2 ; } } return low - 1 ; } int main ( ) { int arr [ ] = { 1 , 3 , 6 } ; int K = 10 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sort ( arr , arr + N ) ; cout << findMaxMedian ( arr , N , K ) ; return 0 ; }
Kth Smallest Element of a Matrix of given dimensions filled with product of indices | C ++ program for the above approach ; Function that returns true if the count of elements is less than mid ; To store count of elements less than mid ; Loop through each row ; Count elements less than mid in the ith row ; Function that returns the Kth smallest element in the NxM Matrix after sorting in an array ; Initialize low and high ; Perform binary search ; Find the mid ; Check if the count of elements is less than mid ; Return Kth smallest element of the matrix ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define LL  long long NEW_LINE bool countLessThanMid ( LL mid , LL N , LL M , LL K ) { LL count = 0 ; for ( int i = 1 ; i <= min ( ( LL ) N , mid ) ; ++ i ) { count = count + min ( mid / i , M ) ; } if ( count >= K ) return false ; else return true ; } LL findKthElement ( LL N , LL M , LL K ) { LL low = 1 , high = N * M ; while ( low <= high ) { LL mid = low + ( high - low ) / 2 ; if ( countLessThanMid ( mid , N , M , K ) ) low = mid + 1 ; else high = mid - 1 ; } return high + 1 ; } int main ( ) { LL N = 2 , M = 3 ; LL int K = 5 ; cout << findKthElement ( N , M , K ) << endl ; return 0 ; }
Count of substrings containing only the given character | C ++ program to implement the above approach ; Function that finds the count of substrings containing only character C in the string S ; To store total count of substrings ; To store count of consecutive C 's ; Loop through the string ; Increase the consecutive count of C 's ; Add count of sub - strings from consecutive strings ; Reset the consecutive count of C 's ; Add count of sub - strings from consecutive strings ; Print the count of sub - strings containing only C ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countSubString ( string S , char C ) { int count = 0 ; int conCount = 0 ; for ( char ch : S ) { if ( ch == C ) conCount ++ ; else { count += ( conCount * ( conCount + 1 ) ) / 2 ; conCount = 0 ; } } count += ( conCount * ( conCount + 1 ) ) / 2 ; cout << count ; } int main ( ) { string S = " geeksforgeeks " ; char C = ' e ' ; countSubString ( S , C ) ; return 0 ; }
Check if both halves of a string are Palindrome or not | C ++ Program to check whether both halves of a string is Palindrome or not ; Function to check if both halves of a string are palindrome or not ; Length of string ; Initialize both part as true ; If first half is not palindrome ; If second half is not palindrome ; If both halves are Palindrome ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkPalindrome ( string S ) { int N = S . size ( ) ; bool first_half = true ; bool second_half = true ; int cnt = ( N / 2 ) - 1 ; for ( int i = 0 ; i < ( ( N / 2 ) / 2 ) ; i ++ ) { if ( S [ i ] != S [ cnt ] ) { first_half = false ; break ; } if ( S [ N / 2 + i ] != S [ N / 2 + cnt ] ) { second_half - false ; break ; } cnt -- ; } if ( first_half && second_half ) { cout << " Yes " << endl ; } else { cout << " No " << endl ; } } int main ( ) { string S = " momdad " ; checkPalindrome ( S ) ; return 0 ; }
Count of pairs of strings whose concatenation forms a palindromic string | C ++ Program to find palindromic string ; Stores frequency array and its count ; Total number of pairs ; Initializing array of size 26 to store count of character ; Counting occurrence of each character of current string ; Convert each count to parity ( 0 or 1 ) on the basis of its frequency ; Adding to answer ; Frequency of single character can be possibly changed , so change its parity ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getCount ( int N , vector < string > & s ) { map < vector < int > , int > mp ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { vector < int > a ( 26 , 0 ) ; for ( int j = 0 ; j < s [ i ] . size ( ) ; j ++ ) { a [ s [ i ] [ j ] - ' a ' ] ++ ; } for ( int j = 0 ; j < 26 ; j ++ ) { a [ j ] = a [ j ] % 2 ; } ans += mp [ a ] ; for ( int j = 0 ; j < 26 ; j ++ ) { vector < int > changedCount = a ; if ( a [ j ] == 0 ) changedCount [ j ] = 1 ; else changedCount [ j ] = 0 ; ans += mp [ changedCount ] ; } mp [ a ] ++ ; } return ans ; } int main ( ) { int N = 6 ; vector < string > A = { " aab " , " abcac " , " dffe " , " ed " , " aa " , " aade " } ; cout << getCount ( N , A ) ; return 0 ; }
Kth Smallest element in a Perfect Binary Search Tree | C ++ program to find K - th smallest element in a perfect BST ; A BST node ; A utility function to create a new BST node ; A utility function to insert a new node with given key in BST ; If the tree is empty ; Recur down the left subtree for smaller values ; Recur down the right subtree for smaller values ; Return the ( unchanged ) node pointer ; FUnction to find Kth Smallest element in a perfect BST ; Find the median ( division operation is floored ) ; If the element is at the median ; calculate the number of nodes in the right and left sub - trees ( division operation is floored ) ; If median is located higher ; If median is located lower ; Driver Code ; Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80 / \ / \ / \ / \ 14 25 35 45 55 65 75 85 ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; Node * left , * right ; } ; Node * newNode ( int item ) { Node * temp = new Node ; temp -> key = item ; temp -> left = temp -> right = NULL ; return temp ; } Node * insert ( Node * node , int key ) { if ( node == NULL ) return newNode ( key ) ; if ( key < node -> key ) node -> left = insert ( node -> left , key ) ; else if ( key > node -> key ) node -> right = insert ( node -> right , key ) ; return node ; } bool KSmallestPerfectBST ( Node * root , int k , int treeSize , int & kth_smallest ) { if ( root == NULL ) return false ; int median_loc = ( treeSize / 2 ) + 1 ; if ( k == median_loc ) { kth_smallest = root -> key ; return true ; } int newTreeSize = treeSize / 2 ; if ( k < median_loc ) { return KSmallestPerfectBST ( root -> left , k , newTreeSize , kth_smallest ) ; } int newK = k - median_loc ; return KSmallestPerfectBST ( root -> right , newK , newTreeSize , kth_smallest ) ; } int main ( ) { Node * root = NULL ; root = insert ( root , 50 ) ; insert ( root , 30 ) ; insert ( root , 20 ) ; insert ( root , 40 ) ; insert ( root , 70 ) ; insert ( root , 60 ) ; insert ( root , 80 ) ; insert ( root , 14 ) ; insert ( root , 25 ) ; insert ( root , 35 ) ; insert ( root , 45 ) ; insert ( root , 55 ) ; insert ( root , 65 ) ; insert ( root , 75 ) ; insert ( root , 85 ) ; int n = 15 , k = 5 ; int ans = -1 ; if ( KSmallestPerfectBST ( root , k , n , ans ) ) { cout << ans << " ▁ " ; } return 0 ; }
Check if given Binary string follows then given condition or not | C ++ program for the above approach ; Function to check if the string follows rules or not ; Check for the first condition ; Check for the third condition ; Check for the second condition ; Driver Code ; Given String str ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkrules ( string s ) { if ( s . length ( ) == 0 ) return true ; if ( s [ 0 ] != '1' ) return false ; if ( s . length ( ) > 2 ) { if ( s [ 1 ] == '0' && s [ 2 ] == '0' ) return checkrules ( s . substr ( 3 ) ) ; } return checkrules ( s . substr ( 1 ) ) ; } int main ( ) { string str = "1111" ; if ( checkrules ( str ) ) { cout << " Valid ▁ String " ; } else { cout << " Invalid ▁ String " ; } return 0 ; }
Length of longest subsequence in an Array having all elements as Nude Numbers | C ++ program for the above approach ; Function to check if the number is a Nude number ; Variable initialization ; Integer ' copy ' is converted to a string ; Total digits in the number ; Loop through all digits and check if every digit divides n or not ; flag is used to keep check ; Return true or false as per the condition ; Function to find the longest subsequence which contain all Nude numbers ; Find the length of longest Nude number subsequence ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isNudeNum ( int n ) { int copy , length , flag = 0 ; copy = n ; string temp ; temp = to_string ( copy ) ; length = temp . length ( ) ; for ( int i = 0 ; i < length ; i ++ ) { int num = temp [ i ] - '0' ; if ( num == 0 or n % num != 0 ) { flag = 1 ; } } if ( flag == 1 ) return false ; else return true ; } int longestNudeSubseq ( int arr [ ] , int n ) { int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isNudeNum ( arr [ i ] ) ) answer ++ ; } return answer ; } int main ( ) { int arr [ ] = { 34 , 34 , 2 , 2 , 3 , 333 , 221 , 32 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << longestNudeSubseq ( arr , n ) << endl ; return 0 ; }
Longest subarray with difference exactly K between any two distinct values | C ++ implementation to find the longest subarray consisting of only two values with difference K ; Function to return the length of the longest sub - array ; Initialize set ; Store 1 st element of sub - array into set ; Check absolute difference between two elements ; If the new element is not present in the set ; If the set contains two elements ; Otherwise ; Update the maximum length ; Remove the set elements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubarray ( int arr [ ] , int n , int k ) { int i , j , Max = 1 ; set < int > s ; for ( i = 0 ; i < n - 1 ; i ++ ) { s . insert ( arr [ i ] ) ; for ( j = i + 1 ; j < n ; j ++ ) { if ( abs ( arr [ i ] - arr [ j ] ) == 0 || abs ( arr [ i ] - arr [ j ] ) == k ) { if ( ! s . count ( arr [ j ] ) ) { if ( s . size ( ) == 2 ) break ; else s . insert ( arr [ j ] ) ; } } else break ; } if ( s . size ( ) == 2 ) { Max = max ( Max , j - i ) ; s . clear ( ) ; } else s . clear ( ) ; } return Max ; } int main ( ) { int arr [ ] = { 1 , 0 , 2 , 2 , 5 , 5 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 1 ; int length = longestSubarray ( arr , N , K ) ; if ( length == 1 ) cout << -1 ; else cout << length ; return 0 ; }
Longest subarray of non | C ++ program for the above approach ; Function to find the maximum length of a subarray of 1 s after removing at most one 0 ; Stores the count of consecutive 1 's from left ; Stores the count of consecutive 1 's from right ; Traverse left to right ; If cell is non - empty ; Increase count ; If cell is empty ; Store the count of consecutive 1 's  till (i - 1)-th index ; Traverse from right to left ; Store the count of consecutive 1 s till ( i + 1 ) - th index ; Stores the length of longest subarray ; Store the maximum ; If array a contains only 1 s return n else return ans ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubarray ( int a [ ] , int n ) { int l [ n ] ; int r [ n ] ; for ( int i = 0 , count = 0 ; i < n ; i ++ ) { if ( a [ i ] == 1 ) count ++ ; else { l [ i ] = count ; count = 0 ; } } for ( int i = n - 1 , count = 0 ; i >= 0 ; i -- ) { if ( a [ i ] == 1 ) count ++ ; else { r [ i ] = count ; count = 0 ; } } int ans = -1 ; for ( int i = 0 ; i < n ; ++ i ) { if ( a [ i ] == 0 ) ans = max ( ans , l [ i ] + r [ i ] ) ; } return ans < 0 ? n : ans ; } int main ( ) { int arr [ ] = { 0 , 1 , 1 , 1 , 0 , 1 , 0 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << longestSubarray ( arr , n ) ; return 0 ; }
Find largest factor of N such that N / F is less than K | C ++ program for the above approach ; Function to find the value of X ; Loop to check all the numbers divisible by N that yield minimum N / i value ; Print the value of packages ; Driver Code ; Given N and K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMaxValue ( int N , int K ) { int packages ; int maxi = 1 ; for ( int i = 1 ; i <= K ; i ++ ) { if ( N % i == 0 ) maxi = max ( maxi , i ) ; } packages = N / maxi ; cout << packages << endl ; } int main ( ) { int N = 8 , K = 7 ; findMaxValue ( N , K ) ; return 0 ; }
Largest square which can be formed using given rectangular blocks | C ++ program for the above approach ; Function to find maximum side length of square ; Sort array in asc order ; Traverse array in desc order ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxSide ( int a [ ] , int n ) { int sideLength = 0 ; sort ( a , a + n ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( a [ i ] > sideLength ) { sideLength ++ ; } else { break ; } } cout << sideLength << endl ; } int main ( ) { int N = 6 ; int arr [ ] = { 3 , 2 , 1 , 5 , 2 , 4 } ; maxSide ( arr , N ) ; return 0 ; }
Count of subtrees from an N | C ++ program for the above approach ; Function to implement DFS traversal ; Mark node v as visited ; Traverse Adj_List of node v ; If current node is not visited ; DFS call for current node ; Count the total red and blue nodes of children of its subtree ; Count the no . of red and blue nodes in the subtree ; If subtree contains all red node & no blue node ; If subtree contains all blue node & no red node ; Function to count the number of nodes with red color ; Function to count the number of nodes with blue color ; Function to create a Tree with given vertices ; Traverse the edge [ ] array ; Create adjacency list ; Function to count the number of subtree with the given condition ; For creating adjacency list ; To store the count of subtree with only blue and red color ; visited array for DFS Traversal ; Count the number of red node in the tree ; Count the number of blue node in the tree ; Function Call to build tree ; DFS Traversal ; Print the final count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Solution_dfs ( int v , int color [ ] , int red , int blue , int * sub_red , int * sub_blue , int * vis , map < int , vector < int > > & adj , int * ans ) { vis [ v ] = 1 ; for ( int i = 0 ; i < adj [ v ] . size ( ) ; i ++ ) { if ( vis [ adj [ v ] [ i ] ] == 0 ) { Solution_dfs ( adj [ v ] [ i ] , color , red , blue , sub_red , sub_blue , vis , adj , ans ) ; sub_red [ v ] += sub_red [ adj [ v ] [ i ] ] ; sub_blue [ v ] += sub_blue [ adj [ v ] [ i ] ] ; } } if ( color [ v ] == 1 ) { sub_red [ v ] ++ ; } if ( color [ v ] == 2 ) { sub_blue [ v ] ++ ; } if ( sub_red [ v ] == red && sub_blue [ v ] == 0 ) { ( * ans ) ++ ; } if ( sub_red [ v ] == 0 && sub_blue [ v ] == blue ) { ( * ans ) ++ ; } } int countRed ( int color [ ] , int n ) { int red = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( color [ i ] == 1 ) red ++ ; } return red ; } int countBlue ( int color [ ] , int n ) { int blue = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( color [ i ] == 2 ) blue ++ ; } return blue ; } void buildTree ( int edge [ ] [ 2 ] , map < int , vector < int > > & m , int n ) { int u , v , i ; for ( i = 0 ; i < n - 1 ; i ++ ) { u = edge [ i ] [ 0 ] - 1 ; v = edge [ i ] [ 1 ] - 1 ; m [ u ] . push_back ( v ) ; m [ v ] . push_back ( u ) ; } } void countSubtree ( int color [ ] , int n , int edge [ ] [ 2 ] ) { map < int , vector < int > > adj ; int ans = 0 ; int sub_red [ n + 3 ] = { 0 } ; int sub_blue [ n + 3 ] = { 0 } ; int vis [ n + 3 ] = { 0 } ; int red = countRed ( color , n ) ; int blue = countBlue ( color , n ) ; buildTree ( edge , adj , n ) ; Solution_dfs ( 0 , color , red , blue , sub_red , sub_blue , vis , adj , & ans ) ; cout << ans ; } int main ( ) { int N = 5 ; int color [ ] = { 1 , 0 , 0 , 0 , 2 } ; int edge [ ] [ 2 ] = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } , { 4 , 5 } } ; countSubtree ( color , N , edge ) ; return 0 ; }
Minimize difference after changing all odd elements to even | C ++ program for the above approach ; Function to minimize the difference between two elements of array ; Find all the element which are possible by multiplying 2 to odd numbers ; Sort the array ; Find the minimum difference Iterate and find which adjacent elements have the minimum difference ; Print the minimum difference ; Driver Code ; Given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE void minDiff ( vector < ll > a , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) a . push_back ( a [ i ] * 2 ) ; } sort ( a . begin ( ) , a . end ( ) ) ; ll mindifference = a [ 1 ] - a [ 0 ] ; for ( int i = 1 ; i < a . size ( ) ; i ++ ) { mindifference = min ( mindifference , a [ i ] - a [ i - 1 ] ) ; } cout << mindifference << endl ; } int main ( ) { vector < ll > arr = { 3 , 8 , 13 , 30 , 50 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minDiff ( arr , n ) ; return 0 ; }
Longest subarray having sum K | Set 2 | C ++ program for the above approach ; To store the prefix sum array ; Function for searching the lower bound of the subarray ; Iterate until low less than equal to high ; For each mid finding sum of sub array less than or equal to k ; Return the final answer ; Function to find the length of subarray with sum K ; Initialize sum to 0 ; Push the prefix sum of the array arr [ ] in prefix [ ] ; Search r for each i ; Update ans ; Print the length of subarray found in the array ; Driver Code ; Given array arr [ ] ; Given sum K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > v ; int bin ( int val , int k , int n ) { int lo = 0 ; int hi = n ; int mid ; int ans = -1 ; while ( lo <= hi ) { mid = lo + ( hi - lo ) / 2 ; if ( v [ mid ] - val <= k ) { lo = mid + 1 ; ans = mid ; } else hi = mid - 1 ; } return ans ; } void findSubarraySumK ( int arr [ ] , int N , int K ) { int sum = 0 ; v . push_back ( 0 ) ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; v . push_back ( sum ) ; } int l = 0 , ans = 0 , r ; for ( int i = 0 ; i < N ; i ++ ) { r = bin ( v [ i ] , K , N ) ; ans = max ( ans , r - i ) ; } cout << ans ; } int main ( ) { int arr [ ] = { 6 , 8 , 14 , 9 , 4 , 11 , 10 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 13 ; findSubarraySumK ( arr , N , K ) ; return 0 ; }
Minimum number of reversals to reach node 0 from every other node | C ++ program for the above approach ; Function to find minimum reversals ; Add all adjacent nodes to the node in the graph ; Insert edges in the graph ; Insert edges in the reversed graph ; Create array visited to mark all the visited nodes ; Stores the number of edges to be reversed ; BFS Traversal ; Pop the current node from the queue ; mark the current node visited ; Intitialize count of edges need to be reversed to 0 ; Push adjacent nodes in the reversed graph to the queue , if not visited ; Push adjacent nodes in graph to the queue , if not visited count the number of nodes added to the queue ; Update the reverse edge to the final count ; Return the result ; Driver Code ; Given edges to the graph ; Number of nodes ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minRev ( vector < vector < int > > edges , int n ) { unordered_map < int , vector < int > > graph ; unordered_map < int , vector < int > > graph_rev ; for ( int i = 0 ; i < edges . size ( ) ; i ++ ) { int x = edges [ i ] [ 0 ] ; int y = edges [ i ] [ 1 ] ; graph [ x ] . push_back ( y ) ; graph_rev [ y ] . push_back ( x ) ; } queue < int > q ; vector < int > visited ( n , 0 ) ; q . push ( 0 ) ; int ans = 0 ; while ( ! q . empty ( ) ) { int curr = q . front ( ) ; visited [ curr ] = 1 ; int count = 0 ; q . pop ( ) ; for ( int i = 0 ; i < graph_rev [ curr ] . size ( ) ; i ++ ) { if ( ! visited [ graph_rev [ curr ] [ i ] ] ) { q . push ( graph_rev [ curr ] [ i ] ) ; } } for ( int i = 0 ; i < graph [ curr ] . size ( ) ; i ++ ) { if ( ! visited [ graph [ curr ] [ i ] ] ) { q . push ( graph [ curr ] [ i ] ) ; count ++ ; } } ans += count ; } return ans ; } int main ( ) { vector < vector < int > > edges ; edges = { { 0 , 1 } , { 1 , 3 } , { 2 , 3 } , { 4 , 0 } , { 4 , 5 } } ; int n = 6 ; cout << minRev ( edges , n ) ; return 0 ; }
Partition a set into two subsets such that difference between max of one and min of other is minimized | C ++ program for the above approach ; Function to split the array ; Sort the array in increasing order ; Calculating the max difference between consecutive elements ; Return the final minimum difference ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int splitArray ( int arr [ ] , int N ) { sort ( arr , arr + N ) ; int result = INT_MAX ; for ( int i = 1 ; i < N ; i ++ ) { result = min ( result , arr [ i ] - arr [ i - 1 ] ) ; } return result ; } int main ( ) { int arr [ ] = { 3 , 1 , 2 , 6 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << splitArray ( arr , N ) ; return 0 ; }
Find the element at R ' th ▁ row ▁ and ▁ C ' th column in given a 2D pattern | C ++ implementation to compute the R ' th ▁ row ▁ and ▁ C ' th column of the given pattern ; Function to compute the R ' th ▁ row ▁ and ▁ C ' th column of the given pattern ; First element of a given row ; Element in the given column ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findValue ( int R , int C ) { int k = ( R * ( R - 1 ) ) / 2 + 1 ; int diff = R + 1 ; for ( int i = 1 ; i < C ; i ++ ) { k = ( k + diff ) ; diff ++ ; } return k ; } int main ( ) { int R = 4 ; int C = 4 ; int k = findValue ( R , C ) ; cout << k ; return 0 ; }
Maximize number of groups formed with size not smaller than its largest element | C ++ implementation of above approach ; Function that prints the number of maximum groups ; Store the number of occurrence of elements ; Make all groups of similar elements and store the left numbers ; Condition for finding first leftover element ; Condition for current leftover element ; Condition if group size is equal to or more than current element ; Printing maximum number of groups ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void makeGroups ( int a [ ] , int n ) { vector < int > v ( n + 1 , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { v [ a [ i ] ] ++ ; } int no_of_groups = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { no_of_groups += v [ i ] / i ; v [ i ] = v [ i ] % i ; } int i = 1 ; int total = 0 ; for ( i = 1 ; i <= n ; i ++ ) { if ( v [ i ] != 0 ) { total = v [ i ] ; break ; } } i ++ ; while ( i <= n ) { if ( v [ i ] != 0 ) { total += v [ i ] ; if ( total >= i ) { int rem = total - i ; no_of_groups ++ ; total = rem ; } } i ++ ; } cout << no_of_groups << " STRNEWLINE " ; } int main ( ) { int arr [ ] = { 2 , 3 , 1 , 2 , 2 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; makeGroups ( arr , size ) ; return 0 ; }