Java Program to Increment All Element of an Array by One
Last Updated :
11 Nov, 2020
Given the array, the task is to increment each element of the array by 1. Complete traversal is required for incrementing all the elements of an array. An optimized way to complete a given task is having time complexity as O(N).
Examples:
Input : arr1[] = {50, 25, 32, 12, 6, 10, 100, 150}
Output: arr1[] = {51, 25, 33, 13, 7, 11, 101, 151}
Input : arr2[] = {3, 6, 8, 12, 45}
Output: arr2[] = {4, 7, 9, 13, 46}
1. with pre-defined values:
First, Initializing the array arr[] with pre-defined values, and after that, the length of the array should be calculated. Then use a loop, to perform the operation that is to increment the values one by one with the help of for loop. After that, the resultant of the above operation will get stored into the array and then the output will be shown with the help of for loop one by one.
Java
// Java Program to Increment All
// Element of an Array by One
public class Increment {
public static void main(String[] args)
{
// assigning values in array
int[] arr = { 50, 25, 32, 12, 6, 10, 100, 150 };
// finding the length of the array
// and assigning it to the variable n
int n = arr.length;
System.out.println(
"******* BEFORE INCREMENT *******");
// printing the elements of array
// before performing operation
for (int i = 0; i < n; i++) {
System.out.print(arr[i]);
System.out.print(" ");
}
// incrementing the values of array
// by 1 with the help of loop
for (int i = 0; i < n; i++) {
arr[i] = arr[i] + 1;
}
System.out.println(" ");
// elements of the array after increment
System.out.println(
"******* AFTER INCREMENT *******");
// printing the elements of array after operation
for (int i = 0; i < n; i++) {
System.out.print(arr[i]);
System.out.print(" ");
}
}
}
Output******* BEFORE INCREMENT *******
50 25 32 12 6 10 100 150
******* AFTER INCREMENT *******
51 26 33 13 7 11 101 151
Time Complexity: O(N), Where N is the size of an array
2. with user-defined values:
Initializing the array arr[] with user-defined values i.e. the values of the array will be given by the user, and after that, the length of the array should be calculated. Then use a loop, to perform the operation that is to increment the values one by one with the help of for loop. After that, the resultant of the above operation will get stored into the array and then the output will be shown with the help of for loop one by one.
Java
// Java Program to Increment All
// Element of an Array by One
import java.util.Scanner;
public class Increment2 {
public static void main(String[] args)
{
System.out.println(
"Enter the length of the array: ");
// The object of scanner class is created
// in order to take the input from user
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
// Initializing the array of size that
// is given by the user
int[] arr = new int[n];
// Calculating the length of the array
int l = arr.length;
System.out.println(
"Enter the values you want to insert in array: ");
// Taking the values from user
for (int i = 0; i < l; i++) {
arr[i] = sc.nextInt();
}
System.out.println(
"******* BEFORE INCREMENT *******");
// The elements of array before increment
for (int i = 0; i < l; i++) {
System.out.print(arr[i]);
System.out.print(" ");
}
// Incrementing the values of the array by 1
for (int i = 0; i < l; i++) {
arr[i] = arr[i] + 1;
}
System.out.println(" ");
System.out.println(
"******* AFTER INCREMENT *******");
// Elements of array after increment
for (int i = 0; i < l; i++) {
System.out.print(arr[i]);
System.out.print(" ");
}
}
}
Output:
Time Complexity: O(N), where N is the size of an array
Similar Reads
Java Program to Print the Elements of an Array An array is a data structure that stores a collection of like-typed variables in contiguous memory allocation. Once created, the size of an array in Java cannot be changed. It's important to note that arrays in Java function differently than they do in C/C++As you see, the array of size 9 holds elem
6 min read
Java Program to Find Sum of Array Elements Given an array of integers. Write a Java Program to find the sum of the elements of the array. Examples: Input : arr[] = {1, 2, 3} Output : 6 1 + 2 + 3 = 6 Input : arr[] = {15, 12, 13, 10} Output : 50 15 + 12 + 13 + 10 = 50 An array is a data structure that contains a group of elements. Typically th
3 min read
Java Program to Convert an Array into a List In Java, arrays and lists are two commonly used data structures. While arrays have a fixed size and are simple to use, lists are dynamic and provide more flexibility. There are times when you may need to convert an array into a list, for instance, when you want to perform operations like adding or r
4 min read
Java Program to Print the kth Element in the Array We need to print the element at the kth position in the given array. So we start the program by taking input from the user about the size of an array and then all the elements of that array. Now by entering the position k at which you want to print the element from the array, the program will print
2 min read
Java Program to Replace an Element in a List List in Java provides a way to store the ordered collection. The list Interface provides various methods to add, delete, or replace items in the List. In this article, we will learn about how we could replace an element in the list in Java. Methods to Replace an Element in a ListThere are 3 ways to
4 min read
Java Program to Increment by 1 to all the Digits of a given Integer Given an integer, the task is to generate a Java Program to Increment by 1 All the Digits of a given Integer. Examples: Input: 12345 Output: 23456 Input: 110102 Output: 221213 Approach 1: In this approach, we will create a number which will be of the same length as the input and will contain only 1
2 min read
Java Program to Move all zeroes to end of array Given an array of random numbers, Push all the zero's of a given array to the end of the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. The order of all other elements should be same. Expected time complexity i
3 min read
Java Program to Count 1's in a sorted binary array Given a binary array sorted in non-increasing order, count the number of 1's in it. Examples: Input: arr[] = {1, 1, 0, 0, 0, 0, 0} Output: 2 Input: arr[] = {1, 1, 1, 1, 1, 1, 1} Output: 7 Input: arr[] = {0, 0, 0, 0, 0, 0, 0} Output: 0 A simple solution is to linearly traverse the array. The time c
3 min read
Java Program for k-th missing element in sorted array Given an increasing sequence a[], we need to find the K-th missing contiguous element in the increasing sequence which is not present in the sequence. If no k-th missing element is there output -1. Examples : Input : a[] = {2, 3, 5, 9, 10}; k = 1; Output : 1 Explanation: Missing Element in the incre
5 min read
Sort an Array and Insert an Element Inside Array in Java Sorting an array can be done by using inbuilt sort function while for the insertion we have to create a new array to do so as arrays in Java are immutable. To learn more about sorting in Java follow the article mentioned below: Sorting: Arrays.sort() in Java with examples Approach 1: Create a new ar
3 min read