Session 1 Sheet (Problems and Answers)
Session 1 Sheet (Problems and Answers)
Input
The first line contains a single integer N (1 ≤ N ≤ 200) the number of elements
in the array.
The second line contains N integers A1, A2, ..., AN (−10⁹ ≤ Ai ≤ 10⁹) the
elements of the array.
Output
Print YES if the array is increasing. Otherwise, print NO.
2
Answer of Problem A
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
int arr[N];
for (int i = 0; i < N; i++)
{
cin >> arr[i];
}
for (int i = 1; i < N; i++)
{
if (arr[i] < arr[i - 1])
{
cout << "NO";
return 0;
}
}
cout << "YES";
return 0;
}
3
Problem B: Most Frequent Number (1st Form)
Input file: standard input Time limit: 1 seconds
Output file: standard output Memory limit: 256 megabytes
You are given an integer N and an array A of N integers. Your task is to find
the number that appears the most frequently in the array.
Input
The first line contains a single integer N (1 ≤ N ≤ 200) the number of elements
in the array.
The second line contains N integers A1, A2, ..., AN (0 ≤ Ai ≤ 150) the elements
of the array.
Output
Print a single integer the number that occurs most frequently in the array.
4
Answer of Problem B
#include <bits/stdc++.h>
using namespace std;
int main()
{
int N;
cin >> N;
int arr[N];
int frequencyArray[151] = {0};
for (int i = 0; i < N; i++)
{
cin >> arr[i];
frequencyArray[arr[i]]++;
}
int max_index = 0;
for (int i = 0; i < 151; i++)
{
if (frequencyArray[i] > frequencyArray[max_index])
max_index = i;
}
cout << max_index << " repeated " <<
frequencyArray[max_index];
return 0;
}
5
Problem C: Most Frequent Number (2nd Form)
Input file: standard input Time limit: 1 seconds
Output file: standard output Memory limit: 256 megabytes
You are given an integer N and an array A of N integers. Your task is to find
the number that appears the most frequently in the array.
Input
The first line contains a single integer N (1 ≤ N ≤ 200) the number of elements
in the array.
The second line contains N integers A1, A2, ..., AN (-500 ≤ Ai ≤ 270) the
elements of the array.
Output
Print a single integer the number that occurs most frequently in the array.
6
Answer of Problem C
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, value, max_position = 0;
cin >> N;
int A[771] = {0};
for (int i = 0; i < N; i++)
{
cin >> value;
A[value + 500]++;
}
for (int i = 0; i < 771; i++)
{
if (A[i] > A[max_position])
max_position = i;
}
cout << max_position - 500 << " repeated " << A[max_position] << "
times";
return 0;
}
7
Problem D: Digits Frequency
Input file: standard input Time limit: 1 seconds
Output file: standard output Memory limit: 256 megabytes
You are given an integer N and an array A of N integers. Your task is to count
how many times each digit from 0 to 9 appears in all the numbers.
Each number can have multiple digits. You need to print, for each digit from
0 to 9, how many times it appeared in total, each on a separate line.
Input
The first line contains a single integer N (1 ≤ N ≤ 200) the number of
elements in the array.
The second line contains N integers A1, A2, ..., AN (0 ≤ Ai ≤ 1000).
Output
Print 10 lines. Each line contains two integers separated by a space:
• The digit d (from 0 to 9)
• The number of times digit d appeared in the array.
Standard Input Standard Output
3 01
78 307 10
20
31
40
50
60
72
81
90
8
Answer of Problem D
#include <bits/stdc++.h>
using namespace std;
int main()
{
int N, frequency[10] = {0};
cin >> N;
int A[N];
for (int i = 0; i < N; i++)
{
cin >> A[i];
while (A[i] > 0)
{
frequency[A[i] % 10]++;
A[i] /= 10;
}
}
for (int d = 0; d < 10; d++)
{
cout << d << " " << frequency[d] << endl;
}
return 0;
}