Professional Documents
Culture Documents
1(ARRAYS)
1.1 Aim: Given an array, A of N integers, print a’s elements in reverse
order as a single line of space separated numbers.
1.2 Input:
No of element = 5;
arr[5] = {4,3,7,6,2}
1.3 Output:
Result = 2,6,7,3,4
1.4 Code:
#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 = n-1; i >= 0 ; i--){
cout << arr[i] << " ";
}
return 0;
// UID: 20BCS7774
}
2.1 Aim: Given an array of integers, find the sum of its elements.
Description:
2.2 Input:
No of element = 6;
arr[5] = {1,2,3,4,10,11}
2.3 Output:
Result = 31
2.4 Code:
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int arr[n];
int s = 0;
for(int i = 0; i < n ; i++){
cin >> arr[i];
}
for(int i = 0; i < n ; i++){
s = s + arr[i];
}
cout << s;
return 0;
// UID: 20BCS7774
}
3.1 Aim:
Alice and Bob each created one problem for HackerRank. A reviewer rates the
two challenges, awarding points on a scale from 1 to 100 for three
categories: problem clarity, originality, and difficulty.
The rating for Alice's challenge is the triplet a = (a[0], a[1], a[2]), and the
comparing a[0] with b[0], a[1] with b[1], and a[2] with b[2].
Description:
3.2 Input:
A=123
B=221
3.3 Output:
Result = 1 1
3.4 Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a[3], b[3], al=0, bo=0;
for(int i = 0; i < 3; i++){
cin >> a[i];
}
for(int i = 0; i < 3; i++){
cin >> b[i];
}
for(int i = 0; i < 3; i++){
if(a[i] < b[i]){
bo++;
}
else if(a[i] > b[i]){
al++;
}
}
cout << al << " " << bo;
return 0;
//UID: 20BCS7774
}
4.1 Aim:
Given a square matrix, calculate the absolute difference between the sums of its
diagonals.
Description:
4.2 Input:
3
11 2 4
456
10 8 -12
4.3 Output:
Result = 15
4.4 Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int arr[n][n], s1=0, s2=0;
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
cin >> arr[i][j];
if(i == j){
s1 = s1 + arr[i][j];
}
if(j == (n-i-1)){
s2 = s2 + arr[i][j];
}
}
}
int d = s1 - s2;
if(d >= 0){
cout << d;
}
else{
cout << -d;
}
return 0;
//UID: 20BCS7774
}