0% found this document useful (0 votes)
2 views9 pages

Session 1 Sheet (Problems and Answers)

The document outlines four programming problems related to arrays: determining if an array is increasing, finding the most frequent number in two different forms, and counting the frequency of each digit from 0 to 9 in a set of numbers. Each problem includes input and output specifications along with example cases. Additionally, the document provides C++ code solutions for each problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Session 1 Sheet (Problems and Answers)

The document outlines four programming problems related to arrays: determining if an array is increasing, finding the most frequent number in two different forms, and counting the frequency of each digit from 0 to 9 in a set of numbers. Each problem includes input and output specifications along with example cases. Additionally, the document provides C++ code solutions for each problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Session 1 Sheet

Problem A: Is Increasing Array?


Input file: standard input Time limit: 2 seconds
Output file: standard output Memory limit: 64 megabytes

You are given an integer N and an array A of N integers. Your task is to


determine whether the array is increasing.
An array is considered increasing if every element is greater than or equal to
the previous element.

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.

Standard Input Standard Output


4 YES
1225
5 NO
10789

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.

Standard Input Standard Output


7 1 repeated 3
1213155
6 3 repeated 4
383933

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.

Standard Input Standard Output


8 -1 repeated 3
-1 2 -1 3 -1 5 5 7
5 2 repeated 2
2 -2 -1 2 1

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;
}

You might also like