Queries C++

QUERIES

Source code:

#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);

/*
 * Complete the 'viralAdvertising' function below.
 *
 * The function is expected to return an INTEGER.
 * The function accepts INTEGER n as parameter.
 */

int viralAdvertising(int n) {
    int T_people=5;
    int like=T_people/2;
    int sum=like;
    
    for(int i=2;i<=n;i++)
    {
                   T_people=3*like;
                   like=T_people/2;
                   sum+=like;   
                   cout<<"->"<<sum<<endl;
    }
    
    return sum;
}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    string n_temp;
    getline(cin, n_temp);

    int n = stoi(ltrim(rtrim(n_temp)));

    int result = viralAdvertising(n);

    fout << result << "\n";

    fout.close();

    return 0;
}

string ltrim(const string &str) {
    string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
    );

    return s;
}

string rtrim(const string &str) {
    string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
        s.end()
    );

    return s;
}


Question 15:

//Question:-FIND that CONSECUTIVE intgers of array is 1, 2, and 3

#include <iostream>

#include<string>

using namespace std;

int main() {

    int n,c[10];

    cout<<"Enter the size of array: \n";

    cin>>n;

    for(int i=0;i<n;i++)

    {

        cout<<"Enter the element at index "<<i+1<<" : ";

        cin>>c[i];

        cout<<endl;

    }

   for(int i=0;i<n;i++)

    {

        for(int j=i+1;j<n;j++)

        {

            for(int k=j+1;k<n;k++)

            {

                if(c[i]==1 && c[j]==2 && c[k]==3)

                {

                    cout<<"TRUE";

                }

            }

        }

    }

    return 0;

}




Question 14:

//Question:-Conversion from decimal to binary

#include <iostream>

using namespace std;

void decToBinary(int n)

{

        int binaryNum[32];

        int i = 0;

        while (n > 0) {

binaryNum[i] = n % 2;

n = n / 2;

i++;

}

for (int j = i - 1; j >= 0; j--)

cout << binaryNum[j];

}

int main()

{

    int n;

    cout<<"Enter a number :";

    cin>>n;

decToBinary(n);

return 0;

}





Question 14:

//Question:-finding integer is zero, negative or positive

#include <iostream>

#include<string>

using namespace std;

void test(int n)

{

    if(n==0)

    {

        cout<<"The number "<<n<<" is Zero";

    }

    else if(n>0)

    {

        cout<<"The number "<<n<<" is Positive";

    }

    else

        cout<<"The number "<<n<<" is Negative";

}

int main() {

    int n;

    cout<<"Enter the number : \n";

    cin>>n;

    test(n);

    return 0;

}




Question 13:

//Question:-Finding number of even integers in array.

#include <iostream>

#include<string>

using namespace std;


int main() {

    int n,c[10];

    cout<<"Enter the size of array: \n";

    cin>>n;

    for(int i=0;i<n;i++)

    {

        cout<<"Enter the element at index "<<i+1<<" : ";

        cin>>c[i];

        cout<<endl;

    }

    int count=0;

    for(int i=0;i<n;i++)

    {

        if(c[i]%2==0)

        {

            count++;

        }

    }

    cout<<"Number of even integers in array : "<<count;

    return 0;

}




Question 12:From Hacker rank


#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);

/*
 * Complete the 'utopianTree' function below.
 *
 * The function is expected to return an INTEGER.
 * The function accepts INTEGER n as parameter.
 */

int utopianTree(int n) {
    
    int height=1;
    for(int i=1;i<=n;i++)
    {
            if(i%2!=0)
            {
                height*=2;
            }
            else {
            height+=1;
            }
            
    }
    return height;
}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    string t_temp;
    getline(cin, t_temp);

    int t = stoi(ltrim(rtrim(t_temp)));

    for (int t_itr = 0; t_itr < t; t_itr++) {
        string n_temp;
        getline(cin, n_temp);

        int n = stoi(ltrim(rtrim(n_temp)));

        int result = utopianTree(n);

        fout << result << "\n";
    }

    fout.close();

    return 0;
}

string ltrim(const string &str) {
    string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
    );

    return s;
}

string rtrim(const string &str) {
    string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
        s.end()
    );

    return s;
}



Question 11:From hacker rank 

#include <bits/stdc++.h>


using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);

/*
 * Complete the 'angryProfessor' function below.
 *
 * The function is expected to return a STRING.
 * The function accepts following parameters:
 *  1. INTEGER k
 *  2. INTEGER_ARRAY a
 */

string angryProfessor(int k, vector<int> a) {
    
    int size_a=a.size();
    int count=0;
    for(int i=0;i<size_a;i++)
    {
        if(a[i]<=0)
        {
            count++;
        }
    }
    string res;
    if(count>=k)
    {
        res="NO";
    }
    else {
    res="YES";
    }
    return res;
}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    string t_temp;
    getline(cin, t_temp);

    int t = stoi(ltrim(rtrim(t_temp)));

    for (int t_itr = 0; t_itr < t; t_itr++) {
        string first_multiple_input_temp;
        getline(cin, first_multiple_input_temp);

        vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp));

        int n = stoi(first_multiple_input[0]);

        int k = stoi(first_multiple_input[1]);

        string a_temp_temp;
        getline(cin, a_temp_temp);

        vector<string> a_temp = split(rtrim(a_temp_temp));

        vector<int> a(n);

        for (int i = 0; i < n; i++) {
            int a_item = stoi(a_temp[i]);

            a[i] = a_item;
        }

        string result = angryProfessor(k, a);

        fout << result << "\n";
    }

    fout.close();

    return 0;
}

string ltrim(const string &str) {
    string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
    );

    return s;
}

string rtrim(const string &str) {
    string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
        s.end()
    );

    return s;
}

vector<string> split(const string &str) {
    vector<string> tokens;

    string::size_type start = 0;
    string::size_type end = 0;

    while ((end = str.find(" ", start)) != string::npos) {
        tokens.push_back(str.substr(start, end - start));

        start = end + 1;
    }

    tokens.push_back(str.substr(start));

    return tokens;
}



Question 10:

//Question:-Largest Sum of Contiguous Subarray.

#include <iostream>

#include<string>

using namespace std;

int main() {

    int n,ar[10];

    cout<<"Enter the size of array: \n";

    cin>>n;

    for(int i=0;i<n;i++)

    {

        cout<<"Enter the element at index "<<i+1<<" : ";

        cin>>ar[i];

        cout<<endl;

    }

    for(int i=0;i<n;i++)

    {

        ar[i]=ar[i]+ar[i+1];

    }

    ar[n-1]=0;

    int hold=ar[0];

    for(int i=0;i<n;i++)

    {

        if(hold<ar[i])

        {

            hold=ar[i];

        }

    }

    cout<<"The largest sum of Contiguous subarray : "<<hold;


    return 0;

}





Question 9:

//Question:-check for Larger or equal to the number 10 in c++.

#include <iostream>

#include<string>

using namespace std;

int main() {

    int n,ar[10];

    cout<<"Enter the size of array: \n";

    cin>>n;

    for(int i=0;i<n;i++)

    {

        cout<<"Enter the element at index "<<i+1<<" : ";

        cin>>ar[i];

        cout<<endl;

    }

    for(int i=0;i<n;i++)

    {

        if(ar[i]>=10)

        {

            cout<<"Number is : "<<ar[i]<<endl;

        }

    }

    return 0;

}




Question 8:

//Question:-check for lARGEST OF THREE NUMBERS c++

#include <iostream>

#include<string>

using namespace std;

int main() {

    int a,b,c;

    cout<<"Enter three number to find the largest among them: \n";

    cin>>a>>b>>c;

    if(a>b &&a>c)

    {

        cout<<"Largest is : "<<a;

    }

    else if(b>a && b>c)

    {

        cout<<"Largest is : "<<b;

    }

    else

        cout<<"Largest is : "<<c;

    return 0;

}



Question 7:

//Question:-check for REVERSING INTEGERS

#include <iostream>

#include<string>

#include <algorithm>

using namespace std;

int main() {

    int num;

    cout<<"Enter a number you want to reverse : \n";

    cin>>num;

    string num1=to_string(num); //use to convert integer to string

    reverse(num1.begin(), num1.end()); //reverse the string

    int num3=stoi(num1); //use to convert string to integer

    cout<<num3;

    return 0;

}


Question 6:

//Question:-check for vowel and consonant in c++

#include <iostream>

using namespace std;

int main() {

    char alpha;

    cout<<"Enter a character you want to check for either it's consonant or vowel :\n";

    cin>>alpha;

    if(alpha=='a' || alpha=='e' || alpha=='i' || alpha=='o' || alpha=='u'

    || alpha=='A' || alpha=='E' || alpha=='I' || alpha=='O' || alpha=='U')

    {

        cout<<endl<<alpha<<" is vowel";

    }

    else

    cout<<endl<<alpha<<" is consonant";

    return 0;

}



Question 5:

#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

int main() {

    int N, ar[1000];

    cin>>N;

    for(int i=0;i<N;i++)

    {

        cin>>ar[i];

    }

    for(int i=N-1;i>=0;i--)

    {

        cout<<ar[i]<<" ";

    }

    return 0;

}

Question 4:

#include <iostream>

using namespace std;

void temperature()

{

    int temp[100],n=7;

    for(int i=1;i<=n;i++)

    {

    cout<<"Enter the temperature at Day "<<i<<" : "<<endl;

    cin>>temp[i];

    }

    //for average temperature .

    int avg_temp=0;

    for(int i=1;i<=n;i++)

    {

        avg_temp+=temp[i];

    }

    cout<<"Average temperature of Week is : "<<(avg_temp/7)<<endl;

    //for maximum and minimum temperature.

    int max_temp=temp[1],min_temp=temp[1],max_index,min_index;

    for(int i=1;i<=n;i++)

    {

            if(max_temp<temp[i])

            {

                max_temp=temp[i];

                max_index=i;

            }

            else if(min_temp>temp[i])

            {

                min_temp=temp[i];

                min_index=i;

            }

    }

    cout<<"Maximum Temperature in the week is : "<<max_temp<<" At Day : "<<max_index<<endl;

    cout<<"Minimum Temperature in the week is : "<<min_temp<<" At Day : "<<min_index<<endl;

}

int main()

{

    temperature();

    return 0;

}


Question : 3

#include<iostream>

using namespace std;

int main()

{

    int x,y,z;

    cout<<"Enter three numbers to find greatest :";

    cin>>x>>y>>z;

    int* a=&x;

    int* b=&y;

    int* c=&z;

    if(*a>*b && *a>*c)

    {

        cout<<"Greatest number is : "<<*a;

    }

    else if(*b>*a && *b>*c)

    {

        cout<<"Greatest number is : "<<*b;

    }

    else{

        cout<<"Greatest number is : "<<*c;

    }

    return 0;

}


Question : 4

#include<iostream>

using namespace std;

int arr1[10],n;

void input_sort()

{

    cout<<"Enter the size of array : ";

    cin>>n;

    for(int i=0;i<n;i++)

    {

        cout<<"Enter the value of "<<i<<" element : "<<endl;

        cin>>arr1[i];

    }


}

void sort1()

{


    for(int i=0;i<n;i++)

        {

        for(int j=i+1;j<n;j++)

        {

            if(arr1[j]<arr1[i])

        {

            int temp=arr1[i];

            arr1[i]=arr1[j];

            arr1[j]=temp;

        }

        }

    }

    cout<<"\nArray elemets : \n";

    for(int i=0;i<n;i++)

    {

        cout<<"element at : "<<i<<" is : "<<arr1[i]<<endl;

    }

}

void display()

{

    cout<<"\nArray elemets : \n";

    for(int i=0;i<n;i++)

    {

        cout<<"element at : "<<i<<" is : "<<arr1[i]<<endl;

    }

}

int main()

{

    input_sort();

    display();

    sort1();


    return 0;

}


Comments

Post a Comment

Popular posts from this blog

RunwayWithCode

web development Lectures

C++ Lectures