samedi 15 août 2020

find number of triplet such that sum of 2 of elements is equal to the other in the same array.first i/p-no of test cases.2nd-size of array,third array

This below is my code-

#include<iostream>
using namespace std;

int triplet(int a[],int N)
{ int i,count=0;
    for(i=0;i<N-2;i++)
   { if(a[i+2]=a[i+1]+a[i])
     count++;
   }
   if(count!=0)
    cout<<count<<" ";
    else 
    cout<<-1;
}
int swap(int a[],int N)
{ for(int i=0;i<N-1;i++)
{if(a[i]>a[i+1])
 {
     int t=a[i];
     a[i]=a[i+1];
     a[i+1]=t;
  }} triplet(a,N);
}
int main() {
  int T,N,a[100],i,j,k;
 
  cin>>T;
  for(i=0;i<T;i++)
  {cin>>N;
  for(j=0;j<N;j++)
  cin>>a[j];
  swap(a,N);
  }}

i want to get the output by this logic only.My output is coming as 2 and 1 instead of 2 and -1. can anybody please help me out where am i going wrong with the count. input- 2 //no of test cases 4/size of array 1 5 3 2/array 3//size of array 3 2 7//array output=2,-1(-1 coz none of the numbers sum upto any of the array

Aucun commentaire:

Enregistrer un commentaire