Filip13

index_of_last_zero

Sep 24th, 2024
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | Source Code | 0 0
  1. namespace index_of_last_zero
  2. {
  3.     internal class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             string input = Console.ReadLine();
  8.  
  9.             int indexOfLastZero = GetIndexOfLastZero(input);
  10.  
  11.             Console.WriteLine(indexOfLastZero);
  12.         }
  13.  
  14.         private static int GetIndexOfLastZero(string input)
  15.         {
  16.             int l = 0, r = input.Length - 1;
  17.             int retrunValue = -1;
  18.  
  19.             while (l <= r)
  20.             {
  21.                 int m = (l + r) / 2;
  22.  
  23.                 if (input[m] == '0' && m < input.Length - 1 && input[m + 1] == '1')
  24.                 {
  25.                     return m;
  26.                 }
  27.  
  28.                 if (input[m] == '0')
  29.                 {
  30.                     l = m + 1;
  31.                     retrunValue = input.Length - 1;
  32.                 }
  33.                 else
  34.                 {
  35.                     r = m - 1;
  36.                 }
  37.             }
  38.  
  39.             return retrunValue;
  40.         }
  41.  
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment