Problem Solving in C++: Class and Object & Dynamic Memory Allocation
Introduction
Welcome to the realm of Sunday problem-solving adventures! Delve into the world of coding challenges as we unlock the keys to efficient programming. Transform your Sundays into productive and rewarding coding sessions.
Sort
Problem Statement
You will be given an array A of size N. Initially, you need to print the array by sorting it in ascending order. Afterward, you need to print the array sorted in descending order.
Input Format
- The first line will contain N.
- The next line will contain the array A.
Constraints
- 1 <= N <= 10⁵
- -10⁹ <= A[i] <= 10⁹ Where 0 <= i < N
Output Format
- Print two lines. The first line will contain the array sorted in ascending order. The next line will contain the array sorted in descending order.
Sample Input 0
5
2 4 6 1 3
Sample Output 0
1 2 3 4 6
6 4 3 2 1
Try first on your own.
Answer:-
#include<bits/stdc++.h>
using namespace std;
int main (){
int n;
cin>>n;
int a[n];
for(int i=0; i<n;i++){
cin>>a[i];
}
sort(a,a+n);
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
sort(a,a+n, greater<int>());
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
return 0;
}
Problem name: Sort It 2
Problem Statement
You will be given an array A and the size of that array N. You need to create a function named sort_it(). After taking the input for the size in the main function, call that function by giving the size as a parameter and take the array input inside that function. After that, you need to sort the array in descending order. Then, return that array from the function and receive it in the main function. Finally, print the sorted array in the main function.
Input Format
- The first line will contain N.
- The second line will contain the array A.
Constraints
- 1 <= N <= 10⁵
- -10⁹ <= A[i] <= 10⁹ Where 0 <= i < N
Output Format
- Output the array in descending order.
Sample Input 0
5
1 4 2 3 5
Sample Output 0
5 4 3 2 1
Try first on your own.
Answer:-
#include<bits/stdc++.h>
using namespace std;
int* sort_it(int n){
int *ar = new int[n];
for(int i = 0; i < n; i++){
cin >> ar[i];
}
sort(ar, ar + n, greater<int>());
return ar;
}
int main (){
int size;
cin >> size;
int *a = sort_it(size);
sort(a, a + size, greater<int>());
for(int i = 0; i < size; i++){
cout << a[i] << " ";
}
delete[] a;
return 0;
}
Problem name:- Choose Three
Problem Statement
You will be given an array A and the size of that array N. Additionally, you will be given a sum S. Your task is to determine whether it is possible to select three distinct indexed values from the array such that their summation equals S.
Input Format
- First line will contain T, the number of test cases.
- First line of each test case will contain N and S.
- Second line of each test case will contain the array A.
Constraints
- 1 <= T <= 100
- 1 <= N <= 100
- 1 <= S <= 1000
- 1 <= A[i] <= 1000 Where 0 <= i < N
Output Format
- Output “YES” if it is possible, otherwise output “NO”.
Sample Input 0
5
5 10
1 2 3 4 5
5 6
4 2 3 5 4
3 6
2 2 2
4 4
2 8 1 5
1 3
1
Sample Output 0
YES
NO
YES
NO
NO
Explanation 0
In the first test case, we can make 10 by adding 5+4+1. There are other ways too.
In the second test case, it is not possible to make 6 by adding three different indexed values from the array.
In the third case, it is possible to make 6 by using three different indexed values.
Try on your own.
Answer:-
#include<bits/stdc++.h>
using namespace std;
int main (){
int t;
cin>>t;
while(t - ){
int n, S;
cin>> n>> S;
int a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
bool found= false;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
for (int k = j + 1; k < n; ++k) {
if (a[i] + a[j] + a[k] == S) {
found = true;
break;
}
}
if (found) {
break;
}
}
if (found) {
break;
}
}
if(found){
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
}
return 0;
}
Who Is It
Problem Statement
A student has several pieces of information, such as a unique ID, name, section, and total marks. You will be given the information of three students. Your task is to determine and print the details of the student who achieved the highest total marks. In the case of a tie (i.e., two or more students having the same total marks), print the information of the student with the smaller ID.
Input Format
- First line will contain T, the number of test cases.
- For each test case there will be 3 lines. Each line will contain — ID, Name, Section, Total Marks of a student. The name will contain lowercase English alphabets only.
Constraints
- 1 <= T <= 1000
- 1 <= ID <= 3
- 1 <= |Name| <= 100
- ‘A’ <= Section <= ‘Z’
- 0 <= Total Marks <= 100
Output Format
- Output the information as asked in the question.
Sample Input 0
3
1 sakib A 50
2 rakib D 96
3 akib C 90
1 sakib A 50
2 rakib D 96
3 akib C 96
1 sakib A 50
2 rakib D 50
3 akib C 40
Sample Output 0
2 rakib D 96
2 rakib D 96
1 sakib A 50
Try on your own.
Answer:-
#include<bits/stdc++.h>
using namespace std;
class Student{
public:
int uid;
char name [101];
char a;
int score;
Student():uid(0),a('\0'),score(0) {
name[0] ='\0';
}
Student (int uid, char *name, char a, int score){
this->uid=uid;
strcpy(this->name, name);
this->a=a;
this->score=score;
}
};
int main (){
int T;
cin >> T;
for (int t = 0; t < T; t++) {
Student students[3];
for (int i = 0; i < 3; i++) {
int uid, score;
char name[101];
char a;
cin >> uid >> name >> a >> score;
students[i]= Student(uid, name, a, score);
}
int maxScore = students[0].score;
int maxID= students[0].uid;
for(int i = 1; i < 3; i++){
if(students[i].score > maxScore){
maxScore= students[i].score;
maxID= students[i].uid;
}
else if(students[i].score == maxScore && students[i].uid < maxID){
maxScore =students[i].score;
maxID= students[i].uid;
}
}
cout << maxID <<" "<< students[maxID - 1].name <<" "<<students[maxID - 1].a <<" "<< maxScore<<endl;
}
return 0;
}
Problem Name:- Monkey
Problem Statement:
Amena has just learned alphabets. She can read write from a to z only in lowercase. But, Amena always writes in alphabetic order (alphabetic order means from a to z in sorted order) what she saw. Also she writes a line as a word. For example, she writes monkey as ekmnoy. Her mother wants to test her reading and writing skills. Her mother gave her some lines, can you tell what she will write?
Note: Input will be given by EOF.
Input Format
- Input consist of a line S. The line will contain lowercase letters and spaces. It is possible that there are multiple spaces together and the line end with spaces.
Constraints
- 1 <= |S| <= 10⁵
Output Format
- Output what Amena will write.
Sample Input 0
monkey
i love flower
Sample Output 0
ekmnoy
eefilloorvw
Try on your own.
Answer:-
#include<bits/stdc++.h>
using namespace std;
int main (){
char str[100005];
while(cin.getline(str,100005)){
int x=strlen(str);
sort(str, (str+x));
for(int i=0; i<x; i++){
if(str[i]==' '){
continue;
}
cout<<str[i];
}
cout<<endl;
}
return 0;
}
Please leave a Clap and Comment on my work. Your feedback will motivate me to create and share more content. Thank you!