Zoho Array Questions
Zoho Array Questions
#include<stdio.h>
int main() {
int n;
scanf("%d",&n);
int A[n];
for(int i=0;i<n;i++){
scanf("%d",&A[i]);
}
int k;
scanf("%d",&k);
for(int i=0,j=k-1;j<n;i++,j++){
int max = A[i];
for(int h=i+1;h<=j;h++){
if(max<A[h])
max = A[h];
}
printf("%d ",max);
}
return 0;
}
Question 3: video done
Given an array with n elements print the number of occurrences of
that number each number in that array. The order of number
doesn’t matter. You can reorder the elements.
Example :
Sample Input:
[2,1,3,2,2,5,8,9,8]
Output:
2–3
1–1
3–1
5–1
8–2
9–1
Question 4: Video Done
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
int A[n];
for(int i=0;i<n;i++){
scanf("%d",&A[i]);
}
int threshold;
scanf("%d",&threshold);
//5 8 10 13 6 2
int count=0;//5
for(int i=0;i<n;i++){
int k = A[i];//8
int y = k/threshold;//8/3 = 2
count = count+y;//
int d = k%threshold;//2
if(d!=0){
count=count+1;
}
}
printf("%d",count);
return 0;
}
Question 6: video done
You’re given an array. Print the elements of the array which are
greater than its previous elements in the array.
Input: 2, -3, -4, 5, 9, 7, 8
Output: 2 5 9
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
int A[n];
for(int i=0;i<n;i++){
scanf("%d",&A[i]);
}
Given an array, find the minimum of all the greater numbers for
each element in the array.
Sample:
Array: {2, 3, 7, 1, 11, 5, 8}
Output:
{2>3, 3>5, 7>8, 1>2, 11>, 5>7, 8>11,}
#include<stdio.h>
int main() {
int n;
scanf("%d",&n);
int A[n];
for(int i=0;i<n;i++){
scanf("%d",&A[i]);
}
int max=A[0];
for(int i=0;i<n;i++){
if(max<A[i])
max = A[i];
}
for(int j=0;j<n;j++){
int a = A[j];
int b=max;
for(int i=0;i<n;i++){
if(A[i]>a && A[i]<b){
b = A[i];
}
}
if(a!=b){//Remaining elements
printf("%d>",a);
printf("%d",b);
printf(",");
}
else if(a==b){//This match only at maximum element....
printf("%d>",a);
printf(",");
}
}
return 0;
}
Method:
#include<stdio.h>
int main() {
int n;
scanf("%d",&n);
int A[n];
for(int i=0;i<n;i++){
scanf("%d",&A[i]);
}
//1. Process
int max=A[0];
for(int i=0;i<n;i++){
if(max<A[i])
max = A[i];
}
//2. New array
int B[max+1];
//3.All elements zero
for(int i=0;i<(max+1);i++){
B[i] = 0;
}
//4.Hashing Process
for(int i=0;i<n;i++){
int k = A[i];
B[k]++;
}
//5.Result
for(int i=0;i<n;i++){
int k = A[i];
printf("%d>",k);
int j = k+1;
for(;j<(max+1);j++){
if(B[j]!=0){
printf("%d",j);
break;
}
}
printf(",");
}
return 0;
}
Question 8: Video Done
Find the largest sum contiguous sub array which should not have
negative numbers. We have to print the sum and the corresponding
array elements which brought up the sum.
Input:
Array: {2, 7, -5, 1, 3, 2, 9, -7}
Output:
Sum: 15
Elements: 1, 3, 2, 9
#include<stdio.h>
int main() {
int n;
scanf("%d",&n);
int A[n];
for(int i=0;i<n;i++){
scanf("%d",&A[i]);
}
int y=0;
int index=0;
int sum=0;
int max=0;
int start=0;
int count=0;
for(int i=0;i<n;i++){
if(A[i]>=0){
sum = sum+A[i];
if(count==0){
start = i;
count++;
}
}
else{
if(max<sum){
max = sum;
y = start;
index = i-1;
}
sum=0;
count=0;
}
}
printf("%d\n",max);
for(int i=y;i<=index;i++){
printf("%d ",A[i]);
}
return 0;
}
//5 3 4 7 6 2 5
Question 9: Video done Array Problem 3
Adding 2 numbers
Given 2 huge numbers as separate digits, store them in array and
process them and calculate the sum of 2 numbers and store the
result in an array and print the sum.
Input:
Number of digits: 12
928135673116
Number of digits: 9
784621997
Output:
928920295113
#include<stdio.h>
int main() {
int n;
scanf("%d",&n);
int A[n];
for(int i=0;i<n;i++){
scanf("%d",&A[i]);
}
int N;
scanf("%d",&N);
//Algorithm => Time o(n^2)
for(int i=0;i<(n-1);i++){
int sum=0;
int k=A[i];
sum = sum+k;
int count=0;
for(int j=i+1;j<n;j++){
sum = sum+A[j];
if(N==sum){
printf("true");
count++;
break;
}
}
if(count!=0)
break;
}
return 0;
}
Question 11: Video Done
Given array find maximum sum of contiguous sub array
{-2 -3 4 -1 -2 1 5 -3}
output 7
elements [ 4 -1 -2 1 5]
#include<stdio.h>
int main() {
int n;
scanf("%d",&n);
int A[n];
for(int i=0;i<n;i++){
scanf("%d",&A[i]);
}
int start = 0;
int end = 0;
int max=A[0];
for(int i=0;i<n;i++){
int sum=A[i];
if(max<sum){
max = sum;
start = i;
end = i;
}
for(int j=i+1;j<n;j++){
sum = sum+A[j];
if(max<sum){
max = sum;
start = i;
end = j;
}
}
}
printf("%d\n",max);
for(int h=start;h<=end;h++){
printf("%d ",A[h]);
}
return 0;
}
Question 12: Video Done
Method: 2
#include<stdio.h>
int main() {
int n;
scanf("%d",&n);
int A[n];
for(int i=0;i<n;i++){
scanf("%d",&A[i]);
}
int N;
scanf("%d",&N);
for(int i=0;i<n;i++){
int sum = A[i];
if(A[i]==N){
printf("%d\n",A[i]);
}
else if(A[i]<N){
for(int j=i+1;j<n;j++){
sum = sum+A[j];
if(sum==N){
for(int h=i;h<=j;h++){
printf("%d ",A[h]);
}
printf("\n");
}
}
}
}
return 0;
}
Question 13: Video Done
Input: [-1, 0, 3, 2]
[3, 4, 0, -1, 2]
Output: 4 is the extra element in array 2 at index 1
Program 1:
#include<stdio.h>
int main() {
int n1;
int n2;
scanf("%d%d",&n1,&n2);
int A[n1];
int B[n2];
for(int i=0;i<n1;i++){
scanf("%d",&A[i]);
}
for(int i=0;i<n2;i++){
scanf("%d",&B[i]);
}
if(n1>n2){
for(int i=0;i<n1;i++){
int k = A[i];
int count = 0;
for(int j=0;j<n2;j++){
if(k==B[j]){
count++;
break;
}
}
if(count==0){
printf("%d - %d",k,i);
break;
}
}
}
else{
for(int i=0;i<n2;i++){
int k = B[i];
int count = 0;
for(int j=0;j<n1;j++){
if(k==A[j]){
count++;
break;
}
}
if(count==0){
printf("%d - %d",k,i);
break;
}
}
}
return 0;
}
Program:2
#include<stdio.h>
int main() {
int n1;
int n2;
scanf("%d%d",&n1,&n2);
int A[n1];
int B[n2];
for(int i=0;i<n1;i++){
scanf("%d",&A[i]);
}
for(int i=0;i<n2;i++){
scanf("%d",&B[i]);
}
int min1=A[0];
for(int i=0;i<n1;i++){
if(min1>A[i])
min1=A[i];
}
int min2=B[0];
for(int i=0;i<n2;i++){
if(min2>B[i])
min2=B[i];
}
int min=0;
if(min1<min2)
min = min1;
else
min = min2;
int y = -min;
int max1=A[0];
for(int i=0;i<n1;i++){
if(max1<A[i])
max1=A[i];
}
int max2=B[0];
for(int i=0;i<n2;i++){
if(max2<B[i])
max2=B[i];
}
int max = 0;
if(max1>max2)
max = max1;
else
max = max2;
int C[max+1+y];
for(int i=0;i<(max+1+y);i++){
C[i]=0;
}
for(int i=0;i<n1;i++){
int k = A[i];
C[k+y]++;
}
for(int i=0;i<n2;i++){
int k = B[i];
C[k+y]++;
}
int element = 0;
for(int i=0;i<(max+y+1);i++){
if(C[i]==1){
element = i-y;
break;
}
}
printf("%d",element);
if(n1>n2){
for(int i=0;i<n1;i++){
if(element==A[i]){
printf(" is the extra element in array 1 at index %d",i);
}
}
}
else{
for(int i=0;i<n2;i++){
if(element==B[i]){
printf(" is the extra element in array 2 at index %d\n",i);
}
}
}
return 0;
}
Question 21: Video Done
Find the least prime number that can be added with first array
element that makes them divisible by second array elements at
respective index (check for prime numbers under 1000, if not exist
return -1 as answer) & (Consider 1 as prime number)
Input: [20, 7]
[11, 5]
Output: [2, 3]
Explanation:
(20 + ?) % 11 =>(20+2%11)=> 22%11 = 0
( 7 + ?) % 5 => (7+3)%5 = 0
Program:
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
int A[n];
int B[n];
for(int i=0;i<n;i++){
scanf("%d",&A[i]);
}
for(int i=0;i<n;i++){
scanf("%d",&B[i]);
}
for(int i=0;i<n;i++){
if((A[i]+1)%B[i]==0)
printf("1 ");
else if((A[i]+2)%B[i]==0)
printf("2 ");
else{
for(int j=3;j<1001;j++){
int count=0;
for(int k=2;k<j;k++){
if(j%k==0){//it works means the number is not a prime
number
count++;
break;
}
}
if(count==0){
if(((A[i]+j)%B[i])==0){
printf("%d ",j);
}
break;
}
}
}
}
return 0;
}
#include<stdio.h>
int main() {
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
int min=0;
for(int i=0;i<n;i++){//finding minimum
if(a[i]<min){
min=a[i];
}
}
int y = -min;
int max=0;
for(int i=0;i<n;i++){//finding maximum
if(max<a[i]){
max=a[i];
}
}
int b[max+y+1];//new array declaration...
Sort the array alternately i.e first element should be max value,
second min value, third second max, third second min.
Eg:
arr[] = {1,2,3,4,5,6,7}
O/P: {7,1,6,2,5,3,4}
Note: no extra space and time complexity should be less
Already solved it
Find the union intersection of two list and also find except (remove
even elements from list1 and odd elements from list2)
Input
List 1: 1,3,4,5,6,8,9
List 2: 1,5,8,9,2
Output:
Union: 1,3,4,5,6,8,9,2
Intersection: 1,5,8,9
Except: 1,3,5,9,8,2
Question 25: Video Done
Input:
n=8
43567819
k=2
Output:
43195678
Input 2:
n = 11
43567819721
k=3
Output 2:
21435197678
#include<stdio.h>
int main() {
int n;
scanf("%d",&n);
int A[n];
for(int i=0;i<n;i++){
scanf("%d",&A[i]);
}
int k;
scanf("%d",&k);
int z = n%k;
for(int i=n-z;i<n;i++){
printf("%d ",A[i]);
}
int h = (n/k)/2;
int s1=0;int e1=k-1;
int s2=n-k-z; int e2=n-1-z;
while(h){
for(int i=s1;i<=e1;i++){
printf("%d ",A[i]);
}
for(int i=s2;i<=e2;i++){
printf("%d ",A[i]);
}
s1=s1+k;e1=e1+k;
s2=s2-k;e2=e2-k;
h--;
}
if(e1==e2 && s1==s2){
for(int i=s1;i<=e1;i++){
printf("%d ",A[i]);
}
}
return 0;
}
//5 2 4 3 5 7 8 1 2 6 5
Question 26: Video Done (We can use the duplicate videos
hashing technique)
Already solved it
Question 27: Video Done
Already solved it
Note: You must use the same array, cannot create the other array.
Already solved
#include<stdio.h>
int main() {
int n;
scanf("%d",&n);
int A[n];
for(int i=0;i<n;i++){
scanf("%d",&A[i]);
}
int k;
scanf("%d",&k);
int y = n%k;
for(int i=n-y,j=n-1;i<j;i++,j--){
int k = A[j];
A[j] = A[i];
A[i] = k;
}
int h = n/k;
int i=0; int j=k-1;
while(h){
for(int d=i,g=j;d<g;d++,g--){
int k = A[g];
A[g] = A[d];
A[d] = k;
}
i = i+k;
j = j+k;
h--;
}
for(int i=0;i<n;i++){
printf("%d ",A[i]);
}
return 0;
}