-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplusOne.java
More file actions
37 lines (27 loc) · 882 Bytes
/
plusOne.java
File metadata and controls
37 lines (27 loc) · 882 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// You are given an array you have to convert that array into number add one to it and return the number in the array form
import java.util.*;
public class plusOne {
static int[] po(int[] arr) {
int n = arr.length;
long ans = 0;
for (int i = 0; i < n; i++) {
ans = ans * 10 + arr[i];
}
long hope = ans + 1;
String s = Long.toString(hope);
int length = s.length();
int arr2[] = new int[length];
int k = length - 1;
while (hope > 0) {
arr2[k] = (int)(hope % 10);
hope = hope / 10;
k--;
}
return arr2;
}
public static void main(String[] args) {
int arr[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
int[] n = po(arr);
System.out.println(Arrays.toString(n));
}
}