codeparrot/xlcost-text-to-code huggingface dataset:
Rat in a Maze with multiple steps or jump allowed | C / C ++ program to solve Rat in a Maze problem using backtracking ; Maze size ; A utility function to print solution matrix sol [ N ] [ N ] ; A utility function to check if x , y is valid index for N * N maze ; if ( x , y outside maze ) return false ; This function solves the Maze problem using Backtracking . It mainly uses solveMazeUtil ( ) to solve the problem . It returns false if no path is possible , otherwise return true and prints the path in the form of 1 s . Please note that there may be more than one solutions , this function prints one of the feasible solutions . ; A recursive utility function to solve Maze problem ; if ( x , y is goal ) return true ; Check if maze [ x ] [ y ] is valid ; mark x , y as part of solution path ; Move forward in x direction ; Move forward in x direction ; If moving in x direction doesn 't give  solution then Move down in y direction  ; If none of the above movements work then BACKTRACK : unmark x , y as part of solution path ; driver program to test above function
#include <stdio.h> NEW_LINE #define N  4 NEW_LINE bool solveMazeUtil ( int maze [ N ] [ N ] , int x , int y , int sol [ N ] [ N ] ) ; void printSolution ( int sol [ N ] [ N ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) printf ( " ▁ % d ▁ " , sol [ i ] [ j ] ) ; printf ( " STRNEWLINE " ) ; } } bool isSafe ( int maze [ N ] [ N ] , int x , int y ) { if ( x >= 0 && x < N && y >= 0 && y < N && maze [ x ] [ y ] != 0 ) return true ; return false ; } bool solveMaze ( int maze [ N ] [ N ] ) { int sol [ N ] [ N ] = { { 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } } ; if ( solveMazeUtil ( maze , 0 , 0 , sol ) == false ) { printf ( " Solution ▁ doesn ' t ▁ exist " ) ; return false ; } printSolution ( sol ) ; return true ; } bool solveMazeUtil ( int maze [ N ] [ N ] , int x , int y , int sol [ N ] [ N ] ) { if ( x == N - 1 && y == N - 1 ) { sol [ x ] [ y ] = 1 ; return true ; } if ( isSafe ( maze , x , y ) == true ) { sol [ x ] [ y ] = 1 ; for ( int i = 1 ; i <= maze [ x ] [ y ] && i < N ; i ++ ) { if ( solveMazeUtil ( maze , x + i , y , sol ) == true ) return true ; if ( solveMazeUtil ( maze , x , y + i , sol ) == true ) return true ; } sol [ x ] [ y ] = 0 ; return false ; } return false ; } int main ( ) { int maze [ N ] [ N ] = { { 2 , 1 , 0 , 0 } , { 3 , 0 , 0 , 1 } , { 0 , 1 , 0 , 1 } , { 0 , 0 , 0 , 1 } } ; solveMaze ( maze ) ; return 0 ; }
Prime numbers after prime P with sum S | CPP Program to print all N primes after prime P whose sum equals S ; vector to store prime and N primes whose sum equals given S ; function to check prime number ; square root of x ; since 1 is not prime number ; if any factor is found return false ; no factor found ; function to display N primes whose sum equals S ; function to evaluate all possible N primes whose sum equals S ; if total equals S And total is reached using N primes ; display the N primes ; if total is greater than S or if index has reached last element ; add prime [ index ] to set vector ; include the ( index ) th prime to total ; remove element from set vector ; exclude ( index ) th prime ; function to generate all primes ; all primes less than S itself ; if i is prime add it to prime vector ; if primes are less than N ; Driver Code
#include <iostream> NEW_LINE #include <vector> NEW_LINE #include <cmath> NEW_LINE using namespace std ; vector < int > set ; vector < int > prime ; bool isPrime ( int x ) { int sqroot = sqrt ( x ) ; bool flag = true ; if ( x == 1 ) return false ; for ( int i = 2 ; i <= sqroot ; i ++ ) if ( x % i == 0 ) return false ; return true ; } void display ( ) { int length = set . size ( ) ; for ( int i = 0 ; i < length ; i ++ ) cout << set [ i ] << " ▁ " ; cout << " STRNEWLINE " ; } void primeSum ( int total , int N , int S , int index ) { if ( total == S && set . size ( ) == N ) { display ( ) ; return ; } if ( total > S || index == prime . size ( ) ) return ; set . push_back ( prime [ index ] ) ; primeSum ( total + prime [ index ] , N , S , index + 1 ) ; set . pop_back ( ) ; primeSum ( total , N , S , index + 1 ) ; } void allPrime ( int N , int S , int P ) { for ( int i = P + 1 ; i <= S ; i ++ ) { if ( isPrime ( i ) ) prime . push_back ( i ) ; } if ( prime . size ( ) < N ) return ; primeSum ( 0 , N , S , 0 ) ; } int main ( ) { int S = 54 , N = 2 , P = 3 ; allPrime ( N , S , P ) ; return 0 ; }
A backtracking approach to generate n bit Gray Codes | CPP program to find the gray sequence of n bits . ; we have 2 choices for each of the n bits either we can include i . e invert the bit or we can exclude the bit i . e we can leave the number as it is . ; base case when we run out bits to process we simply include it in gray code sequence . ; ignore the bit . ; invert the bit . ; returns the vector containing the gray code sequence of n bits . ; num is passed by reference to keep track of current code . ; Driver function .
#include <iostream> NEW_LINE #include <vector> NEW_LINE using namespace std ; void grayCodeUtil ( vector < int > & res , int n , int & num ) { if ( n == 0 ) { res . push_back ( num ) ; return ; } grayCodeUtil ( res , n - 1 , num ) ; num = num ^ ( 1 << ( n - 1 ) ) ; grayCodeUtil ( res , n - 1 , num ) ; } vector < int > grayCodes ( int n ) { vector < int > res ; int num = 0 ; grayCodeUtil ( res , n , num ) ; return res ; } int main ( ) { int n = 3 ; vector < int > code = grayCodes ( n ) ; for ( int i = 0 ; i < code . size ( ) ; i ++ ) cout << code [ i ] << endl ; return 0 ; }
Partition of a set into K subsets with equal sum | C ++ program to check whether an array can be partitioned into K subsets of equal sum ; * array - given input array subsetSum array - sum to store each subset of the array taken - boolean array to check whether element is taken into sum partition or not K - number of partitions needed N - total number of element in array curIdx - current subsetSum index limitIdx - lastIdx from where array element should be taken ; current index ( K - 2 ) represents ( K - 1 ) subsets of equal sum last partition will already remain with sum ' subset ' ; recursive call for next subsetition ; start from limitIdx and include elements into current partition ; if already taken , continue ; if temp is less than subset then only include the element and call recursively ; mark the element and include into current partition sum ; after recursive call unmark the element and remove from subsetition sum ; Method returns true if arr can be partitioned into K subsets with equal sum ; If K is 1 , then complete array will be our answer ; If total number of partitions are more than N , then division is not possible ; if array sum is not divisible by K then we can 't divide  array into K partitions ; the sum of each subset should be subset ( = sum / K ) ; Initialize sum of each subset from 0 ; mark all elements as not taken ; initialize first subsubset sum as last element of array and mark that as taken ; call recursive method to check K - substitution condition ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isKPartitionPossibleRec ( int arr [ ] , int subsetSum [ ] , bool taken [ ] , int subset , int K , int N , int curIdx , int limitIdx ) { if ( subsetSum [ curIdx ] == subset ) { if ( curIdx == K - 2 ) return true ; return isKPartitionPossibleRec ( arr , subsetSum , taken , subset , K , N , curIdx + 1 , N - 1 ) ; } for ( int i = limitIdx ; i >= 0 ; i -- ) { if ( taken [ i ] ) continue ; int tmp = subsetSum [ curIdx ] + arr [ i ] ; if ( tmp <= subset ) { taken [ i ] = true ; subsetSum [ curIdx ] += arr [ i ] ; bool nxt = isKPartitionPossibleRec ( arr , subsetSum , taken , subset , K , N , curIdx , i - 1 ) ; taken [ i ] = false ; subsetSum [ curIdx ] -= arr [ i ] ; if ( nxt ) return true ; } } return false ; } bool isKPartitionPossible ( int arr [ ] , int N , int K ) { if ( K == 1 ) return true ; if ( N < K ) return false ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; if ( sum % K != 0 ) return false ; int subset = sum / K ; int subsetSum [ K ] ; bool taken [ N ] ; for ( int i = 0 ; i < K ; i ++ ) subsetSum [ i ] = 0 ; for ( int i = 0 ; i < N ; i ++ ) taken [ i ] = false ; subsetSum [ 0 ] = arr [ N - 1 ] ; taken [ N - 1 ] = true ; return isKPartitionPossibleRec ( arr , subsetSum , taken , subset , K , N , 0 , N - 1 ) ; } int main ( ) { int arr [ ] = { 2 , 1 , 4 , 5 , 3 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 3 ; if ( isKPartitionPossible ( arr , N , K ) ) cout << " Partitions ▁ into ▁ equal ▁ sum ▁ is ▁ possible . STRNEWLINE " ; else cout << " Partitions ▁ into ▁ equal ▁ sum ▁ is ▁ not ▁ possible . STRNEWLINE " ; }
Remove Invalid Parentheses | C / C ++ program to remove invalid parenthesis ; method checks if character is parenthesis ( openor closed ) ; method returns true if string contains valid parenthesis ; method to remove invalid parenthesis ; visit set to ignore already visited string ; queue to maintain BFS ; pushing given string as starting node into queue ; If answer is found , make level true so that valid string of only that level are processed . ; Removing parenthesis from str and pushing into queue , if not visited already ; Driver code to check above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isParenthesis ( char c ) { return ( ( c == ' ( ' ) || ( c == ' ) ' ) ) ; } bool isValidString ( string str ) { int cnt = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == ' ( ' ) cnt ++ ; else if ( str [ i ] == ' ) ' ) cnt -- ; if ( cnt < 0 ) return false ; } return ( cnt == 0 ) ; } void removeInvalidParenthesis ( string str ) { if ( str . empty ( ) ) return ; set < string > visit ; queue < string > q ; string temp ; bool level ; q . push ( str ) ; visit . insert ( str ) ; while ( ! q . empty ( ) ) { str = q . front ( ) ; q . pop ( ) ; if ( isValidString ( str ) ) { cout << str << endl ; level = true ; } if ( level ) continue ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( ! isParenthesis ( str [ i ] ) ) continue ; temp = str . substr ( 0 , i ) + str . substr ( i + 1 ) ; if ( visit . find ( temp ) == visit . end ( ) ) { q . push ( temp ) ; visit . insert ( temp ) ; } } } } int main ( ) { string expression = " ( ) ( ) ) ( ) " ; removeInvalidParenthesis ( expression ) ; expression = " ( ) v ) " ; removeInvalidParenthesis ( expression ) ; return 0 ; }
N Queen Problem | Backtracking | C / C ++ program to solve N Queen Problem using backtracking ; ld is an array where its indices indicate row - col + N - 1 ( N - 1 ) is for shifting the difference to store negative indices ; rd is an array where its indices indicate row + col and used to check whether a queen can be placed on right diagonal or not ; column array where its indices indicates column and used to check whether a queen can be placed in that row or not ; A utility function to print solution ; A recursive utility function to solve N Queen problem ; base case : If all queens are placed then return true ; Consider this column and try placing this queen in all rows one by one ; A check if a queen can be placed on board [ row ] [ col ] . We just need to check ld [ row - col + n - 1 ] and rd [ row + coln ] where ld and rd are for left and right diagonal respectively ; Place this queen in board [ i ] [ col ] ; recur to place rest of the queens ; board [ i ] [ col ] = 0 ; BACKTRACK ; If the queen cannot be placed in any row in this colum col then return false ; This function solves the N Queen problem using Backtracking . It mainly uses solveNQUtil ( ) to solve the problem . It returns false if queens cannot be placed , otherwise , return true and prints placement of queens in the form of 1 s . Please note that there may be more than one solutions , this function prints one of the feasible solutions . ; driver program to test above function
#define N  4 NEW_LINE #include <stdbool.h> NEW_LINE #include <stdio.h> NEW_LINE int ld [ 30 ] = { 0 } ; int rd [ 30 ] = { 0 } ; int cl [ 30 ] = { 0 } ; void printSolution ( int board [ N ] [ N ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) printf ( " ▁ % d ▁ " , board [ i ] [ j ] ) ; printf ( " STRNEWLINE " ) ; } } bool solveNQUtil ( int board [ N ] [ N ] , int col ) { if ( col >= N ) return true ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( ld [ i - col + N - 1 ] != 1 && rd [ i + col ] != 1 ) && cl [ i ] != 1 ) { board [ i ] [ col ] = 1 ; ld [ i - col + N - 1 ] = rd [ i + col ] = cl [ i ] = 1 ; if ( solveNQUtil ( board , col + 1 ) ) return true ; ld [ i - col + N - 1 ] = rd [ i + col ] = cl [ i ] = 0 ; } } return false ; } bool solveNQ ( ) { int board [ N ] [ N ] = { { 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } } ; if ( solveNQUtil ( board , 0 ) == false ) { printf ( " Solution ▁ does ▁ not ▁ exist " ) ; return false ; } printSolution ( board ) ; return true ; } int main ( ) { solveNQ ( ) ; return 0 ; }
Given an array A [ ] and a number x , check for pair in A [ ] with sum as x | Code in cpp to tell if there exists a pair in array whose sum results in x . ; Function to print pairs ; initializing the rem values with 0 's. ; Perform the remainder operation only if the element is x , as numbers greater than x can 't  be used to get a sum x.  Updating the count of remainders. ; Traversing the remainder list from start to middle to find pairs ; The elements with remainders i and x - i will result to a sum of x . Once we get two elements which add up to x , we print x and break . ; Once we reach middle of remainder array , we have to do operations based on x . ; if x is even and we have more than 1 elements with remainder x / 2 , then we will have two distinct elements which add up to x . if we dont have more than 1 element , print " No " . ; When x is odd we continue the same process which we did in previous loop . ; Driver Code ; Function calling
#include <iostream> NEW_LINE using namespace std ; void printPairs ( int a [ ] , int n , int x ) { int i ; int rem [ x ] ; for ( i = 0 ; i < x ; i ++ ) { rem [ i ] = 0 ; } for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] < x ) { rem [ a [ i ] % x ] ++ ; } } for ( i = 1 ; i < x / 2 ; i ++ ) { if ( rem [ i ] > 0 && rem [ x - i ] > 0 ) { cout << " Yes " << " STRNEWLINE " ; break ; } } if ( i >= x / 2 ) { if ( x % 2 == 0 ) { if ( rem [ x / 2 ] > 1 ) { cout << " Yes " << " STRNEWLINE " ; } else { cout << " No " << " STRNEWLINE " ; } } else { if ( rem [ x / 2 ] > 0 && rem [ x - x / 2 ] > 0 ) { cout << " Yes " << " STRNEWLINE " ; } else { cout << " No " << " STRNEWLINE " ; } } } } int main ( ) { int A [ ] = { 1 , 4 , 45 , 6 , 10 , 8 } ; int n = 16 ; int arr_size = sizeof ( A ) / sizeof ( A [ 0 ] ) ; printPairs ( A , arr_size , n ) ; return 0 ; }
Find remainder when a number A raised to N factorial is divided by P | C ++ program for above approach ; Function to calculate factorial of a Number ; Calculating factorial ; Returning factorial ; Function to calculate resultant remainder ; Function call to calculate factorial of n ; Calculating remainder ; Returning resultant remainder ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int fact ( long long int n ) { long long int ans = 1 ; for ( long long int i = 2 ; i <= n ; i ++ ) ans *= i ; return ans ; } long long int remainder ( long long int n , long long int a , long long int p ) { long long int len = fact ( n ) ; long long int ans = 1 ; for ( long long int i = 1 ; i <= len ; i ++ ) ans = ( ans * a ) % p ; return ans ; } int main ( ) { long long int A = 2 , N = 1 , P = 2 ; cout << remainder ( N , A , P ) << endl ; }
Check if a number N can be expressed in base B | C ++ program for the above approach ; Function to check if a number N can be expressed in base B ; Check if n is greater than 0 ; Initialize a boolean variable ; Check if digit is 0 or 1 ; Subtract the appropriate power of B from it and increment higher digit ; Driver Code ; Given Number N and base B ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int n , int w ) { vector < int > a ( 105 ) ; int p = 0 ; while ( n > 0 ) { a [ p ++ ] = n % w ; n /= w ; } bool flag = true ; for ( int i = 0 ; i <= 100 ; i ++ ) { if ( a [ i ] == 0 a [ i ] == 1 ) continue ; else if ( a [ i ] == w a [ i ] == w - 1 ) a [ i + 1 ] ++ ; else flag = false ; } return flag ; } int main ( ) { int B = 3 , N = 7 ; if ( check ( N , B ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Count of groups among N people having only one leader in each group | C ++ program for the above approach ; Function to find 2 ^ x using modular exponentiation ; Base cases ; If B is even ; If B is odd ; Function to count the number of ways to form the group having one leader ; Find 2 ^ ( N - 1 ) using modular exponentiation ; Count total ways ; Print the total ways ; Driver Code ; Given N number of peoples ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long mod = 1000000007 ; int exponentMod ( int A , int B ) { if ( A == 0 ) return 0 ; if ( B == 0 ) return 1 ; long long y ; if ( B % 2 == 0 ) { y = exponentMod ( A , B / 2 ) ; y = ( y * y ) % mod ; } else { y = A % mod ; y = ( y * exponentMod ( A , B - 1 ) % mod ) % mod ; } return ( int ) ( ( y + mod ) % mod ) ; } void countWays ( int N ) { long long select = exponentMod ( 2 , N - 1 ) ; long long ways = ( ( N % mod ) * ( select % mod ) ) ; ways %= mod ; cout << ways ; } int main ( ) { int N = 5 ; countWays ( N ) ; }
Expected number of moves to reach the end of a board | Matrix Exponentiation | C ++ implementation of the approach ; Function to multiply two 7 * 7 matrix ; Function to perform matrix exponentiation ; 7 * 7 identity matrix ; Loop to find the power ; Function to return the required count ; Base cases ; Multiplier matrix ; Finding the required multiplier i . e mul ^ ( X - 6 ) ; Final answer ; Driver code
#include <bits/stdc++.h> NEW_LINE #define maxSize  50 NEW_LINE using namespace std ; vector < vector < double > > matrix_product ( vector < vector < double > > a , vector < vector < double > > b ) { vector < vector < double > > c ( 7 ) ; for ( int i = 0 ; i < 7 ; i ++ ) c [ i ] . resize ( 7 , 0 ) ; for ( int i = 0 ; i < 7 ; i ++ ) for ( int j = 0 ; j < 7 ; j ++ ) for ( int k = 0 ; k < 7 ; k ++ ) c [ i ] [ j ] += a [ i ] [ k ] * b [ k ] [ j ] ; return c ; } vector < vector < double > > mul_expo ( vector < vector < double > > mul , int p ) { vector < vector < double > > s = { { 1 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 1 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 1 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 1 } } ; while ( p != 1 ) { if ( p % 2 == 1 ) s = matrix_product ( s , mul ) ; mul = matrix_product ( mul , mul ) ; p /= 2 ; } return matrix_product ( mul , s ) ; } double expectedSteps ( int x ) { if ( x == 0 ) return 0 ; if ( x <= 6 ) return 6 ; vector < vector < double > > mul = { { ( double ) 7 / 6 , 1 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 1 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 1 } , { ( double ) -1 / 6 , 0 , 0 , 0 , 0 , 0 , 0 } } ; mul = mul_expo ( mul , x - 6 ) ; return ( mul [ 0 ] [ 0 ] + mul [ 1 ] [ 0 ] + mul [ 2 ] [ 0 ] + mul [ 3 ] [ 0 ] + mul [ 4 ] [ 0 ] + mul [ 5 ] [ 0 ] ) * 6 ; } int main ( ) { int n = 10 ; cout << expectedSteps ( n - 1 ) ; return 0 ; }
Place the prisoners into cells to maximize the minimum difference between any two | C ++ implementation of the approach ; Function that returns true if the prisoners can be placed such that the minimum distance between any two prisoners is at least sep ; Considering the first prisoner is placed at 1 st cell ; If the first prisoner is placed at the first cell then the last_prisoner_placed will be the first prisoner placed and that will be in cell [ 0 ] ; Checking if the prisoner can be placed at ith cell or not ; If all the prisoners got placed then return true ; Function to return the maximized distance ; Sort the array so that binary search can be applied on it ; Minimum possible distance for the search space ; Maximum possible distance for the search space ; To store the result ; Binary search ; If the prisoners can be placed such that the minimum distance between any two prisoners is at least mid ; Update the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool canPlace ( int a [ ] , int n , int p , int sep ) { int prisoners_placed = 1 ; int last_prisoner_placed = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { int current_cell = a [ i ] ; if ( current_cell - last_prisoner_placed >= sep ) { prisoners_placed ++ ; last_prisoner_placed = current_cell ; if ( prisoners_placed == p ) { return true ; } } } return false ; } int maxDistance ( int cell [ ] , int n , int p ) { sort ( cell , cell + n ) ; int start = 0 ; int end = cell [ n - 1 ] - cell [ 0 ] ; int ans = 0 ; while ( start <= end ) { int mid = start + ( ( end - start ) / 2 ) ; if ( canPlace ( cell , n , p , mid ) ) { ans = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } return ans ; } int main ( ) { int cell [ ] = { 1 , 2 , 8 , 4 , 9 } ; int n = sizeof ( cell ) / sizeof ( int ) ; int p = 3 ; cout << maxDistance ( cell , n , p ) ; return 0 ; }
Find N in the given matrix that follows a pattern | C ++ implementation of the approach ; Function to return the row and the column of the given integer ; Binary search for the row number ; Condition to get the maximum x that satisfies the criteria ; Binary search for the column number ; Condition to get the maximum y that satisfies the criteria ; Get the row and the column number ; Return the pair ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; pair < int , int > solve ( int n ) { int low = 1 , high = 1e4 , x = n , p = 0 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int sum = ( mid * ( mid + 1 ) ) / 2 ; if ( x - sum >= 1 ) { p = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } int start = 1 , end = 1e4 , y = 1 , q = 0 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; int sum = ( mid * ( mid + 1 ) ) / 2 ; if ( y + sum <= n ) { q = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } x = x - ( p * ( p + 1 ) ) / 2 ; y = y + ( q * ( q + 1 ) ) / 2 ; int r = x ; int c = q + 1 - n + y ; pair < int , int > ans = { r , c } ; return ans ; } int main ( ) { int n = 5 ; pair < int , int > p = solve ( n ) ; cout << p . first << " ▁ " << p . second ; return 0 ; }