20. Program
Find out sequential 3 index where difference of first two should be same as third ?
class C
{
public static void main(String args[])
{
int[] a = {4, 3, 1, 2, 0, 2, 3, 1, 2, 10};
int x, y, z;
for(int i = 0; i < a.length - 2; i++)
{
x = a[i];
y = a[i + 1];
z = a[i + 2];
if((x - y) == z)
{
System.out.print((i));
System.out.print((i+1));
System.out.println((i+2) + ",");
}
}
}
}
21. Program
(Imp_Q) Write a program which has to reverse an elements of an array ?
class E
{
public static void main(String args[])
{
int[] a = {2, 4, 4, 2, 4, 7, 4, 5};
int mid = a.length / 2;
for(int i = 0; i < a.length; i++)
{
System.out.print(a[i] + " ,");
}
System.out.println();
for(int i = 0; i < mid; i++)
{
int temp = a[i];
a[i] = a[a.length - i - 1];
a[a.length - i - 1] = temp;
}
for(int i = 0; i < a.length; i++)
{
System.out.print(a[i] + " ,");
}
System.out.println();
}
}
22. Program
Write a program which has to left shift one elements in the array ?
class F
{
public static void main(String args[])
{
int[] a = {2, 4, 4, 2, 4, 7, 4, 5};
for(int i = 0; i < a.length; i++)
{
System.out.print(a[i] + " ,");
}
System.out.println();
for(int i = 1; i < a.length; i++)
{
a[i-1] = a[i];
}
for(int i = 0; i < a.length; i++)
{
System.out.print(a[i] + " ,");
}
System.out.println();
}
}