Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Solution: Reduction Operations to Make the Array Elements Equal
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given an array of integers nums, return the number of operations required to make all elements in nums equal. To perform one operation, you can follow the below steps:

  • Select the maximum element of nums. If there are multiple occurrences of the maximum element, choose the element which has lowest index i.
  • Select the second largest element of nums.
  • Replace the element at index i with the second largest element.

Examples

Example 1:

  • Input: [3, 5, 5, 2]
  • Expected output: 5
  • Justification:

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible