Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Java Articles
Page 4 of 450
Difference between the largest and the smallest primes in an array in Java
Given an array of integers (all elements less than 1, 000, 000), find the difference between the largest and smallest prime numbers in the array. Worked Example Array: [1, 2, 3, 4, 5] Primes in array: 2, 3, 5 Largest Prime = 5 Smallest Prime = 2 Difference = 5 - 2 = 3 Solution Approach Use the Sieve of Eratosthenes to pre-compute all prime numbers up to 1, 000, 000. Then scan the array to find the largest and smallest primes and return their difference. The sieve runs in ...
Read MoreDifference between sums of odd level and even level nodes of a Binary Tree in Java
Given a binary tree, find the difference between the sum of nodes at odd levels and the sum of nodes at even levels. The root is at level 1 (odd), its children are at level 2 (even), and so on. Problem Statement The tree structure for our example is − Level 1 (odd) Level 2 (even) Level 3 (odd) Level 4 (even) ...
Read MoreDifference between sums of odd and even digits.
Given a number n, check whether the difference between the sum of digits at odd positions and the sum of digits at even positions is zero. Positions are indexed starting from 0 (rightmost digit). Key Insight A number is divisible by 11 if and only if the alternating sum of its digits (difference between sum of digits at odd and even positions) is 0 or divisible by 11. So a simple n % 11 == 0 check can determine if the difference is zero. Worked Example For n = 1212112 − Digits: ...
Read MoreDifference between sum of the squares of and square of sum first n natural numbers.
Given a number n, find the difference between the square of the sum and the sum of the squares of the first n natural numbers. This is a classic mathematical problem that can be solved efficiently using direct formulas. Formulas The two formulas used are − Sum of squares of first n natural numbers: n(n+1)(2n+1) / 6 Sum of first n natural numbers: n(n+1) / 2, then square it The difference is: (Square of Sum) − (Sum of Squares). Worked Example For n = 3 − Sum of squares = ...
Read MoreDifference between ArrayBlockingQueue and ArrayDeque
ArrayBlockingQueue and ArrayDeque are both array-based collection classes in Java, but they serve different purposes. ArrayBlockingQueue is a thread-safe, bounded FIFO queue designed for producer-consumer scenarios, while ArrayDeque is a fast, resizable double-ended queue for single-threaded use. ArrayBlockingQueue ArrayBlockingQueue implements the BlockingQueue interface. It stores elements in FIFO (First-In-First-Out) order − insertion always happens at the tail and removal at the head. It is thread-safe and bounded: once created with a fixed capacity, the size cannot change. If the queue is full, the inserting thread blocks until space becomes available. ArrayDeque ArrayDeque implements the Deque (double-ended ...
Read MoreDifference between OpenId and OAuth
OAuth and OpenID are both protocols used in web authentication and authorization, but they serve different purposes. OAuth is designed for authorization (granting access to resources without sharing passwords), while OpenID is designed for authentication (verifying who a user is). OAuth OAuth (Open Authorization) is an HTTP-based protocol that allows a third-party application to access a user's resources without the user sharing their password. Instead, OAuth provides an access token that the application uses to interact with APIs on behalf of the user. For example, when a mobile app asks to access your Google Drive files, it uses ...
Read MoreDifference between Java and C language
Both Java and C are among the most popular programming languages in the world. Java is an object-oriented, platform-independent language, while C is a procedural, platform-dependent language. Despite their differences, both have been widely influential in shaping modern software development. Key Differences Feature Java C Developed By James Gosling (1995) Dennis M. Ritchie (1969–1973) Paradigm Object-Oriented (high-level) Procedural (middle-level) Compilation Source → bytecode → JVM interprets/JIT compiles Source → machine code (compiled directly) Functional Unit Objects and classes Functions Inheritance Supported Not supported Threading ...
Read MoreDifference between Traditional Collections and Concurrent Collections in java
In Java, Collections are fundamental data structures that store and manipulate groups of objects efficiently. However, traditional collections like ArrayList and HashMap are not designed for multi-threaded environments. To overcome this, Java 5 introduced Concurrent Collections in the java.util.concurrent package, providing thread-safe alternatives with better performance. Traditional Collections Traditional collections (from java.util) include classes like ArrayList, LinkedList, HashMap, and HashSet. They are not synchronized by default. While synchronized wrappers and legacy classes like Vector and Stack exist, they lock the entire collection, causing performance bottlenecks in multi-threaded scenarios. Concurrent Collections Concurrent collections (from java.util.concurrent) were introduced ...
Read MoreDifferences between Difference between getc(), getchar(), getch() and getche() functions
All of these functions are used to read a character from input and each returns an integer value. They differ in where they read from, whether they use a buffer, and whether they echo the character to the screen. getc() getc() reads a single character from any input stream (file, stdin, etc.). It uses a buffer and waits for the Enter key. Returns EOF on failure. Syntax − int getc(FILE *stream); getchar() getchar() reads a single character from standard input only. It is equivalent to calling getc(stdin). It uses a buffer and ...
Read MoreDifferences between Interface and class in Java
In Java, both classes and interfaces are fundamental building blocks of object-oriented programming. A class provides a complete blueprint for objects, while an interface defines a contract of behaviors that implementing classes must follow. Class A class is a blueprint from which individual objects are created. A class can contain the following variable types − Local Variables − Defined inside methods, constructors, or blocks. They are created when the method is called and destroyed when it completes. Instance Variables − Declared within a class but outside any method. They are initialized when the class is instantiated ...
Read More