Advertisement
3vo

Problem 6. Is Array Symmetric

3vo
Nov 8th, 2022
1,124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*Problem 6. Is Array Symmetric
  2.  
  3. Write a program that finds if an array of numbers is symmetric - that is, the first number is equal to the last, the second - to the second last and so on
  4.  
  5.  
  6. Examples:
  7.  
  8.  
  9. Input                           Output
  10. 1 2 3 4 4 3 2 1     Yes
  11. Input                           Output
  12. 7 8 9 8 7   Yes
  13. Input                           Output
  14. 3 4 5 3 4 5     No
  15. */
  16.  
  17.  
  18.  
  19. input = [
  20.   '1 2 3 4 4 3 2 1'
  21.   // '7 8 9 8 7'
  22.   // '3 4 5 3 4 5'
  23. ];
  24.  
  25. let print = this.print || console.log;
  26. let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  27.  
  28. const array = gets().split(' ');
  29. let result = 'Yes'
  30.  
  31. for (let i = 0; i <= (array.length - 1) / 2; i++) {
  32.   if (array[i] !== array[array.length - 1 - i]) {
  33.     result = 'No';
  34.     break;
  35.   }
  36. }
  37.  
  38. console.log(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement