Get best answers to any doubt/query/question related to programming , jobs, gate, internships and tech-companies. Feel free to ask a question and you will receive the best advice/suggestion related to anything you ask about software-engineering , development and programming problems .

0 like 0 dislike
1,441 views
in Online Assessments by Expert (44,360 points)

2 Answers

0 like 0 dislike
 
Best answer

Images of ques

image
image

by Expert (44,360 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 (44,360 points)
...