Preview
Open Original
case 1 : Reverse an array using another array
class Main {
public static void main(String[] args) {
int [] num = {10,11,12,13,14,15,16};
System.out.println("Original Array :");
for(int i=0;i<num.length;i++){
System.out.print(num[i]+" ");
}
//Create a result array to hold the required values, having the same length as num.
int [] result = new int[num.length];
// reverse the array here
for(int i=num.length-1,j=0;i>=0;i--){
result[j++]=num[i];
}
System.out.println();
System.out.println("Reversed Array :");
// print the resultant array
for(int i=0;i<result.length;i++){
System.out.print(result[i]+" ");
}
}
}
case 2 : Reverse an array in-place without extra space
- A palindrome number is a number which remains the same when it is reversed.
class Main {
public s...
case 1 : Reverse an array using another array
class Main {
public static void main(String[] args) {
int [] num = {10,11,12,13,14,15,16};
System.out.println("Original Array :");
for(int i=0;i<num.length;i++){
System.out.print(num[i]+" ");
}
//Create a result array to hold the required values, having the same length as num.
int [] result = new int[num.length];
// reverse the array here
for(int i=num.length-1,j=0;i>=0;i--){
result[j++]=num[i];
}
System.out.println();
System.out.println("Reversed Array :");
// print the resultant array
for(int i=0;i<result.length;i++){
System.out.print(result[i]+" ");
}
}
}
case 2 : Reverse an array in-place without extra space
- A palindrome number is a number which remains the same when it is reversed.
class Main {
public static void main(String[] args) {
int [] num = {10,11,12,13,14,15,16};
System.out.println("Original Array :");
for(int i=0;i<num.length;i++){
System.out.print(num[i]+" ");
}
System.out.println();
int left =0;
int right = num.length-1;
while(left < right){
num[left] = (num[left]+num[right])-(num[right]=num[left]);
left++; right--;
}
System.out.println("Reversed Array :");
for(int i=0;i<num.length;i++){
System.out.print(num[i]+" ");
}
}
}
case 3 : Check whether the given string is a palindrome or not
class Main {
public static void main(String[] args) {
String str = "abcdcba";
int left =0;
int right=str.length()-1;
boolean flag = true;
while(left < right){
if(str.charAt(left)!=str.charAt(right)){
flag=false;
break;
}
left++; right--;
}
if(flag==true){
System.out.println(str+" is palindrome number");
}
else{
System.out.println(str+" is not palindrome number");
}
}
}