Compress the video Solution || Chef Favorite Recipe || Optimizing Video Size || How to Minimize the Size of a Video: A Step-by-Step Algorithmic Approach ||

Compress the video:

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 N frames initially. The value of the ith frame is A. Chef can do the following type of operation any number of times:

• Choose an index (1≤i≤N) such that the value of the ith frame is equal to the value of either of its neighbors and remove the ith frame.

Find the minimum number of frames Chef can achieve.


Input Format

• The first line contains a single integer N - the number of frames initially.

• The second line contains N space-separated integers, A1,A2,...,AN - the values of the frames.


Output Format

Print the output in a single line the minimum number of frames Chef can achieve.


Constraints

• 1≤N≤105

1≤A≤106

• Sum of N over all test cases does not exceed 2.105.


Example:

Input1:

2

11


Output1:

1


Explanation:

There are two frames where both frames have value 1. 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 [1]. The minimum number of frames Chef can achieve is 1.


Input2:

3

123


Output2:

3


Explanation:

There are 3 frames. All frames have distinct values. Thus, the minimum number of frames Chef can achieve is 3.


Sample input 

2

1 1


Sample output

1




Program:

#C

#include <stdio.h>


int main() {

    int num, arr[100], count=0;

    // Take an integer input 'num' from the user

    scanf("%d", &num);

    // Take 'num' integer values from the user and store them in array 'arr'

    for (int i = 0; i < num; i++) {

        scanf("%d", &arr[i]);

    }

    // Loop through the array 'arr', comparing each element with the next element

    for (int i = 0; i < num-1; i++) {

        if (arr[i] == arr[i+1]) {

            // If two consecutive elements are equal, increment the variable 'count'

            count++;

        }   

    }   

    // Print the number of unique values in the array 'arr'

    printf("%d\n", num-count);

    return 0;

}



#Python 
num = int(input())
arr = list(map(int, input().split()))

count = 0

# Loop through the array 'arr', comparing each element with the next element
for i in range(num-1):
    if arr[i] == arr[i+1]:
        # If two consecutive elements are equal, increment the variable 'count'
        count += 1

# Print the number of unique values in the array 'arr'
print(num-count)








Explanation:

    Program that takes an integer input 'num' from the user, followed by 'num' integer values stored in an array 'arr'.

    The program then loops through the array 'arr', comparing each element with the next element in the array. If two consecutive elements are equal, the program increments a variable 'count'.

    Finally, the program prints the value of 'num' minus 'count' to the console. This represents the number of unique values in the array 'arr'.

Post a Comment

0 Comments