- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Minimum number of moves to make all elements equal using C++.
Problem statement
Given an array of N elements and an integer K., It is allowed to perform the following operation any number of times on the given array −
Insert the Kth element at the end of the array and delete the first element of the array.
The task is to find the minimum number of moves needed to make all elements of the array equal. Print -1 if it is not possible
If arr[] = {1, 2, 3, 4, 5, 6} and k = 6 then minimum 5 moves are required: Move-1: {2, 3, 4, 5, 6, 6} Move-2: {3, 4, 5, 6, 6, 6} Move-3: {4, 5, 6, 6, 6, 6} Move-4: {5, 6, 6, 6, 6, 6} Move-5: {6, 6, 6, 6, 6, 6}
Algorithm
1. First we copy a[k] to the end, then a[k+1] and so on 2. To make sure that we only copy equal elements, all elements in the range K to N should be equal. We need to remove all elements in range 1 to K that are not equal to a[k] 3. Keep applying operations until we reach the rightmost term in range 1 to K that is not equal to a[k].
Example
#include <iostream> #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; int getMinMoves(int *arr, int n, int k){ int i; for (i = k - 1; i < n; ++i) { if (arr[i] != arr[k - 1]) { return -1; } } for (i = k - 1; i >= 0; --i) { if (arr[i] != arr[k - 1]) { return i + 1; } } return 0; } int main(){ int arr[] = {1, 2, 3, 4, 5, 6}; int k = 6; cout << "Minimum moves required = " << getMinMoves(arr, SIZE(arr), k) << endl; return 0; }
Output
When you compile and execute the above program. It generates the following output −
Minimum moves required = 5
- Related Articles
- Minimum Moves to Equal Array Elements in C++
- Minimum operation to make all elements equal in array in C++
- Minimum number of operations on an array to make all elements 0 using C++.
- Minimum operations required to make all the array elements equal in C++
- Minimum number of given moves required to make N divisible by 25 using C++.
- Minimum operations of given type to make all elements of a matrix equal in C++
- Find the minimum number of preprocess moves required to make two strings equal in Python
- Minimum Moves to Equal Array Elements II in Python
- Minimum number of given operations required to make two strings equal using C++.
- Minimum number of elements to add to make median equals x using C++.
- Minimum number of elements to be removed to make XOR maximum using C++.
- Find the number of operations required to make all array elements Equal in C++
- Number of operations required to make all array elements Equal in Python
- Minimum number of operations required to delete all elements of the array using C++.
- Minimum move to end operations to make all strings equal in C++

Advertisements