Count of iterations to make minimum as 0 by rotating Array followed by reducing it from original Array
Given an array arr[]. The task is to find the number of iterations required to make the minimum element in the array as 0. In one iteration, left rotate the array by one and subtract the corresponding element of the original array and rotated array.
Examples:
Input: arr[] = { 2, 6, 3, 4, 8, 7 }
Output: 3
Explanation: Refer to the image below for explanation.
Input: arr[] = { 4, 10, 12, 3, 9, 7 }
Output: 5
Naive Approach: The easiest way to solve this problem is by using the Greedy Approach.
- Simply pop the first element of the array and append it to the end and then perform the subtraction on the corresponding element.
- Similarly, perform the same operation on the resultant array till we get minimum element in an array as zero, and return the count of iteration.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
int minZero(vector< int >& A, int n)
{
int c = 0;
if (*min_element(A.begin(), A.end()) == 0)
return c;
while (*min_element(A.begin(), A.end()) != 0) {
vector< int > A1 = A;
int x = A[0];
A.erase(A.begin());
A.push_back(x);
for ( int i = 0; i < n; i++)
A[i] = abs (A[i] - A1[i]);
c += 1;
}
return c;
}
int main()
{
vector< int > arr = { 2, 6, 3, 4, 8, 7 };
int x = minZero(arr, arr.size());
cout << (x);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minZero(Vector<Integer> A, int n)
{
int c = 0 ;
if (Collections.min(A) == 0 )
return c;
while (Collections.min(A) != 0 ) {
Vector<Integer> A1 = (Vector<Integer>) A.clone();
int x = A.get( 0 );
A.remove( 0 );
A.add(x);
for ( int i = 0 ; i < n; i++)
A.set(i, Math.abs(A.get(i) - A1.get(i)));
c += 1 ;
}
return c;
}
public static void main(String[] args)
{
Integer []arr = { 2 , 6 , 3 , 4 , 8 , 7 };
Vector<Integer> v = new Vector<Integer>();
Collections.addAll(v, arr);
int x = minZero(v, arr.length);
System.out.print(x);
}
}
|
Python3
def minZero(A, n):
c = 0
if min (A) = = 0 :
return c
while min (A) ! = 0 :
A1 = A[:]
x = A.pop( 0 )
A.append(x)
for i in range (n):
A[i] = abs (A[i] - A1[i])
c + = 1
return c
arr = [ 2 , 6 , 3 , 4 , 8 , 7 ]
x = minZero(arr, len (arr))
print (x)
|
C#
using System;
using System.Linq;
class GFG{
static int minZero( int []A, int n)
{
int c = 0;
if (A.Min()== 0)
return c;
while (A.Min() != 0) {
int x = ( int )A[0];
for ( int i = 0; i < (n - 1); i++) {
A[i] = Math.Abs(( int )A[i] - ( int )A[i + 1]);
}
A[n - 1] = Math.Abs(( int )A[n - 1] - x);
c += 1;
}
return c;
}
public static void Main()
{
int []arr = { 2, 6, 3, 4, 8, 7 };
int N = arr.Length;
int x = minZero(arr, N);
Console.Write(x);
}
}
|
Javascript
<script>
function minZero(A, n){
let c = 0
let _A = [...A].sort((a, b) => a - b);
if ([...A].sort((a, b) => a - b)[0] == 0)
return c
while ([...A].sort((a, b) => a - b)[0] != 0){
let A1 = [...A]
let x = A[0];
A.shift();
A.push(x)
for (let i = 0; i < n; i++)
A[i] = Math.abs(A[i]-A1[i])
c += 1
}
return c
}
let arr = [2, 6, 3, 4, 8, 7]
let x = minZero(arr, arr.length)
document.write(x)
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Efficient Approach: A space-optimized approach is a logic and implementation-based. Follow the steps below to solve the given problem.
- Store the first element of array in variable x.
- Now find absolute difference between consecutive elements.
- Replace result from index 0.
- Subtract last element from variable x and store it.
- count the iteration and repeat the steps.
- Return count as the final answer.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
int minZero( int A[], int n)
{
int c = 0;
if (*min_element(A + 0, A + n - 1) == 0)
return c;
while (*min_element(A + 0, A + n - 1) != 0) {
int x = A[0];
for ( int i = 0; i < (n - 1); i++) {
A[i] = abs (A[i] - A[i + 1]);
}
A[n - 1] = abs (A[n - 1] - x);
c += 1;
}
return c;
}
int main()
{
int arr[] = { 2, 6, 3, 4, 8, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
int x = minZero(arr, N);
cout << (x);
}
|
Java
import java.util.*;
class GFG{
static int minZero( int A[], int n)
{
int c = 0 ;
if (Arrays.stream(A).min().getAsInt()== 0 )
return c;
while (Arrays.stream(A).min().getAsInt() != 0 ) {
int x = A[ 0 ];
for ( int i = 0 ; i < (n - 1 ); i++) {
A[i] = Math.abs(A[i] - A[i + 1 ]);
}
A[n - 1 ] = Math.abs(A[n - 1 ] - x);
c += 1 ;
}
return c;
}
public static void main(String[] args)
{
int arr[] = { 2 , 6 , 3 , 4 , 8 , 7 };
int N = arr.length;
int x = minZero(arr, N);
System.out.print(x);
}
}
|
Python3
def minZero(A, n):
c = 0
if min (A) = = 0 :
return c
while min (A) ! = 0 :
x = A[ 0 ]
for i in range (n - 1 ):
A[i] = abs (A[i] - A[i + 1 ])
A[n - 1 ] = abs (A[n - 1 ] - x)
c + = 1
return c
arr = [ 2 , 6 , 3 , 4 , 8 , 7 ]
N = len (arr)
x = minZero(arr, N)
print (x)
|
C#
using System;
using System.Linq;
class GFG{
static int minZero( int []A, int n)
{
int c = 0;
if (A.Min()== 0)
return c;
while (A.Min() != 0) {
int x = ( int )A[0];
for ( int i = 0; i < (n - 1); i++) {
A[i] = Math.Abs(( int )A[i] - ( int )A[i + 1]);
}
A[n - 1] = Math.Abs(( int )A[n - 1] - x);
c += 1;
}
return c;
}
public static void Main()
{
int []arr = { 2, 6, 3, 4, 8, 7 };
int N = arr.Length;
int x = minZero(arr, N);
Console.Write(x);
}
}
|
Javascript
<script>
function minZero(A, n) {
let c = 0
if (Math.min(...A) == 0)
return c
while (Math.min(...A) != 0) {
let x = A[0]
for (let i = 0; i < n - 1; i++) {
A[i] = Math.abs(A[i] - A[i + 1])
}
A[n - 1] = Math.abs(A[n - 1] - x)
c += 1
}
return c
}
let arr = [2, 6, 3, 4, 8, 7]
let N = arr.length
let x = minZero(arr, N)
document.write(x)
</script>
|
PHP
<?php
function minZero( $A , $n )
{
$c = 0;
if (min( $A ) == 0)
{
return $c ;
}
while (min( $A ) != 0)
{
$x = $A [0];
for ( $i = 0; $i < $n - 1; $i ++)
{
$A [ $i ] = abs ( $A [ $i ] - $A [ $i +1]);
}
$A [ $n - 1] = abs ( $A [ $n - 1] - $x );
$c += 1;
}
return $c ;
}
$arr = array (2, 6, 3, 4, 8, 7);
$N = count ( $arr );
$x = minZero( $arr , $N );
echo ( $x );
?>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
02 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...