Posts

First Missing Positive

 class Solution {     public int firstMissingPositive(int[] nums) {         for(int i=0;i<nums.length;i++){             int correctPos=nums[i]-1;             while(((1<=nums[i])&&(nums[i]<=nums.length))&&nums[i]!=nums[correctPos])             {                 int t=nums[i];                 nums[i]=nums[correctPos];                 nums[correctPos]=t;                 correctPos=nums[i]-1;             }         }         for(int i=0;i<nums.length;i++){           if(i+1!=nums[i]){               return i+1;           }         }         return nums.length+1;              } }

Find String Contains Valid Parentheses or Not

boolean isValid(String str) {    Stack<Character> st=new Stack<Character>();    for(char ch:str.toCharArray())    {        if(ch=='['||ch=='('||ch=='{')       {             st.push(st);       }else if(ch==')'&&!st.isEmpty()&&st.peek()=='('){          st.pop():      }else if(ch=='}'&&!st.isEmpty()&&st.peek()=='}'){          st.pop():      }else if(ch=='}'&&!st.isEmpty()&&st.peek()=='{'){          st.pop():      }else{        return false;       }    }    return st.isEmpty(); }

Power Of Two Code using java

Finding the length of connected cells of 1's (regions) in an matrix of 1's and 0's

import java.io.*; class GFG {  static int max = 0;  static int count = 0;  public static void traverse(int a[][], int r, int c, int i, int j) {   if (i >= r || j >= c || i < 0 || j < 0) {    return;   }   if (a[i][j] == 0) {    return;   } else {    a[i][j] = 0;    count++;    traverse(a, r, c, i - 1, j);    traverse(a, r, c, i, j - 1);    traverse(a, r, c, i + 1, j);    traverse(a, r, c, i, j + 1);    traverse(a, r, c, i - 1, j - 1);    traverse(a, r, c, i + 1, j + 1);    traverse(a, r, c, i - 1, j + 1);    traverse(a, r, c, i + 1, j - 1);   }  }  public static void main(String[] args) {   int R = 5;   int C = 5;  int a[][] = { { 0, 0, 0, 0, 0}, { 0, 1, 1, 0, 0}, { 0, 0, 1, 0, 1}, { 1, 0, 0, 0, 1}, { 0, 1, 0, 1, 1} };   for (int i = 0; i < R; i++) {    for (int j = 0; j < C; j++) {     if (a[i][j] == 1) {      count = 0;      traverse(a, R, C, i, j);      if (count > max) {       max = count;

Top 10 websites for software engineers

*Top 10 Sites for your career:* 1. LinkedIN 2. Indeed 3. Careerealism 4. Job-Hunt 5. JobBait 6. Careercloud 7. GM4JH 8. Personalbrandingblog 9. Jibberjobber 10. Neighbors-helping-neighbors *Top 10 Tech Skills in demand in 2019:* 1. Machine Learning 2. Mobile Development 3. SEO/SEM Marketing 4. Data Visualization 5. Data Engineeringj 6. UI/UX Design 7. Cyber-security 8. Cloud Computing/AWS 9. Blockchain 10. IOT *Top 10 Sites to learn Excel for free:* Search for these on Google: 1. Microsoft Excel Help Center 2. Excel Exposure 3. Chandoo 4. Excel Central 5. Contextures 6. Excel Hero 7. Mr. Excel 8. Improve Your Excel 9. Excel Easy 10. Excel Jet *Top 10 Sites for Free Online Education:* *Search for these on Google:* 1. Coursera 2. edX 3. Khan Academy 4. Udemy 5. iTunesU Free Courses 6. MIT OpenCourseWare 7. Stanford Online 8. Codecademy 9. Open Culture Online Courses *Top 10 Sites to review your resume for free:* 1. Zety Resume Builder 2. Resumonk 3. https://t.co/h

Rotate a matrix 270 degree AntiClockWise

#include<stdio.h> void printMt(int mat[][4]) {     int i, j;     for (i = 0; i < 4; i++) {         for (j = 0; j < 4; j++) {             printf("%d ", mat[i][j]);         }         printf("\n");     } } void AntiClock90(int mat[][4]) {     int i, j, t;     for (i = 0; i < 4 / 2; i++) {         for (j = i; j < 4; j++) {             t = mat[4 - i - 1][j];             mat[4 - i - 1][j] = mat[i][j];             mat[i][j] = t;         }     }     printf("90 anticlock\n");     printMt(mat); } void Anti270(int mat1[][4]) {     int i,j,t;     for(i=0; i<4/2; i++)     {         for( j=i; j<4; j++)         {             t=mat1[j][4-i-1];             mat1[j][4-i-1]=mat1[j][i];             mat1[j][i]=t;         }     }     printf("270 anticlock \n");     printMt(mat1); } int main() {     int mat[4][4]= {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};  

Binary Tree Creation Code Using C

#include<stdio.h> #include<stdlib.h> struct node { int key; struct node *left; struct node *right; }; struct node *createNode() { struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); temp->left=NULL; temp->right=NULL; return temp; } struct node *queue[100]; int front=-1,rear=-1; int isEmptyt() {     if(front==-1||front==rear+1)     {  return 0;     }     else     {     return 1; } } void Enqueue(struct node *temp) { if(front==-1) { front=0; } rear=rear+1; queue[rear]=temp; } struct node *Dequeue() { int t; if(front==-1||front==rear+1) {  return 0; } t=front; front++; return queue[t]; } void Delete() { front=-1; rear=-1; } struct node* BinaryTree(struct node *root,int key) {      struct node *temp=createNode();      temp->key=key;      if(root==NULL)      {       root=temp;       return root; }      else      {       Enqueue(root);       while(isE

EASIEST WAY FOR MERGE TWO LINKLIST

/* Link list Node struct Node {     int data;     Node* next; }; */ Node* SortedMerge(Node* head1,   Node* head2) {     struct  Node *head,*last;     if(head1==NULL)     {         return head2;     }     if(head2==NULL)     {         return head1;     }     if(head1&&head2)     {         if(head1->data<head2->data)         {             last=head1;             head1=last->next;         }         else         {             last=head2;             head2=last->next;         }     }     head=last;     while(head1&&head2)     {         if(head1->data<head2->data)         {             last->next=head1;             last=head1;             head1=last->next;         }         else         {             last->next=head2;             last=head2;             head2=last->next;         }     }     if(head1)     {         last->next=head1;     }     if(head2)     {         last->next=head2

BINARY SEARCH TREE CREATION THROUGH C

#include<stdio.h> #include<stdlib.h> struct tree {   int data;   struct  tree *left;   struct   tree *right; }; int pre(struct tree *root) {    if(root!=NULL)    {     printf("%d  ",root->data);     pre(root->left);     pre(root->right);    } } struct tree *Insert_node(struct tree *root) {     struct tree *temp,*ptr,*prev=NULL; temp=(struct tree *)malloc(sizeof(tree)); int n; printf("enter data"); scanf("%d",&n); temp->data=n; temp->left=NULL; temp->right=NULL; if(root==NULL) {   root=temp; } else {      ptr=root; while(ptr!=NULL) { prev=ptr; if(ptr->data>temp->data) { ptr=ptr->left; }        else if(ptr->data<temp->data) { ptr=ptr->right; } else if(ptr->data==temp->data) {  printf("duplticate key");  break; }  }   if(temp->data<prev->data) { prev->left=tem

Largest Sum Contiguous Subarray. Kadane's Algorithm

// C++ program to print largest contiguous array sum #include<iostream> #include<climits> using namespace std; int maxSubArraySum(int a[], int size) { int max_so_far = INT_MIN, max_ending_here = 0; for (int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } /*Driver program to test maxSubArraySum*/ int main() { int a[] = {-2, -3, -4, -6, -7, -9, -90, -30}; int n = sizeof(a)/sizeof(a[0]); int max_sum = maxSubArraySum(a, n); cout << "Maximum contiguous sum is " << max_sum; return 0; }

Static Keyword in C

In C, static variables can only be initialized using constant literals. For example, following program fails in compilation. See this for more details. #include<stdio.h> int initializer( void ) {      return 50; }     int main() {      static int i = initializer();      printf ( " value of i = %d" , i);      getchar ();      return 0; } Output In function 'main': 9:5: error: initializer element is not constant static int i = initializer(); ^

void pointer in C

void pointer in C A void pointer is a pointer that has no associated data type with it. A void pointer can hold address of any type and can be typcasted to any type. int a = 10; char b = 'x' ;   void *p = &a;  // void pointer holds address of int 'a' p = &b; // void pointer holds address of char 'b' Advantages of void pointers: 1) malloc() and calloc() return void * type and this allows these functions to be used to allocate memory of any data type (just because of void *) int main(void) {      // Note that malloc() returns void * which can be      // typecasted to any type like int *, char *, ..      int *x = malloc(sizeof(int) * n); } Note that the above program compiles in C, but doesn’t compile in C++. In C++, we must explicitly typecast return value of malloc to (int *). 2) void pointers in C are used to implement generic functions in C. For example compare function whic

Check current time using C program

#include<stdio.h> #include<time.h> int main() {      struct tm *local,*gm;      time_t t;      t=time(NULL);      local=localtime(&t);      printf("time-->%d:%d:%d",local->tm_hour,local->tm_min,local->tm_sec); }

find even odd using & operator

int main() { int i =0; for(i=1;i<10;i++) { (i& 1)? printf("Odd"): printf("Even");   printf("\n"); } return 0; }

program to convert binary to decimal using c

#include<stdio.h> int main() {     int n,rem,d,j=1,dec=0;     printf("Enter the number in binary");     scanf("%d",&n);    while(n>0)    {        rem=n%10;        d=rem*j;        dec=dec+d;       j*=2;       n=n/10;    }    printf("decimal is=%d",dec); }