Integers - Divide and Add Remainders

 Integers - Divide and Add Remainders

PROBLEM STATEMENT :

The program must accept N integers and an integer K. Starting from the 1st element to the Kth element, the program must divide the integers starting from the K+1th element and add the respective remainders to the array elements. The program must finally print the revised array values.

Boundary Condition(s):
1 <= K < N <= 100
1 <= Each integer value <= 10^5

Input Format:
The first line contains N.
The second line contains N integer values separated by a space.
The third line contains K.

Output Format:
The first line contains the revised array values separated by a space.


Example Input/Output 1: 
Input: () 
5
10 24 61 78 90
2 
Output: 10 24 76 100 108

Explanation: 

Here K=2.
After dividing by 10 and adding the remainders the array becomes
10 24 62 86 90.

After dividing by 24 and adding the remainders the array becomes
10 24 76 100 108.


Example Input/Output 2: 
Input: () 
6
12 49 86 57 18 63
3 
Output: 12 49 86 166 96 166




                    


            1)    LEARN THRICE 

                                👇 

            2)    THINK TWICE

                                👇 

            3)    APPLY ONCE




SOLUTION :

C (Programming Language)


      

#include<stdio.h> #include<stdlib.h> int main() { int n,i,j,k; scanf("%d",&n); int a[n]; for(i=0;i<n;++i) { scanf("%d",&a[i]); } scanf("%d",&k); for(i=0;i<k;++i) { for(j=k;j<n;++j) { a[j]+=a[j]%a[i]; } } for(i=0;i<n;++i) { printf("%d ",a[i]); } }



C++ (CPP)

       

#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { int n,i,j,k; cin>>n; int a[n]; for(i=0;i<n;++i) { cin>>a[i]; } cin>>k; for(i=0;i<k;++i) { for(j=k;j<n;++j) { a[j]+=a[j]%a[i]; } } for(i=0;i<n;++i) { cout<<a[i]<<" "; } }



JAVA

       

import java.util.*; public class Hello { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(),i,j,k; int[] a=new int[n]; for(i=0;i<n;++i) { a[i]=sc.nextInt(); } k=sc.nextInt(); for(i=0;i<k;++i) { for(j=k;j<n;++j) { a[j]+=a[j]%a[i]; } } for(i=0;i<n;++i) { System.out.print(a[i]+" "); } } }



PYTHON

       

n=int(input()) l=list(map(int,input().split())) k=int(input()) for i in l[:k]: for j in range(k,n): l[j]+=(l[j]%i) print(*l)



Never Stop Learning !!


Comments

Popular posts from this blog

DP (1) - Count number of ways to cover a distance

Zero Insert After K Times One

Left Number Twice Right