PINOCH1 - Editorial (Kodeathon 15.2)

PROBLEM LINK:

Practice

Contest

Author & Editorialist: Pulkit Sharma

Tester: Satyam Gupta

DIFFICULTY:

CAKEWALK

PREREQUISITES:

loops (Array Traversal)

PROBLEM:

Given an array A of size N , find out the number of indexes (i) , where A[i] is not equal to A[i-1]

The problem is pretty straightforward , and I hope everyone who took part in the contest was able to solve this one ! (and if you were not able to, please go through the editorial)

EXPLANATION:

Lets start with day 1 in the diary.

We do not have any prior knowledge of Pinocchio’s Nose Length. So, we can not be sure that whether he lied on that day or not.

Remember that the day the first entry in the diary was recorded on some arbitrary time in Pinocchio’s Lifetime , Therefore it can be of any value which satisfies the constraints.

So, we are sure that when N = 1 , answer = 0 . (Because , the problem asks for number of days on which Pinocchio lied ‘for sure’ , again we are not sure about the first day).

When N > 1 , then we can just inspect each element of the array and compare it with the previous one.

If any mismatch is found , we will increment the answer by 1 as we are sure that Pinocchio lied on that particular day.

the following code will work fine

answer = 0;
for(int i = 1 ; i < n ; i++) //we do not care for the first element
{
	if(A[i] != A[i-1])
	{
		answer = answer + 1;
	}	
}

Finally print answer . And remember to put a newline character after printing the value of answer

AUTHOR’S SOLUTION:

#include<bits/stdc++.h>

#define ll long long int
#define f0(i,n) for(i=0;i<n;i++)
#define f1(i,n) for(i=1;i<=n;i++)
#define f01(i,n) for(i=1;i<n;i++)
#define fr0(i,n) for(i=n-1;i>=0;i--)
#define fr1(i,n) for(i=n;i>0;i--)

using namespace std;

int main()
{
	int t,n,l,i,ans,x;

	cin>>t;
	
	while(t--)
	{
		cin>>n;
		ans=0;
		
		cin>>x;
		
		for(i=1;i<n;i++)
		{
			cin>>l;
			
			if(l!=x) ans++;
			
			x=l;			
		}
		
		cout<<ans<<endl;
	}
	
	return 0;
}