String Immutable Or Mutable

Immutable means that we can't change the content of the String once it is initialized.

1. In C++ String is Mutable . Which means you can modify the String just like array.

2. In Java String is Immutable. Which will create problems so many problems , we can resolve with the help of converting String to CharArray or Using StringBuilder .

Testing on String By Modifying It :

/**
 *
 */
package com.techyield.operationsinarray;

/**
 * @author satyanarayanagokavarapu
 *
 */
public class StringModification {

/**
* @param args
*/
public static void main(String[] args) {

String s1 = "Sunny";

s1[4]=','; // It will show compile Time Error as This type of expression can be array type //can be resolved to String .

System.out.println(s1);

}

}

Introduction To String

String :

A String is an array of Unicode Characters. But like Array we can perform all the operations on String .

But there are some differences based on programming language.

Operations&Functions On Strings :

Compare Function :

String has its own compare function.

"==" Can be used in Java to compare whether the two objects are same but cannot be used to compare two Strings.

"==" Can be used in C++ to Compare Two Strings as it supports Operator Overloading.


Example :


package com.techyield.operationsinarray;

public class OperationsInString {

public static void main(String[] args) {
String s1 = "Hello World! Chitti Version 2.0";
// s2 is reference of s1
String s2 = s1;
// s3 is copy of s1
String s3 = new String(s1);
System.out.println("s1 is \""+s1+"\"");
System.out.println("s2 is "+s2);
System.out.println("s3 is "+s3);
// compare using ==
System.out.println("Compare Strings using \"==\"");
System.out.println("Comparing s1 and s2"+ (s1==s2));
System.out.println("Comparing s2 and s3 using \"==\""+ (s2==s3));
System.out.println("Comparing s1 and s3 using \"==\""+ (s1==s3));

// compare using equals method
System.out.println("Compare Strings using equals method");
System.out.println("Comparing s1 and s2"+ (s1.equals(s2)));
System.out.println("Comparing s2 and s3 using \"==\""+ (s2.equals(s3)));
System.out.println("Comparing s1 and s3 using \"==\""+ (s1.equals(s1)));

// compare using compareTo method
System.out.println("Compare Strings using compareTo method");
System.out.println("Comparing s1 and s2"+ (s1.compareTo(s2) == 0));
System.out.println("Comparing s2 and s3 using \"==\""+ (s2.compareTo(s3) == 0));
System.out.println("Comparing s1 and s3 using \"==\""+ (s1.compareTo(s1) == 0));

}


}


Introduction to 2D Array


Like in One Dimensional Array . Two Dimensional array also consists of sequence of elements.

But the elements can be laid in rectangular grid rather than a line.

We also have 2D Dynamic array Just as 1D Dynamic array https://techyield.blogspot.com/2019/06/introduction-to-dynamic-array.html



Let's take a look at an example of using a two-dimensional array:
package com.techyield.operationsinarray;

public class OperationsIn2Darray {



private static void printArray(int[][] a) {
for(int i =0; i < a.length; i++) {
         System.out.println(a[i]);
}
for(int i = 0 ; i< a.length; i++) {
for(int k=0; a[i]!= null && k < a[i].length; k++) {
System.out.println(a[i][k]+" ");
}
System.out.println();
}
}



public static void main(String[] args) {
System.out.println("Example I:");
int [][] a = new int[2][];
printArray(a);
System.out.println(" Example 2:");
int [][] b = new int[3][5];
printArray(b);
System.out.println("Example 3:");
b[0] = new int[3];
b[1] = new int[5];
printArray(b);
}

}





Introduction To Dynamic Array

In general Array has fixed capacity .  Array that we are talking about is linear array and we need to give size of the array when we initialize it  , which is wasteful and inconvenient sometimes.


Most programming languages offer built in dynamic array. Which is a random access list data structure with variable size .

We have ARRAYLIST in Java which is similar to Vector C++.

/**
 *
 */
package com.techyield.operationsinarray;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

class OperationsInDynamicArray {

/**
* @param args
*/
public static void main(String[] args) {
       
// Initialize
List<Integer> l1 = new ArrayList<>();
List<Integer> l2 ; // here l2 == null

// Cast an array to List
Integer[] array = {1,2,3,4,5,6,7,8};
l2 = new ArrayList<>(Arrays.asList(array));

// Make a copy

List<Integer> l3 = l2;  // another reference to l1
List<Integer> l4 = new ArrayList<>(l2); // make an actual copy of l2

// Length

System.out.println("The size of list is "+l2.size());

// Access Element
System.out.println("The first element in l2 is "+l2.get(0));

// Iterate the linkedList
System.out.println("The contents of l2 are : ");
for(Integer a : l2) {
System.out.println(a);
}
System.out.println(" Version2 ............");
for(int i =0 ; i < l2.size(); i++) {
System.out.println(" "+l2.get(i));
}
System.out.println();

// Modify the element
l2.set(0,5);
System.out.println("l2.get(0)"+l2.get(0));
// Modify the element
l2.set(0, -1);
System.out.println("l2.get(0)"+l2.get(0));
// here l3 refers to l2 so modifying l2 modifies l3
// Sort
Collections.sort(l2);
// Add a new element at the end of the ArrayList
l2.add(9);

l2.add(2,5);

// Remove an element

l2.remove(l2.size()-1);

for(int u : l2) {
System.out.println(u);
}




}

}








Introduction To Array

Array :

Array is an basic data structure used to store a collection of elements sequentially.

Elements in an array can be accessed randomly because elements in array can be identified by array index.

We have one dimensional array and multi-dimensional array.

One dimensional array is known as Linear array.

Example :


Here , we can access 2 as num[0] , 6 as num[3]

Operations in Array :
Initialize 
Length of an array 
Access an Element 
Iterate all the Elements 
Modify Elements
Sort

// Example :


package com.techyield.operationsinarray;

import java.util.Arrays;

public class OperationsinOneDimensionalArray {

public static void main(String[] args) {
//Intialize
int [] array1 = new int[5];
int [] array = {1,2,3,4,5,6};
// Getting Length
System.out.println("The Size of array1 is "+array1.length);
System.out.println("The Size of array2 is "+array.length);
// Access 4th element  in array2
System.out.println("The 4th element in array"+array[3]);
// Iterate all the elements in array
// First Version
System.out.println("Fetching elements using for loop");
for(int i =0; i < array.length; i++) {
System.out.println(" "+array[i]);
}
System.out.println();
System.out.println("Fetching elements from array using forEach ");
for(int k : array) {
System.out.println(" "+k);
}
System.out.println();
// Modify Elements 
System.out.println("Modify 3rd ELement in array ");
array[2]= 9;
for(int p : array) {
System.out.println(" "+p);
}
System.out.println(" Sort the Elements");
// Sort the Elements
Arrays.sort(array);
for(int u = 0; u < array.length;u++)
System.out.println(" "+array[u]);
 

}


}


// Output :


The Size of array1 is 5
The Size of array2 is 6
The 4th element in array4
Fetching elements using for loop
 1
 2
 3
 4
 5
 6

Fetching elements from array using forEach 
 1
 2
 3
 4
 5
 6

Modify 3rd ELement in array 
 1
 2
 9
 4
 5
 6
 Sort the Elements
 1
 2
 4
 5
 6
 9



   

P ,NP , NP-Complete , NP-hard

I assume that you are looking for intuitive definitions, since the technical definitions require quite some time to understand. First of all, let's remember a preliminary needed concept to understand those definitions.
  • Decision problem: A problem with a yes or no answer.

Now, let us define those complexity classes.

P

P is a complexity class that represents the set of all decision problems that can be solved in polynomial time. That is, given an instance of the problem, the answer yes or no can be decided in polynomial time.
Example
Given a connected graph G, can its vertices be coloured using two colours so that no edge is monochromatic?
Algorithm: start with an arbitrary vertex, color it red and all of its neighbours blue and continue. Stop when you run out of vertices or you are forced to make an edge have both of its endpoints be the same color.

NP

NP is a complexity class that represents the set of all decision problems for which the instances where the answer is "yes" have proofs that can be verified in polynomial time.
This means that if someone gives us an instance of the problem and a certificate (sometimes called a witness) to the answer being yes, we can check that it is correct in polynomial time.
Example
Integer factorisation is in NP. This is the problem that given integers n and m, is there an integer f with 1 < f < m, such that f divides n (f is a small factor of n)?
This is a decision problem because the answers are yes or no. If someone hands us an instance of the problem (so they hand us integers n and m) and an integer f with 1 < f < m, and claim that f is a factor of n (the certificate), we can check the answer in polynomial time by performing the division n / f.

NP-Complete

NP-Complete is a complexity class which represents the set of all problems X in NP for which it is possible to reduce any other NP problem Y to X in polynomial time.
Intuitively this means that we can solve Y quickly if we know how to solve X quickly. Precisely, Yis reducible to X, if there is a polynomial time algorithm f to transform instances y of Y to instances x = f(y) of X in polynomial time, with the property that the answer to y is yes, if and only if the answer to f(y) is yes.
Example
3-SAT. This is the problem wherein we are given a conjunction (ANDs) of 3-clause disjunctions (ORs), statements of the form
(x_v11 OR x_v21 OR x_v31) AND 
(x_v12 OR x_v22 OR x_v32) AND 
...                       AND 
(x_v1n OR x_v2n OR x_v3n)
where each x_vij is a boolean variable or the negation of a variable from a finite predefined list (x_1, x_2, ... x_n).
It can be shown that every NP problem can be reduced to 3-SAT. The proof of this is technical and requires use of the technical definition of NP (based on non-deterministic Turing machines). This is known as Cook's theorem.
What makes NP-complete problems important is that if a deterministic polynomial time algorithm can be found to solve one of them, every NP problem is solvable in polynomial time (one problem to rule them all).

NP-hard

Intuitively, these are the problems that are at least as hard as the NP-complete problems. Note that NP-hard problems do not have to be in NP, and they do not have to be decision problems.
The precise definition here is that a problem X is NP-hard, if there is an NP-complete problem Y, such that Y is reducible to X in polynomial time.
But since any NP-complete problem can be reduced to any other NP-complete problem in polynomial time, all NP-complete problems can be reduced to any NP-hard problem in polynomial time. Then, if there is a solution to one NP-hard problem in polynomial time, there is a solution to all NP problems in polynomial time.
Example
The halting problem is an NP-hard problem. This is the problem that given a program P and input I, will it halt? This is a decision problem but it is not in NP. It is clear that any NP-complete problem can be reduced to this one. As another example, any NP-complete problem is NP-hard.
My favorite NP-complete problem is the Minesweeper problem.

P = NP

This one is the most famous problem in computer science, and one of the most important outstanding questions in the mathematical sciences. In fact, the Clay Institute is offering one million dollars for a solution to the problem (Stephen Cook's writeup on the Clay website is quite good).
It's clear that P is a subset of NP. The open question is whether or not NP problems have deterministic polynomial time solutions. It is largely believed that they do not. Here is an outstanding recent article on the latest (and the importance) of the P = NP problem

Featured Post

H1B Visa Stamping at US Consulate

  H1B Visa Stamping at US Consulate If you are outside of the US, you need to apply for US Visa at a US Consulate or a US Embassy and get H1...