January 28, 2021

Write a java program to rotate 1D array by n steps where n is a positive value

package com.leetcode.contest;

/**
 * Given an array, rotate the array to the right by k steps, where k is
 * non-negative.
 * 
 * Follow up:
 * 
 * Try to come up as many solutions as you can, there are at least 3 different
 * ways to solve this problem. Could you do it in-place with O(1) extra space?
 * 
 * @author Athul
 *
 */
class Solution21 {
public void rotate(int[] nums, int k) {

int len = nums.length;

int[] temp = new int[k];

int start = nums.length - k;

for (int i = 0; i < k; i++) {

temp[i] = nums[start];
start++;
}

int[] temp2 = new int[start];
int j = 0;
for (int i = 0; i < start; i++) {

temp2[i] = nums[j];
j++;
}
j = 0;
for (int i = 0; i < len; i++) {

if (i < k) {
nums[i] = temp[i];
} else {
nums[i] = temp2[j];
j++;
}

}
System.out.println();
}

public static void main(String[] args) {
Solution21 s = new Solution21();
int[] nums = { -1 };
s.rotate(nums, 2);
}
}

January 21, 2021

An Elephant and the Rope story | Motivational stories

As a man was passing the elephants, he suddenly stopped, confused by the fact that these huge creatures were being held by only a small rope tied to their front leg. No chains, no cages. It was obvious that the elephants could, at anytime, break away from their bonds but for some reason, they did not.

He saw a trainer nearby and asked why these animals just stood there and made no attempt to get away. “Well,” trainer said, “when they are very young and much smaller we use the same size rope to tie them and, at that age, it’s enough to hold them. As they grow up, they are conditioned to believe they cannot break away. They believe the rope can still hold them, so they never try to break free.”

The man was amazed. These animals could at any time break free from their bonds but because they believed they couldn’t, they were stuck right where they were.

Like the elephants, how many of us go through life hanging onto a belief that we cannot do something, simply because we failed at it once before?

Failure is part of learning; we should never give up the struggle in life

January 07, 2021

Coding test -parking lot problem solved

problem

I own a parking lot that can hold up to 'n' cars at any given point in time. Each slot is given a number starting at 1 increasing with increasing distance from the entry point in steps of one. I want to create an automated ticketing system that allows my customers to use my parking lot without human intervention. When a car enters my parking lot, I want to have a ticket issued to the driver. The ticket issuing process includes us documenting the registration number (number plate) and the colour of the car and allocating an available parking slot to the car before actually handing over a ticket to the driver (we assume that our customers are nice enough to always park in the slots allocated to them). The customer should be allocated a parking slot which is nearest to the entry. At the exit the customer returns the ticket with the time the car was parked in the lot, which then marks the slot they were using as being available. Total parking charge should be calculated as per the parking time. Charge applicable is AED 100 for first 2 hours and AED100 for every additional hour. We interact with the system via a simple set of commands which produce a specific output. Please take a look at the example below, which includes all the commands you need to support - they're self explanatory. The system should accept a filename as a parameter at the command prompt and read the commands from that file.

Solution

git hub url

Facebook comments