Left Number Twice Right
Left Number Twice Right
PROBLEM STATEMENT :
A set of N numbers (separated by one or more spaces) is passed as input to the program. The program must identify the count of numbers C where the number on the left is twice the number on the right.
Boundary Conditions:
3 <= N <= 50
The value of the numbers can be from -99999999 to 99999999
Input Format: The first line will contain the N numbers separated by one or more spaces.
Output Format: The first line contains the value of C.
Example Input/Output 1:
Input: 10 20 5 40 15 -2 -30 -1 60
Output: 2
Explanation: The numbers meeting the criteria are 20, -30
Example Input/Output 2:
Input: 5 90 10 2 5 -4 10 6 5 3
Output: 3
Explanation: The numbers meeting the criteria are 2, 6, 5
SOLUTION :
C (Programming Language)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[50],n=-1,i,cnt=0;
while(scanf("%d",&a[++n])==1); //scans the array
for(i=1;i<n;++i) //Here n is the size of the array
{
if(a[i+1]*2==a[i-1]) cnt++;
}
printf("%d",cnt);
}
C++ (CPP)
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int a[50],n=-1,i,cnt=0;
while(cin>>a[++n]);
for(i=1;i<n-1;++i)
{
if(a[i+1]*2==a[i-1]) cnt++;
}
cout<<cnt;
}
JAVA
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int[] a=new int[50];
int n=0,i,cnt=0;
while(sc.hasNext())
{
a[n++]=sc.nextInt();
}
for(i=1;i<n-1;++i)
{
if(a[i+1]*2==a[i-1]) cnt++;
}
System.out.println(cnt);
}
}
PYTHON
l=list(map(int,input().split()))
print(len([i for i in range(len(l)-1) if l[i+1]*2==l[i-1]]))
Comments
Post a Comment