# Check if all array elements can be reduced to less than X

Given an array **A[]** consisting of **N** positive integers and an integer **X**, the task is to determine if it is possible to convert all array elements to less than **X** by performing the following operations:

- Select 2 distinct indices
**j**and**k**. - Select an index
**i**, where**A[i] > X**. - Replace
**A[i] = gcd(A[j], A[k])**if and only if gcd(A[j], A[k]) ≠ 1.

**Examples:**

Input:A[] = {2, 1, 5, 3, 6}, X = 4Output:YesExplanation:

Selecting i = 3, j = 4, k = 5, set A[i] = gcd(A[j], A[k]) = 3. Therefore, A[] modifies to {2, 1, 3, 3, 6}.Selecting i = 5, j = 4, k = 5, set A[i] = gcd(A[j], A[k]) = 3. Therefore, A[] modifies to {2, 1, 3, 3, 3}.

Input:A[] = {2, 3, 2, 5, 4}, X = 3Output:Yes

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

*Find the two numbers having*these**gcd ≠ 1**as well as**gcd ≤ X**, then, by using*two numbers, the required number***A[i]**can be replaced with**gcd(A[j], A[k])**.- Using the fact that
**gcd(x, y) ≤ min(x, y)**, the array elements can be reduced to**≤ X**. - This way, the rest of the array can be converted to
**≤ X**using step**2**.

Below is the implementation of the above approach:

## C++

`// C++ program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to check if all array` `// elements are≤ X` `bool` `check(` `int` `A[], ` `int` `X, ` `int` `N)` `{` ` ` `for` `(` `int` `i = 0; i < N; i++)` ` ` `{` ` ` `if` `(A[i] > X)` ` ` `{` ` ` `return` `false` `;` ` ` `}` ` ` `}` ` ` `return` `true` `;` `}` `// Function to check if all array elements` `// can be reduced to less than X or not` `bool` `findAns(` `int` `A[], ` `int` `N, ` `int` `X)` `{` ` ` ` ` `// Checks if all array elements` ` ` `// are already ≤ X or not` ` ` `if` `(check(A, X, N))` ` ` `{` ` ` `return` `true` `;` ` ` `}` ` ` `// Traverse every possible pair` ` ` `for` `(` `int` `i = 0; i < N; i++)` ` ` `{` ` ` `for` `(` `int` `j = i + 1; j < N; j++)` ` ` `{` ` ` ` ` `// Calculate GCD of two` ` ` `// array elements` ` ` `int` `g = __gcd(A[i], A[j]);` ` ` `// If gcd is ≠ 1` ` ` `if` `(g != 1)` ` ` `{` ` ` ` ` `// If gcd is ≤ X, then a pair` ` ` `// is present to reduce all` ` ` `// array elements to ≤ X` ` ` `if` `(g <= X)` ` ` `{` ` ` `return` `true` `;` ` ` `}` ` ` `}` ` ` `}` ` ` `}` ` ` ` ` `// If no pair is present` ` ` `// with gcd is ≤ X` ` ` `return` `false` `;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `X = 4;` ` ` `int` `A[] = { 2, 1, 5, 3, 6 };` ` ` `int` `N = 5;` ` ` ` ` `if` `(findAns(A, N, X))` ` ` `{` ` ` `cout << ` `"true"` `;` ` ` `}` ` ` `else` ` ` `{` ` ` `cout << ` `"false"` `;` ` ` `}` `}` `// This code is contributed by mohit kumar 29` |

## Java

`// Java Program to implement` `// the above approach` `import` `java.io.*;` `import` `java.util.Arrays;` `class` `GFG {` ` ` `// Function to check if all array elements` ` ` `// can be reduced to less than X or not` ` ` `public` `static` `boolean` `findAns(` ` ` `int` `[] A, ` `int` `N, ` `int` `X)` ` ` `{` ` ` `// Checks if all array elements` ` ` `// are already ≤ X or not` ` ` `if` `(check(A, X)) {` ` ` `return` `true` `;` ` ` `}` ` ` `// Traverse every possible pair` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) {` ` ` `for` `(` `int` `j = i + ` `1` `; j < N; j++) {` ` ` `// Calculate GCD of two` ` ` `// array elements` ` ` `int` `gcd = gcd(A[i], A[j]);` ` ` `// If gcd is ≠ 1` ` ` `if` `(gcd != ` `1` `) {` ` ` `// If gcd is ≤ X, then a pair` ` ` `// is present to reduce all` ` ` `// array elements to ≤ X` ` ` `if` `(gcd <= X) {` ` ` `return` `true` `;` ` ` `}` ` ` `}` ` ` `}` ` ` `}` ` ` `// If no pair is present` ` ` `// with gcd is ≤ X` ` ` `return` `false` `;` ` ` `}` ` ` `// Function to check if all array elements are≤ X` ` ` `public` `static` `boolean` `check(` `int` `[] A, ` `int` `X)` ` ` `{` ` ` `for` `(` `int` `i = ` `0` `; i < A.length; i++) {` ` ` `if` `(A[i] > X) {` ` ` `return` `false` `;` ` ` `}` ` ` `}` ` ` `return` `true` `;` ` ` `}` ` ` `// Function to calculate gcd of two numbers` ` ` `public` `static` `int` `gcd(` `int` `a, ` `int` `b)` ` ` `{` ` ` `if` `(b == ` `0` `)` ` ` `return` `a;` ` ` `return` `gcd(b, a % b);` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `X = ` `4` `;` ` ` `int` `[] A = { ` `2` `, ` `1` `, ` `5` `, ` `3` `, ` `6` `};` ` ` `int` `N = ` `5` `;` ` ` `System.out.println(findAns(A, N, X));` ` ` `}` `}` |

## Python3

`# Python3 program to implement` `# the above approach` `# Function to check if all array elements` `# can be reduced to less than X or not` `def` `findAns(A, N, X):` ` ` ` ` `# Checks if all array elements` ` ` `# are already ≤ X or not` ` ` `if` `(check(A, X)):` ` ` `return` `True` ` ` ` ` `# Traverse every possible pair` ` ` `for` `i ` `in` `range` `(N):` ` ` `for` `j ` `in` `range` `(i ` `+` `1` `, N):` ` ` ` ` `# Calculate GCD of two` ` ` `# array elements` ` ` `gcd ` `=` `GCD(A[i], A[j])` ` ` `# If gcd is ≠ 1` ` ` `if` `(gcd !` `=` `1` `):` ` ` ` ` `# If gcd is ≤ X, then a pair` ` ` `# is present to reduce all` ` ` `# array elements to ≤ X` ` ` `if` `(gcd <` `=` `X):` ` ` `return` `True` ` ` `# If no pair is present` ` ` `# with gcd is ≤ X` ` ` `return` `False` `# Function to check if all array elements are≤ X` `def` `check(A, X):` ` ` `for` `i ` `in` `range` `(` `len` `(A)):` ` ` `if` `(A[i] > X):` ` ` `return` `False` ` ` `return` `True` `# Function to calculate gcd of two numbers` `def` `GCD(a, b):` ` ` `if` `(b ` `=` `=` `0` `):` ` ` `return` `a` ` ` `return` `GCD(b, a ` `%` `b)` `# Driver Code` `X ` `=` `4` `A ` `=` `[ ` `2` `, ` `1` `, ` `5` `, ` `3` `, ` `6` `]` `N ` `=` `5` `print` `(findAns(A, N, X))` `# This code is contributed by rohitsingh07052` |

## C#

`// C# Program to implement` `// the above approach` `using` `System;` `class` `GFG {` ` ` `// Function to check if all array elements` ` ` `// can be reduced to less than X or not` ` ` `public` `static` `bool` `findAns(` ` ` `int` `[] A, ` `int` `N, ` `int` `X)` ` ` `{` ` ` `// Checks if all array elements` ` ` `// are already ≤ X or not` ` ` `if` `(check(A, X))` ` ` `{` ` ` `return` `true` `;` ` ` `}` ` ` `// Traverse every possible pair` ` ` `for` `(` `int` `i = 0; i < N; i++)` ` ` `{` ` ` `for` `(` `int` `j = i + 1; j < N; j++)` ` ` `{` ` ` `// Calculate GCD of two` ` ` `// array elements` ` ` `int` `gcd = gcdFoo(A[i], A[j]);` ` ` `// If gcd is ≠ 1` ` ` `if` `(gcd != 1)` ` ` `{` ` ` `// If gcd is ≤ X, then a pair` ` ` `// is present to reduce all` ` ` `// array elements to ≤ X` ` ` `if` `(gcd <= X)` ` ` `{` ` ` `return` `true` `;` ` ` `}` ` ` `}` ` ` `}` ` ` `}` ` ` `// If no pair is present` ` ` `// with gcd is ≤ X` ` ` `return` `false` `;` ` ` `}` ` ` `// Function to check if all array elements are≤ X` ` ` `public` `static` `bool` `check(` `int` `[] A, ` `int` `X)` ` ` `{` ` ` `for` `(` `int` `i = 0; i < A.Length; i++)` ` ` `{` ` ` `if` `(A[i] > X)` ` ` `{` ` ` `return` `false` `;` ` ` `}` ` ` `}` ` ` `return` `true` `;` ` ` `}` ` ` `// Function to calculate gcd of two numbers` ` ` `static` `int` `gcdFoo(` `int` `a, ` `int` `b)` ` ` `{` ` ` `if` `(b == 0)` ` ` `return` `a;` ` ` `return` `gcdFoo(b, a % b);` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `Main(String[] args)` ` ` `{` ` ` `int` `X = 4;` ` ` `int` `[] A = { 2, 1, 5, 3, 6 };` ` ` `int` `N = 5;` ` ` `Console.WriteLine(findAns(A, N, X));` ` ` `}` `}` `// This code is contributed by 29AjayKumar` |

## Javascript

`<script>` `// javascript program of the above approach` ` ` `// Function to check if all array elements` ` ` `// can be reduced to less than X or not` ` ` `function` `findAns(` ` ` `A, N, X)` ` ` `{` ` ` `// Checks if all array elements` ` ` `// are already ≤ X or not` ` ` `if` `(check(A, X)) {` ` ` `return` `true` `;` ` ` `}` ` ` ` ` `// Traverse every possible pair` ` ` `for` `(let i = 0; i < N; i++) {` ` ` `for` `(let j = i + 1; j < N; j++) {` ` ` ` ` `// Calculate GCD of two` ` ` `// array elements` ` ` `let gcdd = gcd(A[i], A[j]);` ` ` ` ` `// If gcd is ≠ 1` ` ` `if` `(gcdd != 1) {` ` ` ` ` `// If gcd is ≤ X, then a pair` ` ` `// is present to reduce all` ` ` `// array elements to ≤ X` ` ` `if` `(gcdd <= X) {` ` ` ` ` `return` `true` `;` ` ` `}` ` ` `}` ` ` `}` ` ` `}` ` ` ` ` `// If no pair is present` ` ` `// with gcd is ≤ X` ` ` `return` `false` `;` ` ` `}` ` ` ` ` `// Function to check if all array elements are≤ X` ` ` `function` `check(A, X)` ` ` `{` ` ` `for` `(let i = 0; i < A.length; i++) {` ` ` `if` `(A[i] > X) {` ` ` `return` `false` `;` ` ` `}` ` ` `}` ` ` `return` `true` `;` ` ` `}` ` ` ` ` `// Function to calculate gcd of two numbers` ` ` `function` `gcd(a, b)` ` ` `{` ` ` `if` `(b == 0)` ` ` `return` `a;` ` ` `return` `gcd(b, a % b);` ` ` `}` ` ` ` ` `// Driver Code` ` ` ` ` `let X = 4;` ` ` `let A = [ 2, 1, 5, 3, 6 ];` ` ` `let N = 5;` ` ` ` ` `document.write(findAns(A, N, X));` `// This code is contributed by target_2.` `</script>` |

**Output**

true

**Time Complexity: **O(N^{2}) **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. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.