Codechef Long challenge 

JULY 2020

 

PROBLEM - Chef and Strings (CHEFSTR1)

PROBLEM DESCRIPTION

Example

Arr1 = [1,6,11,6,10,11]

Example

Arr1 = [1,6,11,6,10,11]

1 -> 6

Example

Arr1 = [1,6,11,6,10,11]

1 -> 6

1-       2 - 3 - 4 - 5-    6

Example

Arr1 = [1,6,11,6,10,11]

1 -> 6

1-       2 - 3 - 4 - 5-    6

abs(6-1) - 1 = 5 - 1 

= 4 steps.

Example

Arr1 = [1,6,11,6,10,11]

1 -> 6

1-       2 - 3 - 4 - 5-    6

abs(6-1) - 1 = 5 - 1 

= 4 steps.

Compute for whole array..!!

Approach :


	int t;
	cin >> t;
	while(t--)
	{
		long long int n;
		cin >> n;
		long long int* arr=  new long long int[n];
		for(int i = 0; i<n; i++)
			cin >> arr[i];
		long long int diff = 0;
		for(int  i = 1; i<n; i++)
		{
                diff += abs(arr[i] - arr[i-1])-1;
                //cout << diff << endl;

		}
		cout << diff << endl;

	}

Still, Have any Doubts???

Still, Have any Doubts???

Comment it You will get a reply

OR

Send your doubt to email provided in description.

Made with Slides.com