You are on page 1of 4

Experiment – 1.

1
Student Name: Aniket Sugara UID: 20BCS7284
Branch: BE-CSE Section/Group: 619-A
Semester: 5 Date of Performance: 08/08/22
Subject Name: Competitive Coding - I Subject Code: 20CSP-314

Q1. Given an array, of integers, print's elements in reverse order as a single line of space-
separated numbers.
Link: https://www.hackerrank.com/challenges/30-arrays/problem

Code:
for(int i = n-1; i >= 0; i--){
cout << arr[i] << ' ';
}

Output:
Q2. Complete the simpleArraySum function in the editor below. It must return the sum of
the array elements as an integer.
Link: https://www.hackerrank.com/challenges/simple-array-sum/problem?isFullScreen=true

Code:
int simpleArraySum(vector<int> ar) {
int sum = 0;
for(int i = 0; i < ar.size(); i++)
sum += ar[i];
return sum;
}

Output:
Q3.https://www.hackerrank.com/challenges/compare-the-triplets/problem?isFullScreen=true

Code:
#include <bits/stdc++.h>
using namespace std;

int main(){
int arr[3];
int arr1[3];
for(int i = 0; i < 3; i++)
cin >> arr[i];
for(int j = 0; j < 3; j++)
cin >> arr1[j];
int p1=0;
int p2=0;
for(int r = 0; r < 3; r++){
if (arr[r] > arr1[r])
p1++;
else if(arr[r] < arr1[r])
p2++;
}
cout << p1 << " " << p2 << endl;
return 0;
}

Output:
Q4. https://www.hackerrank.com/challenges/diagonal-difference/problem?isFullScreen=true

Code:
int diagonalDifference(vector<vector<int>> arr) {
int row = arr.size();
int col = arr[0].size();
int d1 = 0;
int d2 = 0;

for (int i = 0; i < row; i++) {


d1 += arr[i][i];
d2 += arr[i][col-1-i];
}

return abs(d1 - d2);


}

Output:

You might also like