Programming Articles - Page 2162 of 3366

Maximum number of fixed points using at most 1 swaps in C++

Narendra Kumar
Updated on 10-Jan-2020 07:58:37

205 Views

Problem statementGiven a permutation of N elements from 0 to N-1. A fixed point is an index at which the value is same as the index i.e. arr[i] = i. You are allowed to make at most 1 swap. Find the maximum number of fixed points that you can get.ExampleIf input array is {0, 1, 2, 3, 4, 6, 5} then answer is 7.To adjust fixed point, we have to swap 6 and 5After this entire array becomes fixed point and maximum value of fixed point is 7.AlgorithmCreate an array pos which keeps the position of each element in the ... Read More

Maximum number of edges to be added to a tree so that it stays a Bipartite graph in C++

Narendra Kumar
Updated on 10-Jan-2020 07:56:14

347 Views

Problem statementA tree is always a Bipartite Graph as we can always break into two disjoint sets with alternate levels.In other words, we always color it with two colors such that alternate levels have same color. The task is to compute the maximum no. of edges that can be added to the tree so that it remains Bipartite Graph.ExampleTree edges are represented in vertex pairs as follows −{1, 2}{1, 3}{2, 4}{3, 5} then we need 2 more edges to keep it Bipartite GraphIn a coloring graph the graph {1, 4, 5} and {2, 3} from two different sets. Since, 1 ... Read More

Maximum number of edges in Bipartite graph in C++

Narendra Kumar
Updated on 10-Jan-2020 07:53:40

364 Views

Problem statementGiven an integer N which represents the number of Vertices. The Task is to find the maximum number of edges possible in a Bipartite graph of N vertices.Bipartite GraphA Bipartite graph is one which is having 2 sets of vertices.The set are such that the vertices in the same set will never share an edge between them.ExampleIf N = 10 then there will be total 25 edges −Both sets will contain 5 vertices and every vertex of first set will have an edge to every other vertex of the second setHence total edges will be 5 * 5 = ... Read More

Maximum number of 3-person teams formed from two groups in C++

Narendra Kumar
Updated on 03-Jun-2020 08:38:05

1K+ Views

In this problem, we are given two integers N and M, N is the number of people in group 1, and M is the number of people in group 2. Our task is to create a program to find the maximum number of 3-person teams formed from two groups.We will be creating teams of 3 people by selecting a person from these groups such that the maximum teams can be made. Each team must have at least one person from each group.Let’s take an example to understand the problem, Input − N = 5, M = 3Output − 2Explanation −The ... Read More

How to write lambda expression code for SwingUtilities.invokeLater in Java?

raja
Updated on 13-Jul-2020 13:02:57

888 Views

An event handling code that runs on a special thread called event dispatch thread(EDT). Most of the code that invokes swing methods also runs on this EDT thread. It is necessary because most of swing object methods do not thread-safe. SwingUtilities is a utility class and has one important static method, invokeLater(). This method can be used to perform a task asynchronously in the AWT event dispatcher thread.Syntaxpublic static void invokeLater(Runnable doRun)Exampleimport javax.swing.*; public class InvokeLAterLambdaTest {    public static void main(String[] args) {       SwingUtilities.invokeLater(() -> {    // lambda expression code          JFrame frame = ... Read More

Maximum modulo of all the pairs of array where arr[i] >= arr[j] in C++

Narendra Kumar
Updated on 03-Jun-2020 08:40:13

634 Views

In this problem, we are given an array, are of n elements. Our task is to create a program to find the maximum modulo of all pairs of array where arr[i] >= arr[j].Here, we have to find the maximum value of arr[i] % arr[j] where arr[i] >= arr[j].Let’s take an example to understand the problem, Input − arr[] = {3, 5, 9}Output − 4Explanation −All possible Pairs arr[i] and arr[j], 5, 3 => 5%3 = 2 9, 3 => 9%3 = 0 9, 5 => 9%5 = 4To solve this problem, a simple and direct approach will be run two ... Read More

Maximum length of the sub-array whose first and last elements are same in C++

Narendra Kumar
Updated on 03-Jun-2020 08:41:26

359 Views

In this problem, we are given an array of characters. Our task is to create a program to print the maximum length of the subarray whose first and last elements are same in C++.Let’s take an example to understand the problem, Input − array = {‘t’, ‘u’, ‘t’, ‘o’, ‘r’, ‘i’, ‘a’, ‘l’, ‘s’, ‘p’, ‘o’, ‘i’, ‘n’, ‘t’ }Output − 14Explanation −The subarray {‘t’, ‘u’, ‘t’, ‘o’, ‘r’, ‘i’, ‘a’, ‘l’, ‘s’, ‘p’, ‘o’, ‘i’, ‘n’, ‘t’ } starts and ends with t.To solve this problem, we find the first and last occurrence a character in the array and ... Read More

Difference between system level exception and Application level exception.

Mahesh Parahar
Updated on 25-Feb-2020 06:26:47

3K+ Views

As we know that exception is something that refers to the interruption in the flow of program or application. This unwanted event is known as Exception and is generally gives the indication regarding something wrong within the code. Basically particularly in language C# an exception can be a System or an Application Level exception. So on the basisSr. No.KeySystem level exceptionApplication level exception1DerivationSystem exceptions are derived from the base class System.SystemException which in itself is a derived class of SystemException.On other hand Application-level exceptions are derived from the base class System.ApplicationException which is again a derived class of SystemException2OccurrenceIn general ... Read More

Maximum length of segments of 0’s and 1’s in C++

Narendra Kumar
Updated on 10-Jan-2020 07:40:21

264 Views

Problem statementGiven a string comprising of ones and zeros. The task is to find the maximum length of the segments of string such that a number of 1 in each segment is greater than 0ExampleIf input string is “10111000001011” the answer will 12 as follows −First segment is of length 7 10111000001011Second segment is of length 5 10111000001011Total length is length of (segment 1 + segment 2) = (7 + 5) = 12AlgorithmIf start == n then return 0.Run a loop from start till n, computing for each subarray till n.If character is 1 then increment the count of 1 ... Read More

Maximum length of rod for Qth person in C++

Narendra Kumar
Updated on 10-Jan-2020 07:36:21

160 Views

Problem statementGiven lengths of n rods in an array. If any person picks any rod, half of the longest rod (or (max + 1) / 2 ) is assigned and remaining part (max – 1) / 2 is put back. It may be assumed that sufficient number of rods are always available, answer M queries given in an array q[] to find the largest length of rod available for qith person, provided qi is a valid person number starting from 1ExampleInput : a[] = {6, 5, 9, 10, 12}    q[] = {1, 3} Output : 12 9 The first ... Read More

Advertisements