answer should be in java not C or C++ Exercise1: ReverseChar: Please write the f
ID: 3793254 • Letter: A
Question
answer should be in java not C or C++ Exercise1:
ReverseChar: Please write the function char[] reverseChar(char[] arr, int k) to return the array after reversing the characters in sub-group with size as k.
Example 1: Input: arr=[‘a’,’b’,’c’,’d’], k = 2; Return: [‘b’,’a’, ‘d’, ‘c’]
Example 2: Input: arr=[‘a’,’b’,’c’,’d’], k = 3; Return [‘c’,’b’,’a’, ‘d’]
Exercise 2: Count Apples: If you have N apples, then you have N/12 dozen apples, with N%12 apples left over. Write the function int[] countApples(int n) to return the number of dozen and the number of extra eggs left over.
Example 1: Input: N = 12; Return: [1,0] (because 12/12 = 1 and 12%12 =0)
Example 2: Input: N = 14; Return: [1,2] (because 12/12 = 1 and 14%12 =2)
Exercise 3: get Sum: Please write the function long getSum(int n) to get the sum as 1 * 2 + 3 * 4 + … + (n-1 * n) or n:
Example 1: Input: n = 5; Return: 19 (because 1*2+3*4+5 = 19)
Example 2: Input: n = 4; Return 14 (because 1*2 + 3*4 = 14)
Exercise 4: Anti-Zigzag Elements: Please fill the function antiZigzag(int[][] arr): Given a square array with size of N x N, return an array including the anti-zigzag elements only.
Example 1: Input: [0,2; 3,1] Return: [2,0,1,3]
Example 2: Input: [0,1,2; 3,4,5; 6,7,8] Return: [2,1,0,4,8,7,6]
Exercise 5: Magic Square: A magic square is a N x N matrix with all the vertical and horizontal sums are the same. Please write the function chkMagicSquare(int[][] arr)to check if the given matrix is a magic square.
Example 1: Input: [1,2; 3,0] Return: False
Example 2: Input: [0,2,5; 4,2,1; 3,3,1] Return: True
Explanation / Answer
//Function implementation for exercise 1:
public char[] reverseChar(char[] arr,int k)
{
int i=0;
while(i<k) //This while loop reverses the first k characters of an arr
{
char temp=arr[i];
arr[i]=arr[k];
arr[k]=temp;
i++;
k--;
}
return arr;
}
//Function implementation for exercise 2:
public int[] countApples(int n)
{
int arr[]=new int[2];
arr[0]=n/12;
arr[1]=n%12;
return arr;
}
//Function implementation for exercise 3:
public long getSum(int n)
{
long ans=0;
//This loop increment variable i by two and add the product of two numbers to ans
for(int i=1;i<=n;i+=2)
{
//Condition for n is odd or even
if(i!=n)
{
ans+=i*(i+1);
}
else
{
ans+=i;
}
}
return ans;
}
//Function implementation for exercise 4:
public int[] antiZigZag(int [][]arr)
{
int n=arr.length;
int anti[]=new int[3*N]; //Because int antizigzag sequence there is atmost 3*N-2 elements
int counter=0;
for(int j=n-1;j>=0;j--)
{
anti[counter++]=arr[0][j];
}
for(int j=1;j<n-1;j++)
{
anti[counter++]=arr[j][j];
}
for(int j=n-1;j>=0;j--)
{
anti[counter++]=arr[n-1][j];
}
return anti;
}
//Function implementation for exercise 5:
public boolean chkMagicSquare(int [][]arr)
{
int n=arr.length;
int ans=0;
for(int i=0;i<n;i++)
ans+=arr[0][i];
//Checking sum row wise
for(int i=0;i<n;i++)
{
int sum=0;
for(int j=0;j<n;j++)
{
sum+=arr[i][j];
}
if(sum!=ans)
return false;
}
//Checking sum column wise
for(int i=0;i<n;i++)
{
int sum=0;
for(int j=0;j<n;j++)
{
sum+=arr[j][i];
}
if(sum!=ans)
return false;
}
return true;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.