• + 0 comments

    My C# solution

    public static void Main(string[] args) { int n = Convert.ToInt32(Console.ReadLine().Trim());

        string binary = Convert.ToString(n ,2);
    
        char[] c = binary.ToCharArray();
    
        int iCount = 1;   
        int left = 0;
        int right = 1;
        // This list will be used to store each count.
        // After each iteration, we add the count to the list.
        // After the loop has finished, we sort the elements in the 
        //list,
        // and finally, we print the last element of the list.
       List<int> lstCounts = new List<int>();
    
        for(int i = 0 ; i < c.Length ; i++)
        {
             if(right <= c.Length -1 )
            {
                 if(c[left] == c[right] && c[right] == '1')
                { 
                    iCount++;
                    lstCounts.Add(iCount);
                }
                 else 
                 { 
                    lstCounts.Add(iCount);
                    iCount = 1;
                 }
            }
    
            left++;
            right++;          
        }
    
    // sort elements from the list  
     lstCounts.Sort();
     //print the highest count from the list
     Console.Write(lstCounts.LastOrDefault()); 
    
    
    }