Justyn  Ortiz

Justyn Ortiz

1598479200

Minimum length of subsequence having unit GCD

Given an array arr[] of N positive integers. The task is to find the length of the shortest sub-sequence such that the GCD of the subsequence is 1. If none of the sub-sequence has GCD 1, then print “-1“.

Examples:

Input:_ arr[] = {2, 6, 12, 3}_

Output:_ 2 _

Explanation:

The GCD of 2, 3 = 1, which is the smallest length of subsequence as 2.

Input:_ arr[] = {2, 4}_

Output:_ -1_

Explanation:

_GCD of 2, 4 = 2 _

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

Naive Approach: The idea is to generate all possible subsequence of the given array and print the length of that subsequence whose GCD is unity and have a minimum length. If none of the sub-sequence has GCD 1, then print “-1“.

Time Complexity:_ O(2N)_

Auxiliary Space:_ O(1)_

Efficient Approach: There are 2 key observations for solving this problem:

  1. Two numbers will have their GCD equal to one only when their prime factors are different.
  2. Any positive number which is less than 109 can have a maximum of 9 prime factors. For Example: 2×3×5×7×11×13×17×19×23 = 22, 30, 92, 870. If we multiply this number by the next prime number that is 29, it will be greater than 10^9.

Follow the steps below to solve the problem:

  1. Express the numbers as the product of its prime factors. Since we have a maximum of 9 prime factors, we can use the concept of Bitmask to store the state of the number. For Example, prime factors of 12 are 2, 3. This can be expressed in binary as 11 (Ignoring the preceding zeroes) meaning two prime factors are there for this number.
  2. For every number in the input array, check if any other number has the corresponding bits set or not. This could be achieved using Bitwise AND operation. The resultant of this operation is another state of our solution space.
  3. Now use the concept of Dynamic Programming to memorize the states. The idea is to use an array to store the states of the solution space. This works since only 9 bits could be set a time, and an array of size 1024 could capture all the states of the solution space.
  4. For every state use dynamic programming to store the shortest way to reach that state.
  5. If the Bitwise AND of any two states is equal to 0, then the GCD is equal to one, i.e., if there is a possibility to reach state 0 from the current state, then it will have the minimum length sub-sequence and print that length otherwise print “-1”.

Below is the implementation of the above approach:

  • Java

// Java program for the above approach

**import** java.io.*;

**import** java.util.*;

**class** GFG {

// Function that finds the prime

// factors of a number

**private** **static** **int**``[] findPrimeFactors(``**int** n)

{

// To store the prime factor

**int**``[] primeFactors = **new** **int**``[``9``];

**int** j = 0``;

// 2s that divide n

**if** (n % 2 == 0``) {

primeFactors[j++] = 2``;

**while** (n % 2 == 0``)

n >>= 1``;

}

// N must be odd at this point

// Skip one element

**for** (``**int** i = 3``;

i * i <= n; i += 2``) {

**if** (n % i == 0``) {

// Update the prime factor

primeFactors[j++] = i;

**while** (n % i == 0``)

n /= i;

}

}

// If n is a prime number

// greater than 2

**if** (n > 2``)

primeFactors[j++] = n;

**return** Arrays.copyOfRange(primeFactors, 0``, j);

}

// Function that finds the shortest

// subsequence

**private** **static** **void**

findShortestSubsequence(``**int**``[] dp, **int**``[] a,

**int** index,

**int**``[] primeFactors)

{

**int** n = a.length;

**for** (``**int** j = index; j < n; j++) {

**int** bitmask = 0``;

**for** (``**int** p = 0``;

p < primeFactors.length; p++) {

// Check if the prime factor

// of first number, is also

// the prime factor of the

// rest numbers in array

**if** (a[j] % primeFactors[p] == 0``) {

// Set corresponding bit

// of prime factor to 1,

// it means both these

// numbers have the

// same prime factor

bitmask ^= (``1 << p);

}

}

**for** (``**int** i = 0``;

i < dp.length; i++) {

// If no states encountered

// so far continue for this

// combination of bits

**if** (dp[i] == n + 1``)

**continue**``;

// Update this state with

// minimum ways to reach

// this state

dp[bitmask & i]

= Math.min(dp[bitmask & i],

dp[i] + 1``);

}

}

}

// Function that print the minimum

// length of subsequence

**private** **static** **void**

printMinimumLength(``**int**``[] a)

{

**int** min = a.length + 1``;

**for** (``**int** i = 0``;

i < a.length - 1``; i++) {

// Find the prime factors of

// the first number

**int**``[] primeFactors

= findPrimeFactors(a[i]);

**int** n = primeFactors.length;

**int**``[] dp = **new** **int**``[``1 << n];

// Initialize the array with

// maximum steps, size of the

// array + 1 for instance

Arrays.fill(dp, a.length + 1``);

// Express the prime factors

// in bit representation

// Total number of set bits is

// equal to the total number

// of prime factors

**int** setBits = (``1 << n) - 1``;

// Indicates there is one

// way to reach the number

// under consideration

dp[setBits] = 1``;

findShortestSubsequence(dp, a, i + 1``,

primeFactors);

// State 0 corresponds

// to gcd of 1

min = Math.min(dp[``0``], min);

}

// If not found such subsequence

// then print "-1"

**if** (min == a.length + 1``)

System.out.println(-``1``);

// Else print the length

**else**

System.out.println(min);

}

// Driver Code

**public** **static** **void** main(String[] args)

{

// Given array arr[]

**int**``[] arr = { 2``, 6``, 12``, 3 };

// Function Call

printMinimumLength(arr);

}

}

Output:

2

Time Complexity:_ O(N2)_

Auxiliary Space:_ O(1)_

Better Approach: The minimum length of a sub-sequence having GCD equals to 1 can’t be more than 2, because if a sub-sequence have GCD equals to 1 then it can be proved that the sub-sequence has at least one co-prime pair. Below are the steps:

  1. Traverse the given array to generate all the possible pairs of elements.
  2. If any pair is found having GCD as 1, then exit from the loop.
  3. Print “2” if pair is found otherwise print “1”.

#arrays #bit magic #dynamic programming #mathematical #algorithms-dynamic programming #bitwise-and #gcd-lcm #subsequence

What is GEEK

Buddha Community

Minimum length of subsequence having unit GCD
Justyn  Ortiz

Justyn Ortiz

1598479200

Minimum length of subsequence having unit GCD

Given an array arr[] of N positive integers. The task is to find the length of the shortest sub-sequence such that the GCD of the subsequence is 1. If none of the sub-sequence has GCD 1, then print “-1“.

Examples:

Input:_ arr[] = {2, 6, 12, 3}_

Output:_ 2 _

Explanation:

The GCD of 2, 3 = 1, which is the smallest length of subsequence as 2.

Input:_ arr[] = {2, 4}_

Output:_ -1_

Explanation:

_GCD of 2, 4 = 2 _

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

Naive Approach: The idea is to generate all possible subsequence of the given array and print the length of that subsequence whose GCD is unity and have a minimum length. If none of the sub-sequence has GCD 1, then print “-1“.

Time Complexity:_ O(2N)_

Auxiliary Space:_ O(1)_

Efficient Approach: There are 2 key observations for solving this problem:

  1. Two numbers will have their GCD equal to one only when their prime factors are different.
  2. Any positive number which is less than 109 can have a maximum of 9 prime factors. For Example: 2×3×5×7×11×13×17×19×23 = 22, 30, 92, 870. If we multiply this number by the next prime number that is 29, it will be greater than 10^9.

Follow the steps below to solve the problem:

  1. Express the numbers as the product of its prime factors. Since we have a maximum of 9 prime factors, we can use the concept of Bitmask to store the state of the number. For Example, prime factors of 12 are 2, 3. This can be expressed in binary as 11 (Ignoring the preceding zeroes) meaning two prime factors are there for this number.
  2. For every number in the input array, check if any other number has the corresponding bits set or not. This could be achieved using Bitwise AND operation. The resultant of this operation is another state of our solution space.
  3. Now use the concept of Dynamic Programming to memorize the states. The idea is to use an array to store the states of the solution space. This works since only 9 bits could be set a time, and an array of size 1024 could capture all the states of the solution space.
  4. For every state use dynamic programming to store the shortest way to reach that state.
  5. If the Bitwise AND of any two states is equal to 0, then the GCD is equal to one, i.e., if there is a possibility to reach state 0 from the current state, then it will have the minimum length sub-sequence and print that length otherwise print “-1”.

Below is the implementation of the above approach:

  • Java

// Java program for the above approach

**import** java.io.*;

**import** java.util.*;

**class** GFG {

// Function that finds the prime

// factors of a number

**private** **static** **int**``[] findPrimeFactors(``**int** n)

{

// To store the prime factor

**int**``[] primeFactors = **new** **int**``[``9``];

**int** j = 0``;

// 2s that divide n

**if** (n % 2 == 0``) {

primeFactors[j++] = 2``;

**while** (n % 2 == 0``)

n >>= 1``;

}

// N must be odd at this point

// Skip one element

**for** (``**int** i = 3``;

i * i <= n; i += 2``) {

**if** (n % i == 0``) {

// Update the prime factor

primeFactors[j++] = i;

**while** (n % i == 0``)

n /= i;

}

}

// If n is a prime number

// greater than 2

**if** (n > 2``)

primeFactors[j++] = n;

**return** Arrays.copyOfRange(primeFactors, 0``, j);

}

// Function that finds the shortest

// subsequence

**private** **static** **void**

findShortestSubsequence(``**int**``[] dp, **int**``[] a,

**int** index,

**int**``[] primeFactors)

{

**int** n = a.length;

**for** (``**int** j = index; j < n; j++) {

**int** bitmask = 0``;

**for** (``**int** p = 0``;

p < primeFactors.length; p++) {

// Check if the prime factor

// of first number, is also

// the prime factor of the

// rest numbers in array

**if** (a[j] % primeFactors[p] == 0``) {

// Set corresponding bit

// of prime factor to 1,

// it means both these

// numbers have the

// same prime factor

bitmask ^= (``1 << p);

}

}

**for** (``**int** i = 0``;

i < dp.length; i++) {

// If no states encountered

// so far continue for this

// combination of bits

**if** (dp[i] == n + 1``)

**continue**``;

// Update this state with

// minimum ways to reach

// this state

dp[bitmask & i]

= Math.min(dp[bitmask & i],

dp[i] + 1``);

}

}

}

// Function that print the minimum

// length of subsequence

**private** **static** **void**

printMinimumLength(``**int**``[] a)

{

**int** min = a.length + 1``;

**for** (``**int** i = 0``;

i < a.length - 1``; i++) {

// Find the prime factors of

// the first number

**int**``[] primeFactors

= findPrimeFactors(a[i]);

**int** n = primeFactors.length;

**int**``[] dp = **new** **int**``[``1 << n];

// Initialize the array with

// maximum steps, size of the

// array + 1 for instance

Arrays.fill(dp, a.length + 1``);

// Express the prime factors

// in bit representation

// Total number of set bits is

// equal to the total number

// of prime factors

**int** setBits = (``1 << n) - 1``;

// Indicates there is one

// way to reach the number

// under consideration

dp[setBits] = 1``;

findShortestSubsequence(dp, a, i + 1``,

primeFactors);

// State 0 corresponds

// to gcd of 1

min = Math.min(dp[``0``], min);

}

// If not found such subsequence

// then print "-1"

**if** (min == a.length + 1``)

System.out.println(-``1``);

// Else print the length

**else**

System.out.println(min);

}

// Driver Code

**public** **static** **void** main(String[] args)

{

// Given array arr[]

**int**``[] arr = { 2``, 6``, 12``, 3 };

// Function Call

printMinimumLength(arr);

}

}

Output:

2

Time Complexity:_ O(N2)_

Auxiliary Space:_ O(1)_

Better Approach: The minimum length of a sub-sequence having GCD equals to 1 can’t be more than 2, because if a sub-sequence have GCD equals to 1 then it can be proved that the sub-sequence has at least one co-prime pair. Below are the steps:

  1. Traverse the given array to generate all the possible pairs of elements.
  2. If any pair is found having GCD as 1, then exit from the loop.
  3. Print “2” if pair is found otherwise print “1”.

#arrays #bit magic #dynamic programming #mathematical #algorithms-dynamic programming #bitwise-and #gcd-lcm #subsequence

Zenzi Hemp Gummies United Kingdom - Stronger, Fast & Effective Pain Relief Formula!

Zenzi Hemp Gummies United Kingdom:- has a plenty of benefits that anybody can exploit. We’ve arranged a rundown of some vital benefits to watch out for. Kindly view a portion of the critical benefits that will be accessible to you.
Decrease Mental Stress: – With the assistance of this oil, any psychological difficulties can promptly mitigated. Subsequent to managing this oil, the individual’s body will be liberated from tension and stress.

Counter Physical Pain: – By utilizing it’s anything but a regular schedule, all actual agony in the body of the individual can be promptly limited. It will essentially ease each torment without adding any more strain to the body tone.
Official Website:- http://top10cbdoilstore.com/zenzi-hemp-gummies-united-kingdom/
Follow us@@>>> https://www.facebook.com/Zenzi-Hemp-Gummies-United-Kingdom-103651802015972
https://www.facebook.com/Zenzi-Hemp-Gummies-United-Kingdom-102082088814554
https://twitter.com/ZenziGummiesUK
Google Sites@@>>> https://sites.google.com/view/zenzi-hemp-gummies-uk-buy/
https://sites.google.com/view/zenzihempgummiesunitedkingdom/
Read More;- https://www.crunchbase.com/organization/zenzi-hemp-gummies-united-kingdom
https://kit.co/ZenziHempGummy/zenzi-hemp-gummies-united-kingdom
https://kit.co/ZenziHempGummy
https://about.me/zenzihempgummiesuk
https://www.spreaker.com/show/zenzi-hemp-gummies-uk-ingredients

https://caramellaapp.com/zenzihempgummiesunitedkingdom/8HP7oLYkb/zenzi-hemp-gummies-united-kingdom
https://sketchfab.com/ZenziHempGummiesUnitedKingdom
http://health2wellness4u.over-blog.com/zenzi-hemp-gummies-united-kingdom
https://health2wellness4u.blogspot.com/2021/07/zenzi-hemp-gummies-united-kingdom.html
https://twitter.com/ZenziGummiesUK

#zenzi hemp gummies united kingdom risk free #zenzi hemp gummies united kingdom trial #zenzi hemp gummies united kingdom reviews #zenzi hemp gummies united kingdom benefits ##zenzi hemp gummies united kingdomcost

Luna  Hermann

Luna Hermann

1599207000

JavaScript Problem Solvers: Minimum Subsequence

Subsequences have always been a concept that trips me up a bit. I don’t know why. But as with anything, the more time I spend working with subsequences, the more I slowly learn and get more comfortable with them.

Either way, the problem we are going to tackle today might seem complicated at first glance, and will provide a good challenge for our critical thinking skills (hopefully).

So, let’s get solving.


THE PROBLEM

Here is a link to the problem on LeetCode

Given the array nums, obtain a subsequence of the array whose sum of elements is strictly greater than the sum of the non included elements in such subsequence.
If there are multiple solutions, return the subsequence with minimum size and if there still exist multiple solutions, return the subsequence with the maximum total sum of all its elements. A subsequence of an array can be obtained by erasing some (possibly zero) elements from the array.
Note that the solution with the given constraints is guaranteed to be unique. Also return the answer sorted in non-increasing order.

THE CONSTRAINTS

The provided constraints don’t really yield any noteworthy pieces of information, but as always let’s through each one and see if we can uncover any clues to a solution:

1 <= nums.length <= 500

The first constraint gives us a range of numbers we should expect in the nums array. With a lower limit of 1 <= nums.length, we don’t have to worry about nums being empty or not having any elements. The upper limit of nums.length <= 500 also doesn’t give us any particular information other than the largest amount of elements we would need to iterate through.

1 <= nums[i] <= 100

The second constraint is the range for elements in nums. We learn that we won’t have to deal with any negative numbers or if an element in nums is 0, as the lower limit of elements in nums is 1 <= nums[i]. With the upper limit of nums[i] <= 100, we also do not have to worry about any really big numbers. Relatively speaking, an upper limit of 100 is quite small.

#austin-smith #programming #javascript #leetcode #minimum-subsequence

How to Find the Length of List in Python | Ways to Find Length of List

Python  is in high demand among software developers and data scientists  around the world. This programming language has various data types and six of them in particular for storing sequences. These include List, Number, String, Dictionary, and Tuple. In this tutorial, we will understand what are Python lists and also look at how to find the length of a list in Python.

Python Lists Explained

The Way Forward

**Conclusion **

#data science #length of list #length of list in python #python

rick william

1626849093

MAGENTO 2 MINIMUM ORDER AMOUNT FOR CUSTOMER GROUP

Set a minimum order amount based on customer groups. Magento 2 Minimum Order Amount for Customer Group Extension helps the store owner to set a minimum order amount for better customer experience to encourage customers to purchase more products or buy bulk products from the store.

#magento 2 minimum order amount for customer group #minimum order amount for customer group #magento 2 #minimum order amount for customer group magento 2