codeparrot/xlcost-text-to-code huggingface dataset:
Minimum increment / decrement operations required on Array to satisfy given conditions | C ++ program for the above approach ; Function to find minimum number of operations to get desired array ; For odd ' i ' , sum of elements till ' i ' is positive ; If i is even and sum is positive , make it negative by subtracting 1 + | s | from a [ i ] ; If i is odd and sum is negative , make it positive by adding 1 + | s | into a [ i ] ; For odd ' i ' , sum of elements till ' i ' is negative ; Check if ' i ' is odd and sum is positive , make it negative by subtracting 1 + | s | from a [ i ] ; Check if ' i ' is even and sum is negative , make it positive by adding 1 + | s | into a [ i ] ; Return the minimum of the two ; Driver Code ; Given array arr [ ] ; Function Call
#include <iostream> NEW_LINE using namespace std ; int minOperations ( int a [ ] , int N ) { int num_of_ops1 , num_of_ops2 , sum ; num_of_ops1 = num_of_ops2 = sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; if ( i % 2 == 0 && sum >= 0 ) { num_of_ops1 += ( 1 + abs ( sum ) ) ; sum = -1 ; } else if ( i % 2 == 1 && sum <= 0 ) { num_of_ops1 += ( 1 + abs ( sum ) ) ; sum = 1 ; } } sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; if ( i % 2 == 1 && sum >= 0 ) { num_of_ops2 += ( 1 + abs ( sum ) ) ; sum = -1 ; } else if ( i % 2 == 0 && sum <= 0 ) { num_of_ops2 += ( 1 + abs ( sum ) ) ; sum = 1 ; } } return min ( num_of_ops1 , num_of_ops2 ) ; } int main ( ) { int arr [ ] = { 3 , -4 , 5 , 0 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minOperations ( arr , N ) ; return 0 ; }
Check if all elements of a Circular Array can be made equal by increments of adjacent pairs | C ++ Program to implement the above approach ; Function to check if all array elements can be made equal ; Stores the sum of even and odd array elements ; If index is odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkEquall ( int arr [ ] , int N ) { int sumEven = 0 , sumOdd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i & 1 ) sumOdd += arr [ i ] ; else sumEven += arr [ i ] ; } if ( sumEven == sumOdd ) return true ; else return false ; } int main ( ) { int arr [ ] = { 2 , 7 , 3 , 5 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( checkEquall ( arr , N ) ) cout << " YES " << endl ; else cout << " NO " << endl ; return 0 ; }
Check if a number can be expressed as sum of two Perfect powers | C ++ program for the above approach ; Function that returns true if n can be written as a ^ m + b ^ n ; Taking isSum boolean array for check the sum exist or not ; To store perfect squares ; Initially all sums as false ; If sum exist then push that sum into perfect square vector ; Mark all perfect powers as false ; Traverse each perfectPowers ; Calculating Sum with perfect powers array ; Driver Code ; Given Number n ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isSumOfPower ( int n ) { bool isSum [ n + 1 ] ; vector < int > perfectPowers ; perfectPowers . push_back ( 1 ) ; for ( int i = 0 ; i < ( n + 1 ) ; i ++ ) { isSum [ i ] = false ; } for ( long long int i = 2 ; i < ( n + 1 ) ; i ++ ) { if ( isSum [ i ] == true ) { perfectPowers . push_back ( i ) ; continue ; } for ( long long int j = i * i ; j > 0 && j < ( n + 1 ) ; j *= i ) { isSum [ j ] = true ; } } for ( int i = 0 ; i < perfectPowers . size ( ) ; i ++ ) { isSum [ perfectPowers [ i ] ] = false ; } for ( int i = 0 ; i < perfectPowers . size ( ) ; i ++ ) { for ( int j = i ; j < perfectPowers . size ( ) ; j ++ ) { int sum = perfectPowers [ i ] + perfectPowers [ j ] ; if ( sum < ( n + 1 ) ) isSum [ sum ] = true ; } } return isSum [ n ] ; } int main ( ) { int n = 9 ; if ( isSumOfPower ( n ) ) { cout << " true STRNEWLINE " ; } else { cout << " false STRNEWLINE " ; } }
Minimum increments by index value required to obtain at least two equal Array elements | C ++ Program to implement the above approach ; Function to update every element adding to it its index value ; Function to check if at least two elements are equal or not ; Count the frequency of arr [ i ] ; Function to calculate the number of increment operations required ; Stores the minimum number of steps ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void update ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] += ( i + 1 ) ; } } bool check ( int arr [ ] , int N ) { bool f = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { count ++ ; } } if ( count >= 2 ) { f = 1 ; break ; } } if ( f == 1 ) return true ; else return false ; } void incrementCount ( int arr [ ] , int N ) { int min = 0 ; while ( check ( arr , N ) != true ) { update ( arr , N ) ; min ++ ; } cout << min ; } int main ( ) { int N = 3 ; int arr [ N ] = { 12 , 8 , 4 } ; incrementCount ( arr , N ) ; return 0 ; }
Rearrange array such that all even | C ++ Program to implement the above approach ; Function to check if it the array can be rearranged such such that every even indices contains an even element ; Stores the count of even elements ; Traverse array to count even numbers ; If even_no_count exceeds count of even indices ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkPossible ( int a [ ] , int n ) { int even_no_count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 0 ) even_no_count ++ ; } if ( n / 2 > even_no_count ) { cout << " No " << endl ; return ; } cout << " Yes " << endl ; int j = 0 ; for ( int i = 1 ; i < n ; i += 2 ) { if ( a [ i ] % 2 == 0 ) continue ; else { while ( j < n && a [ j ] % 2 != 0 ) j += 2 ; a [ i ] += a [ j ] ; a [ j ] = a [ i ] - a [ j ] ; a [ i ] -= a [ j ] ; } } for ( int i = 0 ; i < n ; i ++ ) { cout << a [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; checkPossible ( arr , n ) ; return 0 ; }
Largest number M having bit count of N such that difference between their OR and XOR value is maximized | C ++ program for the above approach ; Function to find the largest number M having the same length in binary form as N such that the difference between N | M and N ^ M is maximum ; Find the most significant bit of N ; Initialize M ; Set all the bits of M ; Return the answer ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxORminusXOR ( int N ) { int MSB = log2 ( N ) ; int M = 0 ; for ( int i = 0 ; i <= MSB ; i ++ ) M += ( 1 << i ) ; return M ; } int main ( ) { int N = 10 ; cout << maxORminusXOR ( N ) ; return 0 ; }
Maximum count of Equilateral Triangles that can be formed within given Equilateral Triangle | C ++ program for the above approach ; Function to find the number of equilateral triangle formed within another triangle ; Check for the valid condition ; Number of triangles having upward peak ; Number of inverted triangles ; Total no . of K sized triangle ; Driver Code ; Given N and K ; Function Call
#include <iostream> NEW_LINE using namespace std ; int No_of_Triangle ( int N , int K ) { if ( N < K ) return -1 ; else { int Tri_up = 0 ; Tri_up = ( ( N - K + 1 ) * ( N - K + 2 ) ) / 2 ; int Tri_down = 0 ; Tri_down = ( ( N - 2 * K + 1 ) * ( N - 2 * K + 2 ) ) / 2 ; return Tri_up + Tri_down ; } } int main ( ) { int N = 4 , K = 2 ; cout << No_of_Triangle ( N , K ) ; return 0 ; }
Check if Array forms an increasing | C ++ program for the above approach ; Function to check if the given array forms an increasing decreasing sequence or vice versa ; Base Case ; First subarray is stricly increasing ; Check for strictly increasing condition & find the break point ; Check for strictly decreasing condition & find the break point ; If i is equal to length of array ; First subarray is strictly Decreasing ; Check for strictly increasing condition & find the break point ; Check for strictly increasing condition & find the break point ; If i is equal to length of array - 1 ; Condition if ar [ 0 ] == ar [ 1 ] ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool canMake ( int n , int ar [ ] ) { if ( n == 1 ) return true ; else { if ( ar [ 0 ] < ar [ 1 ] ) { int i = 1 ; while ( i < n && ar [ i - 1 ] < ar [ i ] ) { i ++ ; } while ( i + 1 < n && ar [ i ] > ar [ i + 1 ] ) { i ++ ; } if ( i >= n - 1 ) return true ; else return false ; } else if ( ar [ 0 ] > ar [ 1 ] ) { int i = 1 ; while ( i < n && ar [ i - 1 ] > ar [ i ] ) { i ++ ; } while ( i + 1 < n && ar [ i ] < ar [ i + 1 ] ) { i ++ ; } if ( i >= n - 1 ) return true ; else return false ; } else { for ( int i = 2 ; i < n ; i ++ ) { if ( ar [ i - 1 ] <= ar [ i ] ) return false ; } return true ; } } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof arr / sizeof arr [ 0 ] ; if ( canMake ( n , arr ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Minimum Count of Bit flips required to make a Binary String Palindromic | C ++ Program to implement the above approach ; Function to calculate the length of the binary string ; Length ; Right shift of n ; Increment the length ; Return the length ; Function to check if the bit present at i - th position is a set bit or not ; Returns true if the bit is set ; Function to count the minimum number of bit flips required ; Length of the binary form ; Number of flips ; Pointer to the LSB ; Pointer to the MSB ; Check if the bits are equal ; Decrementing the left pointer ; Incrementing the right pointer ; Returns the number of bits to flip . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int check_length ( int n ) { int ans = 0 ; while ( n ) { n = n >> 1 ; ans ++ ; } return ans ; } int check_ith_bit ( int n , int i ) { return ( n & ( 1 << ( i - 1 ) ) ) ? true : false ; } int no_of_flips ( int n ) { int len = check_length ( n ) ; int ans = 0 ; int right = 1 ; int left = len ; while ( right < left ) { if ( check_ith_bit ( n , right ) != check_ith_bit ( n , left ) ) ans ++ ; left -- ; right ++ ; } return ans ; } int main ( ) { int n = 12 ; cout << no_of_flips ( n ) ; return 0 ; }
Split N into two integers whose addition to A and B makes them equal | C ++ Program to implement the above approach ; Function to calculate the splitted numbers ; Calculate X ; If X is odd ; No pair is possible ; Otherwise ; Calculate X ; Calculate Y ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPair ( int A , int B , int N ) { int X , Y ; X = N - B + A ; if ( X % 2 != 0 ) { cout << " - 1" ; } else { X = X / 2 ; Y = N - X ; cout << X << " ▁ " << Y ; } } int main ( ) { int A = 1 ; int B = 3 ; int N = 4 ; findPair ( A , B , N ) ; return 0 ; }
Find the amplitude and number of waves for the given array | C ++ program for the above approach ; Function to find the amplitude and number of waves for the given array ; Check for both sides adjacent elements that both must be less or both must be greater than current element ; Update amplitude with max value ; Print the Amplitude ; Driver Code ; Given array a [ ] ; Calculate number of waves ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int a [ ] , int n ) { int ma = a [ 1 ] - a [ 0 ] ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( ( a [ i ] > a [ i - 1 ] && a [ i + 1 ] < a [ i ] ) || ( a [ i ] < a [ i - 1 ] && a [ i + 1 ] > a [ i ] ) ) ma = max ( ma , abs ( a [ i ] - a [ i + 1 ] ) ) ; else return false ; } cout << " Amplitude ▁ = ▁ " << ma ; cout << endl ; return true ; } int main ( ) { int a [ ] = { 1 , 2 , 1 , 5 , 0 , 7 , -6 } ; int n = sizeof a / sizeof a [ 0 ] ; int wave = ( n - 1 ) / 2 ; if ( check ( a , n ) ) cout << " Waves ▁ = ▁ " << wave ; else cout << " - 1" ; return 0 ; }
Find relative rank of each element in array | C ++ program for the above approach ; Function to find relative rank for each element in the array A [ ] ; Create Rank Array ; Stack to store numbers in non - decreasing order from right ; Push last element in stack ; Iterate from second last element to first element ; If current element is less than the top of stack and push A [ i ] in stack ; Rank is stack size - 1 for current element ; Pop elements from stack till current element is greater than the top ; Push current element in Stack ; Rank is stack size - 1 ; Print rank of all elements ; Driver Code ; Given array A [ ] ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findRank ( int A [ ] , int N ) { int rank [ N ] = { } ; stack < int > s ; s . push ( A [ N - 1 ] ) ; for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( A [ i ] < s . top ( ) ) { s . push ( A [ i ] ) ; rank [ i ] = s . size ( ) - 1 ; } else { while ( ! s . empty ( ) && A [ i ] >= s . top ( ) ) { s . pop ( ) ; } s . push ( A [ i ] ) ; rank [ i ] = s . size ( ) - 1 ; } } for ( int i = 0 ; i < N ; i ++ ) { cout << rank [ i ] << " ▁ " ; } } int main ( ) { int A [ ] = { 1 , 2 , 3 , 5 , 4 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; findRank ( A , N ) ; return 0 ; }
Largest string obtained in Dictionary order after deleting K characters | C ++ program for the above approach ; Function to find the largest string after deleting k characters ; Deque dq used to find the largest string in dictionary after deleting k characters ; Iterate till the length of the string ; Condition for popping characters from deque ; To store the resultant string ; To form resultant string ; Return the resultant string ; Driver code ; Given String ; Function call ; Print the answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; string largestString ( int n , int k , string s ) { deque < char > deq ; for ( int i = 0 ; i < n ; ++ i ) { while ( deq . size ( ) > 0 && deq . back ( ) < s [ i ] && k > 0 ) { deq . pop_front ( ) ; k -- ; } deq . push_back ( s [ i ] ) ; } string st = " " ; for ( char c : deq ) st = st + c ; return st ; } int main ( ) { int n = 4 ; int k = 2 ; string sc = " ritz " ; string result = largestString ( n , k , sc ) ; cout << result << endl ; return 0 ; }
Count total set bits in all numbers from range L to R | C ++ program for the above approach ; Function that counts the set bits from 0 to N ; To store sum of set bits from 0 - N ; Until n >= to 2 ^ i ; This k will get flipped after 2 ^ i iterations ; Change is iterator from 2 ^ i to 1 ; This will loop from 0 to n for every bit position ; When change = 1 flip the bit ; Again set change to 2 ^ i ; Increment the position ; Function that counts the set bit in the range ( L , R ) ; Return the count ; Driver Code ; Given L and R ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSetBit ( int n ) { int i = 0 ; int ans = 0 ; while ( ( 1 << i ) <= n ) { bool k = 0 ; int change = 1 << i ; for ( int j = 0 ; j <= n ; j ++ ) { ans += k ; if ( change == 1 ) { k = ! k ; change = 1 << i ; } else { change -- ; } } i ++ ; } return ans ; } int countSetBits ( int L , int R ) { return abs ( countSetBit ( R ) - countSetBit ( L - 1 ) ) ; } int main ( ) { int L = 3 , R = 5 ; cout << " Total ▁ set ▁ bit ▁ count ▁ is ▁ " << countSetBits ( L , R ) << endl ; return 0 ; }
Count total set bits in all numbers from range L to R | C ++ program for the above approach ; Function to count set bit in range ; Count variable ; Find the set bit in Nth number ; If last bit is set ; Left sift by one bit ; Return count ; Driver Code ; Given Range L and R ; Function Call
#include <iostream> NEW_LINE using namespace std ; int countSetBits ( int L , int R ) { int count = 0 ; for ( int i = L ; i <= R ; i ++ ) { int n = i ; while ( n > 0 ) { count += ( n & 1 ) ; n = n >> 1 ; } } return count ; } int main ( ) { int L = 3 , R = 5 ; cout << " Total ▁ set ▁ Bit ▁ count ▁ is ▁ " << countSetBits ( L , R ) ; return 0 ; }
Count total set bits in all numbers from range L to R | C ++ program for the above approach ; Function to count set bit in [ L , R ] ; Variable for count set bit in range ; Count set bit for all number in range ; Use inbuilt function ; Driver Code ; Given range L and R ; Function Call
#include <iostream> NEW_LINE using namespace std ; int countSetBits ( int L , int R ) { int count = 0 ; for ( int i = L ; i <= R ; i ++ ) { count += __builtin_popcount ( i ) ; } return count ; } int main ( ) { int L = 3 , R = 5 ; cout << " Total ▁ set ▁ bit ▁ count ▁ is ▁ " << countSetBits ( L , R ) ; return 0 ; }
Make max elements in B [ ] equal to that of A [ ] by adding / subtracting integers in range [ 0 , K ] | C ++ program for the above approach ; Function that count the number of integers from array B [ ] such that subtracting element in the range [ 0 , K ] given any element in A [ ] ; To store the count of element ; Traverse the array B [ ] ; Traverse the array A [ ] ; Find the difference ; If difference is atmost K then increment the cnt ; Print the count ; Driver Code ; Given array A [ ] and B [ ] ; Given K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countElement ( int A [ ] , int N , int B [ ] , int M , int K ) { int cnt = 0 ; for ( int i = 0 ; i < M ; i ++ ) { int currentElement = B [ i ] ; for ( int j = 0 ; j < N ; j ++ ) { int diff = abs ( currentElement - A [ j ] ) ; if ( diff <= K ) { cnt ++ ; break ; } } } cout << cnt ; } int main ( ) { int A [ ] = { 100 , 65 , 35 , 85 , 55 } ; int B [ ] = { 30 , 60 , 75 , 95 } ; int K = 5 ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int M = sizeof ( B ) / sizeof ( B [ 0 ] ) ; countElement ( A , N , B , M , K ) ; return 0 ; }
Generate a String of having N * N distinct non | C ++ Program to implement the above approach ; Function to construct a string having N * N non - palindromic substrings ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void createString ( int N ) { for ( int i = 0 ; i < N ; i ++ ) { cout << ' a ' ; } for ( int i = 0 ; i < N ; i ++ ) { cout << ' b ' ; } } int main ( ) { int N = 4 ; createString ( N ) ; return 0 ; }
Maximum Subset Sum possible by negating the entire sum after selecting the first Array element | C ++ Program to implement the above approach ; Function returns maximum subset sum from the given array = ; Case 2 : Negate values from A [ 1 ] to A [ N - 1 ] ; Include only positives for max subset sum ; Return max sum obtained ; Function to return maximum of the maximum subset sum calculated for the two cases ; Case 1 ; Case 2 ; Modifying the sum ; Including first element ; Negating again ; Return the required answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSubset ( vector < int > & A , bool flag ) { int n = A . size ( ) ; int sum = 0 ; if ( flag ) { for ( int i = 1 ; i < n ; i ++ ) A [ i ] = - A [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] > 0 ) { sum += A [ i ] ; } } return sum ; } int findBest ( vector < int > A ) { int x = maxSubset ( A , 0 ) ; int y = maxSubset ( A , 1 ) ; y = - y ; y += A [ 0 ] ; y = - y ; return max ( x , y ) ; } int main ( ) { vector < int > A = { 1 , 10 , 4 , -6 , 3 } ; cout << findBest ( A ) << endl ; return 0 ; }
Construct an Array of Strings having Longest Common Prefix specified by the given Array | C ++ Program to implement the above approach ; Function to find the array of strings ; Marks the ( N + 1 ) th string ; To generate remaining N strings ; Find i - th string using ( i + 1 ) - th string ; Check if current character is b ; Otherwise ; Insert the string ; Return the answer ; Driver Code ; Print the strings
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < string > solve ( int n , int arr [ ] ) { string s = string ( n , ' a ' ) ; vector < string > ans ; ans . push_back ( s ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { char ch = s [ arr [ i ] ] ; if ( ch == ' b ' ) ch = ' a ' ; else ch = ' b ' ; s [ arr [ i ] ] = ch ; ans . push_back ( s ) ; } return ans ; } int main ( ) { int arr [ ] = { 2 , 0 , 3 } ; int n = sizeof arr / sizeof arr [ 0 ] ; vector < string > ans = solve ( n , arr ) ; for ( int i = ans . size ( ) - 1 ; i >= 0 ; i -- ) { cout << ans [ i ] << endl ; } return 0 ; }
Minimize swaps required to maximize the count of elements replacing a greater element in an Array | C ++ Program to implement the above approach ; Function to find the minimum number of swaps required ; Sort the array in ascending order ; Iterate until a greater element is found ; Keep incrementing ind ; If a greater element is found ; Increase count of swap ; Increment ind ; If end of array is reached ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSwaps ( int A [ ] , int n ) { sort ( A , A + n ) ; int ind = 1 , res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( ind < n and A [ ind ] == A [ i ] ) ind ++ ; if ( ind < n and A [ ind ] > A [ i ] ) { res ++ ; ind ++ ; } if ( ind >= n ) break ; } return res ; } int main ( ) { int A [ ] = { 4 , 3 , 3 , 2 , 5 } ; cout << countSwaps ( A , 5 ) ; return 0 ; }
Minimize swaps required to maximize the count of elements replacing a greater element in an Array | C ++ Program to implement the above approach ; Function to find the minimum number of swaps required ; Stores the frequency of the array elements ; Stores maximum frequency ; Find the max frequency ; Update frequency ; Update maximum frequency ; Driver Code ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSwaps ( int A [ ] , int n ) { map < int , int > mp ; int max_frequency = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mp [ A [ i ] ] ++ ; max_frequency = max ( max_frequency , mp [ A [ i ] ] ) ; } return n - max_frequency ; } int main ( ) { int A [ ] = { 6 , 5 , 4 , 3 , 2 , 1 } ; cout << countSwaps ( A , 6 ) ; return 0 ; }
Minimum number of steps required to obtain the given Array by the given operations | C ++ Program to implement the above approach ; Function to calculate the minimum steps to obtain the desired array ; Initialize variable ; Iterate over the array arr [ ] ; Check if i > 0 ; Update the answer ; Return the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int min_operation ( int a [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i > 0 ) ans += abs ( a [ i ] - a [ i - 1 ] ) ; else ans += abs ( a [ i ] ) ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << min_operation ( arr , n ) ; return 0 ; }
Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y | C ++ implementation of the above approach ; Function to print the required matrix ; For 1 * 1 matrix ; Greater number ; Smaller number ; Sets / Resets for alternate filling of the matrix ; Print the matrix ; If end of row is reached ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void FindMatrix ( int n , int m , int x , int y ) { int a , b , i , j ; if ( n * m == 1 ) { if ( x > y ) { cout << y << " STRNEWLINE " ; } else { cout << x << " STRNEWLINE " ; } return ; } a = min ( x , y ) ; b = min ( 2 * x , y ) - a ; bool flag = true ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { if ( flag ) cout << a << ' ▁ ' ; else cout << b << ' ▁ ' ; flag = ! flag ; } if ( ( ( n % 2 != 0 && m % 2 == 0 ) || ( n % 2 == 0 && m % 2 == 0 ) ) ) flag = ! flag ; cout << " STRNEWLINE " ; } } int main ( ) { int N , M , X , Y ; N = 3 ; M = 3 ; X = 5 ; Y = 3 ; FindMatrix ( N , M , X , Y ) ; }