elena1234

Array Elements Equal to Their Index

Oct 1st, 2020 (edited)
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ArrayElementsEqualtoTheirIndex
  6. {
  7.     class MainClass
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             int[] numbers = Console.ReadLine().Split().Select(int.Parse).ToArray();
  12.             List<int> result = new List<int>();
  13.  
  14.             for (int i = 0; i < numbers.Length; i++)
  15.             {
  16.                 if (numbers[i] == i)
  17.                 {
  18.                     result.Add(numbers[i]);
  19.                 }
  20.             }
  21.  
  22.             Console.WriteLine(string.Join(" ",result));
  23.         }
  24.     }
  25. }
  26.  
Add Comment
Please, Sign In to add comment