codeparrot/xlcost-text-to-code huggingface dataset:
Append X digits to the end of N to make it divisible by M | C ++ program to implement the above approach ; Function to check if the value of N by appending X digits on right side of N is divisible by M or not ; Base Case ; If N is divisible by M ; Update res ; Iterate over the range [ 0 , 9 ] ; If N is divisible by M by appending X digits ; Driver Code ; Stores the number by appending X digits on the right side of N
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDiv ( int N , int X , int M , int & res ) { if ( X == 0 ) { if ( N % M == 0 ) { res = N ; return true ; } return false ; } for ( int i = 0 ; i <= 9 ; i ++ ) { if ( isDiv ( N * 10 + i , X - 1 , M , res ) ) { return true ; } } } int main ( ) { int N = 4 , M = 50 , X = 2 ; int res = -1 ; isDiv ( N , X , M , res ) ; cout << res ; }
Minimum increments to modify array such that value of any array element can be splitted to make all remaining elements equal | C ++ program for the above approach ; Function to count minimum moves ; Stores sum of given array ; Stores maximum array element ; Base Case ; If N is 2 , the answer will always be 0 ; Traverse the array ; Calculate sum of the array ; Finding maximum element ; Calculate ceil ( sum / N - 1 ) ; If k is smaller than maxelement ; Final sum - original sum ; Print the minimum number of increments required ; Driver Code ; Given array ; Size of given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumMoves ( int arr [ ] , int N ) { int sum = 0 ; int maxelement = -1 ; if ( N == 2 ) { cout << 0 ; } for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; maxelement = max ( maxelement , arr [ i ] ) ; } int K = ( sum + N - 2 ) / ( N - 1 ) ; K = max ( maxelement , K ) ; int ans = K * ( N - 1 ) - sum ; cout << ans ; } int main ( ) { int arr [ ] = { 2 , 3 , 7 } ; int N = 3 ; minimumMoves ( arr , N ) ; return 0 ; }
Split first N natural numbers into two subsequences with non | C ++ program for the above approach ; Function to split 1 to N into two subsequences with non - coprime sums ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void printSubsequence ( int N ) { cout << " { ▁ " ; for ( int i = 1 ; i < N - 1 ; i ++ ) { cout << i << " , ▁ " ; } cout << N - 1 << " ▁ } STRNEWLINE " ; cout << " { ▁ " << N << " ▁ } " ; } int main ( ) { int N = 8 ; printSubsequence ( N ) ; return 0 ; }
Generate an array with product of all subarrays of length exceeding one divisible by K | C ++ program for the above approach ; Function to check if the required array can be generated or not ; To check if divisor exists ; To store divisiors of K ; Check if K is prime or not ; If array can be generated ; Print d1 and d2 alternatively ; No such array can be generated ; Driver Code ; Given N and K ; Function Call
#include <iostream> NEW_LINE using namespace std ; void array_divisbleby_k ( int N , int K ) { bool flag = false ; int d1 , d2 ; for ( int i = 2 ; i * i <= K ; i ++ ) { if ( K % i == 0 ) { flag = true ; d1 = i ; d2 = K / i ; break ; } } if ( flag ) { for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 1 ) { cout << d2 << " ▁ " ; } else { cout << d1 << " ▁ " ; } } } else { cout << -1 ; } } int main ( ) { int N = 5 , K = 21 ; array_divisbleby_k ( N , K ) ; return 0 ; }
Minimum characters to be replaced to make a string concatenation of a K | C ++ program for the above approach ; Function to add an edge to graph ; Function to perform DFS traversal on the graph recursively from a given vertex u ; Visit the current vertex ; Total number of nodes in this component ; Increment the frequency of u ; Function for finding the minimum number changes required in given string ; Form the edges according to the given conditions ; Find minimum number of operations ; Frequency array for finding the most frequent character ; Frequency array for finding the most frequent character ; Finding most frequent character ; Change rest of the characters to most frequent one ; Print total number of changes ; Driver Code ; Function Call
#include " bits / stdc + + . h " NEW_LINE using namespace std ; void addEdge ( vector < int > adj [ ] , int u , int v ) { adj [ u ] . push_back ( v ) ; adj [ v ] . push_back ( u ) ; } void DFS ( int u , vector < int > adj [ ] , int & cnt , vector < bool > & visited , int fre [ ] , string S ) { visited [ u ] = true ; cnt ++ ; fre [ S [ u ] - ' a ' ] ++ ; for ( int i = 0 ; i < adj [ u ] . size ( ) ; i ++ ) { if ( ! visited [ adj [ u ] [ i ] ] ) { DFS ( adj [ u ] [ i ] , adj , cnt , visited , fre , S ) ; } } } int minimumOperations ( string & S , int m ) { int V = 100 ; vector < int > adj [ V ] ; int total = 0 , N = S . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { addEdge ( adj , i , N - i - 1 ) ; addEdge ( adj , N - i - 1 , i ) ; } for ( int i = 0 ; i < N - m ; i ++ ) { addEdge ( adj , i , i + m ) ; addEdge ( adj , i + m , i ) ; } vector < bool > visited ( V , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! visited [ i ] ) { int fre [ 26 ] = { 0 } ; int cnt = 0 , maxx = -1 ; DFS ( i , adj , cnt , visited , fre , S ) ; for ( int j = 0 ; j < 26 ; j ++ ) maxx = max ( maxx , fre [ j ] ) ; total += cnt - maxx ; } } cout << total ; } int main ( ) { string S = " abaaba " ; int K = 2 ; minimumOperations ( S , K ) ; return 0 ; }
Replace specified matrix elements such that no two adjacent elements are equal | C ++ program for the above approach ; Function to display the valid matrix ; Traverse the matrix ; If the current cell is a free space and is even - indexed ; If the current cell is a free space and is odd - indexed ; Print the matrix ; Driver Code ; Given N and M ; Given matrix ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void print ( vector < vector < char > > arr , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { char a = arr [ i ] [ j ] ; if ( ( i + j ) % 2 == 0 && a == ' F ' ) { arr [ i ] [ j ] = '1' ; } else if ( a == ' F ' ) { arr [ i ] [ j ] = '2' ; } } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { cout << arr [ i ] [ j ] ; } cout << endl ; } } int main ( ) { int n = 4 , m = 4 ; vector < vector < char > > arr = { { ' F ' , ' F ' , ' F ' , ' F ' } , { ' F ' , ' O ' , ' F ' , ' F ' } , { ' F ' , ' F ' , ' O ' , ' F ' } , { ' F ' , ' F ' , ' F ' , ' F ' } , } ; print ( arr , n , m ) ; return 0 ; }
Lexicographically smallest string formed by removing duplicates | C ++ program for the above approach ; Function that finds lexicographically smallest string after removing the duplicates from the given string ; Stores the frequency of characters ; Mark visited characters ; Stores count of each character ; Stores the resultant string ; Decrease the count of current character ; If character is not already in answer ; Last character > S [ i ] and its count > 0 ; Mark letter unvisited ; Add s [ i ] in res and mark it visited ; Return the resultant string ; Driver Code ; Given string S ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string removeDuplicateLetters ( string s ) { int cnt [ 26 ] = { 0 } ; int vis [ 26 ] = { 0 } ; int n = s . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) cnt [ s [ i ] - ' a ' ] ++ ; string res = " " ; for ( int i = 0 ; i < n ; i ++ ) { cnt [ s [ i ] - ' a ' ] -- ; if ( ! vis [ s [ i ] - ' a ' ] ) { while ( res . size ( ) > 0 && res . back ( ) > s [ i ] && cnt [ res . back ( ) - ' a ' ] > 0 ) { vis [ res . back ( ) - ' a ' ] = 0 ; res . pop_back ( ) ; } res += s [ i ] ; vis [ s [ i ] - ' a ' ] = 1 ; } } return res ; } int main ( ) { string S = " acbc " ; cout << removeDuplicateLetters ( S ) ; return 0 ; }
Number of pairs whose product is a power of 2 | C ++ program for the above approach ; Function to count pairs having product equal to a power of 2 ; Stores count of array elements which are power of 2 ; If array element contains only one set bit ; Increase count of powers of 2 ; Count required number of pairs ; Print the required number of pairs ; Driver Code ; Given array ; Size of the array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int arr [ ] , int N ) { int countPowerof2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( __builtin_popcount ( arr [ i ] ) == 1 ) countPowerof2 ++ ; } int desiredPairs = ( countPowerof2 * ( countPowerof2 - 1 ) ) / 2 ; cout << desiredPairs << ' ▁ ' ; } int main ( ) { int arr [ 4 ] = { 2 , 4 , 7 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countPairs ( arr , N ) ; return 0 ; }
Count ways to split N ! into two distinct co | C ++ Program for the above approach ; Maximum value of N ; Stores at each indices if given number is prime or not ; Stores count_of_primes ; Function to generate primes using Sieve of Eratsothenes ; Assume all odds are primes ; If a prime is encountered ; Mark all its multiples as non - prime ; Count primes <= MAXN ; Function to calculate ( x ^ y ) % p in O ( log y ) ; Utility function to count the number of ways N ! can be split into co - prime factors ; Driver Code ; Calling sieve function ; Given N ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAXN  1000000 NEW_LINE int is_prime [ MAXN ] = { 0 } ; int count_of_primes [ MAXN ] = { 0 } ; void sieve ( ) { for ( int i = 3 ; i < MAXN ; i += 2 ) { is_prime [ i ] = 1 ; } for ( int i = 3 ; i * i < MAXN ; i += 2 ) { if ( is_prime [ i ] ) for ( int j = i * i ; j < MAXN ; j += i ) { is_prime [ j ] = 0 ; } } is_prime [ 2 ] = 1 ; for ( int i = 1 ; i < MAXN ; i ++ ) count_of_primes [ i ] = count_of_primes [ i - 1 ] + is_prime [ i ] ; } long long int power ( long long int x , long long int y , long long int p ) { long long result = 1 ; while ( y > 0 ) { if ( y & 1 == 1 ) result = ( result * x ) % p ; x = ( x * x ) % p ; y >>= 1 ; } return result ; } void numberOfWays ( int N ) { long long int count = count_of_primes [ N ] - 1 ; long long int mod = 1000000007 ; long long int answer = power ( 2 , count , mod ) ; if ( N == 1 ) answer = 0 ; cout << answer ; } int main ( ) { sieve ( ) ; int N = 7 ; numberOfWays ( N ) ; return 0 ; }
Maximum possible sum of squares of stack elements satisfying the given properties | C ++ Program to implement the above approach ; Function to find the maximum sum of squares of stack elements ; Stores the sum ofsquares of stack elements ; Check if sum is valid ; Initialize all stack elements with 1 ; Stores the count the number of stack elements not equal to 1 ; Add the sum of squares of stack elements not equal to 1 ; Add 1 * 1 to res as the remaining stack elements are 1 ; Print the result ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxSumOfSquares ( int N , int S ) { int res = 0 ; if ( S < N S > 9 * N ) { cout << ( -1 ) ; return ; } S = S - N ; int c = 0 ; while ( S > 0 ) { c ++ ; if ( S / 8 > 0 ) { res += 9 * 9 ; S -= 8 ; } else { res += ( S + 1 ) * ( S + 1 ) ; break ; } } res = res + ( N - c ) ; cout << ( res ) ; } int main ( ) { int N = 3 ; int S = 12 ; maxSumOfSquares ( N , S ) ; }
Unset least significant K bits of a given number | C ++ program for the above approach ; Function to return the value after unsetting K LSBs ; Create a mask ; Bitwise AND operation with the number and the mask ; Driver Code ; Given N and K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int clearLastBit ( int N , int K ) { int mask = ( -1 << K + 1 ) ; return N = N & mask ; } int main ( ) { int N = 730 , K = 3 ; cout << clearLastBit ( N , K ) ; return 0 ; }
Check if quantities of 3 distinct colors can be converted to a single color by given merge | C ++ program for the above approach ; Function to check whether it is possible to do the operation or not ; Calculate modulo 3 of all the colors ; Check for any equal pair ; Otherwise ; Driver Code ; Given colors ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int r , int b , int g ) { r = r % 3 ; b = b % 3 ; g = g % 3 ; if ( r == b b == g g == r ) { return true ; } else { return false ; } } int main ( ) { int R = 1 , B = 3 , G = 6 ; if ( isPossible ( R , B , G ) ) { cout << " Yes " << endl ; } else { cout << " No " << endl ; } return 0 ; }
Minimize steps required to make all array elements same by adding 1 , 2 or 5 | C ++ program for the above approach ; Function to calculate the minimum number of steps ; count stores number of operations required to make all elements equal to minimum value ; Remark , the array should remain unchanged for further calculations with different minimum ; Storing the current value of arr [ i ] in val ; Finds how much extra amount is to be removed ; Subtract the maximum number of 5 and stores remaining ; Subtract the maximum number of 2 and stores remaining ; Restores the actual value of arr [ i ] ; Return the count ; Function to find the minimum number of steps to make array elements same ; Sort the array in descending order ; Stores the minimum array element ; Stores the operations required to make array elements equal to minimum ; Stores the operations required to make array elements equal to minimum - 1 ; Stores the operations required to make array elements equal to minimum - 2 ; Return minimum of the three counts ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculate_steps ( int arr [ ] , int n , int minimum ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int val = arr [ i ] ; if ( arr [ i ] > minimum ) { arr [ i ] = arr [ i ] - minimum ; count += arr [ i ] / 5 ; arr [ i ] = arr [ i ] % 5 ; count += arr [ i ] / 2 ; arr [ i ] = arr [ i ] % 2 ; if ( arr [ i ] ) { count ++ ; } } arr [ i ] = val ; } return count ; } int solve ( int arr [ ] , int n ) { sort ( arr , arr + n , greater < int > ( ) ) ; int minimum = arr [ n - 1 ] ; int count1 = 0 , count2 = 0 , count3 = 0 ; count1 = calculate_steps ( arr , n , minimum ) ; count2 = calculate_steps ( arr , n , minimum - 1 ) ; count3 = calculate_steps ( arr , n , minimum - 2 ) ; return min ( count1 , min ( count2 , count3 ) ) ; } int main ( ) { int arr [ ] = { 3 , 6 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << solve ( arr , N ) ; }
Largest palindromic string possible from given strings by rearranging the characters | C ++ program for the above approach ; Function to find largest palindrome possible from S and P after rearranging characters to make palindromic string T ; Using unordered_map to store frequency of elements mapT will have freq of elements of T ; Size of both the strings ; Take all character in mapT which occur even number of times in respective strings & simultaneously update number of characters left in map ; Check if a unique character is present in both string S and P ; Making string T in two halves half1 - first half half2 - second half ; Reverse the half2 to attach with half1 to make palindrome T ; If same unique element is present in both S and P , then taking that only which is already taken through mapT ; If same unique element is not present in S and P , then take characters that make string T lexicographically smallest ; If no unique element is present in both string S and P ; Driver Code ; Given two strings S and P ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string mergePalindromes ( string S , string P ) { unordered_map < char , int > mapS , mapP , mapT ; int n = S . size ( ) , m = P . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mapS [ S [ i ] ] ++ ; } for ( int i = 0 ; i < m ; i ++ ) { mapP [ P [ i ] ] ++ ; } for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( mapS [ i ] % 2 == 0 ) { mapT [ i ] += mapS [ i ] ; mapS [ i ] = 0 ; } else { mapT [ i ] += mapS [ i ] - 1 ; mapS [ i ] = 1 ; } if ( mapP [ i ] % 2 == 0 ) { mapT [ i ] += mapP [ i ] ; mapP [ i ] = 0 ; } else { mapT [ i ] += mapP [ i ] - 1 ; mapP [ i ] = 1 ; } } int check = 0 ; for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( mapS [ i ] > 0 && mapP [ i ] > 0 ) { mapT [ i ] += 2 ; check = 1 ; break ; } } string half1 = " " , half2 = " " ; for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { for ( int j = 0 ; ( 2 * j ) < mapT [ i ] ; j ++ ) { half1 += i ; half2 += i ; } } reverse ( half2 . begin ( ) , half2 . end ( ) ) ; if ( check ) { return half1 + half2 ; } for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( mapS [ i ] > 0 mapP [ i ] > 0 ) { half1 += i ; return half1 + half2 ; } } return half1 + half2 ; } int main ( ) { string S = " aeabb " ; string P = " dfedf " ; cout << mergePalindromes ( S , P ) ; return 0 ; }
Count of subarrays having sum equal to its length | C ++ program for the above approach ; Function that counts the subarrays with sum of its elements as its length ; Decrementing all the elements of the array by 1 ; Making prefix sum array ; Declare map to store count of elements upto current element ; To count all the subarrays whose prefix sum is 0 ; Iterate the array ; Increment answer by count of current element of prefix array ; Return the answer ; Driver Code ; Given array arr [ ] ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOfSubarray ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) arr [ i ] -- ; int pref [ N ] ; pref [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) pref [ i ] = pref [ i - 1 ] + arr [ i ] ; map < int , int > mp ; int answer = 0 ; mp [ 0 ] ++ ; for ( int i = 0 ; i < N ; i ++ ) { answer += mp [ pref [ i ] ] ; mp [ pref [ i ] ] ++ ; } return answer ; } int main ( ) { int arr [ ] = { 1 , 1 , 0 } ; int N = sizeof arr / sizeof arr [ 0 ] ; cout << countOfSubarray ( arr , N ) ; return 0 ; }
Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum | C ++ program to print subset at the nth position ordered by the sum of the elements ; Function to print the elements of the subset at pos n ; Initialize count = 0 and x = 0 ; create a vector for storing the elements of subsets ; doing until all the set bits of n are used ; this part is executed only when the last bit is set ; right shift the bit by one position ; increasing the count each time by one ; printing the values os elements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printsubset ( int n , int k ) { int count = 0 , x = 0 ; vector < int > vec ; while ( n ) { x = n & 1 ; if ( x ) { vec . push_back ( pow ( k , count ) ) ; } n = n >> 1 ; count ++ ; } for ( int i = 0 ; i < vec . size ( ) ; i ++ ) cout << vec [ i ] << " ▁ " ; } int main ( ) { int n = 7 , k = 4 ; printsubset ( n , k ) ; return 0 ; }
Minimum digits to be removed to make either all digits or alternating digits same | C ++ program for the above approach ; Function to find longest possible subsequence of s beginning with x and y ; Iterate over the string ; Increment count ; Swap the positions ; Return the result ; Function that finds all the possible pairs ; Update count ; Return the answer ; Driver Code ; Given string s ; Find the size of the string ; Function Call ; This value is the count of minimum element to be removed
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( string s , int x , int y ) { int res = 0 ; for ( auto c : s ) { if ( c - '0' == x ) { res ++ ; swap ( x , y ) ; } } if ( x != y && res % 2 == 1 ) -- res ; return res ; } int find_min ( string s ) { int count = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { count = max ( count , solve ( s , i , j ) ) ; } } return count ; } int main ( ) { string s = "100120013" ; int n = s . size ( ) ; int answer = find_min ( s ) ; cout << ( n - answer ) ; return 0 ; }
Count total set bits in all numbers from range L to R | C ++ program for the above approach ; Function to count set bits in x ; Base Case ; Recursive Call ; Function that returns count of set bits present in all numbers from 1 to N ; Initialize the result ; Return the setbit count ; Driver Code ; Given L and R ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned int countSetBitsUtil ( unsigned int x ) { if ( x <= 0 ) return 0 ; return ( ( x % 2 == 0 ? 0 : 1 ) + countSetBitsUtil ( x / 2 ) ) ; } unsigned int countSetBits ( unsigned int L , unsigned int R ) { int bitCount = 0 ; for ( int i = L ; i <= R ; i ++ ) { bitCount += countSetBitsUtil ( i ) ; } return bitCount ; } int main ( ) { int L = 3 , R = 5 ; printf ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ % d " , countSetBits ( L , R ) ) ; return 0 ; }
Possible values of Q such that , for any value of R , their product is equal to X times their sum | C ++ program for the above approach ; Function to find all possible values of Q ; Vector initialization to store all numbers satisfying the given condition ; Iterate for all the values of X ; Check if condition satisfied then push the number ; Possible value of Q ; Print all the numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void values_of_Q ( int X ) { vector < int > val_Q ; for ( int i = 1 ; i <= X ; i ++ ) { if ( ( ( ( X + i ) * X ) ) % i == 0 ) { val_Q . push_back ( X + i ) ; } } cout << val_Q . size ( ) << endl ; for ( int i = 0 ; i < val_Q . size ( ) ; i ++ ) { cout << val_Q [ i ] << " ▁ " ; } } int main ( ) { int X = 3 ; values_of_Q ( X ) ; return 0 ; }
Program to find Greatest Common Divisor ( GCD ) of N strings | C ++ program for the above approach ; Function that finds gcd of 2 strings ; If str1 length is less than that of str2 then recur with gcd ( str2 , str1 ) ; If str1 is not the concatenation of str2 ; GCD string is found ; Cut off the common prefix part of str1 & then recur ; Function to find GCD of array of strings ; Return the GCD of strings ; Driver Code ; Given array of strings ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string gcd ( string str1 , string str2 ) { if ( str1 . length ( ) < str2 . length ( ) ) { return gcd ( str2 , str1 ) ; } else if ( str1 . find ( str2 ) != 0 ) { return " " ; } else if ( str2 == " " ) { return str1 ; } else { return gcd ( str1 . substr ( str2 . length ( ) ) , str2 ) ; } } string findGCD ( string arr [ ] , int n ) { string result = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { result = gcd ( result , arr [ i ] ) ; } return result ; } int main ( ) { string arr [ ] = { " GFGGFG " , " GFGGFG " , " GFGGFGGFGGFG " } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findGCD ( arr , n ) ; }
Check whether the given Matrix is balanced or not | C ++ program for the above approach ; Define size of matrix ; Function to check given matrix balanced or unbalanced ; Flag for check matrix is balanced or unbalanced ; Iterate row until condition is true ; Iterate cols until condition is true ; Check for corner edge elements ; Check for border elements ; Check for the middle ones ; Return balanced or not ; Driver Code ; Given Matrix mat [ ] [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  4 NEW_LINE #define M  4 NEW_LINE string balancedMatrix ( int mat [ ] [ M ] ) { bool is_balanced = true ; for ( int i = 0 ; i < N && is_balanced ; i ++ ) { for ( int j = 0 ; j < M && is_balanced ; j ++ ) { if ( ( i == 0 i == N - 1 ) && ( j == 0 j == M - 1 ) ) { if ( mat [ i ] [ j ] >= 2 ) is_balanced = false ; } else if ( i == 0 i == N - 1 j == 0 j == M - 1 ) { if ( mat [ i ] [ j ] >= 3 ) is_balanced = false ; } else { if ( mat [ i ] [ j ] >= 4 ) is_balanced = false ; } } } if ( is_balanced ) return " Balanced " ; else return " Unbalanced " ; } int main ( ) { int mat [ N ] [ M ] = { { 1 , 2 , 3 , 4 } , { 3 , 5 , 2 , 6 } , { 5 , 3 , 6 , 1 } , { 9 , 5 , 6 , 0 } } ; cout << balancedMatrix ( mat ) ; return 0 ; }