Integer Overflow in Java

Integer Overflow in Java

Today I am going to discuss about two such scenarios which I have faced during problem solving.

While coding we may face some *integer overflow *cases for large inputs. Today I am going to discuss about two such scenarios which I have faced during problem solving.

Say you are given a sorted integer array and you are asked to find a particular element in that array. Since the array is sorted we can apply binary search algorithm to find the target element in O(logN) time; here N is the size of the input array.

The basic idea of binary search is each time we divide the array in half, find the mid index and compare the target element with the middle element. If the target element is greater than the middle element in that case we need to search only the right part of the array starting from the next index of the mid index. If the target element is less than the middle element then we need to search the left part of the array which ends before the mid index.

So the question is how we are going to find the mid index. It’s very simple; we need to maintain two pointers: start _and _end; start _pointer is initialized with 0 and _end pointer is initialized with N-1(N is the size of the array). So the formula we are going to use to find the mid index is:

mid = (start + end)/2.

At first glance this formula might seem innocent, but if we closely look at this formula we can see at first part it adds two integer numbers. Since we are adding two integer numbers there is a possibility of integer overflow if these two numbers are very large. So we should avoid this formula. Instead we can use the below formula to calculate the mid index:

mid = start + (end-start)/2;

Clearly “(end-start)/2” is less than “end” and this formula will not cause integer overflow for large values of start and end.

problem-solving sorting integer-overflow java binary-search

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Java Binary Search Program | Binary Search In Java Example

In the binary search, array elements must be in ascending order. If you have an unsorted array, you can sort the array using Arrays.sort(arr) method.

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Java: How to Delete a Node in Binary Search Tree

**Problem Statement:** Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference of the BST.

Searching Algorithms in Java

Searching Algorithms in Java. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Java Integer Cache: Why Integer.valueOf(127) == Integer.valueOf(127) Is True

The short answer to this question is, direct assignment of an int literal to an Integer reference is an example of auto-boxing concept where the literal value to object conversion code is handled by the compiler, so during compilation phase compiler converts Integer a = 127; to Integer a = Integer.valueOf(127);