codeparrot/xlcost-text-to-code huggingface dataset:
Two player game in which a player can remove all occurrences of a number | C ++ implementation for Two player game in which a player can remove all occurrences of a number ; Function that print whether player1 can wins or loses ; storing the number of occurrence of elements in unordered map ; variable to check if the occurrence of repeated elements is >= 4 and multiple of 2 or not ; count elements which occur more than once ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void game ( int v [ ] , int n ) { unordered_map < int , int > m ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . find ( v [ i ] ) == m . end ( ) ) m [ v [ i ] ] = 1 ; else m [ v [ i ] ] ++ ; } int count = 0 ; int check = 0 ; for ( auto i : m ) { if ( i . second > 1 ) { if ( i . second >= 4 && i . second % 2 == 0 ) check ++ ; count ++ ; } } if ( check % 2 != 0 ) bool flag = false ; if ( check % 2 != 0 ) cout << " Yes " << endl ; else if ( n % 2 == 0 && count % 2 == 0 ) cout << " No " << endl ; else cout << " Yes " << endl ; } int main ( ) { int arr [ ] = { 3 , 2 , 2 , 3 , 3 , 5 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; game ( arr , size ) ; return 0 ; }
Count of elements which form a loop in an Array according to given constraints | C ++ program to number of elements which form a cycle in an array ; Function to count number of elements forming a cycle ; Array to store parent node of traversal . ; Array to determine whether current node is already counted in the cycle . ; Initialize the arrays . ; Check if current node is already traversed or not . If node is not traversed yet then parent value will be - 1. ; Traverse the graph until an already visited node is not found . ; Check parent value to ensure a cycle is present . ; Count number of nodes in the cycle . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define mp  make_pair NEW_LINE #define pb  push_back NEW_LINE #define mod  1000000007 NEW_LINE int solve ( int A [ ] , int n ) { int i , cnt = 0 , j ; int parent [ n ] ; int vis [ n ] ; memset ( parent , -1 , sizeof ( parent ) ) ; memset ( vis , 0 , sizeof ( vis ) ) ; for ( i = 0 ; i < n ; i ++ ) { j = i ; if ( parent [ j ] == -1 ) { while ( parent [ j ] == -1 ) { parent [ j ] = i ; j = __gcd ( j , A [ j ] ) % n ; } if ( parent [ j ] == i ) { while ( ! vis [ j ] ) { vis [ j ] = 1 ; cnt ++ ; j = __gcd ( j , A [ j ] ) % n ; } } } } return cnt ; } int main ( ) { int A [ ] = { 1 , 1 , 6 , 2 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << solve ( A , n ) ; return 0 ; }
Find farthest node from each node in Tree | C ++ implementation to find the farthest node from each vertex of the tree ; Adjacency list to store edges ; Add edge between U and V in tree ; Edge from U to V ; Edge from V to U ; DFS to find the first End Node of diameter ; Calculating level of nodes ; Go in opposite direction of parent ; Function to clear the levels of the nodes ; set all value of lvl [ ] to 0 for next dfs ; Set maximum with 0 ; DFS will calculate second end of the diameter ; Calculating level of nodes ; Store the node with maximum depth from end1 ; Go in opposite direction of parent ; Function to find the distance of the farthest distant node ; Storing distance from end1 to node u ; Function to find the distance of nodes from second end of diameter ; storing distance from end2 to node u ; Joining Edge between two nodes of the tree ; Find the one end of the diameter of tree ; Find the other end of the diameter of tree ; Find the distance to each node from end1 ; Find the distance to each node from end2 ; Comparing distance between the two ends of diameter ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  10000 NEW_LINE vector < int > adj [ N ] ; int lvl [ N ] , dist1 [ N ] , dist2 [ N ] ; void AddEdge ( int u , int v ) { adj [ u ] . push_back ( v ) ; adj [ v ] . push_back ( u ) ; } int end1 , end2 , maxi ; void findFirstEnd ( int u , int p ) { lvl [ u ] = 1 + lvl [ p ] ; if ( lvl [ u ] > maxi ) { maxi = lvl [ u ] ; end1 = u ; } for ( int i = 0 ; i < adj [ u ] . size ( ) ; i ++ ) { if ( adj [ u ] [ i ] != p ) { findFirstEnd ( adj [ u ] [ i ] , u ) ; } } } void clear ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) { lvl [ i ] = 0 ; } maxi = 0 ; dist1 [ 0 ] = dist2 [ 0 ] = -1 ; } void findSecondEnd ( int u , int p ) { lvl [ u ] = 1 + lvl [ p ] ; if ( lvl [ u ] > maxi ) { maxi = lvl [ u ] ; end2 = u ; } for ( int i = 0 ; i < adj [ u ] . size ( ) ; i ++ ) { if ( adj [ u ] [ i ] != p ) { findSecondEnd ( adj [ u ] [ i ] , u ) ; } } } void findDistancefromFirst ( int u , int p ) { dist1 [ u ] = 1 + dist1 [ p ] ; for ( int i = 0 ; i < adj [ u ] . size ( ) ; i ++ ) { if ( adj [ u ] [ i ] != p ) { findDistancefromFirst ( adj [ u ] [ i ] , u ) ; } } } void findDistancefromSecond ( int u , int p ) { dist2 [ u ] = 1 + dist2 [ p ] ; for ( int i = 0 ; i < adj [ u ] . size ( ) ; i ++ ) { if ( adj [ u ] [ i ] != p ) { findDistancefromSecond ( adj [ u ] [ i ] , u ) ; } } } void findNodes ( ) { int n = 5 ; AddEdge ( 1 , 2 ) ; AddEdge ( 1 , 3 ) ; AddEdge ( 3 , 4 ) ; AddEdge ( 3 , 5 ) ; findFirstEnd ( 1 , 0 ) ; clear ( n ) ; findSecondEnd ( end1 , 0 ) ; findDistancefromFirst ( end1 , 0 ) ; findDistancefromSecond ( end2 , 0 ) ; for ( int i = 1 ; i <= n ; i ++ ) { int x = dist1 [ i ] ; int y = dist2 [ i ] ; if ( x >= y ) { cout << end1 << ' ▁ ' ; } else { cout << end2 << ' ▁ ' ; } } } int main ( ) { findNodes ( ) ; return 0 ; }
Sum and Product of all even digit sum Nodes of a Singly Linked List | C ++ program for the above approach ; Node of Linked List ; Function to insert a node at the beginning of the singly Linked List ; Allocate new node ; Insert the data ; Link old list to the new node ; Move head to point the new node ; Function to find the digit sum for a number ; Return the sum ; Function to find the required sum and product ; Initialise the sum and product to 0 and 1 respectively ; Traverse the given linked list ; If current node has even digit sum then include it in resultant sum and product ; Find the sum and the product ; Print the final Sum and Product ; Driver Code ; Head of the linked list ; Create the linked list 15 -> 16 -> 8 -> 6 -> 13 ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * next ; } ; void push ( Node * * head_ref , int new_data ) { Node * new_node = ( Node * ) malloc ( sizeof ( struct Node ) ) ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } int digitSum ( int num ) { int sum = 0 ; while ( num ) { sum += ( num % 10 ) ; num /= 10 ; } return sum ; } void sumAndProduct ( Node * head_ref ) { int prod = 1 ; int sum = 0 ; Node * ptr = head_ref ; while ( ptr != NULL ) { if ( ! ( digitSum ( ptr -> data ) & 1 ) ) { prod *= ptr -> data ; sum += ptr -> data ; } ptr = ptr -> next ; } cout << " Sum ▁ = ▁ " << sum << endl ; cout << " Product ▁ = ▁ " << prod ; } int main ( ) { Node * head = NULL ; push ( & head , 13 ) ; push ( & head , 6 ) ; push ( & head , 8 ) ; push ( & head , 16 ) ; push ( & head , 15 ) ; sumAndProduct ( head ) ; return 0 ; }
Shortest Palindromic Substring | C ++ program to find the shortest palindromic substring ; Function return the shortest palindromic substring ; One by one consider every character as center point of even and length palindromes ; Find the longest odd length palindrome with center point as i ; Find the even length palindrome with center points as i - 1 and i . ; Smallest substring which is not empty ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string ShortestPalindrome ( string s ) { int n = s . length ( ) ; vector < string > v ; for ( int i = 0 ; i < n ; i ++ ) { int l = i ; int r = i ; string ans1 = " " ; string ans2 = " " ; while ( l >= 0 && r < n && s [ l ] == s [ r ] ) { ans1 += s [ l ] ; l -- ; r ++ ; } l = i - 1 ; r = i ; while ( l >= 0 && r < n && s [ l ] == s [ r ] ) { ans2 += s [ l ] ; l -- ; r ++ ; } v . push_back ( ans1 ) ; v . push_back ( ans2 ) ; } string ans = v [ 0 ] ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( v [ i ] != " " ) { ans = min ( ans , v [ i ] ) ; } } return ans ; } int main ( ) { string s = " geeksforgeeks " ; cout << ShortestPalindrome ( s ) ; return 0 ; }
Minimum increment or decrement operations required to make the array sorted | C ++ implementation of the approach ; Function to return the minimum number of given operations required to sort the array ; Number of elements in the array ; Smallest element in the array ; Largest element in the array ; dp ( i , j ) represents the minimum number of operations needed to make the array [ 0 . . i ] sorted in non - decreasing order given that ith element is j ; Fill the dp [ ] ] [ array for base cases ; Using results for the first ( i - 1 ) elements , calculate the result for the ith element ; If the ith element is j then we can have any value from small to j for the i - 1 th element We choose the one that requires the minimum operations ; If we made the ( n - 1 ) th element equal to j we required dp ( n - 1 , j ) operations We choose the minimum among all possible dp ( n - 1 , j ) where j goes from small to large ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMinimumOps ( vector < int > ar ) { int n = ar . size ( ) ; int small = * min_element ( ar . begin ( ) , ar . end ( ) ) ; int large = * max_element ( ar . begin ( ) , ar . end ( ) ) ; int dp [ n ] [ large + 1 ] ; for ( int j = small ; j <= large ; j ++ ) { dp [ 0 ] [ j ] = abs ( ar [ 0 ] - j ) ; } for ( int i = 1 ; i < n ; i ++ ) { int minimum = INT_MAX ; for ( int j = small ; j <= large ; j ++ ) { minimum = min ( minimum , dp [ i - 1 ] [ j ] ) ; dp [ i ] [ j ] = minimum + abs ( ar [ i ] - j ) ; } } int ans = INT_MAX ; for ( int j = small ; j <= large ; j ++ ) { ans = min ( ans , dp [ n - 1 ] [ j ] ) ; } return ans ; } int main ( ) { vector < int > ar = { 1 , 2 , 1 , 4 , 3 } ; cout << getMinimumOps ( ar ) ; return 0 ; }
Minimize the cost of partitioning an array into K groups | C ++ implementation of the approach ; Function to return the minimum number of operations needed to partition the array in k contiguous groups such that all elements of a given group are identical ; n is the size of the array ; dp ( i , j ) represents the minimum cost for partitioning the array [ 0. . i ] into j groups ; Base case , cost is 0 for parititoning the array [ 0. .0 ] into 1 group ; Fill dp ( i , j ) and the answer will be stored at dp ( n - 1 , k ) ; The maximum groups that the segment 0. . i can be divided in is represented by maxGroups ; Initialize dp ( i , j ) to infinity ; Divide segment 0. . i in 1 group ; map and freqOfMode are together used to keep track of the frequency of the most occurring element in [ 0. . i ] ; Change all the elements in the range 0. . i to the most frequent element in this range ; If the jth group is the segment from it . . i , we change all the elements in this range to this range 's most occurring element ; Number of elements we need to change in the jth group i . e . the range it . . i ; For all the possible sizes of the jth group that end at the ith element we pick the size that gives us the minimum cost for dp ( i , j ) elementsToChange is the cost of making all the elements in the jth group identical and we make use of dp ( it - 1 , j - 1 ) to find the overall minimal cost ; Return the minimum cost for partitioning array [ 0. . n - 1 ] into k groups which is stored at dp ( n - 1 , k ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMinimumOps ( vector < int > ar , int k ) { int n = ar . size ( ) ; int dp [ n ] [ k + 1 ] ; dp [ 0 ] [ 1 ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int maxGroups = min ( k , i + 1 ) ; for ( int j = 1 ; j <= maxGroups ; j ++ ) { dp [ i ] [ j ] = INT_MAX ; if ( j == 1 ) { unordered_map < int , int > freq ; int freqOfMode = 0 ; for ( int it = 0 ; it <= i ; it ++ ) { freq [ ar [ it ] ] ++ ; int newElementFreq = freq [ ar [ it ] ] ; if ( newElementFreq > freqOfMode ) freqOfMode = newElementFreq ; } dp [ i ] [ 1 ] = ( i + 1 ) - freqOfMode ; } else { unordered_map < int , int > freq ; int freqOfMode = 0 ; for ( int it = i ; it >= j - 1 ; it -- ) { freq [ ar [ it ] ] ++ ; int newElementFreq = freq [ ar [ it ] ] ; if ( newElementFreq > freqOfMode ) freqOfMode = newElementFreq ; int elementsToChange = i - it + 1 ; elementsToChange -= freqOfMode ; dp [ i ] [ j ] = min ( dp [ it - 1 ] [ j - 1 ] + elementsToChange , dp [ i ] [ j ] ) ; } } } } return dp [ n - 1 ] [ k ] ; } int main ( ) { int k = 3 ; vector < int > ar = { 3 , 1 , 3 , 3 , 2 , 1 , 8 , 5 } ; cout << getMinimumOps ( ar , k ) ; return 0 ; }
Find numbers which are multiples of first array and factors of second array | C ++ implementation of the approach ; Function to return the LCM of two numbers ; Function to print the required numbers ; To store the lcm of array a [ ] elements and the gcd of array b [ ] elements ; Finding LCM of first array ; Finding GCD of second array ; No such element exists ; All the multiples of lcmA which are less than or equal to gcdB and evenly divide gcdB will satisfy the conditions ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lcm ( int x , int y ) { int temp = ( x * y ) / __gcd ( x , y ) ; return temp ; } void findNumbers ( int a [ ] , int n , int b [ ] , int m ) { int lcmA = 1 , gcdB = 0 ; for ( int i = 0 ; i < n ; i ++ ) lcmA = lcm ( lcmA , a [ i ] ) ; for ( int i = 0 ; i < m ; i ++ ) gcdB = __gcd ( gcdB , b [ i ] ) ; if ( gcdB % lcmA != 0 ) { cout << " - 1" ; return ; } int num = lcmA ; while ( num <= gcdB ) { if ( gcdB % num == 0 ) cout << num << " ▁ " ; num += lcmA ; } } int main ( ) { int a [ ] = { 1 , 2 , 2 , 4 } ; int b [ ] = { 16 , 32 , 64 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int m = sizeof ( b ) / sizeof ( b [ 0 ] ) ; findNumbers ( a , n , b , m ) ; return 0 ; }
Probability such that two subset contains same number of elements | C ++ implementation of the above approach ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; Initialize result ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Change x to x ^ 2 ; Function to find probability ; Calculate total possible ways and favourable ways . ; Divide by gcd such that they become relatively coprime ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binomialCoeff ( int n , int k ) { int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } int power ( int x , unsigned int y ) { int res = 1 ; while ( y > 0 ) { if ( y & 1 ) res = res * x ; y = y >> 1 ; x = x * x ; } return res ; } void FindProbability ( int n ) { int up = binomialCoeff ( 2 * n , n ) ; int down = power ( 2 , 2 * n ) ; int g = __gcd ( up , down ) ; up /= g , down /= g ; cout << up << " / " << down << endl ; } int main ( ) { int N = 8 ; FindProbability ( N ) ; return 0 ; }
How to learn Pattern printing easily ? | C ++ implementation of the approach
#include <iostream> NEW_LINE using namespace std ; int main ( ) { int N = 4 , i , j , min ; cout << " Value ▁ of ▁ N : ▁ " << N << endl ; for ( i = 1 ; i <= N ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { min = i < j ? i : j ; cout << N - min + 1 ; } for ( j = N - 1 ; j >= 1 ; j -- ) { min = i < j ? i : j ; cout << N - min + 1 ; } cout << endl ; } for ( i = N - 1 ; i >= 1 ; i -- ) { for ( j = 1 ; j <= N ; j ++ ) { min = i < j ? i : j ; cout << N - min + 1 ; } for ( j = N - 1 ; j >= 1 ; j -- ) { min = i < j ? i : j ; cout << N - min + 1 ; } cout << endl ; } return 0 ; }
Largest perfect square number in an Array | CPP program to find the largest perfect square number among n numbers ; Function to check if a number is perfect square number or not ; takes the sqrt of the number ; checks if it is a perfect square number ; Function to find the largest perfect square number in the array ; stores the maximum of all perfect square numbers ; Traverse all elements in the array ; store the maximum if current element is a perfect square ; Driver Code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; bool checkPerfectSquare ( double n ) { double d = sqrt ( n ) ; if ( d * d == n ) return true ; return false ; } int largestPerfectSquareNumber ( int a [ ] , double n ) { int maxi = -1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( checkPerfectSquare ( a [ i ] ) ) maxi = max ( a [ i ] , maxi ) ; } return maxi ; } int main ( ) { int a [ ] = { 16 , 20 , 25 , 2 , 3 , 10 } ; double n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << largestPerfectSquareNumber ( a , n ) ; return 0 ; }
Maximum number of parallelograms that can be made using the given length of line segments | Function to find the maximum number of parallelograms can be made ; Finding the length of the frequency array ; Increasing the occurrence of each segment ; To store the count of parallelograms ; Counting parallelograms that can be made using 4 similar sides ; counting segments which have occurrence left >= 2 ; Adding parallelograms that can be made using 2 similar sides ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void convert ( int n , int a [ ] ) { int z = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] > z ) z = a [ i ] ; } z = z + 1 ; int ff [ z ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { ff [ a [ i ] ] += 1 ; } int cc = 0 ; for ( int i = 0 ; i < z ; i ++ ) { cc += int ( ff [ i ] / 4 ) ; ff [ i ] = ff [ i ] % 4 ; } int vv = 0 ; for ( int i = 0 ; i < z ; i ++ ) { if ( ff [ i ] >= 2 ) vv += 1 ; } cc += int ( vv / 2 ) ; cout << ( cc ) ; } int main ( ) { int n = 4 ; int a [ ] = { 1 , 2 , 1 , 2 } ; convert ( n , a ) ; }
Count pairs ( i , j ) such that ( i + j ) is divisible by A and B both | C ++ implementation of above approach ; Function to find the LCM ; Function to count the pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find_LCM ( int x , int y ) { return ( x * y ) / __gcd ( x , y ) ; } int CountPairs ( int n , int m , int A , int B ) { int cnt = 0 ; int lcm = find_LCM ( A , B ) ; for ( int i = 1 ; i <= n ; i ++ ) cnt += ( m + ( i % lcm ) ) / lcm ; return cnt ; } int main ( ) { int n = 60 , m = 90 , A = 5 , B = 10 ; cout << CountPairs ( n , m , A , B ) ; return 0 ; }
Sorting without comparison of elements | C ++ program to sort an array without comparison operator . ; Represents node of a doubly linked list ; Count of elements in given range ; Count frequencies of all elements ; Traverse through range . For every element , print it its count times . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sortArr ( int arr [ ] , int n , int min , int max ) { int m = max - min + 1 ; vector < int > c ( m , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) c [ arr [ i ] - min ] ++ ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < c [ i ] ; j ++ ) cout << ( i + min ) << " ▁ " ; } int main ( ) { int arr [ ] = { 10 , 10 , 1 , 4 , 4 , 100 , 0 } ; int min = 0 , max = 100 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortArr ( arr , n , min , max ) ; return 0 ; }
Check if given Array can be divided into subsequences of K increasing consecutive integers | C ++ program for the above approach ; Function to check if the array can be divided into subsequences of K consecutive integers in increasing order ; If N is not divisible by K or K > N , no possible set of required subsequences exist ; Stores the indices of each element in a set ; Stores the index of each number ; Stores if the integer at current index is already included in a subsequence ; Stores the count of total visited elements ; If current integer is already in a subsequence ; Stores the value of last element of the current subsequence ; Stores the index of last element of the current subsequence ; Mark Visited ; Increment the visited count ; Find the next K - 1 elements of the subsequence starting from index i ; No valid index found ; Find index of j such that it is greater than last_index ; if no such index found , return false ; Update last_index ; Mark current index as visited ; Increment total_visited by 1 ; Remove the index from set because it has been already used ; Return the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( vector < int > nums , int K ) { int N = nums . size ( ) ; if ( N % K != 0 K > N ) { return false ; } map < int , set < int > > idx ; for ( int i = 0 ; i < nums . size ( ) ; i ++ ) { idx [ nums [ i ] ] . insert ( i ) ; } int visited [ N ] = { 0 } ; int total_visited = 0 ; for ( int i = 0 ; i < nums . size ( ) ; i ++ ) { if ( visited [ i ] ) { continue ; } int num = nums [ i ] ; int last_index = i ; visited [ i ] = 1 ; total_visited ++ ; for ( int j = num + 1 ; j < num + K ; j ++ ) { if ( idx [ j ] . size ( ) == 0 ) { return false ; } auto it = idx [ j ] . upper_bound ( last_index ) ; if ( it == idx [ j ] . end ( ) * it <= last_index ) { return false ; } last_index = * it ; visited [ last_index ] = 1 ; total_visited ++ ; idx [ j ] . erase ( it ) ; } } return total_visited == N ; } int main ( ) { vector < int > arr = { 4 , 3 , 1 , 2 } ; int K = 2 ; cout << ( isPossible ( arr , K ) ? " Yes " : " No " ) ; return 0 ; }
Minimize the maximum distance between adjacent points after adding K points anywhere in between | C ++ program for the above approach ; Function to check if it is possible to add K points such that the maximum distance between adjacent points is D ; Stores the count of point used ; Iterate over all given points ; Add number of points required to be placed between ith and ( i + 1 ) th point ; Return answer ; Function to find the minimum value of maximum distance between adjacent points after adding K points any where between ; Stores the lower bound and upper bound of the given range ; Perform binary search ; Find the middle value ; Update the current range to lower half ; Update the current range to upper half ; Driver Code
#include <iostream> NEW_LINE using namespace std ; bool isPossible ( double D , int arr [ ] , int N , int K ) { int used = 0 ; for ( int i = 0 ; i < N - 1 ; ++ i ) { used += ( int ) ( ( arr [ i + 1 ] - arr [ i ] ) / D ) ; } return used <= K ; } double minMaxDist ( int stations [ ] , int N , int K ) { double low = 0 , high = 1e8 ; while ( high - low > 1e-6 ) { double mid = ( low + high ) / 2.0 ; if ( isPossible ( mid , stations , N , K ) ) { high = mid ; } else { low = mid ; } } return low ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int K = 9 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minMaxDist ( arr , N , K ) ; return 0 ; }
Find Binary permutations of given size not present in the Array | C ++ program for the above approach ; Function to find a Binary String of same length other than the Strings present in the array ; Map all the strings present in the array ; Find all the substring that can be made ; If num already exists then increase counter ; If not found print ; If all the substrings are present then print - 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMissingBinaryString ( vector < string > & nums , int N ) { unordered_set < string > s ; int counter = 0 ; for ( string str : nums ) { s . insert ( str ) ; } int total = ( int ) pow ( 2 , N ) ; string ans = " " ; for ( int i = 0 ; i < total ; i ++ ) { string num = " " ; for ( int j = N - 1 ; j >= 0 ; j -- ) { if ( i & ( 1 << j ) ) { num += '1' ; } else { num += '0' ; } } if ( s . find ( num ) != s . end ( ) ) { continue ; counter ++ ; } else { cout << num << " , ▁ " ; } } if ( counter == total ) { cout << " - 1" ; } } int main ( ) { int N = 3 ; vector < string > arr = { "101" , "111" , "001" , "011" , "100" , "110" } ; findMissingBinaryString ( arr , N ) ; return 0 ; }