Minimum of 3
PROBLEM :
Given an array consisting of N numbers your task is to find the minimum sum of the array such that out of 3 consecutive elments you need to add atleast one.
Input:
First line consists of T Test cases. Each test case contains 2 lines . The first line consists of a number N specifying the number of elements in an array and the next line contains N elements.
Output:
Output in a single line minimum sum that can be obtained.
Constraints:
1<=T<=100
1<=N<=43
Example:
Input:
2
6
1 2 3 6 7 1
12
1 2 3 6 7 1 8 6 2 7 7 1
Output:
4
7
Explanation:
For First Test case moving from left to right 3+1
For second Test case moving from left to right 3+1+2+1. When 3 is added next 3 consecutive elements be 6 7 and 1 and so on.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<limits.h>
int Minimum_of_three(int [],int ) ;
int minof(int ,int ,int ) ;
int main()
{
int t,no,arr[50],i ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
no=Minimum_of_three(arr,no) ;
cout<<no<<endl ;
}
return 0;
}
int Minimum_of_three(int arr[],int no)
{
int temp[no] ;
int i,min ;
temp[0]=arr[0] ;
temp[1]=arr[1] ;
temp[2]=arr[2] ;
for(i=3;i<no;i++)
temp[i]=arr[i]+minof(temp[i-1],temp[i-2],temp[i-3]) ;
min=minof(temp[no-1],temp[no-2],temp[no-3]) ;
return min ;
}
int minof(int a,int b,int c)
{
int temp ;
temp=a<b?a:b ;
return temp<c?temp:c ;
}
---------------------------------------------------------------------------------
Given an array consisting of N numbers your task is to find the minimum sum of the array such that out of 3 consecutive elments you need to add atleast one.
Input:
First line consists of T Test cases. Each test case contains 2 lines . The first line consists of a number N specifying the number of elements in an array and the next line contains N elements.
Output:
Output in a single line minimum sum that can be obtained.
Constraints:
1<=T<=100
1<=N<=43
Example:
Input:
2
6
1 2 3 6 7 1
12
1 2 3 6 7 1 8 6 2 7 7 1
Output:
4
7
Explanation:
For First Test case moving from left to right 3+1
For second Test case moving from left to right 3+1+2+1. When 3 is added next 3 consecutive elements be 6 7 and 1 and so on.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<limits.h>
int Minimum_of_three(int [],int ) ;
int minof(int ,int ,int ) ;
int main()
{
int t,no,arr[50],i ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
no=Minimum_of_three(arr,no) ;
cout<<no<<endl ;
}
return 0;
}
int Minimum_of_three(int arr[],int no)
{
int temp[no] ;
int i,min ;
temp[0]=arr[0] ;
temp[1]=arr[1] ;
temp[2]=arr[2] ;
for(i=3;i<no;i++)
temp[i]=arr[i]+minof(temp[i-1],temp[i-2],temp[i-3]) ;
min=minof(temp[no-1],temp[no-2],temp[no-3]) ;
return min ;
}
int minof(int a,int b,int c)
{
int temp ;
temp=a<b?a:b ;
return temp<c?temp:c ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment