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:
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:
#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:
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]);
}
return 0;
}
Question 7:
Given an array, find the minimum of all the greater numbers for
each element in the array.
Sample:
Array: {2, 3, 7, 1, 8, 5, 11}
Output:
{2>3, 3>5, 7>8, 1>2, 8>11, 5>7, 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:
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:
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 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]);
}
//Algorithm for this problem
long sum1=0;
for(int i=0;i<n1;i++){//First array appending process
sum1 = (sum1*10)+A[i];
}
long sum2=0;
for(int i=0;i<n2;i++){///Second array appending process
sum2 = (sum2*10)+B[i];
}
long sum3 = sum1+sum2;//Addition of both values
long m = sum3;//assigning the sum3 to m vavraible
int digit=0;
while(m){//Finding the no. of digits in the result
digit++;
m=m/10;
}
printf("%d\n",digit);
int C[digit];//Declaring the new with digit as the size...
int j=digit-1;
while(sum3){//Taking the remainder value and place in the array
C in reverse
int y = sum3%10;
C[j] = y;
j--;
sum3 = sum3/10;
}
for(int i=0;i<digit;i++){//Print the array C
printf("%d ",C[i]);
}
return 0;
}
//9 2 8 9 2 0 2 9 5 1 1 3
Question 10:
#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:
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:
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:
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;
}
Question 22:
#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:
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:
Already solved it
Question 27:
Find the occurrence of a number and display the values in
ascending order of the given input
Input: 3 4 3 4 1 2 3 1 2
Output: 2 2 3 2
Explanation:
1 = 2 times
2 = 2 times
3 = 3 times
4 = 2 times
Output: 2 2 3 2
Already solved it
Question 28:
Question 29:
Input:
8
45367287
3
Output:
35427678
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;
}