Saturday, 28 August 2021

Sliding Window In Java - Maximum sum of contigous sub array of size k

public static void main(String[] args) {
int[] arr = {5,2,1,7,4,9};
int k = 3;
int n = arr.length;
//brute force O(N*K)
/**
* 5+2+1=8
* 2+1+7=10
* 1+7+4=12
* 7+4+9=20
*/
int sum = 0;
for(int i=0;i<=n-k;i++) {
int s = 0;
for(int j=i;j< (i+k); j++) {
s += arr[j];
}
sum = s > sum ? s : sum;
}
System.out.println("" + sum);//20
//smart O(N)
/**>>>> 5,2,1,7,4,9
* 5+2+1=8
* add 7(next), minus 1 (first)
* add 4 minus 2
* add 9 minus 1
*/
int win = 0, max = 0;
for(int i=0;i win += arr[i];
}
for(int i=k;i win = win + arr[i] - arr[i-k];
max = win > max ? win : max;
}
System.out.println("max " + max);//20
}

Sunday, 16 May 2021

Java Rotate Array - From Right

Hi, welcome to my blog.

Today we will learn how to rotate an array from the right.
Consinder the array [1,2,3,4,5,6,7], rotate k = 3 steps.
The output will be [5,6,7,1,2,3,4].
There are many ways of solving this.
First, I will show you what I would refer to as "less optimal solution", because we will use an extra sapce. Then I will show you a more optimal solution.

1. Less optmal solution - extra space O(n) , run time O(n).

We are going to loop n times, every loop we will swap two numbers.
That is, index [i] and index j = i+k%array.length.
You should figure out why i+k%array.length.
/**
*
* i=0 to i = 6, k=3 [swap i = 0 and j = 3].
* So, why j = 3?
* i=0, j=(i+k%array.length)
* So, why are we doing this? Please think about it.
*                                   1,2,3,4,5,6,7
* i=0,j=(0+3=3 %7=3 ->*,*,*,1,*,*,*
* i=1,j=(1+3=4 %7=4 ->*,*,*,1,2,*,*
* i=2,j=(2+3=5 %7=5 ->*,*,*,1,2,3,*
* i=3,j=(3+3=6 %7=6 ->*,*,*,1,2,3,4
* i=4,j=(4+3=7 %7=0 ->5,*,*,1,2,3,4
* i=5,j=(5+3=8 %7=1 ->5,6,*,1,2,3,4
* i=6,j=(6+3=9 %7=2 ->5,6,7,1,2,3,4
*
*/


Sample code.

public static void rotate2(int[] nums, int k) {
System.out.println(Arrays.toString(nums));
int n = nums.length;
int[] temp = Arrays.copyOf(nums, n);
for(int i=0;i nums[(i+k) % n] = temp[i];
}
}


2. More optimal solution, not extra space, run time O(n).
This solution is quite straightforward.
We will swap number by calling a function.
First, swap [0 to 2], or until i < array.length in every loop we will increment i and decrement array.length which we will store in a local variable.
/**
* ### 0 to n-1(6)
* 1,2,3,4,5,6,7
* 1. 7,2,3,4,5,6,1 --> start =0,end=6
* 2. 7,6,3,4,5,2,1 --> start =1,end=5
* 3. 7,6,5,4,3,2,1 --> start =2,end=4
* This will not execute --> start =3,end=3 ==
* output = 7,6,5,4,3,2,1
* ### 0 to k-1(2)
* 7,6,5,4,3,2,1
* 1. 5,6,7,4,3,2,1 --> start =0,end=2
* This is not execute == start=1,end=1
* ### 3 to n-1(6)
* 5,6,7,4,3,2,1
* 1. 5,6,7,1,3,2,4 --> start=3,end=6
* 2. 5,6,7,1,2,3,4 --> start=4,end=5
* This will not execute, start=5,end=4
**/


Sample code.

public static void rotate(int[] nums, int k) {
/**
* when we do array.length, we get the length from JVM heap
* if we stored the value in a primitive variable,(stack)
* this would mean faster access,
* but this is trivial/little significance.
*/
if(nums.length == 1)
return;
k = k % nums.length; This line is very important... when k is > array.length
swap(nums, 0, nums.length-1);
swap(nums, 0, k-1);
swap(nums, k, nums.length-1);
}


private static void swap(int[] arr, int start, int end){
//[1,2,3,4,5,6,7] s=0,e=6
while(start < end){
int temp = arr[start];//1
arr[start] = arr[end];//swap first, 1 with last, 7
arr[end] = temp;//first, 1 to last
start++;
end--;
}
}




Note: left rotation using the last method would be very simple.
Just change the below lines ...

swap(a, 0 , n-1);
swap(a, n-k , n-1);
swap(a, 0 , n-k-1);


Where..
a is the input array, like nums
and
int n = a.length;
and
k = k % a.length;




Thanks for reading...




Sunday, 1 September 2019

Java String Class and String manipulation

Hi reader,

Welcome, I will take you through String basics in Java and I hope you will have a deeper understanding at the end of the day.

Objectives on this post.

1. Deeper understanding of String class and String manipulation in Java.

Subtopics

1. The String Class (extends Object, implements Serializable, Comparable and charSequence interfaces)

2. Two ways of creating a string

- via String literal (add into string pool in the heap)

- new keyword (creates an object in the heap)

3. String manipulations

4. String, String Builder, and String Buffer

Let's get started.

A String is a sequence of characters. A character is a letter, a number, space, punctuation mark, or a symbol that can be typed on a computer. In java, a character or "char" is a single character, say a letter like 'A', a number like '9', a special character like '^' caret, etc. While char is a primitive type, String is a reference type. A primitive data type is the most basic type that acts as a building block for other data types. In Java, there are 8 primitive types namely: boolean, byte, char, short, int, long, float and double. In Java world, a reference type is a data type that is based on a class rather than on one of the primitive types.

Java post-increment and pre-increment

Hi reader,


Welcome to my simple explanation on post and pre-increment in Java.


Let have the following,
int a = 100;

Post-Increment

int b = a++;// post increment, compute result first, then increment
System.out.println(b);//prints 100
//now a = 101

Pre-Increment

int c = ++a;//pre increment, increment first then compute result
//increment a from 101 to 102
System.out.println(c);//print 102

Sample code

public class Calculations {

public Calculations() {}

public static void main(String[] args) {

postAndPreIncrement();

} public static void postAndPreIncrement() {

int a = 100;
int b = a++;// post increment, compute result first, then increment
System.out.println(b);//prints 100

//now a = 101

int c = ++a;//pre increment, increment first then compute result

//increment a from 101 to 102
System.out.println(c);//print 102
}
}

Thursday, 16 July 2015

Guide to flash and root phones

Unbrick or Flash MTK Android Phone.

Drivers

1)Snappea
2)Pdanet drivers
3)Mediatek Preloader USB VCOM Drivers - (Install file)
and
here the raw drivers



ROMs

find a ROM of your choice. Either stock or custom.

Requirements:
(OS)
1) i suggest windows7
2) Sp flash tool

To root/Backup current ROM
use MTKDroidTool
Procedure:
1)Open Driver manager window on your windows7 machine.
2)Remove battery from your Phone.
3) Connect usb cord to your phone.
4) check the window in (1) above, it will blink as the drivers try to install.
5) on the top of the window (4) above..add legacy drivers..browse to the folder with your Preloader drivers.
6) load the drivers.
7)confrim that Preloader drivers have installed.
8)install other drivers. you may restart your machine.
Once all the drivers have installed, open sp flash tool
9) load scatter files( specific for your phone)
10) select how you want to flash the phone,, i prefer download only.
11) you are almost done...proceed,
Make sure progress bar shows your progress in percentage.
NB:
your phone must be rooted..
To root use either MTKDroidTool or other apps like framaroot.
contact me for help on how to,
1)to flash sum-sang phones(odin required).
2)nokia phone (infinity box required)
3)spreadtrum phones etc
4)get a box(miracle,Dragoncrut etc )
My email peter mwenda
Am not responsible for: bricked phone, void warranty etc.