codeparrot/xlcost-text-to-code huggingface dataset:
Minimum swaps of same | C ++ program for the above approach ; Function to count the minimum swaps of same - indexed elements from arrays arr1 [ ] and arr2 [ ] required to make the sum of both the arrays even ; Store the sum of elements of the array arr1 and arr2 respectively ; Store the array sum of both the arrays ; If both sumArr1 and sumArr2 are even , print 0 and return ; If both sumArr1 and sumArr2 are odd and check for a pair with sum odd sum ; Stores if a pair with odd sum exists or not ; Traverse the array ; If a pair exists with odd sum , set flag = 1 ; Print the answer and return ; For all other cases , print - 1 ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumSwaps ( int arr1 [ ] , int arr2 [ ] , int n ) { int sumArr1 = 0 , sumArr2 = 0 ; for ( int i = 0 ; i < n ; ++ i ) { sumArr1 += arr1 [ i ] ; sumArr2 += arr2 [ i ] ; } if ( sumArr1 % 2 == 0 && sumArr2 % 2 == 0 ) { cout << 0 ; return ; } if ( sumArr1 % 2 != 0 && sumArr2 % 2 != 0 ) { int flag = -1 ; for ( int i = 0 ; i < n ; ++ i ) { if ( ( arr1 [ i ] + arr2 [ i ] ) % 2 == 1 ) { flag = 1 ; break ; } } cout << flag ; return ; } cout << -1 ; } int main ( ) { int arr1 [ ] = { 11 , 14 , 20 , 2 } ; int arr2 [ ] = { 5 , 9 , 6 , 3 } ; int N = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; minimumSwaps ( arr1 , arr2 , N ) ; return 0 ; }
Count of seats booked on each of the given N flights | C ++ program for the above approach ; Function to find the total of the seats booked in each of the flights ; Stores the resultant sequence ; Traverse the array ; Store the first booked flight ; Store the last booked flight ; Store the total number of seats booked in flights [ l , r ] ; Add K to the flight L ; Subtract K from flight number R + 1 ; Find the prefix sum of the array ; Print the total number of seats booked in each flight ; Driver Code ; Given list of bookings ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void corpFlightBookings ( vector < vector < int > > & Bookings , int N ) { vector < int > res ( N , 0 ) ; for ( int i = 0 ; i < Bookings . size ( ) ; i ++ ) { int l = Bookings [ i ] [ 0 ] ; int r = Bookings [ i ] [ 1 ] ; int K = Bookings [ i ] [ 2 ] ; res [ l - 1 ] = res [ l - 1 ] + K ; if ( r <= res . size ( ) - 1 ) res [ r ] = ( - K ) + res [ r ] ; } for ( int i = 1 ; i < res . size ( ) ; i ++ ) res [ i ] = res [ i ] + res [ i - 1 ] ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) { cout << res [ i ] << " ▁ " ; } } int main ( ) { vector < vector < int > > bookings { { 1 , 3 , 100 } , { 2 , 6 , 100 } , { 3 , 4 , 100 } } ; int N = 6 ; corpFlightBookings ( bookings , N ) ; return 0 ; }
Bitwise XOR of all odd numbers from a given range | C ++ program for the above approach ; Function to calculate Bitwise XOR of odd numbers in the range [ 1 , N ] ; N & 3 is equivalent to n % 4 ; If n is multiple of 4 ; If n % 4 gives remainder 1 ; If n % 4 gives remainder 2 ; If n % 4 gives remainder 3 ; Function to find the XOR of odd numbers less than or equal to N ; If number is even ; Print the answer ; If number is odd ; Print the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findXOR ( int n ) { switch ( n & 3 ) { case 0 : return n ; case 1 : return 1 ; case 2 : return n + 1 ; case 3 : return 0 ; } } void findOddXOR ( int n ) { if ( n % 2 == 0 ) cout << ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( n / 2 ) ) ) ; else cout << ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( ( n - 1 ) / 2 ) ) ) ; } int main ( ) { int N = 11 ; findOddXOR ( N ) ; return 0 ; }
Smallest number greater than or equal to N which is divisible by its non | C ++ program for the above approach ; Function to find the smallest number greater than or equal to N such that it is divisible by its non - zero digits ; Iterate in range [ N , N + 2520 ] ; To check if the current number satisfies the given condition ; Store the number in a temporary variable ; Loop until temp > 0 ; Check only for non zero digits ; Extract the current digit ; If number is divisible by current digit or not ; Otherwise , set possible to 0 ; Break out of the loop ; Divide by 10 ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSmallestNumber ( int n ) { for ( int i = n ; i <= ( n + 2520 ) ; ++ i ) { bool possible = 1 ; int temp = i ; while ( temp ) { if ( temp % 10 != 0 ) { int digit = temp % 10 ; if ( i % digit != 0 ) { possible = 0 ; break ; } } temp /= 10 ; } if ( possible == 1 ) { cout << i ; return ; } } } int main ( ) { int N = 31 ; findSmallestNumber ( N ) ; return 0 ; }
Print path from a node to root of given Complete Binary Tree | C ++ program for the above approach ; Function to print the path from node to root ; Iterate until root is reached ; Print the value of the current node ; Move to parent of the current node ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void path_to_root ( int node ) { while ( node >= 1 ) { cout << node << ' ▁ ' ; node /= 2 ; } } int main ( ) { int N = 7 ; path_to_root ( N ) ; return 0 ; }
Smallest number whose sum of digits is N and every digit occurring at most K times | C ++ program for the above approach ; Function to find the smallest number whose sum of digits is N and each digit occurring at most K times ; Maximum sum possible using each digit at most K times ; Append smallest number into the resultant string ; Iterate over the range [ 9 , 1 ] ; If the count of the digit is K , then update count and check for the next digit ; If N is greater than current digit ; Subtract i from N ; Insert i as a digit ; Insert remaining N as a digit ; Increment count ; Reverse the string ; Print the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSmallestNumberPossible ( int N , int K ) { if ( N > 45 * K ) { cout << " - 1" ; return ; } string res = " " ; int count = 0 ; for ( int i = 9 ; i >= 1 ; ) { if ( count == K ) { i -- ; count = 0 ; } if ( N > i ) { N -= i ; res += ( char ) 48 + i ; } else { res += ( char ) N + 48 ; N = 0 ; break ; } count ++ ; } reverse ( res . begin ( ) , res . end ( ) ) ; cout << res ; } int main ( ) { int N = 25 , K = 3 ; findSmallestNumberPossible ( N , K ) ; return 0 ; }
Minimum sum of values subtracted from array elements to make all array elements equal | C ++ program for the above approach ; Function to find the sum of values removed to make all array elements equal ; Stores the minimum of the array ; Stores required sum ; Traverse the array ; Add the value subtracted from the current element ; Return the total sum ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minValue ( int arr [ ] , int n ) { int minimum = * min_element ( arr , arr + n ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ( arr [ i ] - minimum ) ; } return sum ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minValue ( arr , N ) ; return 0 ; }
Smallest positive number made up of non | C ++ program for the above approach ; Function to find smallest positive number made up of non - repeating digits whose sum of its digits is equal to n ; No such number exists ; Stores the required answer ; Store the digit at unit 's place ; Iterate until n > digit ; Push digit at the start of res ; Decrement n by digit ; Decrement digit by 1 ; Push the remaining number as the starting digit ; Print the required number ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void result ( int n ) { if ( n > 45 ) { cout << -1 ; return ; } string res ; int digit = 9 ; while ( n > digit ) { res = char ( '0' + digit ) + res ; n -= digit ; digit -= 1 ; } if ( n > 0 ) { res = char ( '0' + n ) + res ; } cout << res ; } int main ( ) { int N = 19 ; result ( N ) ; return 0 ; }
Count pair of integers having even sum | C ++ program for the above approach ; Function to count even pairs ; Stores count of pairs having even sum ; Stores count of even numbers up to N ; Stores count of odd numbers up to N ; Stores count of even numbers up to M ; Stores count of odd numbers up to M ; Return the count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countEvenPairs ( int N , int M ) { int count = 0 ; int nEven = floor ( N / 2 ) ; int nOdd = ceil ( N / 2 ) ; int mEven = floor ( M / 2 ) ; int mOdd = ceil ( M / 2 ) ; count = nEven * mEven + nOdd * mOdd ; return count ; } int main ( ) { int N = 4 ; int M = 6 ; cout << countEvenPairs ( N , M ) ; return 0 ; }
Generate an N | C ++ program to implement the above approach ; Function to generate a string of length N having longest palindromic substring of length K ; Fill first K characters with ' a ' ; Stores a non - palindromic sequence to be repeated for N - k slots ; Print N - k remaining characters ; Driver Code ; Given N and K
#include <bits/stdc++.h> NEW_LINE using namespace std ; void string_palindrome ( int N , int K ) { for ( int i = 0 ; i < K ; i ++ ) cout << " a " ; string s = " bcd " ; for ( int i = 0 ; i < N - K ; i ++ ) cout << s [ i % 3 ] ; } int main ( ) { int N = 5 , K = 3 ; string_palindrome ( N , K ) ; return 0 ; }
Last remaining character after repeated removal of the first character and flipping of characters of a Binary String | C ++ program to implement the above approach ; Function to find the last removed character from the string ; Stores length of the string ; Base Case : ; If the second last character is '0' ; If the second last character is '1' ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char lastRemovedCharacter ( string str ) { int n = str . length ( ) ; if ( n == 1 ) return str [ 0 ] ; if ( str [ n - 2 ] == '0' ) { return ( '1' - str [ n - 1 ] + '0' ) ; } else return str [ n - 1 ] ; } int main ( ) { string str = "10010" ; cout << lastRemovedCharacter ( str ) ; }
Find the triplet from given Bitwise XOR and Bitwise AND values of all its pairs | C ++ program to implement the above approach ; Function to find the triplet with given Bitwise XOR and Bitwise AND values of all possible pairs of the triplet ; Stores values of a triplet ; Stores a + b ; Stores a + c ; Stores b + c ; Calculate aSUMb ; Calculate aSUMc ; Calculate bSUMc ; Calculate a ; Calculate b ; Calculate c ; Print a ; Print b ; Print c ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNumbers ( int aXORb , int aANDb , int aXORc , int aANDc , int bXORc , int bANDc ) { int a , b , c ; int aSUMb ; int aSUMc ; int bSUMc ; aSUMb = aXORb + aANDb * 2 ; aSUMc = aXORc + aANDc * 2 ; bSUMc = bXORc + bANDc * 2 ; a = ( aSUMb - bSUMc + aSUMc ) / 2 ; b = aSUMb - a ; c = aSUMc - a ; cout << " a ▁ = ▁ " << a ; cout << " , ▁ b ▁ = ▁ " << b ; cout << " , ▁ c ▁ = ▁ " << c ; } int main ( ) { int aXORb = 30 , aANDb = 0 , aXORc = 20 , aANDc = 10 , bXORc = 10 , bANDc = 20 ; findNumbers ( aXORb , aANDb , aXORc , aANDc , bXORc , bANDc ) ; }
Find N distinct numbers whose Bitwise XOR is equal to K | C ++ program for the above approach ; Function to find N integers having Bitwise XOR equal to K ; Base Cases ; Assign values to P and Q ; Stores Bitwise XOR of the first ( N - 3 ) elements ; Print the first N - 3 elements ; Calculate Bitwise XOR of first ( N - 3 ) elements ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findArray ( int N , int K ) { if ( N == 1 ) { cout << " ▁ " << K ; return ; } if ( N == 2 ) { cout << 0 << " ▁ " << K ; return ; } int P = N - 2 ; int Q = N - 1 ; int VAL = 0 ; for ( int i = 1 ; i <= ( N - 3 ) ; i ++ ) { cout << " ▁ " << i ; VAL ^= i ; } if ( VAL == K ) { cout << P << " ▁ " << Q << " ▁ " << ( P ^ Q ) ; } else { cout << 0 << " ▁ " << P << " ▁ " << ( P ^ K ^ VAL ) ; } } int main ( ) { int N = 4 , X = 6 ; findArray ( N , X ) ; return 0 ; }
Flip consecutive set bits starting from LSB of a given number | C ++ program for the above approach ; Function to find the number after converting 1 s from end to 0 s ; Count of 1 s ; AND operation of N and 1 ; Left shift N by count ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNumber ( int N ) { int count = 0 ; while ( ( N & 1 ) == 1 ) { N = N >> 1 ; count ++ ; } return N << count ; } int main ( ) { int N = 39 ; cout << findNumber ( N ) ; return 0 ; }
Flip consecutive set bits starting from LSB of a given number | C ++ program for the above approach ; Function to find the number after converting 1 s from end to 0 s ; Return the logical AND of N and ( N + 1 ) ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNumber ( int N ) { return N & ( N + 1 ) ; } int main ( ) { int N = 39 ; cout << findNumber ( N ) ; return 0 ; }
Count of substrings of a string containing another given string as a substring | C ++ program for the above approach ; Function to store all substrings of S ; Stores the substrings of S ; Pick start point in outer loop and lengths of different strings for a given starting point ; Return the array containing substrings of S ; Function to check if a string is present in another string ; Check if target is in the string str or not ; Function to count the substring of S containing T in it as substring ; Store all substrings of S in the array v [ ] ; Store required count of substrings ; Iterate through all the substrings of S ; If string T is present in the current substring , then increment the ans ; Print the answer ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < string > subString ( string s , int n ) { vector < string > v ; for ( int i = 0 ; i < n ; i ++ ) { for ( int len = 1 ; len <= n - i ; len ++ ) { string find = s . substr ( i , len ) ; v . push_back ( find ) ; } } return v ; } int IsPresent ( string & str , string & target ) { if ( str . find ( target ) != string :: npos ) { return 1 ; } return -1 ; } void countSubstrings ( string & S , string & T ) { vector < string > v = subString ( S , S . length ( ) ) ; int ans = 0 ; for ( auto it : v ) { if ( IsPresent ( it , T ) != -1 ) { ans ++ ; } } cout << ans ; } int main ( ) { string S = " dabc " ; string T = " ab " ; countSubstrings ( S , T ) ; return 0 ; }
Minimum flips of odd indexed elements from odd length subarrays to make two given arrays equal | C ++ program for the above approach ; Function to find the minimum flip of subarrays required at alternate index to make binary arrays equals ; Stores count of total operations ; Stores count of consecutive unequal elements ; Loop to run on odd positions ; Incrementing the global counter ; Change count to 0 ; If all last elements are equal ; Loop to run on even positions ; Incrementing the global counter ; Change count to 0 ; Print the minimum operations ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minOperation ( int X [ ] , int Y [ ] , int n ) { int C = 0 ; int count = 0 ; for ( int i = 1 ; i < n ; i = i + 2 ) { if ( X [ i ] != Y [ i ] ) { count ++ ; } else { if ( count != 0 ) C ++ ; count = 0 ; } } if ( count != 0 ) C ++ ; count = 0 ; for ( int i = 0 ; i < n ; i = i + 2 ) { if ( X [ i ] != Y [ i ] ) { count ++ ; } else { if ( count != 0 ) C ++ ; count = 0 ; } } if ( count != 0 ) C ++ ; cout << C ; } int main ( ) { int X [ ] = { 1 , 0 , 0 , 0 , 0 , 1 } ; int Y [ ] = { 1 , 1 , 0 , 1 , 1 , 1 } ; int N = sizeof ( X ) / sizeof ( X [ 0 ] ) ; minOperation ( X , Y , N ) ; return 0 ; }
Minimum subarray reversals required to make given binary array alternating | C ++ program to implement the above approach ; Function to count minimum subarray reversal operations required to make array alternating ; Stores minimum count of operations required to make array alternating ; Traverse the array ; If arr [ i ] is greater than arr [ i + 1 ] ; Update cntOp ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumcntOperationReq ( int arr [ ] , int N ) { int cntOp = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { cntOp ++ ; } } return ( cntOp + 1 ) / 2 ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 , 0 , 1 , 0 , 0 , 0 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minimumcntOperationReq ( arr , N ) ; return 0 ; }
Modify sequence of first N natural numbers to a given array by replacing pairs with their GCD | C ++ program for the above approach ; Function to check if array arr [ ] can be obtained from first N natural numbers or not ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void isSequenceValid ( vector < int > & B , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( ( i + 1 ) % B [ i ] != 0 ) { cout << " No " ; return ; } } cout << " Yes " ; } int main ( ) { int N = 4 ; vector < int > arr { 1 , 2 , 3 , 2 } ; isSequenceValid ( arr , N ) ; return 0 ; }
Construct an array of first N natural numbers such that every adjacent pair is coprime | C ++ program to implement the above approach ; Function to construct an arrary with adjacent elements as co - prime numbers ; Iterate over the range [ 1 , N ] ; Print i ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void ConstArrayAdjacentCoprime ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) { cout << i << " ▁ " ; } } int main ( ) { int N = 6 ; ConstArrayAdjacentCoprime ( N ) ; return 0 ; }
Maximize count of rows consisting of equal elements by flipping columns of a Matrix | C ++ program to implement the above approach ; Function to find the maximum number of rows containing all equal elements ; Store each row of the matrix ; Traverse each row of the matrix ; Update frequency of current row ; Stores maximum count of rows containing all equal elements ; Traverse each row of the matrix ; Stores 1 's complement  of the current row ; Traverse current row of the given matrix ; Stores 1 s complement of mat [ i ] [ j ] ; Update cntMaxRows ; Driver Code ; Stores count of rows ; Stores count of columns
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxEqrows ( vector < vector < int > > & mat , int N , int M ) { map < vector < int > , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { mp [ mat [ i ] ] ++ ; } int cntMaxRows = 0 ; for ( int i = 0 ; i < N ; i ++ ) { vector < int > onesCompRow ( M , 0 ) ; for ( int j = 0 ; j < M ; j ++ ) { onesCompRow [ j ] = ( mat [ i ] [ j ] ^ 1 ) ; } cntMaxRows = max ( cntMaxRows , mp [ mat [ i ] ] + mp [ onesCompRow ] ) ; } return cntMaxRows ; } int main ( ) { vector < vector < int > > mat = { { 0 , 1 , 0 , 0 , 1 } , { 1 , 1 , 0 , 1 , 1 } , { 1 , 0 , 1 , 1 , 0 } } ; int N = mat . size ( ) ; int M = mat [ 0 ] . size ( ) ; cout << maxEqrows ( mat , N , M ) ; }
Minimum cost to convert one given string to another using swap , insert or delete operations | C ++ program for the above approach ; Function to find the minimum cost to convert string a to b ; Stores the frequency of string a and b respectively ; Store the frequencies of characters in a ; Store the frequencies of characters in b ; Minimum cost to convert A to B ; Find the minimum cost ; Print the minimum cost ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumCost ( string a , string b ) { vector < int > fre1 ( 256 ) , fre2 ( 256 ) ; for ( char c : a ) fre1 [ ( int ) ( c ) ] ++ ; for ( char c : b ) fre2 [ ( int ) ( c ) ] ++ ; int mincost = 0 ; for ( int i = 0 ; i < 256 ; i ++ ) { mincost += abs ( fre1 [ i ] - fre2 [ i ] ) ; } cout << mincost << endl ; } int main ( ) { string A = "1AB + - " , B = " cc " ; minimumCost ( A , B ) ; return 0 ; }
Minimum cost required to move all elements to the same position | C ++ program to implement the above approach ; Function to find the minimum cost required to place all elements in the same position ; Stores the count of even and odd elements ; Traverse the array arr [ ] ; Count even elements ; Count odd elements ; Print the minimum count ; Driver Code ; Given array ; Function Call
#include <iostream> NEW_LINE using namespace std ; int minCost ( int arr [ ] , int arr_size ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < arr_size ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } cout << min ( even , odd ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 } ; int arr_size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minCost ( arr , arr_size ) ; }
Maximize array sum by alternating the signs of adjacent elements | C ++ program to implement the above approach ; Function to find the maximum sum by alternating the signs of adjacent elements of the array ; Stores count of negative elements in the array ; Stores maximum sum by alternating the signs of adjacent elements ; Stores smallest absolute value of array elements ; Stores sum of absolute value of array elements ; Traverse the array ; If arr [ i ] is a negative number ; Update cntNeg ; Update sum ; Update SmValue ; Update MaxAltSum ; If cntNeg is an odd number ; Update MaxAltSum ; Drivers Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxSumByAlternatingSign ( int arr [ ] , int N ) { int cntNeg = 0 ; int MaxAltSum = 0 ; int SmValue = 0 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 0 ) { cntNeg += 1 ; } sum += abs ( arr [ i ] ) ; SmValue = min ( SmValue , abs ( arr [ i ] ) ) ; } MaxAltSum = sum ; if ( cntNeg & 1 ) { MaxAltSum -= 2 * SmValue ; } return MaxAltSum ; } int main ( ) { int arr [ ] = { 1 , 1 , -2 , -4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMaxSumByAlternatingSign ( arr , N ) ; }
Construct array with sum of product of same indexed elements in the given array equal to zero | C ++ program to implement the above approach ; Function to generate a new array with product of same indexed elements with arr [ ] equal to 0 ; Stores sum of same indexed array elements of arr and new array ; Traverse the array ; If i is an even number ; Insert arr [ i + 1 ] into the new array newArr [ ] ; Insert - arr [ i - 1 ] into the new array newArr [ ] ; Print new array elements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void constructNewArraySumZero ( int arr [ ] , int N ) { int newArr [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { newArr [ i ] = arr [ i + 1 ] ; } else { newArr [ i ] = - arr [ i - 1 ] ; } } for ( int i = 0 ; i < N ; i ++ ) { cout << newArr [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , -5 , -6 , 8 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; constructNewArraySumZero ( arr , N ) ; return 0 ; }
Generate Bitonic Sequence of length N from integers in a given range | C ++ program for the above approach ; Function to construct bitonic sequence of length N from integers in the range [ L , R ] ; If sequence is not possible ; Store the resultant list ; If size of deque < n ; Add elements from start ; Print the stored in the list ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void bitonicSequence ( int num , int lower , int upper ) { if ( num > ( upper - lower ) * 2 + 1 ) { cout << -1 ; return ; } deque < int > ans ; deque < int > :: iterator j = ans . begin ( ) ; for ( int i = 0 ; i < min ( upper - lower + 1 , num - 1 ) ; i ++ ) ans . push_back ( upper - i ) ; for ( int i = 0 ; i < num - ans . size ( ) ; i ++ ) ans . push_front ( upper - i - 1 ) ; cout << ' [ ' ; for ( j = ans . begin ( ) ; j != ans . end ( ) ; ++ j ) cout << ' ▁ ' << * j ; cout << ' ▁ ' << ' ] ' ; } int main ( ) { int N = 5 , L = 3 , R = 10 ; bitonicSequence ( N , L , R ) ; return 0 ; }