codeparrot/xlcost-text-to-code huggingface dataset:
Count of ways to select K consecutive empty cells from a given Matrix | C ++ program to find no of ways to select K consecutive empty cells from a row or column ; Function to Traverse the matrix row wise ; Initialize ans ; Traverse row wise ; Initialize no of consecutive empty cells ; Check if blocked cell is encountered then reset countcons to 0 ; Check if empty cell is encountered , then increment countcons ; Check if number of empty consecutive cells is greater or equal to K , increment the ans ; Return the count ; Function to Traverse the matrix column wise ; Initialize ans ; Traverse column wise ; Initialize no of consecutive empty cells ; Check if blocked cell is encountered then reset countcons to 0 ; Check if empty cell is encountered , increment countcons ; Check if number of empty consecutive cells is greater than or equal to K , increment the ans ; Return the count ; Driver Code ; If k = 1 only traverse row wise ; Traverse both row and column wise
#include <bits/stdc++.h> NEW_LINE using namespace std ; int rowWise ( char * v , int n , int m , int k ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int countcons = 0 ; for ( int j = 0 ; j < m ; j ++ ) { if ( * ( v + i * m + j ) == '1' ) { countcons = 0 ; } else { countcons ++ ; } if ( countcons >= k ) { ans ++ ; } } } return ans ; } int colWise ( char * v , int n , int m , int k ) { int ans = 0 ; for ( int i = 0 ; i < m ; i ++ ) { int countcons = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( * ( v + j * n + i ) == '1' ) { countcons = 0 ; } else { countcons ++ ; } if ( countcons >= k ) { ans ++ ; } } } return ans ; } int main ( ) { int n = 3 , m = 3 , k = 1 ; char v [ n ] [ m ] = { '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' } ; if ( k == 1 ) { cout << rowWise ( v [ 0 ] , n , m , k ) ; } else { cout << colWise ( v [ 0 ] , n , m , k ) + rowWise ( v [ 0 ] , n , m , k ) ; } return 0 ; }
Check if the count of inversions of two given types on an Array are equal or not | C ++ Program to implement the above approach ; Function to check if the count of inversion of two types are same or not ; If maximum value is found to be greater than a [ j ] , then that pair of indices ( i , j ) will add extra value to inversion of Type 1 ; Update max ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool solve ( int a [ ] , int n ) { int mx = INT_MIN ; for ( int j = 1 ; j < n ; j ++ ) { if ( mx > a [ j ] ) return false ; mx = max ( mx , a [ j - 1 ] ) ; } return true ; } int main ( ) { int a [ ] = { 1 , 0 , 2 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; bool possible = solve ( a , n ) ; if ( possible ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
How to validate an IP address using ReGex | C ++ program to validate IP address using Regex ; Function for Validating IP ; Regex expression for validating IPv4 ; Regex expression for validating IPv6 ; Checking if it is a valid IPv4 addresses ; Checking if it is a valid IPv6 addresses ; Return Invalid ; Driver Code ; IP addresses to validate
#include <bits/stdc++.h> NEW_LINE using namespace std ; string Validate_It ( string IP ) { regex ipv4 ( " ( ( [ 0-9 ] ▁ [ 1-9 ] [ 0-9 ] ▁ 1[0-9 ] [ 0-9 ] ▁ 2[0-4 ] [ 0-9 ] ▁ 25[0-5 ] ) \\ . ) { 3 } ( [0-9 ] ▁ [ 1-9 ] [ 0-9 ] ▁ 1[0-9 ] [ 0-9 ] ▁ 2[0-4 ] [ 0-9 ] ▁ 25[0-5 ] ) " ) ; regex ipv6 ( " ( ( ( [0-9a - fA - F ] ) { 1,4 } ) \\ : ) { 7 } ( [0-9a - fA - F ] ) { 1,4 } " ) ; if ( regex_match ( IP , ipv4 ) ) return " Valid ▁ IPv4" ; else if ( regex_match ( IP , ipv6 ) ) return " Valid ▁ IPv6" ; return " Invalid ▁ IP " ; } int main ( ) { string IP = "203.120.223.13" ; cout << Validate_It ( IP ) << endl ; IP = " fffe : 3465 : efab : 23fe : 2235:6565 : aaab : 0001" ; cout << Validate_It ( IP ) << endl ; IP = "2F33:12a0:3Ea0:0302" ; cout << Validate_It ( IP ) << endl ; return 0 ; }
Count of prime factors of N to be added at each step to convert N to M | C ++ program to find the minimum steps required to convert a number N to M . ; Array to store shortest prime factor of every integer ; Function to precompute shortest prime factors ; Function to insert distinct prime factors of every integer into a set ; Store distinct prime factors ; Function to return minimum steps using BFS ; Queue of pairs to store the current number and distance from root . ; Set to store distinct prime factors ; Run BFS ; Find out the prime factors of newNum ; Iterate over every prime factor of newNum . ; If M is obtained ; Return number of operations ; If M is exceeded ; Otherwise ; Update and store the new number obtained by prime factor ; If M cannot be obtained ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int spf [ 100009 ] ; void sieve ( ) { memset ( spf , -1 , 100005 ) ; for ( int i = 2 ; i * i <= 100005 ; i ++ ) { for ( int j = i ; j <= 100005 ; j += i ) { if ( spf [ j ] == -1 ) { spf [ j ] = i ; } } } } set < int > findPrimeFactors ( set < int > s , int n ) { while ( n > 1 ) { s . insert ( spf [ n ] ) ; n /= spf [ n ] ; } return s ; } int MinimumSteps ( int n , int m ) { queue < pair < int , int > > q ; set < int > s ; q . push ( { n , 0 } ) ; while ( ! q . empty ( ) ) { int newNum = q . front ( ) . first ; int distance = q . front ( ) . second ; q . pop ( ) ; set < int > k = findPrimeFactors ( s , newNum ) ; for ( auto i : k ) { if ( newNum == m ) { return distance ; } else if ( newNum > m ) { break ; } else { q . push ( { newNum + i , distance + 1 } ) ; } } } return -1 ; } int main ( ) { int N = 7 , M = 16 ; sieve ( ) ; cout << MinimumSteps ( N , M ) ; }
Count of elements which is product of a pair or an element square | C ++ Program to implement the above approach ; Stores all factors a number ; Function to calculate and store in a vector ; Function to return the count of array elements which are a product of two array elements ; Copy elements into a a duplicate array ; Sort the duplicate array ; Store the count of elements ; If the factors are not calculated already ; Traverse its factors ; If a pair of factors is found ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > v [ 100000 ] ; void div ( int n ) { for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v [ n ] . push_back ( i ) ; } } } int prodof2elements ( int arr [ ] , int n ) { int arr2 [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr2 [ i ] = arr [ i ] ; } sort ( arr2 , arr2 + n ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( v [ arr [ i ] ] . size ( ) == 0 ) div ( arr [ i ] ) ; for ( auto j : v [ arr [ i ] ] ) { if ( binary_search ( arr2 , arr2 + n , j ) and binary_search ( arr2 , arr2 + n , arr [ i ] / j ) ) { ans ++ ; break ; } } } return ans ; } int main ( ) { int arr [ ] = { 2 , 1 , 8 , 4 , 32 , 18 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << prodof2elements ( arr , N ) ; return 0 ; }
Largest subarray with frequency of all elements same | C ++ program for the above approach ; Function to find maximum subarray size ; Generating all subarray i -> starting index j -> end index ; Map 1 to hash frequency of all elements in subarray ; Map 2 to hash frequency of all frequencies of elements ; ele_count is the previous frequency of arr [ j ] ; Finding previous frequency of arr [ j ] in map 1 ; Increasing frequency of arr [ j ] by 1 ; Check if previous frequency is present in map 2 ; Delete previous frequency if hash is equal to 1 ; Decrement the hash of previous frequency ; Incrementing hash of new frequency in map 2 ; Check if map2 size is 1 and updating answer ; Return the maximum size of subarray ; Driver Code ; Given array arr [ ] ; Function Call
#include <iostream> NEW_LINE #include <unordered_map> NEW_LINE using namespace std ; int max_subarray_size ( int N , int arr [ ] ) { int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { unordered_map < int , int > map1 ; unordered_map < int , int > map2 ; for ( int j = i ; j < N ; j ++ ) { int ele_count ; if ( map1 . find ( arr [ j ] ) == map1 . end ( ) ) { ele_count = 0 ; } else { ele_count = map1 [ arr [ j ] ] ; } map1 [ arr [ j ] ] ++ ; if ( map2 . find ( ele_count ) != map2 . end ( ) ) { if ( map2 [ ele_count ] == 1 ) { map2 . erase ( ele_count ) ; } else { map2 [ ele_count ] -- ; } } map2 [ ele_count + 1 ] ++ ; if ( map2 . size ( ) == 1 ) ans = max ( ans , j - i + 1 ) ; } } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 2 , 5 , 6 , 5 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << max_subarray_size ( N , arr ) ; return 0 ; }
Longest substring consisting of vowels using Binary Search | C ++ implementation of the above approach ; Function to check if a character is vowel or not ; 0 - a 1 - b 2 - c and so on 25 - z ; Function to check if any substring of length k exists which contains only vowels ; Applying sliding window to get all substrings of length K ; Remove the occurence of ( i - k + 1 ) th character ; Function to perform Binary Search ; Doing binary search on the lengths ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool vowel ( int vo ) { if ( vo == 0 vo == 4 vo == 8 vo == 14 vo == 20 ) return true ; else return false ; } bool check ( string s , int k ) { vector < int > cnt ( 26 , 0 ) ; for ( int i = 0 ; i < k - 1 ; i ++ ) { cnt [ s [ i ] - ' a ' ] ++ ; } for ( int i = k - 1 ; i < s . size ( ) ; i ++ ) { cnt [ s [ i ] - ' a ' ] ++ ; int flag1 = 0 ; for ( int j = 0 ; j < 26 ; j ++ ) { if ( vowel ( j ) == false && cnt [ j ] > 0 ) { flag1 = 1 ; break ; } } if ( flag1 == 0 ) return true ; cnt [ s [ i - k + 1 ] - ' a ' ] -- ; } return false ; } int longestSubstring ( string s ) { int l = 1 , r = s . size ( ) ; int maxi = 0 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; if ( check ( s , mid ) ) { l = mid + 1 ; maxi = max ( maxi , mid ) ; } else r = mid - 1 ; } return maxi ; } int main ( ) { string s = " sedrewaefhoiu " ; cout << longestSubstring ( s ) ; return 0 ; }
Find number of pairs ( x , y ) in an Array such that x ^ y > y ^ x | Set 2 | C ++ program to finds the number of pairs ( x , y ) from X [ ] and Y [ ] such that x ^ y > y ^ x ; Function to return the count of pairs ; Compute suffix sums till i = 3 ; Base Case : x = 0 ; No valid pairs ; Base Case : x = 1 ; Store the count of 0 's ; Base Case : x = 2 ; Store suffix sum upto 5 ; Base Case : x = 3 ; Store count of 2 and suffix sum upto 4 ; For all other values of x ; For all x >= 2 , every y = 0 and every y = 1 makes a valid pair ; Return the count of pairs ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int X [ ] , int Y [ ] , int m , int n ) { vector < int > suffix ( 1005 ) ; long long total_pairs = 0 ; for ( int i = 0 ; i < n ; i ++ ) suffix [ Y [ i ] ] ++ ; for ( int i = 1e3 ; i >= 3 ; i -- ) suffix [ i ] += suffix [ i + 1 ] ; for ( int i = 0 ; i < m ; i ++ ) { if ( X [ i ] == 0 ) continue ; else if ( X [ i ] == 1 ) { total_pairs += suffix [ 0 ] ; continue ; } else if ( X [ i ] == 2 ) total_pairs += suffix [ 5 ] ; else if ( X [ i ] == 3 ) total_pairs += suffix [ 2 ] + suffix [ 4 ] ; else total_pairs += suffix [ X [ i ] + 1 ] ; total_pairs += suffix [ 0 ] + suffix [ 1 ] ; } return total_pairs ; } int main ( ) { int X [ ] = { 10 , 19 , 18 } ; int Y [ ] = { 11 , 15 , 9 } ; int m = sizeof ( X ) / sizeof ( X [ 0 ] ) ; int n = sizeof ( Y ) / sizeof ( Y [ 0 ] ) ; cout << countPairs ( X , Y , m , n ) ; return 0 ; }
Min number of moves to traverse entire Matrix through connected cells with equal values | C ++ program to find the minimum number of moves to traverse a given matrix ; Function to find the minimum number of moves to traverse the given matrix ; Constructing another matrix consisting of distinct values ; Updating the array B by checking the values of A that if there are same values connected through an edge or not ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Store all distinct elements in a set ; Return answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int A [ ] [ 10 ] , int N , int M ) { int B [ N ] [ M ] ; int c = 1 ; set < int > s ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) B [ i ] [ j ] = c ++ ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( i != 0 ) { if ( A [ i - 1 ] [ j ] == A [ i ] [ j ] ) B [ i - 1 ] [ j ] = B [ i ] [ j ] ; } if ( i != N - 1 ) { if ( A [ i + 1 ] [ j ] == A [ i ] [ j ] ) B [ i + 1 ] [ j ] = B [ i ] [ j ] ; } if ( j != 0 ) { if ( A [ i ] [ j - 1 ] == A [ i ] [ j ] ) B [ i ] [ j - 1 ] = B [ i ] [ j ] ; } if ( j != M - 1 ) { if ( A [ i ] [ j + 1 ] == A [ i ] [ j ] ) B [ i ] [ j + 1 ] = B [ i ] [ j ] ; } } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) s . insert ( B [ i ] [ j ] ) ; } return s . size ( ) ; } int main ( ) { int N = 2 , M = 3 ; int A [ ] [ 10 ] = { { 2 , 1 , 3 } , { 1 , 1 , 2 } } ; cout << solve ( A , N , M ) ; }
Number of times an array can be partitioned repetitively into two subarrays with equal sum | C ++ program for the above approach ; Recursion Function to calculate the possible splitting ; If there are less than two elements , we cannot partition the sub - array . ; Iterate from the start to end - 1. ; Recursive call to the left and the right sub - array . ; If there is no such partition , then return 0 ; Function to find the total splitting ; Prefix array to store the prefix - sum using 1 based indexing ; Store the prefix - sum ; Function Call to count the number of splitting ; Driver Code ; Given array ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int splitArray ( int start , int end , int * arr , int * prefix_sum ) { if ( start >= end ) return 0 ; for ( int k = start ; k < end ; ++ k ) { if ( ( prefix_sum [ k ] - prefix_sum [ start - 1 ] ) == ( prefix_sum [ end ] - prefix_sum [ k ] ) ) { return 1 + splitArray ( start , k , arr , prefix_sum ) + splitArray ( k + 1 , end , arr , prefix_sum ) ; } } return 0 ; } void solve ( int arr [ ] , int n ) { int prefix_sum [ n + 1 ] ; prefix_sum [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; ++ i ) { prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i - 1 ] ; } cout << splitArray ( 1 , n , arr , prefix_sum ) ; } int main ( ) { int arr [ ] = { 12 , 3 , 3 , 0 , 3 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; solve ( arr , N ) ; return 0 ; }
Minimise the maximum element of N subarrays of size K | C ++ implementation to choose N subarrays of size K such that the maximum element of subarrays is minimum ; Function to choose N subarrays of size K such that the maximum element of subarrays is minimum ; Condition to check if it is not possible to choose k sized N subarrays ; Using binary search ; calculating mid ; Loop to find the count of the K sized subarrays possible with elements less than mid ; Condition to check if the answer is in right subarray ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minDays ( vector < int > & arr , int n , int k ) { int l = arr . size ( ) , left = 1 , right = 1e9 ; if ( n * k > l ) return -1 ; while ( left < right ) { int mid = ( left + right ) / 2 , cnt = 0 , product = 0 ; for ( int j = 0 ; j < l ; ++ j ) { if ( arr [ j ] > mid ) { cnt = 0 ; } else if ( ++ cnt >= k ) { product ++ ; cnt = 0 ; } } if ( product < n ) { left = mid + 1 ; } else { right = mid ; } } return left ; } int main ( ) { vector < int > arr { 1 , 10 , 3 , 10 , 2 } ; int n = 3 , k = 1 ; cout << minDays ( arr , n , k ) << endl ; return 0 ; }
Count of distinct power of prime factor of N | C ++ program for the above approach ; Function to count the number of distinct positive power of prime factor of integer N ; Iterate for all prime factor ; If it is a prime factor , count the total number of times it divides n . ; Find the Number of distinct possible positive numbers ; Return the final count ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countFac ( int n ) { int m = n ; int count = 0 ; for ( int i = 2 ; ( i * i ) <= m ; ++ i ) { int total = 0 ; while ( n % i == 0 ) { n /= i ; ++ total ; } int temp = 0 ; for ( int j = 1 ; ( temp + j ) <= total ; ++ j ) { temp += j ; ++ count ; } } if ( n != 1 ) ++ count ; return count ; } int main ( ) { int N = 24 ; cout << countFac ( N ) ; return 0 ; }
Minimum total sum from the given two arrays | C ++ program for the above approach ; Function that prints minimum sum after selecting N elements ; Initialise the dp array ; Base Case ; Adding the element of array a if previous element is also from array a ; Adding the element of array a if previous element is from array b ; Adding the element of array b if previous element is from array a with an extra penalty of integer C ; Adding the element of array b if previous element is also from array b ; Print the minimum sum ; Driver Code ; Given array arr1 [ ] and arr2 [ ] ; Given cost ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumSum ( int a [ ] , int b [ ] , int c , int n ) { vector < vector < int > > dp ( n , vector < int > ( 2 , 1e6 ) ) ; dp [ 0 ] [ 0 ] = a [ 0 ] ; dp [ 0 ] [ 1 ] = b [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] [ 0 ] = min ( dp [ i ] [ 0 ] , dp [ i - 1 ] [ 0 ] + a [ i ] ) ; dp [ i ] [ 0 ] = min ( dp [ i ] [ 0 ] , dp [ i - 1 ] [ 1 ] + a [ i ] + c ) ; dp [ i ] [ 1 ] = min ( dp [ i ] [ 1 ] , dp [ i - 1 ] [ 0 ] + b [ i ] + c ) ; dp [ i ] [ 1 ] = min ( dp [ i ] [ 1 ] , dp [ i - 1 ] [ 1 ] + b [ i ] ) ; } cout << min ( dp [ n - 1 ] [ 0 ] , dp [ n - 1 ] [ 1 ] ) << " STRNEWLINE " ; } int main ( ) { int arr1 [ ] = { 7 , 6 , 18 , 6 , 16 , 18 , 1 , 17 , 17 } ; int arr2 [ ] = { 6 , 9 , 3 , 10 , 9 , 1 , 10 , 1 , 5 } ; int C = 2 ; int N = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; minimumSum ( arr1 , arr2 , C , N ) ; return 0 ; }
Minimum flips required in a binary string such that all K | C ++ program to find the minimum numbers of flips required in a binary string such that all substrings of size K has atleast one 1 ; Function to calculate and return the minimum number of flips to make string valid ; Stores the count of required flips ; Stores the last index of '1' in the string ; Check for the first substring of length K ; If i - th character is '1' ; If the substring had no '1' ; Increase the count of required flips ; Flip the last index of the window ; Update the last index which contains 1 ; Check for remaining substrings ; If last_idx does not belong to current window make it - 1 ; If the last character of the current substring is '1' , then update last_idx to i + k - 1 ; ; If last_idx == - 1 , then the current substring has no 1 ; Increase the count of flips ; Update the last index of the current window ; Store the last index of current window as the index of last '1' in the string ; Return the number of operations ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumMoves ( string S , int K ) { int N = S . length ( ) ; int ops = 0 ; int last_idx = -1 ; for ( int i = 0 ; i < K ; i ++ ) { if ( S [ i ] == '1' ) last_idx = i ; } if ( last_idx == -1 ) { ++ ops ; S [ K - 1 ] = '1' ; last_idx = K - 1 ; } for ( int i = 1 ; i < N - K + 1 ; i ++ ) { if ( last_idx < i ) last_idx = -1 ; if ( S [ i + K - 1 ] == '1' ) last_idx = i + K - 1 ; if ( last_idx == -1 ) { ++ ops ; S [ i + K - 1 ] = '1' ; last_idx = i + K - 1 ; } } return ops ; } int main ( ) { string S = "001010000" ; int K = 3 ; cout << minimumMoves ( S , K ) ; return 0 ; }
Lexicographically smallest string which differs from given strings at exactly K indices | C ++ program for the above approach ; Function to find the string which differ at exactly K positions ; Initialise s3 as s2 ; Number of places at which s3 differ from s2 ; Minimum possible value is ceil ( d / 2 ) if it is not possible then - 1 ; Case 2 when K is less equal d ; X show the modification such that this position only differ from only one string ; modify the position such that this differ from both S1 & S2 & decrease the T at each step ; Finding the character which is different from both S1 and S2 ; After we done T start modification to meet our requirement for X type ; Resultant string ; Case 1 when K > d In first step , modify all the character which are not same in S1 and S3 ; Finding character which is different from both S1 and S2 ; Our requirement not satisfied by performing step 1. We need to modify the position which matches in both string ; Finding the character which is different from both S1 and S2 ; Resultant string ; Driver Code ; Given two strings ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; char arr [ ] = { ' a ' , ' b ' , ' c ' } ; void findString ( int n , int k , string s1 , string s2 ) { string s3 = s2 ; int d = 0 ; for ( int i = 0 ; i < s1 . size ( ) ; i ++ ) { if ( s1 [ i ] != s2 [ i ] ) d ++ ; } if ( ( d + 1 ) / 2 > k ) { cout << " - 1" << endl ; return ; } else { if ( k <= d ) { int X = d - k ; int T = 2 * k - d ; for ( int i = 0 ; i < s3 . size ( ) ; i ++ ) { if ( s1 [ i ] != s2 [ i ] ) { if ( T > 0 ) { for ( int j = 0 ; j < 3 ; j ++ ) { if ( arr [ j ] != s1 [ i ] && arr [ j ] != s2 [ i ] ) { s3 [ i ] = arr [ j ] ; T -- ; break ; } } } else if ( X > 0 ) { s3 [ i ] = s1 [ i ] ; X -- ; } } } cout << s3 << endl ; } else { for ( int i = 0 ; i < s1 . size ( ) ; i ++ ) { if ( s1 [ i ] != s3 [ i ] ) { for ( int j = 0 ; j < 3 ; j ++ ) { if ( arr [ j ] != s1 [ i ] && arr [ j ] != s3 [ i ] ) { s3 [ i ] = arr [ j ] ; k -- ; break ; } } } } for ( int i = 0 ; i < s1 . size ( ) ; i ++ ) { if ( s1 [ i ] == s3 [ i ] && k ) { for ( int j = 0 ; j < 3 ; j ++ ) { if ( arr [ j ] != s1 [ i ] && arr [ j ] != s3 [ i ] ) { s3 [ i ] = arr [ j ] ; k -- ; break ; } } } } cout << s3 << endl ; } } } int main ( ) { int N = 4 , k = 2 ; string S1 = " zzyy " ; string S2 = " zxxy " ; findString ( N , k , S1 , S2 ) ; return 0 ; }
Count of pairs of integers whose difference of squares is equal to N | C ++ program for the above approach ; Function to find the integral solutions of the given equation ; Initialise count to 0 ; Iterate till sqrt ( N ) ; If divisor 's pair sum is even ; Print the total possible solutions ; Driver Code ; Given number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSolutions ( int N ) { int count = 0 ; for ( int i = 1 ; i <= sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { if ( ( i + N / i ) % 2 == 0 ) { count ++ ; } } } cout << 4 * count << endl ; } int main ( ) { int N = 80 ; findSolutions ( N ) ; return 0 ; }
Find the integral roots of a given Cubic equation | C ++ program for the above approach ; Function to find the value at x of the given equation ; Find the value equation at x ; Return the value of ans ; Function to find the integral solution of the given equation ; Initialise start and end ; Implement Binary Search ; Find mid ; Find the value of f ( x ) using current mid ; Check if current mid satisfy the equation ; Print mid and return ; Print " NA " if not found any integral solution ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int check ( int A , int B , int C , int D , long long int x ) { long long int ans ; ans = ( A * x * x * x + B * x * x + C * x + D ) ; return ans ; } void findSolution ( int A , int B , int C , int D , int E ) { int start = 0 , end = 100000 ; long long int mid , ans ; while ( start <= end ) { mid = start + ( end - start ) / 2 ; ans = check ( A , B , C , D , mid ) ; if ( ans == E ) { cout << mid << endl ; return ; } if ( ans < E ) start = mid + 1 ; else end = mid - 1 ; } cout << " NA " ; } int main ( ) { int A = 1 , B = 0 , C = 0 ; int D = 0 , E = 27 ; findSolution ( A , B , C , D , E ) ; }