Sitemap
Aarya Raaz

Aarya Raaz

Simple and civilized common man.

Compare Sums

2 min readJan 4, 2026

This is a problem from Code360 (Coding Ninjas). In this problem, an array is given that is first in ascending order and then in descending order. We have to return 0 if the sum of the ascending part of the array is greater than the sum of the descending part, 1 if it is vice versa, otherwise -1.

#include<bits/stdc++.h>

using namespace std;

int findTheLarger(int n, vector<int> &v) {

// Write your code here.

int asSum = v.at(0), desSum = v.at(n — 1);

int peekIndex = 0;

for(int i = 1; i < n; i++) {

if(v.at(i — 1) < v.at(i)) {

if(i == n — 1) {

// cout << “delete 1” << endl;

// cout << “sum A: “ << asSum << endl;

return 0;

}

asSum += v.at(i);

}

else {

peekIndex = i — 1;

break;

}

}

// cout << “delete 2” << endl;

Get Aarya Raaz’s stories in your inbox

Join Medium for free to get updates from this writer.

// cout << “sum A: “ << asSum << “, peekIndex: “ << peekIndex << endl;

if(peekIndex == 0) return 1;

for(int i = peekIndex; i < n — 1; i++) {

desSum += v.at(i);

if(desSum > asSum) {

// cout << “delete 3” << endl;

// cout << “sum B: “ << desSum << endl;

return 1;

}

}

// cout << “delete 4” << endl;

// cout << “sum A: “ << asSum << “, sum B: “ << desSum << endl;

return asSum > desSum ? 0 : -1;

}

int main() {

vector<int> vec = {1, 2, 5, 3, 2};

int size = vec.size();

for(int x : vec) cout << x << “ “;

cout << endl;

int result = findTheLarger(size, vec);

cout << “Result: “ << result << endl;

return 0;

}

--

--

Aarya Raaz
Aarya Raaz

Written by Aarya Raaz

0 followers

Simple and civilized common man.