Programming Articles - Page 1123 of 3363

How to find the longest distance to a character in a given string using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 07:16:51

309 Views

Create 2 different arrays leftDis and rightDis. The leftDis will store the value when moved from left direction. The rightDis will store the shortest value when moved from right. Whenever the character is met add the position of the character to the array. In the last step calculate the maximum of both the arrays.Time Complexity − O(n)Space Complexity − O(n)Examplepublic class Arrays{    public int[] LongestDistanceToCharacter(string s, char c){       int stringLength = s.Length;       int[] leftDis = new int[s.Length];       int[] rightDis = new int[s.Length];       leftDis = Enumerable.Range(0, s.Length).Select(n => ... Read More

How to find the shortest distance to a character in a given string using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 07:07:19

401 Views

Create 2 different arrays leftDis and rightDis. The leftDis will store the value when moved from left direction. The rightDis will store the shortest value when moved from right. Whenever the character is met add the position of the character to the array. In the last step calculate the minimum of both the arrays.Time Complexity − O(n)Space Complexity − O(n)Examplepublic class Arrays{    public int[] ShortestDistanceToCharacter(string s, char c){       int stringLength = s.Length;       int[] leftDis = new int[s.Length];       int[] rightDis = new int[s.Length];       leftDis = Enumerable.Range(0, s.Length).Select(n => ... Read More

How to find all unique triplets that adds up to sum Zero using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 07:03:40

417 Views

The easy approach is that we could create three nested loops and check one by one that the sum of all the three elements is zero or not. If the sum of the three elements is zero then print elements.Time Complexity − O(n3)Space Complexity − O(1)We could use an unordered set data structure to store each value of the array. Set provides the benefit of searching an element in O(1) time. So, for each pair in the array, we will look for the negative of their sum that might exist in the set. If such an element is found then ... Read More

How to search in a row wise increased matrix using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 06:49:57

219 Views

The primitive solution for this problem is to scan all elements stored in the input matrix to search for the given key. This linear search approach costs O(MN) time if the size of the matrix is MxN.The matrix needs to be scanned from the top right, if the search element is greater than the top right element then increments the row or else decrement the column. The below piece of code develops a function SearchRowwiseIncrementedMatrix that takes a two-dimensional array and search key as input and returns either true or false depending upon the success or failure of search key ... Read More

How to search in a row wise and column wise increased matrix using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 06:46:07

281 Views

The primitive solution for this problem is to scan all elements stored in the input matrix to search for the given key. This linear search approach costs O(MN) time if the size of the matrix is MxN.The matrix can be viewed as a sorted one-dimensional array. If all rows in the input matrix are concatenated in top-down order, it forms a sorted one-dimensional array. And, in that case binary search algorithm is suitable for this 2D array. The below piece of code develops a function SearchRowwiseColumnWiseMatrix that takes a two-dimensional array and search key as input and returns either true ... Read More

How to check whether the given matrix is a Toeplitz matrix using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 06:39:33

711 Views

A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.Example 1[[1, 2, 3, 4], [5, 1, 2, 3], [9, 5, 1, 2]]Output −trueIn the above grid, the diagonals are −"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]".In each diagonal all elements are the same, so the answer is True.Example 2Input: matrix [[1, 2], [2, 2]]Output −falseThe diagonal "[1, 2]" has different elementsCodepublic class Matrix    {    public bool ToeplitzMatrix(int[, ] mat)    {       int row = getMatrixRowSize(mat);       int col = getMatrixColSize(mat);       ... Read More

Node.js – Base64 Encoding & Decoding

Mayank Agarwal
Updated on 16-Aug-2021 12:18:20

7K+ Views

The buffer object can be encoded and decoded into Base64 string. The buffer class can be used to encode a string into a series of bytes. The Buffer.from() method takes a string as an input and converts it into Base64.The converted bytes can be changed again into String. The toString() method is used for converting the Base64 buffer back into the string format.SyntaxBuffer.from(string, [encoding]) object.toString(encoding)ParametersThe parameters are described below:string − This input parameter takes input for the string that will be encoded into the base64 format.encoding − This input parameter takes input for the encoding in which string will be encoded and ... Read More

Node.js – util.format() method

Mayank Agarwal
Updated on 16-Aug-2021 12:14:39

2K+ Views

The util.format() method returns a formatted string that will use the first argument as a printf like format string. This format can also contain zero or more format specifiers. These specifiers can be replaced with the converted value from the corresponding argument.Following are some of the formatted specifiers:%s − String will be used to convert all values except bigInt, object and -0.%d − In this case, number will be used to convert all values except BigInt and symbo%i − parseInt(value, 10) will be used for all values except BigInt and symbol.%f − parseFloat(value) will be used for all values except Symbol%j − This format ... Read More

Node.js – util.callbackify() Method

Mayank Agarwal
Updated on 16-Aug-2021 12:12:32

628 Views

The util.callbackify() method takes an async function as a parameter (or a function with Promise) and returns a function with the callback style. The callback will hold the rejection reason as the first parameter (or null in case of Promise) and the resolved value as the second parameter.Syntaxutil.callbackify(function)Parametersfunction − The input parameter for the async_function required for callback.Example 1Create a file "callbackify.js" and copy the following code snippet. After creating the file, use the command "node callbackify.js" to run this code.// util.callbackify() demo example // Importing the util module const util = require('util'); // Defining a simple async function ... Read More

Node.js – hmac.digest() Method

Mayank Agarwal
Updated on 16-Aug-2021 12:10:41

1K+ Views

The Hmac class is one of the many utility classes that is used for creating the cryptographic HMAC digests. The Hmac.digest() method is used for calculating all the data that is updated using the Hmac.update() method. If encoding is provided, a string will be returned, else a buffer is returned.Syntaxhmac.digest( [encoding] )Parametersencoding − This input parameter takes input for the encoding to be considered while calculating hmac.Example 1Create a file "hmacDigest.js" and copy the following code snippet. After creating the file use, use the command "node hmacDigest.js" to run this code. Live Demo// Hmac.digest() Demo Example // Importing the crypto module ... Read More

Advertisements