codeparrot/xlcost-text-to-code huggingface dataset:
Print the first N terms of the series 6 , 28 , 66 , 120 , 190 , 276 , ... | C ++ program for the above approach ; Function to print the series ; Initialise the value of k with 2 ; Iterate from 1 to n ; Print each number ; Increment the value of K by 2 for next number ; Driver Code ; Given number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSeries ( int n ) { int k = 2 ; for ( int i = 0 ; i < n ; i ++ ) { cout << ( k * ( 2 * k - 1 ) ) << " ▁ " ; k += 2 ; } cout << endl ; } int main ( ) { int N = 12 ; printSeries ( N ) ; return 0 ; }
Find a string which matches all the patterns in the given array | C ++ implementation to find the string which matches all the patterns ; Function to find a common string which matches all the pattern ; For storing prefix till first most * without conflicts ; For storing suffix till last most * without conflicts ; For storing all middle characters between first and last * ; Loop to iterate over every pattern of the array ; Index of the first " * " ; Index of Last " * " ; Iterate over the first " * " ; Prefix till first most * without conflicts ; Iterate till last most * from last ; Make suffix till last most * without conflicts ; Take all middle characters in between first and last most * ; Driver Code ; Take all the strings ; Method for finding common string
#include <bits/stdc++.h> NEW_LINE using namespace std ; string find ( vector < string > S , int N ) { string pref ; string suff ; string mid ; for ( int i = 0 ; i < N ; i ++ ) { int first = int ( S [ i ] . find_first_of ( ' * ' ) ) ; int last = int ( S [ i ] . find_last_of ( ' * ' ) ) ; for ( int z = 0 ; z < int ( pref . size ( ) ) && z < first ; z ++ ) { if ( pref [ z ] != S [ i ] [ z ] ) { return " * " ; } } for ( int z = int ( pref . size ( ) ) ; z < first ; z ++ ) { pref += S [ i ] [ z ] ; } for ( int z = 0 ; z < int ( suff . size ( ) ) && int ( S [ i ] . size ( ) ) - 1 - z > last ; z ++ ) { if ( suff [ z ] != S [ i ] [ int ( S [ i ] . size ( ) ) - 1 - z ] ) { return " * " ; } } for ( int z = int ( suff . size ( ) ) ; int ( S [ i ] . size ( ) ) - 1 - z > last ; z ++ ) { suff += S [ i ] [ int ( S [ i ] . size ( ) ) - 1 - z ] ; } for ( int z = first ; z <= last ; z ++ ) { if ( S [ i ] [ z ] != ' * ' ) mid += S [ i ] [ z ] ; } } reverse ( suff . begin ( ) , suff . end ( ) ) ; return pref + mid + suff ; } int main ( ) { int N = 3 ; vector < string > s ( N ) ; s [ 0 ] = " pq * du * q " ; s [ 1 ] = " pq * abc * q " ; s [ 2 ] = " p * d * q " ; cout << find ( s , N ) ; return 0 ; }
Mountain Sequence Pattern | C ++ program for the above approach ; Function to print pattern recursively ; Base Case ; Condition to check row limit ; Condition for assigning gaps ; Conditions to print * ; Else print ' ▁ ' ; Recursive call for columns ; Recursive call for rows ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int k1 = 2 ; int k2 = 2 ; int gap = 5 ; int printPattern ( int i , int j , int n ) { if ( j >= n ) { k1 = 2 ; k2 = 2 ; k1 -- ; k2 ++ ; if ( i == 2 ) { k1 = 0 ; k2 = n - 1 ; } return 0 ; } if ( i >= 3 ) { return 1 ; } if ( j > k2 ) { k1 += gap ; k2 += gap ; } if ( j >= k1 && j <= k2 i == 2 ) { cout << " * " ; } else { cout << " ▁ " ; } if ( printPattern ( i , j + 1 , n ) == 1 ) { return 1 ; } cout << endl ; return printPattern ( i + 1 , 0 , n ) ; } int main ( ) { int N = 3 ; printPattern ( 0 , 0 , N * 5 ) ; return 0 ; }
Build a DFA to accept Binary strings that starts or ends with "01" | C ++ program to check if a string either starts or ends with 01 ; Function for transition state A ; State transition to B if the character is 0 ; State transition to D if the character is 1 ; Function for transition state B ; Check if the string has ended ; State transition to C if the character is 1 ; State transition to D if the character is 0 ; Function for transition state C ; Function for transition state D ; State transition to D if the character is 1 ; State transition to E if the character is 0 ; Function for transition state E ; State transition to E if the character is 0 ; State transition to F if the character is 1 ; Function for transition state F ; State transition to D if the character is 1 ; State transition to E if the character is 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void stateA ( string ) ; void stateB ( string ) ; void stateC ( string ) ; void stateD ( string ) ; void stateE ( string ) ; void stateF ( string ) ; void checkstateA ( string n ) { if ( n [ 0 ] == '0' ) stateB ( n . substr ( 1 ) ) ; else stateD ( n . substr ( 1 ) ) ; } void stateB ( string n ) { if ( n . length ( ) == 0 ) cout << " string ▁ not ▁ accepted " ; else { if ( n [ 0 ] == '1' ) stateC ( n . substr ( 1 ) ) ; else stateD ( n . substr ( 1 ) ) ; } } void stateC ( string n ) { cout << " String ▁ accepted " ; } void stateD ( string n ) { if ( n . length ( ) == 0 ) cout << " string ▁ not ▁ accepted " ; else { if ( n [ 0 ] == '1' ) stateD ( n . substr ( 1 ) ) ; else stateE ( n . substr ( 1 ) ) ; } } void stateE ( string n ) { if ( n . length ( ) == 0 ) cout << " string ▁ not ▁ accepted " ; else { if ( n [ 0 ] == '0' ) stateE ( n . substr ( 1 ) ) ; else stateF ( n . substr ( 1 ) ) ; } } void stateF ( string n ) { if ( n . length ( ) == 0 ) cout << " string ▁ accepred " ; else { if ( n [ 0 ] == '1' ) stateD ( n . substr ( 1 ) ) ; else stateE ( n . substr ( 1 ) ) ; } } int main ( ) { string n = "0100101" ; checkstateA ( n ) ; return 0 ; }
Find the Nth Hogben Numbers | C ++ program to print N - th Hogben Number ; Function returns N - th Hogben Number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int HogbenNumber ( int a ) { int p = ( pow ( a , 2 ) - a + 1 ) ; return p ; } int main ( ) { int N = 10 ; cout << HogbenNumber ( N ) ; return 0 ; }
Check if left and right shift of any string results into given string | C ++ program to check if left and right shift of any string results into the given string ; Function to check string exist or not as per above approach ; Check if any character at position i and i + 2 are not equal then string doesnot exist ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void check_string_exist ( string S ) { int size = S . length ( ) ; bool check = true ; for ( int i = 0 ; i < size ; i ++ ) { if ( S [ i ] != S [ ( i + 2 ) % size ] ) { check = false ; break ; } } if ( check ) cout << " Yes " << endl ; else cout << " No " << endl ; } int main ( ) { string S = " papa " ; check_string_exist ( S ) ; return 0 ; }
Cumulative product of digits of all numbers in the given range | C ++ program to print the product of all numbers in range L and R ; Function to get product of digits ; Function to find the product of digits of all natural numbers in range L to R ; Iterate between L to R ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getProduct ( int n ) { int product = 1 ; while ( n != 0 ) { product = product * ( n % 10 ) ; n = n / 10 ; } return product ; } int productinRange ( int l , int r ) { if ( r - l > 9 ) return 0 ; else { int p = 1 ; for ( int i = l ; i <= r ; i ++ ) p *= getProduct ( i ) ; return p ; } } int main ( ) { int l = 11 , r = 15 ; cout << productinRange ( l , r ) << endl ; l = 1 , r = 15 ; cout << productinRange ( l , r ) ; return 0 ; }
Check whether the string can be printed using same row of qwerty keypad | C ++ Program to check whether the string can be printed using same row of qwerty keypad ; Function to find the row of the character in the qwerty keypad ; Sets to include the characters from the same row of the qwerty keypad ; Condition to check the row of the current character of the string ; Function to check the characters are from the same row of the qwerty keypad ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int checkQwertyRow ( char x ) { set < char > first_row = { '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '0' , ' - ' , ' = ' } ; set < char > second_row = { ' Q ' , ' W ' , ' E ' , ' R ' , ' T ' , ' Y ' , ' U ' , ' I ' , ' O ' , ' P ' , ' [ ' , ' ] ' , ' q ' , ' w ' , ' e ' , ' r ' , ' t ' , ' y ' , ' u ' , ' i ' , ' o ' , ' p ' } ; set < char > third_row = { ' A ' , ' S ' , ' D ' , ' F ' , ' G ' , ' H ' , ' J ' , ' K ' , ' L ' , ' ; ' , ' : ' , ' a ' , ' s ' , ' d ' , ' f ' , ' g ' , ' h ' , ' j ' , ' k ' , ' l ' } ; set < char > fourth_row = { ' Z ' , ' X ' , ' C ' , ' V ' , ' B ' , ' N ' , ' M ' , ' , ' , ' . ' , ' / ' , ' z ' , ' x ' , ' c ' , ' v ' , ' b ' , ' n ' , ' m ' } ; if ( first_row . count ( x ) > 0 ) { return 1 ; } else if ( second_row . count ( x ) > 0 ) { return 2 ; } else if ( third_row . count ( x ) > 0 ) { return 3 ; } else if ( fourth_row . count ( x ) > 0 ) { return 4 ; } return 0 ; } bool checkValidity ( string str ) { char x = str [ 0 ] ; int row = checkQwertyRow ( x ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { x = str [ i ] ; if ( row != checkQwertyRow ( x ) ) { return false ; } } return true ; } int main ( ) { string str = " GeeksforGeeks " ; if ( checkValidity ( str ) ) cout << " Yes " ; else cout << " No " ; return ( 0 ) ; }
Last digit of sum of numbers in the given range in the Fibonacci series | C ++ program to calculate last digit of the sum of the fibonacci numbers from M to N ; Calculate the sum of the first N Fibonacci numbers using Pisano period ; The first two Fibonacci numbers ; Base case ; Pisano period for % 10 is 60 ; Checking the remainder ; The loop will range from 2 to two terms after the remainder ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long fib ( long long n ) { long long f0 = 0 ; long long f1 = 1 ; if ( n == 0 ) return 0 ; if ( n == 1 ) return 1 ; else { long long rem = n % 60 ; if ( rem == 0 ) return 0 ; for ( long long i = 2 ; i < rem + 3 ; i ++ ) { long long f = ( f0 + f1 ) % 60 ; f0 = f1 ; f1 = f ; } long long s = f1 - 1 ; return s ; } } int main ( ) { long long m = 10087887 ; long long n = 2983097899 ; long long final = abs ( fib ( n ) - fib ( m - 1 ) ) ; cout << final % 10 << endl ; }
Find N values of X1 , X2 , ... Xn such that X1 < X2 < ... < XN and sin ( X1 ) < sin ( X2 ) < ... < sin ( XN ) | C ++ program for the above approach ; Function to print all such Xi s . t . all Xi and sin ( Xi ) are strictly increasing ; Till N becomes zero ; Find the value of sin ( ) using inbuilt function ; increment by 710 ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSinX ( int N ) { int Xi = 0 ; int num = 1 ; while ( N -- ) { cout << " X " << num << " ▁ = ▁ " << Xi ; cout << " ▁ sin ( X " << num << " ) ▁ = ▁ " << fixed ; cout << setprecision ( 6 ) << sin ( Xi ) << endl ; num += 1 ; Xi += 710 ; } } int main ( ) { int N = 5 ; printSinX ( N ) ; return 0 ; }
Sum of all elements in an array between zeros | C ++ program for the above approach ; Function to find the sum between two zeros in the given array arr [ ] ; To store the sum of element between two zeros ; To store the sum ; Find first index of 0 ; Traverse the given array arr [ ] ; If 0 occurs then add it to A [ ] ; Else add element to the sum ; Print all the sum stored in A ; If there is no such element print - 1 ; Driver Code ; Function call
#include " bits / stdc + + . h " NEW_LINE using namespace std ; void sumBetweenZero ( int arr [ ] , int N ) { int i = 0 ; vector < int > A ; int sum = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { i ++ ; break ; } } for ( ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { A . push_back ( sum ) ; sum = 0 ; } else { sum += arr [ i ] ; } } for ( int i = 0 ; i < A . size ( ) ; i ++ ) { cout << A [ i ] << ' ▁ ' ; } if ( A . size ( ) == 0 ) cout << " - 1" ; } int main ( ) { int arr [ ] = { 1 , 0 , 3 , 4 , 0 , 4 , 4 , 0 , 2 , 1 , 4 , 0 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sumBetweenZero ( arr , N ) ; return 0 ; }
Find the Nth term of the series 2 , 15 , 41 , 80 , 132. . . | C ++ program for the above approach ; Recursive function to find Nth term ; Base Case ; Recursive Call according to Nth term of the series ; Driver Code ; Input Nth term ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nthTerm ( int N ) { if ( N == 1 ) { return 2 ; } return ( ( N - 1 ) * 13 ) + nthTerm ( N - 1 ) ; } int main ( ) { int N = 17 ; cout << nthTerm ( N ) << endl ; return 0 ; }
Subsequence pair from given Array having all unique and all same elements respectively | C ++ program for the above approach ; Function to find the maximum length of subsequences with given property ; To store the frequency ; Traverse the array to store the frequency ; M . size ( ) given count of distinct element in arr [ ] ; Traverse map to find max frequency ; Find the maximum length on the basis of two cases in the approach ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumSubsequence ( int arr [ ] , int N ) { unordered_map < int , int > M ; for ( int i = 0 ; i < N ; i ++ ) { M [ arr [ i ] ] ++ ; } int distinct_size = M . size ( ) ; int maxFreq = 1 ; for ( auto & it : M ) { maxFreq = max ( maxFreq , it . second ) ; } cout << max ( min ( distinct_size , maxFreq - 1 ) , min ( distinct_size - 1 , maxFreq ) ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 4 , 4 , 4 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maximumSubsequence ( arr , N ) ; return 0 ; }
Total length of string from given Array of strings composed using given characters | C ++ implementation to find total length of string composed of given characters formed from given Array of strings ; Function to count the total length ; Unordered_map for keeping frequency of characters ; Calculate the frequency ; Iterate in the N strings ; Iterates in the string ; Checks if given character of string string appears in it or not ; Adds the length of string if all characters are present ; Return the final result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countCharacters ( vector < string > & strings , string chars ) { int res = 0 ; unordered_map < char , int > freq ; for ( int i = 0 ; i < chars . length ( ) ; i ++ ) freq [ chars [ i ] ] += 1 ; for ( auto st : strings ) { bool flag = true ; for ( auto c : st ) { if ( ! freq ) { flag = false ; break ; } } if ( flag ) res += st . length ( ) ; } return res ; } int main ( ) { vector < string > strings = { " hi " , " data " , " geeksforgeeks " } ; string chars = " tiadhae " ; cout << countCharacters ( strings , chars ) ; return 0 ; }
Construct an Array of size N in which sum of odd elements is equal to sum of even elements | C ++ program to Create an array of size N consisting of distinct elements where sum of odd elements is equal to sum of even elements ; Function to construct the required array ; To construct first half , distinct even numbers ; To construct second half , distinct odd numbers ; Calculate the last number of second half so as to make both the halves equal ; Function to construct the required array ; check if size is multiple of 4 then array exist ; function call to construct array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void arrayConstruct ( int N ) { for ( int i = 2 ; i <= N ; i = i + 2 ) cout << i << " ▁ " ; for ( int i = 1 ; i < N - 1 ; i = i + 2 ) cout << i << " ▁ " ; cout << N - 1 + ( N / 2 ) << endl ; } void createArray ( int N ) { if ( N % 4 == 0 ) arrayConstruct ( N ) ; else cout << -1 << endl ; } int main ( ) { int N = 8 ; createArray ( N ) ; return 0 ; }
Count of all sub | C ++ implementation to Count all sub - strings with sum of weights at most K ; Function to count all substrings ; Hashmap to store substrings ; iterate over all substrings ; variable to maintain sum of all characters encountered ; variable to maintain substring till current position ; get position of character in string W ; add weight to current sum ; add current character to substring ; check if sum of characters is <= K insert in Hashmap ; Driver code ; initialise string ; initialise weight
#include <bits/stdc++.h> NEW_LINE using namespace std ; int distinctSubstring ( string & P , string & Q , int K , int N ) { unordered_set < string > S ; for ( int i = 0 ; i < N ; ++ i ) { int sum = 0 ; string s ; for ( int j = i ; j < N ; ++ j ) { int pos = P [ j ] - ' a ' ; sum += Q [ pos ] - '0' ; s += P [ j ] ; if ( sum <= K ) { S . insert ( s ) ; } else { break ; } } } return S . size ( ) ; } int main ( ) { string S = " abcde " ; string W = "12345678912345678912345678" ; int K = 5 ; int N = S . length ( ) ; cout << distinctSubstring ( S , W , K , N ) ; return 0 ; }
Count all sub | C ++ program to find the count of all the sub - strings with weight of characters atmost K ; Function to find the count of all the substrings with weight of characters atmost K ; Hashmap to store all substrings ; Iterate over all substrings ; Maintain the sum of all characters encountered so far ; Maintain the substring till the current position ; Get the position of the character in string Q ; Add weight to current sum ; Add current character to substring ; If sum of characters is <= K then insert in into the set ; Finding the size of the set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int distinctSubstring ( string & P , string & Q , int K , int N ) { unordered_set < string > S ; for ( int i = 0 ; i < N ; ++ i ) { int sum = 0 ; string s ; for ( int j = i ; j < N ; ++ j ) { int pos = P [ j ] - ' a ' ; sum += Q [ pos ] - '0' ; s += P [ j ] ; if ( sum <= K ) { S . insert ( s ) ; } else { break ; } } } return S . size ( ) ; } int main ( ) { string P = " abcde " ; string Q = "12345678912345678912345678" ; int K = 5 ; int N = P . length ( ) ; cout << distinctSubstring ( P , Q , K , N ) ; return 0 ; }
Program to print a Hollow Triangle inside a Triangle | C ++ implementation of the above approach ; Function to print the pattern ; Loop for rows ; Loop for column ; For printing equal sides of outer triangle ; For printing equal sides of inner triangle ; For printing base of both triangle ; For spacing between the triangle ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPattern ( int n ) { int i , j ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j < 2 * n ; j ++ ) { if ( j == ( n - i + 1 ) || j == ( n + i - 1 ) ) { cout << " * ▁ " ; } else if ( ( i >= 4 && i <= n - 4 ) && ( j == n - i + 4 j == n + i - 4 ) ) { cout << " * ▁ " ; } else if ( i == n || ( i == n - 4 && j >= n - ( n - 2 * 4 ) && j <= n + n - 2 * 4 ) ) { cout << " * ▁ " ; } else { cout << " ▁ " << " ▁ " ; } } cout << " STRNEWLINE " ; } } int main ( ) { int N = 9 ; printPattern ( N ) ; }
Check if a string is made up of K alternating characters | C ++ implementation of the approach ; Function to check if a string is made up of k alternating characters ; Check if all the characters at indices 0 to K - 1 are different ; If that bit is already set in checker , return false ; Otherwise update and continue by setting that bit in the checker ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isKAlternating ( string s , int k ) { if ( s . length ( ) < k ) return false ; int checker = 0 ; for ( int i = 0 ; i < k ; i ++ ) { int bitAtIndex = s [ i ] - ' a ' ; if ( ( checker & ( 1 << bitAtIndex ) ) > 0 ) { return false ; } checker = checker | ( 1 << bitAtIndex ) ; } for ( int i = k ; i < s . length ( ) ; i ++ ) if ( s [ i - k ] != s [ i ] ) return false ; return true ; } int main ( ) { string str = " acdeac " ; int K = 4 ; if ( isKAlternating ( str , K ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Find all Factors of Large Perfect Square Natural Number in O ( sqrt ( sqrt ( N ) ) | C ++ program to find the factors of large perfect square number in O ( sqrt ( sqrt ( N ) ) ) time ; Function that find all the prime factors of N ; Store the sqrt ( N ) in temp ; Initialise factor array with 1 as a factor in it ; Check divisibility by 2 ; Store the factors twice ; Check for other prime factors other than 2 ; If j is a prime factor ; Store the prime factor twice ; If j is prime number left other than 2 ; Store j twice ; Initialise Matrix M to to store all the factors ; tpc for rows tpr for column ; Initialise M [ 0 ] [ 0 ] = 1 as it also factor of N ; Traversing factor array ; If current and previous factors are not same then move to next row and insert the current factor ; If current and previous factors are same then , Insert the factor with previous factor inserted in matrix M ; The arr1 [ ] and arr2 [ ] used to store all the factors of N ; Initialise arrays as 1 ; Traversing the matrix M ; Traversing till column element doesn 't become 0 ; Store the product of every element of current row with every element in arr1 [ ] ; Copying every element of arr2 [ ] in arr1 [ ] ; length of arr2 [ ] and arr1 [ ] are equal after copying ; Print all the factors ; Drivers Code
#include " bits / stdc + + . h " NEW_LINE using namespace std ; int MAX = 100000 ; void findFactors ( int N ) { int temp = sqrt ( N ) ; int factor [ MAX ] = { 1 } ; int i , j , k ; int len1 = 1 ; while ( temp % 2 == 0 ) { factor [ len1 ++ ] = 2 ; factor [ len1 ++ ] = 2 ; temp /= 2 ; } for ( j = 3 ; j < sqrt ( temp ) ; j += 2 ) { while ( temp % j == 0 ) { factor [ len1 ++ ] = j ; factor [ len1 ++ ] = j ; temp /= j ; } } if ( temp > 2 ) { factor [ len1 ++ ] = temp ; factor [ len1 ++ ] = temp ; } int M [ len1 ] [ MAX ] = { 0 } ; int tpc = 0 , tpr = 0 ; M [ 0 ] [ 0 ] = 1 ; j = 1 ; while ( j < len1 ) { if ( factor [ j ] != factor [ j - 1 ] ) { tpr ++ ; M [ tpr ] [ 0 ] = factor [ j ] ; j ++ ; tpc = 1 ; } else { M [ tpr ] [ tpc ] = M [ tpr ] [ tpc - 1 ] * factor [ j ] ; j ++ ; tpc ++ ; } } int arr1 [ MAX ] , arr2 [ MAX ] ; int l1 , l2 ; l1 = l2 = 1 ; arr1 [ 0 ] = arr2 [ 0 ] = 1 ; for ( i = 1 ; i < tpr + 1 ; i ++ ) { for ( j = 0 ; M [ i ] [ j ] != 0 ; j ++ ) { for ( k = 0 ; k < l1 ; k ++ ) { arr2 [ l2 ++ ] = arr1 [ k ] * M [ i ] [ j ] ; } } for ( j = l1 ; j < l2 ; j ++ ) { arr1 [ j ] = arr2 [ j ] ; } l1 = l2 ; } for ( i = 0 ; i < l2 ; i ++ ) { cout << arr2 [ i ] << ' ▁ ' ; } } int main ( ) { int N = 900 ; findFactors ( N ) ; return 0 ; }
Construct a matrix such that union of ith row and ith column contains every element from 1 to 2 N | C ++ implementation of the above approach ; Function to find the square matrix ; For Matrix of order 1 , it will contain only 1 ; For Matrix of odd order , it is not possible ; For Matrix of even order ; All diagonal elements of the matrix can be N itself . ; Assign values at desired place in the matrix ; Loop to add N in the lower half of the matrix such that it contains elements from 1 to 2 * N - 1 ; Loop to print the matrix ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int matrix [ 100 ] [ 100 ] ; void printRequiredMatrix ( int n ) { if ( n == 1 ) { cout << "1" << " STRNEWLINE " ; } else if ( n % 2 != 0 ) { cout << " - 1" << " STRNEWLINE " ; } else { for ( int i = 0 ; i < n ; i ++ ) { matrix [ i ] [ i ] = n ; } int u = n - 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { matrix [ i ] [ u ] = i + 1 ; for ( int j = 1 ; j < n / 2 ; j ++ ) { int a = ( i + j ) % ( n - 1 ) ; int b = ( i - j + n - 1 ) % ( n - 1 ) ; if ( a < b ) swap ( a , b ) ; matrix [ b ] [ a ] = i + 1 ; } } for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) matrix [ i ] [ j ] = matrix [ j ] [ i ] + n ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) cout << matrix [ i ] [ j ] << " ▁ " ; cout << " STRNEWLINE " ; } } cout << " STRNEWLINE " ; } int main ( ) { int n = 1 ; printRequiredMatrix ( n ) ; n = 3 ; printRequiredMatrix ( n ) ; n = 6 ; printRequiredMatrix ( n ) ; return 0 ; }