fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Scanner scanner = new Scanner(System.in);
  13. int n = scanner.nextInt();
  14. int[] arr = new int[n];
  15.  
  16. for (int i = 0; i < n; i++) {
  17. arr[i] = scanner.nextInt();
  18. }
  19.  
  20. int m = scanner.nextInt();
  21.  
  22. int[] elements = new int[m];
  23. int[] positions = new int[m];
  24. for (int i = 0; i < m; i++) {
  25. elements[i] = scanner.nextInt();
  26. }
  27.  
  28. for (int i = 0; i < m; i++) {
  29. positions[i] = scanner.nextInt();
  30. }
  31.  
  32. for (int i = 0; i < m; i++) {
  33. int pos = positions[i] - 1;
  34. if (pos >= 0 && pos < n) {
  35. arr[pos] = elements[i];
  36. } else {
  37. System.out.println("Invalid position: " + positions[i]);
  38. }
  39. }
  40.  
  41. System.out.println("Updated Array: " + Arrays.toString(arr));
  42.  
  43. scanner.close();
  44. }
  45. }
Success #stdin #stdout 0.18s 60868KB
stdin
5
0 1 2 3 4
2 
5 6 
2 4
stdout
Updated Array: [0, 5, 2, 6, 4]