Skip to content

Commit 0995b0c

Browse files
Merge branch 'smv1999:master' into Keerthana
2 parents 8e263a7 + d940c16 commit 0995b0c

33 files changed

+2317
-0
lines changed

Arrays/Maximum_Difference.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
From all the positive integers entered in a list, the aim of the program is
3+
to subtract any two integers such that the result/output is the maximum
4+
possible difference.
5+
6+
'''
7+
8+
# class to compute the difference
9+
class Difference:
10+
def __init__(self, a):
11+
# getting all elements from the entered list
12+
self.__elements = a
13+
def computeDifference(self):
14+
# maximum difference would be the difference between the largest and the smallest integer
15+
Difference.maximumDifference = max(self.__elements)-min(self.__elements)
16+
return Difference.maximumDifference
17+
# end of Difference class
18+
19+
# getting the input
20+
_ = input()
21+
a = [int(e) for e in input().split(' ')]
22+
23+
# creating an object of the class
24+
d = Difference(a)
25+
# calling function 'computeDifference' to compute the difference
26+
d.computeDifference()
27+
28+
# printing the result
29+
print(d.maximumDifference)
30+
31+
'''
32+
33+
COMPLEXITY:
34+
35+
Time Complexity -> O(N)
36+
Space Complexity -> O(1)
37+
38+
Sample Input:
39+
3
40+
1 2 5
41+
42+
Sample Output:
43+
4
44+
45+
Explanation:
46+
Integer with max value--> 5
47+
Integer with min value--> 1
48+
Hence, maximum difference--> 5-1 = 4
49+
50+
'''

Arrays/Min_Max_Sum.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
Aim: Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers.
3+
4+
Example:
5+
arr = [1,3,5,7,9]
6+
The minimum sum is 1+3+5+7 = 16 and the maximum sum is 3+5+7+9 = 24. So, the output will be:
7+
16 24
8+
9+
'''
10+
11+
def MMSum(a):
12+
# sorting the array so that it becomes easier to find the min and max set of values
13+
a.sort()
14+
minn=0
15+
maxx=0
16+
for i in range(0,4):
17+
# summing up all minimum values
18+
minn+=a[i]
19+
for i in range(1,5):
20+
# summing up all maximum values
21+
maxx+=a[i]
22+
print(minn,maxx)
23+
24+
# getting the input
25+
user_input = (input().strip().split())
26+
array = []
27+
for i in user_input:
28+
array.append(int(i))
29+
# calling the Min-Max-Sum function
30+
MMSum(array)
31+
32+
'''
33+
34+
COMPLEXITY:
35+
36+
Time Complexity -> O(N)
37+
Space Complexity -> O(N)
38+
39+
Sample Input:
40+
2 1 3 4 5
41+
42+
Sample Output:
43+
10 14
44+
45+
Explanation:
46+
Sorted array: [1,2,3,4,5]
47+
Min Sum: 1+2+3+4 = 10
48+
Max Sum: 2+3+4+5 = 14
49+
50+
'''

Arrays/integer.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* Title : Enter the data elments before that mention the their total count . .
2+
After entering the data you first missed positive integer missed will
3+
be displayed .
4+
Code is done in C language .
5+
*/
6+
#include<stdio.h>
7+
#include<stdlib.h>
8+
int main()
9+
{
10+
int n ,j=0, i,k=1 , val;
11+
int opt=1;
12+
printf("\n Enter the total number of elements : \n");
13+
scanf("%d",&n);
14+
if(n<=0)
15+
{
16+
printf("\n Invalid Input , Try again later !! \n");
17+
exit(0);
18+
}
19+
int a[n];
20+
printf("\n Enter the elements : \n");
21+
for(i=0;i<n;i++)
22+
{ scanf("%d",&a[i]); }
23+
24+
for(i=0;i<n;i++)
25+
{ for(j=0;j<n-1;j++)
26+
if(a[j]>a[j+1])
27+
{ val=a[j];
28+
a[j]=a[j+1];
29+
a[j+1]=val; }
30+
}
31+
printf("\n Sorted array : \n");
32+
for(i=0;i<n;i++)
33+
{
34+
printf(" %d ", a[i]);
35+
}
36+
printf("\n \n");
37+
printf("\n Postive element missed from the data is ");
38+
if(a[n-1]<=0)
39+
{
40+
printf("\n1\n");
41+
42+
43+
}
44+
else {
45+
46+
for(i=0;i<n;i++)
47+
{
48+
if(a[i]>0)
49+
{
50+
for(j=0+i, k=1;j<=n && k<=n+1;k++,j++)
51+
{
52+
if(a[j]==k)
53+
{
54+
continue ;
55+
}
56+
else
57+
{
58+
59+
printf(" %d " ,k);
60+
exit(0);
61+
62+
}
63+
64+
65+
66+
}
67+
}
68+
}
69+
}
70+
71+
}
72+
73+
74+
/*
75+
76+
Enter the total number of elements :
77+
7
78+
79+
Enter the elements :
80+
0
81+
-1
82+
1
83+
-2
84+
-3
85+
-4
86+
-5
87+
88+
Sorted array :
89+
-5 -4 -3 -2 -1 0 1
90+
91+
92+
Postive element missed from the data is 2
93+
94+
*/

Codechef Problems/Food_Chain.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* PROBLEM STATEMENT
2+
A great deal of energy is lost as metabolic heat when the organisms from one trophic level are consumed by the next level.
3+
Suppose in Chefland the energy reduces by a factor of K, i.e, if initially, the energy is X, then the transfer of energy to the next tropic level is ⌊XK⌋. This limits the length of foodchain which is defined to be the highest level receiving non-zero energy.
4+
E is the energy at the lowest tropic level. Given E and K for an ecosystem, find the maximum length of foodchain.
5+
Note: ⌊x⌋ denoted the floor function, and it returns the greatest integer that is less than or equal to x (i.e rounds down to the nearest integer). For example, ⌊1.4⌋=1, ⌊5⌋=5, ⌊−1.5⌋=−2, ⌊−3⌋=−3 , ⌊0⌋=0.
6+
Input Format
7+
First line will contain T, number of testcases. Then the testcases follow.
8+
Each testcase contains a single line of input, two integers E,K.
9+
Output Format
10+
For each testcase, output in a single line answer to the problem.
11+
Constraints
12+
1≤T≤104
13+
1≤E≤109
14+
2≤K≤109
15+
Sample Input 1
16+
3
17+
5 3
18+
6 7
19+
10 2
20+
Sample Output 1
21+
2
22+
1
23+
4
24+
Explanation
25+
TestCase 1: The energy at first level is 5 units. For the second level energy becomes ⌊53⌋=1 units. So the length of foodchain is 2 since from the next level onwards 0 units of energy will be received.
26+
TestCase 3: The energy at different levels is:
27+
Level 1- 10 units
28+
Level 2- ⌊102⌋=5 units
29+
Level 3- ⌊52⌋=2 units
30+
Level 4- ⌊22⌋=1 units
31+
Level 5- ⌊12⌋=0 units
32+
So the answer is 4, since it is the last level to receive non-zero energy. */
33+
#include <stdio.h>
34+
35+
int main(void) {
36+
37+
int t,e,k;
38+
int i; int j=0;
39+
scanf("%d",&t);
40+
int tc=t; int arr[t];
41+
while(t--)
42+
{
43+
scanf("%d %d",&e,&k);
44+
int count=0;
45+
i=e;
46+
while(i>0)
47+
{
48+
count++;
49+
i=i/k;
50+
51+
}
52+
arr[j]=count;
53+
j++;
54+
}
55+
56+
for(j=0;j<tc;j++)
57+
printf("%d\n",arr[j]);
58+
return 0;
59+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Implementation of Maximum Array XOR problem from CodeChef
3+
*
4+
* Problem name: Maximum Array XOR problem
5+
* Problem code: MAXARXOR
6+
* Problem link: https://www.codechef.com/START7C/problems/MAXARXOR
7+
*
8+
* Solution:
9+
*
10+
* Value of an array A is defined as the sum of (A(i)⊕i) for all 0≤i<L, where ⊕ denotes bitwise xor operation.
11+
* You are given an integer N and an array A consisting of 2^N integers where A(i)=i for all 0≤i<2^N.
12+
* Example :
13+
* - For N=1, you have an array A of length 2^1=2 and A=[0,1].
14+
* - For N=2, you have an array A of length 2^2=4 and A=[0,1,2,3].
15+
*
16+
* You can do at most K operations on this array. In one operation, you can choose
17+
* two indices i and j (0≤i,j<2^N) and swap A(i) and A(j) (i.e. A(i) becomes A(j) and vice versa).
18+
*
19+
* In this problem, we need to find the maximum value of array A after at most K operations.
20+
* Maximum value is only possible when the value (A(i)⊕i) is 2^N - 1 for each index. So to get maximum value for each index
21+
* we have to swap values according to the condition. But to get the solution of this problem we don't really need to swap.
22+
* We can find the maximum value of array without actually swapping them.
23+
*
24+
* For every single swap we will maximize the value of two indexes in the array, so for K operations we can
25+
* maximize 2*K cells. But it is also possible that 2*K cells can be greater than 2^N so that's why
26+
* we will reduce it. It's given in the question that atmost K operations are allowed. So now we need
27+
* to multiply 2*K to 2^N - 1, to get the maximum value.
28+
*
29+
*/
30+
import java.util.*;
31+
import java.lang.*;
32+
import java.io.*;
33+
34+
class MaximumArrayXOR
35+
{
36+
public static void main (String[] args) throws java.lang.Exception
37+
{
38+
Scanner sc=new Scanner(System.in);
39+
int t=sc.nextInt();
40+
41+
while(t-->0)
42+
{
43+
long count=0;
44+
long N=sc.nextInt();
45+
long K=sc.nextInt();
46+
47+
//maximum value each cell of the array should have
48+
count=(long)Math.pow(2,N);
49+
50+
//reducing value of K if it is greater than count/2
51+
//count/2 is the maximum number of swaps that can be performed to get the maximum value of Array
52+
if(K>=count/2)
53+
K=count/2;
54+
55+
//finding out the maximum value of Array
56+
count=(count-1)*K*2;
57+
58+
System.out.println(count);
59+
60+
}
61+
}
62+
}
63+
/*
64+
* Sample Input 1
65+
* 3
66+
* 2 0
67+
* 2 1
68+
* 10 100
69+
*
70+
* Sample Output 1
71+
* 0
72+
* 6
73+
* 204600
74+
*
75+
* Time complexity: O(n)
76+
*/

0 commit comments

Comments
 (0)