Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
763 views
in Online Assessments by Expert (46,090 points) | 763 views

2 Answers

0 like 0 dislike
Best answer

Images of ques

image
image

by Expert (46,090 points)
0 like 0 dislike

Q1 is a simple greedy algorithm in which you just need to find the sum of absolute difference of adjascent elements.
Code:

 

#include <bits/stdc++.h>
using namespace std;
#define int long long

int32_t main() {
     int n;
     cin>>n;
     int arr[n];
     for(int i=0;i<n;i++)
     cin>>arr[i];
     int sum=0;
     for(int i=n-1;i>=1;i--)
     sum+=abs(arr[i]-arr[i-1]);
     cout<<sum<<endl;
     return 0;
}
by Expert (46,090 points)