12 TH Mar Dynamic Programming
12 TH Mar Dynamic Programming
#include <stdio.h>
#include<string.h>
***********************************************************************************
*****************************************************
[0,2,1,2,1,0,1,2] - Sort using single loop without new array creation.
#include <stdio.h>
int main()
{
int arr[]={0,2,1,2,1,0,1,2};
int n=8;
int L=0,R=n-1,i=0;
int temp;
while(i<=R)
{
if(arr[i]==0)
{
temp=arr[L];
arr[L]=arr[i];
arr[i]=temp;
L++;
}
if(arr[i]==2)
{
temp=arr[R];
arr[R]=arr[i];
arr[i]=temp;
R--;
}
i++;
}
for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
}
***********************************************************************************
*****************************************************
Traverse linked list single time and find the middle element
***********************************************************************************
******************************************************
sol:1
traverse using slow and fast pointer ....... at some point both met at same pointer
.....
if it meets then it is circular linked list
sol:2
Using hashmap in Java
***********************************************************************************
******************************************************
int main()
{
int arr[]={1,0,2,1,0,0,2};
int sum=0,n=7;
for(int i=0;i<n;i++)
{
sum=sum^arr[i];
}
printf("%d",sum);
}
***********************************************************************************
******************************************************
Hashmap - using keys ... hashing the value and storing in keys
//Java
Hashmap<key_dataType,Value_dataType> objName= new Hashmap();
***********************************************************************************
******************************************************
[1,7,18,6,2,7,9] find the sum of pairs of the element just with one loop
sum=8
using hashset delete the element value with sum and save in hashset
now use contains() and check each element .... now the pair will return true and
none pair will return false
***********************************************************************************
******************************************************