C++ Program to Solve the 0-1 Knapsack Problem

Nancy Den
Updated on 29-Apr-2025 15:35:40

6K+ Views

In the 0-1 knapsack problem, a set of items is given, each with a weight and a value. We need to determine the number of each item to include in a collection so that the total weight is less than or equal to the given limit and the total value is as large as possible.ExampleThe following example explains the 0-1 knapsack problem: Input: Weights: 1 2 3 6 7 4 Values: 10 20 25 40 60 70 Max Weight Capacity: 7 Output: Maximum value: 100 Here is an explanation of the above example: Weights: ... Read More

Convert List to JSON Array Using Jackson Library in Java

Manisha Chand
Updated on 29-Apr-2025 12:24:45

12K+ Views

JSON is used in Java applications in APIs, file storage, and data communication between systems. Sometimes, we need to convert a list into a JSON array. In this article, we will learn how to convert a list to JSON array using the Jackson library. Jackson Library It is a library that is used in Java to work with JSON data. It provides APIs to serialize Java objects into JSON and deserialize JSON back into Java objects. If you want to read more about the Jackson library, you can refer Jackson library. There are mainly three components of Jackson - ... Read More

fabs in C++

Chandu yadav
Updated on 29-Apr-2025 11:32:37

681 Views

In C++, fabs is used to calculate the absolute value of a floating-point number. It is a function and defined to a header file. The fab() FunctionThis fabs() function accepts one argument, which is a floating-point number (double, float, or long double), and returns its absolute value as a floating-point number of the same type. Syntax Following is the syntax to fabs() function: double fabs(double x); The fabs() function is mostly used when we need to find the non-negative magnitude of a number. Using fabs() with positive and negative values Here, we will demonstrate how fabs() handles ... Read More

C++ Program to Find Sum of Leaf Nodes in Binary Tree

AYUSH MISHRA
Updated on 28-Apr-2025 21:36:08

5K+ Views

We can find the sum of leaf nodes in a binary tree using an iterative and a recursive approach. This problem has many real-life applications, such as analyzing hierarchies, calculating final results in decision trees, or summing final nodes in various kinds of trees used in computer algorithms. In this article, we are going to learn how can find the sum of all leaf nodes in a binary tree in the C++ language. What are Leaf Nodes in a Binary Tree? The leaf nodes of a binary tree are the nodes that do not have any children. Both the ... Read More

Remove Array Items Recursively in Lodash

AYUSH MISHRA
Updated on 28-Apr-2025 20:25:06

6K+ Views

In this problem, we have to remove certain items from arrays; this removal of array items needs to be done in a recursive manner for nested arrays. Lodash is a popular JavaScript library that helps us achieve this easily. What is lodash? Lodash is a popular JavaScript library used to deal with arrays, objects, and other data structures with ease. It has many helper functions such as map, filter, and invoke as well as function binding, JavaScript templating, deep equality checks, creating indexes, and much more. Problem Description We are given an array, and we have to remove an item ... Read More

Overloading Unary Plus Operator in C++

Akansha Kumari
Updated on 28-Apr-2025 19:03:29

411 Views

The unary operators are operators that operate only on a single operand. There are mainly thirteen unary operators in C++, for example, ++, !, ~, typeof, delete, etc.Overloading a unary operator means setting a customized behaviour for the unary operators for the objects of a class. which means you can define how an operator will work when applied to instances of a class instead of using its default behavior. This operator is generally used on the left side of the object, as in +obj, !obj, -obj, and ++obj, but can also be used as a postfix like obj++ or obj--. So, ... Read More

Early Binding and Late Binding in C++

George John
Updated on 28-Apr-2025 18:35:32

17K+ Views

In C++, binding is the process of connecting names such as variables and functions to their actual memory locations. When you intend to call a function, the program must identify the proper function definition for the execution. So, binding is the process of making this connection. This happens either at compile time (early binding) or at runtime (late binding). Early Binding This is compile time polymorphism and decides which function to call before it runs, making execution faster and direct. Example In this example, we demonstrate the early binding, where the base class function runs instead of the derived class ... Read More

Print System Time in C++

Ankith Reddy
Updated on 28-Apr-2025 18:34:23

773 Views

In C++, the standard library does not provide any proper built-in date type. Instead of that we use structures and functions to inherit from the C language for the manipulation. To access this date and time related functions and structures, we need to include header file to the C++ program. There are four time-related types: clock_t, time_t, size_t, and tm. The types: clock_t, size_t and time_t are capable of representing the system time and date as some sort of integer. time_t: It is used to store calendar time (like the number of seconds since ... Read More

How malloc and free Work in C/C++

Chandu yadav
Updated on 28-Apr-2025 18:21:44

2K+ Views

Both malloc() and free() are used to manage memory at runtime. The malloc() is very useful because it allocates memory based on the program needs, while free() releases the memory. But the free() can lead to memory leakage, which is one of its disadvantages. What is malloc()? The function malloc() is used to allocate the requested size of bytes and it returns a pointer to the first byte of allocated memory. It returns null pointer, if it fails. Syntax Following is the basic syntax of malloc(): pointer_name = (cast-type*) malloc(size); Here, pointer_name : Any ... Read More

Check for Balanced Parenthesis Using Stacks in C++

Jennifer Nicholas
Updated on 28-Apr-2025 18:19:10

10K+ Views

In this article, we will learn how to check for a balanced parentheses using stack data structure in C++ program. First of all let's understand what is balanced parentheses. A string of parentheses is said to be balanced parentheses, If Every opening bracket has corresponding closing bracket of same type. The brackets are closed in correct order. For an example, we can say that the expression [{} () {()}] it is balanced. But {[}] is not balanced eventhough it contains opening and closing brackets of same type. ... Read More

Advertisements