Find the integers that doesnot ends with T1 or T2 when squared and added X
Last Updated :
30 Aug, 2022
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++
#include <bits/stdc++.h>
using namespace std;
void findIntegers( int n, int a[],
int x, int t1, int t2)
{
bool flag = true ;
for ( int i = 0; i < n; i++) {
int temp = pow (a[i], 2) + x;
if (temp % 10 != t1 && temp % 10 != t2) {
cout << temp << " " ;
flag = false ;
}
}
if (flag)
cout << "-1" ;
}
int main()
{
int N = 4, X = 10, T1 = 5, T2 = 6;
int a[N] = { 3, 1, 4, 7 };
findIntegers(N, a, X, T1, T2);
cout << endl;
N = 4, X = 2, T1 = 5, T2 = 6;
int b[N] = { 2, 18, 22, 8 };
findIntegers(N, b, X, T1, T2);
return 0;
}
|
Java
class GFG
{
static void findIntegers( int n, int a[],
int x, int t1, int t2)
{
boolean flag = true ;
for ( int i = 0 ; i < n; i++)
{
int temp = ( int )Math.pow(a[i], 2 ) + x;
if (temp % 10 != t1 && temp % 10 != t2)
{
System.out.print(temp + " " );
flag = false ;
}
}
if (flag)
{
System.out.println();
System.out.print( "-1" );
}
}
public static void main(String args[])
{
int N = 4 ;
int X = 10 ;
int T1 = 5 ;
int T2 = 6 ;
int a[] = { 3 , 1 , 4 , 7 };
findIntegers(N, a, X, T1, T2);
N = 4 ; X = 2 ; T1 = 5 ; T2 = 6 ;
int b[] = { 2 , 18 , 22 , 8 };
findIntegers(N, b, X, T1, T2);
}
}
|
Python3
def findIntegers(n, a, x, t1, t2):
flag = True
for i in range (n):
temp = pow (a[i], 2 ) + x
if (temp % 10 ! = t1 and
temp % 10 ! = t2):
print (temp, end = " " )
flag = False
if flag:
print ( - 1 )
N , X , T1 , T2 = 4 , 10 , 5 , 6
a = [ 3 , 1 , 4 , 7 ]
findIntegers(N, a, X, T1, T2);
print ()
N , X , T1 , T2 = 4 , 2 , 5 , 6
b = [ 2 , 18 , 22 , 8 ]
findIntegers(N, b, X, T1, T2)
|
C#
using System;
class GFG
{
static void findIntegers( int n, int []a,
int x, int t1, int t2)
{
bool flag = true ;
for ( int i = 0; i < n; i++)
{
int temp = ( int )Math.Pow(a[i], 2) + x;
if (temp % 10 != t1 &&
temp % 10 != t2)
{
Console.Write(temp + " " );
flag = false ;
}
}
if (flag)
{
Console.WriteLine();
Console.Write( "-1" );
}
}
public static void Main(String []args)
{
int N = 4;
int X = 10;
int T1 = 5;
int T2 = 6;
int []a = { 3, 1, 4, 7 };
findIntegers(N, a, X, T1, T2);
N = 4; X = 2; T1 = 5; T2 = 6;
int []b = { 2, 18, 22, 8 };
findIntegers(N, b, X, T1, T2);
}
}
|
Javascript
<script>
function findIntegers(n, a, x, t1, t2)
{
let flag = true ;
for (let i = 0; i < n; i++) {
let temp = Math.pow(a[i], 2) + x;
if (temp % 10 != t1 && temp % 10 != t2) {
document.write(temp + " " );
flag = false ;
}
}
if (flag)
document.write( "-1" );
}
let N = 4, X = 10, T1 = 5, T2 = 6;
let a = [ 3, 1, 4, 7 ];
findIntegers(N, a, X, T1, T2);
document.write( "<br>" );
N = 4, X = 2, T1 = 5, T2 = 6;
let b = [ 2, 18, 22, 8 ];
findIntegers(N, b, X, T1, T2);
</script>
|
Time Complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(1) as no extra space is being used.
Share your thoughts in the comments
Please Login to comment...