Problem
Chef recorded a video explaining his favorite recipe. However, the size of the video is too large to upload on the internet. He wants to compress the video so that it has the minimum size possible.
Chef's video has frames initially. The value of the frame is . Chef can do the following type of operation any number of times:
- Choose an index such that the value of the frame is equal to the value of either of its neighbors and remove the frame.
Find the minimum number of frames Chef can achieve.
Input Format
- First line will contain , the number of test cases. Then the test cases follow.
- The first line of each test case contains a single integer - the number of frames initially.
- The second line contains space-separated integers, - the values of the frames.
Output Format
For each test case, output in a single line the minimum number of frames Chef can achieve.
Constraints
- Sum of over all test cases does not exceed .
Sample 1:
4 1 5 2 1 1 3 1 2 3 4 2 1 2 2
1 1 3 3
Explanation:
Test case : There is only one frame with value . Since there are no neighbors, Chef won't remove any frame and the minimum number of frames Chef can achieve is .
Test case : There are two frames where both frames have value . Chef can remove the first frame as the value of the first frame is equal to that of the second frame. The remaining frames have values . The minimum number of frames Chef can achieve is .
Test case : There are frames. All frames have distinct values. Thus, the minimum number of frames Chef can achieve is .
Test case : Chef can remove the fourth frame as the value of the fourth frame is equal to that of the third frame. The remaining frames have values . Thus, the minimum number of frames Chef can achieve is .
Program :
#include <stdio.h>
int main(void) {
// your code goes here
int t;
scanf("%d",&t);
while(t--)
{
int n,count=0,i;
scanf("%d",&n);
int a[n];
a[-1]=-1;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i-1]!=a[i])
{
count+=1;
}
}
printf("%d\n",count);
}
return 0;
}
0 Comments