Dictionary ContainsKey Method in C#

AmitDiwan
Updated on 04-Nov-2019 07:49:19

2K+ Views

The Dictionary.ContainsKey() method in C# checks whether the Dictionary

Find Sum of Factorials in an Array in C++

Arnab Chakraborty
Updated on 04-Nov-2019 07:43:09

248 Views

Consider we have an array A, which is sorted. It has all elements appears twice, but one element is present for only one time. We have to find that element. If the array is [1, 1, 3, 3, 4, 4, 5, 6, 6, 7, 7, 9, 9], so the single element is 5.We will use the binary search approach to solve this. All elements before the single element has their first occurrence at index 0, 2, 4, … and first occurrence at index 1, 3, 5, … but after the single element, all occurrences of the first number will be ... Read More

Find Largest Word in Dictionary by Deleting Characters in C++

Samual Sam
Updated on 01-Nov-2019 10:54:54

225 Views

Consider we have a dictionary, and a string s. Find the longest string in the dictionary, that can be formed by deleting some characters of the string s. Suppose the s is “apbreoigroakml”, The dictionary has {“prog”, “ram”, “program”}, then the result will be “program”.To solve this, we will traverse all dictionary words, and for each word, we will check whether the subsequence of the given string and is longest of all such words. Finally return longest word with given string as subsequence.Example#include #include using namespace std; bool isSubSequence(string s1, string s2) {    int m = s1.length(), n = ... Read More

Find Numbers A and B that Satisfy Given Condition in C++

Arnab Chakraborty
Updated on 01-Nov-2019 10:35:58

557 Views

Consider we have an integer n. Our task is to find two numbers a and b, where these three conditions will be satisfied.a mod b = 0a * b > na / b < nIf no pair is found, print -1.For an example, if the number n = 10, then a and b can be a = 90, b = 10. This satisfies given rules.To solve this problem, we will follow these steps −Let b = n. a can be found using these three conditionsa mod b = 0 when a is multiple of ba / b < n, so ... Read More

Find N-Digit Number Divisible by D in C++

Arnab Chakraborty
Updated on 01-Nov-2019 10:33:46

177 Views

Suppose we have two numbers N and D. We have to find N digit number, that is divisible by D. If N is 3, and D is 5, then the number can be 500. This can be solved easily. If D is 10 and N is 1, then it will be impossible. We can put D, and suppose the D has m number of digits, then attach N – m number of 0s to make it N digit number and divisible by D.Example#include using namespace std; string nDigitDivByD(int n, int d) {    string ans = "";    if (d ... Read More

Find Most Significant Set Bit of a Number in C++

Arnab Chakraborty
Updated on 01-Nov-2019 10:28:04

2K+ Views

Here we will see if a number is given, then how to find the value of Most Significant Bit value, that is set. The value is power of 2. So if the number is 10, MSB value will be 8.We have to find the position of MSB, then find the value of the number with a set-bit at kth position.Example#include #include using namespace std; int msbBitValue(int n) {    int k = (int)(log2(n));    return (int)(pow(2, k)); } int main() {    int n = 150;    cout

Find Middle of Singly Linked List Recursively in C++

Arnab Chakraborty
Updated on 01-Nov-2019 10:26:31

521 Views

Consider we have a list of numbers; our task is to find the middle of the linked list using recursion. So if the list elements are [12, 14, 18, 36, 96, 25, 62], then the middle element is 36.To solve this problem, we will count total number of nodes in the list in recursive manner and do half of this. Then rolling back through recursion decrement n by 1 in each call, return element where n is zero.Example#include #include using namespace std; class Node{    public:       int data;       Node *next; }; Node* getNode(int data){ ... Read More

Find Maximum Element of Each Row in a Matrix in C++

Arnab Chakraborty
Updated on 01-Nov-2019 10:23:30

695 Views

Consider we have a matrix, our task is to find the maximum element of each row of that matrix and print them. This task is simple. For each row, reset the max, and find the max element, and print it. Let us see the code for better understanding.Example#include #define MAX 10 using namespace std; void largestInEachRow(int mat[][MAX], int rows, int cols) {    for (int i = 0; i < rows; i++) {       int max_row_element = mat[i][0];    for (int j = 1; j < cols; j++) {       if (mat[i][j] > max_row_element)          max_row_element = mat[i][j];    }    cout

Re-throwing Exceptions in Java

Maruthi Krishna
Updated on 01-Nov-2019 10:22:40

5K+ Views

When an exception is cached in a catch block, you can re-throw it using the throw keyword (which is used to throw the exception objects).While re-throwing exceptions you can throw the same exception as it is without adjusting it as −try {    int result = (arr[a])/(arr[b]);    System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result); } catch(ArithmeticException e) {    throw e; }Or, wrap it within a new exception and throw it. When you wrap a cached exception within another exception and throw it, it is known as exception chaining or, exception wrapping, by doing this you can adjust your exception, throwing a ... Read More

Find Maximum Element of Each Column in a Matrix in C++

Arnab Chakraborty
Updated on 01-Nov-2019 10:21:54

575 Views

Consider we have a matrix, our task is to find the maximum element of each column of that matrix and print them. This task is simple. For each column, reset the max, and find the max element, and print it. Let us see the code for better understanding.Example#include #define MAX 10 using namespace std; void largestInEachCol(int mat[][MAX], int rows, int cols) {    for (int i = 0; i < cols; i++) {       int max_col_element = mat[0][i];    for (int j = 1; j < rows; j++) {       if (mat[j][i] > max_col_element)          max_col_element = mat[j][i];    }    cout

Advertisements