codeparrot/xlcost-text-to-code huggingface dataset:
Travelling Salesman Problem | Greedy Approach | C ++ program for the above approach ; Function to find the minimum cost path for all the paths ; Starting from the 0 th indexed city i . e . , the first city ; Traverse the adjacency matrix tsp [ ] [ ] ; Corner of the Matrix ; If this path is unvisited then and if the cost is less then update the cost ; Check all paths from the ith indexed city ; Update the ending city in array from city which was last visited ; Started from the node where we finished as well . ; Driver Code ; Input Matrix ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinRoute ( vector < vector < int > > tsp ) { int sum = 0 ; int counter = 0 ; int j = 0 , i = 0 ; int min = INT_MAX ; map < int , int > visitedRouteList ; visitedRouteList [ 0 ] = 1 ; int route [ tsp . size ( ) ] ; while ( i < tsp . size ( ) && j < tsp [ i ] . size ( ) ) { if ( counter >= tsp [ i ] . size ( ) - 1 ) { break ; } if ( j != i && ( visitedRouteList [ j ] == 0 ) ) { if ( tsp [ i ] [ j ] < min ) { min = tsp [ i ] [ j ] ; route [ counter ] = j + 1 ; } } j ++ ; if ( j == tsp [ i ] . size ( ) ) { sum += min ; min = INT_MAX ; visitedRouteList [ route [ counter ] - 1 ] = 1 ; j = 0 ; i = route [ counter ] - 1 ; counter ++ ; } } i = route [ counter - 1 ] - 1 ; for ( j = 0 ; j < tsp . size ( ) ; j ++ ) { if ( ( i != j ) && tsp [ i ] [ j ] < min ) { min = tsp [ i ] [ j ] ; route [ counter ] = j + 1 ; } } sum += min ; cout << ( " Minimum ▁ Cost ▁ is ▁ : ▁ " ) ; cout << ( sum ) ; } int main ( ) { vector < vector < int > > tsp = { { -1 , 10 , 15 , 20 } , { 10 , -1 , 35 , 25 } , { 15 , 35 , -1 , 30 } , { 20 , 25 , 30 , -1 } } ; findMinRoute ( tsp ) ; }
Count distinct elements after adding each element of First Array with Second Array | C ++ program for the above approach ; Function to find Occurrence of each element from 1 to 2 * MAX ; Initialise MAX ; Count vector to store count of each element from 1 to 2 * MAX ; Size of Arr1 and Arr2 ; Find the elements of arr3 [ ] and increase count of element by 1 ; Print the result ; Driver Code ; Given arrays arr1 [ ] and arr2 [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findCount ( vector < int > & Arr1 , vector < int > & Arr2 ) { int MAX = max ( * max_element ( Arr1 . begin ( ) , Arr1 . end ( ) ) , * max_element ( Arr2 . begin ( ) , Arr2 . end ( ) ) ) ; vector < int > Count ( 2 * MAX + 1 , 0 ) ; int n = Arr1 . size ( ) , m = Arr2 . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { int element = Arr1 [ i ] + Arr2 [ j ] ; Count [ element ] ++ ; } } for ( int i = 1 ; i <= 2 * MAX ; i ++ ) { if ( Count [ i ] > 0 ) { cout << i << " - > " << Count [ i ] << endl ; } } } int main ( ) { vector < int > arr1 = { 1 , 2 } ; vector < int > arr2 = { 1 , 2 , 1 } ; findCount ( arr1 , arr2 ) ; }
Find two numbers with difference and division both same as N | C ++ program for the above approach ; Function to find two numbers with difference and division both as N ; Condition if the answer is not possible ; Calculate a and b ; Print the values ; Driver Code ; Given N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findAandB ( double N ) { if ( N == 1 ) { cout << " No " ; return ; } double a = N * N / ( N - 1 ) ; double b = a / N ; cout << " a ▁ = ▁ " << a << endl ; cout << " b ▁ = ▁ " << b << endl ; } int main ( ) { double N = 6 ; findAandB ( N ) ; return 0 ; }
Maximize number of elements from Array with sum at most K | C ++ program for the above approach ; Function to select a maximum number of elements in array whose sum is at most K ; Sort the array ; Calculate the sum and count while iterating the sorted array ; Iterate for all the elements in the array ; Add the current element to sum ; Increment the count ; Return the answer ; Driver Code ; Given array ; Given sum k ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSelections ( int A [ ] , int n , int k ) { sort ( A , A + n ) ; int sum = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + A [ i ] ; if ( sum > k ) { break ; } count ++ ; } return count ; } int main ( ) { int A [ ] = { 3 , 7 , 2 , 9 , 4 } ; int k = 15 ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << maxSelections ( A , n , k ) ; return 0 ; }
Pair of integers with difference K having an element as the K | C ++ Program to implement the above problem ; Function to find the required pair ; No pair possible ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void computePair ( double K ) { if ( K == 1 ) { cout << " No " ; return ; } else { cout << K * K / ( K - 1 ) << " ▁ " ; cout << K / ( K - 1 ) << endl ; } } int main ( ) { double K = 6 ; computePair ( K ) ; return 0 ; }
Generate a Binary String without any consecutive 0 ' s ▁ and ▁ at ▁ most ▁ K ▁ consecutive ▁ 1' s | C ++ Program to implement the above approach ; Function to construct the binary string ; Conditions when string construction is not possible ; Stores maximum 1 's that  can be placed in between ; Place 0 's ; Place 1 's in between ; Count remaining M 's ; Place 1 's at the end ; Place 1 's at the beginning ; Return the final string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string ConstructBinaryString ( int N , int M , int K ) { if ( M < ( N - 1 ) || M > K * ( N + 1 ) ) return " - 1" ; string ans = " " ; int l = min ( K , M / ( N - 1 ) ) ; int temp = N ; while ( temp -- ) { ans += '0' ; if ( temp == 0 ) break ; for ( int i = 0 ; i < l ; i ++ ) { ans += '1' ; } } M -= ( N - 1 ) * l ; if ( M == 0 ) return ans ; l = min ( M , K ) ; for ( int i = 0 ; i < l ; i ++ ) ans += '1' ; M -= l ; while ( M > 0 ) { ans = '1' + ans ; M -- ; } return ans ; } int main ( ) { int N = 5 , M = 9 , K = 2 ; cout << ConstructBinaryString ( N , M , K ) ; }
Find smallest number formed by inverting digits of given number N | C ++ program for the above approach ; Function to invert the digits of integer N to form minimum possible number ; Initialize the array ; Iterate till the number N exists ; Last digit of the number N ; Checking if the digit is smaller than 9 - digit ; Store the smaller digit in the array ; Reduce the number each time ; Check if the digit starts with 0 or not ; Print the answer ; Driver Code ; Given Number ; Function Call
#include <iostream> NEW_LINE using namespace std ; void number ( int num ) { int a [ 20 ] , r , i = 0 , j ; while ( num > 0 ) { r = num % 10 ; if ( 9 - r > r ) a [ i ] = r ; else a [ i ] = 9 - r ; i ++ ; num = num / 10 ; } if ( a [ i - 1 ] == 0 ) { cout << 9 ; i -- ; } for ( j = i - 1 ; j >= 0 ; j -- ) cout << a [ j ] ; } int main ( ) { long long int num = 4545 ; number ( num ) ; return 0 ; }
Check if the concatenation of first N natural numbers is divisible by 3 | C ++ program for the above approach ; Function that returns True if concatenation of first N natural numbers is divisible by 3 ; Check using the formula ; Driver Code ; Given Number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDivisible ( int N ) { return ( N - 1 ) % 3 != 0 ; } int main ( ) { int N = 6 ; if ( isDivisible ( N ) ) cout << ( " Yes " ) ; else cout << ( " No " ) ; return 0 ; }
Count of N digit Numbers whose sum of every K consecutive digits is equal | C ++ program for the above approach ; Function to count the number of N - digit numbers such that sum of every k consecutive digits are equal ; Range of numbers ; Extract digits of the number ; Store the sum of first K digits ; Check for every k - consecutive digits ; If sum is not equal then break the loop ; Increment the count if it satisfy the given condition ; Driver Code ; Given N and K ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigitSum ( int N , int K ) { int l = ( int ) pow ( 10 , N - 1 ) , r = ( int ) pow ( 10 , N ) - 1 ; int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { int num = i ; int digits [ N ] ; for ( int j = N - 1 ; j >= 0 ; j -- ) { digits [ j ] = num % 10 ; num /= 10 ; } int sum = 0 , flag = 0 ; for ( int j = 0 ; j < K ; j ++ ) sum += digits [ j ] ; for ( int j = 1 ; j < N - K + 1 ; j ++ ) { int curr_sum = 0 ; for ( int m = j ; m < j + K ; m ++ ) curr_sum += digits [ m ] ; if ( sum != curr_sum ) { flag = 1 ; break ; } } if ( flag == 0 ) { count ++ ; } } return count ; } int main ( ) { int N = 2 , K = 1 ; cout << countDigitSum ( N , K ) ; return 0 ; }
Perfect Square factors of a Number | C ++ Program to implement the above approach ; Function that returns the count of factors that are perfect squares ; Stores the count of number of times a prime number divides N . ; Stores the number of factors that are perfect square ; Count number of 2 's  that divides N ; Calculate ans according to above formula ; Check for all the possible numbers that can divide it ; Check the number of times prime number i divides it ; Calculate ans according to above formula ; Return final count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int noOfFactors ( int N ) { if ( N == 1 ) return 1 ; int count = 0 ; int ans = 1 ; while ( N % 2 == 0 ) { count ++ ; N = N / 2 ; } ans *= ( count / 2 + 1 ) ; for ( int i = 3 ; i * i <= N ; i = i + 2 ) { count = 0 ; while ( N % i == 0 ) { count ++ ; N = N / i ; } ans *= ( count / 2 + 1 ) ; } return ans ; } int main ( ) { int N = 100 ; cout << noOfFactors ( N ) ; return 0 ; }
Nth angle of a Polygon whose initial angle and per angle increment is given | C ++ program for the above approach ; Function to check if the angle is possible or not ; Angular sum of a N - sided polygon ; Angular sum of N - sided given polygon ; Check if both sum are equal ; Function to find the nth angle ; Calculate nth angle ; Return the nth angle ; Driver Code ; Checks the possibility of the polygon exist or not ; Print nth angle of the polygon
#include <iostream> NEW_LINE using namespace std ; bool possible ( int N , int a , int b , int n ) { int sum_of_angle = 180 * ( N - 2 ) ; int Total_angle = ( N * ( ( 2 * a ) + ( N - 1 ) * b ) ) / 2 ; if ( sum_of_angle != Total_angle ) return false ; else return true ; } int nth_angle ( int N , int a , int b , int n ) { int nth = 0 ; nth = a + ( n - 1 ) * b ; return nth ; } int main ( ) { int N = 3 , a = 30 , b = 30 , n = 3 ; if ( possible ( N , a , b , n ) ) cout << nth_angle ( N , a , b , n ) ; else cout << " Not ▁ Possible " ; return 0 ; }
Check if the given string is linear or not | C ++ program for the above approach ; Function to check if the given string is linear or not ; Iterate over string ; If character is not same as the first character then return false ; Increment the tmp ; Driver Code ; Given String str ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int is_linear ( string s ) { int tmp = 0 ; char first = s [ 0 ] ; for ( int pos = 0 ; pos < s . length ( ) ; pos += tmp ) { if ( s [ pos ] != first ) { return false ; } tmp ++ ; } return true ; } int main ( ) { string str = " aapaxyayziabcde " ; if ( is_linear ( str ) ) { cout << " Yes " << endl ; } else { cout << " No " << endl ; } return 0 ; }
Minimize cost to color all the vertices of an Undirected Graph | C ++ Program to implement the above approach ; Function to implement DFS Traversal to marks all the vertices visited from vertex U ; Mark U as visited ; Traverse the adjacency list of U ; Function to find the minimum cost to color all the vertices of graph ; To store adjacency list ; Loop through the edges to create adjacency list ; To check if a vertex of the graph is visited ; Mark visited to all the vertices that can be reached by colored vertices ; Perform DFS ; To store count of uncolored sub - graphs ; Loop through vertex to count uncolored sub - graphs ; If vertex not visited ; Increase count of uncolored sub - graphs ; Perform DFS to mark visited to all vertices of current sub - graphs ; Calculate minimum cost to color all vertices ; Print the result ; Driver Code ; Given number of vertices and edges ; Given edges ; Given cost of coloring and adding an edge ; Given array of colored vertices
#include <bits/stdc++.h> NEW_LINE using namespace std ; void DFS ( int U , int * vis , vector < int > adj [ ] ) { vis [ U ] = 1 ; for ( int V : adj [ U ] ) { if ( vis [ V ] == 0 ) DFS ( V , vis , adj ) ; } } void minCost ( int N , int M , int vCost , int eCost , int sorc [ ] , vector < int > colored , int destination [ ] ) { vector < int > adj [ N + 1 ] ; for ( int i = 0 ; i < M ; i ++ ) { adj [ sorc [ i ] ] . push_back ( destination [ i ] ) ; adj [ destination [ i ] ] . push_back ( sorc [ i ] ) ; } int vis [ N + 1 ] = { 0 } ; for ( int i = 0 ; i < colored . size ( ) ; i ++ ) { DFS ( colored [ i ] , vis , adj ) ; } int X = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( vis [ i ] == 0 ) { X ++ ; DFS ( i , vis , adj ) ; } } int mincost = X * min ( vCost , eCost ) ; cout << mincost << endl ; } int main ( ) { int N = 3 , M = 1 ; int sorc [ ] = { 1 } ; int destination [ ] = { 2 } ; int vCost = 3 , eCost = 2 ; vector < int > colored = { 1 } ; minCost ( N , M , vCost , eCost , sorc , colored , destination ) ; return 0 ; }
Minimum cost to reduce the integer N to 1 as per given conditions | C ++ program for the above approach ; Function to find the minimum cost to reduce the integer N to 1 by the given operations ; Check if x is 1 ; Print the answer ; Prestore the answer ; Iterate till n exists ; Divide N by x ; Reduce n by x ; Add the cost ; Update the answer ; Return the answer ; Driver Code ; Initialize the variables ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int min_cost ( int n , int x , int p , int q ) { if ( x == 1 ) { cout << ( n - 1 ) * p << endl ; return 0 ; } int ans = ( n - 1 ) * p ; int pre = 0 ; while ( n > 1 ) { int tmp = n / x ; if ( tmp < 0 ) break ; pre += ( n - tmp * x ) * p ; n /= x ; pre += q ; ans = min ( ans , pre + ( n - 1 ) * p ) ; } return ans ; } int main ( ) { int n = 5 , x = 2 , p = 2 , q = 3 ; cout << min_cost ( n , x , p , q ) ; return 0 ; }
Number of ways to sum up a total of N from limited denominations | C ++ program for the above approach ; Function to find the number of ways to sum up a total of N from limited denominations ; Store the count of denominations ; Stores the final result ; As one of the denominations is rupee 1 , so we can reduce the computation by checking the equality for N - ( A * 1 ) = N - A ; Increment the count for number of ways ; Driver Code ; Given Denominations ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateWays ( int arr1 [ ] , int arr2 [ ] , int N ) { int A = arr2 [ 0 ] , B = arr2 [ 1 ] ; int C = arr2 [ 2 ] , D = arr2 [ 3 ] ; int ans = 0 ; for ( int b = 0 ; b <= B && b * 5 <= ( N ) ; b ++ ) for ( int c = 0 ; c <= C && b * 5 + c * 10 <= ( N ) ; c ++ ) for ( int d = 0 ; d <= D && b * 5 + c * 10 + d * 20 <= ( N ) ; d ++ ) if ( ( b * 5 ) + ( c * 10 ) + ( d * 20 ) >= ( N - A ) ) ans ++ ; return ans ; } int main ( ) { int N = 123 ; int arr1 [ ] = { 1 , 5 , 10 , 20 } ; int arr2 [ ] = { 6 , 4 , 3 , 5 } ; cout << calculateWays ( arr1 , arr2 , N ) ; return 0 ; }
Number of ways to sum up a total of N from limited denominations | C ++ program for the above approach ; Function to find the number of ways to sum up a total of N from limited denominations ; Store the count of denominations ; Stores the final result ; This will give the number of coins for all combinations of coins with value 1 and 5 ; L2 will sum the values of those indices of ways [ ] which is equal to ( N - ( c * 10 + d * 20 ) ) ; Return the final count ; Driver Code ; Given Denominations ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ways [ 1010 ] ; int calculateWays ( int arr1 [ ] , int arr2 [ ] , int N ) { int A = arr2 [ 0 ] , B = arr2 [ 1 ] ; int C = arr2 [ 2 ] , D = arr2 [ 3 ] ; int ans = 0 ; for ( int b = 0 ; b <= B && b * 5 <= N ; b ++ ) { for ( int a = 0 ; a <= A && a * 1 + b * 5 <= N ; a ++ ) { ways [ a + b * 5 ] ++ ; } } for ( int c = 0 ; c <= C && c * 10 <= ( N ) ; c ++ ) { for ( int d = 0 ; d <= D && c * 10 + d * 20 <= ( N ) ; d ++ ) { ans += ways [ N - c * 10 - d * 20 ] ; } } return ans ; } int main ( ) { int N = 123 ; int arr1 [ ] = { 1 , 5 , 10 , 20 } ; int arr2 [ ] = { 6 , 4 , 3 , 5 } ; cout << calculateWays ( arr1 , arr2 , N ) ; return 0 ; }
Count of Missing Numbers in a sorted array | C ++ Program for the above approach ; Function that find the count of missing numbers in array a [ ] ; Calculate the count of missing numbers in the array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countMissingNum ( int a [ ] , int N ) { int count = a [ N - 1 ] - a [ 0 ] + 1 - N ; cout << count << endl ; } int main ( ) { int arr [ ] = { 5 , 10 , 20 , 40 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countMissingNum ( arr , N ) ; return 0 ; }
Length of longest subsequence whose XOR value is odd | C ++ program for the above approach ; Function for find max XOR subsequence having odd value ; Initialize odd and even count ; Count the number of odd and even numbers in given array ; if all values are odd in given array ; if all values are even in given array ; if both odd and even are present in given array ; Driver Code ; Given array arr [ ] ; Function Call
#include <iostream> NEW_LINE using namespace std ; int maxXORSubsequence ( int arr [ ] , int n ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 ) odd ++ ; else even ++ ; } int maxlen ; if ( odd == n ) { if ( odd % 2 == 0 ) maxlen = n - 1 ; else maxlen = n ; } else if ( even == n ) { maxlen = 0 ; } else { if ( odd % 2 == 0 ) maxlen = even + odd - 1 ; else maxlen = even + odd ; } } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxXORSubsequence ( arr , n ) ; }
Count of pairs with sum N from first N natural numbers | C ++ program to count the number of pairs among the first N natural numbers with sum N ; Function to return the count of pairs ; If n is even ; Count of pairs ; Otherwise ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int numberOfPairs ( int n ) { if ( n % 2 == 0 ) return n / 2 - 1 ; else return n / 2 ; } int main ( ) { int n = 8 ; cout << numberOfPairs ( n ) ; return 0 ; }
Count pair of strings whose concatenation has every vowel | C ++ program for the above approach ; Function to return the count of all concatenated string with each vowel at least once ; Concatenating all possible pairs of string ; Creating an array which checks , the presence of each vowel ; Checking for each vowel by traversing the concatenated string ; Checking if all the elements are set in vowel [ ] ; Check if all vowels are present or not ; Return the final count ; Driver Code ; Given array of strings ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int good_pair ( string str [ ] , int N ) { int countStr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { string res = str [ i ] + str [ j ] ; int vowel [ 5 ] = { 0 } ; for ( int k = 0 ; k < res . length ( ) ; k ++ ) { if ( res [ k ] == ' a ' ) vowel [ 0 ] = 1 ; else if ( res [ k ] == ' e ' ) vowel [ 1 ] = 1 ; else if ( res [ k ] == ' i ' ) vowel [ 2 ] = 1 ; else if ( res [ k ] == ' o ' ) vowel [ 3 ] = 1 ; else if ( res [ k ] == ' u ' ) vowel [ 4 ] = 1 ; } int temp = 0 ; for ( int ind = 0 ; ind < 5 ; ind ++ ) { if ( vowel [ ind ] == 1 ) temp ++ ; } if ( temp == 5 ) countStr ++ ; } } return countStr ; } int main ( ) { string arr [ ] = { " aaweiolkju " , " oxdfgujkmi " } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << good_pair ( arr , N ) ; }
Find sum of exponents of prime factors of numbers 1 to N | C ++ program for the above approach ; Function to implement sieve of erastosthenes ; Create a boolean array and initialize all entries as false ; Initializing smallest factor equal to 2 for all the even numbers ; Iterate for odd numbers less then equal to n ; s ( i ) for a prime is the number itself ; For all multiples of current prime number ; i is the smallest prime factor for number " i * j " ; Function to generate prime factors and its power ; s [ i ] is going to store smallest prime factor of i ; Current prime factor of N ; Power of current prime factor ; Calculating prime factors and their powers sum ; Increment the count and continue the process ; Add count to the sum ; Reinitialize count ; Return the result ; Function to find the sum of all the power of prime factors of N ; Iterate for in [ 2 , N ] ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sieveOfEratosthenes ( int N , int s [ ] ) { vector < bool > prime ( N + 1 , false ) ; for ( int i = 2 ; i <= N ; i += 2 ) s [ i ] = 2 ; for ( int i = 3 ; i <= N ; i += 2 ) { if ( prime [ i ] == false ) { s [ i ] = i ; for ( int j = i ; j * i <= N ; j += 2 ) { if ( prime [ i * j ] == false ) { prime [ i * j ] = true ; s [ i * j ] = i ; } } } } } int generatePrimeFactors ( int N ) { int s [ N + 1 ] ; int sum = 0 ; sieveOfEratosthenes ( N , s ) ; int curr = s [ N ] ; int cnt = 1 ; while ( N > 1 ) { N /= s [ N ] ; if ( curr == s [ N ] ) { cnt ++ ; continue ; } sum = sum + cnt ; curr = s [ N ] ; cnt = 1 ; } return sum ; } void findSum ( int N ) { int sum = 0 ; for ( int i = 2 ; i <= N ; i ++ ) { sum += generatePrimeFactors ( i ) ; } cout << sum << endl ; } int main ( ) { int N = 4 ; findSum ( N ) ; return 0 ; }
Minimum LCM of all subarrays of length at least 2 | C ++ program for the above approach ; Function to find LCM pf two numbers ; Initialise lcm value ; Check for divisibility of a and b by the lcm ; Function to find the Minimum LCM of all subarrays of length greater than 1 ; Store the minimum LCM ; Traverse the array ; Find LCM of consecutive element ; Check if the calculated LCM is less than the minLCM then update it ; Print the minimum LCM ; Driver Code ; Given array arr [ ] ; Size of the array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LCM ( int a , int b ) { int lcm = a > b ? a : b ; while ( true ) { if ( lcm % a == 0 && lcm % b == 0 ) break ; else lcm ++ ; } return lcm ; } void findMinLCM ( int arr [ ] , int n ) { int minLCM = INT_MAX ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int val = LCM ( arr [ i ] , arr [ i + 1 ] ) ; if ( val < minLCM ) { minLCM = val ; } } cout << minLCM << endl ; } int main ( ) { int arr [ ] = { 4 , 8 , 12 , 16 , 20 , 24 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMinLCM ( arr , n ) ; return 0 ; }
Minimum integer with at most K bits set such that their bitwise AND with N is maximum | C ++ program for the above approach ; Function to find the integer with maximum bitwise with N ; Store answer in the bitset Initialized with 0 ; To maintain the count of set bits that should exceed k ; Start traversing from the Most significantif that bit is set in n then we will set in our answer i . e in X ; Checking if the ith bit is set in n or not ; Converting into integer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find_max ( int n , int k ) { bitset < 32 > X ( 0 ) ; int cnt = 0 ; for ( int i = 31 ; i >= 0 && cnt != k ; i -- ) { if ( n & ( 1 << i ) ) { X [ i ] = 1 ; cnt ++ ; } } return X . to_ulong ( ) ; } int main ( ) { int n = 10 , k = 2 ; cout << find_max ( n , k ) << endl ; return 0 ; }
Convert string to integer without using any in | C ++ program for the above approach ; Function to convert string to integer without using functions ; Initialize a variable ; Iterate till length of the string ; Subtract 48 from the current digit ; Print the answer ; Driver Code ; Given string of number ; Function Call
#include <iostream> NEW_LINE using namespace std ; void convert ( string s ) { int num = 0 ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) num = num * 10 + ( int ( s [ i ] ) - 48 ) ; cout << num ; } int main ( ) { char s [ ] = "123" ; convert ( s ) ; return 0 ; }