Advertisement
Guest User

Untitled

a guest
Nov 24th, 2021
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace BasicStackOperations
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int[] commands = Console.ReadLine()
  12.                 .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  13.                 .Select(int.Parse)
  14.                 .ToArray();
  15.  
  16.             int[] numbers = Console.ReadLine()
  17.                 .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  18.                 .Select(int.Parse)
  19.                 .ToArray();
  20.  
  21.             Stack<int> stackOfNumbers = new Stack<int>();
  22.  
  23.             for (int i = 0; i < commands[0]; i++)
  24.             {
  25.                 stackOfNumbers.Push(numbers[i]);
  26.             }
  27.  
  28.             for (int i = 0; i < commands[1]; i++)
  29.             {
  30.                 if (stackOfNumbers.Count > 0)
  31.                 {
  32.                     stackOfNumbers.Pop();
  33.                 }
  34.             }
  35.  
  36.             //If stack is not empty:
  37.             if (stackOfNumbers.Count != 0)
  38.             {
  39.                 int smallestNumberInStack = int.MaxValue;
  40.                 bool printSmallestNumber = true;
  41.  
  42.                 for (int i = 0; i < stackOfNumbers.Count; i++)
  43.                 {
  44.                     // If number is presented in the stack -> print "true".
  45.                     if (commands[2] == stackOfNumbers.Peek())
  46.                     {
  47.                         Console.WriteLine("true");
  48.                         printSmallestNumber = false;
  49.                         break;
  50.                     }
  51.                     // If number is not presented in the stack -> remember smallest number.
  52.                     else
  53.                     {
  54.                         if (stackOfNumbers.Peek() <= smallestNumberInStack)
  55.                         {
  56.                             smallestNumberInStack = stackOfNumbers.Peek();
  57.                         }
  58.                         stackOfNumbers.Pop();
  59.                         i--;
  60.                     }
  61.                 }
  62.  
  63.                 // Print smallest number.
  64.                 if (printSmallestNumber == true)
  65.                 {
  66.                     Console.WriteLine(smallestNumberInStack);
  67.                 }
  68.             }
  69.             // If stack is empty -> print 0
  70.             else
  71.             {
  72.                 Console.WriteLine(0);
  73.             }
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement