Fruit Slicing
Practice
3.8 (11 votes)
Hashing
Greedy algorithms
Basics of greedy algorithms
Algorithms
Problem
79% Success 2223 Attempts 10 Points 2s Time Limit 256MB Memory 1024 KB Max Code

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

\(1 \leq T \leq 10\\ 1 \leq N \leq 10^5\\ 1 \leq A[i] \leq 10^{12} \)

Code Snippets(also called starter code/boilerplate code)

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

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:10
48 votes
Tags:
AlgorithmsEasyGreedy Algorithms
Points:10
6 votes
Tags:
AlgorithmsBasics of Greedy AlgorithmsC++Greedy Algorithms
Points:10
60 votes
Tags:
AlgorithmsEasyGreedy Algorithms