1) Simple Encoded Array: Maya has stored few confidential numbers in an array (array of int). To ensure that others do not find the numbers easily, she has applied a simple encoding.
Encoding used: Each array element has been substituted with a value that is the sum of its original value and its succeeding element’s value.
i.e. arr[i] = arr[i] + arr[i+1]
e.g. value in arr[0] = original value of arr[0] + original value of arr[1]
Also note that value of last element i.e. arr[last index] remains unchanged.
Example:
If the original array is –
{2, 5, 1, 7, 9, 3}
The encoded array would be –
{7, 6, 8, 16, 12, 3}
Provided the encoded array, you are expected to find the –
a) First number (value in index 0) in the original array
b) Sum of all numbers in the original array
The prototype of the function is:
public static void findOriginalFirstAndSum(int[] input1);
where input1 is the encoded array.
The method is expected to –
- find the value of the first number of the original array and store it in the member output1 and
- find the sum of all numbers in the original array and store it in the member output2
Assumption(s):
- The array elements can be positive and/or negative numbers
Example 1:
Original array = {2, 5, 1, 7, 9, 3}
Encoded array = {7, 6, 8, 16, 12, 3}
First number in original array = 2
Sum of all numbers in original array = 27
NOTE: Only the “Encoded array” will be supplied to the function and it is expected to do the processing to find the expected result values.
Top comments (5)
import java.util.Scanner;
class Main {
}
class Employee {
private Long empId;
private String empEmail;
private String empPassword;
private Long empPhoneNo;
private Integer age;
}
class Validator {
}
String[] parts = empEmail.split("@");
if (parts.length != 2) {
return false;
}
String part1 = parts[0];
String part2 = parts[1];
class Validator {
// ID Validation
public static Boolean isValidEmployeeId(Long empId) {
String empIdString = String.valueOf(empId);
return empIdString.matches("[0-9]{6,7}");
}
// PhoneNo Validation
public static Boolean isValidEmployeePhoneNo(Long empPhoneNo) {
String phoneNoString = String.valueOf(empPhoneNo);
return phoneNoString.matches("[1-9]{1}[0-9]{9}") && !phoneNoString.matches("(.)\1+");
}
}
String empIdString = String.valueOf(empId);
return empIdString.matches("[0-9]{6,7}");