Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //{ Driver Code Starts
- //Initial Template for C++
- #include <bits/stdc++.h>
- using namespace std;
- // } Driver Code Ends
- //User function Template for C++
- /* Function to calculate the longest consecutive ones
- * N: given input to calculate the longest consecutive ones
- */
- class Solution
- {
- public:
- int maxConsecutiveOnes(int N)
- {
- int ans = 0;
- int c = 0;
- while (N != 0)
- {
- if (N & 1)
- {
- c++;
- ans = max(ans, c);
- }
- else
- {
- c = 0;
- }
- N = N >> 1;
- }
- ans = max(ans, c);
- return ans;
- }
- };
- //{ Driver Code Starts.
- // Driver Code
- int main() {
- int t;
- cin>>t;//testcases
- while(t--)
- {
- int n;
- cin>>n;//input n
- Solution obj;
- //calling maxConsecutiveOnes() function
- cout<<obj.maxConsecutiveOnes(n)<<endl;
- }
- return 0;
- }
- // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment