Rotate image (square matrix) by 90 deg
Tue, 06 May 2025
The question below is given in the format in which questions are asked on Online Competitive coding Platforms. Solution:
You can see the question live on hackerrank.com at the link below: https://www.hackerrank.com/contests/rtech-april-18-01/challenges/vertical-level-sum-differences
Some of the solutions are:
Java
import java.io.*;
import java.util.*;
public class Solution {
Node root;
static int odd=0,even=0;
public static class Node{
int data;
Node left;
Node right;
Node(int data){
this.data=data;
left=null;
right=null;
}
}
public static Node insert(int a[],Node root,int i){
if (i < a.length) {
if(a[i]==-1){
Node temp = null;
}
else{
Node temp = new Node(a[i]);
root = temp;
root.left = insert(a, root.left,2 * i + 1);
root.right = insert(a, root.right, 2 * i + 2);
}
}
return root;
}
public static void print(Node root,int i){
if(root!=null){
print(root.left,i-1);
if(i%2==0)
even+=root.data;
else
odd+=root.data;
//System.out.println(root.data);
print(root.right,i+1);
}
}
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
Solution sol=new Solution();
sol.root=insert(a,sol.root,0);
print(sol.root,0);
System.out.println((even)-(odd));
}
}
C++ Solution:
int a[100005];
int b[100005];
int main()
{
int n;
cin >> n;
for(int i = 1 ; i<=n ; i++) { cin >> a[i];
}
b[1] = 0;
for(int i = 1 ; i<=n ; i++)
{
b[2*i] = b[i]-1;
b[2*i+1] = b[i]+1;
}
int even = 0 , odd = 0;
for(int i = 1 ; i<=n ; i++)
{
if(abs(b[i])%2==0)
{
if(a[i]!=-1)
{
even+=a[i];
}
}
else
{
if(a[i]!=-1)
{
odd+=a[i];
}
}
}
cout << even - odd;
return 0;
}
Tue, 06 May 2025
Tue, 06 May 2025
Tue, 06 May 2025
Leave a comment