Help to find mistake. c++

My output don't much with needed output. In first case, I received 7 instead of 6. Please help me find a bag. How should I change my code to receive correct output. Thx in advance.

// Count Subarrays
//Given an array A1, A2, ..., AN, count the number of subarrays of array A
// which are non - decreasing.
//A subarray A[i, j], where 1 ≤ i ≤ j ≤ N is a sequence of integers
// Ai, Ai + 1, ..., Aj.
//A subarray A[i, j] is non - decreasing if Ai ≤ Ai + 1 ≤ Ai + 2 ≤ ... ≤ Aj.
// You have to count the total number of such subarrays.

//The first line of input contains an integer T denoting the number of test cases.
// The description of T test cases follows.
//The first line of each test case contains a single integer N denoting the size
// of array.
//The second line contains N space - separated integers A1, A2, ..., AN
// denoting the elements of the array.

//For each test case, output in a single line the required answer.
//1 ≤ T ≤ 5
//1 ≤ N ≤ 105
//1 ≤ Ai ≤ 109
//Subtask 1 (20 points) : 1 ≤ N ≤ 100
//Subtask 2 (30 points) : 1 ≤ N ≤ 1000
//Subtask 3 (50 points) : Original constraints
//Input :
//1 4 2 3
//Output :
//      6
//      1
//      Explanation
//      Example case 1.
//      All valid subarrays are A[1, 1], A[1, 2], A[2, 2], A[3, 3], A[3, 4], A[4, 4].
//      Note that singleton subarrays are identically non - decreasing.
//      Example case 2.
//      Only single subarray A[1, 1] is non - decreasing.

#include <iostream>

using namespace std; 

int main() {
	int t; 
	cin >> t; 
	while (t--) {

		int n, sum = 0; 
		cin >> n; 
		int * arr = new int[n];
		for (int i = 0; i < n; i++) {

			cin >> arr[i];

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {

				if (arr[i] == arr[j])

				if (arr[i] - arr[j] == -1)

		cout << sum << endl; 

	return 0; 

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

With a learning exercise like this, you should become adept at using the debugger. Please give it a try. If you use it, it will save years of your life.

Then if you have a specific question on your debugging result, don't hesitate to ask.'s-Debugging-Guide-using-Visual-Studio-2008-Express.html
This article goes into a little more depth in using breakpoints:

Since you are dealing with arrays, you probably would like to see their contents. Have a look here:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.