Programming Articles - Page 1888 of 3366

Initialization of local variable in a conditional block in Java

AmitDiwan
Updated on 04-Jul-2020 08:14:59

386 Views

Java compiler doesn’t allow abandoning an uninitialized local variable. When a local variable is initialized inside a conditional block, there are 3 possibilities that could potentially occur −Code compiles successfully if values are provided in the conditional block and the given condition is true.Code gives a compilation error if variables are provided (instead of values) in the conditional block and the condition is true.Code gives compilation error if the condition that needs to be checked is false.If the local variable is initialized to a default value outside of the conditional block in the code, it won’t give any error and ... Read More

Importance of HashSet in Java

AmitDiwan
Updated on 23-Nov-2023 11:42:41

443 Views

The HashSet use Hashing to manipulate data. Let us see an example − Example import java.util.*; public class Demo{ private final String f_str, l_str; public Demo(String f_str, String l_str){ this.f_str = f_str; this.l_str = l_str; } public boolean equals(Object o){ if (o instanceof Demo) return true; Demo n = (Demo)o; return n.f_str.equals(f_str) && n.l_str.equals(l_str); } public static void main(String[] args){ ... Read More

Implementing Checksum Using Java

AmitDiwan
Updated on 04-Jul-2020 08:09:40

2K+ Views

Following is the code to implement Checksum using Java −Example Live Demoimport java.util.*; public class Demo{    public static void main(String args[]){       Scanner my_scan = new Scanner(System.in);       System.out.println("Enter the input string ");       String my_in = my_scan.next();       int my_checksum = generate_checksum(my_in);       System.out.println("The checksum that has been generated is " + Integer.toHexString(my_checksum));       System.out.println("Enter the data that needs to be sent to the receiver ");       my_in = my_scan.next();       System.out.println("Enter the checksum that needs to be sent to the receiver "); ... Read More

How to convert hex string to byte Array in Java?

sudhir sharma
Updated on 03-Jul-2020 08:12:11

6K+ Views

We can convert a hex string to byte array in Java by first converting the hexadecimal number to integer value using the parseInt() method of the Integer class in java. This will return an integer value which will be the decimal conversion of hexadecimal value. We will then use the toByteArray() method of BigInteger class that will return a byte array.Example Live Demoimport java.math.BigInteger; public class Demo {    public static void main(String args[]) {       String str = "1D08A";       int it = Integer.parseInt(str, 16);       System.out.println("Hexadecimal String " + str);       ... Read More

Maximum size rectangle binary sub-matrix with all 1s in C++

Ayush Gupta
Updated on 03-Jul-2020 08:06:57

213 Views

In this tutorial, we will be discussing a program to find maximum size rectangle binary sub-matrix with all 1s.For this we will be provided with 2D matrix containing zeroes and ones. Our task is to find the largest 2D matrix subset containing only ones.Example Live Demo#include using namespace std; #define R 4 #define C 4 //finding the maximum area int maxHist(int row[]) {    stack result;    int top_val;    int max_area = 0;    int area = 0;    int i = 0;    while (i < C) {       if (result.empty() || row[result.top()]

Maximum size of sub-array that satisfies the given condition in C++

Ayush Gupta
Updated on 03-Jul-2020 08:02:35

196 Views

In this tutorial, we will be discussing a program to find maximum size of sub-array that satisfies the given condition.For this we will be provided with an array of integers. Our task is to find the maximum length subset of that array satisfying either of arr[k] > arr[k + 1] when k is odd and arr[k] < arr[k + 1] when k is even, arr[k] > arr[k + 1] when k is even and arr[k] < arr[k + 1] when k is odd.Example Live Demo#include using namespace std; //comparing values of a and b int cmp(int a, int b) {   ... Read More

Maximum set bit sum in array without considering adjacent elements in C++

Ayush Gupta
Updated on 15-Sep-2020 14:11:22

203 Views

In this problem, we are given an array arr[] of integers. Our task is to create a program to calculate the Maximum set bit sum in array without considering adjacent elements in C++.Problem description − Here, we have an array arr[]. We have to find the number of set bits for each number. Then, we will find the maximum set bit sum in adjacent elements of the array. i.e. maximum sum for a[i] + a[i+2] ….Let’s take an example to understand the problem, Inputarr[] = {1, 4, 6, 7}Output4ExplanationArray with the element’s in binary formarr[] = {01, 100, 101, 111} ... Read More

Maximum score after flipping a Binary Matrix atmost K times in C++

Ayush Gupta
Updated on 15-Sep-2020 14:12:56

329 Views

In this problem, we are given a 2-D array arr[] consisting of boolean values (i.e 0’s and 1’s) and an integer K. Our task is to create a program to find the Maximum score after flipping a Binary Matrix atmost K times in C++.Problem Description − Here, for the 2-D array, and the k moves, we need to find the number that is created by the elements of the array. In each move, we will take a row or column and flip all the elements of the row or column. The choice will be done to keep in mind that ... Read More

Maximum rational number (or fraction) from an array in C++

Ayush Gupta
Updated on 15-Sep-2020 14:14:32

284 Views

In this problem, we are given a 2-D array that contains rational numbers (one in each row). Our task is to create a program to calculate the maximum rational number (or fraction) from an array in C++.Problem Description − The 2-D array is of the form [n][2]. Each row has two integer values that denote the value of a and b in the equation of rational number, a/b. We need to find the greatest number out of all these rational numbers.Let’s take an example to understand the problem, Inputrat[][] = {    {3, 2},    {5, 7},    {1, 9}, ... Read More

Maximum profit after buying and selling the stocks in C++

Ayush Gupta
Updated on 15-Sep-2020 14:15:16

852 Views

In this problem, we are given an array stkprice[] that denotes the price of a certain stock on i-th day. Our task is to create a program to calculate Maximum profit after buying and selling the stocks in C++.Problem Description − Here, we need to check when can be bought and sell the stock to gain profit. To gain profit, we need to buy the stock at a low price and sell it when the price goes up. And repeat the same when the drop is encountered again.Let’s take an example to understand the problem, Inputstkprice[] = {120, 310, 405, ... Read More

Advertisements