rvndr
BAN USERInput arr[] = [1, 2, 3, 4, 5, 6, 7,,,,,,,,100], let m=5
1) Store m elements in a temp array
temp[] = [1, 2,3,4,5]
2) Shift rest of the arr[]
arr[] = [ 6, 7,8,,,,,,100,96,97,98,99,100]
3) Store back the m elements
arr[] = [6, 7, 8,9,,,,,100,1,2,3,4,5]
complexity:-o(n)
proper and simple implementation for the given problem is..
#include<stdio.h>
int fib(int n);
int count0,count1;
main()
{
int n,T,i;
scanf("%d",&T);
while(T>0)
{
count0=0;
count1=0;
scanf("%d",&n);
fib(n);
printf("%d %d\n",count0,count1);
}
}
int fib(int n)
{
if(n==0)
{
count0++;
return 0;
}
if(n==1)
{
count1++;
return 1;
}
return (fib(n-1)+fib(n-2));
}
..
- rvndr December 13, 2013Here is proper implementation for the given question..
#include<stdio.h>
int fib(int n);
int count0,count1;
main()
{
int n,T;
scanf("%d",&T);
while(T>0)
{
count0=0;
count1=0;
scanf("%d",&n);
fib(n);
printf("%d %d\n",count0,count1);
T--;
}
}
int fib(int n)
{
if(n==0)
{
count0++;
return 0;
}
if(n==1)
{
count1++;
return 1;
}
return (fib(n-1)+fib(n-2));
}
you are right bro...your solution is true ..but what if i say integer sizes goes beyond limit..than it will not work..:)
solution for that will be ..=====
1.) XOR all no.s from 1 to n.let XOR be X
2) .XOR all array elements .let say XOR be Y
3) X xor Y is our answer
assuming only one ticket can be processed at a time....imagine trickets are marked as (start,end)....->
let we have tickets (1,2).,(6,8).,(11,20).,(4,6).,(2,4)..etc.
STEP1:
so,store all of these structures in an array of structures...and then sort the array according to 1st element.
STEP2:
now simply take a scan from starting element of array up to where 1st element is continous...
eg.=>(1,2),(2,4),(4,6),(6,8)....
now our result is =>8-1=7 unit time is longest continous time
Input arr[] = {3, 4, 5, 1, 2}
Element to Search = 1
1) Find out pivot point and divide the array in two
sub-arrays. (pivot = 2) /*Index of 5*/
2) Now call binary search for one of the two sub-arrays.
(a) If element is greater than 0th element then
search in left array
(b) Else Search in right array
(1 will go in else as 1 < 0th element(3))
3) If element is found in selected sub-array then return index
Else return -1.
based on my this solution ..code in c is...
assumption: array elements are single digit ...for anything else u can make a suitable hash function
#include<stdio.h>
#include<conio.h>
int hash[10];
main()
{
int i,modifiedlength;
int x;
int arr[7]={0,8,9,8,0,2,0};
modifiedlength=removedup(arr,7);
for(i=0;i<modifiedlength-1;i++)
printf("%d ",arr[i]);
getch();
}
int removedup(int a[],int n)
{
int i,k;
i=0;k=0;
hash[a[0]]=1;
a[k++]=a[i];
while(i<n)
{
if((a[i]!=a[i+1])&&(hash[a[i+1]]==0))
{
a[k++]=a[i+1];
hash[a[i+1]]=1;
}
i++;
}
return k;
}
int k=0,i=0;
a[k++]=a[i];
while(i<n)
{
if((a[i]!=a[i+1])&&(notcomesbefore()))
a[k++]=a[i+1];
i++;
}
notcomesbefore()
{
use hash table ..........//hash table is initialised to zero
if(hash array has value 1) //this value is already taken
return 0;
else
return 1;
}
@MrA ...yaa thats right thnx.....your 1st case can be handled by adding one more condition for zero..
and...as far as we talk about 2nd case of overflow ...we can have data type long in c or as u described for java ..this one is not a standard problem,so it can be managed
it can be solved in O(n2)..two steps are inolved ..
- rvndr August 18, 2014step 1. first take each point from array of points and calculate slope with other points.and keep storing it into an array.
step 2. now from this new array we can tell maximum total no. of points having same slope