Find the integers that doesnot ends with T1 or T2 when squared and added X
Given an array of N integers. Given two single digit numbers T1 and T2 and a number X. The task is to find out the integers among them that doesnot ends with either T1 or T2 when they are squared and X is added to them. If there exists no such integer, print -1.
Examples:
Input: N = 4, arr[] = {3, 1, 4, 7} X = 10, T1 = 5, T2 = 6
Output: 19 11 59
Explanation :
The modified value of the 3 is 19 (3^2 + 10).
The modified value of the 1 is 11 (1^2 + 10).
The modified value of the 4 is 26 (4^2 + 10).
The modified value of the 7 is 59 (7^2 + 10).
The modified values which do not end with 5 or 6
are 19, 11 and 59.
Hence the output is 19 11 59.
Input: N = 4, arr[] = {2, 18, 22, 8} X = 2, T1 = 5, T2 = 6
Output: -1
Explanation:
The modified value of the 2 is 6 (2^2 + 2).
The modified value of the 18 is 326 (18^2 + 2).
The modified value of the 22 is 486 (22^2 + 2).
The modified value of the 8 is 66 (8^2 + 2).
As, there are no modified values
which do not end with 5 or 6.
Hence the output is -1.
Approach:
- Initialize a Boolean variable flag as true.
- Traverse through the elements in the array a[n].
- Store the sum of X and the square of a[i] in a variable temp.
- Check if the last digit in temp is neither T1 nor T2.
- IF yes then print the value in temp and change the flag to false.
- After traversing through all the elements in the array if the flag is true then print -1.
Below is the implementation of the above approach:
C++
// C++ program to find the integers // that ends with either T1 or T2 // when squared and added X #include <bits/stdc++.h> using namespace std; // Function to print the elements // Not ending with T1 or T2 void findIntegers(int n, int a[], int x, int t1, int t2) { // Flag to check if none of the elements // Do not end with t1 or t2 bool flag = true; // Traverse through all the elements for (int i = 0; i < n; i++) { // Temporary variable to store the value int temp = pow(a[i], 2) + x; // If the last digit is neither t1 // nor t2 then if (temp % 10 != t1 && temp % 10 != t2) { // Print the number cout << temp << " "; // Set the flag as False flag = false; } } // If none of the elements // meets the specification if (flag) cout << "-1"; } // Driver Code int main() { // Test case 1 int N = 4, X = 10, T1 = 5, T2 = 6; int a[N] = { 3, 1, 4, 7 }; // Call the function findIntegers(N, a, X, T1, T2); cout << endl; // Test case 2 N = 4, X = 2, T1 = 5, T2 = 6; int b[N] = { 2, 18, 22, 8 }; // Call the function findIntegers(N, b, X, T1, T2); return 0; } |
Java
// Java program to find the integers // that ends with either T1 or T2 // when squared and added X class GFG { // Function to print the elements // Not ending with T1 or T2 static void findIntegers(int n, int a[], int x, int t1, int t2) { // Flag to check if none of the elements // Do not end with t1 or t2 boolean flag = true; // Traverse through all the elements for (int i = 0; i < n; i++) { // Temporary variable to store the value int temp = (int)Math.pow(a[i], 2) + x; // If the last digit is neither t1 // nor t2 then if (temp % 10 != t1 && temp % 10 != t2) { // Print the number System.out.print(temp + " "); // Set the flag as False flag = false; } } // If none of the elements // meets the specification if (flag) { System.out.println(); System.out.print("-1"); } } // Driver Code public static void main(String args[]) { // Test case 1 int N = 4; int X = 10; int T1 = 5; int T2 = 6; int a[] = { 3, 1, 4, 7 }; // Call the function findIntegers(N, a, X, T1, T2); // Test case 2 N = 4; X = 2; T1 = 5; T2 = 6; int b[] = { 2, 18, 22, 8 }; // Call the function findIntegers(N, b, X, T1, T2); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 program to find the integers # that ends with either T1 or T2 # when squared and added X # Function to print the elements # Not ending with T1 or T2 def findIntegers(n, a, x, t1, t2): # Flag to check if none of the elements # Do not end with t1 or t2 flag = True # Traverse through all the elements for i in range(n): # Temporary variable to store the value temp = pow(a[i], 2) + x # If the last digit is neither t1 # nor t2 then if(temp % 10 != t1 and temp % 10 != t2): # Print the number print(temp, end = " ") # Set the flag as False flag = False # If none of the elements # meets the specification if flag: print(-1) # Driver Code # Test case 1 N , X , T1 , T2 = 4 , 10 , 5 , 6a = [ 3, 1, 4, 7 ] # Call the function findIntegers(N, a, X, T1, T2); print() # Test case 2 N , X , T1 , T2 = 4 , 2 , 5 , 6b = [ 2, 18, 22, 8 ] # Call the function findIntegers(N, b, X, T1, T2) # This code is contributed by divyamohan123 |
C#
// C# program to find the integers // that ends with either T1 or T2 // when squared and added X using System; class GFG { // Function to print the elements // Not ending with T1 or T2 static void findIntegers(int n, int []a, int x, int t1, int t2) { // Flag to check if none of the elements // Do not end with t1 or t2 bool flag = true; // Traverse through all the elements for (int i = 0; i < n; i++) { // Temporary variable to store the value int temp = (int)Math.Pow(a[i], 2) + x; // If the last digit is neither t1 // nor t2 then if (temp % 10 != t1 && temp % 10 != t2) { // Print the number Console.Write(temp + " "); // Set the flag as False flag = false; } } // If none of the elements // meets the specification if (flag) { Console.WriteLine(); Console.Write("-1"); } } // Driver Code public static void Main(String []args) { // Test case 1 int N = 4; int X = 10; int T1 = 5; int T2 = 6; int []a = { 3, 1, 4, 7 }; // Call the function findIntegers(N, a, X, T1, T2); // Test case 2 N = 4; X = 2; T1 = 5; T2 = 6; int []b = { 2, 18, 22, 8 }; // Call the function findIntegers(N, b, X, T1, T2); } } // This code is contributed by 29AjayKumar |
19 11 59 -1
Recommended Posts:
- Find the minimum value to be added so that array becomes balanced
- Find the repeating and the missing | Added 3 new methods
- Find amount to be added to achieve target ratio in a given mixture
- Squared triangular number (Sum of cubes)
- Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d)
- Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
- Find whether an array is subset of another array | Added Method 3
- Find the prime P using given four integers
- Find the number of integers from 1 to n which contains digits 0's and 1's only
- Find the first N integers such that the sum of their digits is equal to 10
- Find N integers with given difference between product and sum
- Find three integers less than or equal to N such that their LCM is maximum
- Find the largest interval that contains exactly one of the given N integers.
- Find the number which when added to the given ratio a : b, the ratio changes to c : d
- Find the first repeating element in an array of integers
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.



