Finding Reverse in java
Task -
For this challenge, you will be given an integer and an array and you are asked to find the reverse of the number in the array.
Input Format -
you will be taking a number as an input from stdin which tells about the length of the array. On another line, array elements should be there with single space between them. Next line, there is an integer whose reverse need to be found.
Output Format -
print 'True' if the reverse of the number is present in the array otherwise return 'False'.
Sample Test Case:
Sample Input:-6
78 92 44 63 71 97
29
Sample Output -
True
Explanation:
Given integer is 29 and the reverse of the number is 92 and it is present in the array. Hence, print 'True'.
For this challenge, you will be given an integer and an array and you are asked to find the reverse of the number in the array.
Input Format -
you will be taking a number as an input from stdin which tells about the length of the array. On another line, array elements should be there with single space between them. Next line, there is an integer whose reverse need to be found.
Output Format -
print 'True' if the reverse of the number is present in the array otherwise return 'False'.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* For this challenge, you will be given an integer and an array | |
* and you are asked to find the reverse of the number in the array. | |
* | |
* you will be taking a number as an input from stdin which tells about | |
* the length of the array. On another line, array elements should be | |
* there with single space between them. Next line, there is an integer | |
* whose reverse need to be found. | |
* | |
* Output Format - | |
print 'True' if the reverse of the number is present in the array otherwise return 'False'. | |
Sample Test Case: | |
Sample Input:- | |
6 | |
78 92 44 63 71 97 | |
29 | |
Sample Output - | |
True | |
Explanation: | |
Given integer is 29 and the reverse of the number is 92 and it is present in the array. Hence, print 'True'. | |
*/ | |
import java.util.*; | |
import java.io.*; | |
public class FindReverse { | |
public static void main(String[] args) { | |
Scanner sc=new Scanner(System.in); | |
int size; | |
size=sc.nextInt(); | |
int [] arr=new int[size]; | |
for(int i=0;i<size;i++){ | |
arr[i]=sc.nextInt(); | |
} | |
sc.nextLine(); | |
String rev=sc.nextLine().trim(); | |
StringBuilder find=new StringBuilder(); | |
find=find.append(rev); //a process of reversing number | |
find=find.reverse(); //number to be find | |
boolean isExist=false; | |
for(int j=0;j<arr.length;j++){ | |
if(arr[j]==Integer.parseInt(String.valueOf(find))){ //check whether array contains reversed number | |
isExist=true; //if passed, make output as true and set a break; | |
break; | |
} | |
} | |
System.out.println(isExist?"True":"False"); //print the output. | |
sc.close(); | |
} | |
} |
Sample Input:-6
78 92 44 63 71 97
29
Sample Output -
True
Explanation:
Given integer is 29 and the reverse of the number is 92 and it is present in the array. Hence, print 'True'.
Comments
Post a Comment