Given an array of integers **arr[]** and two integers** X** and** Y**, the task is to check if it is possible to obtain a sequence having sum **X** such that the sum of each element of the subsequence multiplied by an array element is equal to **Y**. **Note:** Here X is always less than Y.

**Examples:**

Input:arr[] = {1, 2, 7, 9, 10}, X = 11, Y = 13Output:YesExplanation:

The given value of X( = 11) can be split into a sequence {9, 2} such that 9 * 1(= arr[0] + 2 * 2(= arr[1]) = 13( = Y)

Input:arr[] ={1, 3, 5, 7}, X = 27, Y = 34Output:No

**Approach: **Follow the steps below in order to solve the problem:

- Calculate the
**difference**between**Y**and**X**. - For every array element
**arr[i]**, which is**> 1**, update**(Y – X) % (arr[i] – 1)**. - If the
**difference**is reduced 0, print “**Yes**“. Otherwise, print**“No”**.

Below is the implementation of the above approach:

`// C++ Program to implement ` `// the above approach ` `#include <iostream> ` `using` `namespace` `std; `
` ` `// Function to check if it is possible ` `// to obtain sum Y from a sequence of ` `// sum X from the array arr[] ` `void` `solve(` `int` `arr[], ` `int` `n, ` `int` `X, ` `int` `Y) `
`{ ` ` ` ` ` `// Store the difference `
` ` `int` `diff = Y - X; `
` ` ` ` `// Iterate over the array `
` ` `for` `(` `int` `i = 0; i < n; i++) { `
` ` ` ` `if` `(arr[i] != 1) { `
` ` `diff = diff % (arr[i] - 1); `
` ` `} `
` ` `} `
` ` ` ` `// If diff reduced to 0 `
` ` `if` `(diff == 0) `
` ` `cout << ` `"Yes"` `; `
` ` `else`
` ` `cout << ` `"No"` `; `
`} ` ` ` `// Driver Code ` `int` `main() `
`{ ` ` ` `int` `arr[] = { 1, 2, 7, 9, 10 }; `
` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); `
` ` `int` `X = 11, Y = 13; `
` ` `solve(arr, n, X, Y); `
` ` ` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`// Java program to implement ` `// the above approach ` `class` `GFG{ `
` ` `// Function to check if it is possible ` `// to obtain sum Y from a sequence of ` `// sum X from the array arr[] ` `static` `void` `solve(` `int` `arr[], ` `int` `n, `
` ` `int` `X, ` `int` `Y) `
`{ ` ` ` ` ` `// Store the difference `
` ` `int` `diff = Y - X; `
` ` ` ` `// Iterate over the array `
` ` `for` `(` `int` `i = ` `0` `; i < n; i++) `
` ` `{ `
` ` `if` `(arr[i] != ` `1` `) `
` ` `{ `
` ` `diff = diff % (arr[i] - ` `1` `); `
` ` `} `
` ` `} `
` ` ` ` `// If diff reduced to 0 `
` ` `if` `(diff == ` `0` `) `
` ` `System.out.print( ` `"Yes"` `); `
` ` `else`
` ` `System.out.print(` `"No"` `); `
`} ` ` ` `// Driver Code ` `public` `static` `void` `main (String []args) `
`{ ` ` ` `int` `arr[] = { ` `1` `, ` `2` `, ` `7` `, ` `9` `, ` `10` `}; `
` ` `int` `n = arr.length; `
` ` `int` `X = ` `11` `, Y = ` `13` `; `
` ` ` ` `solve(arr, n, X, Y); `
`} ` `} ` ` ` `// This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

`# Python3 program to implement ` `# the above approach ` ` ` `# Function to check if it is possible ` `# to obtain sum Y from a sequence of ` `# sum X from the array arr[] ` `def` `solve(arr, n, X, Y): `
` ` ` ` `# Store the difference `
` ` `diff ` `=` `Y ` `-` `X `
` ` ` ` `# Iterate over the array `
` ` `for` `i ` `in` `range` `(n): `
` ` `if` `(arr[i] !` `=` `1` `): `
` ` `diff ` `=` `diff ` `%` `(arr[i] ` `-` `1` `) `
` ` ` ` `# If diff reduced to 0 `
` ` `if` `(diff ` `=` `=` `0` `): `
` ` `print` `(` `"Yes"` `) `
` ` `else` `: `
` ` `print` `(` `"No"` `) `
` ` `# Driver Code ` `arr ` `=` `[ ` `1` `, ` `2` `, ` `7` `, ` `9` `, ` `10` `] `
`n ` `=` `len` `(arr) `
`X, Y ` `=` `11` `, ` `13`
` ` `# Function call ` `solve(arr, n, X, Y) ` ` ` `# This code is contributed by Shivam Singh` |

*chevron_right*

*filter_none*

`// C# program to implement ` `// the above approach ` `using` `System; `
` ` `class` `GFG{ `
` ` `// Function to check if it is possible ` `// to obtain sum Y from a sequence of ` `// sum X from the array []arr ` `static` `void` `solve(` `int` `[]arr, ` `int` `n, `
` ` `int` `X, ` `int` `Y) `
`{ ` ` ` ` ` `// Store the difference `
` ` `int` `diff = Y - X; `
` ` ` ` `// Iterate over the array `
` ` `for` `(` `int` `i = 0; i < n; i++) `
` ` `{ `
` ` `if` `(arr[i] != 1) `
` ` `{ `
` ` `diff = diff % (arr[i] - 1); `
` ` `} `
` ` `} `
` ` ` ` `// If diff reduced to 0 `
` ` `if` `(diff == 0) `
` ` `Console.Write(` `"Yes"` `); `
` ` `else`
` ` `Console.Write(` `"No"` `); `
`} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String []args) `
`{ ` ` ` `int` `[]arr = { 1, 2, 7, 9, 10 }; `
` ` `int` `n = arr.Length; `
` ` `int` `X = 11, Y = 13; `
` ` ` ` `solve(arr, n, X, Y); `
`} ` `} ` ` ` `// This code is contributed by Amit Katiyar` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Time Complexity:** O(N) **Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if K can be obtained by performing arithmetic operations on any permutation of an Array
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Maximum value obtained by performing given operations in an Array
- Arrange the array such that upon performing given operations an increasing order is obtained
- Check if N can be obtained by repetitive addition or subtraction of two given numbers
- Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings
- Maximum array sum that can be obtained after exactly k changes
- Kth character from the Nth string obtained by the given operations
- Check if N can be obtained from 1 by repetitively multiplying by 10 or 20
- Check if a string can be obtained by appending subsequences of another string
- Check if given array can be made 0 with given operations performed any number of times
- Array obtained by repeatedly reversing array after every insertion from given array
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Maximum possible score that can be obtained by constructing a Binary Tree based on given conditions
- Find maximum points which can be obtained by deleting elements from array
- Smallest array that can be obtained by replacing adjacent pairs with their products
- Sum of two numbers if the original ratio and new ratio obtained by adding a given number to each number is given
- Minimum sum obtained from groups of four elements from the given array
- Check if array sum can be made K by three operations on it
- Check if all array elements can be converted to K using given operations

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.