C++ program for product array puzzleIn this article, we will discuss how to find product array puzzles in C++ with several methods. Problem statement:We have taken an array of integers, and we must create a new array of the same size as the input array numbers where each element of the new array is equal to the product of all elements in the old given array nums except the nums[i]. Input format: We are taking an integer array: Example nums = {1, 2, 3, 4, 5} Output format: We are expected to print the array elements of the array after doing the above operations. Example: the new array is product = {120, 60, 40, 30, 24} Explanation: Product[0] = 2*3*4*5 Product[1] = 1*3*4*5 Product[2] = 1*2*4*5 Product[3] = 1*2*3*5 Product[4] = 1*2*3*4 There are many ways to solve the given problem. Here, we discuss 4 ways to solve the problem. This problem has to be solved without using the division operator. Method 1: Bruth Force MethodLet's take an example to demonstrate the product array puzzle using the Bruth Force Method in C++: Output: Explanation: There is an integer n, which is the length of the array, and an array of integers. In the above program, two loops are running. The first loop iterates the array. After that, the inner loop contains three variables: left, right, and mul variables. The left variable will decrease by one for getting the product of numbers before the current index, and the right variable will increase by one for getting the product of numbers after the current index. These two variables, left and right, have separate while loops. After getting the product of the elements to the left of the index and to the right of the index, we will print the multiplication of both. Method 2: Using the pow methodLet's take an example to demonstrate the product array puzzle using the pow method in C++: Output: Explanation: This program has one for loop, which iterates through the given array and then finds the total product of the array elements and stores it in the variables named mul. After that, we again use for loop that is used to iterate the array again, but this time, we will multiply the product of the total elements with the power of the current element and minus one. It means we are multiplying the reciprocal of the current element with the entire product, which will give the same result that we expected. Method 3: Using a left product and a right productLet's take an example to demonstrate the product array puzzle using the left and right product in C++: Output: Explanation: This program is also used to solve the problem by using two arrays, left and right. This program has four for loops in which one for loop is used to initialize every element of the arrays left and right to 0, and then a second for loop is used to get the prefix product of every element. After that, the third for loop is used to get the suffix product of the element, and then the final for loop produces the prefix product and the suffix product according to the current element. Method 4: Using log and antilogLet's take an example to demonstrate the product array puzzle using the log and antilog in C++: Output: |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India