Google Kickstart Round D 2k20 | Alien Piano problem (7,11)

KICK-START ROUND D SOLUTION

RECORD BREAKER PROBLEM (7pts,11pts)

Problem Explanation :




Code in Python :


def main():
    n = int(input())
    arr = list(map(intinput().split()))
    t1, t2, count = 000
    for i in range(n):
        if arr[i] > arr[i - 1]:
            t1 += 1
            t2 = 0

            if t1 == 4:
                t1 = 0
                count += 1
        elif arr[i] < arr[i - 1]:
            t2 += 1
            t1 = 0

            if t2 == 4:
                t2 = 0
                count += 1
    return count


Code in CPP/C++ :

#include<iostream>
using namespace std;

int solve()
{
 int n,count=0;
 cin>>n;

 int arr[n];
 for(int i=0;i<n;i++)
  cin>>arr[i];

 int t1=0,t2=0;

 for(int i=1;i<n;i++)
 {
  if(arr[i]>arr[i-1])
  {
   t1++;
   t2=0;
   if(t1==4)
   {
    t1=0;
    count++;
   }
  }
  else if(arr[i]<arr[i-1])
  {
   t2++;
   t1=0;
   if(t2==4)
   {
    t2=0;
    count++;
   }
  }
 }

 return count;
}

int main()
{
 int t;
 cin>>t;

 for(int i=1;i<=t;i++)
  cout<<"Case #"<<i<<": "<<solve()<<endl;

 return 0;
}

Code in Java :

# THE CODE LOOKS BIG AS IT WAS USED AS A TEMPLATE FOR CP

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

public class Solution {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        AlienPiano solver = new AlienPiano();
        int testCount = Integer.parseInt(in.next());
        for (int i = 1; i <= testCount; i++)
            solver.solve(i, in, out);
        out.close();
    }

    static class AlienPiano {
        public void solve(int testNumber, InputReader sc, PrintWriter out) {
            int n = sc.nextInt();

            int a[] = new int[n];
            int b[] = new int[n];
            for (int i = 0; i < n; ++i)
                a[i] = sc.nextInt();
            b[0] = 0;
            for (int i = 1; i < n; ++i) {
                if (a[i] > a[i - 1])
                    b[i] = 1;
                else if (a[i] < a[i - 1])
                    b[i] = -1;
                else
                    b[i] = 0;
            }

            int ans = 0;
            int ind = 0;
            while (ind < n) {
                ans++;
                ind++;
                int l = 0, r = 3;
                while (ind < n) {
                    if (b[ind] == -1) {
                        r--;
                        l = 0;
                    } else if (b[ind] == 1) {
                        r = 3;
                        l++;
                    }

                    if (l > 3 || r < 0)
                        break;
                    ind++;
                }
            }

            out.println("Case #" + testNumber + ": " + (ans - 1));
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }

                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-') {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(c));

            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public String next() {
            return readString();
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}

Comments

Popular posts from this blog

How to NOT write UGLY CODE ? | PEP-8 | Python Enhancement Proposals | VS-Code Plugins | autopep8 | Prettier | Preffered Settings

DC's DSA-CRACKER #3 | Array | Kth Maxima and Minima in an Array

DC's DSA-CRACKER #1 | Array | REVERSE THE ARRAY