codeparrot/xlcost-text-to-code huggingface dataset:
Count of substrings of a string containing another given string as a substring | Set 2 | C ++ program for the above approach ; Function to count the substrings of string containing another given string as a substring ; Store length of string S ; Store length of string T ; Store the required count of substrings ; Store the starting index of last occurence of T in S ; Iterate in range [ 0 , n1 - n2 ] ; Check if substring from i to i + n2 is equal to T ; Check if substring from i to i + n2 is equal to T ; Mark chk as false and break the loop ; If chk is true ; Add ( i + 1 - last ) * ( n1 - ( i + n2 - 1 ) ) to answer ; Update the last to i + 1 ; Print the answer ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findOccurrences ( string S , string T ) { int n1 = S . size ( ) ; int n2 = T . size ( ) ; int ans = 0 ; int last = 0 ; for ( int i = 0 ; i <= n1 - n2 ; i ++ ) { bool chk = true ; for ( int j = 0 ; j < n2 ; j ++ ) { if ( T [ j ] != S [ i + j ] ) { chk = false ; break ; } } if ( chk ) { ans += ( i + 1 - last ) * ( n1 - ( i + n2 - 1 ) ) ; last = i + 1 ; } } cout << ans ; } int main ( ) { string S = " dabc " , T = " ab " ; findOccurrences ( S , T ) ; }
Lexicographically largest N | C ++ program for the above approach ; Function to find the lexicographically largest bitonic sequence of size N elements lies in the range [ low , high ] ; Store index of highest element ; If high_index > ( N - 1 ) / 2 , then remaining N / 2 elements cannot be placed in bitonic order ; If high_index <= 0 , then set high_index as 1 ; Stores the resultant sequence ; Store the high value ; Maintain strictly decreasing sequence from index high_index to 0 starting with temp ; Store the value and decrement the temp variable by 1 ; Maintain the strictly decreasing sequence from index high_index + 1 to N - 1 starting with high - 1 ; Store the value and decrement high by 1 ; Print the resultant sequence ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void LargestArray ( int N , int low , int high ) { int high_index = N - ( high - low + 1 ) ; if ( high_index > ( N - 1 ) / 2 ) { cout << " Not ▁ Possible " ; return ; } if ( high_index <= 0 ) high_index = 1 ; int A [ N ] ; int temp = high ; for ( int i = high_index ; i >= 0 ; i -- ) { A [ i ] = temp -- ; } high -= 1 ; for ( int i = high_index + 1 ; i < N ; i ++ ) A [ i ] = high -- ; for ( int i = 0 ; i < N ; i ++ ) { cout << A [ i ] << ' ▁ ' ; } } int main ( ) { int N = 5 , low = 2 , high = 6 ; LargestArray ( N , low , high ) ; return 0 ; }
Print matrix elements from top | C ++ program for the above approach ; Function to traverse the matrix diagonally upwards ; Stores the maximum size of vector from all row of matrix nums [ ] [ ] ; Store elements in desired order ; Store every element on the basis of sum of index ( i + j ) ; Print the stored result ; Reverse all sublist ; Driver code ; Given vector of vectors arr ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printDiagonalTraversal ( vector < vector < int > > & nums ) { int max_size = nums . size ( ) ; for ( int i = 0 ; i < nums . size ( ) ; i ++ ) { if ( max_size < nums [ i ] . size ( ) ) { max_size = nums [ i ] . size ( ) ; } } vector < vector < int > > v ( 2 * max_size - 1 ) ; for ( int i = 0 ; i < nums . size ( ) ; i ++ ) { for ( int j = 0 ; j < nums [ i ] . size ( ) ; j ++ ) { v [ i + j ] . push_back ( nums [ i ] [ j ] ) ; } } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { reverse ( v [ i ] . begin ( ) , v [ i ] . end ( ) ) ; for ( int j = 0 ; j < v [ i ] . size ( ) ; j ++ ) cout << v [ i ] [ j ] << " ▁ " ; } } int main ( ) { vector < vector < int > > arr = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; printDiagonalTraversal ( arr ) ; return 0 ; }
Check if given string satisfies the following conditions | C ++ program for the above approach ; Function to check if given string satisfies the given conditions ; Dimensions ; Left diagonal ; Right diagonal ; Conditions not satisfied ; Print Yes ; Driver Code ; Given String ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void isValid ( string s ) { int n = sqrt ( s . length ( ) ) ; char check = s [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = i , y = i ; while ( x >= 0 && y < n ) { if ( s [ ( n * x ) + y ] != check || s [ ( n * y ) + x ] != check ) { cout << " No " << endl ; return ; } x -- ; y ++ ; } } cout << " Yes " << endl ; } int main ( ) { string str = " abacdaeaafaghaia " ; isValid ( str ) ; return 0 ; }
Sum of all possible strings obtained by removal of non | C ++ program for the above approach ; Function to convert a character to its equivalent digit ; Function to precompute powers of 10 ; Function to precompute prefix sum of numerical strings ; Function to return the i - th term of Triangular Number ; Function to return the sum of all resulting strings ; Precompute powers of 10 ; Precompute prefix sum ; Initialize result ; Apply the above general formula for every i ; Return the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 10 ; int pref [ N ] , power [ N ] ; int toDigit ( char ch ) { return ( ch - '0' ) ; } void powerOf10 ( ) { power [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) power [ i ] = power [ i - 1 ] * 10 ; } void precomputePrefix ( string str , int n ) { pref [ 0 ] = str [ 0 ] - '0' ; for ( int i = 1 ; i < n ; i ++ ) pref [ i ] = pref [ i - 1 ] + toDigit ( str [ i ] ) ; } int triangularNumber ( int i ) { int res = i * ( i + 1 ) / 2 ; return res ; } int sumOfSubstrings ( string str ) { int n = str . size ( ) ; powerOf10 ( ) ; precomputePrefix ( str , n ) ; int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { ans += ( pref [ n - i - 2 ] * ( i + 1 ) + toDigit ( str [ n - i - 1 ] ) * triangularNumber ( n - i - 1 ) ) * power [ i ] ; } return ans ; } int main ( ) { string str = "1234" ; cout << sumOfSubstrings ( str ) ; return 0 ; }
Sum of products of all possible Subarrays | C ++ program for the above approach ; Function that finds the sum of products of all subarray of arr [ ] ; Stores sum of all subarrays ; Iterate array from behind ; Update the ans ; Update the res ; Print the final sum ; Driver Code ; Given array arr [ ] ; Size of array ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sumOfSubarrayProd ( int arr [ ] , int n ) { int ans = 0 ; int res = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int incr = arr [ i ] * ( 1 + res ) ; ans += incr ; res = incr ; } cout << ( ans ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sumOfSubarrayProd ( arr , N ) ; }
Sum of all odd length subarrays | C ++ program for the above approach ; Function to calculate the sum of all odd length subarrays ; Stores the sum ; Size of array ; Traverse the array ; Generate all subarray of odd length ; Add the element to sum ; Return the final sum ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int OddLengthSum ( vector < int > & arr ) { int sum = 0 ; int l = arr . size ( ) ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = i ; j < l ; j += 2 ) { for ( int k = i ; k <= j ; k ++ ) { sum += arr [ k ] ; } } } return sum ; } int main ( ) { vector < int > arr = { 1 , 5 , 3 , 1 , 2 } ; cout << OddLengthSum ( arr ) ; return 0 ; }
Sum of all odd length subarrays | C ++ program for the above approach ; Function that finds the sum of all the element of subarrays of odd length ; Stores the sum ; Size of array ; Traverse the given array arr [ ] ; Add to the sum for each contribution of the arr [ i ] ; Return the final sum ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int OddLengthSum ( vector < int > & arr ) { int sum = 0 ; int l = arr . size ( ) ; for ( int i = 0 ; i < l ; i ++ ) { sum += ( ( ( i + 1 ) * ( l - i ) + 1 ) / 2 ) * arr [ i ] ; } return sum ; } int main ( ) { vector < int > arr = { 1 , 5 , 3 , 1 , 2 } ; cout << OddLengthSum ( arr ) ; return 0 ; }
Check if Euler Totient Function is same for a given number and twice of that number | C ++ program of the above approach ; Function to find the Euler 's Totient Function ; Initialize result as N ; Consider all prime factors of n and subtract their multiples from result ; Return the count ; Function to check if phi ( n ) is equals phi ( 2 * n ) ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int phi ( int n ) { int result = 1 ; for ( int p = 2 ; p < n ; p ++ ) { if ( __gcd ( p , n ) == 1 ) { result ++ ; } } return result ; } bool sameEulerTotient ( int n ) { return phi ( n ) == phi ( 2 * n ) ; } int main ( ) { int N = 13 ; if ( sameEulerTotient ( N ) ) cout << " Yes " ; else cout << " No " ; }
Check if Euler Totient Function is same for a given number and twice of that number | C ++ program of the above approach ; Function to check if phi ( n ) is equals phi ( 2 * n ) ; Return if N is odd ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool sameEulerTotient ( int N ) { return ( N & 1 ) ; } int main ( ) { int N = 13 ; if ( sameEulerTotient ( N ) ) cout << " Yes " ; else cout << " No " ; }
Smallest occurring element in each subsequence | C ++ program for the above approach ; Function that count the subsequence such that each element as the minimum element in the subsequence ; Store index in a map ; Sort the array ; To store count of subsequence ; Traverse the array ; Store count of subsequence ; Print the count of subsequence ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int arr [ ] , int N ) { map < int , int > M ; for ( int i = 0 ; i < N ; i ++ ) { M [ i ] = arr [ i ] ; } sort ( arr , arr + N ) ; unordered_map < int , int > Count ; for ( int i = 0 ; i < N ; i ++ ) { Count [ arr [ i ] ] = pow ( 2 , N - i - 1 ) ; } for ( auto & it : M ) { cout << Count [ M [ it . second ] ] << ' ▁ ' ; } } int main ( ) { int arr [ ] = { 5 , 2 , 1 } ; int N = sizeof arr / sizeof arr [ 0 ] ; solve ( arr , N ) ; }
Program to insert dashes between two adjacent odd digits in given Number | C ++ program for the above approach ; Function to check if char ch is odd or not ; Function to insert dash - between any 2 consecutive digit in string str ; Traverse the string character by character ; Compare every consecutive character with the odd value ; Print the resultant string ; Driver Code ; Given number in form of string ; Function Call
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; bool checkOdd ( char ch ) { return ( ( ch - '0' ) & 1 ) ; } string Insert_dash ( string num_str ) { string result_str = num_str ; for ( int x = 0 ; x < num_str . length ( ) - 1 ; x ++ ) { if ( checkOdd ( num_str [ x ] ) && checkOdd ( num_str [ x + 1 ] ) ) { result_str . insert ( x + 1 , " - " ) ; num_str = result_str ; x ++ ; } } return result_str ; } int main ( ) { string str = "1745389" ; cout << Insert_dash ( str ) ; return 0 ; }
Check if a Matrix is Reverse Bitonic or Not | C ++ Program to check if a matrix is Reverse Bitonic or not ; Function to check if an array is Reverse Bitonic or not ; Check for decreasing sequence ; Check for increasing sequence ; Function to check whether given matrix is bitonic or not ; Check row - wise ; Check column wise ; Generate an array consisting of elements of the current column ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 3 , M = 3 ; bool checkReverseBitonic ( int arr [ ] , int n ) { int i , j , f = 0 ; for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) continue ; if ( arr [ i ] == arr [ i - 1 ] ) return false ; else { f = 1 ; break ; } } if ( i == n ) return true ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] > arr [ j - 1 ] ) continue ; if ( arr [ i ] == arr [ i - 1 ] ) return false ; else { if ( f == 1 ) return false ; } } return true ; } void check ( int arr [ N ] [ M ] ) { int f = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! checkReverseBitonic ( arr [ i ] , M ) ) { cout << " No " << endl ; return ; } } for ( int i = 0 ; i < N ; i ++ ) { int temp [ N ] ; for ( int j = 0 ; j < N ; j ++ ) { temp [ j ] = arr [ j ] [ i ] ; } if ( ! checkReverseBitonic ( temp , N ) ) { cout << " No " << endl ; return ; } } cout << " Yes " ; } int main ( ) { int m [ N ] [ M ] = { { 2 , 3 , 4 } , { 1 , 2 , 3 } , { 4 , 5 , 6 } } ; check ( m ) ; return 0 ; }
Find the element at the specified index of a Spirally Filled Matrix | C ++ Program to find the element at given position in spirally filled matrix ; Function to return the element at ( x , y ) ; If y is greater ; If y is odd ; If y is even ; If x is even ; If x is odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int SpiralElement ( int x , int y ) { int r ; if ( x < y ) { if ( y % 2 == 1 ) { r = y * y ; return ( r - x + 1 ) ; } else { r = ( y - 1 ) * ( y - 1 ) ; return ( r + x ) ; } } else { if ( x % 2 == 0 ) { r = x * x ; return ( r - y + 1 ) ; } else { r = ( x - 1 ) * ( x - 1 ) ; return ( r + y ) ; } } } int main ( ) { int x = 2 , y = 3 ; cout << SpiralElement ( x , y ) ; return 0 ; }
Make the string in AP by changing a character | C ++ program for the above problem ; Function to modify the given string and find the index where modification is needed ; Array to store the ASCII values of alphabets ; loop to compute the ASCII values of characters a - z ; Set to store all the possible differences between consecutive elements ; Loop to find out the differences between consecutive elements and storing them in the set ; Checks if any character of the string disobeys the pattern ; Constructing the strings with all possible values of consecutive difference and comparing them with staring string S . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void string_modify ( string s ) { char alphabets [ 26 ] ; int flag = 0 , hold_i ; char hold_l ; int i ; for ( i = 0 ; i < 26 ; i ++ ) { alphabets [ i ] = i + ' a ' ; } set < int > difference ; string reconstruct = " " ; for ( int i = 1 ; i < s . size ( ) ; i ++ ) { difference . insert ( s [ i ] - s [ i - 1 ] ) ; } if ( difference . size ( ) == 1 ) { cout << " No ▁ modifications ▁ required " ; return ; } for ( auto it = difference . begin ( ) ; it != difference . end ( ) ; it ++ ) { int index = s [ 0 ] - ' a ' ; reconstruct = " " ; flag = 0 ; for ( int i = 0 ; i < s . size ( ) && flag <= 1 ; i ++ ) { reconstruct += alphabets [ index ] ; index += * it ; if ( index < 0 ) { index += 26 ; } index %= 26 ; if ( reconstruct [ i ] != s [ i ] ) { flag ++ ; hold_i = i ; hold_l = s [ i ] ; } } if ( flag == 1 ) { s [ hold_i ] = reconstruct [ hold_i ] ; break ; } } if ( flag > 1 ) { hold_i = 0 ; hold_l = s [ 0 ] ; int temp = ( s [ 1 ] - ' a ' - ( s [ 2 ] - s [ 1 ] ) ) % 26 ; if ( temp < 0 ) { temp += 26 ; } s [ 0 ] = alphabets [ temp ] ; } cout << hold_i << " ▁ - > ▁ " << hold_l << endl << s << endl ; } int main ( ) { string s = " aeimqux " ; string_modify ( s ) ; }
Create matrix whose sum of diagonals in each sub matrix is even | C ++ program for the above approach ; Function to print N * N order matrix with all sub - matrix of even order is sum of its diagonal also even ; Even index ; Odd index ; Iterate two nested loop ; For even index the element should be consecutive odd ; for odd index the element should be consecutive even ; Driver Code ; Given order of matrix ; Function call
#include <iostream> NEW_LINE using namespace std ; void evenSubMatrix ( int N ) { int even = 1 ; int odd = 2 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( ( i + j ) % 2 == 0 ) { cout << even << " ▁ " ; even += 2 ; } else { cout << odd << " ▁ " ; odd += 2 ; } } cout << " STRNEWLINE " ; } } int main ( ) { int N = 4 ; evenSubMatrix ( N ) ; return 0 ; }
Remove leading zeros from a Number given as a string | C ++ Program to implement the above approach ; Function to remove all leading zeros from a a given string ; Regex to remove leading zeros from a string ; Replaces the matched value with given string ; Driver Code
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; void removeLeadingZeros ( string str ) { const regex pattern ( " ^ 0 + ( ? ! $ ) " ) ; str = regex_replace ( str , pattern , " " ) ; cout << str ; } int main ( ) { string str = "0001234" ; removeLeadingZeros ( str ) ; return 0 ; }
Maximum inversions in a sequence of 1 to N after performing given operations at most K times | C ++ program for the above approach ; Function which computes the maximum number of inversions ; ' answer ' will store the required number of inversions ; We do this because we will never require more than floor ( n / 2 ) operations ; left pointer in the array ; right pointer in the array ; Doing k operations ; Incrementing ans by number of inversions increase due to this swapping ; Driver code ; Input 1 ; Input 2
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximum_inversion ( int n , int k ) { int answer = 0 ; k = min ( k , n / 2 ) ; int left = 1 ; int right = n ; while ( k -- ) { answer += 2 * ( right - left ) - 1 ; left ++ ; right -- ; } cout << answer << endl ; } int main ( ) { int N = 5 ; int K = 3 ; maximum_inversion ( N , K ) ; N = 4 ; K = 1 ; maximum_inversion ( N , K ) ; return 0 ; }
First number to leave an odd remainder after repetitive division by 2 | C ++ Program to implement the above approach ; Function to return the position least significant set bit ; Function return the first number to be converted to an odd integer ; Stores the positions of the first set bit ; If both are same ; If A has the least significant set bit ; Otherwise ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getFirstSetBitPos ( int n ) { return log2 ( n & - n ) + 1 ; } int oddFirst ( int a , int b ) { int steps_a = getFirstSetBitPos ( a ) ; int steps_b = getFirstSetBitPos ( b ) ; if ( steps_a == steps_b ) { return -1 ; } if ( steps_a > steps_b ) { return b ; } if ( steps_a < steps_b ) { return a ; } } int main ( ) { int a = 10 ; int b = 8 ; cout << oddFirst ( a , b ) ; }
Check if a string can be split into substrings starting with N followed by N characters | C ++ Program to implement the above approach ; Function to check if the given can be split into desired substrings ; Length of the string ; Traverse the string ; Extract the digit ; Check if the extracted number does not exceed the remaining length ; Check for the remaining string ; If generating desired substrings is not possible ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool helper ( string & s , int pos ) { int len = s . size ( ) ; if ( pos >= len ) return true ; if ( ! isdigit ( s [ pos ] ) ) return false ; int num = 0 ; for ( int i = pos ; i < len ; i ++ ) { num = num * 10 + s [ pos ] - '0' ; if ( i + 1 + num > len ) return false ; if ( helper ( s , i + 1 + num ) ) return true ; } return false ; } int main ( ) { string s = "123abc4db1c " ; if ( helper ( s , 0 ) ) cout << " Yes " ; else cout << " No " ; }
Represent K as sum of N | C ++ program for the above problem ; array to store the N - Bonacci series ; Function to express K as sum of several N_bonacci numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long N_bonacci [ 100 ] ; void N_bonacci_nums ( int n , int k ) { N_bonacci [ 0 ] = 1 ; for ( int i = 1 ; i <= 50 ; ++ i ) { for ( int j = i - 1 ; j >= i - k and j >= 0 ; -- j ) N_bonacci [ i ] += N_bonacci [ j ] ; } vector < long long > ans ; for ( int i = 50 ; i >= 0 ; -- i ) if ( n - N_bonacci [ i ] >= 0 ) { ans . push_back ( N_bonacci [ i ] ) ; n -= N_bonacci [ i ] ; } if ( ans . size ( ) == 1 ) ans . push_back ( 0 ) ; cout << ans . size ( ) << endl ; for ( int i = 0 ; i < ans . size ( ) ; ++ i ) cout << ans [ i ] << " , ▁ " ; } int main ( ) { int n = 21 , k = 5 ; N_bonacci_nums ( n , k ) ; return 0 ; }
Divide N into K parts in the form ( X , 2 X , ... , KX ) for some value of X | C ++ program for the above approach ; Function to find the division ; Calculating value of x1 ; Print - 1 if division is not possible ; Get the first number ie x1 then successively multiply it by x1 k times by index number to get the required answer ; Driver Code ; Given N and K ; Function Call
#include <iostream> NEW_LINE using namespace std ; typedef long long int ll ; void solve ( int n , int k ) { int x1 , d ; d = k * ( k + 1 ) ; if ( ( 2 * n ) % d != 0 ) { cout << " - 1" ; return ; } x1 = 2 * n / d ; for ( int i = 1 ; i <= k ; i ++ ) { cout << x1 * i << " ▁ " ; } cout << endl ; } int main ( ) { int n = 10 , k = 4 ; solve ( n , k ) ; }
Bitonic string | C ++ program for the above approach ; Function to check if the given string is bitonic ; Check for increasing sequence ; If end of string has been reached ; Check for decreasing sequence ; If the end of string hasn 't  been reached ; Return true if bitonic ; Driver Code ; Given string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int checkBitonic ( string s ) { int i , j ; for ( i = 1 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] > s [ i - 1 ] ) continue ; if ( s [ i ] <= s [ i - 1 ] ) break ; } if ( i == s . size ( ) - 1 ) return 1 ; for ( j = i + 1 ; j < s . size ( ) ; j ++ ) { if ( s [ j ] < s [ j - 1 ] ) continue ; if ( s [ j ] >= s [ j - 1 ] ) break ; } i = j ; if ( i != s . size ( ) ) return 0 ; return 1 ; } int main ( ) { string s = " abcdfgcba " ; ( checkBitonic ( s ) == 1 ) ? cout << " YES " : cout << " NO " ; return 0 ; }
Find pair with maximum GCD for integers in range 2 to N | C ++ Program to find a pair of integers less than or equal to N such that their GCD is maximum ; Function to find the required pair whose GCD is maximum ; If N is even ; If N is odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int N ) { if ( N % 2 == 0 ) { cout << N / 2 << " ▁ " << N << endl ; } else { cout << ( N - 1 ) / 2 << " ▁ " << ( N - 1 ) << endl ; } } int main ( ) { int N = 10 ; solve ( N ) ; return 0 ; }
Find initial sequence that produces a given Array by cyclic increments upto index P | C ++ program to implement the above approach ; Function to generate and return the required initial arrangement ; Store the minimum element in the array ; Store the number of increments ; Subtract mi - 1 from every index ; Start from the last index which had been incremented ; Stores the index chosen to distribute its element ; Traverse the array cyclically and find the index whose element was distributed ; If any index has its value reduced to 0 ; Index whose element was distributed ; Store the number of increments at the starting index ; Print the original array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findArray ( int * a , int n , int P ) { int mi = * min_element ( a , a + n ) ; int ctr = 0 ; mi = max ( 0 , mi - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] -= mi ; ctr += mi ; } int i = P - 1 ; int start = -1 ; while ( 1 ) { if ( a [ i ] == 0 ) { start = i ; break ; } a [ i ] -= 1 ; ctr += 1 ; i = ( i - 1 + n ) % n ; } a [ start ] = ctr ; for ( int i = 0 ; i < n ; i ++ ) { cout << a [ i ] << " , ▁ " ; } } int main ( ) { int N = 5 ; int P = 2 ; int arr [ ] = { 3 , 2 , 0 , 2 , 7 } ; findArray ( arr , N , P ) ; return 0 ; }
Generate a unique Array of length N with sum of all subarrays divisible by N | C ++ implementation of the above approach ; Function to print the required array ; Print the array ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void makeArray ( int a [ ] , int n ) { for ( int i = 1 ; i <= n ; i ++ ) cout << i * n << " ▁ " ; } int main ( ) { int N = 6 ; int arr [ N ] ; makeArray ( arr , N ) ; }
Find elements in a given range having at least one odd divisor | C ++ program to print all numbers with least one odd factor in the given range ; Function to prints all numbers with at least one odd divisor ; Check if the number is not a power of two ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int printOddFactorNumber ( int n , int m ) { for ( int i = n ; i <= m ; i ++ ) { if ( ( i > 0 ) && ( ( i & ( i - 1 ) ) != 0 ) ) cout << i << " ▁ " ; } } int main ( ) { int N = 2 , M = 10 ; printOddFactorNumber ( N , M ) ; return 0 ; }