Amdocs Coding Question | Array Sum with twist

Hello friends, I hope you all are doing great. In this post today we will learn to solve the  coding questions which is being asked by the Amdocs company recently. If you are preparing for selection in any company then this post will help you definitely.

Lets Start

Click here to get All Company Coding + Interview Questions and Programs All Coding Programs

Array Sum with twist

In this task you are requested to find whether the sum of two numbers in given array equals to a given sum. If such numbers exist you should return them as a tuple , If not you should return -1.

For example: given the following array:

[1,3,6,8,9]

and the following sum:17

The function should return (8,9) or (9,8).

Example :-

  • Input:
    • [1,3,6,8,9]
    • 17
  • Output:
    • (8,9) or (9,8)

Explanation:-

8+9=17 , 9+8=17

Code in Python

# code in Python
def ArraySum(lst, sum):
    top = []
    for i in range(len(lst) - 1):
        if (sum == (lst[i] + lst[i + 1])):
            top.append(lst[i])
            top.append(lst[i + 1])
    if(len(top)==0):
        return -1
    else:
        return tuple(top)


# Driver code
print(ArraySum([1, 3, 6, 8, 9], 17))

# Output (8, 9)

Code in Java
import java.util.*;

class ArraySum {

    static String arraySum(int ar[], int sum) {
        String s = "";
        for (int i = 0; i < ar.length - 1; i++) {
            if (sum == (ar[i] + ar[i + 1])) {
                s = s + "(" + ar[i] + "," + ar[i + 1] + ")";
            }
        }
        return s.length()>0?s:-1+"";
    }

    public static void main(String[] args) {
        int ar[] = {1, 3, 6, 8, 9};
        int sum = 17;
        System.out.println(arraySum(ar, sum));
    }
}
/*
OUTPUT
(8,9)
 */

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 

If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thanks

Post a Comment

0 Comments