Find Pairs with Given Sum in Different Rows in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:00:13

196 Views

Suppose we have a matrix of unique elements and a sum; we have to find all the pairs from the matrix whose sum is equal to given sum. Here, each element of pair will be taken from different rows.So, if the input is like −24356987101114121311516sum = 13, then the output will be [(2, 11), (4, 9), (3, 10), (5, 8), (12, 1)]To solve this, we will follow these steps −res := a new listn := size of matrixfor i in range 0 to n, dosort the list matrix[i]for i in range 0 to n - 1, dofor j in range ... Read More

Convert Integer to String with Padding Zero in C#

Nizamuddin Siddiqui
Updated on 19-Aug-2020 10:59:55

2K+ Views

There are several ways to convert an integer to a string in C#.PadLeft − Returns a new string of a specified length in which the beginning of the current string is padded with spaces or with a specified Unicode characterToString − Returns a string that represents the current object.String Interpolation − The $ special character identifies a string literal as an interpolated string. This feature is available starting with C# 6.Example using string padding−Example Live Demousing System; namespace DemoApplication{    class Program{       public static void Main(){          int number = 5;         ... Read More

Query for Ancestor-Descendant Relationship in a Tree in C++

Ayush Gupta
Updated on 19-Aug-2020 10:56:20

172 Views

In this tutorial, we will be discussing a program to find query for ancestor-descendant relationship in a tree.For this we will be provided with a rooted tree and Q queries. Our task is to find the two roots given in the query is an ancestor of the other or not.Example Live Demo#include using namespace std; //using DFS to find the relation between //given nodes void performingDFS(vector g[], int u, int parent, int timeIn[], int timeOut[], int& count) {    timeIn[u] = count++;    for (int i = 0; i < g[u].size(); i++) {       int v = g[u][i]; ... Read More

Find Three Integers with Maximum LCM in C++

Ayush Gupta
Updated on 19-Aug-2020 10:54:33

125 Views

In this tutorial, we will be discussing a program to find three integers less than or equal to N such that their LCM is maximum.For this we will be provided with an integer value. Our task is to find other three integers smaller than the given value such that their LCM is maximum.Example Live Demo#include using namespace std; //finding three integers less than given value //having maximum LCM void findMaximumLCM(int n) {    if (n % 2 != 0) {       cout

Absolute Difference Between Lth and Rth Smallest Number in C++

Ayush Gupta
Updated on 19-Aug-2020 10:53:41

149 Views

In this tutorial, we will be discussing a program to find queries to return the absolute difference between L-th smallest number and the R-th smallest number.For this we will be provided with an array containing integers and Q queries. Our task is to find the absolute difference between the indices of Lth smallest and Rth smallest values.Example Live Demo#include using namespace std; //returning the result of a query int respondingQuery(pair arr[], int l, int r) {    int result = abs(arr[l - 1].second - arr[r - 1].second);    return result; } //implementing the queries void calcDifference(int givenarr[], int a, int ... Read More

Query Distinct Colors in a Subtree of a Colored Tree Using Bit in C++

Ayush Gupta
Updated on 19-Aug-2020 10:51:40

202 Views

In this tutorial, we will be discussing a program to find querying the number of distinct colors in a subtree of a colored tree using BIT.For this we will be provided with rooted tree where each node has a color denoted by given array. Our task is to find all the distinct coloured nodes below the given node in the tree.Example Live Demo#include #define MAXIMUM_COLOUR 1000005 #define MAXIMUM_NUMBER 100005 using namespace std; vector tree[MAXIMUM_NUMBER]; vector table[MAXIMUM_COLOUR]; int isTraversing[MAXIMUM_COLOUR]; int bit[MAXIMUM_NUMBER], getVisTime[MAXIMUM_NUMBER], getEndTime[MAXIMUM_NUMBER]; int getFlatTree[2 * MAXIMUM_NUMBER]; bool vis[MAXIMUM_NUMBER]; int tim = 0; vector< pair< pair, int> > queries; //storing results of ... Read More

Update Index and Find GCD in Range in C++

Ayush Gupta
Updated on 19-Aug-2020 10:48:16

140 Views

In this tutorial, we will be discussing a program to find queries to update a given index and find gcd in range.For this we will be provided with an array containing integers and Q queries. Our task is to find the result of given queries (updating a given value by X, finding the gcd between two given values).Example Live Demo#include using namespace std; //getting middle index int findMiddle(int s, int e) {    return (s + (e - s) / 2); } //updating values at given indices void updateIndexValue(int* st, int ss, int se, int i, int diff, int si) ... Read More

Find Time Taken for Signal to Reach All Positions in a String in C++

Ayush Gupta
Updated on 19-Aug-2020 10:44:25

107 Views

In this tutorial, we will be discussing a program to find time taken for signal to reach all positions in a stringFor this we will be provided with a string containing ‘x’ and ‘o’. A signal originates from ‘x’ and travels in both directions changing one ‘o’ value in one unit time. Our task is to calculate the complete time to convert whole string into ‘x’s.Example Live Demo#include using namespace std; //calculating the total required time int findMaximumDuration(string s, int n) {    int right = 0, left = 0;    int count = 0, maximumLength = INT_MIN;    s ... Read More

Find Two Numbers with Sum and Product Both Same as n in C++

Ayush Gupta
Updated on 19-Aug-2020 10:42:22

175 Views

In this tutorial, we will be discussing a program to find two numbers with sum and product both same as N.For this we will be provided with an integer value. Our task is to find two other integer values whose product and sum is equal to the given value.Example Live Demo#include using namespace std; //finding a and b such that //a*b=N and a+b=N void calculateTwoValues(double N) {    double val = N * N - 4.0 * N;    if (val < 0) {       cout

Find Triplet with Maximum Connecting Nodes in C++

Ayush Gupta
Updated on 19-Aug-2020 10:41:07

111 Views

In this tutorial, we will be discussing a program to find triplet such that number of nodes connecting these triplets is maximum.For this we will be provided with a tree with N nodes. Our task is to find a triplet of nodes such that the nodes covered in the path joining them in maximum.Example Live Demo#include #define ll long long int #define MAX 100005 using namespace std; vector nearNode[MAX]; bool isTraversed[MAX]; //storing the required nodes int maxi = -1, N; int parent[MAX]; bool vis[MAX]; int startnode, endnode, midNode; //implementing DFS to search nodes void performDFS(int u, int count) {   ... Read More

Advertisements