Problem : Accico Equi Pairs
Ron Wesley has been bit by a three headed snake and Harry Potter is
searching for a potion. The Witch promises to tell the ingredients of
the medicine if Harry can find equi pair of an array. Listen to the conversation between Harry The witch to know more about equi pairs.
Conversation:-
The Witch : To find the equi pair, you must know how to find the slices first.
Harry : What is a slice?
The Witch : If Z is an array with N elements, a slice of indices (X, Y) is Z[X] + Z[X+1]...Z[Y]
Harry : How can I use it to find equi pair?
The Witch : (a, b) is an equi pair if slice of (0, a-1) = slice of (a+1, b-1) = slice of (b+1, N-1) and b>a+1 and size of array > 4
Conversation:-
The Witch : To find the equi pair, you must know how to find the slices first.
Harry : What is a slice?
The Witch : If Z is an array with N elements, a slice of indices (X, Y) is Z[X] + Z[X+1]...Z[Y]
Harry : How can I use it to find equi pair?
The Witch : (a, b) is an equi pair if slice of (0, a-1) = slice of (a+1, b-1) = slice of (b+1, N-1) and b>a+1 and size of array > 4
Input Format:
An array of N integers delimited by white space
An array of N integers delimited by white space
Output Format:
Print equi pair in first line in the format { a,b }
Print slices in the format { 0,a-1 }, { a+1,b-1 }, { b+1,N-1 }
OR
Print "Array does not contain any equi pair" if there are no equi pairs in the array
Print equi pair in first line in the format { a,b }
Print slices in the format { 0,a-1 }, { a+1,b-1 }, { b+1,N-1 }
OR
Print "Array does not contain any equi pair" if there are no equi pairs in the array
Constraints:
Zi >= 0 and 1<= i <=N
size of array (N) > 4
b > (a+1)
SNo. | Input | Output |
---|---|---|
1 | 8 3 5 2 10 6 7 9 5 2 | Indices which form equi pair { 3,6 } Slices are { 0,2 },{ 4,5 },{ 7,9 } |
2 | 6 2 6 2 3 3 1 9 | Array does not contain any equi pair |
Explanation for sample input output 1:
Here index { 3,6 } is an equi pair. Because Slice of { 0,2 } = 8+3+5=16 is equal to Slice of { 4,5 }=10+6 = 16 and it is equal to Slice of { 7,9 }=9+5+2 =16
Here index { 3,6 } is an equi pair. Because Slice of { 0,2 } = 8+3+5=16 is equal to Slice of { 4,5 }=10+6 = 16 and it is equal to Slice of { 7,9 }=9+5+2 =16
Note:
Please do not use package and namespace in your code. For object oriented languages your code should be written in one class.
Please do not use package and namespace in your code. For object oriented languages your code should be written in one class.
Note:
Participants submitting solutions in C language should not use functions from / as these files do not exist in gcc
Participants submitting solutions in C language should not use functions from / as these files do not exist in gcc
Note:
For C and C++, return type of main() function should be int.
For C and C++, return type of main() function should be int.
SOLUTION : IN JAVA
import java.util.*;
public class AccicoPeris {
public static void main(String []args){
int n,count=0;String s,s1,s2;char[] ch;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
int []a=new int[n];
if(n<=4){
System.out.println("invalid input");
}
else
{
for(int m=1;m<=n;m++){
a[m-1]=sc.nextInt();
if(a[m-1]<=0){
System.out.println("invalid input");
}
for(int i=1;i<=n;i++){
for(int j=(i+2);j<=n;j++){
int sum1=0,sum2=0,sum3=0,count1=0,count2=0,count3=0;
for(int k=0;k<i;k++){
count1++;
sum1+=a[k];
}
for(int k=i+1;k<=j-1;k++){
count2++;
sum2+=a[k];
}
for(int k=j+1;k<=n-1;k++){
count3++;
sum3+=a[k];
}
if(sum1==sum2&&sum2==sum3&&count1>1&&count2>1&&count3>1){
System.out.println("Indices which form an equi pair { "+i+","+j+" } Slices are {0, "+(i-1)+"} , {"+(i+1)+","+(j-1)+"} , {"+(j+1)+","+(n-1)+"}");
count++;
}
}
}
}}
if(count==0){
System.out.println("Array does not contain any equi pair.");
}
}
}
public class AccicoPeris {
public static void main(String []args){
int n,count=0;String s,s1,s2;char[] ch;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
int []a=new int[n];
if(n<=4){
System.out.println("invalid input");
}
else
{
for(int m=1;m<=n;m++){
a[m-1]=sc.nextInt();
if(a[m-1]<=0){
System.out.println("invalid input");
}
for(int i=1;i<=n;i++){
for(int j=(i+2);j<=n;j++){
int sum1=0,sum2=0,sum3=0,count1=0,count2=0,count3=0;
for(int k=0;k<i;k++){
count1++;
sum1+=a[k];
}
for(int k=i+1;k<=j-1;k++){
count2++;
sum2+=a[k];
}
for(int k=j+1;k<=n-1;k++){
count3++;
sum3+=a[k];
}
if(sum1==sum2&&sum2==sum3&&count1>1&&count2>1&&count3>1){
System.out.println("Indices which form an equi pair { "+i+","+j+" } Slices are {0, "+(i-1)+"} , {"+(i+1)+","+(j-1)+"} , {"+(j+1)+","+(n-1)+"}");
count++;
}
}
}
}}
if(count==0){
System.out.println("Array does not contain any equi pair.");
}
}
}
No comments:
Post a Comment