Problem Solving in C++: Data Structure & Vector (Part-3)

Mohammed Muzahidul Islam
4 min readSep 17, 2024

--

Duplicate

Problem Statement

You will be given an array A of size N. Print “YES” if there is any duplicate value in the array, “NO” otherwise.

Input Format

  • First line will contain N.
  • Second line will contain the array A.

Constraints

  1. 1 <= N <= 100000
  2. 0 <= A[i] <= 10⁹; Where 0 <= i < N

Output Format

  • Output “YES” or “NO” without the quotation marks according to the problem statement.

Sample Input 0

5
1 2 3 4 5

Sample Output 0

NO

Sample Input 1

6
2 1 3 5 2 1

Sample Output 1

YES

Try on your own first.

Photo by charlesdeluvio on Unsplash

Solution:

#include<bits/stdc++.h>
using namespace std;
int main (){
int n;
cin>>n;
vector<int> v;
for(int i=0; i<n; i++){
int x;
cin>>x;
v.push_back(x);
}
bool duplicate=false;
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
if(v[i]==v[j]){
duplicate=true;
break;
}
}
if(duplicate){
break;
}
}
if(duplicate){
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
return 0;
}

Sorted

Problem Statement

You will given an array A of size N. You need to tell if the array is already sorted or not. If the array is sorted in ascending order print “YES”, otherwise print “NO”.

Input Format

  • First line will contain T, the number of test cases.
  • The first line of each test case will contain N.
  • The second line of each test case will contain the array A.

Constraints

  1. 1 <= T <= 1000
  2. 1 <= N <= 1000
  3. 0 <= A[i] <= 1000; Where 0 <= i < N

Output Format

  • Output “YES” or “NO” without the quotation marks according to the problem statement.

Sample Input 0

3
5
2 4 6 7 10
8
1 100 101 120 120 121 1000 1000
4
100 1 102 12

Sample Output 0

YES
YES
NO

Try on your first.

Photo by Emile Perron on Unsplash

Solution:

#include<bits/stdc++.h>
using namespace std;
int main (){
int x;
cin>>x;
while(x--){
long long int n;
cin>>n;
vector<long long int>v(n);
for(long long int i=0; i<n; i++){
cin>>v[i];
}
bool sorted=true;
for(int i=0; i<n-1; i++){
if(v[i]>v[i+1]){
sorted=false;
break;
}
}
if(sorted){
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
}
return 0;
}

Insert it

Problem Statement

You will given an integer array A of size N and another array B of size M. Also you will be given an index X. You need to insert the whole array B to the index X of array A.

Input Format

  • First line will contain N.
  • Second line will contain array A.
  • Third line will contain M.
  • Fourth line will contain array B.
  • The last line will contain X.

Constraints

  1. 1 <= N, M <= 10³
  2. 1 <= A[i], B[j] <= 10³; Where 0 <= i < N and 0 <= j < M
  3. 0 <= X <= N

Output Format

  • Output the final array A.

Sample Input 0

5
2 3 4 5 6
3
10 20 30
3

Sample Output 0

2 3 4 10 20 30 5 6

Sample Input 1

5
2 3 4 5 6
3
10 20 30
0

Sample Output 1

10 20 30 2 3 4 5 6

Sample Input 2

4
3 4 5 6
3
10 20 30
4

Sample Output 2

3 4 5 6 10 20 30

Try on your own first.

Photo by Adrien on Unsplash

Solution:

#include<bits/stdc++.h>
using namespace std;
int main(){
int n, m, x;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
}
cin>>m;
vector<int> v2(m);
for (int i = 0; i < m; ++i) {
cin >> v2[i];
}
cin>>x;
v.insert(v.begin() + x, v2.begin(), v2.end());
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << " ";
}
return 0;
}

Printing X

Problem Statement

You will be given a positive odd integer N, you need to print the pattern for it. See sample input and output for understanding the pattern.

Input Format

  • Input will contain only N.

Constraints

  1. 1 <= N <= 20 and N is odd.

Output Format

  • Output the pattern.

Sample Input 0

5

Sample Output 0

\   /
\ /
X
/ \
/ \

Sample Input 1

7

Sample Output 1

\     /
\ /
\ /
X
/ \
/ \
/ \

Sample Input 2

3

Sample Output 2

\ /
X
/ \

Sample Input 3

1

Sample Output 3

X

Try on your own.

Photo by Susan Wilkinson on Unsplash

Solution:

#include<bits/stdc++.h>
using namespace std;
int main (){
int n; cin>>n;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(i==j && i+j==n-1){
cout<<"X";
}
else if( i==j){
cout<<"\\";
}
else if(i+j==n-1){
cout<<"/";
}
else(cout<<" ");
}
cout<<endl;
}
return 0;
}

Thank you for your time. Do share and follow for more.

--

--