Hackerrank - Sequence Equation
Given a sequence of integers, where each element is distinct and satisfies . For each where , that is increments from to , find any integer such that and keep a history of the values of in a return array.
Example
Each value of between and , the length of the sequence, is analyzed as follows:
- , so
 - , so
 - , so
 - , so
 - , so
 
The values for are .
Function Description
Complete the permutationEquation function in the editor below.
permutationEquation has the following parameter(s):
- int p[n]: an array of integers
 
Returns
- int[n]: the values of for all in the arithmetic sequence to
 
Input Format
The first line contains an integer , the number of elements in the sequence.
The second line contains  space-separated integers  where .
Constraints
- , where .
 - Each element in the sequence is distinct.
 
Sample Input 0
3
2 3 1
Sample Output 0
2
3
1
Explanation 0
Given the values of , , and , we calculate and print the following values for each from to :
- , so we print the value of on a new line.
 - , so we print the value of on a new line.
 - , so we print the value of on a new line.
 
Sample Input 1
5
4 3 5 1 2
Sample Output 1
1
3
5
4
2import java.io.*;import java.math.*;import java.security.*;import java.text.*;import java.util.*;import java.util.concurrent.*;import java.util.function.*;import java.util.regex.*;import java.util.stream.*;import static java.util.stream.Collectors.joining;import static java.util.stream.Collectors.toList;
class Result {
    public static List<Integer> permutationEquation(List<Integer> p) {     HashMap<Integer, Integer> map = new HashMap<>();          for (int i = 0; i < p.size(); i++) {        map.put(p.get(i),i+1) ;     }          List<Integer> result = new ArrayList<>();          for (Integer num : map.values()) {         result.add(map.get(num));     }          System.out.println(result);          return result;    }
}
public class Solution {    public static void main(String[] args) throws IOException {        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
        int n = Integer.parseInt(bufferedReader.readLine().trim());
        List<Integer> p = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))            .map(Integer::parseInt)            .collect(toList());
        List<Integer> result = Result.permutationEquation(p);
        bufferedWriter.write(            result.stream()                .map(Object::toString)                .collect(joining("\n"))            + "\n"        );
        bufferedReader.close();        bufferedWriter.close();    }}
Comments
Post a Comment