Skip to main content

Find largest multiple of 3 in array of digits

Code Solution

/*
    C Program 
    Find largest multiple of 3 in array of digits
*/
#include <stdio.h>

// Print result
void printResult(int result[])
{
    int i = 0;
    int j = 0;

    for (j = 9; j >= 0; --j)
    {
        i = result[j];

        while(i > 0)
        {
            printf("%d",j);
            i--;
        }
    }
    printf("\n");
}
// Handles the request of find the largest multiple of 3
void multipleOfThree(int data[],int n)
{

    int result[10];
    int sum = 0;
    int i = 0;

    // Set initial count digit
    for (i = 0; i < 10; ++i)
    {
        result[i] = 0;
    }

    for (i = 0; i < n; ++i)
    {
        if(data[i] > 9 || data[i] < 0)
        {
            // When array not contain valid digit
            return ;
        }
        sum += data[i];
        result[data[i]]++;
    }

   
    if(sum % 3 == 0)
    {
        // All elements of array is part of result
        printResult(result);
        return;
    }
    else if(sum % 3 == 1)
    {
        // When need to remove single digit which is divisible by 3 and remainder is 1
        
        for (i = 1; i < 10; ++i)
        {
            if(result[i] > 0 && (result[i] % 3) == 1 )
            {
                // Remove smallest
                result[i]--;
                printResult(result);
                return;
            }
        }

    }
    else
    {
        // When remender is 2

        int p1 = -1;
        int p2 = -1;

        for (i = 1; i < 10; ++i)
        {
            if( result[i] > 0 && i % 3 == 1 )
            {
               
                if(p1 == -1)
                {
                    p1 = i;
                }
                else if(p2==-1)
                {
                    p2 = i;
                }
            }
            else if(result[i] > 0 && i % 3 == 2)
            {
                // We get a single digit which reminder is 2 when it's divided by 3
                result[i]--;
                printResult(result);
                return;
            }
        }

        if(p1 != -1 && p2 != -1)
        {
            // Remove 2 smallest
            result[p1]--;

            if(result[p1] > 0)
            {
                // When p1 are exist
               result[p1]--; 
            }
            else
            {
                result[p2]--; 
            }
           
            printResult(result);
            return;
        }
    }
    // When array sum is not multiple of 3
    printf("None\n");
}

int main(int argc, char const *argv[])
{ 
    // Define arrays of positive digits
    int data1[] = {7,0,1,2,8,1,1,6} ; 
    int data2[] = {1,1,0,0,0} ; 
    int data3[] = {9, 2, 4, 8, 3, 0, 0, 0, 0, 1, 4,3,2,3,2,3,6,2,4,6,5,2,3,5,2,4,1,0,0,0,0,0} ; 

    // Get the size of data1
    int n = sizeof(data1)/sizeof(data1[0]); 

    multipleOfThree(data1,n);

    // Get the size of data2
    n = sizeof(data2)/sizeof(data2[0]); 
    
    multipleOfThree(data2,n);

    // Get the size of data3
    n = sizeof(data3)/sizeof(data3[0]); 
    
    multipleOfThree(data3,n);

    return 0;
}

Output

8761110
None
98665544443333322222211000000000
/*
  Java Program for
  Find largest multiple of 3 in array of digits
*/
class Multiplier
{
    // Print result
    public void printResult(int[] result)
    {
        int i = 0;
        int j = 0;
        for (j = 9; j >= 0; --j)
        {
            i = result[j];
            while (i > 0)
            {
                System.out.print(j);
                i--;
            }
        }
        System.out.print("\n");
    }
    // Handles the request of find the largest multiple of 3
    public void multipleOfThree(int[] data, int n)
    {
        int[] result = new int[10];
        int sum = 0;
        int i = 0;
        // Set initial count digit
        for (i = 0; i < 10; ++i)
        {
            result[i] = 0;
        }
        for (i = 0; i < n; ++i)
        {
            if (data[i] > 9 || data[i] < 0)
            {
                // When array not contain valid digit
                return;
            }
            sum += data[i];
            result[data[i]]++;
        }
        if (sum % 3 == 0)
        {
            // All elements of array is part of result
            printResult(result);
            return;
        }
        else if (sum % 3 == 1)
        {
            // When need to remove single digit which is divisible by 3 and remainder is 1
            for (i = 1; i < 10; ++i)
            {
                if (result[i] > 0 && (result[i] % 3) == 1)
                {
                    // Remove smallest
                    result[i]--;
                    printResult(result);
                    return;
                }
            }
        }
        else
        {
            // When remender is 2
            int p1 = -1;
            int p2 = -1;
            for (i = 1; i < 10; ++i)
            {
                if (result[i] > 0 && i % 3 == 1)
                {
                    if (p1 == -1)
                    {
                        p1 = i;
                    }
                    else if (p2 == -1)
                    {
                        p2 = i;
                    }
                }
                else if (result[i] > 0 && i % 3 == 2)
                {
                    // We get a single digit which reminder is 2 when it's divided by 3
                    result[i]--;
                    printResult(result);
                    return;
                }
            }
            if (p1 != -1 && p2 != -1)
            {
                // Remove 2 smallest
                result[p1]--;
                if (result[p1] > 0)
                {
                    // When p1 are exist
                    result[p1]--;
                }
                else
                {
                    result[p2]--;
                }
                printResult(result);
                return;
            }
        }
        // When array sum is not multiple of 3
        System.out.print("None\n");
    }
    public static void main(String[] args)
    {
        Multiplier task = new Multiplier();
        // Define arrays of positive digits
        int[] data1 = {
            7 , 0 , 1 , 2 , 8 , 1 , 1 , 6
        };
        int[] data2 = {
            1 , 1 , 0 , 0 , 0
        };
        int[] data3 = {
            9 , 2 , 4 , 8 , 3 , 0 , 0 , 0 , 0 , 1 , 4 , 3 , 2 , 3 , 2 , 
            3 , 6 , 2 , 4 , 6 , 5 , 2 , 3 , 5 , 2 , 4 , 1 , 0 , 0 , 0 , 0 , 0
        };
        // Get the size of data1
        int n = data1.length;
        task.multipleOfThree(data1, n);
        // Get the size of data2
        n = data2.length;
        task.multipleOfThree(data2, n);
        // Get the size of data3
        n = data3.length;
        task.multipleOfThree(data3, n);
    }
}

Output

8761110
None
98665544443333322222211000000000
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Find largest multiple of 3 in array of digits
*/
class Multiplier
{
	public:
		// Print result
		void printResult(int result[])
		{
			int i = 0;
			int j = 0;
			for (j = 9; j >= 0; --j)
			{
				i = result[j];
				while (i > 0)
				{
					cout << j;
					i--;
				}
			}
			cout << "\n";
		}
	// Handles the request of find the largest multiple of 3
	void multipleOfThree(int data[], int n)
	{
		int result[10];
		int sum = 0;
		int i = 0;
		// Set initial count digit
		for (i = 0; i < 10; ++i)
		{
			result[i] = 0;
		}
		for (i = 0; i < n; ++i)
		{
			// When array not contain valid digit
			if (data[i] > 9 || data[i] < 0)
			{
				return;
			}
			sum += data[i];
			result[data[i]]++;
		}
		if (sum % 3 == 0)
		{
			// All elements of array is part of result
			this->printResult(result);
			return;
		}
		else if (sum % 3 == 1)
		{
			// When need to remove single digit which is divisible by 3 and remainder is 1
			for (i = 1; i < 10; ++i)
			{
				if (result[i] > 0 && (result[i] % 3) == 1)
				{
					// Remove smallest
					result[i]--;
					this->printResult(result);
					return;
				}
			}
		}
		else
		{
			// When remender is 2
			int p1 = -1;
			int p2 = -1;
			for (i = 1; i < 10; ++i)
			{
				if (result[i] > 0 && i % 3 == 1)
				{
					if (p1 == -1)
					{
						p1 = i;
					}
					else if (p2 == -1)
					{
						p2 = i;
					}
				}
				else if (result[i] > 0 && i % 3 == 2)
				{
					// We get a single digit which reminder is 2 when it's divided by 3
					result[i]--;
					this->printResult(result);
					return;
				}
			}
			if (p1 != -1 && p2 != -1)
			{
				// Remove 2 smallest
				result[p1]--;
				if (result[p1] > 0)
				{
					// When p1 are exist
					result[p1]--;
				}
				else
				{
					result[p2]--;
				}
				this->printResult(result);
				return;
			}
		}
		// When array sum is not multiple of 3
		cout << "None\n";
	}
};
int main()
{
	Multiplier task = Multiplier();
	// Define arrays of positive digits
	int data1[] = {
		7 , 0 , 1 , 2 , 8 , 1 , 1 , 6
	};
	int data2[] = {
		1 , 1 , 0 , 0 , 0
	};
	int data3[] = {
		9 , 2 , 4 , 8 , 3 , 0 , 0 , 0 , 0 , 1 , 4 , 3 , 2 , 3 , 2 , 3 , 
        6 , 2 , 4 , 6 , 5 , 2 , 3 , 5 , 2 , 4 , 1 , 0 , 0 , 0 , 0 , 0
	};
	// Get the size of data1
	int n = sizeof(data1) / sizeof(data1[0]);
	task.multipleOfThree(data1, n);
	// Get the size of data2
	n = sizeof(data2) / sizeof(data2[0]);
	task.multipleOfThree(data2, n);
	// Get the size of data3
	n = sizeof(data3) / sizeof(data3[0]);
	task.multipleOfThree(data3, n);
	return 0;
}

Output

8761110
None
98665544443333322222211000000000
// Include namespace system
using System;
/*
  C# Program for
  Find largest multiple of 3 in array of digits
*/
public class Multiplier
{
	// Print result
	public void printResult(int[] result)
	{
		int i = 0;
		int j = 0;
		for (j = 9; j >= 0; --j)
		{
			i = result[j];
			while (i > 0)
			{
				Console.Write(j);
				i--;
			}
		}
		Console.Write("\n");
	}
	// Handles the request of find the largest multiple of 3
	public void multipleOfThree(int[] data, int n)
	{
		int[] result = new int[10];
		int sum = 0;
		int i = 0;
		// Set initial count digit
		for (i = 0; i < 10; ++i)
		{
			result[i] = 0;
		}
		for (i = 0; i < n; ++i)
		{
			// When array not contain valid digit
			if (data[i] > 9 || data[i] < 0)
			{
				return;
			}
			sum += data[i];
			result[data[i]]++;
		}
		if (sum % 3 == 0)
		{
			// All elements of array is part of result
			printResult(result);
			return;
		}
		else if (sum % 3 == 1)
		{
			// When need to remove single digit which is divisible by 3 and remainder is 1
			for (i = 1; i < 10; ++i)
			{
				if (result[i] > 0 && (result[i] % 3) == 1)
				{
					// Remove smallest
					result[i]--;
					printResult(result);
					return;
				}
			}
		}
		else
		{
			// When remender is 2
			int p1 = -1;
			int p2 = -1;
			for (i = 1; i < 10; ++i)
			{
				if (result[i] > 0 && i % 3 == 1)
				{
					if (p1 == -1)
					{
						p1 = i;
					}
					else if (p2 == -1)
					{
						p2 = i;
					}
				}
				else if (result[i] > 0 && i % 3 == 2)
				{
					// We get a single digit which reminder is 2 when it's divided by 3
					result[i]--;
					printResult(result);
					return;
				}
			}
			if (p1 != -1 && p2 != -1)
			{
				// Remove 2 smallest
				result[p1]--;
				if (result[p1] > 0)
				{
					// When p1 are exist
					result[p1]--;
				}
				else
				{
					result[p2]--;
				}
				printResult(result);
				return;
			}
		}
		// When array sum is not multiple of 3
		Console.Write("None\n");
	}
	public static void Main(String[] args)
	{
		Multiplier task = new Multiplier();
		// Define arrays of positive digits
		int[] data1 = {
			7 , 0 , 1 , 2 , 8 , 1 , 1 , 6
		};
		int[] data2 = {
			1 , 1 , 0 , 0 , 0
		};
		int[] data3 = {
			9 , 2 , 4 , 8 , 3 , 0 , 0 , 0 , 0 , 1 , 4 , 3 , 2 , 3 , 2 , 3 , 6 , 
            2 , 4 , 6 , 5 , 2 , 3 , 5 , 2 , 4 , 1 , 0 , 0 , 0 , 0 , 0
		};
		// Get the size of data1
		int n = data1.Length;
		task.multipleOfThree(data1, n);
		// Get the size of data2
		n = data2.Length;
		task.multipleOfThree(data2, n);
		// Get the size of data3
		n = data3.Length;
		task.multipleOfThree(data3, n);
	}
}

Output

8761110
None
98665544443333322222211000000000
<?php
/*
  Php Program for
  Find largest multiple of 3 in array of digits
*/
class Multiplier
{
	// Print result
	public	function printResult( & $result)
	{
		$i = 0;
		$j = 0;
		for ($j = 9; $j >= 0; --$j)
		{
			$i = $result[$j];
			while ($i > 0)
			{
				echo $j;
				$i--;
			}
		}
		echo "\n";
	}
	// Handles the request of find the largest multiple of 3
	public	function multipleOfThree( & $data, $n)
	{
		$result = array_fill(0, 10, 0);
		$sum = 0;
		$i = 0;
		for ($i = 0; $i < $n; ++$i)
		{
			// When array not contain valid digit
			if ($data[$i] > 9 || $data[$i] < 0)
			{
				return;
			}
			$sum += $data[$i];
			$result[$data[$i]]++;
		}
		if ($sum % 3 == 0)
		{
			// All elements of array is part of result
			$this->printResult($result);
			return;
		}
		else if ($sum % 3 == 1)
		{
			// When need to remove single digit which is divisible by 3 and remainder is 1
			for ($i = 1; $i < 10; ++$i)
			{
				if ($result[$i] > 0 && ($result[$i] % 3) == 1)
				{
					// Remove smallest
					$result[$i]--;
					$this->printResult($result);
					return;
				}
			}
		}
		else
		{
			// When remender is 2
			$p1 = -1;
			$p2 = -1;
			for ($i = 1; $i < 10; ++$i)
			{
				if ($result[$i] > 0 && $i % 3 == 1)
				{
					if ($p1 == -1)
					{
						$p1 = $i;
					}
					else if ($p2 == -1)
					{
						$p2 = $i;
					}
				}
				else if ($result[$i] > 0 && $i % 3 == 2)
				{
					// We get a single digit which reminder is 2 when it's divided by 3
					$result[$i]--;
					$this->printResult($result);
					return;
				}
			}
			if ($p1 != -1 && $p2 != -1)
			{
				// Remove 2 smallest
				$result[$p1]--;
				if ($result[$p1] > 0)
				{
					// When p1 are exist
					$result[$p1]--;
				}
				else
				{
					$result[$p2]--;
				}
				$this->printResult($result);
				return;
			}
		}
		// When array sum is not multiple of 3
		echo "None\n";
	}
}

function main()
{
	$task = new Multiplier();
	// Define arrays of positive digits
	$data1 = array(7, 0, 1, 2, 8, 1, 1, 6);
	$data2 = array(1, 1, 0, 0, 0);
	$data3 = array(9, 2, 4, 8, 3, 0, 0, 0, 0, 1, 4, 3, 2, 3, 2, 3, 
                   6, 2, 4, 6, 5, 2, 3, 5, 2, 4, 1, 0, 0, 0, 0, 0);
	// Get the size of data1
	$n = count($data1);
	$task->multipleOfThree($data1, $n);
	// Get the size of data2
	$n = count($data2);
	$task->multipleOfThree($data2, $n);
	// Get the size of data3
	$n = count($data3);
	$task->multipleOfThree($data3, $n);
}
main();

Output

8761110
None
98665544443333322222211000000000
/*
  Node Js Program for
  Find largest multiple of 3 in array of digits
*/
class Multiplier
{
	// Print result
	printResult(result)
	{
		var i = 0;
		var j = 0;
		for (j = 9; j >= 0; --j)
		{
			i = result[j];
			while (i > 0)
			{
				process.stdout.write(""+j);
				i--;
			}
		}
		process.stdout.write("\n");
	}
	// Handles the request of find the largest multiple of 3
	multipleOfThree(data, n)
	{
		var result = Array(10).fill(0);
		var sum = 0;
		var i = 0;
		for (i = 0; i < n; ++i)
		{
			// When array not contain valid digit
			if (data[i] > 9 || data[i] < 0)
			{
				return;
			}
			sum += data[i];
			result[data[i]]++;
		}
		if (sum % 3 == 0)
		{
			// All elements of array is part of result
			this.printResult(result);
			return;
		}
		else if (sum % 3 == 1)
		{
			// When need to remove single digit which is divisible by 3 and remainder is 1
			for (i = 1; i < 10; ++i)
			{
				if (result[i] > 0 && (result[i] % 3) == 1)
				{
					// Remove smallest
					result[i]--;
					this.printResult(result);
					return;
				}
			}
		}
		else
		{
			// When remender is 2
			var p1 = -1;
			var p2 = -1;
			for (i = 1; i < 10; ++i)
			{
				if (result[i] > 0 && i % 3 == 1)
				{
					if (p1 == -1)
					{
						p1 = i;
					}
					else if (p2 == -1)
					{
						p2 = i;
					}
				}
				else if (result[i] > 0 && i % 3 == 2)
				{
					// We get a single digit which reminder is 2 when it's divided by 3
					result[i]--;
					this.printResult(result);
					return;
				}
			}
			if (p1 != -1 && p2 != -1)
			{
				// Remove 2 smallest
				result[p1]--;
				if (result[p1] > 0)
				{
					// When p1 are exist
					result[p1]--;
				}
				else
				{
					result[p2]--;
				}
				this.printResult(result);
				return;
			}
		}
		// When array sum is not multiple of 3
		process.stdout.write("None\n");
	}
}

function main()
{
	var task = new Multiplier();
	// Define arrays of positive digits
	var data1 = [7, 0, 1, 2, 8, 1, 1, 6];
	var data2 = [1, 1, 0, 0, 0];
	var data3 = [9, 2, 4, 8, 3, 0, 0, 0, 0, 1, 4, 3, 2, 3, 2, 3, 6, 2, 
                 4, 6, 5, 2, 3, 5, 2, 4, 1, 0, 0, 0, 0, 0];
	// Get the size of data1
	var n = data1.length;
	task.multipleOfThree(data1, n);
	// Get the size of data2
	n = data2.length;
	task.multipleOfThree(data2, n);
	// Get the size of data3
	n = data3.length;
	task.multipleOfThree(data3, n);
}
main();

Output

8761110
None
98665544443333322222211000000000
#   Python 3 Program for
#   Find largest multiple of 3 in array of digits

class Multiplier :
	#  Print result
	def printResult(self, result) :
		i = 0
		j = 9
		while (j >= 0) :
			i = result[j]
			while (i > 0) :
				print(j, end = "")
				i -= 1
			
			j -= 1
		
		print(end = "\n")
	
	#  Handles the request of find the largest multiple of 3
	def multipleOfThree(self, data, n) :
		result = [0] * (10)
		sum = 0
		i = 0
		while (i < n) :
			#  When array not contain valid digit
			if (data[i] > 9 or data[i] < 0) :
				return
			
			sum += data[i]
			result[data[i]] += 1
			i += 1
		
		if (sum % 3 == 0) :
			#  All elements of array is part of result
			self.printResult(result)
			return
		
		elif(sum % 3 == 1) :
			#  When need to remove single digit which is divisible by 3 and remainder is 1
			i = 1
			while (i < 10) :
				if (result[i] > 0 and(result[i] % 3) == 1) :
					#  Remove smallest
					result[i] -= 1
					self.printResult(result)
					return
				
				i += 1
			
		else :
			#  When remender is 2
			p1 = -1
			p2 = -1
			i = 1
			while (i < 10) :
				if (result[i] > 0 and i % 3 == 1) :
					if (p1 == -1) :
						p1 = i
					
					elif(p2 == -1) :
						p2 = i
					
				
				elif(result[i] > 0 and i % 3 == 2) :
					#  We get a single digit which reminder is 2 when it's divided by 3
					result[i] -= 1
					self.printResult(result)
					return
				
				i += 1
			
			if (p1 != -1 and p2 != -1) :
				#  Remove 2 smallest
				result[p1] -= 1
				if (result[p1] > 0) :
					#  When p1 are exist
					result[p1] -= 1
				else :
					result[p2] -= 1
				
				self.printResult(result)
				return
			
		
		#  When array sum is not multiple of 3
		print("None")
	

def main() :
	task = Multiplier()
	#  Define arrays of positive digits
	data1 = [7, 0, 1, 2, 8, 1, 1, 6]
	data2 = [1, 1, 0, 0, 0]
	data3 = [9, 2, 4, 8, 3, 0, 0, 0, 0, 1, 4, 3, 2, 3, 2, 3, 6, 2, 4,
             6, 5, 2, 3, 5, 2, 4, 1, 0, 0, 0, 0, 0]
	#  Get the size of data1
	n = len(data1)
	task.multipleOfThree(data1, n)
	#  Get the size of data2
	n = len(data2)
	task.multipleOfThree(data2, n)
	#  Get the size of data3
	n = len(data3)
	task.multipleOfThree(data3, n)

if __name__ == "__main__": main()

Output

8761110
None
98665544443333322222211000000000
#   Ruby Program for
#   Find largest multiple of 3 in array of digits

class Multiplier 
	#  Print result
	def printResult(result) 
		i = 0
		j = 9
		while (j >= 0) 
			i = result[j]
			while (i > 0) 
				print(j)
				i -= 1
			end

			j -= 1
		end

		print("\n")
	end

	#  Handles the request of find the largest multiple of 3
	def multipleOfThree(data, n) 
		result = Array.new(10) {0}
		sum = 0
		i = 0
		while (i < n) 
			#  When array not contain valid digit
			if (data[i] > 9 || data[i] < 0) 
				return
			end

			sum += data[i]
			result[data[i]] += 1
			i += 1
		end

		if (sum % 3 == 0) 
			#  All elements of array is part of result
			self.printResult(result)
			return
		elsif(sum % 3 == 1) 
			#  When need to remove single digit which is divisible by 3 and remainder is 1
			i = 1
			while (i < 10) 
				if (result[i] > 0 && (result[i] % 3) == 1) 
					#  Remove smallest
					result[i] -= 1
					self.printResult(result)
					return
				end

				i += 1
			end

		else 
			#  When remender is 2
			p1 = -1
			p2 = -1
			i = 1
			while (i < 10) 
				if (result[i] > 0 && i % 3 == 1) 
					if (p1 == -1) 
						p1 = i
					elsif(p2 == -1) 
						p2 = i
					end

				elsif(result[i] > 0 && i % 3 == 2) 
					#  We get a single digit which reminder is 2 when it's divided by 3
					result[i] -= 1
					self.printResult(result)
					return
				end

				i += 1
			end

			if (p1 != -1 && p2 != -1) 
				#  Remove 2 smallest
				result[p1] -= 1
				if (result[p1] > 0) 
					#  When p1 are exist
					result[p1] -= 1
				else 
					result[p2] -= 1
				end

				self.printResult(result)
				return
			end

		end

		#  When array sum is not multiple of 3
		print("None\n")
	end

end

def main() 
	task = Multiplier.new()
	#  Define arrays of positive digits
	data1 = [7, 0, 1, 2, 8, 1, 1, 6]
	data2 = [1, 1, 0, 0, 0]
	data3 = [9, 2, 4, 8, 3, 0, 0, 0, 0, 1, 4, 3, 2, 3, 2, 3, 6, 2, 4, 
             6, 5, 2, 3, 5, 2, 4, 1, 0, 0, 0, 0, 0]
	#  Get the size of data1
	n = data1.length
	task.multipleOfThree(data1, n)
	#  Get the size of data2
	n = data2.length
	task.multipleOfThree(data2, n)
	#  Get the size of data3
	n = data3.length
	task.multipleOfThree(data3, n)
end

main()

Output

8761110
None
98665544443333322222211000000000
/*
  Scala Program for
  Find largest multiple of 3 in array of digits
*/
class Multiplier
{
	// Print result
	def printResult(result: Array[Int]): Unit = {
		var i: Int = 0;
		var j: Int = 9;
		while (j >= 0)
		{
			i = result(j);
			while (i > 0)
			{
				print(j);
				i -= 1;
			}
			j -= 1;
		}
		print("\n");
	}
	// Handles the request of find the largest multiple of 3
	def multipleOfThree(data: Array[Int], n: Int): Unit = {
		var result: Array[Int] = Array.fill[Int](10)(0);
		var sum: Int = 0;
		var i: Int = 0;
		while (i < n)
		{
			// When array not contain valid digit
			if (data(i) > 9 || data(i) < 0)
			{
				return;
			}
			sum += data(i);
			result(data(i)) += 1;
			i += 1;
		}
		if (sum % 3 == 0)
		{
			// All elements of array is part of result
			this.printResult(result);
			return;
		}
		else if (sum % 3 == 1)
		{
			// When need to remove single digit which is divisible by 3 and remainder is 1
			i = 1;
			while (i < 10)
			{
				if (result(i) > 0 && (result(i) % 3) == 1)
				{
					// Remove smallest
					result(i) -= 1;
					this.printResult(result);
					return;
				}
				i += 1;
			}
		}
		else
		{
			// When remender is 2
			var p1: Int = -1;
			var p2: Int = -1;
			i = 1;
			while (i < 10)
			{
				if (result(i) > 0 && i % 3 == 1)
				{
					if (p1 == -1)
					{
						p1 = i;
					}
					else if (p2 == -1)
					{
						p2 = i;
					}
				}
				else if (result(i) > 0 && i % 3 == 2)
				{
					// We get a single digit which reminder is 2 when it's divided by 3
					result(i) -= 1;
					this.printResult(result);
					return;
				}
				i += 1;
			}
			if (p1 != -1 && p2 != -1)
			{
				// Remove 2 smallest
				result(p1) -= 1;
				if (result(p1) > 0)
				{
					// When p1 are exist
					result(p1) -= 1;
				}
				else
				{
					result(p2) -= 1;
				}
				this.printResult(result);
				return;
			}
		}
		// When array sum is not multiple of 3
		print("None\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Multiplier = new Multiplier();
		// Define arrays of positive digits
		var data1: Array[Int] = Array(7, 0, 1, 2, 8, 1, 1, 6);
		var data2: Array[Int] = Array(1, 1, 0, 0, 0);
		var data3: Array[Int] = Array(9, 2, 4, 8, 3, 0, 0, 0, 0, 1, 4, 3, 2, 3, 2, 
                                      3, 6, 2, 4, 6, 5, 2, 3, 5, 2, 4, 1, 0, 0, 0, 0, 0);
		// Get the size of data1
		var n: Int = data1.length;
		task.multipleOfThree(data1, n);
		// Get the size of data2
		n = data2.length;
		task.multipleOfThree(data2, n);
		// Get the size of data3
		n = data3.length;
		task.multipleOfThree(data3, n);
	}
}

Output

8761110
None
98665544443333322222211000000000
/*
  Swift 4 Program for
  Find largest multiple of 3 in array of digits
*/
class Multiplier
{
	// Print result
	func printResult(_ result: [Int])
	{
		var i: Int = 0;
		var j: Int = 9;
		while (j >= 0)
		{
			i = result[j];
			while (i > 0)
			{
				print(j, terminator: "");
				i -= 1;
			}
			j -= 1;
		}
		print(terminator: "\n");
	}
	// Handles the request of find the largest multiple of 3
	func multipleOfThree(_ data: [Int], _ n: Int)
	{
		var result: [Int] = Array(repeating: 0, count: 10);
		var sum: Int = 0;
		var i: Int = 0;
		while (i < n)
		{
			// When array not contain valid digit
			if (data[i] > 9 || data[i] < 0)
			{
				return;
			}
			sum += data[i];
			result[data[i]] +=   1;
			i += 1;
		}
		if (sum % 3 == 0)
		{
			// All elements of array is part of result
			self.printResult(result);
			return;
		}
		else if (sum % 3 == 1)
		{
			// When need to remove single digit which is divisible by 3 and remainder is 1
			i = 1;
			while (i < 10)
			{
				if (result[i] > 0 && (result[i] % 3) == 1)
				{
					// Remove smallest
					result[i] -= 1;
					self.printResult(result);
					return;
				}
				i += 1;
			}
		}
		else
		{
			// When remender is 2
			var p1: Int = -1;
			var p2: Int = -1;
			i = 1;
			while (i < 10)
			{
				if (result[i] > 0 && i % 3 == 1)
				{
					if (p1 == -1)
					{
						p1 = i;
					}
					else if (p2 == -1)
					{
						p2 = i;
					}
				}
				else if (result[i] > 0 && i % 3 == 2)
				{
					// We get a single digit which reminder is 2 when it"s divided by 3
					result[i] -= 1;
					self.printResult(result);
					return;
				}
				i += 1;
			}
			if (p1  != -1 && p2  != -1)
			{
				// Remove 2 smallest
				result[p1] -= 1;
				if (result[p1] > 0)
				{
					// When p1 are exist
					result[p1] -= 1;
				}
				else
				{
					result[p2] -= 1;
				}
				self.printResult(result);
				return;
			}
		}
		// When array sum is not multiple of 3
		print("None");
	}
}
func main()
{
	let task: Multiplier = Multiplier();
	// Define arrays of positive digits
	let data1: [Int] = [7, 0, 1, 2, 8, 1, 1, 6];
	let data2: [Int] = [1, 1, 0, 0, 0];
	let data3: [Int] = [9, 2, 4, 8, 3, 0, 0, 0, 0, 1, 4, 3, 2, 3, 2, 3, 6, 2, 
                        4, 6, 5, 2, 3, 5, 2, 4, 1, 0, 0, 0, 0, 0];
	// Get the size of data1
	var n: Int = data1.count;
	task.multipleOfThree(data1, n);
	// Get the size of data2
	n = data2.count;
	task.multipleOfThree(data2, n);
	// Get the size of data3
	n = data3.count;
	task.multipleOfThree(data3, n);
}
main();

Output

8761110
None
98665544443333322222211000000000
/*
  Kotlin Program for
  Find largest multiple of 3 in array of digits
*/
class Multiplier
{
	// Print result
	fun printResult(result: Array < Int > ): Unit
	{
		var i: Int ;
		var j: Int = 9;
		while (j >= 0)
		{
			i = result[j];
			while (i > 0)
			{
				print(j);
				i -= 1;
			}
			j -= 1;
		}
		print("\n");
	}
	// Handles the request of find the largest multiple of 3
	fun multipleOfThree(data: Array < Int > , n: Int): Unit
	{
		var result: Array < Int > = Array(10)
		{
			0
		};
		var sum: Int = 0;
		var i: Int = 0;
		while (i < n)
		{
			// When array not contain valid digit
			if (data[i] > 9 || data[i] < 0)
			{
				return;
			}
			sum += data[i];
			result[data[i]] += 1;
			i += 1;
		}
		if (sum % 3 == 0)
		{
			// All elements of array is part of result
			this.printResult(result);
			return;
		}
		else if (sum % 3 == 1)
		{
			// When need to remove single digit which is divisible by 3 and remainder is 1
			i = 1;
			while (i < 10)
			{
				if (result[i] > 0 && (result[i] % 3) == 1)
				{
					// Remove smallest
					result[i] -= 1;
					this.printResult(result);
					return;
				}
				i += 1;
			}
		}
		else
		{
			// When remender is 2
			var p1: Int = -1;
			var p2: Int = -1;
			i = 1;
			while (i < 10)
			{
				if (result[i] > 0 && i % 3 == 1)
				{
					if (p1 == -1)
					{
						p1 = i;
					}
					else if (p2 == -1)
					{
						p2 = i;
					}
				}
				else if (result[i] > 0 && i % 3 == 2)
				{
					// We get a single digit which reminder is 2 when it's divided by 3
					result[i] -= 1;
					this.printResult(result);
					return;
				}
				i += 1;
			}
			if (p1 != -1 && p2 != -1)
			{
				// Remove 2 smallest
				result[p1] -= 1;
				if (result[p1] > 0)
				{
					// When p1 are exist
					result[p1] -= 1;
				}
				else
				{
					result[p2] -= 1;
				}
				this.printResult(result);
				return;
			}
		}
		// When array sum is not multiple of 3
		print("None\n");
	}
}
fun main(args: Array <String> ): Unit
{
	var task: Multiplier = Multiplier();
	// Define arrays of positive digits
	var data1: Array <Int> = arrayOf(7, 0, 1, 2, 8, 1, 1, 6);
	var data2: Array <Int> = arrayOf(1, 1, 0, 0, 0);
	var data3: Array <Int> = arrayOf(9, 2, 4, 8, 3, 0, 0, 0, 0, 1, 4, 3, 2, 3, 2, 3, 
                                       6, 2, 4, 6, 5, 2, 3, 5, 2, 4, 1, 0, 0, 0, 0, 0);
	// Get the size of data1
	var n: Int = data1.count();
	task.multipleOfThree(data1, n);
	// Get the size of data2
	n = data2.count();
	task.multipleOfThree(data2, n);
	// Get the size of data3
	n = data3.count();
	task.multipleOfThree(data3, n);
}

Output

8761110
None
98665544443333322222211000000000




Comment

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