A non-empty array A consisting of N integers is given. The consecutive elements of array A represent consecutive cars on a road.
Array A contains only 0s and/or 1s:
- 0 represents a car traveling east,
- 1 represents a car traveling west.
The goal is to count passing cars. We say that a pair of cars (P, Q), where 0 ≤ P < Q < N, is passing when P is traveling to the east and Q is traveling to the west.
For example, consider array A such that:
A[0] = 0 A[1] = 1 A[2] = 0 A[3] = 1 A[4] = 1We have five pairs of passing cars: (0, 1), (0, 3), (0, 4), (2, 3), (2, 4).
Write a function:
int solution(int A[], int N);
that, given a non-empty array A of N integers, returns the number of pairs of passing cars.
The function should return −1 if the number of pairs of passing cars exceeds 1,000,000,000.
For example, given:
A[0] = 0 A[1] = 1 A[2] = 0 A[3] = 1 A[4] = 1the function should return 5, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- each element of array A is an integer that can have one of the following values: 0, 1.
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
long long count =0;
long long[] B = A;
for(long i =1; i<sizeof(B);i++)
{
B[i] += B[i-1];
}
long last = B[sizeof(B)];
for(long i =1; i<sizeof(A);i++)
{
if (A[i]==0)
{
count += last-B[i];
}
}
return count;
}
func.c: In function 'solution': func.c:7:14: error: expected identifier or '(' before '[' token long long[] B = A; ^ func.c:8:29: error: 'B' undeclared (first use in this function) for(long i =1; i<sizeof(B);i++) ^ func.c:8:29: note: each undeclared identifier is reported only once for each function it appears in
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
long long count =0;
long long B[] = A;
for(long i =1; i<sizeof(B);i++)
{
B[i] += B[i-1];
}
long last = B[sizeof(B)];
for(long i =1; i<sizeof(A);i++)
{
if (A[i]==0)
{
count += last-B[i];
}
}
return count;
}
func.c: In function 'solution': func.c:7:5: error: invalid initializer long long B[] = A; ^
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
long long count =0;
long B[] = A;
for(long i =1; i<sizeof(B);i++)
{
B[i] += B[i-1];
}
long last = B[sizeof(B)];
for(long i =1; i<sizeof(A);i++)
{
if (A[i]==0)
{
count += last-B[i];
}
}
return count;
}
func.c: In function 'solution': func.c:7:5: error: invalid initializer long B[] = A; ^
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
long long count =0;
long B[] ;
B=A;
for(long i =1; i<sizeof(B);i++)
{
B[i] += B[i-1];
}
long last = B[sizeof(B)];
for(long i =1; i<sizeof(A);i++)
{
if (A[i]==0)
{
count += last-B[i];
}
}
return count;
}
func.c: In function 'solution': func.c:7:10: error: array size missing in 'B' long B[] ; ^ func.c:8:6: error: incompatible types when assigning to type 'long int[1]' from type 'int *' B=A; ^
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
long long count =0;
long B[sizeof(A)] = A ;
for(long i =1; i<sizeof(B);i++)
{
B[i] += B[i-1];
}
long last = B[sizeof(B)];
for(long i =1; i<sizeof(A);i++)
{
if (A[i]==0)
{
count += last-B[i];
}
}
return count;
}
func.c: In function 'solution': func.c:7:5: error: invalid initializer long B[sizeof(A)] = A ; ^
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
long long count =0;
long B[sizeof(A)] ;
B = (long)A;
for(long i =1; i<sizeof(B);i++)
{
B[i] += B[i-1];
}
long last = B[sizeof(B)];
for(long i =1; i<sizeof(A);i++)
{
if (A[i]==0)
{
count += last-B[i];
}
}
return count;
}
func.c: In function 'solution': func.c:8:7: error: incompatible types when assigning to type 'long int[8]' from type 'long int' B = (long)A; ^
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
long long count =0;
long B[sizeof(A)] ;
B = A;
for(long i =1; i<sizeof(B);i++)
{
B[i] += B[i-1];
}
long last = B[sizeof(B)];
for(long i =1; i<sizeof(A);i++)
{
if (A[i]==0)
{
count += last-B[i];
}
}
return count;
}
func.c: In function 'solution': func.c:8:7: error: incompatible types when assigning to type 'long int[8]' from type 'int *' B = A; ^
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
long long count =0;
long B[sizeof(A)] ;
B = A;
for(long i =1; i<sizeof(B);i++)
{
B[i] += B[i-1];
}
long last = B[sizeof(B)];
for(long i =1; i<sizeof(A);i++)
{
if (A[i]==0)
{
count += last-B[i];
}
}
}
}
Compilation failed: 2 error(s), 0 warnings Solution.cs(11,13): error CS1525: Unexpected symbol `long' Solution.cs(12,11): error CS0650: Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
long count =0;
long B[sizeof(A)] ;
B = A;
for(long i =1; i<sizeof(B);i++)
{
B[i] += B[i-1];
}
long last = B[sizeof(B)];
for(long i =1; i<sizeof(A);i++)
{
if (A[i]==0)
{
count += last-B[i];
}
}
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(12,11): error CS0650: Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
long count =0;
long B[A.Length] ;
B = A;
for(long i =1; i<sizeof(B);i++)
{
B[i] += B[i-1];
}
long last = B[sizeof(B)];
for(long i =1; i<sizeof(A);i++)
{
if (A[i]==0)
{
count += last-B[i];
}
}
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(12,11): error CS0650: Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
long long count =0;
for(long i =1; i<sizeof(A);i++)
{
A[i] += A[i-1];
}
long last = A[sizeof(A)];
for(long i =1; i<sizeof(A);i++)
{
if (A[i]!=A[i-1])
{
count += last-A[i-1];
}
}
return count;
}
Segmentation Fault
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
long long count =0;
for(long i =1; i<sizeof(A);i++)
{
A[i] += A[i-1];
}
long last = A[sizeof(A)-1];
for(long i =1; i<sizeof(A);i++)
{
if (A[i]!=A[i-1])
{
count += last-A[i-1];
}
}
return count;
}
Segmentation Fault
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
long long count =0;
for(long i =1; i<N;i++)
{
A[i] += A[i-1];
}
long last = A[N-1];
for(long i =1; i<N;i++)
{
if (A[i]!=A[i-1])
{
count += last-A[i-1];
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
long long count =0;
for(long i =1; i<N;i++)
{
A[i] += A[i-1];
}
long last = A[N-1];
for(long i =1; i<N;i++)
{
if (A[i]!=A[i-1])
{
count += last-A[i-1];
}
}
return count;
}
[0, 0, 1, 1, 0, 0, 1]
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
long long count =0;
int B[] = A[];
for(long i =1; i<N;i++)
{
A[i] += A[i-1];
}
long last = A[N-1];
for(long i =1; i<N;i++)
{
if (A[i]!=A[i-1])
{
count += last-A[i-1];
}
}
return count;
}
func.c: In function 'solution': func.c:7:17: error: expected expression before ']' token int B[] = A[]; ^ func.c:7:9: warning: unused variable 'B' [-Wunused-variable] int B[] = A[]; ^
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99
long long count =0;
bool B[N];
B[0] = (bool)A[0];
for(long i =1; i<N;i++)
{
B[i]=(bool)A[i];
A[i] += A[i-1];
}
long last = A[N-1];
for(long i =0; i<N;i++)
{
if (B[i])
{
count += last-A[i];
}
}
return count;
}
func.c: In function 'solution': func.c:7:5: error: unknown type name 'bool' bool B[N]; ^ func.c:8:13: error: 'bool' undeclared (first use in this function) B[0] = (bool)A[0]; ^ func.c:8:13: note: each undeclared identifier is reported only once for each function it appears in func.c:8:18: error: expected ';' before 'A' B[0] = (bool)A[0]; ^ func.c:11:20: error: expected ';' before 'A' B[i]=(bool)A[i]; ^
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int solution(int A[], int N) {
// write your code in C99
long long count =0;
bool B[N];
B[0] = (bool)A[0];
for(long i =1; i<N;i++)
{
B[i]=(bool)A[i];
A[i] += A[i-1];
}
long last = A[N-1];
for(long i =0; i<N;i++)
{
if (B[i])
{
count += last-A[i];
}
}
return count;
}
func.c:3:21: fatal error: iostream: No such file or directory #include <iostream> ^ compilation terminated.
// you can write to stdout for debugging purposes, e.g.
printf("this is a debug message\n");
#include <string>
#include <cstdlib>
using namespace std;
int solution(int A[], int N) {
// write your code in C99
long long count =0;
bool B[N];
B[0] = (bool)A[0];
for(long i =1; i<N;i++)
{
B[i]=(bool)A[i];
A[i] += A[i-1];
}
long last = A[N-1];
for(long i =0; i<N;i++)
{
if (B[i])
{
count += last-A[i];
}
}
return count;
}
func.c:2:9: error: expected declaration specifiers or '...' before string constant printf("this is a debug message\n"); ^ func.c:4:18: fatal error: string: No such file or directory #include <string> ^ compilation terminated.
// you can write to stdout for debugging purposes, e.g.
#include <string>
#include <cstdlib>
using namespace std;
int solution(int A[], int N) {
// write your code in C99
printf("this is a debug message\n");
long long count =0;
bool B[N];
B[0] = (bool)A[0];
for(long i =1; i<N;i++)
{
B[i]=(bool)A[i];
A[i] += A[i-1];
}
long last = A[N-1];
for(long i =0; i<N;i++)
{
if (B[i])
{
count += last-A[i];
}
}
return count;
}
func.c:3:18: fatal error: string: No such file or directory #include <string> ^ compilation terminated.
// you can write to stdout for debugging purposes, e.g.
int solution(int A[], int N) {
// write your code in C99
printf("this is a debug message\n");
long long count =0;
bool B[N];
B[0] = (bool)A[0];
for(long i =1; i<N;i++)
{
B[i]=(bool)A[i];
A[i] += A[i-1];
}
long last = A[N-1];
for(long i =0; i<N;i++)
{
if (B[i])
{
count += last-A[i];
}
}
return count;
}
func.c: In function 'solution': func.c:8:5: error: unknown type name 'bool' bool B[N]; ^ func.c:9:13: error: 'bool' undeclared (first use in this function) B[0] = (bool)A[0]; ^ func.c:9:13: note: each undeclared identifier is reported only once for each function it appears in func.c:9:18: error: expected ';' before 'A' B[0] = (bool)A[0]; ^ func.c:12:20: error: expected ';' before 'A' B[i]=(bool)A[i]; ^
// you can write to stdout for debugging purposes, e.g.
int solution(int A[], int N) {
// write your code in C99
printf("this is a debug message\n");
long long count =0;
int B[N];
B[0] = A[0];
for(long i =1; i<N;i++)
{
B[i]=A[i];
A[i] += A[i-1];
}
long last = A[N-1];
for(long i =0; i<N;i++)
{
if (B[i])
{
count += last-A[i];
}
}
return count;
}
[0, 0, 1, 1, 0, 0, 1]
this is a debug message
function result: 3
this is a debug message
// you can write to stdout for debugging purposes, e.g.
int solution(int A[], int N) {
// write your code in C99
printf("this is a debug message\n");
long long count =0;
int B[N];
B[0] = A[0];
for(long i =1; i<N;i++)
{
B[i]=A[i];
A[i] += A[i-1];
}
long last = A[N-1];
for(long i =0; i<N;i++)
{
if (B[i]==0)
{
count += last-A[i];
}
}
return count;
}
[0, 0, 1, 1, 0, 0, 1]
function result: 8
this is a debug message
// you can write to stdout for debugging purposes, e.g.
int solution(int A[], int N) {
// write your code in C99
// printf("this is a debug message\n");
long long count =0;
int B[N];
B[0] = A[0];
for(long i =1; i<N;i++)
{
B[i]=A[i];
A[i] += A[i-1];
}
long last = A[N-1];
for(long i =0; i<N;i++)
{
if (B[i]==0)
{
count += last-A[i];
}
}
return count;
}
[0, 0, 1, 1, 0, 0, 1]
// you can write to stdout for debugging purposes, e.g.
int solution(int A[], int N) {
// write your code in C99
// printf("this is a debug message\n");
long long count =0;
int B[N];
B[0] = A[0];
for(long i =1; i<N;i++)
{
B[i]=A[i];
A[i] += A[i-1];
}
long last = A[N-1];
for(long i =0; i<N;i++)
{
if (B[i]==0)
{
count += last-A[i];
}
}
return count;
}
[0, 0, 1, 1, 0, 0, 1]
// you can write to stdout for debugging purposes, e.g.
int solution(int A[], int N) {
// write your code in C99
// printf("this is a debug message\n");
long long count =0;
int B[N];
B[0] = A[0];
for(long i =1; i<N;i++)
{
B[i]=A[i];
A[i] += A[i-1];
}
long last = A[N-1];
for(long i =0; i<N;i++)
{
if (B[i]==0)
{
count += last-A[i];
}
}
return count;
}
The following issues have been detected: wrong answers.