31. Program
(Imp_Q) Write a program to swap odd indexed element with even indexed element ?
class O
{
public static void main(String args[])
{
int[] a = {3, 9, 20, 45, 50, 200, 401, 500, 600, 808, 909};
int temp = 0;
for(int i = 0; i < a.length; i++)
{
System.out.print(a[i] + " ,");
}
System.out.println();
for(int i = 0; i < a.length-1; i+=2)
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
System.out.println();
for(int i = 0; i < a.length; i++)
{
System.out.print(a[i] + " ,");
}
}
}
32. Program
(Imp_Q) Find out the index of an array, left elements sum should be same as right elements sum ?
class D
{
public static void main(String args[])
{
int[] a = {2, 4, 4, 2, 4, 7, 4, 5};
int endindex = a.length - 1;
int startindex = 0;
int sumLeft = 0;
int sumRight = 0;
while(true)
{
if(sumLeft > sumRight)
{
sumRight += a[endindex--];
}
else
{
sumLeft += a[startindex++];
}
if(startindex > endindex)
{
if(sumLeft == sumRight)
{
System.out.println(endindex);
}
else
{
System.out.println("doesn't have");
}
break;
}
}
}
}