You are given an integer N denoting the value of a year. Your task is to write a program that checks whether the year is a leap year or not.
If the given year is a leap year then print "YES", if not then print "NO".
INPUT:
Contains an integer value denoting the year.
OUTPUT:
Print YES or NO as output.
Test Case: 1
INPUT:
2004
OUTPUT:
YES
Test Case: 2
INPUT:
2003
OUTPUT:
NO
#include < stdio.h>
int main()
{
int year;
scanf("%d", &year);
if (year % 400 == 0)
{
printf("YES");
}
else if (year % 100 == 0)
{
printf("NO");
}
else if (year % 4 == 0)
{
printf("YES");
}
else
{
printf("NO");
}
return 0;
}
Given an array arr[ ] of length N consisting cost of N chocolates and an integer K depicting the amount, you have with you. Your task is to find the maximum number of chocolates you can buy with K amount.
INPUT:
The first line of input contains T for test cases.
For each Test Case:
The first line contains the value of N denoting the total number of chocolates.
The second line contains the value of K denoting the amount you have.
The next line contains N space separated integers denoting the value of different chocolates.
OUTPUT:
Contains a single integer denoting the number of chocolates you can buy.
Test Case:1
INPUT:
1
5
70
12 35 18 6 34
OUTPUT:
4
Explanation:
Given you have an amount of 70 with you. So in this amount you can buy the chocolates of amount 12, 18, 6, 34 as 12 + 18 + 6 + 34 <=70.
SO our answer is 4.
#include < bits/stdc++.h>
using namespace std;
class Solution
{
public:
int toyCount(int N, int K,vector< int> arr)
{
int count = 0;
sort(arr.begin() , arr.end());
for(int i = 0; i < N; i++)
{
K -= arr[i];
if(K < 0)
break;
count++;
}
return count;
}
};
int main()
{
int T;
cin>>T;
while(T--)
{
int N, K;
cin>>N>>K;
vector< int> arr(N);
for(int i = 0;i < N;i++)
cin>>arr[i];
Solution ob;
cout<< ob.toyCount(N, K, arr)<< endl;
}
return 0;
}
You are given an array A[ ], find maximum and minimum elements from the array.
Input:
The first line of input contains an integer T, denoting the number of testcases.
The first line of each testcase contains a single integer N denoting the size of the array.
The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
For each testcase, print the maximum and minimum element in a single line with space in between.
Test Case: 1
INPUT:
1
3
3 2 4
OUTPUT:
4 2
EXPLANATION:
Maximum Element is 4
Minimum Element is 2
#include< iostream>
using namespace std;
int main()
{
//code
int T;
cin >> T;
while(T--)
{
int n;
cin >>n;
int arr[100];
for(int i=0; i< n; i++)
{
cin >> arr[i];
}
int max_element=0;
int min_element=100;
for(int i=0; i< n; i++)
{
if(max_element < arr[i])
{
max_element=arr[i];
}
if(min_element > arr[i])
{
min_element = arr[i];
}
}
cout << max_element << " " << min_element<< endl;
}
return 0;
}
You are given the value of the area of a triangle that is maximum possible for a particular hypotenuse of a right angled triangle. Your task is to find that particular hypotenuse.
Note: If the answer comes in Decimal, find it's Floor value.
INPUT:
The first line of input contains the number of test cases T.
For each test case next line contains the value of N which is the area.
OUTPUT:
For each test case the output contains an integer which is the hypotenuse,
Test Case: 1
INPUT:
1
25
OUTPUT:
10
Explanation:
For a maximum area of 25 square units the value of the hypotenuse is 10.
#include < bits/stdc++.h>
using namespace std;
class Solution {
public:
int getHypotenuse(long long N) {
return floor(sqrt(4*N));
}
};
int main() {
int t;
cin >> t;
while (t--) {
long long N;
cin>>N;
Solution ob;
cout << ob.getHypotenuse(N) << endl;
}
return 0;
}
Given a street of N houses (a row of houses), each house having K amount of money kept inside; now there is a thief who is going to steal this money but he has a constraint/rule that he cannot steal/rob two adjacent houses. Find the maximum money he can rob.
INPUT:
First line of input contains T test cases.
For each test case the next line contains two space separated integers indicating the values of N and K respectively.
OUTPUT:
contains the value of sum of the money.
Test Case: 1
INPUT:
1
2 15
OUTPUT:
15
#include < bits/stdc++.h>
using namespace std;
class Solution {
public:
int maximizeMoney(int N , int K) {
int sum=0;
for(int i=1; i<=N; i++)
{
if(i%2!=0)
{
sum += K;
}
}
return sum;
}
};
int main() {
int t;
cin >> t;
while (t--) {
int N,K;
cin>>N>>K;
Solution ob;
cout << ob.maximizeMoney(N,K) << endl;
}
return 0;
}