# Minimum operations required to make all the array elements equal

Given an array arr[] of n integer and an integer k. The task is to count the minimum number of times the given operation is required to make all the array elements equal. In a single operation, the kth element of the array is appended at the end of the array and the first element of the array gets deleted (the size of the array remains same). If the array elements cannot be made equal with this operation then print -1 else print the count of minimum operations required.

Examples:

Input: arr[] = {2, 1, 1, 1, 1}, k = 3
Output: 1
Applying the operation 1st time
3rd element in the array is 1 we append it to the end of the array and get arr[] = {2, 1, 1, 1, 1, 1}
then we delete the 1st element and get arr[] = {1, 1, 1, 1, 1}

Input: arr[] = {1, 2, 3, 4}, k = 3
Output: -1

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: At each operation at first the kth element is copied to the end then the (k + 1)th element from the initial sequence is copied, then (k + 2)th and so on. So all the elements will become equal if and only if all the elements in the array starting from the kth element are equal. It’s now also obvious that the number of operations needed for it is equal to the index of the last number that is not equal to the nth element of the initial sequence

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the above approach  ` `#include ` ` `  `using` `namespace` `std; ` ` `  `    ``// Function to return the minimum number of  ` `    ``// given operation required to make all the  ` `    ``// array elements equal  ` `    ``void` `minOperation(``int` `n, ``int` `k, ``int` `a[])  ` `    ``{  ` `         `  `        ``// Check if all the elements  ` `        ``// from kth index to last are equal  ` `        ``for` `(``int` `i = k; i < n; i++)  ` `        ``{  ` `            ``if``(a[i] != a[k - 1])  ` `                ``cout << (-1)< -1; i--)  ` `        ``{  ` `            ``if``(a[i] != a[k - 1])  ` `                ``cout << (i + 1) << endl;  ` `        ``}  ` `    ``}  ` ` `  `    ``// Driver code  ` `    ``int` `main ()  ` `    ``{ ` `        ``int` `n = 5;  ` `        ``int` `k = 3;  ` `        ``int` `a[] = {2, 1, 1, 1, 1};  ` `         `  `        ``minOperation(n, k, a);  ` `    ``} ` ` `  `// This code is contributed by ` `// Surendra_Gangwar `

## Java

 `// Java implementation of the above approach  ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `         `  `    ``// Function to return the minimum number of  ` `    ``// given operation required to make all the  ` `    ``// array elements equal  ` `    ``static` `void` `minOperation(``int` `n, ``int` `k, ``int` `a[])  ` `    ``{  ` `         `  `        ``// Check if all the elements  ` `        ``// from kth index to last are equal  ` `        ``for` `(``int` `i = k; i < n; i++)  ` `        ``{  ` `            ``if``(a[i] != a[k - ``1``])  ` `                ``System.out.println(-``1``);  ` `        ``}  ` `         `  `        ``// Finding the 1st element which is  ` `        ``// not equal to the kth element  ` `        ``for` `(``int` `i = k - ``2``; i > -``1``; i--)  ` `        ``{  ` `            ``if``(a[i] != a[k - ``1``])  ` `                ``System.out.println(i + ``1``);  ` `        ``}  ` `    ``}  ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `     `  `        ``int` `n = ``5``;  ` `        ``int` `k = ``3``;  ` `        ``int` `a[] = {``2``, ``1``, ``1``, ``1``, ``1``};  ` `         `  `        ``minOperation(n, k, a);  ` `    ``} ` `} ` ` `  `// This code is contributed by ajit. `

## Python

 `# Python3 implementation of the approach ` ` `  `# Function to return the minimum number of given operation ` `# required to make all the array elements equal ` `def` `minOperation(n, k, a): ` `     `  `    ``# Check if all the elements  ` `    ``# from kth index to last are equal ` `    ``for` `i ``in` `range``(k, n): ` `        ``if``(a[i] !``=` `a[k ``-` `1``]): ` `            ``return` `-``1` `             `  `    ``# Finding the 1st element  ` `    ``# which is not equal to the kth element ` `    ``for` `i ``in` `range``(k``-``2``, ``-``1``, ``-``1``): ` `        ``if``(a[i] !``=` `a[k``-``1``]): ` `            ``return` `i ``+` `1` `             `  `# Driver code ` `n ``=` `5` `k ``=` `3` `a ``=` `[``2``, ``1``, ``1``, ``1``, ``1``] ` `print``(minOperation(n, k, a)) `

## C#

 `// C# implementation of the above approach  ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `    ``// Function to return the minimum number of  ` `    ``// given operation required to make all the  ` `    ``// array elements equal  ` `    ``static` `void` `minOperation(``int` `n, ``int` `k, ``int` `[]a)  ` `    ``{  ` `         `  `        ``// Check if all the elements  ` `        ``// from kth index to last are equal  ` `        ``for` `(``int` `i = k; i < n; i++)  ` `        ``{  ` `            ``if``(a[i] != a[k - 1])  ` `                ``Console.WriteLine(-1);  ` `             `  `        ``}  ` `         `  `        ``// Finding the 1st element which is  ` `        ``// not equal to the kth element  ` `        ``for` `(``int` `i = k - 2; i > -1; i--)  ` `        ``{  ` `            ``if``(a[i] != a[k - 1])  ` `                ``Console.WriteLine(i + 1);  ` `        ``}  ` `    ``}  ` ` `  `    ``// Driver code  ` `    ``static` `public` `void` `Main () ` `    ``{ ` `        ``int` `n = 5;  ` `        ``int` `k = 3;  ` `        ``int` `[]a = {2, 1, 1, 1, 1};  ` `         `  `        ``minOperation(n, k, a);  ` `    ``} ` `} ` ` `  `// This code is contributed by Ryuga `

## PHP

 ` -1; ``\$i``--) ` `    ``{ ` `        ``if``(``\$a``[``\$i``] != ``\$a``[``\$k` `- 1]) ` `            ``return` `(``\$i` `+ 1); ` `    ``} ` `} ` ` `  `// Driver code ` `\$n` `= 5; ` `\$k` `= 3; ` `\$a` `= ``array``(2, 1, 1, 1, 1); ` `echo``(minOperation(``\$n``, ``\$k``, ``\$a``)); ` ` `  `// This code is contributed ` `// by Shivi_Aggarwal ` `?> `

Output:

```1
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
{"ft_lang_label":"__label__en","ft_lang_prob":0.673654,"math_prob":0.9906537,"size":6184,"snap":"2020-10-2020-16","text_gpt3_token_len":1872,"char_repetition_ratio":0.19805825,"word_repetition_ratio":0.3654147,"special_character_ratio":0.32761967,"punctuation_ratio":0.12841092,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9990903,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-02-26T22:43:29Z\",\"WARC-Record-ID\":\"<urn:uuid:de9d4754-412f-49b4-8f48-efdc68977efe>\",\"Content-Length\":\"170341\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:28fdb0b3-1d02-45f5-82e2-4068d8d4d5bd>\",\"WARC-Concurrent-To\":\"<urn:uuid:b4c7321b-3cf3-48b8-a8ad-8a977b62594f>\",\"WARC-IP-Address\":\"23.45.181.178\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/minimum-operations-required-to-make-all-the-array-elements-equal/\",\"WARC-Payload-Digest\":\"sha1:HOSGZWIZEKHGGD6LLEEDTBS2FL43S4R3\",\"WARC-Block-Digest\":\"sha1:SYYUGGGLHVLGGVW4REYESYXERK2HPDOJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-10/CC-MAIN-2020-10_segments_1581875146562.94_warc_CC-MAIN-20200226211749-20200227001749-00513.warc.gz\"}"}