Problem
The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450 |
Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
Solution Approach
In this problem of the Largest product in a series, we have to find the thirteen adjacent numbers. The numbers should be chosen such that their product will be maximum. We have to choose thirteen digit number from a 1000 digit number provided. We have to only find the product now the actual numbers which constitute in giving the highest product value.
This problem can be easily tackled by the brute force method. We will iterate over all the numbers twice and keeping a window of 13 numbers to calculate the product. Since the product can become huge we have to use long or any other suitable data type for storage. At the end of every thirteen number window, we check if the intermediate product is the largest as of now or not. If it is we replace the product to have the largest value and the cycle repeats. Lastly, we return the maximum product as the answer.
Complexity
The time complexity of this approach is O(n2) as we are iterating through the numbers twice. The space complexity of this approach is constant or O(1) as we need just one variable to store the value of the maximum product and one to store the value of the running product.
Code Approach
Coming to the code part of Largest product in a series problem, we will initialize the maximum product variable. Then we have to run two loops where the outer loop will iterate over all the numbers which will be the starting point for the next loop in the calculation of the product. We also have to initialize the intermediate product variable before the start of the second loop. We calculate the product inside the second loop. After the second loop finishes, we compare the product variable value and maximum product variable value and store the maximum one. In the end, we need to return the maximum product as the answer.
Solution Code
Java Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.*; import java.lang.*; import java.io.*; class Solution { public static void main (String[] args) { String NUMBER = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"; int ADJACENTNUMBER = 13; long maximumProduct = -1; for(int i=0; i+ADJACENTNUMBER<= NUMBER.length(); i++){ long localProduct = 1; for(int j=0; j<ADJACENTNUMBER; j++){ localProduct *= Integer.parseInt("" + NUMBER.charAt(i+j)); } maximumProduct = Math.max(maximumProduct, localProduct); } System.out.println(maximumProduct); } } |
Python Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | def compute(): maximum_product = max(digit_product(NUMBER[i : i + ADJACENT_NUMBER]) for i in range(len(NUMBER) - ADJACENT_NUMBER + 1)) return str(maximum_product) def digit_product(s): result = 1 for c in s: result *= int(c) return result NUMBER = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450" ADJACENT_NUMBER = 13 if __name__ == "__main__": print(compute()) |
For more Project Euler explained solutions visit Project Euler Detailed Solutions.
For Leetcode detailed solutions go to Leetcode Detailed Solutions.
If you like capture the flag challenges visit here.
Check out my socials below in the footer. Feel free to ask any doubts in the comment section or contact me via the Contact page I will surely respond. Happy Coding 🙂