Find minimum subset product of an array
Here given code implementation process.
// C program for
// Find minimum subset product of an array
#include <stdio.h>
#include <limits.h>
int minValue(int a, int b)
{
if (a < b)
{
return a;
}
return b;
}
int maxValue(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
void minimumSubsetSum(int arr[], int n)
{
// Assign min value to negative variable
int negative = INT_MIN;
// Assign max value to positive variable
int positive = INT_MAX;
// Define some useful auxiliary variables
int product = 1;
int countZero = 0;
int countNegative = 0;
for (int i = 0; i < n; ++i)
{
if (arr[i] < 0)
{
// When array[i] is less than zero
// Then increment value of negative counter by 1
countNegative += 1;
negative = maxValue(negative, arr[i]);
}
else if (arr[i] == 0)
{
// When array[i] is zero
// Then increment value of zero counter by 1
countZero += 1;
}
else
{
positive = minValue(positive, arr[i]);
}
if (arr[i] != 0)
{
// Calculate product of array elements
product = product *arr[i];
}
}
if ((countZero > 0 && countNegative == 0) || countZero == n)
{
// When array contain all zeros or
// none zero and no negative element
product = 0;
}
else if (countNegative == 0)
{
product = positive;
}
else if ((countNegative % 2 == 0) && countNegative != 0)
{
product = product / negative;
}
printf("\n Result : %d", product);
}
int main(int argc, char
const *argv[])
{
int arr[] = {
1 , -1 , 2 , -2 , 0 , -3 , 1
};
// Get the number of elements in array
int n = sizeof(arr) / sizeof(arr[0]);
// Test
minimumSubsetSum(arr, n);
return 0;
}
Output
Result : -12
/*
Java program for
Find minimum subset product of an array
*/
public class SubSets
{
public int minValue(int a, int b)
{
if (a < b)
{
return a;
}
return b;
}
public int maxValue(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
public void minimumSubsetSum(int[] arr, int n)
{
// Assign min value to negative variable
int negative = Integer.MIN_VALUE;
// Assign max value to positive variable
int positive = Integer.MAX_VALUE;
// Define some useful auxiliary variables
int product = 1;
int countZero = 0;
int countNegative = 0;
for (int i = 0; i < n; ++i)
{
if (arr[i] < 0)
{
// When array[i] is less than zero
// Then increment value of negative counter by 1
countNegative += 1;
negative = maxValue(negative, arr[i]);
}
else if (arr[i] == 0)
{
// When array[i] is zero
// Then increment value of zero counter by 1
countZero += 1;
}
else
{
positive = minValue(positive, arr[i]);
}
if (arr[i] != 0)
{
// Calculate product of array elements
product = product * arr[i];
}
}
if ((countZero > 0 && countNegative == 0) || countZero == n)
{
// When array contain all zeros or
// none zero and no negative element
product = 0;
}
else if (countNegative == 0)
{
product = positive;
}
else if ((countNegative % 2 == 0) && countNegative != 0)
{
product = product / negative;
}
System.out.print("\n Result : " + product + "");
}
public static void main(String[] args)
{
SubSets task = new SubSets();
int[] arr = {
1 , -1 , 2 , -2 , 0 , -3 , 1
};
// Get the number of elements in array
int n = arr.length;
// Test
task.minimumSubsetSum(arr, n);
}
}
Output
Result : -12
// Include header file
#include <iostream>
#include <limits.h>
using namespace std;
/*
C++ program for
Find minimum subset product of an array
*/
class SubSets
{
public: int minValue(int a, int b)
{
if (a < b)
{
return a;
}
return b;
}
int maxValue(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
void minimumSubsetSum(int arr[], int n)
{
// Assign min value to negative variable
int negative = INT_MIN;
// Assign max value to positive variable
int positive = INT_MAX;
// Define some useful auxiliary variables
int product = 1;
int countZero = 0;
int countNegative = 0;
for (int i = 0; i < n; ++i)
{
if (arr[i] < 0)
{
// When array[i] is less than zero
// Then increment value of negative counter by 1
countNegative += 1;
negative = this->maxValue(negative, arr[i]);
}
else if (arr[i] == 0)
{
// When array[i] is zero
// Then increment value of zero counter by 1
countZero += 1;
}
else
{
positive = this->minValue(positive, arr[i]);
}
if (arr[i] != 0)
{
// Calculate product of array elements
product = product *arr[i];
}
}
if ((countZero > 0 && countNegative == 0) || countZero == n)
{
// When array contain all zeros or
// none zero and no negative element
product = 0;
}
else if (countNegative == 0)
{
product = positive;
}
else if ((countNegative % 2 == 0) && countNegative != 0)
{
product = product / negative;
}
cout << "\n Result : " << product << "";
}
};
int main()
{
SubSets *task = new SubSets();
int arr[] = {
1 , -1 , 2 , -2 , 0 , -3 , 1
};
// Get the number of elements in array
int n = sizeof(arr) / sizeof(arr[0]);
// Test
task->minimumSubsetSum(arr, n);
return 0;
}
Output
Result : -12
// Include namespace system
using System;
/*
Csharp program for
Find minimum subset product of an array
*/
public class SubSets
{
public int minValue(int a, int b)
{
if (a < b)
{
return a;
}
return b;
}
public int maxValue(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
public void minimumSubsetSum(int[] arr, int n)
{
// Assign min value to negative variable
int negative = int.MinValue;
// Assign max value to positive variable
int positive = int.MaxValue;
// Define some useful auxiliary variables
int product = 1;
int countZero = 0;
int countNegative = 0;
for (int i = 0; i < n; ++i)
{
if (arr[i] < 0)
{
// When array[i] is less than zero
// Then increment value of negative counter by 1
countNegative += 1;
negative = this.maxValue(negative, arr[i]);
}
else if (arr[i] == 0)
{
// When array[i] is zero
// Then increment value of zero counter by 1
countZero += 1;
}
else
{
positive = this.minValue(positive, arr[i]);
}
if (arr[i] != 0)
{
// Calculate product of array elements
product = product * arr[i];
}
}
if ((countZero > 0 && countNegative == 0) || countZero == n)
{
// When array contain all zeros or
// none zero and no negative element
product = 0;
}
else if (countNegative == 0)
{
product = positive;
}
else if ((countNegative % 2 == 0) && countNegative != 0)
{
product = product / negative;
}
Console.Write("\n Result : " + product + "");
}
public static void Main(String[] args)
{
SubSets task = new SubSets();
int[] arr = {
1 , -1 , 2 , -2 , 0 , -3 , 1
};
// Get the number of elements in array
int n = arr.Length;
// Test
task.minimumSubsetSum(arr, n);
}
}
Output
Result : -12
package main
import "math"
import "fmt"
/*
Go program for
Find minimum subset product of an array
*/
func minValue(a, b int) int {
if a < b {
return a
}
return b
}
func maxValue(a, b int) int {
if a > b {
return a
}
return b
}
func minimumSubsetSum(arr[] int, n int) {
// Assign min value to negative variable
var negative int = math.MinInt64
// Assign max value to positive variable
var positive int = math.MaxInt64
// Define some useful auxiliary variables
var product int = 1
var countZero int = 0
var countNegative int = 0
for i := 0 ; i < n ; i++ {
if arr[i] < 0 {
// When array[i] is less than zero
// Then increment value of negative counter by 1
countNegative += 1
negative = maxValue(negative, arr[i])
} else if arr[i] == 0 {
// When array[i] is zero
// Then increment value of zero counter by 1
countZero += 1
} else {
positive = minValue(positive, arr[i])
}
if arr[i] != 0 {
// Calculate product of array elements
product = product * arr[i]
}
}
if (countZero > 0 && countNegative == 0) || countZero == n {
// When array contain all zeros or
// none zero and no negative element
product = 0
} else if countNegative == 0 {
product = positive
} else if (countNegative % 2 == 0) && countNegative != 0 {
product = product / negative
}
fmt.Print("\n Result : ", product, "")
}
func main() {
var arr = [] int {1 , -1 , 2 , -2 , 0 , -3 , 1}
// Get the number of elements in array
var n int = len(arr)
// Test
minimumSubsetSum(arr, n)
}
Output
Result : -12
<?php
/*
Php program for
Find minimum subset product of an array
*/
class SubSets
{
public function minValue($a, $b)
{
if ($a < $b)
{
return $a;
}
return $b;
}
public function maxValue($a, $b)
{
if ($a > $b)
{
return $a;
}
return $b;
}
public function minimumSubsetSum($arr, $n)
{
// Assign min value to negative variable
$negative = -PHP_INT_MAX;
// Assign max value to positive variable
$positive = PHP_INT_MAX;
// Define some useful auxiliary variables
$product = 1;
$countZero = 0;
$countNegative = 0;
for ($i = 0; $i < $n; ++$i)
{
if ($arr[$i] < 0)
{
// When array[i] is less than zero
// Then increment value of negative counter by 1
$countNegative += 1;
$negative = $this->maxValue($negative, $arr[$i]);
}
else if ($arr[$i] == 0)
{
// When array[i] is zero
// Then increment value of zero counter by 1
$countZero += 1;
}
else
{
$positive = $this->minValue($positive, $arr[$i]);
}
if ($arr[$i] != 0)
{
// Calculate product of array elements
$product = $product * $arr[$i];
}
}
if (($countZero > 0 && $countNegative == 0) || $countZero == $n)
{
// When array contain all zeros or
// none zero and no negative element
$product = 0;
}
else if ($countNegative == 0)
{
$product = $positive;
}
else if (($countNegative % 2 == 0) && $countNegative != 0)
{
$product = (int)($product / $negative);
}
echo("\n Result : ".$product.
"");
}
}
function main()
{
$task = new SubSets();
$arr = array(1, -1, 2, -2, 0, -3, 1);
// Get the number of elements in array
$n = count($arr);
// Test
$task->minimumSubsetSum($arr, $n);
}
main();
Output
Result : -12
/*
Node JS program for
Find minimum subset product of an array
*/
class SubSets
{
minValue(a, b)
{
if (a < b)
{
return a;
}
return b;
}
maxValue(a, b)
{
if (a > b)
{
return a;
}
return b;
}
minimumSubsetSum(arr, n)
{
// Assign min value to negative variable
var negative = -Number.MAX_VALUE;
// Assign max value to positive variable
var positive = Number.MAX_VALUE;
// Define some useful auxiliary variables
var product = 1;
var countZero = 0;
var countNegative = 0;
for (var i = 0; i < n; ++i)
{
if (arr[i] < 0)
{
// When array[i] is less than zero
// Then increment value of negative counter by 1
countNegative += 1;
negative = this.maxValue(negative, arr[i]);
}
else if (arr[i] == 0)
{
// When array[i] is zero
// Then increment value of zero counter by 1
countZero += 1;
}
else
{
positive = this.minValue(positive, arr[i]);
}
if (arr[i] != 0)
{
// Calculate product of array elements
product = product * arr[i];
}
}
if ((countZero > 0 && countNegative == 0) || countZero == n)
{
// When array contain all zeros or
// none zero and no negative element
product = 0;
}
else if (countNegative == 0)
{
product = positive;
}
else if ((countNegative % 2 == 0) && countNegative != 0)
{
product = parseInt(product / negative);
}
process.stdout.write("\n Result : " + product + "");
}
}
function main()
{
var task = new SubSets();
var arr = [1, -1, 2, -2, 0, -3, 1];
// Get the number of elements in array
var n = arr.length;
// Test
task.minimumSubsetSum(arr, n);
}
main();
Output
Result : -12
import sys
# Python 3 program for
# Find minimum subset product of an array
class SubSets :
def minValue(self, a, b) :
if (a < b) :
return a
return b
def maxValue(self, a, b) :
if (a > b) :
return a
return b
def minimumSubsetSum(self, arr, n) :
# Assign min value to negative variable
negative = -sys.maxsize
# Assign max value to positive variable
positive = sys.maxsize
# Define some useful auxiliary variables
product = 1
countZero = 0
countNegative = 0
i = 0
while (i < n) :
if (arr[i] < 0) :
# When list[i] is less than zero
# Then increment value of negative counter by 1
countNegative += 1
negative = self.maxValue(negative, arr[i])
elif (arr[i] == 0) :
# When list[i] is zero
# Then increment value of zero counter by 1
countZero += 1
else :
positive = self.minValue(positive, arr[i])
if (arr[i] != 0) :
# Calculate product of list elements
product = product * arr[i]
i += 1
if ((countZero > 0 and countNegative == 0) or countZero == n) :
# When list contain all zeros or
# none zero and no negative element
product = 0
elif (countNegative == 0) :
product = positive
elif ((countNegative % 2 == 0) and countNegative != 0) :
product = int(product / negative)
print("\n Result : ", product ,"", end = "")
def main() :
task = SubSets()
arr = [1, -1, 2, -2, 0, -3, 1]
# Get the number of elements in list
n = len(arr)
# Test
task.minimumSubsetSum(arr, n)
if __name__ == "__main__": main()
Output
Result : -12
# Ruby program for
# Find minimum subset product of an array
class SubSets
def minValue(a, b)
if (a < b)
return a
end
return b
end
def maxValue(a, b)
if (a > b)
return a
end
return b
end
def minimumSubsetSum(arr, n)
# Assign min value to negative variable
negative = -(2 ** (0. size * 8 - 2))
# Assign max value to positive variable
positive = (2 ** (0. size * 8 - 2))
# Define some useful auxiliary variables
product = 1
countZero = 0
countNegative = 0
i = 0
while (i < n)
if (arr[i] < 0)
# When array[i] is less than zero
# Then increment value of negative counter by 1
countNegative += 1
negative = self.maxValue(negative, arr[i])
elsif (arr[i] == 0)
# When array[i] is zero
# Then increment value of zero counter by 1
countZero += 1
else
positive = self.minValue(positive, arr[i])
end
if (arr[i] != 0)
# Calculate product of array elements
product = product * arr[i]
end
i += 1
end
if ((countZero > 0 && countNegative == 0) || countZero == n)
# When array contain all zeros or
# none zero and no negative element
product = 0
elsif (countNegative == 0)
product = positive
elsif ((countNegative % 2 == 0) && countNegative != 0)
product = product / negative
end
print("\n Result : ", product ,"")
end
end
def main()
task = SubSets.new()
arr = [1, -1, 2, -2, 0, -3, 1]
# Get the number of elements in array
n = arr.length
# Test
task.minimumSubsetSum(arr, n)
end
main()
Output
Result : -12
/*
Scala program for
Find minimum subset product of an array
*/
class SubSets()
{
def minValue(a: Int, b: Int): Int = {
if (a < b)
{
return a;
}
return b;
}
def maxValue(a: Int, b: Int): Int = {
if (a > b)
{
return a;
}
return b;
}
def minimumSubsetSum(arr: Array[Int], n: Int): Unit = {
// Assign min value to negative variable
var negative: Int = Int.MinValue;
// Assign max value to positive variable
var positive: Int = Int.MaxValue;
// Define some useful auxiliary variables
var product: Int = 1;
var countZero: Int = 0;
var countNegative: Int = 0;
var i: Int = 0;
while (i < n)
{
if (arr(i) < 0)
{
// When array[i] is less than zero
// Then increment value of negative counter by 1
countNegative += 1;
negative = maxValue(negative, arr(i));
}
else if (arr(i) == 0)
{
// When array[i] is zero
// Then increment value of zero counter by 1
countZero += 1;
}
else
{
positive = minValue(positive, arr(i));
}
if (arr(i) != 0)
{
// Calculate product of array elements
product = product * arr(i);
}
i += 1;
}
if ((countZero > 0 && countNegative == 0) || countZero == n)
{
// When array contain all zeros or
// none zero and no negative element
product = 0;
}
else if (countNegative == 0)
{
product = positive;
}
else if ((countNegative % 2 == 0) && countNegative != 0)
{
product = product / negative;
}
print("\n Result : " + product + "");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: SubSets = new SubSets();
var arr: Array[Int] = Array(1, -1, 2, -2, 0, -3, 1);
// Get the number of elements in array
var n: Int = arr.length;
// Test
task.minimumSubsetSum(arr, n);
}
}
Output
Result : -12
import Foundation;
/*
Swift 4 program for
Find minimum subset product of an array
*/
class SubSets
{
func minValue(_ a: Int, _ b: Int) -> Int
{
if (a < b)
{
return a;
}
return b;
}
func maxValue(_ a: Int, _ b: Int) -> Int
{
if (a > b)
{
return a;
}
return b;
}
func minimumSubsetSum(_ arr: [Int], _ n: Int)
{
// Assign min value to negative variable
var negative: Int = Int.min;
// Assign max value to positive variable
var positive: Int = Int.max;
// Define some useful auxiliary variables
var product: Int = 1;
var countZero: Int = 0;
var countNegative: Int = 0;
var i: Int = 0;
while (i < n)
{
if (arr[i] < 0)
{
// When array[i] is less than zero
// Then increment value of negative counter by 1
countNegative += 1;
negative = self.maxValue(negative, arr[i]);
}
else if (arr[i] == 0)
{
// When array[i] is zero
// Then increment value of zero counter by 1
countZero += 1;
}
else
{
positive = self.minValue(positive, arr[i]);
}
if (arr[i] != 0)
{
// Calculate product of array elements
product = product * arr[i];
}
i += 1;
}
if ((countZero > 0 && countNegative == 0) || countZero == n)
{
// When array contain all zeros or
// none zero and no negative element
product = 0;
}
else if (countNegative == 0)
{
product = positive;
}
else if ((countNegative % 2 == 0) && countNegative != 0)
{
product = product / negative;
}
print("\n Result : ", product ,"", terminator: "");
}
}
func main()
{
let task: SubSets = SubSets();
let arr: [Int] = [1, -1, 2, -2, 0, -3, 1];
// Get the number of elements in array
let n: Int = arr.count;
// Test
task.minimumSubsetSum(arr, n);
}
main();
Output
Result : -12
/*
Kotlin program for
Find minimum subset product of an array
*/
class SubSets
{
fun minValue(a: Int, b: Int): Int
{
if (a < b)
{
return a;
}
return b;
}
fun maxValue(a: Int, b: Int): Int
{
if (a > b)
{
return a;
}
return b;
}
fun minimumSubsetSum(arr: Array < Int > , n: Int): Unit
{
// Assign min value to negative variable
var negative: Int = Int.MIN_VALUE;
// Assign max value to positive variable
var positive: Int = Int.MAX_VALUE;
// Define some useful auxiliary variables
var product: Int = 1;
var countZero: Int = 0;
var countNegative: Int = 0;
var i: Int = 0;
while (i < n)
{
if (arr[i] < 0)
{
// When array[i] is less than zero
// Then increment value of negative counter by 1
countNegative += 1;
negative = this.maxValue(negative, arr[i]);
}
else if (arr[i] == 0)
{
// When array[i] is zero
// Then increment value of zero counter by 1
countZero += 1;
}
else
{
positive = this.minValue(positive, arr[i]);
}
if (arr[i] != 0)
{
// Calculate product of array elements
product = product * arr[i];
}
i += 1;
}
if ((countZero > 0 && countNegative == 0) || countZero == n)
{
// When array contain all zeros or
// none zero and no negative element
product = 0;
}
else if (countNegative == 0)
{
product = positive;
}
else if ((countNegative % 2 == 0) && countNegative != 0)
{
product = product / negative;
}
print("\n Result : " + product + "");
}
}
fun main(args: Array < String > ): Unit
{
val task: SubSets = SubSets();
val arr: Array < Int > = arrayOf(1, -1, 2, -2, 0, -3, 1);
// Get the number of elements in array
val n: Int = arr.count();
// Test
task.minimumSubsetSum(arr, n);
}
Output
Result : -12
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