Fruit Slicing

3.6

9 votes
Hashing, Greedy Algorithms, Basics of Greedy Algorithms, Algorithms
Problem

You are given N fruits.

The weight of the fruits is represented by an array A.

All those fruits which have the same weight can be sliced in one step.

Task

Your task is to determine the number of steps to slice all the fruits.

Example

Assumption:

N=6, A=[20,40,30,50,40,20]

Approach:

One of the ways to slice the fruits can be:

In the first step, we can slice fruits at position 1 and 6 as they have equal weights.

In the second step, we can slice fruits at position 2 and 5 as they have equal weights.

In the third step, we can slice fruit at position 3.

In the fourth step, we can slice fruit at position 4.

Hence, the required answer is 4.

Function Description

Complete the function provided in the editor. This function takes two parameters and returns the required answer.

N= The number of fruits.

A[i]= The weight of the fruits.

Input Format

Note: This is the input format you must use to provide custom input (available above the Compiler and Test button).

The first line contains denoting the number of test cases.  also specifies the number of times you have to run the solve function on a different set of inputs.

For each test case:

The first line contains the size of the array.

The second line contains the array.

Output Format

For each test case, print the answer in a new line.

Constraints

1T101N1051A[i]1012

Code Snippets(also called starter code/boilerplate code)

This question has code snippets for C, CPP, Java and Python.
 

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

The first line contains the number of test cases.

The first test case is explained in the above example in the question.

In the second test case,

In the first step, we can slice fruits at position 3 and 4 as they have equal weights.

In the second step, we can slice fruit at the 1 position .

In the third step, we can slice fruit at the 2 position .

Hence, the required answer is 3.
 

Editor Image

?