Find minimum distance from zero to other elements

Here given code implementation process.

// C Program 
// Find minimum distance from zero to other elements
#include <stdio.h>

#include <limits.h>

//Display elements of given array
void printArray(int arr[], int size)
{
	for (int i = 0; i < size; ++i)
	{
		printf("  %d", arr[i]);
	}
	printf("\n");
}
// Calculates the minimum distance from zero to other elements
void findDistance(int arr[], int size)
{
	if (size <= 1)
	{
		return;
	}
	// Loop controlling variables
	int i = 0;
	int j = 0;
	int k = 0;
	// Used to store the minimum distance
	int distance[size];
	// Set initial distance
	for (i = 0; i < size; ++i)
	{
		if (arr[i] == 0)
		{
			// When get zero element
			distance[i] = 0;
			k++;
		}
		else
		{
			distance[i] = INT_MAX;
		}
	}
	// Display array elements
	printf("  Array Element \n");
	printArray(arr, size);
	if (k == 0)
	{
		// When zero not exist
		printf("\n Zero not exist in this array");
	}
	else
	{
		i = 0;
		while (i < size)
		{
			if (arr[i] == 0)
			{
				j = i - 1;
				k = 1;
				// Update new value on left side of zero
				while (j >= 0 && distance[j] != 0 && distance[j] > k)
				{
					// Change distance
					distance[j] = k;
					k++;
					j--;
				}
				j = i + 1;
				k = 1;
				// Update new value on right side of zero
				while (j < size && distance[j] != 0 && distance[j] > k)
				{
					// Change distance
					distance[j] = k;
					k++;
					j++;
				}
				i = j;
			}
			else
			{
				i++;
			}
		}
		// Display calculated result
		printf("  Calculated distance\n");
		printArray(distance, size);
	}
}
int main()
{
	//Define collection of array elements
	int arr1[] = {
		1 , 4 , 9 , 0 , 2 , 3 , 7 , 4 , 0 , 5 , 7 , 3 , 9
	};
	int arr2[] = {
		1 , 2 , 9 , 0 , 8 , 3 , 7 , 4
	};
	//Get the size of array
	int size = sizeof(arr1) / sizeof(arr1[0]);
	findDistance(arr1, size);
	//Get the size of array
	size = sizeof(arr2) / sizeof(arr2[0]);
	findDistance(arr2, size);
	return 0;
}

Output

  Array Element
  1  4  9  0  2  3  7  4  0  5  7  3  9
  Calculated distance
  3  2  1  0  1  2  2  1  0  1  2  3  4
  Array Element
  1  2  9  0  8  3  7  4
  Calculated distance
  3  2  1  0  1  2  3  4
/*
    Java Program
    Find minimum distance from zero to other elements
*/
public class CalculateDistance
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print("  " + arr[i]);
		}
		System.out.print("\n");
	}
	// Calculates the minimum distance from zero to other elements
	public void findDistance(int[] arr, int size)
	{
		if (size <= 1)
		{
			return;
		}
		// Loop controlling variables
		int i = 0;
		int j = 0;
		int k = 0;
		// Used to store the minimum distance
		int[] distance = new int[size];
		// Set initial distance
		for (i = 0; i < size; ++i)
		{
			if (arr[i] == 0)
			{
				// When get zero element
				distance[i] = 0;
				k++;
			}
			else
			{
				distance[i] = Integer.MAX_VALUE;
			}
		}
		// Display array elements
		System.out.print(" Array Element \n");
		printArray(arr, size);
		if (k == 0)
		{
			// When zero not exist
			System.out.print("\n Zero not exist in this array");
		}
		else
		{
			i = 0;
			while (i < size)
			{
				if (arr[i] == 0)
				{
					j = i - 1;
					k = 1;
					// Update new value on left side of zero
					while (j >= 0 && distance[j] != 0 && distance[j] > k)
					{
						// Change distance
						distance[j] = k;
						k++;
						j--;
					}
					j = i + 1;
					k = 1;
					// Update new value on right side of zero
					while (j < size && distance[j] != 0 && distance[j] > k)
					{
						// Change distance
						distance[j] = k;
						k++;
						j++;
					}
					i = j;
				}
				else
				{
					i++;
				}
			}
			// Display calculated result
			System.out.print(" Calculated distance\n");
			printArray(distance, size);
		}
	}
	public static void main(String[] args)
	{
		CalculateDistance task = new CalculateDistance();
		//Define collection of array elements
		int[] arr1 = {
			1 , 4 , 9 , 0 , 2 , 3 , 7 , 4 , 0 , 5 , 7 , 3 , 9
		};
		int[] arr2 = {
			1 , 2 , 9 , 0 , 8 , 3 , 7 , 4
		};
		//Get the size of array
		int size = arr1.length;
		task.findDistance(arr1, size);
		//Get the size of array
		size = arr2.length;
		task.findDistance(arr2, size);
	}
}

Output

 Array Element
  1  4  9  0  2  3  7  4  0  5  7  3  9
 Calculated distance
  3  2  1  0  1  2  2  1  0  1  2  3  4
 Array Element
  1  2  9  0  8  3  7  4
 Calculated distance
  3  2  1  0  1  2  3  4
// Include header file
#include <iostream>
#include<limits.h>
using namespace std;

/*
    C++ Program
    Find minimum distance from zero to other elements
*/

class CalculateDistance
{
    public:
    //Display elements of given array
    void printArray(int arr[], int size)
    {
        for (int i = 0; i < size; ++i)
        {
            cout << "  " << arr[i];
        }
        cout << "\n";
    }
    // Calculates the minimum distance from zero to other elements
    void findDistance(int arr[], int size)
    {
        if (size <= 1)
        {
            return;
        }
        // Loop controlling variables
        int i = 0;
        int j = 0;
        int k = 0;
        // Used to store the minimum distance
        int distance[size];
        // Set initial distance
        for (i = 0; i < size; ++i)
        {
            if (arr[i] == 0)
            {
                // When get zero element
                distance[i] = 0;
                k++;
            }
            else
            {
                distance[i] = INT_MAX;
            }
        }
        // Display array elements
        cout << " Array Element \n";
        this->printArray(arr, size);
        if (k == 0)
        {
            // When zero not exist
            cout << "\n Zero not exist in this array";
        }
        else
        {
            i = 0;
            while (i < size)
            {
                if (arr[i] == 0)
                {
                    j = i - 1;
                    k = 1;
                    // Update new value on left side of zero
                    while (j >= 0 && distance[j] != 0 && distance[j] > k)
                    {
                        // Change distance
                        distance[j] = k;
                        k++;
                        j--;
                    }
                    j = i + 1;
                    k = 1;
                    // Update new value on right side of zero
                    while (j < size && distance[j] != 0 && distance[j] > k)
                    {
                        // Change distance
                        distance[j] = k;
                        k++;
                        j++;
                    }
                    i = j;
                }
                else
                {
                    i++;
                }
            }
            // Display calculated result
            cout << " Calculated distance\n";
            this->printArray(distance, size);
        }
    }
};
int main()
{
    CalculateDistance task = CalculateDistance();
    //Define collection of array elements
    int arr1[] = {
        1 , 4 , 9 , 0 , 2 , 3 , 7 , 4 , 0 , 5 , 7 , 3 , 9
    };
    int arr2[] = {
        1 , 2 , 9 , 0 , 8 , 3 , 7 , 4
    };
    //Get the size of array
    int size = sizeof(arr1) / sizeof(arr1[0]);
    task.findDistance(arr1, size);
    //Get the size of array
    size = sizeof(arr2) / sizeof(arr2[0]);
    task.findDistance(arr2, size);
    return 0;
}

Output

 Array Element
  1  4  9  0  2  3  7  4  0  5  7  3  9
 Calculated distance
  3  2  1  0  1  2  2  1  0  1  2  3  4
 Array Element
  1  2  9  0  8  3  7  4
 Calculated distance
  3  2  1  0  1  2  3  4
// Include namespace system
using System;
/*
    C# Program
    Find minimum distance from zero to other elements
*/
public class CalculateDistance
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write("  " + arr[i]);
		}
		Console.Write("\n");
	}
	// Calculates the minimum distance from zero to other elements
	public void findDistance(int[] arr, int size)
	{
		if (size <= 1)
		{
			return;
		}
		// Loop controlling variables
		int i = 0;
		int j = 0;
		int k = 0;
		// Used to store the minimum distance
		int[] distance = new int[size];
		// Set initial distance
		for (i = 0; i < size; ++i)
		{
			if (arr[i] == 0)
			{
				// When get zero element
				distance[i] = 0;
				k++;
			}
			else
			{
				distance[i] = int.MaxValue;
			}
		}
		// Display array elements
		Console.Write(" Array Element \n");
		printArray(arr, size);
		if (k == 0)
		{
			// When zero not exist
			Console.Write("\n Zero not exist in this array");
		}
		else
		{
			i = 0;
			while (i < size)
			{
				if (arr[i] == 0)
				{
					j = i - 1;
					k = 1;
					// Update new value on left side of zero
					while (j >= 0 && distance[j] != 0 && distance[j] > k)
					{
						// Change distance
						distance[j] = k;
						k++;
						j--;
					}
					j = i + 1;
					k = 1;
					// Update new value on right side of zero
					while (j < size && distance[j] != 0 && distance[j] > k)
					{
						// Change distance
						distance[j] = k;
						k++;
						j++;
					}
					i = j;
				}
				else
				{
					i++;
				}
			}
			// Display calculated result
			Console.Write(" Calculated distance\n");
			printArray(distance, size);
		}
	}
	public static void Main(String[] args)
	{
		CalculateDistance task = new CalculateDistance();
		//Define collection of array elements
		int[] arr1 = {
			1 , 4 , 9 , 0 , 2 , 3 , 7 , 4 , 0 , 5 , 7 , 3 , 9
		};
		int[] arr2 = {
			1 , 2 , 9 , 0 , 8 , 3 , 7 , 4
		};
		//Get the size of array
		int size = arr1.Length;
		task.findDistance(arr1, size);
		//Get the size of array
		size = arr2.Length;
		task.findDistance(arr2, size);
	}
}

Output

 Array Element
  1  4  9  0  2  3  7  4  0  5  7  3  9
 Calculated distance
  3  2  1  0  1  2  2  1  0  1  2  3  4
 Array Element
  1  2  9  0  8  3  7  4
 Calculated distance
  3  2  1  0  1  2  3  4
<?php
/*
    Php Program
    Find minimum distance from zero to other elements
*/
class CalculateDistance
{
	//Display elements of given array
	public	function printArray( $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo "  ". $arr[$i];
		}
		echo "\n";
	}
	// Calculates the minimum distance from zero to other elements
	public	function findDistance( & $arr, $size)
	{
		if ($size <= 1)
		{
			return;
		}
		// Loop controlling variables
		$i = 0;
		$j = 0;
		$k = 0;
		// Used to store the minimum distance
		$distance = array_fill(0, $size, 0);
		// Set initial distance
		for ($i = 0; $i < $size; ++$i)
		{
			if ($arr[$i] == 0)
			{
				// When get zero element
				$distance[$i] = 0;
				$k++;
			}
			else
			{
				$distance[$i] = PHP_INT_MAX;
			}
		}
		// Display array elements
		echo " Array Element \n";
		$this->printArray($arr, $size);
		if ($k == 0)
		{
			// When zero not exist
			echo "\n Zero not exist in this array";
		}
		else
		{
			$i = 0;
			while ($i < $size)
			{
				if ($arr[$i] == 0)
				{
					$j = $i - 1;
					$k = 1;
					// Update new value on left side of zero
					while ($j >= 0 && $distance[$j] != 0 && $distance[$j] > $k)
					{
						// Change distance
						$distance[$j] = $k;
						$k++;
						$j--;
					}
					$j = $i + 1;
					$k = 1;
					// Update new value on right side of zero
					while ($j < $size && $distance[$j] != 0 && $distance[$j] > $k)
					{
						// Change distance
						$distance[$j] = $k;
						$k++;
						$j++;
					}
					$i = $j;
				}
				else
				{
					$i++;
				}
			}
			// Display calculated result
			echo " Calculated distance\n";
			$this->printArray($distance, $size);
		}
	}
}

function main()
{
	$task = new CalculateDistance();
	//Define collection of array elements
	$arr1 = array(1, 4, 9, 0, 2, 3, 7, 4, 0, 5, 7, 3, 9);
	$arr2 = array(1, 2, 9, 0, 8, 3, 7, 4);
	//Get the size of array
	$size = count($arr1);
	$task->findDistance($arr1, $size);
	//Get the size of array
	$size = count($arr2);
	$task->findDistance($arr2, $size);
}
main();

Output

 Array Element
  1  4  9  0  2  3  7  4  0  5  7  3  9
 Calculated distance
  3  2  1  0  1  2  2  1  0  1  2  3  4
 Array Element
  1  2  9  0  8  3  7  4
 Calculated distance
  3  2  1  0  1  2  3  4
/*
    Node Js Program
    Find minimum distance from zero to other elements
*/
class CalculateDistance
{
	//Display elements of given array
	printArray(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write("  " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// Calculates the minimum distance from zero to other elements
	findDistance(arr, size)
	{
		if (size <= 1)
		{
			return;
		}
		// Loop controlling variables
		var i = 0;
		var j = 0;
		var k = 0;
		// Used to store the minimum distance
		var distance = Array(size).fill(0);
		// Set initial distance
		for (i = 0; i < size; ++i)
		{
			if (arr[i] == 0)
			{
				// When get zero element
				distance[i] = 0;
				k++;
			}
			else
			{
				distance[i] = Number.MAX_VALUE;
			}
		}
		// Display array elements
		process.stdout.write(" Array Element \n");
		this.printArray(arr, size);
		if (k == 0)
		{
			// When zero not exist
			process.stdout.write("\n Zero not exist in this array");
		}
		else
		{
			i = 0;
			while (i < size)
			{
				if (arr[i] == 0)
				{
					j = i - 1;
					k = 1;
					// Update new value on left side of zero
					while (j >= 0 && distance[j] != 0 && distance[j] > k)
					{
						// Change distance
						distance[j] = k;
						k++;
						j--;
					}
					j = i + 1;
					k = 1;
					// Update new value on right side of zero
					while (j < size && distance[j] != 0 && distance[j] > k)
					{
						// Change distance
						distance[j] = k;
						k++;
						j++;
					}
					i = j;
				}
				else
				{
					i++;
				}
			}
			// Display calculated result
			process.stdout.write(" Calculated distance\n");
			this.printArray(distance, size);
		}
	}
}

function main()
{
	var task = new CalculateDistance();
	//Define collection of array elements
	var arr1 = [1, 4, 9, 0, 2, 3, 7, 4, 0, 5, 7, 3, 9];
	var arr2 = [1, 2, 9, 0, 8, 3, 7, 4];
	//Get the size of array
	var size = arr1.length;
	task.findDistance(arr1, size);
	//Get the size of array
	size = arr2.length;
	task.findDistance(arr2, size);
}
main();

Output

 Array Element
  1  4  9  0  2  3  7  4  0  5  7  3  9
 Calculated distance
  3  2  1  0  1  2  2  1  0  1  2  3  4
 Array Element
  1  2  9  0  8  3  7  4
 Calculated distance
  3  2  1  0  1  2  3  4
import sys

#  Python 3 Program
#  Find minimum distance from zero to other elements

class CalculateDistance :
	# Display elements of given array
	def printArray(self, arr, size) :
		i = 0
		while (i < size) :
			print("  ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#  Calculates the minimum distance from zero to other elements
	def findDistance(self, arr, size) :
		if (size <= 1) :
			return
		
		#  Loop controlling variables
		i = 0
		j = 0
		k = 0
		#  Used to store the minimum distance
		distance = [0] * (size)
		#  Set initial distance
		while (i < size) :
			if (arr[i] == 0) :
				#  When get zero element
				distance[i] = 0
				k += 1
			else :
				distance[i] = sys.maxsize
			
			i += 1
		
		#  Display array elements
		print("  Array Element ")
		self.printArray(arr, size)
		if (k == 0) :
			#  When zero not exist
			print("\n Zero not exist in this array", end = "")
		else :
			i = 0
			while (i < size) :
				if (arr[i] == 0) :
					j = i - 1
					k = 1
					#  Update new value on left side of zero
					while (j >= 0 and distance[j] != 0 and distance[j] > k) :
						#  Change distance
						distance[j] = k
						k += 1
						j -= 1
					
					j = i + 1
					k = 1
					#  Update new value on right side of zero
					while (j < size and distance[j] != 0 and distance[j] > k) :
						#  Change distance
						distance[j] = k
						k += 1
						j += 1
					
					i = j
				else :
					i += 1
				
			
			#  Display calculated result
			print("  Calculated distance")
			self.printArray(distance, size)
		
	

def main() :
	task = CalculateDistance()
	# Define collection of array elements
	arr1 = [1, 4, 9, 0, 2, 3, 7, 4, 0, 5, 7, 3, 9]
	arr2 = [1, 2, 9, 0, 8, 3, 7, 4]
	# Get the size of array
	size = len(arr1)
	task.findDistance(arr1, size)
	# Get the size of array
	size = len(arr2)
	task.findDistance(arr2, size)

if __name__ == "__main__": main()

Output

  Array Element
   1   4   9   0   2   3   7   4   0   5   7   3   9
  Calculated distance
   3   2   1   0   1   2   2   1   0   1   2   3   4
  Array Element
   1   2   9   0   8   3   7   4
  Calculated distance
   3   2   1   0   1   2   3   4
#   Ruby Program
#   Find minimum distance from zero to other elements

class CalculateDistance 
	# Display elements of given array
	def printArray(arr, size) 
		i = 0
		while (i < size) 
			print("  ", arr[i])
			i += 1
		end

		print("\n")
	end

	#  Calculates the minimum distance from zero to other elements
	def findDistance(arr, size) 
		if (size <= 1) 
			return
		end

		#  Loop controlling variables
		i = 0
		j = 0
		k = 0
		#  Used to store the minimum distance
		distance = Array.new(size) {0}
		#  Set initial distance
		while (i < size) 
			if (arr[i] == 0) 
				#  When get zero element
				distance[i] = 0
				k += 1
			else 
				distance[i] = (2 ** (0. size * 8 - 2))
			end

			i += 1
		end

		#  Display array elements
		print("  Array Element \n")
		self.printArray(arr, size)
		if (k == 0) 
			#  When zero not exist
			print("\n Zero not exist in this array")
		else 
			i = 0
			while (i < size) 
				if (arr[i] == 0) 
					j = i - 1
					k = 1
					#  Update new value on left side of zero
					while (j >= 0 && distance[j] != 0 && distance[j] > k) 
						#  Change distance
						distance[j] = k
						k += 1
						j -= 1
					end

					j = i + 1
					k = 1
					#  Update new value on right side of zero
					while (j < size && distance[j] != 0 && distance[j] > k) 
						#  Change distance
						distance[j] = k
						k += 1
						j += 1
					end

					i = j
				else 
					i += 1
				end

			end

			#  Display calculated result
			print("  Calculated distance\n")
			self.printArray(distance, size)
		end

	end

end

def main() 
	task = CalculateDistance.new()
	# Define collection of array elements
	arr1 = [1, 4, 9, 0, 2, 3, 7, 4, 0, 5, 7, 3, 9]
	arr2 = [1, 2, 9, 0, 8, 3, 7, 4]
	# Get the size of array
	size = arr1.length
	task.findDistance(arr1, size)
	# Get the size of array
	size = arr2.length
	task.findDistance(arr2, size)
end

main()

Output

  Array Element 
  1  4  9  0  2  3  7  4  0  5  7  3  9
  Calculated distance
  3  2  1  0  1  2  2  1  0  1  2  3  4
  Array Element 
  1  2  9  0  8  3  7  4
  Calculated distance
  3  2  1  0  1  2  3  4
/*
    Scala Program
    Find minimum distance from zero to other elements
*/
class CalculateDistance
{
	//Display elements of given array
	def printArray(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print("  " + arr(i));
			i += 1;
		}
		print("\n");
	}
	// Calculates the minimum distance from zero to other elements
	def findDistance(arr: Array[Int], size: Int): Unit = {
		if (size <= 1)
		{
			return;
		}
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		var k: Int = 0;
		// Used to store the minimum distance
		var distance: Array[Int] = Array.fill[Int](size)(0);
		// Set initial distance
		while (i < size)
		{
			if (arr(i) == 0)
			{
				// When get zero element
				distance(i) = 0;
				k += 1;
			}
			else
			{
				distance(i) = Int.MaxValue;
			}
			i += 1;
		}
		// Display array elements
		print("  Array Element \n");
		this.printArray(arr, size);
		if (k == 0)
		{
			// When zero not exist
			print("\n Zero not exist in this array");
		}
		else
		{
			i = 0;
			while (i < size)
			{
				if (arr(i) == 0)
				{
					j = i - 1;
					k = 1;
					// Update new value on left side of zero
					while (j >= 0 && distance(j) != 0 && distance(j) > k)
					{
						// Change distance
						distance(j) = k;
						k += 1;
						j -= 1;
					}
					j = i + 1;
					k = 1;
					// Update new value on right side of zero
					while (j < size && distance(j) != 0 && distance(j) > k)
					{
						// Change distance
						distance(j) = k;
						k += 1;
						j += 1;
					}
					i = j;
				}
				else
				{
					i += 1;
				}
			}
			// Display calculated result
			print("  Calculated distance\n");
			this.printArray(distance, size);
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: CalculateDistance = new CalculateDistance();
		//Define collection of array elements
		var arr1: Array[Int] = Array(1, 4, 9, 0, 2, 3, 7, 4, 0, 5, 7, 3, 9);
		var arr2: Array[Int] = Array(1, 2, 9, 0, 8, 3, 7, 4);
		//Get the size of array
		var size: Int = arr1.length;
		task.findDistance(arr1, size);
		//Get the size of array
		size = arr2.length;
		task.findDistance(arr2, size);
	}
}

Output

  Array Element
  1  4  9  0  2  3  7  4  0  5  7  3  9
  Calculated distance
  3  2  1  0  1  2  2  1  0  1  2  3  4
  Array Element
  1  2  9  0  8  3  7  4
  Calculated distance
  3  2  1  0  1  2  3  4
/*
    Swift 4 Program
    Find minimum distance from zero to other elements
*/
class CalculateDistance
{
	//Display elements of given array
	func printArray(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print("  ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Calculates the minimum distance from zero to other elements
	func findDistance(_ arr: [Int], _ size: Int)
	{
		if (size <= 1)
		{
			return;
		}
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		var k: Int = 0;
		// Used to store the minimum distance
		var distance: [Int] = Array(repeating: 0, count: size);
		// Set initial distance
		while (i < size)
		{
			if (arr[i] == 0)
			{
				// When get zero element
				distance[i] = 0;
				k += 1;
			}
			else
			{
				distance[i] = Int.max;
			}
			i += 1;
		}
		// Display array elements
		print("  Array Element ");
		self.printArray(arr, size);
		if (k == 0)
		{
			// When zero not exist
			print("\n Zero not exist in this array", terminator: "");
		}
		else
		{
			i = 0;
			while (i < size)
			{
				if (arr[i] == 0)
				{
					j = i - 1;
					k = 1;
					// Update new value on left side of zero
					while (j >= 0 && distance[j] != 0 && distance[j] > k)
					{
						// Change distance
						distance[j] = k;
						k += 1;
						j -= 1;
					}
					j = i + 1;
					k = 1;
					// Update new value on right side of zero
					while (j < size && distance[j] != 0 && distance[j] > k)
					{
						// Change distance
						distance[j] = k;
						k += 1;
						j += 1;
					}
					i = j;
				}
				else
				{
					i += 1;
				}
			}
			// Display calculated result
			print("  Calculated distance");
			self.printArray(distance, size);
		}
	}
}
func main()
{
	let task: CalculateDistance = CalculateDistance();
	//Define collection of array elements
	let arr1: [Int] = [1, 4, 9, 0, 2, 3, 7, 4, 0, 5, 7, 3, 9];
	let arr2: [Int] = [1, 2, 9, 0, 8, 3, 7, 4];
	//Get the size of array
	var size: Int = arr1.count;
	task.findDistance(arr1, size);
	//Get the size of array
	size = arr2.count;
	task.findDistance(arr2, size);
}
main();

Output

  Array Element
   1   4   9   0   2   3   7   4   0   5   7   3   9
  Calculated distance
   3   2   1   0   1   2   2   1   0   1   2   3   4
  Array Element
   1   2   9   0   8   3   7   4
  Calculated distance
   3   2   1   0   1   2   3   4
/*
    Kotlin Program
    Find minimum distance from zero to other elements
*/
class CalculateDistance
{
	//Display elements of given array
	fun printArray(arr: Array<Int>, size: Int): Unit
	{
		var i: Int = 0;
		while (i<size)
		{
			print("  " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	// Calculates the minimum distance from zero to other elements
	fun findDistance(arr: Array<Int>, size: Int): Unit
	{
		if (size <= 1)
		{
			return;
		}
		// Loop controlling variables
		var i: Int = 0;
		var j: Int ;
		var k: Int = 0;
		// Used to store the minimum distance
		var distance: Array<Int> = Array(size){0};
		// Set initial distance
		while (i < size)
		{
			if (arr[i] == 0)
			{
				// When get zero element
				distance[i] = 0;
				k += 1;
			}
			else
			{
				distance[i] = Int.MAX_VALUE;
			}
			i += 1;
		}
		// Display array elements
		print("  Array Element \n");
		this.printArray(arr, size);
		if (k == 0)
		{
			// When zero not exist
			print("\n Zero not exist in this array");
		}
		else
		{
			i = 0;
			while (i<size)
			{
				if (arr[i] == 0)
				{
					j = i - 1;
					k = 1;
					// Update new value on left side of zero
					while (j>= 0 && distance[j] != 0 && distance[j]>k)
					{
						// Change distance
						distance[j] = k;
						k += 1;
						j -= 1;
					}
					j = i + 1;
					k = 1;
					// Update new value on right side of zero
					while (j<size && distance[j] != 0 && distance[j]>k)
					{
						// Change distance
						distance[j] = k;
						k += 1;
						j += 1;
					}
					i = j;
				}
				else
				{
					i += 1;
				}
			}
			// Display calculated result
			print("  Calculated distance\n");
			this.printArray(distance, size);
		}
	}
}
fun main(args: Array<String>): Unit
{
	var task: CalculateDistance = CalculateDistance();
	//Define collection of array elements
	var arr1: Array<Int> = arrayOf(1, 4, 9, 0, 2, 3, 7, 4, 0, 5, 7, 3, 9);
	var arr2: Array<Int> = arrayOf(1, 2, 9, 0, 8, 3, 7, 4);
	//Get the size of array
	var size: Int = arr1.count();
	task.findDistance(arr1, size);
	//Get the size of array
	size = arr2.count();
	task.findDistance(arr2, size);
}

Output

  Array Element
  1  4  9  0  2  3  7  4  0  5  7  3  9
  Calculated distance
  3  2  1  0  1  2  2  1  0  1  2  3  4
  Array Element
  1  2  9  0  8  3  7  4
  Calculated distance
  3  2  1  0  1  2  3  4


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved