search
HomeBackend DevelopmentC++In C++, find the number of operations required to make all elements of an array equal

In C++, find the number of operations required to make all elements of an array equal

In this problem, we get an array arr of size n. Our task is to find the number of operations required to make all array elements equal. ##.

If it is not possible to make array elements equal, print -1.

Let us take an example to understand this problem,

Input : arr[] = {7, 3, 3, 3}
Output : 3
Explanation

The allocated array is {4, 4, 4 , 4}Solution

A simple way to solve this problem is to find the maximum value of the array. This maximum value is then used to check if all elements of the array are equal and if the value is equal to the maximum value of the array minus n (or a multiple thereof). If yes, n is returned, if no, -1 is returned (indicating that it is not possible).

Example

Let’s take an example to understand the problem

#include<bits/stdc++.h>
using namespace std;
int findOperationCount(int arr[],int n){
   int j = 0, operations = 0;
   int maxVal = arr[0];
   int minVal = arr[0];
   int maxValInd = 0;
   for (int i = 1; i < n; i++){
      if(arr[i] > maxVal){
         maxVal = arr[i];
         maxValInd = i;
      }
      if(arr[i] < minVal){
         minVal = arr[i];
      }
   }
   for (int i =0;i<n;i++){
      if (arr[i] != maxVal && arr[i] <= minVal && arr[i] != 0){
         arr[j] += 1;
         arr[maxValInd] -= 1;
         maxVal -= 1;
         operations += 1;
         j += 1;
      }
      else if (arr[i] != 0){
         j += 1;
      }
   }
   for (int i = 0; i < n; i++){
      if (arr[i] != maxVal){
         operations = -1;
         break;
      }
   }
   return operations;
}
int main(){
   int arr[] = {4, 4, 8, 4};
   int n = sizeof(arr)/sizeof(arr[0]);
   cout<<"The number of operations required to make all array  elements Equal is "<<findOperationCount(arr, n);
   return 0;
}

Output

The number of operations required to make all array elements Equal is 3

The above is the detailed content of In C++, find the number of operations required to make all elements of an array equal. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
使用C++编写代码,找到第N个非平方数使用C++编写代码,找到第N个非平方数Aug 30, 2023 pm 10:41 PM

我们都知道不是任何数字的平方的数字,如2、3、5、7、8等。非平方数有N个,不可能知道每个数字。因此,在本文中,我们将解释有关无平方数或非平方数的所有内容,以及在C++中查找第N个非平方数的方法。第N个非平方数如果一个数是整数的平方,则该数被称为完全平方数。完全平方数的一些例子是-1issquareof14issquareof29issquareof316issquareof425issquareof5如果一个数不是任何整数的平方,则该数被称为非平方数。例如,前15个非平方数是-2,3,5,6,

使用C++编写的数组右旋转的反转算法使用C++编写的数组右旋转的反转算法Sep 08, 2023 pm 08:17 PM

在本文中,我们将了解逆转算法,将给定的数组向右旋转k个元素,例如&minus;Input:arr[]={4,6,2,6,43,7,3,7},k=4Output:{43,7,3,7,4,6,2,6}Explanation:Rotatingeachelementofarrayby4-elementtotherightgives{43,7,3,7,4,6,2,6}.Input:arr[]={8,5,8,2,1,4,9,3},k=3Output:{4,9,3,8,5,8,2,1}寻找解决方案的方

在C编程中,找到一个圆的面积在C编程中,找到一个圆的面积Aug 25, 2023 pm 10:57 PM

圆是封闭图形。圆上的所有点到圆内一点的距离都相等。中心点称为圆心。点到圆心的距离称为半径。面积是封闭图形尺寸跨度的定量表示。圆的面积是圆的尺寸内包围的面积。计算圆面积的公式,Area=&pi;*r*r为了计算面积,我们给出了圆的半径作为输入,我们将使用公式来计算面积,算法STEP1:Takeradiusasinputfromtheuserusingstdinput.STEP2:Calculatetheareaofcircleusing,&nbsp;&nbsp;area=(

使用C++找到数组中唯一配对的数量使用C++找到数组中唯一配对的数量Sep 07, 2023 am 11:53 AM

我们需要适当的知识才能在C++的数组语法中创建几个唯一的对。在查找唯一对的数量时,我们计算给定数组中的所有唯一对,即可以形成所有可能的对,其中每个对应该是唯一的。例如-Input:array[]={5,5,9}Output:4Explanation:Thenumberofalluniquepairsare(5,5),(5,9),(9,5)and(9,9).Input:array[]={5,4,3,2,2}Output:16寻找解决方案的方法有两种方法可以解决这个问题,它们是&minus;

使用C++编写代码,找到具有相同最小值和最大值的子数组的数量使用C++编写代码,找到具有相同最小值和最大值的子数组的数量Aug 25, 2023 pm 11:33 PM

在本文中,我们将使用C++解决寻找最大值和最小值相同的子数组数量的问题。以下是该问题的示例&minus;Input:array={2,3,6,6,2,4,4,4}Output:12Explanation:{2},{3},{6},{6},{2},{4},{4},{4},{6,6},{4,4},{4,4}and{4,4,4}arethesubarrayswhichcanbeformedwithmaximumandminimumelementsame.Input:array={3,3,1,5,

使用C++按给定大小将双向链表分组反转使用C++按给定大小将双向链表分组反转Sep 04, 2023 am 09:49 AM

在这个问题中,我们得到一个指向链表头部的指针和一个整数k。在大小为k的组中,我们需要反转链表。例如-Input:1<->2<->3<->4<->5(doublylinkedlist),k=3Output:3<->2<->1<->5<->4寻找解决方案的方法在这个问题中,我们将制定一个递归算法来解决这个问题。在这种方法中,我们将使用递归并使用递归来解决问题。示例#include<iostream&

使用C++编写的数组旋转的逆转算法使用C++编写的数组旋转的逆转算法Aug 28, 2023 pm 11:13 PM

在给定的问题中,我们有一个数组,并且我们需要使用反转算法将数组旋转d个元素,例如&minus;Input:arr[]=[1,2,3,4,5,6,7],d=2Output:arr[]=[3,4,5,6,7,1,2]Explanation:Asyoucanseewehavetorotatethisarraybyd=2butourmaintaskistoachievethisbyusingareversaltechnique.我们对数组的旋转进行了一些反转技术的计算,并得出结论:首先,我们反转

使用C++编写,找到一个集合上的自反关系的数量使用C++编写,找到一个集合上的自反关系的数量Aug 26, 2023 pm 08:17 PM

在本文中,我们将解释在一个集合上找到反身关系的方法。在这个问题中,我们给出一个数字n,以及一个由n个自然数组成的集合,我们必须确定反身关系的数量。反身关系-如果对于集合A中的每个'a',(a,a)属于关系R,则称关系R是集合A上的反身关系。例如-Input:x=1Output:1Explanation:set={1},reflexiverelationsonA*A:{{1}}Input:x=2Output:4Explanation:set={1,2},reflexiverelationsonA*

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools