codeparrot/xlcost-text-to-code huggingface dataset:
Find Kth number from sorted array formed by multiplying any two numbers in the array | C ++ implementation to find the Kth number in the list formed from product of any two numbers in the array and sorting them ; Function to find number of pairs ; Negative and Negative ; Add Possible Pairs ; Positive and Positive ; Add Possible pairs ; Negative and Positive ; Add Possible pairs ; Function to find the kth element in the list ; Separate Positive and Negative elements ; Sort the Elements ; Binary search ; Return the required answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( long long x , vector < int > & pos , vector < int > & neg , int k ) { long long pairs = 0 ; int p = neg . size ( ) - 1 ; int nn = neg . size ( ) - 1 ; int pp = pos . size ( ) - 1 ; for ( int i = 0 ; i < neg . size ( ) ; i ++ ) { while ( p >= 0 and neg [ i ] * neg [ p ] <= x ) p -- ; pairs += min ( nn - p , nn - i ) ; } p = 0 ; for ( int i = pos . size ( ) - 1 ; i >= 0 ; i -- ) { while ( p < pos . size ( ) and pos [ i ] * pos [ p ] <= x ) p ++ ; pairs += min ( p , i ) ; } p = pos . size ( ) - 1 ; for ( int i = neg . size ( ) - 1 ; i >= 0 ; i -- ) { while ( p >= 0 and neg [ i ] * pos [ p ] <= x ) p -- ; pairs += pp - p ; } return ( pairs >= k ) ; } long long kth_element ( int a [ ] , int n , int k ) { vector < int > pos , neg ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) pos . push_back ( a [ i ] ) ; else neg . push_back ( a [ i ] ) ; } sort ( pos . begin ( ) , pos . end ( ) ) ; sort ( neg . begin ( ) , neg . end ( ) ) ; long long l = -1e18 , ans = 0 , r = 1e18 ; while ( l <= r ) { long long mid = ( l + r ) >> 1 ; if ( check ( mid , pos , neg , k ) ) { ans = mid ; r = mid - 1 ; } else l = mid + 1 ; } return ans ; } int main ( ) { int a [ ] = { -4 , -2 , 3 , 3 } , k = 3 ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << kth_element ( a , n , k ) ; return 0 ; }
Find root of a number using Newton 's method | C ++ implementation of the approach ; Function to return the square root of a number using Newtons method ; Assuming the sqrt of n as n only ; The closed guess will be stored in the root ; To count the number of iterations ; Calculate more closed x ; Check for closeness ; Update root ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double squareRoot ( double n , float l ) { double x = n ; double root ; int count = 0 ; while ( 1 ) { count ++ ; root = 0.5 * ( x + ( n / x ) ) ; if ( abs ( root - x ) < l ) break ; x = root ; } return root ; } int main ( ) { double n = 327 ; float l = 0.00001 ; cout << squareRoot ( n , l ) ; return 0 ; }
Count the occurrence of Nth term in first N terms of Van Eck 's sequence | C ++ program to count the occurrence of nth term in first n terms of Van Eck 's sequence ; Utility function to compute Van Eck 's sequence ; Initialize sequence array ; Loop to generate sequence ; Check if sequence [ i ] has occured previously or is new to sequence ; If occurrence found then the next term will be how far back this last term occured previously ; Utility function to count the occurrence of nth term in first n terms of the sequence ; Get nth term of the sequence ; Count the occurrence of nth term in first n terms of the sequence ; Return count ; Driver code ; Pre - compute Van Eck 's sequence ; Print count of the occurrence of nth term in first n terms of the sequence ; Print count of the occurrence of nth term in first n terms of the sequence
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100000 NEW_LINE int sequence [ MAX + 1 ] ; void vanEckSequence ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { sequence [ i ] = 0 ; } for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( sequence [ j ] == sequence [ i ] ) { sequence [ i + 1 ] = i - j ; break ; } } } } int getCount ( int n ) { int nthTerm = sequence [ n - 1 ] ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( sequence [ i ] == nthTerm ) count ++ ; } return count ; } int main ( ) { vanEckSequence ( ) ; int n = 5 ; cout << getCount ( n ) << endl ; n = 11 ; cout << getCount ( n ) << endl ; return 0 ; }
Count the occurrence of Nth term in first N terms of Van Eck 's sequence | C ++ program to count the occurrence of nth term in first n terms of Van Eck 's sequence ; Utility function to compute Van Eck 's sequence ; Initialize sequence array ; Loop to generate sequence ; Check if sequence [ i ] has occured previously or is new to sequence ; If occurrence found then the next term will be how far back this last term occured previously ; Utility function to count the occurrence of nth term in first n terms of the sequence ; Initialize count as 1 ; Increment count if ( i + 1 ) th term is non - zero ; Previous occurrence of sequence [ i ] will be it ( i - sequence [ i + 1 ] ) th position ; Return the count of occurrence ; Driver code ; Pre - compute Van Eck 's sequence ; Print count of the occurrence of nth term in first n terms of the sequence ; Print count of the occurrence of nth term in first n terms of the sequence
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100000 NEW_LINE int sequence [ MAX + 1 ] ; void vanEckSequence ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { sequence [ i ] = 0 ; } for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( sequence [ j ] == sequence [ i ] ) { sequence [ i + 1 ] = i - j ; break ; } } } } int getCount ( int n ) { int count = 1 ; int i = n - 1 ; while ( sequence [ i + 1 ] != 0 ) { count ++ ; i = i - sequence [ i + 1 ] ; } return count ; } int main ( ) { vanEckSequence ( ) ; int n = 5 ; cout << getCount ( n ) << endl ; n = 11 ; cout << getCount ( n ) << endl ; return 0 ; }
Program to find the time remaining for the day to complete | Function to find the remaining time ; Formula for total remaining minutes = 1440 - 60 h - m ; Remaining hours ; Remaining minutes ; Driver code ; Current time ; Get the remaining time
#include <bits/stdc++.h> NEW_LINE using namespace std ; void remainingTime ( int h , int m ) { int totalMin , hoursRemaining , minRemaining ; totalMin = 1440 - 60 * h - m ; hoursRemaining = totalMin / 60 ; minRemaining = totalMin % 60 ; cout << hoursRemaining << " : : " << minRemaining << endl ; } int main ( ) { int h = 0 , m = 1 ; remainingTime ( h , m ) ; return 0 ; }
Goldbach 's Weak Conjecture for Odd numbers | C ++ implementation of the approach ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void check ( int n ) { if ( n % 2 == 1 && n > 5 ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; } int main ( ) { int a = 3 ; int b = 7 ; check ( a ) ; check ( b ) ; return 0 ; }
Number of ways to reach ( X , Y ) in a matrix starting from the origin | C ++ implementation of the approach ; To store the factorial and factorial mod inverse of the numbers ; Function to find ( a ^ m1 ) % mod ; Function to find the factorial of all the numbers ; Function to find the factorial modinverse of all the numbers ; Function to return nCr ; Function to return the number of ways to reach ( X , Y ) in a matrix with the given moves starting from the origin ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  1000005 NEW_LINE #define mod  (int)(1e9 + 7) NEW_LINE int factorial [ N ] , modinverse [ N ] ; int power ( int a , int m1 ) { if ( m1 == 0 ) return 1 ; else if ( m1 == 1 ) return a ; else if ( m1 == 2 ) return ( 1LL * a * a ) % mod ; else if ( m1 & 1 ) return ( 1LL * a * power ( power ( a , m1 / 2 ) , 2 ) ) % mod ; else return power ( power ( a , m1 / 2 ) , 2 ) % mod ; } void factorialfun ( ) { factorial [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) factorial [ i ] = ( 1LL * factorial [ i - 1 ] * i ) % mod ; } void modinversefun ( ) { modinverse [ N - 1 ] = power ( factorial [ N - 1 ] , mod - 2 ) % mod ; for ( int i = N - 2 ; i >= 0 ; i -- ) modinverse [ i ] = ( 1LL * modinverse [ i + 1 ] * ( i + 1 ) ) % mod ; } int binomial ( int n , int r ) { if ( r > n ) return 0 ; int a = ( 1LL * factorial [ n ] * modinverse [ n - r ] ) % mod ; a = ( 1LL * a * modinverse [ r ] ) % mod ; return a ; } int ways ( int x , int y ) { factorialfun ( ) ; modinversefun ( ) ; if ( ( 2 * x - y ) % 3 == 0 && ( 2 * y - x ) % 3 == 0 ) { int m = ( 2 * x - y ) / 3 ; int n = ( 2 * y - x ) / 3 ; return binomial ( n + m , n ) ; } return 0 ; } System . out . println ( n + m + " ▁ " + n ) ; int main ( ) { int x = 3 , y = 3 ; cout << ways ( x , y ) ; return 0 ; }
Find Nth even length palindromic number formed using digits X and Y | C ++ program to find nth even palindromic number of only even length composing of 4 ' s ▁ and ▁ 5' s . ; Utility function to compute n 'th palindrome number ; Calculate the length from above formula as discussed above ; Calculate rank for length L ; Mask to check if i 't bit  is set or not ; If bit is set append '5' else append '4' ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string solve ( int n , char x , char y ) { int length = ceil ( log2 ( n + 2 ) ) - 1 ; int rank = n - ( 1 << length ) + 1 ; string left = " " , right = " " ; for ( int i = length - 1 ; i >= 0 ; i -- ) { int mask = 1 << i ; bool bit = mask & rank ; if ( bit ) { left += y ; right += y ; } else { left += x ; right += x ; } } reverse ( right . begin ( ) , right . end ( ) ) ; return left + right ; } int main ( ) { int n = 23 ; char x = '4' , y = '5' ; string ans = solve ( n , x , y ) ; cout << ans << ' ' ; return 0 ; }
Maximum of all the integers in the given level of Pascal triangle | C ++ implementation of the approach ; Function for the binomial coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to return the maximum value in the nth level of the Pascal 's triangle ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binomialCoeff ( int n , int k ) { int C [ n + 1 ] [ k + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } int findMax ( int n ) { return binomialCoeff ( n , n / 2 ) ; } int main ( ) { int n = 5 ; cout << findMax ( n ) ; return 0 ; }
Find two co | C ++ implementation of the approach ; Function to find the required numbers ; GCD of the given numbers ; Printing the required numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNumbers ( int a , int b ) { int gcd = __gcd ( a , b ) ; cout << ( a / gcd ) << " ▁ " << ( b / gcd ) ; } int main ( ) { int a = 12 , b = 16 ; findNumbers ( a , b ) ; return 0 ; }
Find the remaining balance after the transaction | C ++ program to find the remaining balance ; Function to find the balance ; Check if the transaction can be successful or not ; Transaction is successful ; Transaction is unsuccessful ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findBalance ( int x , float bal ) { if ( x % 10 == 0 && ( ( float ) x + 1.50 ) <= bal ) { cout << fixed << setprecision ( 2 ) << ( bal - x - 1.50 ) << endl ; } else { cout << fixed << setprecision ( 2 ) << ( bal ) << endl ; } } int main ( ) { int x = 50 ; float bal = 100.50 ; findBalance ( x , bal ) ; return 0 ; }
Count number of pairs in array having sum divisible by K | SET 2 | C ++ Program to count pairs whose sum divisible by ' K ' ; Program to count pairs whose sum divisible by ' K ' ; Create a frequency array to count occurrences of all remainders when divided by K ; To store count of pairs . ; Traverse the array , compute the remainder and add k - remainder value hash count to ans ; Count number of ( A [ i ] , ( K - rem ) % K ) pairs ; Increment count of remainder in hash map ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countKdivPairs ( int A [ ] , int n , int K ) { int freq [ K ] = { 0 } ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int rem = A [ i ] % K ; ans += freq [ ( K - rem ) % K ] ; freq [ rem ] ++ ; } return ans ; } int main ( ) { int A [ ] = { 2 , 2 , 1 , 7 , 5 , 3 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int K = 4 ; cout << countKdivPairs ( A , n , K ) ; return 0 ; }
Minimum number of Bottles visible when a bottle can be enclosed inside another Bottle | C ++ code for the above approach ; Driver code ; Find the solution
#include <bits/stdc++.h> NEW_LINE using namespace std ; void min_visible_bottles ( int * arr , int n ) { map < int , int > m ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { m [ arr [ i ] ] ++ ; ans = max ( ans , m [ arr [ i ] ] ) ; } cout << " Minimum ▁ number ▁ of ▁ " << " Visible ▁ Bottles ▁ are : ▁ " << ans << endl ; } int main ( ) { int n = 8 ; int arr [ ] = { 1 , 1 , 2 , 3 , 4 , 5 , 5 , 4 } ; min_visible_bottles ( arr , n ) ; return 0 ; }
Remove Nth node from end of the Linked List | CPP program to delete nth node from last ; Structure of node ; Function to insert node in a linked list ; Function to remove nth node from last ; To store length of the linked list ; B > length , then we can 't remove node ; We need to remove head node ; Return head -> next ; Remove len - B th node from starting ; This function prints contents of linked list starting from the given node ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; struct Node * create ( struct Node * head , int x ) { struct Node * temp , * ptr = head ; temp = new Node ( ) ; temp -> data = x ; temp -> next = NULL ; if ( head == NULL ) head = temp ; else { while ( ptr -> next != NULL ) { ptr = ptr -> next ; } ptr -> next = temp ; } return head ; } Node * removeNthFromEnd ( Node * head , int B ) { int len = 0 ; Node * tmp = head ; while ( tmp != NULL ) { len ++ ; tmp = tmp -> next ; } if ( B > len ) { cout << " Length ▁ of ▁ the ▁ linked ▁ list ▁ is ▁ " << len ; cout << " ▁ we ▁ can ' t ▁ remove ▁ " << B << " th ▁ node ▁ from ▁ the " ; cout << " ▁ linked ▁ list STRNEWLINE " ; return head ; } else if ( B == len ) { return head -> next ; } else { int diff = len - B ; Node * prev = NULL ; Node * curr = head ; for ( int i = 0 ; i < diff ; i ++ ) { prev = curr ; curr = curr -> next ; } prev -> next = curr -> next ; return head ; } } void display ( struct Node * head ) { struct Node * temp = head ; while ( temp != NULL ) { cout << temp -> data << " ▁ " ; temp = temp -> next ; } cout << endl ; } int main ( ) { struct Node * head = NULL ; head = create ( head , 1 ) ; head = create ( head , 2 ) ; head = create ( head , 3 ) ; head = create ( head , 4 ) ; head = create ( head , 5 ) ; int n = 2 ; cout << " Linked ▁ list ▁ before ▁ modification : ▁ STRNEWLINE " ; display ( head ) ; head = removeNthFromEnd ( head , 2 ) ; cout << " Linked ▁ list ▁ after ▁ modification : ▁ STRNEWLINE " ; display ( head ) ; return 0 ; }
Find the final sequence of the array after performing given operations | C ++ implementation of the approach ; Function that generates the array b [ ] when n is even ; Fill the first half of the final array with reversed sequence ; Fill the second half ; Function that generates the array b [ ] when n is odd ; Fill the first half of the final array with reversed sequence ; Fill the second half ; Function to find the final array b [ ] after n operations of given type ; Create the array b ; If the array size is even ; Print the final array elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solveEven ( int n , int * arr , int * b ) { int left = n - 1 ; for ( int i = 0 ; i < ( n / 2 ) ; ++ i ) { b [ i ] = arr [ left ] ; left = left - 2 ; if ( left < 0 ) break ; } int right = 0 ; for ( int i = n / 2 ; i <= n - 1 ; ++ i ) { b [ i ] = arr [ right ] ; right = right + 2 ; if ( right > n - 2 ) break ; } } void solveOdd ( int n , int * arr , int * b ) { int left = n - 1 ; for ( int i = 0 ; i < ( n / 2 ) + 1 ; ++ i ) { b [ i ] = arr [ left ] ; left = left - 2 ; if ( left < 0 ) break ; } int right = 1 ; for ( int i = ( n / 2 ) + 1 ; i <= n - 1 ; ++ i ) { b [ i ] = arr [ right ] ; right = right + 2 ; if ( right > n - 2 ) break ; } } void solve ( int n , int * arr ) { int b [ n ] ; if ( n % 2 == 0 ) solveEven ( n , arr , b ) ; else solveOdd ( n , arr , b ) ; for ( int i = 0 ; i <= n - 1 ; ++ i ) { cout << b [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; solve ( n , arr ) ; return 0 ; }
Find the sum of the first half and second half elements of an array | C ++ program to find the sum of the first half elements and second half elements of an array ; Function to find the sum of the first half elements and second half elements of an array ; Add elements in first half sum ; Add elements in the second half sum ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sum_of_elements ( int arr [ ] , int n ) { int sumfirst = 0 , sumsecond = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n / 2 ) sumfirst += arr [ i ] ; else sumsecond += arr [ i ] ; } cout << " Sum ▁ of ▁ first ▁ half ▁ elements ▁ is ▁ " << sumfirst << endl ; cout << " Sum ▁ of ▁ second ▁ half ▁ elements ▁ is ▁ " << sumsecond << endl ; } int main ( ) { int arr [ ] = { 20 , 30 , 60 , 10 , 25 , 15 , 40 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sum_of_elements ( arr , n ) ; return 0 ; }
Sequence with sum K and minimum sum of absolute differences between consecutive elements | C ++ implementation of the approach ; Function to return the minimized sum ; If k is divisible by n then the answer will be 0 ; Else the answer will be 1 ; Driver code
#include <iostream> NEW_LINE using namespace std ; int minimum_sum ( int n , int k ) { if ( k % n == 0 ) return 0 ; return 1 ; } int main ( ) { int n = 3 , k = 56 ; cout << minimum_sum ( n , k ) ; return 0 ; }
Print first N terms of Lower Wythoff sequence | C ++ implementation of the approach ; Function to print the first n terms of the lower Wythoff sequence ; Calculate value of phi ; Find the numbers ; a ( n ) = floor ( n * phi ) ; Print the nth numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void lowerWythoff ( int n ) { double phi = ( 1 + sqrt ( 5 ) ) / 2.0 ; for ( int i = 1 ; i <= n ; i ++ ) { double ans = floor ( i * phi ) ; cout << ans ; if ( i != n ) cout << " , ▁ " ; } } int main ( ) { int n = 5 ; lowerWythoff ( n ) ; return 0 ; }
Create new linked list from two given linked list with greater element at each node | C ++ program to create a new linked list from two given linked list of the same size with the greater element among the two at each node ; Representation of node ; Function to insert node in a linked list ; Function which returns new linked list ; Compare for greater node ; Driver code ; First linked list ; Second linked list
#include <iostream> NEW_LINE using namespace std ; struct Node { int data ; Node * next ; } ; void insert ( Node * * root , int item ) { Node * ptr , * temp ; temp = new Node ; temp -> data = item ; temp -> next = NULL ; if ( * root == NULL ) * root = temp ; else { ptr = * root ; while ( ptr -> next != NULL ) ptr = ptr -> next ; ptr -> next = temp ; } } Node * newList ( Node * root1 , Node * root2 ) { Node * ptr1 = root1 , * ptr2 = root2 , * ptr ; Node * root = NULL , * temp ; while ( ptr1 != NULL ) { temp = new Node ; temp -> next = NULL ; if ( ptr1 -> data < ptr2 -> data ) temp -> data = ptr2 -> data ; else temp -> data = ptr1 -> data ; if ( root == NULL ) root = temp ; else { ptr = root ; while ( ptr -> next != NULL ) ptr = ptr -> next ; ptr -> next = temp ; } ptr1 = ptr1 -> next ; ptr2 = ptr2 -> next ; } return root ; } void display ( Node * root ) { while ( root != NULL ) { cout << root -> data << " - > " ; root = root -> next ; } cout << endl ; } int main ( ) { Node * root1 = NULL , * root2 = NULL , * root = NULL ; insert ( & root1 , 5 ) ; insert ( & root1 , 2 ) ; insert ( & root1 , 3 ) ; insert ( & root1 , 8 ) ; cout << " First ▁ List : ▁ " ; display ( root1 ) ; insert ( & root2 , 1 ) ; insert ( & root2 , 7 ) ; insert ( & root2 , 4 ) ; insert ( & root2 , 5 ) ; cout << " Second ▁ List : ▁ " ; display ( root2 ) ; root = newList ( root1 , root2 ) ; cout << " New ▁ List : TABSYMBOL " ; display ( root ) ; return 0 ; }
Count of quadruplets from range [ L , R ] having GCD equal to K | C ++ implementation of the approach ; Function to return the count of quadruplets having gcd = k ; To store the required count ; Check every quadruplet pair whether its gcd is k ; Return the required count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countQuadruplets ( int l , int r , int k ) { int count = 0 ; for ( int u = l ; u <= r ; u ++ ) { for ( int v = l ; v <= r ; v ++ ) { for ( int w = l ; w <= r ; w ++ ) { for ( int x = l ; x <= r ; x ++ ) { if ( __gcd ( __gcd ( u , v ) , __gcd ( w , x ) ) == k ) count ++ ; } } } } return count ; } int main ( ) { int l = 1 , r = 10 , k = 2 ; cout << countQuadruplets ( l , r , k ) ; return 0 ; }
Find an index such that difference between product of elements before and after it is minimum | C ++ implementation of the approach ; Function to return the index i such that the absolute difference between product of elements up to that index and the product of rest of the elements of the array is minimum ; To store the required index ; Prefix product array ; Compute the product array ; Iterate the product array to find the index ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE int findIndex ( int a [ ] , int n ) { int res ; ll min_diff = INT_MAX ; ll prod [ n ] ; prod [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prod [ i ] = prod [ i - 1 ] * a [ i ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { ll curr_diff = abs ( ( prod [ n - 1 ] / prod [ i ] ) - prod [ i ] ) ; if ( curr_diff < min_diff ) { min_diff = curr_diff ; res = i ; } } return res ; } int main ( ) { int arr [ ] = { 3 , 2 , 5 , 7 , 2 , 9 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findIndex ( arr , N ) ; return 0 ; }
Print all numbers whose set of prime factors is a subset of the set of the prime factors of X | C ++ program to implement the above approach ; Function to print all the numbers ; Iterate for every element in the array ; Find the gcd ; Iterate till gcd is 1 of number and x ; Divide the number by gcd ; Find the new gcdg ; If the number is 1 at the end then print the number ; If no numbers have been there ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printNumbers ( int a [ ] , int n , int x ) { bool flag = false ; for ( int i = 0 ; i < n ; i ++ ) { int num = a [ i ] ; int g = __gcd ( num , x ) ; while ( g != 1 ) { num /= g ; g = __gcd ( num , x ) ; } if ( num == 1 ) { flag = true ; cout << a [ i ] << " ▁ " ; } } if ( ! flag ) cout << " There ▁ are ▁ no ▁ such ▁ numbers " ; } int main ( ) { int x = 60 ; int a [ ] = { 2 , 5 , 10 , 7 , 17 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; printNumbers ( a , n , x ) ; return 0 ; }
Find the final radiations of each Radiated Stations | C ++ implementation of the approach ; Function to print the final radiations ; Function to create the array of the resultant radiations ; Resultant radiations ; Declaring index counter for left and right radiation ; Effective radiation for left and right case ; Radiation for i - th station ; Radiation increment for left stations ; Radiation increment for right stations ; Print the resultant radiation for each of the stations ; Driver code ; 1 - based indexing
#include <bits/stdc++.h> NEW_LINE using namespace std ; void print ( int rStation [ ] , int n ) { for ( int i = 1 ; i <= n ; i ++ ) cout << rStation [ i ] << " ▁ " ; cout << endl ; } void radiated_Station ( int station [ ] , int n ) { int rStation [ n + 1 ] ; memset ( rStation , 0 , sizeof ( rStation ) ) ; for ( int i = 1 ; i <= n ; i ++ ) { int li = i - 1 , ri = i + 1 ; int lRad = station [ i ] - 1 , rRad = station [ i ] - 1 ; rStation [ i ] += station [ i ] ; while ( li >= 1 && lRad >= 1 ) { rStation [ li -- ] += lRad -- ; } while ( ri <= n && rRad >= 1 ) { rStation [ ri ++ ] += rRad -- ; } } print ( rStation , n ) ; } int main ( ) { int station [ ] = { 0 , 7 , 9 , 12 , 2 , 5 } ; int n = ( sizeof ( station ) / sizeof ( station [ 0 ] ) ) - 1 ; radiated_Station ( station , n ) ; return 0 ; }