0% found this document useful (0 votes)
424 views14 pages

Codility Solutions 4

The code counts the number of elements in a 2D array that are located in a subarray with equal row and column sums. It calculates the row and column sums, and then iterates through each element, comparing the sums of the subarrays to the left/above and right/below to determine if they are equal, incrementing the count if they are.

Uploaded by

Ramya Rams
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
424 views14 pages

Codility Solutions 4

The code counts the number of elements in a 2D array that are located in a subarray with equal row and column sums. It calculates the row and column sums, and then iterates through each element, comparing the sums of the subarrays to the left/above and right/below to determine if they are equal, incrementing the count if they are.

Uploaded by

Ramya Rams
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

class Solution {

public static int solution(int N,int[] A,int[] B){


int[] arr=new int[N];
int[] arr1=new int[A.length+1];
int sum=0,temp=N;
for(int i=0;i<A.length;i++){
arr[A[i]-1]++;
arr[B[i]-1]++;
}
for(int i=0;i<N;i++){
arr1[arr[i]]++;
}

for(int i=A.length;i>0;i--){
sum +=((2*temp*arr1[i]-arr1[i]arr1[i]+arr1[i])/2)(i);
temp-=arr1[i];
}
return sum;
}
}
public int solution(String direction, int radius, int[] X, int[] Y) {
int count=0;
switch (direction) {
case "U":
for(int i=0;i<X.length;i++) {
double z=180*(Math.atan2(X[i],Y[i]))/(Math.PI);
double r=Math.sqrt((X[i]*X[i])+(Y[i]*Y[i]));
if(r<=radius && (z<=45 && z>=-45)){
count++;
}
}
break;

case "R":
for(int i=0;i<X.length;i++) {
double z=180*(Math.atan2(X[i],Y[i]))/(Math.PI);
double r=Math.sqrt((X[i]*X[i])+(Y[i]*Y[i]));
if(r<=radius && (z<=135 && z>=45)){
count++;
}
}
break;
case "D":
for(int i=0;i<X.length;i++) {
double z=180*(Math.atan2(X[i],Y[i]))/(Math.PI);
double r=Math.sqrt((X[i]*X[i])+(Y[i]*Y[i]));
if(r<=radius && ((z<=180 & z>=135) || (z<=-135 &
z>=-180))){
count++;
}
}
break;

case "L":
for(int i=0;i<X.length;i++) {
double z=180*(Math.atan2(X[i],Y[i]))/(Math.PI);
double r=Math.sqrt((X[i]*X[i])+(Y[i]*Y[i]));
if(r<=radius && (z<=-45 && z>=-135)){
count++;
}
}
break;

}
return count;
}
}
def solution(S,T):
if abs(len(S)-len(T))>=2:
return "IMPOSSIBLE"
if S==T:
return "EQUAL"
if abs(len(S)-len(T))==1:
if len(S)>len(T):
if S[:-1]==T:
return "REMOVE "+S[-1]
else:
return "IMPOSSIBLE"
else:
if T[1:]==S:
return "INSERT "+T[0]
else:
return "IMPOSSIBLE"
if len(S)==len(T):
flag=0
res=""
for i in range(len(S)):
if S[i]!=T[i]:
flag +=1
res +=S[i]
if flag==2:
return f"SWAP {rs[0]} {res[1]}"
else:
return "IMPOSSIBLE"
class Solution{
public static int solution(int[][] arr)
{
int N=arr.length;

int M=arr[N-1].length;

int[] row=new int[N];

int[] col=new int[M];

int x=0,y=0;

int sumrow=0,colsum=0;

int rowUS=0,rowLS=0,colLS=0,colRS=0;

int a=0,b=0,count=0;

for(int i=0;i<N;i++) {

sumrow=0;
for(int j=0;j<M;j++) {

sumrow+=arr[i][j];

row[x++]=sumrow;

for(int i=0;i<M;i++) {

colsum=0;

for(int j=0;j<N;j++) {

colsum+=arr[j][i];

col[y++]=colsum;

}
//System.out.println(Arrays.toString(row));

//System.out.println(Arrays.toString(col));

for(int i=0;i<N;i++)

a=0;

//System.out.println("i"+i);

rowUS=0;

while(a<i)

rowUS+=row[a++];

//System.out.println("rowUS"+rowUS);

a++;

//System.out.println("a"+a);
rowLS=0;

while(a<N)

rowLS+=row[a++];

//System.out.println("rowLS"+rowLS);

//System.out.println("aLS"+a);

if(rowUS==rowLS)

for(int j=0;j<M;j++)

//System.out.println("j"+j);

b=0;

colLS=0;
while(b<j)

colLS+=col[b++];

//System.out.println("colLS"+colLS);

b++;

colRS=0;

while(b<M)

colRS+=col[b++];

//System.out.println("colRS"+colRS);

if(colLS==colRS) {

//System.out.println("arr"+arr[i][j]);

count++;

}
}
}
}
return count;
}
}

You might also like