Problem-Solving in C++: Class and Object (Part-1)
Introduction
Enhance your problem-solving skills with our weekly programming challenge collection! Dive into this week’s “Class and Object (Part-1)” edition for stimulating puzzles and solutions. Sharpen your coding prowess and join our community of passionate developers.
Comparison
Given a comparison symbol S between two numbers A and B. Determine whether it is Right or Wrong.
The comparison is as follows: A < B, A > B, A = B.
Where A, B are two integer numbers and S refers to the sign between them.
Input
Only one line containing A, S and B respectively (-100 ≤ A, B ≤ 100), S can be (‘<’, ‘>’,’=’) without the quotes.
Output
Print “Right” if the comparison is true, “Wrong” otherwise.
Examples
input
5 > 4
output
Right
input
9 < 1
output
Wrong
input
4 = 4
output
Right
Try on your own first.
Answer:
#include <iostream>
using namespace std;
int main() {
int A, B;
char S;
cin >> A >> S >> B;
switch (S) {
case '<':
cout << (A < B ? "Right" : "Wrong") << endl;
break;
case '>':
cout << (A > B ? "Right" : "Wrong") << endl;
break;
case '=':
cout << (A == B ? "Right" : "Wrong") << endl;
break;
default:
cout << "Invalid comparison symbol" << endl;
}
return 0;
}
Palindrome Array
Given a number N and an array A of N numbers. Determine if it’s palindrome or not.
Note:
An array is called palindrome if it reads the same backward and forward, for example, arrays { 1 } and { 1,2,3,2,1 } are palindromes, while arrays { 1,12 } and { 4,7,5,4 } are not.
Input
First line contains a number N (1≤ N ≤10^5) number of elements.
Second line contains N numbers (1≤ Ai ≤10^9).
Output
Print “YES” (without quotes) if A is a palindrome array, otherwise, print “NO” (without quotes).
Examples
input
5
1 3 2 3 1
output
YES
input
4
1 2 3 4
output
NO
Try on your own first.
Answer:
#include <iostream>
using namespace std;
int main() {
int N;
cin >> N;
int A[N];
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
bool isPalindrome = true;
for (int i = 0, j = N - 1; i < j; ++i, --j) {
if (A[i] != A[j]) {
isPalindrome = false;
break;
}
}
if (isPalindrome) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
Sorting
Given a number N and an array A of N numbers. Print the numbers after sorting them.
Note:
- Don’t use built-in-functions.
- try to solve it with bubble sort algorithm or Selection Sort.
- for more information watch : https://www.youtube.com/watch?v=EnodMqJuQEo.
Input
First line contains a number N (0 < N < 103) number of elements.
Second line contains N numbers ( — 100 ≤ Ai ≤ 100).
Output
Print the numbers after sorting them.
Examples
input
3
3 1 2
output
1 2 3
input
4
5 2 7 3
output
2 3 5 7
Try on your own first.
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];
}
return 0;
}
Codeforces Problem Links:
- V. Comparison
- G. Palindrome Array (solved using two-pointers technique)
- H. Sorting (solved using sort() function)
Please leave a Clap and Comment on my work. Your feedback will motivate me to create and share more content. Thank you!