Get encrypted number Amazon OA 2022 Solution

Page Contents

Want to solve Get encrypted number Amazon OA?, if yes then this article is for you.

Make sure you check the List of Amazon OA questions in the year of 2021 and 2022. Today, we are going to see another problem from amazon oa called Get encrypted number which was asked in 2022, this questions might be still active in some of the Amazon OA so save all the solutions, publishing questions directly is violation of amazon DCMA policy so we try our best to provide similar questions now lets see how can we solve this problem.

Get encrypted number Amazon OA Solution

You are given a 0-indexed integer array nums, where nums[i] is a digit between 0 and 9 (inclusive).

The triangular sum of nums is the value of the only element present in nums after the following process terminates:

  1. Let nums comprise of n elements. If n == 1end the process. Otherwise, create a new 0-indexed integer array newNums of length n - 1.
  2. For each index i, where 0 <= i < n - 1assign the value of newNums[i] as (nums[i] + nums[i+1]) % 10, where % denotes modulo operator.
  3. Replace the array nums with newNums.
  4. Repeat the entire process starting from step 1.

Return the triangular sum of nums.

Example 1:

pascals-triangle
pascals-triangle

Input: nums = [1,2,3,4,5]

Output: 8

Explanation: The above diagram depicts the process from which we obtain the triangular sum of the array.

Example 2:

Input: nums = [5]

Output: 5

Explanation: Since there is only one element in nums, the triangular sum is the value of that element itself.

Constraints:

  • 1 <= nums.length <= 1000
  • 0 <= nums[i] <= 9

Related: Google Online Assessment 2022 Questions List

Get encrypted number Amazon OA Solution
Get encrypted number Amazon OA Solution

SOLUTION

Program: Get encrypted number Amazon OA Solution in Python

class Solution:
    def triangularSum(self, nums: List[int]) -> int:
        m = len(nums) - 1
        mCk = 1
        result = 0
        for k, num in enumerate(nums):
            result = (result + mCk * num) % 10
            # print("r",result)
            mCk *= m - k
            # print("mc",mCk)
            mCk //= k + 1
            # print("mc",mCk)
        return (result)

Program: Get encrypted number Amazon OA Solution in Java

public int triangularSum(int[] nums) {
		if (nums.length == 1)
			return nums[0];

		int run = nums.length - 1; //store the number of times to run the loop

		for (int i = 0; i < run; i++) {

			for (int j = 0; j < nums.length - 1 - i; j++) {

				nums[j] = (nums[j] + nums[j + 1]) % 10; //replace in the orginal array as you move along

			}
		}

		return nums[0]; //first index will have answer
	}

Program: Get encrypted number Amazon OA Solution in C++

class Solution {
public:
    int triangularSum(vector<int>& n) {
         int l=n.size();
            if(l==1){
                return n[0];
            }else{
                vector<int>arr(l-1);
                for(int i=0; i<l-1; i++){
                    arr[i]=(n[i]+n[i+1])%10;
                }
                return triangularSum(arr);
            }
    }
};

Related: Amazon Online Assessment 2022 Questions List