ICD04 - Editorial

#PROBLEM LINK:

[problem][1]

Author: Yasha Jadwani

Tester : Prabhsimran Kaur

Editorialist: Prabhsimran Kaur

DIFFICULTY :
Simple

PREREQUISITES :
Math

PROBLEM:
Given is an array of integers (A1,A2….An) for which each element is to be listed from 1 to the Aith in binary form.
So, you need to find the binary form from 1 to Aith element if each ith element.

EXPLANATION:
Given the above cases, we can find the binary form by simple code and run it from 1 to the Aith number of each Aith number.

For better understanding, consider an example where the array is:
2 3 4
Now, we will first print binary form for 1st number, from 1 to 2
1 10
Then, for 2nd number, from 1 to 3
1 10 11
For 3rd number, 1 to 4
1 10 11 100

SETTER’S SOLUTION
Let ‘p’ be the ith number .The for loop runs from 1 to p converting every number from 1 to p in binary form.

 #include<iostream>
    using namespace std;
     
    int main()
    {
    long t,i,j,k,p,n;
    cin>>t;
    for(i=0;i<t;i++)
     {
     cin>>n;
     for(j=0;j<n;j++)
         {
         cin>>p;
         for(k=1;k<=p;k++)
            {long rem,c=1,sum=0,x=k;
               do
               {
               rem=x%2;
               sum=sum + (c*rem);
               x=x/2;
               c=c*10;
               }while(x>0);
               cout<<sum<<" ";
             } 
         }
       cout<<endl;
     }          
        return 0;
    }

Example:
p=4
k=1 sum=1
k=2 sum=10
k=3 sum=11
k=4 sum=100
Output:
1 10 11 100