Programming Articles

Page 1303 of 2547

Max Heap in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 3K+ Views

Max heap is a complete binary tree, wherein the value of a root node at every step is greater than or equal to value at the child node.Below is an implementation of Max Heap using library functions.Exampleimport java.util.*; public class Demo{    public static void main(String args[]){       PriorityQueue my_p_queue = new PriorityQueue(Collections.reverseOrder());       my_p_queue.add(43);       my_p_queue.add(56);       my_p_queue.add(99);       System.out.println("The elements in the priority queue are : ");       Iterator my_iter = my_p_queue.iterator();       while (my_iter.hasNext())       System.out.println(my_iter.next());       my_p_queue.poll();   ...

Read More

Check if a key is present in every segment of size k in an array in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 510 Views

ConceptWith respect of a given array arr1[] with size of array N, one another key X and a segment size K, the task is to determine that the key X present in every segment of size K in arr1[].Input arr1[] = { 4, 6, 3, 5, 10, 4, 2, 8, 4, 12, 13, 4} X = 4 K = 3Output YesThere are existence of 4 non-overlapping segments of size K in the array, {4, 6, 3}, {5, 10, 4}, {2, 8, 4} and {12, 13, 4}. 4 is present all segments.Input arr1[] = { 22, 24, 57, 66, 35, 55, 77, 33, 24, ...

Read More

Check if a king can move a valid move or not when N nights are there in a modified chessboard in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 476 Views

ConceptWith respect of given infinite chessboard with the same rules as that of chess and given N knights coordinates on the infinite chessboard (-10^9 {3, 4}Output NoThe king can be able to make valid moves.MethodHere, the knight’s move is unusual among chess pieces. Its movement is towards a square that is two squares away horizontally and one square vertically, or two squares vertically and one square horizontally. So, the complete move looks like the letter “L” in every shape possible (8 possible moves). As a result of this, apply a hash map of pairs to mark all possible coordinates where ...

Read More

Thread Interference Error in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 264 Views

Let us see an example to understand the concept of Thread Interference error −Exampleimport java.io.*; class Demo_instance{    static int val_1 = 6;    void increment_val(){       for(int j=1;j

Read More

Check if a line at 45 degree can divide the plane into two equal weight parts in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 198 Views

Suppose we have n different points (Xi, Yi) in 2D coordinate and each point has a weight Wi, we have to check whether a line at 45 degree can be drawn. So that the sum of weights of points on each side will be same.So, if the input is like[[-1, 1, 3], [-2, 1, 1], [1, -1, 4]], then the output will be True/To solve this, we will follow these steps −n := size of vDefine one map weight_at_xmax_x := -2000, min_x := 2000for initialize i := 0, when i < n, update (increase i by 1), do −temp_x := ...

Read More

Merge two sets in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 3K+ Views

To merge two sets in Java, the code is as follows −Exampleimport java.util.stream.*; import java.util.*; import java.io.*; public class Demo{    public static Set set_merge(Set set_1, Set set_2){       Set my_set = set_1.stream().collect(Collectors.toSet());       my_set.addAll(set_2);       return my_set;    }    public static void main(String[] args){       Set my_set_1 = new HashSet();       my_set_1.addAll(Arrays.asList(new Integer[] { 34, 67, 89, 102 }));       Set my_set_2 = new HashSet();       my_set_2.addAll(Arrays.asList(new Integer[] { 77, 11, 0 , -33}));       System.out.println("The first set contains " + ...

Read More

Memory Consistency Error in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 658 Views

When the concept of multithreading is implemented, it is possible that changes made by one thread wouldn’t be visible to the other thread. This indicates that the view of each thread is inconsistent with respect to each other. This is known as memory consistency error.CPU might initiate main memory access in a different order, whereas the threads might access them in a different order.This is usually true when write operation is being performed, thereby avoiding the CPU wait time.The write operation is an atomic one, meaning no other operation would be performed by other threads when a write operation is ...

Read More

Check if a number is a Trojan Numbers in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 201 Views

ConceptWith respect of given number n, the task is to verify whether n is a Trojan Number or not. Trojan Number is defined as a number that is a strong number without a perfect power. We can say that a number n is treated as a strong number if, for every prime divisor or factor p of n, p^2 is also a divisor. We can say in another way, every prime factor appears at least twice. We should remember that all Trojan numbers are strong. But it is not true for vice-versa that means, not all strong numbers are Trojan ...

Read More

Run-time Stack mechanism in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

Eeverytime a process or a code or a thread needs to run in Java, a runtime stack is created so as to store the operations performed while executing the thread.Every entry in the run-time stack is known as stack frame or activation record. Once a function has been called by the process, its associated data is deleted from the runtime stack.Once all the functions have been called, the runtime stack will be empty. This means it needs to be removed from the memory.At this point in time, the runtime stack is destroyed and then the thread is also terminated.A termination ...

Read More

Can we call run() method directly instead of start() in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 483 Views

Yes, we can do that. Let us see an example −Exampleclass my_thread extends Thread{    public void run(){       try{          System.out.println ("The thread " + Thread.currentThread().getId() + " is currently running");       }       catch (Exception e){          System.out.println ("The exception has been caught");       }    } } public class Main{    public static void main(String[] args){       int n = 6;       for (int i=1; i

Read More
Showing 13021–13030 of 25,466 articles
Advertisements