DSA Learning Series Getting Started Practice Contest

Easy Problems to Get Started

learndsa
LEARNDSA


Buy Please 

#include <iostream>
using namespace std;

int main() {
int a,b,x,y;
cin>>a>>b>>x>>y;
cout<<a*x+b*y;
return 0;
}

Is Both Or Not

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    if(n%5==0 && n%11==0) cout<<"BOTH";
    else if(n%5==0 || n%11==0) cout<<"ONE";
    else cout<<"NONE";
return 0;
}

Factors Finding

#include <iostream>
using namespace std;

int main() {
int n,cnt=0,a[100];
cin>>n;
for(int i=1;i<=n;i++){
    if(n%i==0){
        cnt++;
}}
cout<<cnt<<endl;
for(int i=1;i<=n;i++){
    if(n%i==0)
        {cout<<i<<" ";}
}
return 0;
}


Find Second Largest

#include <iostream>
using namespace std;

int main() {
int a,b,c;
cin>>a>>b>>c;
if(a>=b && a>=c)
{
    if(b>c) cout<<b;
    else cout<<c<<endl;
}
if(b>=a && b>=c)
{
    if(a>c) cout<<a;
    else cout<<c<<endl;
}
if(c>=b && c>=a)
{
    if(a>b) cout<<a;
    else cout<<b<<endl;
}
return 0;
}


Range Odd

#include <bits/stdc++.h>
using namespace std;

int main() {
    int a,b;
    cin>>a>>b;
    if(a%2!=0){
        for(int i=a;i<=b;i+=2)
        {cout<<i;
        cout<<" ";}
    }else{
         for(int i=a+1;i<=b;i+=2)
        {cout<<i;
        cout<<" ";}
    }
return 0;
}

Raju and His Trip

#include <iostream>
using namespace std;

int main() {
int n;
cin>>n;
if(n%5==0 || n%6==0) cout<<"YES";
else cout<<"NO";
return 0;
}

Reverse Me

#include <iostream>
using namespace std;

int main() {
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++) {cin>>a[i];}
for(int i=n-1;i>=0;i--){
    cout<<a[i]<<" ";
}
return 0;
}


Find Me

Solution 1 :

#include<bits/stdc++.h>
using namespace std;
bool present(int arr[],int n,int k)
{
    for(int i=0;i<n;i++)
    {
        if(arr[i]==k)
            return true;
    }
    return false;
}
int main()
{
    int n,k;
    cin>>n>>k;
    int arr[n];
    for(int i=0;i<n;i++)
        cin>>arr[i];
    if(present(arr,n,k))
        cout<<"1"<<endl;
    else
        cout<<"-1"<<endl;
}

Solution 2 :

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);

    for (auto &e : a) cin >> e;

    bool present = false;
    for (auto e : a) present |= (e == k);

    cout << (present ? 1 : -1) << endl;
}

Valid Triangle Or Not

#include<bits/stdc++.h>
using namespace std;

bool isValid(int a, int b, int c)
{
if (a + b <= c || a + c <= b || b + c <= a)
return false;
else
return true;
}


int main()
{
int a,b,c;
cin>>a>>b>>c;
if (isValid(a, b, c))
cout << "YES";
else
cout << "NO";
}


Reverse Star Pattern


#include <iostream>
using namespace std;

int main() {
int n;
cin>>n;
for(int i=1;i<=n;i++){
    for(int j=n-i;j>=1;j--)
        cout<<" ";
        for(int k=1;k<=i;k++)
    cout<<"*";
    cout<<endl;
}
return 0;
}

Add Natural Numbers


#include <iostream>
using namespace std;

int main() {
    long long int n;
    cin>>n;
    cout<<(n*(n+1))/2;
return 0;
}

Sum Is Everywhere

#include <iostream>
using namespace std;

int main()
{
    long n;
    cin >> n;
    cout << n * n << " "; //formula for odd numbers
    cout << n * (n + 1);  //formula for even numbers
    return 0;
}

Triangle With Angle

#include "bits/stdc++.h"
using namespace std ;
int main(){
int a,b,c;
cin>>a>>b>>c ;
cout <<(a+b+c==180&&a&&b&&c?"YES":"NO") ;
}

Triangle Everywhere

#include <iostream>
using namespace std;

int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    if (a + b > c && b + c > a && c + a > b)
    {
        if (a == b && b == c)
            cout << "1";
        else if (a == b || b == c || c == a)
            cout << "2";
        else
            cout << "3";
    }
    else
        cout << "-1";
    return 0;
}

Alternative Square Pattern

#include <iostream>
using namespace std;

int main()
{
    int n, k = -4;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        if (i % 2 != 0)
        {
            k += 4;
            for (int j = 1; j <= 5; j++)
                cout << ++k << " ";
            cout << endl;
        }
        else
        {
            k += 6;
            for (int j = 1; j <= 5; j++)
                cout << --k << " ";
            cout << endl;
        }
    }
    return 0;
}