mistakes in problems
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
class Main {
public static void main(String[] args) {
int [] arr={1,2,3,1};
boolean tr=false;
int count=0;
for(int i=0;i<arr.length;i++){
for(int j=arr.length-1;j>0;j--){
if(arr[i]==arr[j]){
count++;
if(count==arr.length){
tr=true;
}
}
}
}
System.out.println((tr)?"palindrome":"not palindrome");
}
}//my logic flawed becase 1231 is not a palindrome
//corrected by chatgt
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
class Main {
public static void main(String[] args) {
int [] arr={1,2,2,1};
boolean tr=true;
int count=0;
for(int i=0;i<arr.length;i++){
for(int j=arr.length-1;j>0;j--){
if(i+j==arr.length-1){
if(arr[i]!=arr[j]){
tr=false;
}
}
}
}
System.out.println((tr)?"palindrome":"not palindrome");
}
}
//copilot
int[] arr = {1, 2, 2, 1};
boolean tr = false;
int count = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = arr.length - 1; j > 0; j--) {
if (arr[i] == arr[j]) {
count++;
if (count == arr.length) {
tr = true;
}
}
}
}
System.out.println((tr) ? "palindrome" : "not palindrome");
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
class Main {
public static void main(String[] args) {
int [] arr={1,2,3,4,5};
int index=0;
int [] temp=new int [arr.length];//right rotate by k steps
int k=2;
if(k<arr.length){
for(int i=arr.length-k;i>0;i--){
temp[index++]=arr[i-1];
}
for(int i=arr.length-1;i>k;i--){
temp[i]=arr[i];
}
}
for(int ele:temp){
System.out.println(ele);
}
}
}//wrong apporach
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int k = 2;
// Normalize k in case it's larger than array length
k = k % arr.length;
int[] temp = new int[arr.length];
for(int i=0;i<k;i++){
temp[i]=arr[arr.length-k+i];
}
for(int i=k;i<arr.length;i++){
temp[i]=arr[i-k];
}
for(int ele:temp){
System.out.println(ele);
}
}
}
Comments
Post a Comment