codeparrot/xlcost-text-to-code huggingface dataset:
Number of swaps to sort when only adjacent swapping allowed | C ++ program to count number of swaps required to sort an array when only swapping of adjacent elements is allowed . ; This function merges two sorted arrays and returns inversion count in the arrays . ; i is index for left subarray ; j is index for right subarray ; k is index for resultant merged subarray ; this is tricky -- see above explanation / diagram for merge ( ) ; Copy the remaining elements of left subarray ( if there are any ) to temp ; Copy the remaining elements of right subarray ( if there are any ) to temp ; Copy back the merged elements to original array ; An auxiliary recursive function that sorts the input array and returns the number of inversions in the array . ; Divide the array into two parts and call _mergeSortAndCountInv ( ) for each of the parts ; Inversion count will be sum of inversions in left - part , right - part and number of inversions in merging ; Merge the two parts ; This function sorts the input array and returns the number of inversions in the array ; Driver progra to test above functions
#include <bits/stdc++.h> NEW_LINE int merge ( int arr [ ] , int temp [ ] , int left , int mid , int right ) { int inv_count = 0 ; int i = left ; int j = mid ; int k = left ; while ( ( i <= mid - 1 ) && ( j <= right ) ) { if ( arr [ i ] <= arr [ j ] ) temp [ k ++ ] = arr [ i ++ ] ; else { temp [ k ++ ] = arr [ j ++ ] ; inv_count = inv_count + ( mid - i ) ; } } while ( i <= mid - 1 ) temp [ k ++ ] = arr [ i ++ ] ; while ( j <= right ) temp [ k ++ ] = arr [ j ++ ] ; for ( i = left ; i <= right ; i ++ ) arr [ i ] = temp [ i ] ; return inv_count ; } int _mergeSort ( int arr [ ] , int temp [ ] , int left , int right ) { int mid , inv_count = 0 ; if ( right > left ) { mid = ( right + left ) / 2 ; inv_count = _mergeSort ( arr , temp , left , mid ) ; inv_count += _mergeSort ( arr , temp , mid + 1 , right ) ; inv_count += merge ( arr , temp , left , mid + 1 , right ) ; } return inv_count ; } int countSwaps ( int arr [ ] , int n ) { int temp [ n ] ; return _mergeSort ( arr , temp , 0 , n - 1 ) ; } int main ( int argv , char * * args ) { int arr [ ] = { 1 , 20 , 6 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( " Number ▁ of ▁ swaps ▁ is ▁ % d ▁ STRNEWLINE " , countSwaps ( arr , n ) ) ; return 0 ; }
Check whether a given number is even or odd | A simple C ++ program to check for even or odd ; Returns true if n is even , else odd ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isEven ( int n ) { return ( n % 2 == 0 ) ; } int main ( ) { int n = 101 ; isEven ( n ) ? cout << " Even " : cout << " Odd " ; return 0 ; }
Find Surpasser Count of each element in array | Naive C ++ program to find surpasser count of each element in array ; Function to find surpasser count of each element in array ; stores surpasser count for element arr [ i ] ; Function to print an array ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSurpasser ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ j ] > arr [ i ] ) count ++ ; cout << count << " ▁ " ; } } void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) printf ( " % d ▁ " , arr [ i ] ) ; printf ( " STRNEWLINE " ) ; } int main ( ) { int arr [ ] = { 2 , 7 , 5 , 3 , 0 , 8 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( " Given ▁ array ▁ is ▁ STRNEWLINE " ) ; printArray ( arr , n ) ; printf ( " Surpasser ▁ Count ▁ of ▁ array ▁ is ▁ STRNEWLINE " ) ; findSurpasser ( arr , n ) ; return 0 ; }
Minimum sum of two numbers formed from digits of an array | C ++ program to find minimum sum of two numbers formed from digits of the array . ; Function to find and return minimum sum of two numbers formed from digits of the array . ; sort the array ; let two numbers be a and b ; fill a and b with every alternate digit of input array ; return the sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int a = 0 , b = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i & 1 ) a = a * 10 + arr [ i ] ; else b = b * 10 + arr [ i ] ; } return a + b ; } int main ( ) { int arr [ ] = { 6 , 8 , 4 , 5 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Sum ▁ is ▁ " << solve ( arr , n ) ; return 0 ; }
Maximum product of a triplet ( subsequence of size 3 ) in array | A C ++ program to find a maximum product of a triplet in array of integers ; Function to find a maximum product of a triplet in array of integers of size n ; if size is less than 3 , no triplet exists ; will contain max product ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxProduct ( int arr [ ] , int n ) { if ( n < 3 ) return -1 ; int max_product = INT_MIN ; for ( int i = 0 ; i < n - 2 ; i ++ ) for ( int j = i + 1 ; j < n - 1 ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) max_product = max ( max_product , arr [ i ] * arr [ j ] * arr [ k ] ) ; return max_product ; } int main ( ) { int arr [ ] = { 10 , 3 , 5 , 6 , 20 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int max = maxProduct ( arr , n ) ; if ( max == -1 ) cout << " No ▁ Triplet ▁ Exists " ; else cout << " Maximum ▁ product ▁ is ▁ " << max ; return 0 ; }
Maximum product of a triplet ( subsequence of size 3 ) in array | A C ++ program to find a maximum product of a triplet in array of integers ; Function to find a maximum product of a triplet in array of integers of size n ; if size is less than 3 , no triplet exists ; Construct four auxiliary vectors of size n and initialize them by - 1 ; will contain max product ; to store maximum element on left of array ; to store minimum element on left of array ; leftMax [ i ] will contain max element on left of arr [ i ] excluding arr [ i ] . leftMin [ i ] will contain min element on left of arr [ i ] excluding arr [ i ] . ; reset max_sum to store maximum element on right of array ; reset min_sum to store minimum element on right of array ; rightMax [ i ] will contain max element on right of arr [ i ] excluding arr [ i ] . rightMin [ i ] will contain min element on right of arr [ i ] excluding arr [ i ] . ; For all array indexes i except first and last , compute maximum of arr [ i ] * x * y where x can be leftMax [ i ] or leftMin [ i ] and y can be rightMax [ i ] or rightMin [ i ] . ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxProduct ( int arr [ ] , int n ) { if ( n < 3 ) return -1 ; vector < int > leftMin ( n , -1 ) ; vector < int > rightMin ( n , -1 ) ; vector < int > leftMax ( n , -1 ) ; vector < int > rightMax ( n , -1 ) ; int max_product = INT_MIN ; int max_sum = arr [ 0 ] ; int min_sum = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { leftMax [ i ] = max_sum ; if ( arr [ i ] > max_sum ) max_sum = arr [ i ] ; leftMin [ i ] = min_sum ; if ( arr [ i ] < min_sum ) min_sum = arr [ i ] ; } max_sum = arr [ n - 1 ] ; min_sum = arr [ n - 1 ] ; for ( int j = n - 2 ; j >= 0 ; j -- ) { rightMax [ j ] = max_sum ; if ( arr [ j ] > max_sum ) max_sum = arr [ j ] ; rightMin [ j ] = min_sum ; if ( arr [ j ] < min_sum ) min_sum = arr [ j ] ; } for ( int i = 1 ; i < n - 1 ; i ++ ) { int max1 = max ( arr [ i ] * leftMax [ i ] * rightMax [ i ] , arr [ i ] * leftMin [ i ] * rightMin [ i ] ) ; int max2 = max ( arr [ i ] * leftMax [ i ] * rightMin [ i ] , arr [ i ] * leftMin [ i ] * rightMax [ i ] ) ; max_product = max ( max_product , max ( max1 , max2 ) ) ; } return max_product ; } int main ( ) { int arr [ ] = { 1 , 4 , 3 , -6 , -7 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int max = maxProduct ( arr , n ) ; if ( max == -1 ) cout << " No ▁ Triplet ▁ Exists " ; else cout << " Maximum ▁ product ▁ is ▁ " << max ; return 0 ; }
Maximum product of a triplet ( subsequence of size 3 ) in array | A C ++ program to find a maximum product of a triplet in array of integers ; Function to find a maximum product of a triplet in array of integers of size n ; if size is less than 3 , no triplet exists ; Sort the array in ascending order ; Return the maximum of product of last three elements and product of first two elements and last element ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxProduct ( int arr [ ] , int n ) { if ( n < 3 ) return -1 ; sort ( arr , arr + n ) ; return max ( arr [ 0 ] * arr [ 1 ] * arr [ n - 1 ] , arr [ n - 1 ] * arr [ n - 2 ] * arr [ n - 3 ] ) ; } int main ( ) { int arr [ ] = { -10 , -3 , 5 , 6 , -20 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int max = maxProduct ( arr , n ) ; if ( max == -1 ) cout << " No ▁ Triplet ▁ Exists " ; else cout << " Maximum ▁ product ▁ is ▁ " << max ; return 0 ; }
Maximum product of a triplet ( subsequence of size 3 ) in array | A O ( n ) C ++ program to find maximum product pair in an array . ; Function to find a maximum product of a triplet in array of integers of size n ; if size is less than 3 , no triplet exists ; Initialize Maximum , second maximum and third maximum element ; Initialize Minimum and second minimum element ; Update Maximum , second maximum and third maximum element ; Update second maximum and third maximum element ; Update third maximum element ; Update Minimum and second minimum element ; Update second minimum element ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxProduct ( int arr [ ] , int n ) { if ( n < 3 ) return -1 ; int maxA = INT_MIN , maxB = INT_MIN , maxC = INT_MIN ; int minA = INT_MAX , minB = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > maxA ) { maxC = maxB ; maxB = maxA ; maxA = arr [ i ] ; } else if ( arr [ i ] > maxB ) { maxC = maxB ; maxB = arr [ i ] ; } else if ( arr [ i ] > maxC ) maxC = arr [ i ] ; if ( arr [ i ] < minA ) { minB = minA ; minA = arr [ i ] ; } else if ( arr [ i ] < minB ) minB = arr [ i ] ; } return max ( minA * minB * maxA , maxA * maxB * maxC ) ; } int main ( ) { int arr [ ] = { 1 , -4 , 3 , -6 , 7 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int max = maxProduct ( arr , n ) ; if ( max == -1 ) cout << " No ▁ Triplet ▁ Exists " ; else cout << " Maximum ▁ product ▁ is ▁ " << max ; return 0 ; }
How to efficiently sort a big list dates in 20 's | C ++ program to sort an array of dates using Radix Sort ; Utilitiy function to obtain maximum date or month or year ; A function to do counting sort of arr [ ] according to given q i . e . ( 0 for day , 1 for month , 2 for year ) ; to extract last digit divide by p then % 10 Store count of occurrences in cnt [ ] ; Build the output array ; Copy the output array to arr [ ] , so that arr [ ] now contains sorted numbers according to current digit ; update p to get the next digit ; The main function that sorts array of dates using Radix Sort ; First sort by day ; Then by month ; Finally by year ; A utility function to print an array ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMax ( int arr [ ] [ 3 ] , int n , int q ) { int maxi = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { maxi = max ( maxi , arr [ i ] [ q ] ) ; } return maxi ; } void sortDatesUtil ( int arr [ ] [ 3 ] , int n , int q ) { int maxi = getMax ( arr , n , q ) ; int p = 1 ; while ( maxi > 0 ) { int cnt [ 10 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { cnt [ ( arr [ i ] [ q ] / p ) % 10 ] ++ ; } for ( int i = 1 ; i < 10 ; i ++ ) { cnt [ i ] += cnt [ i - 1 ] ; } int ans [ n ] [ 3 ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int lastDigit = ( arr [ i ] [ q ] / p ) % 10 ; for ( int j = 0 ; j < 3 ; j ++ ) { ans [ cnt [ lastDigit ] - 1 ] [ j ] = arr [ i ] [ j ] ; } cnt [ lastDigit ] -- ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { arr [ i ] [ j ] = ans [ i ] [ j ] ; } } p *= 10 ; maxi /= 10 ; } } void sortDates ( int dates [ ] [ 3 ] , int n ) { sortDatesUtil ( dates , n , 0 ) ; sortDatesUtil ( dates , n , 1 ) ; sortDatesUtil ( dates , n , 2 ) ; } void printArr ( int arr [ ] [ 3 ] , int n ) { for ( int i = 0 ; i < 6 ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { cout << arr [ i ] [ j ] << " ▁ " ; } cout << endl ; } } int main ( ) { int dates [ ] [ 3 ] = { { 20 , 1 , 2014 } , { 25 , 3 , 2010 } , { 3 , 12 , 2000 } , { 18 , 11 , 2000 } , { 19 , 4 , 2015 } , { 9 , 7 , 2005 } } ; int n = sizeof ( dates ) / sizeof ( dates [ 0 ] ) ; sortDates ( dates , n ) ; cout << " Sorted Dates " printArr ( dates , n ) ; return 0 ; }
Maximize sum of ratios of N given fractions by incrementing numerator and denominators K times by 1 | C ++ program for the above approach ; Function to increment the K fractions from the given array to maximize the sum of ratios of the given fractions ; Size of the array ; Max priority queue ; Iterate through the array ; Insert the incremented value if an operation is performed on the ith index ; Loop to perform K operations ; Increment the numerator and denominator of ith fraction ; Add the incremented value ; Stores the average ratio ; Return the ratio ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double maxAverageRatio ( vector < vector < int > > & arr , int K ) { int N = arr . size ( ) ; priority_queue < pair < double , int > > q ; for ( int i = 0 ; i < N ; i ++ ) { double extra = ( ( ( double ) arr [ i ] [ 0 ] + 1 ) / ( ( double ) arr [ i ] [ 1 ] + 1 ) ) - ( ( double ) arr [ i ] [ 0 ] / ( double ) arr [ i ] [ 1 ] ) ; q . push ( make_pair ( extra , i ) ) ; } while ( K -- ) { int i = q . top ( ) . second ; q . pop ( ) ; arr [ i ] [ 0 ] += 1 ; arr [ i ] [ 1 ] += 1 ; double extra = ( ( ( double ) arr [ i ] [ 0 ] + 1 ) / ( ( double ) arr [ i ] [ 1 ] + 1 ) ) - ( ( double ) arr [ i ] [ 0 ] / ( double ) arr [ i ] [ 1 ] ) ; q . push ( make_pair ( extra , i ) ) ; } double ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { ans += ( ( double ) arr [ i ] [ 0 ] / ( double ) arr [ i ] [ 1 ] ) ; } return ans / N ; } int main ( ) { vector < vector < int > > arr = { { 1 , 2 } , { 3 , 5 } , { 2 , 2 } } ; int K = 2 ; cout << maxAverageRatio ( arr , K ) ; return 0 ; }
Minimum operations to convert Binary Matrix A to B by flipping submatrix of size K | C ++ program for the above approach ; Function to count the operations required to convert matrix A to B ; Store the sizes of matrix ; Stores count of flips required ; Traverse the iven matrix ; Check if the matrix values are not equal ; Increment the count of moves ; Check if the current square sized exists or not ; Flip all the bits in this square sized submatrix ; Count of operations required ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minMoves ( vector < vector < char > > a , vector < vector < char > > b , int K ) { int n = a . size ( ) , m = a [ 0 ] . size ( ) ; int cntOperations = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( a [ i ] [ j ] != b [ i ] [ j ] ) { cntOperations ++ ; if ( i + K - 1 >= n j + K - 1 >= m ) { return -1 ; } for ( int p = 0 ; p <= K - 1 ; p ++ ) { for ( int q = 0 ; q <= K - 1 ; q ++ ) { if ( a [ i + p ] [ j + q ] == '0' ) { a [ i + p ] [ j + q ] = '1' ; } else { a [ i + p ] [ j + q ] = '0' ; } } } } } } return cntOperations ; } int main ( ) { vector < vector < char > > A = { { '1' , '0' , '0' } , { '0' , '0' , '0' } , { '0' , '0' , '0' } } ; vector < vector < char > > B = { { '0' , '0' , '0' } , { '0' , '0' , '0' } , { '0' , '0' , '0' } } ; int K = 3 ; cout << minMoves ( A , B , K ) ; return 0 ; }
Lexicographically largest permutation by sequentially inserting Array elements at ends | C ++ program for the above approach ; Function to find the lexicographically largest permutation by sequentially inserting the array elements ; Stores the current state of the new array ; Stores ythe current maximum element of array arr [ ] ; Iterate the array elements ; If the current element is smaller than the current maximum , then insert ; If the current element is at least the current maximum ; Update the value of the current maximum ; Print resultant permutation ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void largestPermutation ( int arr [ ] , int N ) { deque < int > p ; int mx = arr [ 0 ] ; p . push_back ( arr [ 0 ] ) ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] < mx ) p . push_back ( arr [ i ] ) ; else { p . push_front ( arr [ i ] ) ; mx = arr [ i ] ; } } for ( auto i : p ) cout << i << " ▁ " ; } int main ( ) { int arr [ ] = { 3 , 1 , 2 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; largestPermutation ( arr , N ) ; return 0 ; }
Largest value of K that a set of all possible subset | C ++ program for the above approach ; Function to find maximum count of consecutive integers from 0 in set S of all possible subset - sum ; Stores the maximum possible integer ; Sort the given array in non - decrasing order ; Iterate the given array ; If arr [ i ] <= X + 1 , then update X otherwise break the loop ; Return Answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxConsecutiveCnt ( vector < int > arr ) { int X = 0 ; sort ( arr . begin ( ) , arr . end ( ) ) ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { if ( arr [ i ] <= ( X + 1 ) ) { X = X + arr [ i ] ; } else { break ; } } return X + 1 ; } int main ( ) { vector < int > arr = { 1 , 1 , 1 , 4 } ; cout << maxConsecutiveCnt ( arr ) ; return 0 ; }
Maximum length of subarray with same sum at corresponding indices from two Arrays | C ++ program for the above approach ; Function to find maximum length of subarray of array A and B having equal sum ; Stores the maximum size of valid subarray ; Stores the prefix sum of the difference of the given arrays ; Traverse the given array ; Add the difference of the corresponding array element ; If current difference is not present ; If current difference is present , update the value of maxSize ; Return the maximum length ; Driver Code
#include " bits / stdc + + . h " NEW_LINE using namespace std ; int maxLength ( vector < int > & A , vector < int > & B ) { int n = A . size ( ) ; int maxSize = 0 ; unordered_map < int , int > pre ; int diff = 0 ; pre [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { diff += ( A [ i ] - B [ i ] ) ; if ( pre . find ( diff ) == pre . end ( ) ) { pre = i + 1 ; } else { maxSize = max ( maxSize , i - pre + 1 ) ; } } return maxSize ; } int main ( ) { vector < int > A = { 1 , 2 , 3 , 4 } ; vector < int > B = { 4 , 3 , 2 , 1 } ; cout << maxLength ( A , B ) ; return 0 ; }
Minimum jumps required to reach all array elements using largest element | C ++ program for the above approach ; Function to find next greater element to left and right of current element ; Starting l and r from previous and the next element of the current element ; FInd the next greater element to the left ; Find the next greater element to the right ; Return l and r in the form of array of size 2 ; Function to find the minimum jumps required to reach to all elements from the largest element ; Stores the mapping from index to the element in array A [ ] ; Stores largest array element ; Find the two indices l , r such that A [ l ] > A [ i ] < A [ r ] and l < i < r using expand function ; sorting A in descending order ; Stores the resultant minimum jumps required ; Check if the current element is largest or not ; Find the answer to the current element ; Update the resultant minimum jumps for the current element ; Return the result ; Driver Code ; Print the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ar  array NEW_LINE ar < int , 2 > expand ( int idx , vector < int > & A ) { int l = idx - 1 ; int r = idx + 1 ; while ( l >= 0 ) { if ( ( int ) ( A [ idx ] ) > A [ l ] ) { -- l ; } else { break ; } } if ( l < 0 l == idx ) { l = -2 ; } while ( r < ( int ) ( A . size ( ) ) ) { if ( ( int ) A [ idx ] > A [ r ] ) { ++ r ; } else { break ; } } if ( r >= ( int ) ( A . size ( ) ) r == idx ) { r = -2 ; } return { l , r } ; } vector < int > minJumps ( int N , vector < int > & A ) { vector < int > ans ( N , 0 ) ; map < int , ar < int , 2 > > mp ; map < int , int > iToA ; map < int , int > AToi ; int big = A [ 0 ] ; for ( int i = 0 ; i < N ; ++ i ) { big = max ( { big , A [ i ] } ) ; mp [ i ] = expand ( i , A ) ; iToA [ i ] = A [ i ] ; AToi [ A [ i ] ] = i ; } sort ( A . begin ( ) , A . end ( ) , greater < int > ( ) ) ; for ( int i = 0 ; i < A . size ( ) ; ++ i ) { int m ; if ( A [ i ] == big ) { int cur = AToi [ A [ i ] ] ; ans [ cur ] = 0 ; continue ; } int cur = AToi [ A [ i ] ] ; int l = mp [ cur ] [ 0 ] ; int r = mp [ cur ] [ 1 ] ; if ( l >= 0 && r < N ) { m = min ( ans [ l ] , ans [ r ] ) + 1 ; } else if ( l < 0 && r < N ) { m = ans [ r ] + 1 ; } else if ( l >= 0 && r >= N ) { m = ans [ l ] + 1 ; } ans [ cur ] = m ; } return ans ; } int main ( ) { vector < int > arr = { 5 , 1 , 3 , 4 , 7 } ; int N = arr . size ( ) ; vector < int > out = minJumps ( N , arr ) ; for ( auto & it : out ) cout << it << ' ▁ ' ; return 0 ; }
Largest square sub | C ++ program for the above approach ; Define the prefix sum arrays globally ; Diagonal sum ; Check each row ; Check each column ; Check anti - diagonal ; Store the size of the given grid ; Compute the prefix sum for the rows ; Compute the prefix sum for the columns ; Check for all possible square submatrix ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int prefix_sum_row [ 50 ] [ 51 ] ; int prefix_sum_col [ 51 ] [ 50 ] ; bool is_valid ( int r , int c , int size , vector < vector < int > > & grid ) { int r_end = r + size , c_end = c + size ; int sum = 0 ; for ( int i = r , j = c ; i < r_end ; i ++ , j ++ ) { sum += grid [ i ] [ j ] ; } for ( int i = r ; i < r_end ; i ++ ) { if ( prefix_sum_row [ i ] [ c_end ] - prefix_sum_row [ i ] != sum ) { return false ; } } for ( int i = c ; i < c_end ; i ++ ) { if ( prefix_sum_col [ r_end ] [ i ] - prefix_sum_col [ r ] [ i ] != sum ) { return false ; } } int ad_sum = 0 ; for ( int i = r , j = c_end - 1 ; i < r_end ; i ++ , j -- ) { ad_sum += grid [ i ] [ j ] ; } return ad_sum == sum ; } int largestSquareValidMatrix ( vector < vector < int > > & grid ) { int m = grid . size ( ) , n = grid [ 0 ] . size ( ) ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { prefix_sum_row [ i ] [ j ] = prefix_sum_row [ i ] [ j - 1 ] + grid [ i ] [ j - 1 ] ; } } for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { prefix_sum_col [ i ] [ j ] = prefix_sum_col [ i - 1 ] [ j ] + grid [ i - 1 ] [ j ] ; } } for ( int size = min ( m , n ) ; size > 1 ; size -- ) { for ( int i = 0 ; i <= m - size ; i ++ ) { for ( int j = 0 ; j <= n - size ; j ++ ) { if ( is_valid ( i , j , size , grid ) ) { return size ; } } } } return 1 ; } int main ( ) { vector < vector < int > > grid = { { 7 , 1 , 4 , 5 , 6 } , { 2 , 5 , 1 , 6 , 4 } , { 1 , 5 , 4 , 3 , 2 } , { 1 , 2 , 7 , 3 , 4 } } ; cout << largestSquareValidMatrix ( grid ) ; return 0 ; }
Minimize product of first 2 ^ Kâ €“ 1 Natural Numbers by swapping bits for any pair any number of times | C ++ program for the above approach ; Function to find the minimum positive product after swapping bits at the similar position for any two numbers ; Stores the resultant number ; range / 2 numbers will be 1 and range / 2 numbers will be range - 1 ; Multiply with the final number ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumPossibleProduct ( int K ) { int res = 1 ; int range = ( 1 << K ) - 1 ; for ( int i = 0 ; i < K ; i ++ ) { res *= ( range - 1 ) ; } res *= range ; return res ; } int main ( ) { int K = 3 ; cout << minimumPossibleProduct ( K ) ; return 0 ; }