-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindOriginalArrayFromEncoded.java
More file actions
44 lines (32 loc) · 1.09 KB
/
FindOriginalArrayFromEncoded.java
File metadata and controls
44 lines (32 loc) · 1.09 KB
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
38
39
40
41
42
43
44
import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int input2 = sc.nextInt();
int input1[] = new int[input2];
for(int i=0;i<input2;i++){
input1[i]=sc.nextInt();
}
int original[] = new int[input2];
original[input2-1] = input1[input2-1];
// creating original array.
for(int i=input2-2;i>=0;i--){
int x = input1[i]-original[i+1];
original[i] = x;
}
// finding sum of all elements in original array.
int sum=0;
for(int i=0;i<input2;i++){
sum+=original[i];
}
// first element in original array.
int FirstElementInOriginal = original[0];
// printing two ouputs here..
System.out.println(FirstElementInOriginal+" "+sum);
}
}
Testcase:
input1: 7 6 8 16 12 3
input2: 6
output1: 2 (first index value from original array).
output2: 27 (Sum of all elements in original array).